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.
 
 
 
 

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