You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

370 lines
16 KiB

  1. #!/bin/sh
  2. if [ "$#" -gt 1 ]; then
  3. echo "To many arguments. You will be asked."
  4. exit
  5. fi
  6. if [[ -z $prefix ]]; then
  7. case "$(uname)" in
  8. Linux) prefix="/usr" ;;
  9. *) prefix="/usr/local" ;;
  10. esac
  11. fi
  12. command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  13. [ -z $PASSWORD_STORE_DIR ] && PASSWORD_STORE_DIR="$HOME/.password-store"
  14. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  15. "$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || {
  16. printf "\`pass\` must be installed and initialized to encrypt passwords.\\nBe sure it is installed and run \`pass init <yourgpgemail>\`.\\nIf you don't have a GPG public private key pair, run \`$GPG --full-gen-key\` first.\\n"
  17. exit
  18. }
  19. ! command -v $prefix/bin/mbsync >/dev/null && printf "\`mbsync\` must be installed to run mutt-wizard.\\n" && exit
  20. ! command -v $prefix/bin/msmtp >/dev/null && printf "\`msmtp\` must be installed.\\n" && exit
  21. ! ( command -v mutt >/dev/null || command -v neomutt >/dev/null ) && printf "\`mutt\` must be installed.\\n" && exit
  22. mwconfigdir=${XDG_CONFIG_HOME:-$HOME/.config}
  23. # Main mutt config location
  24. mwmuttdir="$mwconfigdir/mutt"
  25. # Directory for account settings
  26. mwaccdir="$mwmuttdir/accounts"
  27. # Location of mail storage
  28. mwmaildir="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}"
  29. # Regex to confirm valid email address
  30. mwemailre=".\+@.\+\\..\+"
  31. mwshare="$prefix/share/mutt-wizard"
  32. if [[ -n $XDG_CONFIG_HOME ]]; then
  33. mwmbsyncrc="$mwconfigdir/isync/mbsyncrc"
  34. else
  35. mwmbsyncrc="$HOME/.mbsyncrc"
  36. fi
  37. mwconfig="$mwshare/mutt-wizard.muttrc"
  38. mwcachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  39. mwmuttrc="$mwmuttdir/muttrc"
  40. mwmsmtprc="$mwconfigdir/msmtp/config"
  41. mwssltype="IMAPS"
  42. mbsyncbin="$prefix/bin/mbsync -c $mwmbsyncrc"
  43. msmtpbin="$prefix/bin/msmtp"
  44. for x in "/etc/ssl/certs/ca-certificates.crt" "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" "/etc/ssl/cert.pem" "/usr/local/share/ca-certificates/"
  45. do
  46. [ -f "$x" ] && sslcert="$x" && break
  47. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  48. getaccounts() { accounts="$(find "$mwaccdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\..*//" | sort -n)" ;}
  49. mwlist() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  50. mwadd() { asktype && askinfo && tryconnect && finalize || mwdelete ;}
  51. getprofiles() { \
  52. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  53. printf "Creating profiles for \`%s\`..." "$mwaccount"
  54. msmtp_header="defaults
  55. auth on
  56. tls on
  57. tls_trust_file $sslcert
  58. logfile ${XDG_LOG_HOME:-$HOME}/msmtp.log
  59. "
  60. msmtp_profile="account $mwaccount
  61. host $mwsmtp
  62. port $mwsport
  63. from $mwfulladdr
  64. user $mwlogin
  65. passwordeval \"pass mutt-wizard-$mwaccount\"
  66. $starttlsoff
  67. "
  68. mbsync_profile="IMAPStore $mwaccount-remote
  69. Host $mwimap
  70. Port $mwiport
  71. User $mwlogin
  72. PassCmd \"pass mutt-wizard-$mwaccount\"
  73. SSLType $mwssltype
  74. CertificateFile $sslcert
  75. MaildirStore $mwaccount-local
  76. Subfolders Verbatim
  77. Path ~/.local/share/mail/$mwaccount/
  78. Inbox ~/.local/share/mail/$mwaccount/INBOX
  79. Flatten .
  80. Channel $mwaccount
  81. Expunge Both
  82. Master :$mwaccount-remote:
  83. Slave :$mwaccount-local:
  84. Patterns * !\"[Gmail]/All Mail\"
  85. Create Both
  86. SyncState *
  87. MaxMessages $mwmaxmes
  88. # End profile
  89. "
  90. if [ "$mwtype" = "offline" ]; then
  91. mutt_profile="# vim: filetype=neomuttrc
  92. # muttrc file for account $mwaccount
  93. set realname = \"$mwrealname\"
  94. set from = \"$mwfulladdr\"
  95. set sendmail = \"$msmtpbin -a $mwaccount\"
  96. alias me $mwrealname <$mwfulladdr>
  97. set folder = \"$mwmaildir/$mwaccount\"
  98. set mbox_type = Maildir
  99. macro index gm \"<shell-escape>mailsync -V $mwaccount<enter>\" \"sync mail $mwaccount\"
  100. unmailboxes *
  101. "
  102. else
  103. mutt_profile="# vim: filetype=neomuttrc
  104. # muttrc file for account $mwaccount
  105. set realname = \"$mwrealname\"
  106. set from = \"$mwfulladdr\"
  107. set sendmail = \"$msmtpbin -a $mwaccount\"
  108. alias me $mwrealname <$mwfulladdr>
  109. set folder = \"imaps://$mwfulladdr@$mwimap:$mwiport\"
  110. set imap_user = \"$mwlogin\"
  111. set header_cache = $mwcachedir/$mwaccount/headers
  112. set message_cachedir = $mwcachedir/$mwaccount/bodies
  113. set imap_pass = \"\`pass mutt-wizard-$mwaccount\`\"
  114. set mbox_type = Maildir
  115. set ssl_starttls = yes
  116. set ssl_force_tls = yes
  117. unmailboxes *
  118. "
  119. fi
  120. printf "DONE.\\n"
  121. }
  122. askinfo() { \
  123. if [ -z $mwfulladdr ]; then
  124. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  125. read -r mwfulladdr
  126. printf "\033[0m"
  127. while ! echo "$mwfulladdr" | grep "$mwemailre" >/dev/null; do
  128. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  129. read -r mwfulladdr
  130. printf "\033[0m"
  131. done
  132. fi
  133. domain="$(echo "$mwfulladdr" | sed "s/.*@//")"
  134. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  135. mwserverinfo="$(grep "^$domain" "$mwshare/domains.csv" 2>/dev/null)"
  136. if [ -z "$mwserverinfo" ]; then
  137. 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"
  138. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  139. read -r mwimap
  140. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  141. read -r mwiport
  142. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  143. read -r mwsmtp
  144. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  145. read -r mwsport
  146. printf "\033[0m\\nGreat! If you want to be helpful, copy the line below and you can add it to the \`domains.csv\` file on Github.\\nThis will make things easier for others who use your email provider.\\n\\n%s,%s,%s,%s,%s\\n\\nAlthough be sure to test to see if these settings work first! ;-)\\n" "$domain" "$mwimap" "$mwiport" "$mwsmtp" "$mwsport"
  147. else
  148. IFS=, read -r mwservice mwimap mwiport mwsmtp mwsport <<EOF
  149. $mwserverinfo
  150. EOF
  151. printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically been found, so you won't have to look anything up!\\n\t\033[1mIMAP server\033[0m: %s\\n\t\033[1mIMAP port\033[0m: %s\\n\t\033[1mSMTP server\033[0m: %s\\n\t\033[1mSMTP port\033[0m: %s\\nThis data will be used by the wizard.\\n" "$imap" "$mwiport" "$mwsmtp" "$mwsport"
  152. case "$mwservice" in
  153. gmail.com) printf "\033[31mREMEMBER: Gmail users must enable \"less secure\" (third-party) applications first for the sync to work:\\nhttps://support.google.com/accounts/answer/6010255\\n\033[0m" ;;
  154. protonmail.ch|protonmail.com|pm.me) printf "\033[31mREMEMBER: Protonmail users must install and configure Protonmail Bridge first for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" && ssltype="None" ;;
  155. esac
  156. [ "$mwsport" = 465 ] && starttlsoff="tls_starttls off"
  157. fi
  158. if [ -z $mwrealname ]; then
  159. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  160. read -r mwrealname
  161. fi
  162. mwaccount=$mwfulladdr
  163. if [ -z $mwlogin ]; then
  164. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\033[34mMost accounts will not have a separate login, so you should probably leave this blank.\033[0m\\n\tLogin(?): \033[36m"
  165. read -r mwlogin
  166. printf "\033[0m"
  167. [ -z "$mwlogin" ] && mwlogin="$mwfulladdr"
  168. fi
  169. if [ "$mwtype" = "offline" ] && [ -z "$mwmaxmes" ]; then
  170. printf "If you want to limit the number of messages kept offline to a number, enter that number below. If you do not want to limit your mail and would like \`mbsync\` to sync all mail, press enter without typing a number.\\n\t"
  171. read -r mwmaxmes
  172. fi
  173. echo "$mwmaxmes" | grep "[1-9]" >/dev/null || mwmaxmes="0"
  174. getpass
  175. getprofiles
  176. mkdir -p "$mwmuttdir" "$mwaccdir" "$mwcachedir/$mwaccount/bodies" "$mwconfigdir/msmtp"
  177. getaccounts
  178. for x in $(seq 1 9); do echo "$accounts" | grep "$x" >/dev/null 2>&1 || { export idnum="$x"; break ;}; done
  179. [ ! -f "$mwmsmtprc" ] && echo "$msmtp_header" > "$mwmsmtprc"
  180. echo "$msmtp_profile" >> "$mwmsmtprc"
  181. case "$mwservice" in
  182. protonmail.ch|protonmail.com|pm.me) protonfinger || return 1 ;;
  183. esac
  184. echo "$mutt_profile" > "$mwaccdir/$idnum-$mwaccount.muttrc"
  185. mkdir -p ${mwmbsyncrc%/*}
  186. echo "$mbsync_profile" >> "$mwmbsyncrc"
  187. [ ! -f "$mwmuttrc" ] && echo "# vim: filetype=neomuttrc" > "$mwmuttrc" && echo "muttrc created."
  188. ! grep "^source.*mutt-wizard.muttrc" "$mwmuttrc" >/dev/null && echo "source $mwconfig # mw-autogenerated" >> "$mwmuttrc"
  189. ! grep "^source.*.muttrc" "$mwmuttrc" | grep -v "$mwconfig" >/dev/null && echo "source $mwaccdir/$idnum-$mwaccount.muttrc # mw-autogenerated" >> "$mwmuttrc"
  190. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $mwaccdir/$idnum-$mwaccount.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $mwfulladdr\" # mw-autogenerated" >> "$mwmuttrc"
  191. [ "$mwtype" = "offline" ] && echo "macro index gM '<shell-escape>mailsync -Va<enter>' \"sync all mail\" # mw-autogenerate" >> "$mwmuttrc"
  192. }
  193. protonfinger() { printf "Getting Protonmail bridge fingerprint...\\n"
  194. fingerprint="$($msmtpbin --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off)" || return 1
  195. sed -i "s/account $mwaccount/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$mwmsmtprc"
  196. }
  197. getpass() { while : ; do pass rm -f "mutt-wizard-$mwaccount" >/dev/null 2>&1
  198. pass insert "mutt-wizard-$mwaccount" && break; done ;}
  199. formatShortcut() { \
  200. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # mw-autogenerated"
  201. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
  202. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$mwaccdir/$idnum-$mwaccount.muttrc"
  203. done ;}
  204. tryconnect() { \
  205. [ -z "$mailboxes" ] && mailboxes="$($mbsyncbin -l $mwaccount | sed 's/\//./')" >/dev/null 2>&1
  206. if [ -n "$mailboxes" ]; then
  207. [ "$mwtype" = "online" ] && sed -i "/IMAPStore $mwaccount-remote$/,/# End profile/d" "$mwmbsyncrc"
  208. [ "$mwtype" = "offline" ] && notmuchauto
  209. printf "\033[32mMailboxes detected.\033[0m\\n"
  210. echo "$mailboxes" | xargs -I {} mkdir -p "$mwmaildir/$mwaccount/{}"
  211. return 0
  212. else
  213. printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or server settings, or there are other requirements for your account out of the control of mutt-wizard.\\n"
  214. return 1
  215. fi ;}
  216. finalize() { \
  217. boxes="$(find "$mwmaildir/$mwaccount/" -mindepth 1 -maxdepth 1 | sed "s/\ /\\\ /g;s/^.*\//=/")"
  218. [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that mbsync has not been successfully run.\\nRun mbsync, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$mwaccount" && return
  219. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  220. spoolfile=$(echo "$boxes" | grep -i -m 1 inbox | sed 's/=/+/g')
  221. record=$(echo "$boxes" | grep -i -m 1 sent | sed 's/=/+/g')
  222. postponed=$(echo "$boxes" | grep -i -m 1 draft | sed 's/=/+/g')
  223. trash=$(echo "$boxes" | grep -i -m 1 trash | sed 's/=/+/g')
  224. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$mwaccdir/$idnum-$mwaccount.muttrc"
  225. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$mwaccdir/$idnum-$mwaccount.muttrc"
  226. echo "mailboxes =$mwaccount ===================== $(echo "$boxes" | sed -e "s/^\|$/\"/g" | tr "\n" " ")" >> "$mwaccdir/$idnum-$mwaccount.muttrc"
  227. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  228. sed -i "/# mw-autogenerated/d" "$mwaccdir/$idnum-$mwaccount.muttrc"
  229. echo "$boxes" | grep -i inbox | head -n 1 | formatShortcut i inbox
  230. echo "$boxes" | grep -i sent | head -n 1 | formatShortcut s sent
  231. echo "$boxes" | grep -i draft | head -n 1 | formatShortcut d drafts
  232. echo "$boxes" | grep -i trash | head -n 1 | formatShortcut t trash
  233. echo "$boxes" | grep -i spam | head -n 1 | formatShortcut S spam
  234. echo "$boxes" | grep -i junk | head -n 1 | formatShortcut j junk
  235. echo "$boxes" | grep -i archive | head -n 1 | formatShortcut a archive
  236. [ "$mwtype" = "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" "$mwaccount"
  237. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  238. return 0
  239. }
  240. confirm() { printf "Do you want 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
  241. 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
  242. return 0 ;}
  243. mwpick() { printf "Select an accounts to %s:\\n" "$1"
  244. mwlist
  245. read -r input
  246. [ -z "$input" ] && return 1
  247. mwaccount="$(echo "$accounts" | grep "$input" | awk '{print $2}')"
  248. [ -z "$mwaccount" ] && printf "Invalid response." && return 1
  249. return 0 ;}
  250. mwdelete() { sed -i "/IMAPStore $mwaccount-remote$/,/# End profile/d" "$mwmbsyncrc"
  251. rm -rf "${mwcachedir:?}/${mwaccount:?}" "$mwaccdir/"[1-9]"-$mwaccount.muttrc"
  252. sed -i "/[0-9]-$mwaccount.muttrc/d" "$mwmuttrc"
  253. sed -i "/account $mwaccount/,/^\(\s*$\|account\)/d" "$mwmsmtprc"
  254. }
  255. mwcron() { ! pgrep cron >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  256. if crontab -l | grep mailsync >/dev/null; then
  257. echo "Active mail sync cronjob detected. Do you want to remove it?"
  258. printf "\033[36m\t"
  259. read -r rmyn
  260. printf "\033[0m"
  261. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  262. else
  263. echo "How many minutes between each mail sync?"
  264. printf "\033[36m\t"
  265. read -r minnum
  266. printf "\033[0m"
  267. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  268. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  269. read -r minnum
  270. printf "\033[0m"
  271. done
  272. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; $(type mailsync | cut -d' ' -f3)") | crontab - &&
  273. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  274. fi ;}
  275. asktype() { \
  276. if [ -z "$mwtype" ]; then
  277. while : ; do
  278. printf "Offline mail via mbsync? No: Mutt itself will sync to its private cache on demand (slower)? [yes/no]\\n\t"
  279. read -r offnot
  280. case "$offnot" in
  281. [Yy][Ee][Ss]) mwtype="offline" && break ;;
  282. [Nn][Oo]) mwtype="online" && break ;;
  283. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  284. esac
  285. done
  286. fi
  287. }
  288. mwpurge() { confirm "delete all account data" || exit
  289. rm -rf "$mwmbsyncrc" "$mwaccdir" "$mwconfigdir/msmtp" "$mwcachedir"
  290. crontab -l | sed '/mailsync/d' | crontab - >/dev/null
  291. echo "All configs and account settings have been purged."
  292. sed -i "/\# mw-autogenerated/d" "$mwmuttrc"
  293. }
  294. notmuchauto() { \
  295. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  296. [ -f "$NOTMUCH_CONFIG" ] && return 0
  297. nmbasic="[database]
  298. path=$mwmaildir
  299. [user]
  300. name=$mwrealname
  301. primary_email=$mwfulladdr
  302. [new]
  303. tags=unread;inbox;
  304. ignore=
  305. [search]
  306. exclude_tags=deleted;spam;
  307. [maildir]
  308. synchronize_flags=true
  309. [crypto]
  310. gpg_path=$GPG"
  311. mkdir -p ${NOTMUCH_CONFIG%/*}
  312. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  313. trap 'echo -e "\033[0m\n"; exit' STOP INT ABRT KILL
  314. if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  315. case "$1" in
  316. ls) mwlist ;;
  317. add) mwadd ;;
  318. pass) mwpick "change the password of" && getpass ;;
  319. delete) mwpick delete && confirm "delete the \`$mwaccount\` profile" && mwdelete ;;
  320. purge) mwpurge ;;
  321. cron) mwcron ;;
  322. *) cat << EOF
  323. mw: mutt-wizard, auto-configure email accounts for mutt
  324. including downloadable mail with \`isync\`.
  325. Allowed options:
  326. add Add and autoconfigure an email address (9 max.)
  327. ls List configured accounts
  328. delete Pick an account to delete
  329. purge Delete all accounts and settings
  330. cron Enable or disable an autosync via cronjob
  331. all else Print this message
  332. NOTE: Once at least one account is added, you can run
  333. \`mailsync -a\` to begin downloading mail.
  334. EOF
  335. esac
  336. fi