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.
 
 
 
 

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