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.
 
 
 
 

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