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.
 
 
 
 

429 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_STATE_HOME:-$HOME/.local/state}/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() {
  45. 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. }
  52. }
  53. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)"; }
  54. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1; }
  55. prepmsmtp() {
  56. mkdir -p "${msmtprc%/*}" "${msmtplog%/*}"
  57. ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  58. envsubst <"$msmtptemp" >>"$msmtprc"
  59. }
  60. prepmbsync() {
  61. mkdir -p "${mbsyncrc%/*}"
  62. [ -f "$mbsyncrc" ] && echo >>"$mbsyncrc"
  63. envsubst <"$mbsynctemp" >>"$mbsyncrc"
  64. }
  65. prepmpop() {
  66. mkdir -p "${mpoprc%/*}"
  67. envsubst <"$mpoptemp" >>"$mpoprc"
  68. }
  69. prepmutt() {
  70. mkdir -p "${muttrc%/*}" "$accdir"
  71. envsubst <"$mutttemp" >"$accdir/$fulladdr.muttrc"
  72. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" >"$muttrc"
  73. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >>"$muttrc"
  74. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >>"$muttrc"
  75. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >>"$muttrc"
  76. }
  77. getprofiles() {
  78. safename="$(echo $fulladdr | sed 's/@/_/g')"
  79. case "$type" in
  80. online)
  81. folder="imaps://$login@$imap:$iport"
  82. extra="$(envsubst <"$onlinetemp")"
  83. ;;
  84. pop) prepmpop ;;
  85. *)
  86. case "$iport" in
  87. 1143) imapssl=None ;;
  88. 143) imapssl=STARTTLS ;;
  89. esac
  90. prepmbsync
  91. ;;
  92. esac
  93. prepmsmtp
  94. prepmutt
  95. prepnotmuch
  96. }
  97. parsedomains() {
  98. serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  99. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  100. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  101. $serverinfo
  102. EOF
  103. imap="${imap:-$imapsugg}"
  104. smtp="${smtp:-$smtpsugg}"
  105. sport="${sport:-$sportsugg}"
  106. iport="${iport:-$iportsugg}"
  107. }
  108. delete() {
  109. if [ -z "${fulladdr+x}" ]; then
  110. echo "Select the account you would like to delete (by number):"
  111. list || exit 1
  112. read -r input
  113. match="^\s*$input\s\+"
  114. else
  115. match="\s\+$fulladdr$"
  116. getaccounts
  117. fi
  118. fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"
  119. [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1
  120. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null
  121. rm -f "$mbsyncrc"bu
  122. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc"
  123. sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null
  124. rm -f "$muttrc"bu
  125. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null
  126. rm -f "$msmtprc"bu
  127. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null
  128. rm -f "$mpoprc"bu
  129. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  130. [ -n "${purge+x}" ] && safename="$(echo $fulladdr | sed 's/@/_/g')" && rm -rf "${cachedir:?}/${safename:?}" "${maildir:?}/${fulladdr:?}"
  131. }
  132. askinfo() {
  133. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  134. read -r fulladdr
  135. while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
  136. echo "$fulladdr is not a valid email address. Please retype the address:"
  137. read -r fulladdr
  138. done
  139. folder="$maildir/$fulladdr"
  140. getaccounts
  141. echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  142. { echo "$fulladdr has already been added" && exit 1; }
  143. { [ -z "$imap" ] || [ -z "$smtp" ]; } && parsedomains
  144. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  145. read -r imap
  146. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  147. read -r smtp
  148. case $sport in
  149. 587) tlsline="# tls_starttls" ;;
  150. esac
  151. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  152. [ -z "$passprefix" ] && passprefix=""
  153. hostname="${fulladdr#*@}"
  154. login="${login:-$fulladdr}"
  155. if [ -n "${password+x}" ]; then
  156. createpass
  157. else
  158. getpass
  159. fi
  160. }
  161. createpass() {
  162. echo "$password" >"$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  163. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  164. case "$(uname)" in
  165. Darwin | *BSD) rm -P "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  166. *) shred -u "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  167. esac
  168. rm -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  169. }
  170. errorexit() {
  171. echo "Log-on not successful."
  172. case "$imap" in
  173. imap.gmail.com)
  174. echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
  175. Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
  176. ;;
  177. imap.mail.me.com)
  178. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  179. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  180. ;;
  181. esac
  182. exit 1
  183. }
  184. getpass() { while :; do
  185. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  186. pass insert -f "$passprefix$fulladdr" && break
  187. done; }
  188. getboxes() {
  189. if [ -n "${force+x}" ]; then
  190. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  191. else
  192. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  193. [ -z "$info" ] && errorexit
  194. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  195. fi
  196. [ "$type" = "pop" ] && mailboxes="INBOX"
  197. for x in $(
  198. sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u
  199. echo 0
  200. ); do
  201. idnum=$((idnum + 1))
  202. [ "$idnum" -eq "$x" ] || break
  203. done
  204. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' -)"
  205. }
  206. finalize() {
  207. echo "$toappend" >>"$accdir/$fulladdr.muttrc"
  208. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  209. mkdir -p "$cachedir/$safename/bodies"
  210. echo "$fulladdr (account #$idnum) added successfully."
  211. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" >"$HOME/.urlview"
  212. return 0
  213. }
  214. prepnotmuch() {
  215. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  216. [ -f "$NOTMUCH_CONFIG" ] && return 0
  217. envsubst <"$notmuchtemp" >"$NOTMUCH_CONFIG"
  218. }
  219. togglecron() {
  220. cron="$(mktemp)"
  221. crontab -l >"$cron"
  222. if grep -q mailsync "$cron"; then
  223. echo "Removing automatic mailsync..."
  224. sed -ibu /mailsync/d "$cron"
  225. rm -f "$cron"bu
  226. else
  227. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  228. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync" >>"$cron"
  229. fi &&
  230. crontab "$cron"
  231. rm -f "$cron"
  232. }
  233. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  234. echo "Running $1 with $action..."
  235. echo "Incompatible options given. Only one action may be specified per run."
  236. exit 1
  237. else
  238. action="$1"
  239. fi; }
  240. mwinfo() {
  241. cat <<EOF
  242. mw: mutt-wizard, auto-configure email accounts for mutt
  243. including downloadable mail with \`isync\`.
  244. Main actions:
  245. -a your@email.com Add an email address
  246. -l List email addresses configured
  247. -d Remove an already added address
  248. -D your@email.com Force remove account without confirmation
  249. -t number Toggle automatic mailsync every <number> minutes
  250. -T Toggle automatic mailsync
  251. -r Reorder account numbers
  252. Options allowed with -a:
  253. -u Account login name if not full address
  254. -n "Real name" to be on the email account
  255. -i IMAP/POP server address
  256. -I IMAP/POP server port
  257. -s SMTP server address
  258. -S SMTP server port
  259. -x Password for account (recommended to be in double quotes)
  260. -p Add for a POP server instead of IMAP.
  261. -P Pass Prefix (prefix of the file where password is stored)
  262. -X Delete an account's local email too when deleting.
  263. -o Configure address, but keep mail online.
  264. -f Assume typical English mailboxes without attempting log-on.
  265. NOTE: Once at least one account is added, you can run
  266. \`mbsync -a\` to begin downloading mail.
  267. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  268. EOF
  269. }
  270. reorder() {
  271. tempfile="$(mktemp -u)"
  272. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  273. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  274. # DO NOT reorder rows or rename the accounts in the second column." >"$tempfile"
  275. sed -n "
  276. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  277. " "$muttrc" >>"$tempfile"
  278. ${EDITOR:-vim} "$tempfile" || exit 1
  279. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  280. default="$(sort -n "$tempfile" | head -n 1)"
  281. default="${default#* }"
  282. sed -ibu "
  283. /.* i[0-9] .*.muttrc/d
  284. /^source.*accounts.*.muttrc/d
  285. " "$muttrc" 2>/dev/null
  286. rm -f "$muttrc"bu
  287. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  288. {
  289. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  290. }
  291. ' "$tempfile" >>"$muttrc"
  292. }
  293. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:" o; do case "${o}" in
  294. l) setact list ;;
  295. r) setact reorder1 ;;
  296. d) setact delete ;;
  297. D)
  298. setact delete
  299. fulladdr="$OPTARG"
  300. ;;
  301. y)
  302. setact sync
  303. fulladdr="$OPTARG"
  304. ;;
  305. Y) setact sync ;;
  306. a)
  307. setact add
  308. fulladdr="$OPTARG"
  309. ;;
  310. i)
  311. setact add
  312. imap="$OPTARG"
  313. ;;
  314. I)
  315. setact add
  316. iport="$OPTARG"
  317. ;;
  318. s)
  319. setact add
  320. smtp="$OPTARG"
  321. ;;
  322. S)
  323. setact add
  324. sport="$OPTARG"
  325. ;;
  326. u)
  327. setact add
  328. login="$OPTARG"
  329. ;;
  330. n)
  331. setact add
  332. realname="$OPTARG"
  333. ;;
  334. P)
  335. setact add
  336. passprefix="$OPTARG"
  337. ;;
  338. m)
  339. setact add
  340. maxmes="$OPTARG"
  341. ;;
  342. o)
  343. setact add
  344. type="online"
  345. ;;
  346. p)
  347. setact add
  348. type="pop"
  349. protocol="pop3s"
  350. iport="${iport:-995}"
  351. ;;
  352. f)
  353. setact add
  354. force=True
  355. ;;
  356. x)
  357. setact add
  358. password="$OPTARG"
  359. ;;
  360. X)
  361. setact delete
  362. purge=True
  363. ;;
  364. t)
  365. setact toggle
  366. cronmin="$OPTARG"
  367. ;;
  368. T) setact toggle ;;
  369. h) setact info ;;
  370. \?)
  371. echo "See \`$(basename $0) -h\` for possible options and help."
  372. exit 1
  373. ;;
  374. esac done
  375. [ -z "$action" ] && action="info"
  376. case "$action" in
  377. list) list ;;
  378. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  379. delete) delete ;;
  380. sync)
  381. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  382. mailsync $fulladdr
  383. ;;
  384. toggle) togglecron ;;
  385. reorder) reorder ;;
  386. info)
  387. mwinfo
  388. exit 1
  389. ;;
  390. esac