diff --git a/bin/mw b/bin/mw index 6f2934f..8462d8b 100755 --- a/bin/mw +++ b/bin/mw @@ -1,10 +1,11 @@ #!/bin/sh -muttdir="$HOME/.config/mutt" # Main mutt config location [ ! -f "$HOME/.password-store/.gpg-id" ] && printf "\`pass\` must be installed and initialized to encrypt passwords.\\nBe sure it is installed and run \`pass init \`.\\nIf you don't have a GPG public private key pair, run \`gpg --full-gen-key\` first.\\n" && exit +muttdir="$HOME/.config/mutt" # Main mutt config location accdir="$muttdir/accounts" # Directory for account settings maildir="$HOME/.local/share/mail" # Location of mail storage namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username emailre=".\+@.\+\\..\+" # Regex to confirm valid email address +muttshare="/usr/share/mutt-wizard" mbsyncrc="$HOME/.mbsyncrc" sslcert="/etc/ssl/certs/ca-certificates.crt" @@ -66,7 +67,7 @@ bind index,pager g noop bind index,pager M noop bind index,pager C noop bind index gg first-entry -macro index o \"$HOME/.config/mutt/bin/mailsync -V $title\" \"run mbsync to sync $title\" +macro index o \"mailsync -V $title\" \"run mbsync to sync $title\" unmailboxes * " else @@ -98,7 +99,7 @@ fi printf "DONE.\\n" } -addaccount() { \ +askinfo() { \ printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m" read -r fulladdr printf "\033[0m" @@ -109,7 +110,7 @@ addaccount() { \ done domain="$(echo "$fulladdr" | sed "s/.*@//")" printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain" - serverinfo="$(grep "$domain" "$muttdir/domains.csv" 2>/dev/null)" + serverinfo="$(grep "$domain" "$muttshare/domains.csv" 2>/dev/null)" if [ -z "$serverinfo" ]; then printf "Your email domain is not in mutt-wizard's database yet.\\nmutt-wizard will still autoconfigure everything, but you will have to manually type in your service's IMAP and SMTP server information.\\nYou can usually quickly find this by internet searching for it.\\n" printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t" @@ -155,9 +156,8 @@ EOF echo "$msmtp_profile" >> "$HOME/.config/msmtp/config" echo "$mutt_profile" > "$accdir/$title.muttrc" echo "$mbsync_profile" >> "$mbsyncrc" - echo "macro index,pager i$idnum 'source $accdir/$title.muttrc!;' \"switch to $fulladdr\"" >> "$muttdir/personal.muttrc" ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc" - trysync && finalize + echo "macro index,pager i$idnum 'source $accdir/$title.muttrc!;' \"switch to $fulladdr\"" >> "$muttdir/personal.muttrc" } getpass() { while : ; do pass rm -f "mutt-wizard-$title" >/dev/null 2>&1 @@ -169,7 +169,7 @@ formatShortcut() { \ echo "macro index,pager C$1 \"$data\" \"copy mail to $2\" # autogenerated"; } >> "$accdir/$3.muttrc" done ;} -trysync() { ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning \`mbsync %s\` manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" "$title" && return 1 +tryconnect() { ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning \`mbsync %s\` manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" "$title" && return 1 mkdir -p "$maildir/$title" if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1; then [ "$accounttype" = "online" ] && sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" @@ -178,8 +178,7 @@ trysync() { ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection else printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or sever settings, or there are other requirements for your account out of the control of mutt-wizard.\\n" delete "$title"; return 1 - fi -} + fi ;} finalize() { \ boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")" @@ -210,7 +209,7 @@ finalize() { \ [ "$accounttype" = "offline" ] && printf "All done.\\n\033[33mYou should now be able to run \`\033[32mmbsync %s\033[33m\` to begin to download your mail.\033[0m\\n" "$title" } -confirm() { printf "Do you want to to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1 +confirm() { printf "Do you want to to %s? [y/N]\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1 printf "Are you really, really sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1 return 0 ;} @@ -249,58 +248,34 @@ choosecron() { ! pgrep crond >/dev/null && echo "No cron manager running. Instal read -r minnum printf "\033[0m" done - (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus; export DISPLAY=:0; . \$HOME/.profile; $muttdir/bin/mailsync") | crontab - && + (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus; export DISPLAY=:0; . \$HOME/.profile; $(type mailsync | cut -d' ' -f3)") | crontab - && echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running." fi ;} -addtype() { ! command -v mbsync >/dev/null && printf "You must have the \`mbsync\` command to add an account.\\nIt syncs mail and detects remote mailboxes.\\nYou can typically get it by installing \`isync\`.\\n" && return 1 - while : ; do +asktype() { while : ; do printf "Do you want to keep your mail for this account offline with mbsync? [yes/no]\\n\t" read -r offnot case "$offnot" in [Yy][Ee][Ss]) accounttype="offline" && break ;; [Nn][Oo]) accounttype="online" && break ;; *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;; - esac - done - addaccount ;} + esac; done ;} -main() { while : ; do - printf "Welcome to:\\n\033[34m" -cat << "EOF" - _ __ ___ _ _| |_| |_ __ _(_)______ _ _ __ __| | -| '_ ` _ \| | | | __| __|___\ \ /\ / / |_ / _` | '__/ _` | -| | | | | | |_| | |_| ||_____\ V V /| |/ / (_| | | | (_| | -|_| |_| |_|\__,_|\__|\__| \_/\_/ |_/___\__,_|_| \__,_| -EOF +init() { \ + echo "Initializing a mutt-wizard configuration..." + [ -f "$muttdir/muttrc" ] && echo "There is already a preexisting config." && return 1 + cp -v "$muttshare/mailcap" "$muttdir" + cp -v "$muttshare/muttrc" "$muttdir" +} - printf "\033[0mMade and maintained by Luke Smith +case "$1" in + init) init ;; + new) asktype && askinfo && tryconnect && finalize ;; + passedit) pick "change the password of" && confirm "change \`$title\`'s password" && getpass ;; + delete) pick delete && confirm "delete the \`$title\` profile" && delete ;; + purge) confirm "delete all account data" && rm -rf "$mbsyncrc" "$accdir" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged." ;; + cron) choosecron ;; + *) printf "Invalid input.\\n" +esac -What would you like \033[32mmutt-wizard\033[0m to do? - 1 Add an email account - 2 Change an account's password - 3 Remove an account - 4 Purge all account data - 5 Enable/disable autosync - 0 Exit -Input a number to continue or press ctrl-c.\\n" - printf "\033[36m\t" - read -r choice - printf "\033[0m" - case "$choice" in - 1) addtype ;; - 2) pick "change the password of" && confirm "change \`$title\`'s password" && getpass ;; - 3) pick delete && confirm "delete the \`$title\` profile" && delete ;; - 4) confirm "delete all account data" && rm -rf "$mbsyncrc" "$accdir" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged." ;; - 5) choosecron ;; - 0) break ;; - *) printf "Invalid input.\\n" - esac - printf "Press Enter to continue.\\n" - stty -echo - read -r null - stty echo -done ;} - -main rm -rf "$tmpdir" diff --git a/muttrc b/muttrc index e1a54a8..3598f2a 100644 --- a/muttrc +++ b/muttrc @@ -56,7 +56,7 @@ bind editor complete-query macro index,pager a "|abook --add-email\n" 'add sender to abook' macro index \Cr "T~UN." "mark all messages as read" -macro index O "$HOME/.config/mutt/bin/mailsync -Va" "run offlineimap to sync all mail" +macro index O "mailsync -Va" "run offlineimap to sync all mail" macro index \Cf "unset wait_keyread -p 'Enter a search term to find with notmuch: ' x; echo \$x >~/.cache/mutt_terms~i \"\`notmuch search --output=messages \$(cat ~/.cache/mutt_terms) | head -n 600 | perl -le '@a=<>;chomp@a;s/\^id:// for@a;$,=\"|\";print@a'\`\"" "show only messages matching a notmuch pattern" macro index A "all\n" "show all messages (undo limit)"