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.
 
 
 
 

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