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.
 
 
 
 

359 lines
12 KiB

  1. #!/bin/sh
  2. prefix="/usr/local"
  3. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail"
  4. muttshare="$prefix/share/mutt-wizard"
  5. mwconfig="$muttshare/mutt-wizard.muttrc"
  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_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log"
  11. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  12. mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config"
  13. alias mbsync='mbsync -c "$mbsyncrc"'
  14. for x in "/etc/ssl/certs/ca-certificates.crt" "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" "/etc/ssl/cert.pem" "/usr/local/share/ca-certificates/"; do
  15. [ -f "$x" ] && sslcert="$x" && break
  16. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  17. checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  18. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  19. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  20. "$GPG" --list-secret-keys "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" >/dev/null 2>&1 || {
  21. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  22. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  23. exit 1 ;} ;}
  24. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc$//" | sort -n)" ;}
  25. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  26. prepmsmtp() { echo "account $fulladdr
  27. host $smtp
  28. port ${sport:-587}
  29. from $fulladdr
  30. user $login
  31. passwordeval \"pass $fulladdr\"
  32. auth on
  33. ${tlsline:-tls on}
  34. tls_trust_file $sslcert
  35. logfile $msmtplog
  36. " >> "$msmtprc"
  37. # On Ubuntu/Debian, a link is needed since they use an older version.
  38. command -V apt-get >/dev/null 2>&1 && ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  39. }
  40. prepmbsync() { mkdir -p "${mbsyncrc%/*}"
  41. echo "
  42. IMAPStore $fulladdr-remote
  43. Host $imap
  44. Port ${iport:-993}
  45. User $login
  46. PassCmd \"pass $fulladdr\"
  47. AuthMechs LOGIN
  48. SSLType ${imapssl:-IMAPS}
  49. CertificateFile $sslcert
  50. MaildirStore $fulladdr-local
  51. Subfolders Verbatim
  52. Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/
  53. Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
  54. Channel $fulladdr
  55. Expunge Both
  56. Master :$fulladdr-remote:
  57. Slave :$fulladdr-local:
  58. Patterns * !\"[Gmail]/All Mail\"
  59. Create Both
  60. SyncState *
  61. MaxMessages ${maxmes:-0}
  62. ExpireUnread no
  63. # End profile
  64. " >> "$mbsyncrc" ;}
  65. prepmpop() { echo "
  66. account $fulladdr
  67. tls on
  68. user $login
  69. host $imap
  70. delivery maildir ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
  71. passwordeval pass $fulladdr
  72. " >> "$mpoprc" ;}
  73. prepmutt() { echo "# vim: filetype=neomuttrc
  74. # muttrc file for account $fulladdr
  75. set realname = \"$realname\"
  76. set from = \"$fulladdr\"
  77. set sendmail = \"msmtp -a $fulladdr\"
  78. alias me $realname <$fulladdr>
  79. set folder = \"$folder\"
  80. set header_cache = $cachedir/$fulladdr/headers
  81. set message_cachedir = $cachedir/$fulladdr/bodies
  82. set mbox_type = Maildir
  83. $extra
  84. bind index,pager gg noop
  85. bind index,pager g noop
  86. bind index,pager M noop
  87. bind index,pager C noop
  88. bind index gg first-entry
  89. unmailboxes *
  90. unalternates *
  91. unset signature
  92. unmacro index o
  93. $synccmd
  94. " > "$accdir/$idnum-$fulladdr.muttrc"
  95. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  96. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $mwconfig" >> "$muttrc"
  97. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$mwconfig" && echo "source $accdir/$idnum-$fulladdr.muttrc" >> "$muttrc"
  98. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
  99. }
  100. getprofiles() { \
  101. mkdir -p "${muttrc%/*}" "$accdir" "$cachedir/$fulladdr/bodies" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp"
  102. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  103. case "$iport" in
  104. 1143) imapssl=None ;;
  105. 143) imapssl=STARTTLS ;;
  106. esac
  107. case "$type" in
  108. online) folder="imaps://$login@$imap:${iport:-993}"
  109. extra="set imap_user = \"$login\"
  110. set imap_pass = \"\`pass $fulladdr\`\"
  111. set ssl_starttls = yes
  112. set ssl_force_tls = yes"
  113. ;;
  114. pop) synccmd="macro index o \"<shell-escape>mpop $fulladdr<enter>\" \"run mpop to get $fulladdr's mail\""
  115. prepmpop ;;
  116. *) synccmd="macro index o \"<shell-escape>mw -y $fulladdr<enter>\" \"run mbsync to sync $fulladdr\""
  117. folder="$maildir/$fulladdr"
  118. prepmbsync ;;
  119. esac
  120. prepmsmtp
  121. prepmutt
  122. prepnotmuch # Create a notmuch config file if not present already.
  123. }
  124. parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  125. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  126. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  127. $serverinfo
  128. EOF
  129. imap="${imap:-$imapsugg}"
  130. smtp="${smtp:-$smtpsugg}"
  131. sport="${sport:-$sportsugg}"
  132. iport="${iport:-$iportsugg}"
  133. }
  134. delete() { if [ -z "${fulladdr+x}" ]; then
  135. echo "Select the account your would like to delete (by number):"
  136. list
  137. read -r input
  138. match="^$input:"
  139. else
  140. match=" $fulladdr$"
  141. getaccounts
  142. fi
  143. fulladdr="$(echo "$accounts" | grep "$match" | cut -f2 -d' ')"
  144. [ -z "$fulladdr" ] && echo "Invalid account name given." && return 1
  145. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  146. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/"[1-9]"-$fulladdr.muttrc"
  147. sed -ibu "/[0-9]-$fulladdr.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  148. sed -ibu "/account $fulladdr/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  149. pass rm -f "$fulladdr" >/dev/null 2>&1
  150. # Get rid of those multiple newlines because I don't know awk well enough to do it by default lol.
  151. for file in "$msmtprc" "$mbsyncrc"; do
  152. tr '\n' '|' < "$file" | sed "s/||\+/||/g" | tr '|' '\n' >> "$file"bu
  153. mv -f "$file"bu "$file"
  154. done
  155. }
  156. askinfo() { \
  157. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  158. read -r fulladdr
  159. while ! echo "$fulladdr" | grep -qE ".+@.+\..+"; do
  160. echo "\`$fulladdr\` is not a valid email address. Please retype the address:"
  161. read -r fulladdr
  162. done
  163. getaccounts; echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  164. { echo "$fulladdr has already been added" && exit 1 ;}
  165. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  166. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  167. read -r imap
  168. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  169. read -r smtp
  170. [ "$sport" = 465 ] && tlsline="tls_starttls off"
  171. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  172. login="${login:-$fulladdr}"
  173. if [ -n "${password+x}" ]; then
  174. createpass
  175. else
  176. getpass
  177. fi
  178. }
  179. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$fulladdr"
  180. "$GPG" -qer "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" "$PASSWORD_STORE_DIR/$fulladdr"
  181. rm -f "$PASSWORD_STORE_DIR/$fulladdr" ;}
  182. getpass() { while : ; do pass rm -f "$fulladdr" >/dev/null 2>&1
  183. pass insert "$fulladdr" && break; done ;}
  184. formatShortcut() { toappend="$toappend
  185. macro index,pager g$1 \"<change-folder>=$3<enter>\" \"go to $2\"
  186. macro index,pager M$1 \";<save-message>=$3<enter>\" \"move mail to $2\"
  187. macro index,pager C$1 \";<copy-message>=$3<enter>\" \"copy mail to $2\"" ;}
  188. setBox() { toappend="$toappend
  189. set $1 = \"+$2\"" ;}
  190. getboxes() { if [ -n "${force+x}" ]; then
  191. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  192. else
  193. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass $fulladdr)" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  194. [ -z "$info" ] && echo "Log-on not successful." && return 1
  195. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d ' ')"
  196. fi
  197. getaccounts; for x in $(seq 1 9); do echo "$accounts" | grep -q "^$x:" || { export idnum="$x"; break ;}; done
  198. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' - )"
  199. for x in $mailboxes; do
  200. case "$x" in
  201. *[Ss][Ee][Nn][Tt]*) setBox record "$x"; formatShortcut s sent "$x" ;;
  202. *[Dd][Rr][Aa][Ff][Tt][Ss]*) setBox postponed "$x"; formatShortcut d drafts "$x" ;;
  203. *[Tt][Rr][Aa][Ss][Hh]*) formatShortcut t trash "$x"; setBox trash "$x" ;;
  204. *[Jj][Uu][Nn][Kk]*) formatShortcut j junk "$x" ;;
  205. *[Aa][Rr][Cc][Hh][Ii][Vv][Ee]*) formatShortcut a archive "$x" ;;
  206. *[Ss][Pp][Aa][Mm]*) formatShortcut S spam "$x" ;;
  207. *[Ii][Nn][Bb][Oo][Xx]*) formatShortcut i inbox "$x"; setBox spoolfile "$x" inbox="$x" ;;
  208. esac
  209. done
  210. }
  211. finalize() { echo "$toappend" >> "$accdir/$idnum-$fulladdr.muttrc"
  212. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  213. echo "$fulladdr (account #$idnum) added successfully."
  214. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  215. return 0 ;}
  216. prepnotmuch() { \
  217. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  218. [ -f "$NOTMUCH_CONFIG" ] && return 0
  219. nmbasic="[database]
  220. path=$maildir
  221. [user]
  222. name=$realname
  223. primary_email=$fulladdr
  224. [new]
  225. tags=unread;inbox;
  226. ignore=.mbsyncstate;.uidvalidity
  227. [search]
  228. exclude_tags=deleted;spam;
  229. [maildir]
  230. synchronize_flags=true
  231. [crypto]
  232. gpg_path=$GPG"
  233. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  234. togglecron() { cron="$(mktemp)"
  235. crontab -l > "$cron"
  236. if grep -q mailsync "$cron"; then
  237. echo "Removing automatic mailsync..."
  238. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  239. else
  240. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  241. echo "*/${cronmin-10} * * * * $prefix/bin/mailsync" >> "$cron"
  242. fi &&
  243. crontab "$cron"; rm -f "$cron" ;}
  244. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  245. echo "Running $1 with $action..."
  246. echo "Incompatible options given. Only one action may be specified per run."
  247. return 1
  248. else
  249. action="$1"
  250. fi; }
  251. mwinfo() { cat << EOF
  252. mw: mutt-wizard, auto-configure email accounts for mutt
  253. including downloadable mail with \`isync\`.
  254. Main actions:
  255. -a your@email.com Add an email address
  256. -l List email addresses configured
  257. -d Remove an already added address
  258. -D your@email.com Force remove account without confirmation
  259. -y your@email.com Sync mail for account by name
  260. -Y Sync mail for all accounts
  261. -t number Toggle automatic mailsync every <number> minutes
  262. -T Toggle automatic mailsync
  263. Options allowed with -a:
  264. -u Account login name if not full address
  265. -n "Real name" to be on the email account
  266. -i IMAP/POP server address
  267. -I IMAP/POP server port
  268. -s SMTP server address
  269. -S SMTP server port
  270. -x Password for account (recommended to be in double quotes)
  271. -p Install for a Protonmail account.
  272. -P Add for a POP server instead of IMAP
  273. -o Configure address, but keep mail online.
  274. -f Assume typical English mailboxes without attempting log-on.
  275. NOTE: Once at least one account is added, you can run
  276. \`mbsync -a\` to begin downloading mail.
  277. To change an account's password, run \`pass edit your@email.com\`.
  278. EOF
  279. }
  280. while getopts "fpPlhodTYD:y:i:I:s:S:u:a:n:x:m:t:" o; do case "${o}" in
  281. l) setact list || exit 1 ;;
  282. d) setact delete || exit 1 ;;
  283. D) setact delete || exit 1 ; fulladdr="$OPTARG" ;;
  284. y) setact sync || exit 1 ; fulladdr="$OPTARG" ;;
  285. Y) setact sync || exit 1 ;;
  286. a) setact add || exit 1 ; fulladdr="$OPTARG" ;;
  287. i) setact add || exit 1 ; imap="$OPTARG" ;;
  288. I) setact add || exit 1 ; iport="$OPTARG" ;;
  289. s) setact add || exit 1 ; smtp="$OPTARG" ;;
  290. S) setact add || exit 1 ; sport="$OPTARG" ;;
  291. u) setact add || exit 1 ; login="$OPTARG" ;;
  292. n) setact add || exit 1 ; realname="$OPTARG" ;;
  293. m) setact add || exit 1 ; maxmes="$OPTARG" ;;
  294. o) setact add || exit 1 ; type="online" ;;
  295. P) setact add || exit 1 ; type="pop" ;;
  296. f) setact add || exit 1 ; force=True ;;
  297. x) setact add || exit 1 ; password="$OPTARG" ;;
  298. t) setact toggle || exit 1 ; cronmin="$OPTARG" ;;
  299. T) setact toggle || exit 1 ;;
  300. p) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work."
  301. protocol="imap"
  302. imap="127.0.0.1"
  303. iport="1143"
  304. smtp="127.0.0.1"
  305. sport="1025"
  306. tlsline="tls_fingerprint $(msmtp --serverinfo --host=$smtp --port=$sport --tls --tls-certcheck=off | awk '/SHA256:/ {print $2}')"
  307. setact add || exit 1
  308. ;;
  309. *) mwinfo; exit 1 ;;
  310. esac done
  311. case "$action" in
  312. list) list ;;
  313. add) checkbasics && askinfo && getboxes && getprofiles && finalize || { delete ; exit 1 ;} ;;
  314. delete) delete ;;
  315. sync) mailsync $fulladdr ;;
  316. toggle) togglecron ;;
  317. *) mwinfo; exit 1 ;;
  318. esac