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 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. 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. iport="993"
  23. sport="465"
  24. imapssl="IMAPS"
  25. tlsline="tls_starttls off"
  26. maxmes="0"
  27. alias mbsync='mbsync -c "$mbsyncrc"'
  28. # mbsync now requires "Far/Near" rather than "Master/Slave", but Ubuntu/Debian
  29. # have the older version.
  30. if command -V apt-get >/dev/null 2>&1; then
  31. master="Master"
  32. slave="Slave"
  33. else
  34. master="Far"
  35. slave="Near"
  36. fi
  37. for x in "/etc/ssl/certs/ca-certificates.crt" \
  38. "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \
  39. "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \
  40. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \
  41. "/usr/local/share/ca-certificates/"; do
  42. [ -f "$x" ] && sslcert="$x" && break
  43. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  44. checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  45. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  46. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || {
  47. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  48. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  49. exit 1 ;} ;}
  50. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)" ;}
  51. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1 ;}
  52. prepmsmtp() { mkdir -p "${msmtprc%/*}" "${msmtplog%/*}" ; ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null ; envsubst < "$msmtptemp" >> "$msmtprc" ;}
  53. prepmbsync() { mkdir -p "${mbsyncrc%/*}" ; [ -f "$mbsyncrc" ] && echo >> "$mbsyncrc" ; envsubst < "$mbsynctemp" >> "$mbsyncrc" ;}
  54. prepmpop() { mkdir -p "${mpoprc%/*}" ; envsubst < "$mpoptemp" >> "$mpoprc" ;}
  55. prepmutt() { mkdir -p "${muttrc%/*}" "$accdir"
  56. envsubst < "$mutttemp" > "$accdir/$fulladdr.muttrc"
  57. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  58. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >> "$muttrc"
  59. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >> "$muttrc"
  60. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
  61. }
  62. getprofiles() { \
  63. safename="$(echo $fulladdr | sed 's/@/_/g')"
  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}" ] && safename="$(echo $fulladdr | sed 's/@/_/g')" && rm -rf "${cachedir:?}/${safename:?}" "${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. folder="$maildir/$fulladdr"
  118. getaccounts; echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  119. { echo "$fulladdr has already been added" && exit 1 ;}
  120. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  121. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  122. read -r imap
  123. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  124. read -r smtp
  125. case $sport in
  126. 587) tlsline="# tls_starttls" ;;
  127. esac
  128. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  129. [ -z "$passprefix" ] && passprefix=""
  130. hostname="${fulladdr#*@}"
  131. login="${login:-$fulladdr}"
  132. if [ -n "${password+x}" ]; then
  133. createpass
  134. else
  135. getpass
  136. fi
  137. }
  138. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  139. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  140. case "$(uname)" in
  141. Darwin|*BSD) rm -P "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  142. *) shred -u "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  143. esac
  144. rm -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;}
  145. errorexit() {
  146. echo "Log-on not successful."
  147. case "$imap" in
  148. imap.gmail.com)
  149. echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
  150. Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
  151. ;;
  152. imap.mail.me.com)
  153. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  154. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  155. ;;
  156. esac
  157. exit 1
  158. }
  159. getpass() { while : ; do pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  160. pass insert -f "$passprefix$fulladdr" && break; done ;}
  161. getboxes() { if [ -n "${force+x}" ] ; then
  162. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  163. else
  164. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  165. [ -z "$info" ] && errorexit
  166. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  167. fi
  168. [ "$type" = "pop" ] && mailboxes="INBOX"
  169. for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u; echo 0); do
  170. idnum=$((idnum + 1))
  171. [ "$idnum" -eq "$x" ] || break
  172. done
  173. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' - )"
  174. }
  175. finalize() { echo "$toappend" >> "$accdir/$fulladdr.muttrc"
  176. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  177. mkdir -p "$cachedir/$safename/bodies"
  178. echo "$fulladdr (account #$idnum) added successfully."
  179. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  180. return 0 ;}
  181. prepnotmuch() { \
  182. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  183. [ -f "$NOTMUCH_CONFIG" ] && return 0
  184. envsubst < "$notmuchtemp" > "$NOTMUCH_CONFIG" ;}
  185. togglecron() { cron="$(mktemp)"
  186. crontab -l > "$cron"
  187. if grep -q mailsync "$cron"; then
  188. echo "Removing automatic mailsync..."
  189. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  190. else
  191. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  192. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync -q" >> "$cron"
  193. fi &&
  194. crontab "$cron"; rm -f "$cron" ;}
  195. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  196. echo "Running $1 with $action..."
  197. echo "Incompatible options given. Only one action may be specified per run."
  198. exit 1
  199. else
  200. action="$1"
  201. fi; }
  202. mwinfo() { cat << EOF
  203. mw: mutt-wizard, auto-configure email accounts for mutt
  204. including downloadable mail with \`isync\`.
  205. Main actions:
  206. -a your@email.com Add an email address
  207. -l List email addresses configured
  208. -d Remove an already added address
  209. -D your@email.com Force remove account without confirmation
  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 Add for a POP server instead of IMAP.
  222. -P Pass Prefix (prefix of the file where password is stored)
  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