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.
 
 
 
 

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