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.
 
 
 
 

367 lines
13 KiB

  1. #!/bin/sh
  2. prefix="/usr/local"
  3. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail"
  4. muttshare="$prefix/share/mutt-wizard"
  5. mwconfig="$muttshare/mutt-wizard.muttrc"
  6. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  7. muttrc="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/muttrc"
  8. accdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/accounts"
  9. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  10. msmtplog="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log"
  11. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  12. alias mbsync='mbsync -c "$mbsyncrc"'
  13. 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/"; do
  14. [ -f "$x" ] && sslcert="$x" && break
  15. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  16. checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  17. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  18. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  19. "$GPG" --list-secret-keys "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" >/dev/null 2>&1 || {
  20. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  21. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  22. exit 1 ;} ;}
  23. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc$//" | sort -n)" ;}
  24. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  25. getprofiles() { \
  26. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  27. case "$iport" in
  28. 1143) imapssl=None ;;
  29. 143) imapssl=STARTTLS ;;
  30. esac
  31. msmtp_header="defaults
  32. auth on
  33. tls on
  34. tls_trust_file $sslcert
  35. logfile $msmtplog
  36. "
  37. msmtp_profile="account $fulladdr
  38. host $smtp
  39. port ${sport:-587}
  40. from $fulladdr
  41. user $login
  42. passwordeval \"pass $fulladdr\"
  43. $tlsline
  44. "
  45. mbsync_profile="
  46. IMAPStore $fulladdr-remote
  47. Host $imap
  48. Port ${iport:-993}
  49. User $login
  50. PassCmd \"pass $fulladdr\"
  51. AuthMechs LOGIN
  52. SSLType ${imapssl:-IMAPS}
  53. CertificateFile $sslcert
  54. MaildirStore $fulladdr-local
  55. Subfolders Verbatim
  56. Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/
  57. Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
  58. Flatten _
  59. Channel $fulladdr
  60. Expunge Both
  61. Master :$fulladdr-remote:
  62. Slave :$fulladdr-local:
  63. Patterns * !\"[Gmail]/All Mail\"
  64. Create Both
  65. SyncState *
  66. MaxMessages ${maxmes:-0}
  67. ExpireUnread no
  68. # End profile
  69. "
  70. if [ -z "${online+x}" ]; then
  71. mutt_profile="# vim: filetype=neomuttrc
  72. # muttrc file for account $fulladdr
  73. set realname = \"$realname\"
  74. set from = \"$fulladdr\"
  75. set sendmail = \"msmtp -a $fulladdr\"
  76. alias me $realname <$fulladdr>
  77. set folder = \"$maildir/$fulladdr\"
  78. set header_cache = $cachedir/$fulladdr/headers
  79. set message_cachedir = $cachedir/$fulladdr/bodies
  80. set mbox_type = Maildir
  81. bind index,pager gg noop
  82. bind index,pager g noop
  83. bind index,pager M noop
  84. bind index,pager C noop
  85. bind index gg first-entry
  86. macro index o \"<shell-escape>mw -y $fulladdr<enter>\" \"run mbsync to sync $fulladdr\"
  87. unmailboxes *
  88. unalternates *
  89. "
  90. else
  91. mutt_profile="# vim: filetype=neomuttrc
  92. # muttrc file for account $fulladdr
  93. set realname = \"$realname\"
  94. set from = \"$fulladdr\"
  95. set sendmail = \"msmtp -a $fulladdr\"
  96. alias me $realname <$fulladdr>
  97. set folder = \"imaps://$login@$imap:${iport:-993}\"
  98. set imap_user = \"$login\"
  99. set header_cache = $cachedir/$fulladdr/headers
  100. set message_cachedir = $cachedir/$fulladdr/bodies
  101. set imap_pass = \"\`pass $fulladdr\`\"
  102. set mbox_type = Maildir
  103. set ssl_starttls = yes
  104. set ssl_force_tls = yes
  105. bind index,pager gg noop
  106. bind index,pager g noop
  107. bind index,pager M noop
  108. bind index,pager C noop
  109. bind index gg first-entry
  110. unset signature
  111. unmacro index o
  112. unmailboxes *
  113. "
  114. fi
  115. }
  116. parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  117. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  118. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  119. $serverinfo
  120. EOF
  121. imap="${imap:-$imapsugg}"
  122. smtp="${smtp:-$smtpsugg}"
  123. sport="${sport:-$sportsugg}"
  124. iport="${iport:-$iportsugg}"
  125. }
  126. delete() { if [ -z "${fulladdr+x}" ]; then
  127. echo "Select the account your would like to delete (by number):"
  128. list
  129. read -r input
  130. match="^$input:"
  131. else
  132. match=" $fulladdr$"
  133. getaccounts
  134. fi
  135. fulladdr="$(echo "$accounts" | grep "$match" | cut -f2 -d' ')"
  136. [ -z "$fulladdr" ] && echo "Invalid account name given." && return 1
  137. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  138. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/"[1-9]"-$fulladdr.muttrc"
  139. sed -ibu "/[0-9]-$fulladdr.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  140. sed -ibu "/account $fulladdr/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  141. pass rm -f "$fulladdr" >/dev/null 2>&1
  142. # Get rid of those multiple newlines because I don't know awk well enough to do it by default lol.
  143. for file in "$msmtprc" "$mbsyncrc"; do
  144. tr '\n' '|' < "$file" | sed "s/||\+/||/g" | tr '|' '\n' >> "$file"bu
  145. mv -f "$file"bu "$file"
  146. done
  147. }
  148. askinfo() { \
  149. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  150. read -r fulladdr
  151. while ! echo "$fulladdr" | grep -qE ".+@.+\..+"; do
  152. echo "\`$fulladdr\` is not a valid email address. Please retype the address:"
  153. read -r fulladdr
  154. done
  155. grep -q "Channel\s*$fulladdr$" "$mbsyncrc" 2>/dev/null &&
  156. { echo "$fulladdr has already been added" && exit 1 ;}
  157. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  158. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  159. read -r imap
  160. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  161. read -r smtp
  162. [ "$sport" = 465 ] && tlsline="tls_starttls off"
  163. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  164. login="${login:-$fulladdr}"
  165. if [ -n "${password+x}" ]; then
  166. createpass
  167. else
  168. getpass
  169. fi
  170. }
  171. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$fulladdr"
  172. "$GPG" -qer "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" "$PASSWORD_STORE_DIR/$fulladdr"
  173. rm -f "$PASSWORD_STORE_DIR/$fulladdr" ;}
  174. writeinfo() { mkdir -p "${muttrc%/*}" "$accdir" "$cachedir/$fulladdr/bodies" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$maildir/$fulladdr"
  175. # Configure msmtprc for sending mail.
  176. [ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
  177. echo "$msmtp_profile" >> "$msmtprc"
  178. # On Ubuntu/Debian, a link is needed since they use an older version.
  179. command -V apt-get >/dev/null 2>&1 && ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  180. # Create the mbsync config file if local account.
  181. [ -z "${online+x}" ] && mkdir -p "${mbsyncrc%/*}" && echo "$mbsync_profile" >> "$mbsyncrc"
  182. # Create a muttrc for viewing mail.
  183. echo "$mutt_profile" > "$accdir/$idnum-$fulladdr.muttrc"
  184. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  185. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $mwconfig" >> "$muttrc"
  186. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$mwconfig" && echo "source $accdir/$idnum-$fulladdr.muttrc" >> "$muttrc"
  187. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
  188. notmuchauto # Create a notmuch config file if not present already.
  189. }
  190. getpass() { while : ; do pass rm -f "$fulladdr" >/dev/null 2>&1
  191. pass insert "$fulladdr" && break; done ;}
  192. formatShortcut() { toappend="$toappend
  193. macro index,pager g$1 \"<change-folder>=$3<enter>\" \"go to $2\"
  194. macro index,pager M$1 \";<save-message>=$3<enter>\" \"move mail to $2\"
  195. macro index,pager C$1 \";<copy-message>=$3<enter>\" \"copy mail to $2\"" ;}
  196. setBox() { toappend="$toappend
  197. set $1 = \"+$2\"" ;}
  198. getboxes() { if [ -n "${force+x}" ]; then
  199. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  200. else
  201. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass $fulladdr)" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  202. [ -z "$info" ] && echo "Log-on not successful." && return 1
  203. mailboxes="$(echo "$info" | sed "s/.*\" //;s/\"//g" | tr -d ' ')"
  204. fi
  205. getaccounts; for x in $(seq 1 9); do echo "$accounts" | grep -q "^$x:" || { export idnum="$x"; break ;}; done
  206. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' - )"
  207. for x in $mailboxes; do
  208. case "$x" in
  209. *[Ss][Ee][Nn][Tt]*) setBox record "$x"; formatShortcut s sent "$x" ;;
  210. *[Dd][Rr][Aa][Ff][Tt][Ss]*) setBox postponed "$x"; formatShortcut d drafts "$x" ;;
  211. *[Tt][Rr][Aa][Ss][Hh]*) formatShortcut t trash "$x"; setBox trash "$x" ;;
  212. *[Jj][Uu][Nn][Kk]*) formatShortcut j junk "$x" ;;
  213. *[Aa][Rr][Cc][Hh][Ii][Vv][Ee]*) formatShortcut a archive "$x" ;;
  214. *[Ss][Pp][Aa][Mm]*) formatShortcut S spam "$x" ;;
  215. *[Ii][Nn][Bb][Oo][Xx]*) formatShortcut i inbox "$x"; setBox spoolfile "$x" inbox="$x" ;;
  216. esac
  217. done
  218. }
  219. finalize() { echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  220. echo "$toappend" >> "$accdir/$idnum-$fulladdr.muttrc"
  221. [ -z "${online+x}" ] && echo "$fulladdr (account #$idnum) added successfully."
  222. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  223. return 0 ;}
  224. notmuchauto() { \
  225. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  226. [ -f "$NOTMUCH_CONFIG" ] && return 0
  227. nmbasic="[database]
  228. path=$maildir
  229. [user]
  230. name=$realname
  231. primary_email=$fulladdr
  232. [new]
  233. tags=unread;inbox;
  234. ignore=.mbsyncstate;.uidvalidity
  235. [search]
  236. exclude_tags=deleted;spam;
  237. [maildir]
  238. synchronize_flags=true
  239. [crypto]
  240. gpg_path=$GPG"
  241. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  242. togglecron() { cron="$(mktemp)"
  243. crontab -l > "$cron"
  244. if grep -q mailsync "$cron"; then
  245. echo "Removing automatic mailsync..."
  246. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  247. else
  248. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  249. echo "*/${cronmin-10} * * * * $prefix/bin/mailsync" >> "$cron"
  250. fi &&
  251. crontab "$cron"; rm -f "$cron" ;}
  252. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  253. echo "Running $1 with $action..."
  254. echo "Incompatible options given. Only one action may be specified per run."
  255. return 1
  256. else
  257. action="$1"
  258. fi; }
  259. mwinfo() { cat << EOF
  260. mw: mutt-wizard, auto-configure email accounts for mutt
  261. including downloadable mail with \`isync\`.
  262. Main actions:
  263. -a your@email.com Add an email address
  264. -l List email addresses configured
  265. -d Remove an already added address
  266. -D your@email.com Force remove account without confirmation
  267. -y your@email.com Sync mail for account by name
  268. -Y Sync mail for all accounts
  269. -t number Toggle automatic mailsync every <number> minutes
  270. -T Toggle automatic mailsync
  271. Options allowed with -a:
  272. -u Account login name if not full address
  273. -n "Real name" to be on the email account
  274. -i IMAP server address
  275. -I IMAP server port
  276. -s SMTP server address
  277. -S SMTP server port
  278. -x Password for account (recommended to be in double quotes)
  279. -p Install for a Protonmail account.
  280. -o Configure address, but keep mail online.
  281. -f Assume typical English mailboxes without attempting log-on.
  282. NOTE: Once at least one account is added, you can run
  283. \`mbsync -a\` to begin downloading mail.
  284. To change an account's password, run \`pass edit your@email.com\`.
  285. EOF
  286. }
  287. while getopts "fplhodTYD:y:i:I:s:S:u:a:n:x:m:t:" o; do case "${o}" in
  288. l) setact list || exit 1 ;;
  289. d) setact delete || exit 1 ;;
  290. D) setact delete || exit 1 ; fulladdr="$OPTARG" ;;
  291. y) setact sync || exit 1 ; fulladdr="$OPTARG" ;;
  292. Y) setact sync || exit 1 ;;
  293. a) setact add || exit 1 ; fulladdr="$OPTARG" ;;
  294. i) setact add || exit 1 ; imap="$OPTARG" ;;
  295. I) setact add || exit 1 ; iport="$OPTARG" ;;
  296. s) setact add || exit 1 ; smtp="$OPTARG" ;;
  297. S) setact add || exit 1 ; sport="$OPTARG" ;;
  298. u) setact add || exit 1 ; login="$OPTARG" ;;
  299. n) setact add || exit 1 ; realname="$OPTARG" ;;
  300. m) setact add || exit 1 ; maxmes="$OPTARG" ;;
  301. o) setact add || exit 1 ; online=True ;;
  302. f) setact add || exit 1 ; force=True ;;
  303. x) setact add || exit 1 ; password="$OPTARG" ;;
  304. t) setact toggle || exit 1 ; cronmin="$OPTARG" ;;
  305. T) setact toggle || exit 1 ;;
  306. p) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work."
  307. protocol="imap"
  308. imap="127.0.0.1"
  309. iport="1143"
  310. smtp="127.0.0.1"
  311. sport="1025"
  312. tlsline="tls_fingerprint $(msmtp --serverinfo --host=$smtp --port=$sport --tls --tls-certcheck=off | awk '/SHA256:/ {print $2}')"
  313. setact add || exit 1
  314. ;;
  315. *) mwinfo; exit 1 ;;
  316. esac done
  317. case "$action" in
  318. list) list ;;
  319. add) checkbasics && askinfo && getboxes && getprofiles && writeinfo && finalize || { delete ; exit 1 ;} ;;
  320. delete) delete ;;
  321. sync) mailsync $fulladdr ;;
  322. toggle) togglecron ;;
  323. *) mwinfo; exit 1 ;;
  324. esac