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. 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 "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)" ;}
  33. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1 ;}
  34. prepmsmtp() { echo "account $fulladdr
  35. host $smtp
  36. port ${sport:-465}
  37. from $fulladdr
  38. user $login
  39. passwordeval \"pass $fulladdr\"
  40. auth ${auth:-on}
  41. tls on
  42. tls_trust_file $sslcert
  43. logfile $msmtplog
  44. ${tlsline:-tls_starttls off}
  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. source $muttshare/switch.muttrc
  93. set spoolfile = "+INBOX"
  94. set postponed = "+Drafts"
  95. set trash = "+Trash"
  96. set record = "+Sent"
  97. $extra
  98. $synccmd
  99. " > "$accdir/$fulladdr.muttrc"
  100. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  101. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >> "$muttrc"
  102. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >> "$muttrc"
  103. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
  104. }
  105. getprofiles() { \
  106. mkdir -p "${muttrc%/*}" "$accdir" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp"
  107. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  108. case "$iport" in
  109. 1143) imapssl=None ;;
  110. 143) imapssl=STARTTLS ;;
  111. esac
  112. case "$type" in
  113. online) folder="imaps://$login@$imap:${iport:-993}"
  114. extra="set imap_user = \"$login\"
  115. set imap_pass = \"\`pass $fulladdr\`\"
  116. set ssl_starttls = yes
  117. set ssl_force_tls = yes"
  118. ;;
  119. pop) synccmd="macro index o \"<shell-escape>mpop $fulladdr<enter>\" \"run mpop to get $fulladdr's mail\""
  120. folder="$maildir/$fulladdr"
  121. prepmpop ;;
  122. *) synccmd="macro index o \"<shell-escape>mw -y $fulladdr<enter>\" \"run mbsync to sync $fulladdr\""
  123. folder="$maildir/$fulladdr"
  124. prepmbsync ;;
  125. esac
  126. prepmsmtp
  127. prepmutt
  128. prepnotmuch # Create a notmuch config file if not present already.
  129. }
  130. parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  131. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  132. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  133. $serverinfo
  134. EOF
  135. imap="${imap:-$imapsugg}"
  136. smtp="${smtp:-$smtpsugg}"
  137. sport="${sport:-$sportsugg}"
  138. iport="${iport:-$iportsugg}"
  139. }
  140. delete() { if [ -z "${fulladdr+x}" ]; then
  141. echo "Select the account you would like to delete (by number):"
  142. list || exit 1
  143. read -r input
  144. match="^\s*$input\s\+"
  145. else
  146. match="\s\+$fulladdr$"
  147. getaccounts
  148. fi
  149. fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"
  150. [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1
  151. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null ; rm -f "$mbsyncrc"bu
  152. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc"
  153. sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null; rm -f "$muttrc"bu
  154. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null; rm -f "$msmtprc"bu
  155. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null; rm -f "$mpoprc"bu
  156. pass rm -f "$fulladdr" >/dev/null 2>&1
  157. [ -n "${purge+x}" ] && rm -rf "${maildir:?}/${fulladdr:?}"
  158. for file in "$msmtprc" "$mbsyncrc" "$mpoprc"; do
  159. sed -ibu 'N;/^\n$/D;P;D;' "$file" 2>/dev/null; rm -f "$file"bu
  160. done
  161. }
  162. askinfo() { \
  163. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  164. read -r fulladdr
  165. while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
  166. echo "$fulladdr is not a valid email address. Please retype the address:"
  167. read -r fulladdr
  168. done
  169. getaccounts; echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  170. { echo "$fulladdr has already been added" && exit 1 ;}
  171. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  172. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  173. read -r imap
  174. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  175. read -r smtp
  176. [ "$sport" = 587 ] && tlsline="# tls_starttls"
  177. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  178. hostname="${fulladdr#*@}"
  179. login="${login:-$fulladdr}"
  180. if [ -n "${password+x}" ]; then
  181. createpass
  182. else
  183. getpass
  184. fi
  185. }
  186. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$fulladdr"
  187. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$fulladdr"
  188. rm -f "$PASSWORD_STORE_DIR/$fulladdr" ;}
  189. getpass() { while : ; do pass rm -f "$fulladdr" >/dev/null 2>&1
  190. pass insert -f "$fulladdr" && break; done ;}
  191. getboxes() { if [ -n "${force+x}" ] ; then
  192. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  193. else
  194. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  195. [ -z "$info" ] && echo "Log-on not successful." && return 1
  196. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  197. fi
  198. [ "$type" = "pop" ] && mailboxes="INBOX"
  199. for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" | sort -u; echo 0); do
  200. idnum=$((idnum + 1))
  201. [ "$idnum" -eq "$x" ] || break
  202. done
  203. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' - )"
  204. }
  205. finalize() { echo "$toappend" >> "$accdir/$fulladdr.muttrc"
  206. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  207. echo "$fulladdr (account #$idnum) added successfully."
  208. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  209. return 0 ;}
  210. prepnotmuch() { \
  211. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  212. [ -f "$NOTMUCH_CONFIG" ] && return 0
  213. echo "[database]
  214. path=$maildir
  215. [user]
  216. name=$realname
  217. primary_email=$fulladdr
  218. [new]
  219. tags=unread;inbox;
  220. ignore=.mbsyncstate;.uidvalidity
  221. [search]
  222. exclude_tags=deleted;spam;
  223. [maildir]
  224. synchronize_flags=true
  225. [crypto]
  226. gpg_path=$GPG" > "$NOTMUCH_CONFIG" ;}
  227. togglecron() { cron="$(mktemp)"
  228. crontab -l > "$cron"
  229. if grep -q mailsync "$cron"; then
  230. echo "Removing automatic mailsync..."
  231. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  232. else
  233. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  234. echo "*/${cronmin-10} * * * * $prefix/bin/mailsync" >> "$cron"
  235. fi &&
  236. crontab "$cron"; rm -f "$cron" ;}
  237. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  238. echo "Running $1 with $action..."
  239. echo "Incompatible options given. Only one action may be specified per run."
  240. return 1
  241. else
  242. action="$1"
  243. fi; }
  244. mwinfo() { cat << EOF
  245. mw: mutt-wizard, auto-configure email accounts for mutt
  246. including downloadable mail with \`isync\`.
  247. Main actions:
  248. -a your@email.com Add an email address
  249. -l List email addresses configured
  250. -d Remove an already added address
  251. -D your@email.com Force remove account without confirmation
  252. -y your@email.com Sync mail for account by name
  253. -Y Sync mail for all accounts
  254. -t number Toggle automatic mailsync every <number> minutes
  255. -T Toggle automatic mailsync
  256. -r Reorder account numbers
  257. Options allowed with -a:
  258. -u Account login name if not full address
  259. -n "Real name" to be on the email account
  260. -i IMAP/POP server address
  261. -I IMAP/POP server port
  262. -s SMTP server address
  263. -S SMTP server port
  264. -x Password for account (recommended to be in double quotes)
  265. -p Install for a Protonmail account.
  266. -P Add for a POP server instead of IMAP.
  267. -X Delete an account's local email too when deleting.
  268. -o Configure address, but keep mail online.
  269. -f Assume typical English mailboxes without attempting log-on.
  270. NOTE: Once at least one account is added, you can run
  271. \`mbsync -a\` to begin downloading mail.
  272. To change an account's password, run \`pass edit your@email.com\`.
  273. EOF
  274. }
  275. reorder() {
  276. tempfile="$(mktemp -u)"
  277. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  278. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  279. # DO NOT reorder rows or rename the accounts in the second column." > "$tempfile"
  280. sed -n "
  281. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  282. " "$muttrc" >> "$tempfile"
  283. ${EDITOR:-vim} "$tempfile" || exit 1
  284. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  285. default="$(sort -n "$tempfile" | head -n 1)"
  286. default="${default#* }"
  287. sed -ibu "
  288. /.* i[0-9] .*.muttrc/d
  289. /^source.*accounts.*.muttrc/d
  290. " "$muttrc" 2>/dev/null; rm -f "$muttrc"bu
  291. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  292. {
  293. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  294. }
  295. ' "$tempfile" >> "$muttrc"
  296. }
  297. while getopts "rfpPXlhodTYD:y:i:I:s:S:u:a:n:x:m:t:" o; do case "${o}" in
  298. l) setact list || exit 1 ;;
  299. r) setact reorder || exit 1 ;;
  300. d) setact delete || exit 1 ;;
  301. D) setact delete || exit 1 ; fulladdr="$OPTARG" ;;
  302. y) setact sync || exit 1 ; fulladdr="$OPTARG" ;;
  303. Y) setact sync || exit 1 ;;
  304. a) setact add || exit 1 ; fulladdr="$OPTARG" ;;
  305. i) setact add || exit 1 ; imap="$OPTARG" ;;
  306. I) setact add || exit 1 ; iport="$OPTARG" ;;
  307. s) setact add || exit 1 ; smtp="$OPTARG" ;;
  308. S) setact add || exit 1 ; sport="$OPTARG" ;;
  309. u) setact add || exit 1 ; login="$OPTARG" ;;
  310. n) setact add || exit 1 ; realname="$OPTARG" ;;
  311. m) setact add || exit 1 ; maxmes="$OPTARG" ;;
  312. o) setact add || exit 1 ; type="online" ;;
  313. P) setact add || exit 1 ; type="pop"; protocol="pop3s" ; iport="${iport:-995}" ;;
  314. f) setact add || exit 1 ; force=True ;;
  315. x) setact add || exit 1 ; password="$OPTARG" ;;
  316. X) setact delete || exit 1 ; purge=True ;;
  317. t) setact toggle || exit 1 ; cronmin="$OPTARG" ;;
  318. T) setact toggle || exit 1 ;;
  319. p) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work."
  320. protocol="imap"
  321. imap="127.0.0.1"
  322. iport="1143"
  323. smtp="127.0.0.1"
  324. sport="1025"
  325. auth="login"
  326. tlsline="tls_fingerprint $(msmtp --serverinfo --host=$smtp --port=$sport --tls --tls-certcheck=off | sed -n '/SHA256:/ s/^\s*SHA256:\s*// p')"
  327. setact add || exit 1
  328. ;;
  329. *) mwinfo; exit 1 ;;
  330. esac done
  331. case "$action" in
  332. list) list ;;
  333. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  334. delete) delete ;;
  335. sync) mailsync $fulladdr ;;
  336. toggle) togglecron ;;
  337. reorder) reorder ;;
  338. *) mwinfo; exit 1 ;;
  339. esac