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.
 
 
 
 

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