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.
 
 
 
 

361 lines
13 KiB

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