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.
 
 
 
 

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