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.

mw 14 KiB

4 years ago
4 years ago
5 years ago
3 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_CACHE_HOME:-$HOME/.cache}/msmtp/msmtp.log"
  10. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  11. mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config"
  12. alias mbsync='mbsync -c "$mbsyncrc"'
  13. # mbsync now requires "Far/Near" rather than "Master/Slave", but Ubuntu/Debian
  14. # have the older version.
  15. if command -V apt-get >/dev/null 2>&1; then
  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() { mkdir -p "${msmtprc%/*}" "${msmtplog%/*}"
  35. ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  36. echo "account $fulladdr
  37. host $smtp
  38. port ${sport:-465}
  39. from $fulladdr
  40. user $login
  41. passwordeval \"pass $passprefix$fulladdr\"
  42. auth ${auth:-on}
  43. tls on
  44. tls_trust_file $sslcert
  45. logfile $msmtplog
  46. ${tlsline:-tls_starttls off}
  47. " >> "$msmtprc"
  48. }
  49. prepmbsync() { mkdir -p "${mbsyncrc%/*}"
  50. echo "
  51. IMAPStore $fulladdr-remote
  52. Host $imap
  53. Port ${iport:-993}
  54. User $login
  55. PassCmd \"pass $passprefix$fulladdr\"
  56. AuthMechs LOGIN
  57. SSLType ${imapssl:-IMAPS}
  58. CertificateFile $sslcert
  59. MaildirStore $fulladdr-local
  60. Subfolders Verbatim
  61. Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/
  62. Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
  63. Channel $fulladdr
  64. Expunge Both
  65. ${master:-Far} :$fulladdr-remote:
  66. ${slave:-Near} :$fulladdr-local:
  67. Patterns * !\"[Gmail]/All Mail\"
  68. Create Both
  69. SyncState *
  70. MaxMessages ${maxmes:-0}
  71. ExpireUnread no
  72. # End profile
  73. " >> "$mbsyncrc" ;}
  74. prepmpop() { mkdir -p "${mpoprc%/*}"
  75. echo "
  76. account $fulladdr
  77. tls on
  78. user $login
  79. host $imap
  80. delivery maildir ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
  81. passwordeval pass $passprefix$fulladdr
  82. " >> "$mpoprc" ;}
  83. prepmutt() { mkdir -p "${muttrc%/*}" "$accdir"
  84. echo "# vim: filetype=neomuttrc
  85. # muttrc file for account $fulladdr
  86. set realname = \"$realname\"
  87. set from = \"$fulladdr\"
  88. set sendmail = \"msmtp -a $fulladdr\"
  89. alias me $realname <$fulladdr>
  90. set folder = \"${folder:-$maildir/$fulladdr}\"
  91. set header_cache = $cachedir/$fulladdr/headers
  92. set message_cachedir = $cachedir/$fulladdr/bodies
  93. set mbox_type = Maildir
  94. set hostname = \"$hostname\"
  95. source $muttshare/switch.muttrc
  96. set spoolfile = "+INBOX"
  97. set postponed = "+Drafts"
  98. set trash = "+Trash"
  99. set record = "+Sent"
  100. $extra
  101. $synccmd
  102. macro index o \"<shell-escape>mailsync $fulladdr<enter>\" \"sync $fulladdr\"
  103. macro index \eg \"<enter-command>unset wait_key<enter><shell-escape>gpg --list-secret-keys; printf 'Enter email ID of user to publish: '; read eID; printf 'Enter fingerprint of GPG key to publish: '; read eFGPT; $prefix/libexec/gpg-wks-client --create \\\$eFGPT \\\$eID | msmtp --read-envelope-from --read-recipients -a $fulladdr<enter>\" \"publish GPG key to WKS provider\"
  104. macro index \eh \"<pipe-message>$prefix/libexec/gpg-wks-client --receive | msmtp --read-envelope-from --read-recipients -a $fulladdr<enter>\" \"confirm GPG publication\"
  105. " > "$accdir/$fulladdr.muttrc"
  106. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  107. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >> "$muttrc"
  108. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >> "$muttrc"
  109. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
  110. }
  111. getprofiles() { \
  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 $passprefix$fulladdr\`\"
  120. set ssl_starttls = yes
  121. set ssl_force_tls = yes"
  122. ;;
  123. pop) prepmpop ;;
  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 "$passprefix$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. [ -z "$passprefix" ] && passprefix=""
  179. hostname="${fulladdr#*@}"
  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/$passprefix$fulladdr"
  188. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  189. rm -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;}
  190. errorexit() {
  191. echo "Log-on not successful."
  192. case "$imap" in
  193. imap.gmail.com)
  194. echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
  195. Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
  196. ;;
  197. imap.mail.me.com)
  198. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  199. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  200. ;;
  201. esac
  202. exit 1
  203. }
  204. getpass() { while : ; do pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  205. pass insert -f "$passprefix$fulladdr" && break; done ;}
  206. getboxes() { if [ -n "${force+x}" ] ; then
  207. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  208. else
  209. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  210. [ -z "$info" ] && errorexit
  211. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  212. fi
  213. [ "$type" = "pop" ] && mailboxes="INBOX"
  214. for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u; echo 0); do
  215. idnum=$((idnum + 1))
  216. [ "$idnum" -eq "$x" ] || break
  217. done
  218. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' - )"
  219. }
  220. finalize() { echo "$toappend" >> "$accdir/$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. echo "[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" > "$NOTMUCH_CONFIG" ;}
  242. togglecron() { cron="$(mktemp)"
  243. crontab -l > "$cron"
  244. if grep -q mailsync "$cron"; then
  245. echo "Removing automatic mailsync..."
  246. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  247. else
  248. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  249. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync -q" >> "$cron"
  250. fi &&
  251. crontab "$cron"; rm -f "$cron" ;}
  252. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  253. echo "Running $1 with $action..."
  254. echo "Incompatible options given. Only one action may be specified per run."
  255. return 1
  256. else
  257. action="$1"
  258. fi; }
  259. mwinfo() { cat << EOF
  260. mw: mutt-wizard, auto-configure email accounts for mutt
  261. including downloadable mail with \`isync\`.
  262. Main actions:
  263. -a your@email.com Add an email address
  264. -l List email addresses configured
  265. -d Remove an already added address
  266. -D your@email.com Force remove account without confirmation
  267. -y your@email.com Sync mail for account by name
  268. -Y Sync mail for all accounts
  269. -t number Toggle automatic mailsync every <number> minutes
  270. -T Toggle automatic mailsync
  271. -r Reorder account numbers
  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 Pass Prefix (prefix of the file where password is stored)
  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 '$passprefix'your@email.com\`.
  288. EOF
  289. }
  290. reorder() {
  291. tempfile="$(mktemp -u)"
  292. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  293. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  294. # DO NOT reorder rows or rename the accounts in the second column." > "$tempfile"
  295. sed -n "
  296. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  297. " "$muttrc" >> "$tempfile"
  298. ${EDITOR:-vim} "$tempfile" || exit 1
  299. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  300. default="$(sort -n "$tempfile" | head -n 1)"
  301. default="${default#* }"
  302. sed -ibu "
  303. /.* i[0-9] .*.muttrc/d
  304. /^source.*accounts.*.muttrc/d
  305. " "$muttrc" 2>/dev/null; rm -f "$muttrc"bu
  306. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  307. {
  308. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  309. }
  310. ' "$tempfile" >> "$muttrc"
  311. }
  312. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:" o; do case "${o}" in
  313. l) setact list || exit 1 ;;
  314. r) setact reorder || exit 1 ;;
  315. d) setact delete || exit 1 ;;
  316. D) setact delete || exit 1 ; fulladdr="$OPTARG" ;;
  317. y) setact sync || exit 1 ; fulladdr="$OPTARG" ;;
  318. Y) setact sync || exit 1 ;;
  319. a) setact add || exit 1 ; fulladdr="$OPTARG" ;;
  320. i) setact add || exit 1 ; imap="$OPTARG" ;;
  321. I) setact add || exit 1 ; iport="$OPTARG" ;;
  322. s) setact add || exit 1 ; smtp="$OPTARG" ;;
  323. S) setact add || exit 1 ; sport="$OPTARG" ;;
  324. u) setact add || exit 1 ; login="$OPTARG" ;;
  325. n) setact add || exit 1 ; realname="$OPTARG" ;;
  326. P) setact add || exit 1 ; passprefix="$OPTARG" ;;
  327. m) setact add || exit 1 ; maxmes="$OPTARG" ;;
  328. o) setact add || exit 1 ; type="online" ;;
  329. p) setact add || exit 1 ; type="pop"; protocol="pop3s" ; iport="${iport:-995}" ;;
  330. f) setact add || exit 1 ; force=True ;;
  331. x) setact add || exit 1 ; password="$OPTARG" ;;
  332. X) setact delete || exit 1 ; purge=True ;;
  333. t) setact toggle || exit 1 ; cronmin="$OPTARG" ;;
  334. T) setact toggle || exit 1 ;;
  335. *) mwinfo; exit 1 ;;
  336. esac done
  337. case "$action" in
  338. list) list ;;
  339. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  340. delete) delete ;;
  341. sync)
  342. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  343. mailsync $fulladdr
  344. ;;
  345. toggle) togglecron ;;
  346. reorder) reorder ;;
  347. *) mwinfo; exit 1 ;;
  348. esac