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.
 
 
 
 

334 lines
12 KiB

  1. #!/bin/sh
  2. set -a
  3. prefix="/usr/local"
  4. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail"
  5. muttshare="$prefix/share/mutt-wizard"
  6. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  7. muttrc="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/muttrc"
  8. accdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/accounts"
  9. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  10. msmtplog="${XDG_CACHE_HOME:-$HOME/.cache}/msmtp/msmtp.log"
  11. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  12. mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config"
  13. mpoptemp="$muttshare/mpop-temp"
  14. mbsynctemp="$muttshare/mbsync-temp"
  15. mutttemp="$muttshare/mutt-temp"
  16. msmtptemp="$muttshare/msmtp-temp"
  17. alias mbsync='mbsync -c "$mbsyncrc"'
  18. # mbsync now requires "Far/Near" rather than "Master/Slave", but Ubuntu/Debian
  19. # have the older version.
  20. if command -V apt-get >/dev/null 2>&1; then
  21. master="Master"
  22. slave="Slave"
  23. else
  24. master="Far"
  25. slave="Near"
  26. fi
  27. for x in "/etc/ssl/certs/ca-certificates.crt" \
  28. "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \
  29. "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \
  30. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \
  31. "/usr/local/share/ca-certificates/"; do
  32. [ -f "$x" ] && sslcert="$x" && break
  33. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  34. checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  35. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  36. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || {
  37. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  38. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  39. exit 1 ;} ;}
  40. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)" ;}
  41. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1 ;}
  42. prepmsmtp() { mkdir -p "${msmtprc%/*}" "${msmtplog%/*}" ; ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null ; envsubst < "$msmtptemp" >> "$msmtprc" ;}
  43. prepmbsync() { folder="$maildir/$fulladdr" ; maxmes="${maxmes:-0}" ; mkdir -p "${mbsyncrc%/*}" ; envsubst < "$mbsynctemp" >> "$mbsyncrc" ;}
  44. prepmpop() { folder="$maildir/$fulladdr" ; mkdir -p "${mpoprc%/*}" ; envsubst < "$mpoptemp" >> "$mpoprc" ;}
  45. prepmutt() { mkdir -p "${muttrc%/*}" "$accdir"
  46. envsubst < "$mutttemp" > "$accdir/$fulladdr.muttrc"
  47. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  48. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >> "$muttrc"
  49. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >> "$muttrc"
  50. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
  51. }
  52. getprofiles() { \
  53. case "$iport" in
  54. 1143) imapssl=None ;;
  55. 143) imapssl=STARTTLS ;;
  56. "") iport="993"; imapssl=IMAPS ;;
  57. *) imapssl=IMAPS ;;
  58. esac
  59. case "$type" in
  60. online) folder="imaps://$login@$imap:$iport"
  61. extra="set imap_user = \"$login\"
  62. set imap_pass = \"\`pass $passprefix$fulladdr\`\"
  63. set ssl_starttls = yes
  64. set ssl_force_tls = yes"
  65. ;;
  66. pop) prepmpop ;;
  67. *) prepmbsync ;;
  68. esac
  69. prepmsmtp
  70. prepmutt
  71. prepnotmuch # Create a notmuch config file if not present already.
  72. }
  73. parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  74. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  75. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  76. $serverinfo
  77. EOF
  78. imap="${imap:-$imapsugg}"
  79. smtp="${smtp:-$smtpsugg}"
  80. sport="${sport:-$sportsugg}"
  81. iport="${iport:-$iportsugg}"
  82. }
  83. delete() { if [ -z "${fulladdr+x}" ]; then
  84. echo "Select the account you would like to delete (by number):"
  85. list || exit 1
  86. read -r input
  87. match="^\s*$input\s\+"
  88. else
  89. match="\s\+$fulladdr$"
  90. getaccounts
  91. fi
  92. fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"
  93. [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1
  94. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null ; rm -f "$mbsyncrc"bu
  95. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc"
  96. sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null; rm -f "$muttrc"bu
  97. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null; rm -f "$msmtprc"bu
  98. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null; rm -f "$mpoprc"bu
  99. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  100. [ -n "${purge+x}" ] && rm -rf "${maildir:?}/${fulladdr:?}"
  101. for file in "$msmtprc" "$mbsyncrc" "$mpoprc"; do
  102. sed -ibu 'N;/^\n$/D;P;D;' "$file" 2>/dev/null; rm -f "$file"bu
  103. done
  104. }
  105. askinfo() { \
  106. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  107. read -r fulladdr
  108. while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
  109. echo "$fulladdr is not a valid email address. Please retype the address:"
  110. read -r fulladdr
  111. done
  112. getaccounts; echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  113. { echo "$fulladdr has already been added" && exit 1 ;}
  114. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  115. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  116. read -r imap
  117. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  118. read -r smtp
  119. case $sport in
  120. 587) tlsline="# tls_starttls" ;;
  121. "") tlsline="tls_starttls off"; sport="465" ;;
  122. *) tlsline="tls_starttls off" ;;
  123. esac
  124. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  125. [ -z "$passprefix" ] && passprefix=""
  126. hostname="${fulladdr#*@}"
  127. login="${login:-$fulladdr}"
  128. if [ -n "${password+x}" ]; then
  129. createpass
  130. else
  131. getpass
  132. fi
  133. }
  134. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  135. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  136. rm -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;}
  137. errorexit() {
  138. echo "Log-on not successful."
  139. case "$imap" in
  140. imap.gmail.com)
  141. echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
  142. Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
  143. ;;
  144. imap.mail.me.com)
  145. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  146. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  147. ;;
  148. esac
  149. exit 1
  150. }
  151. getpass() { while : ; do pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  152. pass insert -f "$passprefix$fulladdr" && break; done ;}
  153. getboxes() { if [ -n "${force+x}" ] ; then
  154. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  155. else
  156. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  157. [ -z "$info" ] && errorexit
  158. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  159. fi
  160. [ "$type" = "pop" ] && mailboxes="INBOX"
  161. for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u; echo 0); do
  162. idnum=$((idnum + 1))
  163. [ "$idnum" -eq "$x" ] || break
  164. done
  165. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' - )"
  166. }
  167. finalize() { echo "$toappend" >> "$accdir/$fulladdr.muttrc"
  168. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  169. echo "$fulladdr (account #$idnum) added successfully."
  170. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  171. return 0 ;}
  172. prepnotmuch() { \
  173. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  174. [ -f "$NOTMUCH_CONFIG" ] && return 0
  175. echo "[database]
  176. path=$maildir
  177. [user]
  178. name=$realname
  179. primary_email=$fulladdr
  180. [new]
  181. tags=unread;inbox;
  182. ignore=.mbsyncstate;.uidvalidity
  183. [search]
  184. exclude_tags=deleted;spam;
  185. [maildir]
  186. synchronize_flags=true
  187. [crypto]
  188. gpg_path=$GPG" > "$NOTMUCH_CONFIG" ;}
  189. togglecron() { cron="$(mktemp)"
  190. crontab -l > "$cron"
  191. if grep -q mailsync "$cron"; then
  192. echo "Removing automatic mailsync..."
  193. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  194. else
  195. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  196. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync -q" >> "$cron"
  197. fi &&
  198. crontab "$cron"; rm -f "$cron" ;}
  199. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  200. echo "Running $1 with $action..."
  201. echo "Incompatible options given. Only one action may be specified per run."
  202. return 1
  203. else
  204. action="$1"
  205. fi; }
  206. mwinfo() { cat << EOF
  207. mw: mutt-wizard, auto-configure email accounts for mutt
  208. including downloadable mail with \`isync\`.
  209. Main actions:
  210. -a your@email.com Add an email address
  211. -l List email addresses configured
  212. -d Remove an already added address
  213. -D your@email.com Force remove account without confirmation
  214. -y your@email.com Sync mail for account by name
  215. -Y Sync mail for all accounts
  216. -t number Toggle automatic mailsync every <number> minutes
  217. -T Toggle automatic mailsync
  218. -r Reorder account numbers
  219. Options allowed with -a:
  220. -u Account login name if not full address
  221. -n "Real name" to be on the email account
  222. -i IMAP/POP server address
  223. -I IMAP/POP server port
  224. -s SMTP server address
  225. -S SMTP server port
  226. -x Password for account (recommended to be in double quotes)
  227. -P Pass Prefix (prefix of the file where password is stored)
  228. -p Add for a POP server instead of IMAP.
  229. -X Delete an account's local email too when deleting.
  230. -o Configure address, but keep mail online.
  231. -f Assume typical English mailboxes without attempting log-on.
  232. NOTE: Once at least one account is added, you can run
  233. \`mbsync -a\` to begin downloading mail.
  234. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  235. EOF
  236. }
  237. reorder() {
  238. tempfile="$(mktemp -u)"
  239. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  240. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  241. # DO NOT reorder rows or rename the accounts in the second column." > "$tempfile"
  242. sed -n "
  243. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  244. " "$muttrc" >> "$tempfile"
  245. ${EDITOR:-vim} "$tempfile" || exit 1
  246. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  247. default="$(sort -n "$tempfile" | head -n 1)"
  248. default="${default#* }"
  249. sed -ibu "
  250. /.* i[0-9] .*.muttrc/d
  251. /^source.*accounts.*.muttrc/d
  252. " "$muttrc" 2>/dev/null; rm -f "$muttrc"bu
  253. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  254. {
  255. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  256. }
  257. ' "$tempfile" >> "$muttrc"
  258. }
  259. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:" o; do case "${o}" in
  260. l) setact list || exit 1 ;;
  261. r) setact reorder || exit 1 ;;
  262. d) setact delete || exit 1 ;;
  263. D) setact delete || exit 1 ; fulladdr="$OPTARG" ;;
  264. y) setact sync || exit 1 ; fulladdr="$OPTARG" ;;
  265. Y) setact sync || exit 1 ;;
  266. a) setact add || exit 1 ; fulladdr="$OPTARG" ;;
  267. i) setact add || exit 1 ; imap="$OPTARG" ;;
  268. I) setact add || exit 1 ; iport="$OPTARG" ;;
  269. s) setact add || exit 1 ; smtp="$OPTARG" ;;
  270. S) setact add || exit 1 ; sport="$OPTARG" ;;
  271. u) setact add || exit 1 ; login="$OPTARG" ;;
  272. n) setact add || exit 1 ; realname="$OPTARG" ;;
  273. P) setact add || exit 1 ; passprefix="$OPTARG" ;;
  274. m) setact add || exit 1 ; maxmes="$OPTARG" ;;
  275. o) setact add || exit 1 ; type="online" ;;
  276. p) setact add || exit 1 ; type="pop"; protocol="pop3s" ; iport="${iport:-995}" ;;
  277. f) setact add || exit 1 ; force=True ;;
  278. x) setact add || exit 1 ; password="$OPTARG" ;;
  279. X) setact delete || exit 1 ; purge=True ;;
  280. t) setact toggle || exit 1 ; cronmin="$OPTARG" ;;
  281. T) setact toggle || exit 1 ;;
  282. *) mwinfo; exit 1 ;;
  283. esac done
  284. case "$action" in
  285. list) list ;;
  286. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  287. delete) delete ;;
  288. sync)
  289. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  290. mailsync $fulladdr
  291. ;;
  292. toggle) togglecron ;;
  293. reorder) reorder ;;
  294. *) mwinfo; exit 1 ;;
  295. esac