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.

mw 13 KiB

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