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.
 
 
 
 

392 lines
15 KiB

  1. #!/bin/sh
  2. if [ "$#" -gt 1 ]; then
  3. echo "To many arguments. You will be asked."
  4. exit
  5. fi
  6. if [[ -z $prefix ]]; then
  7. case "$(uname)" in
  8. Linux) prefix="/usr" ;;
  9. *) prefix="/usr/local" ;;
  10. esac
  11. fi
  12. command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  13. [ -z $PASSWORD_STORE_DIR ] && PASSWORD_STORE_DIR="$HOME/.password-store"
  14. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  15. "$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || {
  16. 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 \`$GPG --full-gen-key\` first.\\n"
  17. exit
  18. }
  19. ! command -v $prefix/bin/mbsync >/dev/null && printf "\`mbsync\` must be installed to run mutt-wizard.\\n" && exit
  20. ! command -v $prefix/bin/msmtp >/dev/null && printf "\`msmtp\` must be installed.\\n" && exit
  21. ! ( command -v mutt >/dev/null || command -v neomutt >/dev/null ) && printf "\`mutt\` must be installed.\\n" && exit
  22. mwconfigdir=${XDG_CONFIG_HOME:-$HOME/.config}
  23. # Main mutt config location
  24. mwmuttdir="$mwconfigdir/mutt"
  25. # Directory for account settings
  26. mwaccdir="$mwmuttdir/accounts"
  27. # Location of mail storage
  28. mwmaildir="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}"
  29. # Regex to confirm valid email address
  30. mwemailre=".\+@.\+\\..\+"
  31. mwshare="$prefix/share/mutt-wizard"
  32. if [[ -n $XDG_CONFIG_HOME ]]; then
  33. mwmbsyncrc="$mwconfigdir/isync/mbsyncrc"
  34. else
  35. mwmbsyncrc="$HOME/.mbsyncrc"
  36. fi
  37. mwconfig="$mwshare/mutt-wizard.muttrc"
  38. mwcachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  39. mwmuttrc="$mwmuttdir/muttrc"
  40. mwmsmtprc="$mwconfigdir/msmtp/config"
  41. mwssltype="IMAPS"
  42. mbsyncbin="$prefix/bin/mbsync -c $mwmbsyncrc"
  43. msmtpbin="$prefix/bin/msmtp"
  44. 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/"
  45. do
  46. [ -f "$x" ] && sslcert="$x" && break
  47. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  48. getaccounts() {
  49. accounts="$(find "$mwaccdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc//" | sort -n)"
  50. }
  51. mwlist() {
  52. getaccounts && [ -n "$accounts" ] && echo "$accounts"
  53. }
  54. mwadd() {
  55. asktype && askinfo && tryconnect && finalize || mwdelete
  56. }
  57. getprofiles() {
  58. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  59. printf "Creating profiles for \`%s\`..." "$mwaccount"
  60. msmtp_header="defaults
  61. auth on
  62. tls on
  63. tls_trust_file $sslcert
  64. logfile ${XDG_LOG_HOME:-$HOME}/msmtp.log
  65. "
  66. msmtp_profile="account $mwaccount
  67. host $mwsmtp
  68. port $mwsport
  69. from $mwaddr
  70. user $mwlogin
  71. passwordeval \"pass $mwpass\"
  72. $starttlsoff
  73. "
  74. mbsync_profile="IMAPStore $mwaccount-remote
  75. Host $mwimap
  76. Port $mwiport
  77. User $mwlogin
  78. PassCmd \"pass $mwpass\"
  79. SSLType $mwssltype
  80. CertificateFile $sslcert
  81. MaildirStore $mwaccount-local
  82. Subfolders Verbatim
  83. Path $mwmaildir/$mwaccount/
  84. Inbox $mwmaildir/$mwaccount/INBOX
  85. Flatten .
  86. Channel $mwaccount
  87. Expunge Both
  88. Master :$mwaccount-remote:
  89. Slave :$mwaccount-local:
  90. Patterns * !\"[Gmail]/All Mail\"
  91. Create Both
  92. SyncState *
  93. MaxMessages 0
  94. # End profile
  95. "
  96. if [ "$mwtype" = "offline" ]; then
  97. mutt_profile="# vim: filetype=neomuttrc
  98. # muttrc file for account $mwaccount
  99. set realname = \"$mwname\"
  100. set from = \"$mwaddr\"
  101. set sendmail = \"$msmtpbin -a $mwaccount\"
  102. alias me $mwname <$mwaddr>
  103. set folder = \"$mwmaildir/$mwaccount\"
  104. set mbox_type = Maildir
  105. macro index gm \"<shell-escape>mailsync -V $mwaccount<enter>\" \"sync mail $mwaccount\"
  106. unmailboxes *
  107. "
  108. else
  109. mutt_profile="# vim: filetype=neomuttrc
  110. # muttrc file for account $mwaccount
  111. set realname = \"$mwname\"
  112. set from = \"$mwaddr\"
  113. set sendmail = \"$msmtpbin -a $mwaccount\"
  114. alias me $mwname <$mwaddr>
  115. set folder = \"imaps://$mwaddr@$mwimap:$mwiport\"
  116. set imap_user = \"$mwlogin\"
  117. set header_cache = $mwcachedir/$mwaccount/headers
  118. set message_cachedir = $mwcachedir/$mwaccount/bodies
  119. set imap_pass = \"\`pass $mwpass\`\"
  120. set mbox_type = Maildir
  121. set ssl_starttls = yes
  122. set ssl_force_tls = yes
  123. unmailboxes *
  124. "
  125. fi
  126. printf "DONE.\\n"
  127. }
  128. askinfo() {
  129. if [ -z $mwaddr ]; then
  130. printf "Type the \033[31memail address\033[0m\\n\t\033[36m"
  131. read -r mwaddr
  132. printf "\033[0m"
  133. while ! echo "$mwaddr" | grep "$mwemailre" >/dev/null; do
  134. printf "That is not a valid \033[31memail address\033[0m, please retype\\n\t\033[36m"
  135. read -r mwaddr
  136. printf "\033[0m"
  137. done
  138. fi
  139. domain="$(echo "$mwaddr" | sed "s/.*@//")"
  140. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  141. mwserverinfo="$(grep "^$domain" "$mwshare/domains.csv" 2>/dev/null)"
  142. if [ -z "$mwserverinfo" ]; then
  143. printf "Your email domain is not known to mutt-wizard.\\nType in your settings.\\nUsually you find them by an internet search.\\n"
  144. printf "Type the IMAP server (excluding the port number)\\n\033[36m\t"
  145. read -r mwimap
  146. printf "\033[0mIMAP port number (usually 993)\\n\033[36m\t"
  147. read -r mwiport
  148. printf "\033[0mSMTP server (excluding the port number)\\n\033[36m\t"
  149. read -r mwsmtp
  150. printf "\033[0mSMTP port number (usually 587 or 465)\\n\033[36m\t"
  151. read -r mwsport
  152. printf "\033[0m\\nIf you want, you can copy the line below and add it to the \`domains.csv\` file on Github, for others.\\n\\n%s,%s,%s,%s,%s\\n\\nBut be sure the setting works, first! ;-)\\n" "$domain" "$mwimap" "$mwiport" "$mwsmtp" "$mwsport"
  153. else
  154. IFS=, read -r mwservice mwimap mwiport mwsmtp mwsport <<EOF
  155. $mwserverinfo
  156. EOF
  157. printf "\\n\033[3;33mCongrats!\033[0m Server info is known, so you don't need to look it up!\\n\t\033[1mIMAP server\033[0m: %s\\n\t\033[1mIMAP port\033[0m: %s\\n\t\033[1mSMTP server\033[0m: %s\\n\t\033[1mSMTP port\033[0m: %s\\n" "$imap" "$mwiport" "$mwsmtp" "$mwsport"
  158. case "$mwservice" in
  159. gmail.com) printf "\033[31mGmail: \"less secure\" must be enabled before you continue.\\nDo it now, if you have not done it already:\\nhttps://support.google.com/accounts/answer/6010255\\n\033[0m" ;;
  160. protonmail.ch|protonmail.com|pm.me) printf "\033[31mProtonmail: Users must install and configure Protonmail Bridge for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" && ssltype="None" ;;
  161. esac
  162. [ "$mwsport" = 465 ] && starttlsoff="tls_starttls off"
  163. fi
  164. if [ -z "$mwname" ]; then
  165. printf "Name to associate to email.\\n\t"
  166. read -r mwname
  167. fi
  168. mwaccount=$mwaddr
  169. if [ -z $mwlogin ]; then
  170. printf "Type your account username if different from your email address.\\n\033[34mFor most accounts you can probably leave this blank.\033[0m\\n\tLogin(?): \033[36m"
  171. read -r mwlogin
  172. printf "\033[0m"
  173. [ -z "$mwlogin" ] && mwlogin="$mwaddr"
  174. fi
  175. if [ -z "$mwpass" ]; then
  176. mwpass=mutt-wizard-$mwaccount
  177. getpass
  178. fi
  179. getprofiles
  180. mkdir -p "$mwmuttdir" "$mwaccdir" "$mwcachedir/$mwaccount/bodies" "$mwconfigdir/msmtp"
  181. getaccounts
  182. for x in $(seq 1 9); do echo "$accounts" | grep "$x" >/dev/null 2>&1 || { export idnum="$x"; break ;}; done
  183. [ ! -f "$mwmsmtprc" ] && echo "$msmtp_header" > "$mwmsmtprc"
  184. echo "$msmtp_profile" >> "$mwmsmtprc"
  185. case "$mwservice" in
  186. protonmail.ch|protonmail.com|pm.me) protonfinger || return 1 ;;
  187. esac
  188. echo "$mutt_profile" > "$mwaccdir/$idnum-$mwaccount.muttrc"
  189. mkdir -p ${mwmbsyncrc%/*}
  190. echo "$mbsync_profile" >> "$mwmbsyncrc"
  191. [ ! -f "$mwmuttrc" ] && echo "# vim: filetype=neomuttrc" > "$mwmuttrc" && echo "muttrc created."
  192. ! grep "^source.*mutt-wizard.muttrc" "$mwmuttrc" >/dev/null && echo "source $mwconfig # mw-autogenerated" >> "$mwmuttrc"
  193. ! grep "^source.*.muttrc" "$mwmuttrc" | grep -v "$mwconfig" >/dev/null && echo "source $mwaccdir/$idnum-$mwaccount.muttrc # mw-autogenerated" >> "$mwmuttrc"
  194. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $mwaccdir/$idnum-$mwaccount.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $mwaddr\" # mw-autogenerated" >> "$mwmuttrc"
  195. [ "$mwtype" = "offline" ] && echo "macro index gM '<shell-escape>mailsync -Va<enter>' \"sync all mail\" # mw-autogenerated" >> "$mwmuttrc"
  196. return 0
  197. }
  198. protonfinger() {
  199. printf "Getting Protonmail bridge fingerprint...\\n"
  200. fingerprint="$($msmtpbin --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off)" || return 1
  201. sed -i "s/account $mwaccount/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$mwmsmtprc"
  202. }
  203. getpass() {
  204. while : ; do pass rm -f "$mwpass" >/dev/null 2>&1
  205. pass insert "$mwpass" && break; done ;}
  206. formatShortcut() {
  207. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # mw-autogenerated"
  208. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
  209. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$mwaccdir/$idnum-$mwaccount.muttrc"
  210. done
  211. }
  212. tryconnect() {
  213. if [ -z "$mailboxes" ]; then
  214. mkdir -p $mwmaildir/$mwaccount
  215. mailboxes="$($mbsyncbin -l $mwaccount | sed 's/\//./')" >/dev/null 2>&1
  216. fi
  217. if [ -n "$mailboxes" ]; then
  218. [ "$mwtype" = "online" ] && sed -i "/IMAPStore $mwaccount-remote$/,/# End profile/d" "$mwmbsyncrc"
  219. [ "$mwtype" = "offline" ] && notmuchauto
  220. printf "\033[32mMailboxes detected.\033[0m\\n"
  221. echo "$mailboxes" | xargs -I {} mkdir -p "$mwmaildir/$mwaccount/{}"
  222. return 0
  223. else
  224. 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"
  225. return 1
  226. fi
  227. }
  228. finalize() {
  229. boxes="$(find "$mwmaildir/$mwaccount/" -mindepth 1 -maxdepth 1 | sed "s/\ /\\\ /g;s/^.*\//=/")"
  230. [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that mbsync has not been successfully run.\\nRun mbsync, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$mwaccount" && return
  231. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  232. spoolfile=$(echo "$boxes" | grep -i -m 1 inbox | sed 's/=/+/g')
  233. record=$(echo "$boxes" | grep -i -m 1 sent | sed 's/=/+/g')
  234. postponed=$(echo "$boxes" | grep -i -m 1 draft | sed 's/=/+/g')
  235. trash=$(echo "$boxes" | grep -i -m 1 trash | sed 's/=/+/g')
  236. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$mwaccdir/$idnum-$mwaccount.muttrc"
  237. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$mwaccdir/$idnum-$mwaccount.muttrc"
  238. echo "mailboxes =$mwaccount ===================== $(echo "$boxes" | sed -e "s/^\|$/\"/g" | tr "\n" " ")" >> "$mwaccdir/$idnum-$mwaccount.muttrc"
  239. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  240. sed -i "/# mw-autogenerated/d" "$mwaccdir/$idnum-$mwaccount.muttrc"
  241. echo "$boxes" | grep -i inbox | head -n 1 | formatShortcut i inbox
  242. echo "$boxes" | grep -i sent | head -n 1 | formatShortcut s sent
  243. echo "$boxes" | grep -i draft | head -n 1 | formatShortcut d drafts
  244. echo "$boxes" | grep -i trash | head -n 1 | formatShortcut t trash
  245. echo "$boxes" | grep -i spam | head -n 1 | formatShortcut S spam
  246. echo "$boxes" | grep -i junk | head -n 1 | formatShortcut j junk
  247. echo "$boxes" | grep -i archive | head -n 1 | formatShortcut a archive
  248. [ "$mwtype" = "offline" ] && printf "All done.\\n\033[33mYou can now run \`\033[32mmailsync [%s]\033[33m\` to sync your mail.\033[0m\\n" "$mwaccount"
  249. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  250. return 0
  251. }
  252. confirm() {
  253. printf "[y/N]: Do you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  254. return 0 ;
  255. }
  256. mwpick() {
  257. printf "Select an accounts to %s:\\n" "$1"
  258. mwlist
  259. read -r input
  260. [ -z "$input" ] && return 1
  261. mwaddr="$(echo "$accounts" | grep "$input": | awk '{print $2}')"
  262. mwaccount=$mwaddr
  263. [ -z "$mwaccount" ] && printf "Invalid response." && return 1
  264. return 0 ;
  265. }
  266. mwdelete() {
  267. sed -i "/IMAPStore $mwaccount-remote$/,/# End profile/d" "$mwmbsyncrc"
  268. rm -rf "${mwcachedir:?}/${mwaccount:?}"
  269. rm -rf "$mwaccdir/"[1-9]"-$mwaccount.muttrc"
  270. sed -i "/[0-9]-$mwaccount.muttrc/d" "$mwmuttrc"
  271. sed -i "/account $mwaccount/,/^\(\s*$\|account\)/d" "$mwmsmtprc"
  272. }
  273. mwcron() {
  274. ! pgrep cron >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  275. if crontab -l | grep mailsync >/dev/null; then
  276. echo "Active mail sync cronjob detected. Do you want to remove it?"
  277. printf "\033[36m\t"
  278. read -r rmyn
  279. printf "\033[0m"
  280. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  281. else
  282. echo "How many minutes between each mail sync?"
  283. printf "\033[36m\t"
  284. read -r minnum
  285. printf "\033[0m"
  286. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  287. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  288. read -r minnum
  289. printf "\033[0m"
  290. done
  291. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; $(type mailsync | cut -d' ' -f3)") | crontab - &&
  292. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  293. fi
  294. }
  295. asktype() {
  296. if [ -z "$mwtype" ]; then
  297. while : ; do
  298. printf "[yes/no]: Local mail via mbsync? No: Mutt remotes (slower)\\n\t"
  299. read -r offnot
  300. case "$offnot" in
  301. [Yy][Ee][Ss]) mwtype="offline" && break ;;
  302. [Nn][Oo]) mwtype="online" && break ;;
  303. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  304. esac
  305. done
  306. fi
  307. }
  308. mwpurge() {
  309. confirm "delete all account data" || exit
  310. rm -rf "$mwmbsyncrc" "$mwaccdir" "$mwconfigdir/msmtp" "$mwcachedir"
  311. pgrep cron >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null
  312. sed -i "/\# mw-autogenerated/d" "$mwmuttrc"
  313. echo "All configs and account settings have been purged."
  314. }
  315. notmuchauto() {
  316. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  317. [ -f "$NOTMUCH_CONFIG" ] && return 0
  318. nmbasic="[database]
  319. path=$mwmaildir
  320. [user]
  321. name=$mwname
  322. primary_email=$mwaddr
  323. [new]
  324. tags=unread;inbox;
  325. ignore=
  326. [search]
  327. exclude_tags=deleted;spam;
  328. [maildir]
  329. synchronize_flags=true
  330. [crypto]
  331. gpg_path=$GPG"
  332. mkdir -p ${NOTMUCH_CONFIG%/*}
  333. echo "$nmbasic" > "$NOTMUCH_CONFIG"
  334. }
  335. trap 'echo -e "\033[0m\n"; exit' STOP INT ABRT KILL
  336. if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  337. case "$1" in
  338. ls) mwlist ;;
  339. add) mwadd ;;
  340. pass) mwpick "change the password of" && getpass ;;
  341. delete) mwpick delete && confirm "delete the \`$mwaccount\` profile" && mwdelete ;;
  342. purge) mwpurge ;;
  343. cron) mwcron ;;
  344. *) cat << EOF
  345. mw: mutt-wizard, auto-configure email accounts for mutt
  346. including downloadable mail with \`isync\`.
  347. Allowed options:
  348. add Add and autoconfigure an email address (9 max.)
  349. ls List configured accounts
  350. delete Pick an account to delete
  351. purge Delete all accounts and settings
  352. cron Enable or disable an autosync via cronjob
  353. all else Print this message
  354. NOTE: Once at least one account is added, you can run
  355. \`mailsync -a\` to begin downloading mail.
  356. EOF
  357. esac
  358. fi