Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

400 wiersze
17 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 \`$GPG --full-gen-key\` first.\\n"
  7. exit
  8. }
  9. ! command -v mbsync >/dev/null && printf "\`mbsync\` must be installed to run mutt-wizard.\\n" && exit
  10. case "$(uname)" in
  11. Linux) prefix="/usr" ;;
  12. *) prefix="/usr/local" ;;
  13. esac
  14. usage() {
  15. cat << EOF
  16. usage: $0 [options] <command>
  17. mw: mutt-wizard, auto-configure email accounts for mutt
  18. including downloadable mail with \`isync\`.
  19. Options:
  20. -p | --prefix <PATH> Path where share dir may be expected.
  21. -h | --help This message
  22. Commands:
  23. add Add and autoconfigure an email address (9 max.)
  24. ls List configured accounts
  25. delete Pick an account to delete
  26. purge Delete all accounts and settings
  27. cron Enable or disable an autosync via cronjob
  28. NOTE: Once at least one account is added, you can run
  29. \`mbsync -a\` to begin downloading mail.
  30. EOF
  31. }
  32. is_ls=false
  33. is_add=false
  34. is_pass=false
  35. is_delete=false
  36. is_purge=false
  37. is_cron=false
  38. [[ $# -eq 0 ]] && usage && exit 1
  39. while [[ $# -gt 0 ]]; do
  40. case $1 in
  41. ls) is_ls=true ;;
  42. add) is_add=true ;;
  43. pass) is_pass=true ;;
  44. delete) is_deletee=true ;;
  45. purge) is_purge=true ;;
  46. cron) is_cron=true ;;
  47. -p | --prefix)
  48. [[ -z "$2" ]] && usage && exit 1
  49. prefix=$2
  50. shift 1
  51. ;;
  52. -h | --help)
  53. usage
  54. exit 0
  55. ;;
  56. *)
  57. usage
  58. exit 1
  59. esac
  60. shift 1
  61. done
  62. muttdir="$HOME/.config/mutt" # Main mutt config location
  63. accdir="$muttdir/accounts" # Directory for account settings
  64. maildir="$HOME/.local/share/mail" # Location of mail storage
  65. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  66. emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
  67. muttshare="$prefix/share/mutt-wizard"
  68. mbsyncrc="$HOME/.mbsyncrc"
  69. mwconfig="$muttshare/mutt-wizard.muttrc"
  70. cachedir="$HOME/.cache/mutt-wizard"
  71. muttrc="$muttdir/muttrc"
  72. msmtprc="$HOME/.config/msmtp/config"
  73. ssltype="IMAPS" # This is later changed to `None` later in the script if using Protonmail
  74. 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"
  75. do
  76. [ -f "$x" ] && sslcert="$x" && break
  77. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  78. getaccounts() { accounts="$(find "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\..*//" | sort -n)" ;}
  79. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  80. getprofiles() { \
  81. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  82. printf "Creating profiles for \`%s\`..." "$title"
  83. msmtp_header="defaults
  84. auth on
  85. tls on
  86. tls_trust_file $sslcert
  87. logfile ~/.config/msmtp/msmtp.log
  88. "
  89. msmtp_profile="account $title
  90. host $smtp
  91. port $sport
  92. from $fulladdr
  93. user $login
  94. passwordeval \"pass mutt-wizard-$title\"
  95. $starttlsoff
  96. "
  97. mbsync_profile="IMAPStore $title-remote
  98. Host $imap
  99. Port $iport
  100. User $login
  101. PassCmd \"pass mutt-wizard-$title\"
  102. SSLType $ssltype
  103. CertificateFile $sslcert
  104. MaildirStore $title-local
  105. Subfolders Verbatim
  106. Path ~/.local/share/mail/$title/
  107. Inbox ~/.local/share/mail/$title/INBOX
  108. Flatten .
  109. Channel $title
  110. Expunge Both
  111. Master :$title-remote:
  112. Slave :$title-local:
  113. Patterns * !\"[Gmail]/All Mail\"
  114. Create Both
  115. SyncState *
  116. MaxMessages $maxmes
  117. # End profile
  118. "
  119. if [ "$accounttype" = "offline" ]; then
  120. mutt_profile="# vim: filetype=neomuttrc
  121. # muttrc file for account $title
  122. set realname = \"$realname\"
  123. set from = \"$fulladdr\"
  124. set sendmail = \"msmtp -a $title\"
  125. alias me $realname <$fulladdr>
  126. set folder = \"$maildir/$title\"
  127. set header_cache = $cachedir/$title/headers
  128. set message_cachedir = $cachedir/$title/bodies
  129. set mbox_type = Maildir
  130. bind index,pager gg noop
  131. bind index,pager g noop
  132. bind index,pager M noop
  133. bind index,pager C noop
  134. bind index gg first-entry
  135. macro index o \"<shell-escape>mailsync -V $title<enter>\" \"run mbsync to sync $title\"
  136. unmailboxes *
  137. "
  138. else
  139. mutt_profile="# vim: filetype=neomuttrc
  140. # muttrc file for account $title
  141. set realname = \"$realname\"
  142. set from = \"$fulladdr\"
  143. set sendmail = \"$prefix/bin/msmtp -a $title\"
  144. alias me $realname <$fulladdr>
  145. set folder = \"imaps://$fulladdr@$imap:$iport\"
  146. set imap_user = \"$login\"
  147. set header_cache = $cachedir/$title/headers
  148. set message_cachedir = $cachedir/$title/bodies
  149. set imap_pass = \`pass mutt-wizard-$title\`
  150. set mbox_type = Maildir
  151. set ssl_starttls = yes
  152. set ssl_force_tls = yes
  153. bind index,pager gg noop
  154. bind index,pager g noop
  155. bind index,pager M noop
  156. bind index,pager C noop
  157. bind index gg first-entry
  158. unmailboxes *
  159. "
  160. fi
  161. printf "DONE.\\n"
  162. }
  163. askinfo() { \
  164. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  165. read -r fulladdr
  166. printf "\033[0m"
  167. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  168. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  169. read -r fulladdr
  170. printf "\033[0m"
  171. done
  172. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  173. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  174. serverinfo="$(grep "^$domain" "$muttshare/domains.csv" 2>/dev/null)"
  175. if [ -z "$serverinfo" ]; then
  176. printf "Your email domain is not in mutt-wizard's database yet.\\nmutt-wizard will still autoconfigure everything, but you will have to manually type in your service's IMAP and SMTP server information.\\nYou can usually quickly find this by internet searching for it.\\n"
  177. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  178. read -r imap
  179. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  180. read -r iport
  181. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  182. read -r smtp
  183. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  184. read -r sport
  185. printf "\033[0m\\nGreat! If you want to be helpful, copy the line below and you can add it to the \`domains.csv\` file on Github.\\nThis will make things easier for others who use your email provider.\\n\\n%s,%s,%s,%s,%s\\n\\nAlthough be sure to test to see if these settings work first! ;-)\\n" "$domain" "$imap" "$iport" "$smtp" "$sport"
  186. else
  187. IFS=, read -r service imap iport smtp sport <<EOF
  188. $serverinfo
  189. EOF
  190. printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically been found, so you won't have to look anything 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\\nThis data will be used by the wizard.\\n" "$imap" "$iport" "$smtp" "$sport"
  191. case "$service" in
  192. gmail.com) printf "\033[31mREMEMBER: Gmail users must enable \"less secure\" (third-party) applications first for the sync to work:\\nhttps://support.google.com/accounts/answer/6010255\\n\033[0m" ;;
  193. protonmail.ch|protonmail.com|pm.me) printf "\033[31mREMEMBER: Protonmail users must install and configure Protonmail Bridge first for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" && ssltype="None" ;;
  194. esac
  195. [ "$sport" = 465 ] && starttlsoff="tls_starttls off"
  196. fi
  197. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  198. read -r realname
  199. printf "Enter a short, \033[36mone-word identifier\033[0m for this email account that will distinguish them from any other accounts you add.\\n\tAccount name: "
  200. read -r title
  201. while ! echo "$title" | grep "$namere" >/dev/null || ls "$accdir"/[0-9]"-$title.muttrc" >/dev/null 2>&1; do
  202. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or - and that you have \033[1mnot\033[0m used before.\\n\tAccount name: \033[36m\t"
  203. read -r title
  204. printf "\033[0m"
  205. done
  206. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\033[34mMost accounts will not have a separate login, so you should probably leave this blank.\033[0m\\n\tLogin(?): \033[36m"
  207. read -r login
  208. printf "\033[0m"
  209. [ -z "$login" ] && login="$fulladdr"
  210. [ "$accounttype" = "offline" ] && printf "If you want to limit the number of messages kept offline to a number, enter that number below. If you do not want to limit your mail and would like \`mbsync\` to sync all mail, press enter without typing a number.\\n\t" && read -r maxmes
  211. echo "$maxmes" | grep "[1-9]" >/dev/null || maxmes="0"
  212. getpass
  213. getprofiles
  214. mkdir -p "$muttdir" "$accdir" "$cachedir/$title/bodies" "$HOME/.config/msmtp"
  215. getaccounts
  216. for x in $(seq 1 9); do echo "$accounts" | grep "$x" >/dev/null 2>&1 || { export idnum="$x"; break ;}; done
  217. [ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
  218. echo "$msmtp_profile" >> "$msmtprc"
  219. command -V apt-get >/dev/null 2>&1 && ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  220. case "$service" in
  221. protonmail.ch|protonmail.com|pm.me) protonfinger || return 1 ;;
  222. esac
  223. echo "$mutt_profile" > "$accdir/$idnum-$title.muttrc"
  224. echo "$mbsync_profile" >> "$mbsyncrc"
  225. notmuchauto
  226. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created."
  227. ! grep "^source.*mutt-wizard.muttrc" "$muttrc" >/dev/null && echo "source $mwconfig # mw-autogenerated" >> "$muttrc"
  228. ! grep "^source.*.muttrc" "$muttrc" | grep -v "$mwconfig" >/dev/null && echo "source $accdir/$idnum-$title.muttrc # mw-autogenerated" >> "$muttrc"
  229. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$title.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\" # mw-autogenerated" >> "$muttrc"
  230. }
  231. protonfinger() { printf "Getting Protonmail bridge fingerprint...\\n"
  232. fingerprint="$(msmtp --serverinfo --tls --tls-certcheck=off -a "$title")" || return 1
  233. sed -ibu "s/account $title/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$msmtprc" ; rm -f "$msmtprc"bu
  234. }
  235. getpass() { while : ; do pass rm -f "mutt-wizard-$title" >/dev/null 2>&1
  236. pass insert "mutt-wizard-$title" && break; done ;}
  237. formatShortcut() { \
  238. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # mw-autogenerated"
  239. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
  240. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$accdir/$idnum-$title.muttrc"
  241. done ;}
  242. tryconnect() { mkdir -p "$maildir/$title"
  243. if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1 && [ -n "$mailboxes" ]; then
  244. [ "$accounttype" = "online" ] && sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  245. printf "\033[32mMailboxes detected.\033[0m\\n"
  246. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  247. return 0
  248. else
  249. 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"
  250. return 1
  251. fi ;}
  252. finalize() { \
  253. boxes="$(find "$maildir/$title/" -mindepth 1 -maxdepth 1 | sed "s/\ /\\\ /g;s/^.*\//=/")"
  254. [ -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" "$title" && return
  255. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  256. spoolfile=$(echo "$boxes" | grep -i -m 1 inbox | sed 's/=/+/g')
  257. record=$(echo "$boxes" | grep -i -m 1 sent | sed 's/=/+/g')
  258. postponed=$(echo "$boxes" | grep -i -m 1 draft | sed 's/=/+/g')
  259. trash=$(echo "$boxes" | grep -i -m 1 trash | sed 's/=/+/g')
  260. sed -ibu "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  261. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$idnum-$title.muttrc"
  262. echo "mailboxes =$title ===================== $(echo "$boxes" | sed -e "s/^\|$/\"/g" | tr "\n" " ")" >> "$accdir/$idnum-$title.muttrc"
  263. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  264. sed -ibu "/# mw-autogenerated/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  265. echo "$boxes" | grep -i inbox | head -n 1 | formatShortcut i inbox
  266. echo "$boxes" | grep -i sent | head -n 1 | formatShortcut s sent
  267. echo "$boxes" | grep -i draft | head -n 1 | formatShortcut d drafts
  268. echo "$boxes" | grep -i trash | head -n 1 | formatShortcut t trash
  269. echo "$boxes" | grep -i spam | head -n 1 | formatShortcut S spam
  270. echo "$boxes" | grep -i junk | head -n 1 | formatShortcut j junk
  271. echo "$boxes" | grep -i archive | head -n 1 | formatShortcut a archive
  272. [ "$accounttype" = "offline" ] && printf "All done.\\n\033[33mYou should now be able to run \`\033[32mmbsync %s\033[33m\` to begin to download your mail.\033[0m\\n" "$title"
  273. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  274. return 0
  275. }
  276. confirm() { printf "Do you want to %s? [y/N]\\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
  277. printf "Are you really, really sure 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
  278. return 0 ;}
  279. pick() { printf "Select an accounts to %s:\\n" "$1"
  280. list
  281. read -r input
  282. [ -z "$input" ] && return 1
  283. title="$(echo "$accounts" | grep "$input" | awk '{print $2}')"
  284. [ -z "$title" ] && printf "Invalid response." && return 1
  285. return 0 ;}
  286. delete() { sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -rf "$mbsyncrc"bu
  287. rm -rf "${cachedir:?}/${title:?}" "$accdir/"[1-9]"-$title.muttrc"
  288. sed -ibu "/[0-9]-$title.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  289. sed -ibu "/account $title/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  290. }
  291. choosecron() { ! pgrep cron >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  292. if crontab -l | grep mailsync >/dev/null; then
  293. echo "Active mail sync cronjob detected. Do you want to remove it?"
  294. printf "\033[36m\t"
  295. read -r rmyn
  296. printf "\033[0m"
  297. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  298. else
  299. echo "How many minutes between each mail sync?"
  300. printf "\033[36m\t"
  301. read -r minnum
  302. printf "\033[0m"
  303. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  304. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  305. read -r minnum
  306. printf "\033[0m"
  307. done
  308. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; . \$HOME/.profile; $(type mailsync | cut -d' ' -f3)") | crontab - &&
  309. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  310. fi ;}
  311. asktype() { while : ; do
  312. printf "Do you want to keep your mail for this account offline with mbsync? [yes/no]\\n\t"
  313. read -r offnot
  314. case "$offnot" in
  315. [Yy][Ee][Ss]) accounttype="offline" && break ;;
  316. [Nn][Oo]) accounttype="online" && break ;;
  317. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  318. esac; done ;}
  319. purge() { confirm "delete all account data" || exit
  320. rm -rf "$mbsyncrc" "$accdir" "$HOME/.config/msmtp" "$cachedir"
  321. echo "All configs and account settings have been purged."
  322. sed -ibu "/\# mw-autogenerated/d" "$muttrc" ; rm -f "$muttrc"bu
  323. }
  324. notmuchauto() { \
  325. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  326. [ -f "$NOTMUCH_CONFIG" ] && return 0
  327. nmbasic="[database]
  328. path=$maildir
  329. [user]
  330. name=$realname
  331. primary_email=$fulladdr
  332. [new]
  333. tags=unread;inbox;
  334. ignore=
  335. [search]
  336. exclude_tags=deleted;spam;
  337. [maildir]
  338. synchronize_flags=true
  339. [crypto]
  340. gpg_path=$GPG"
  341. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  342. trap 'echo -e "\033[0m\n"' INT
  343. if $is_ls ; then
  344. list
  345. elif $is_add ; then
  346. asktype && askinfo && tryconnect && finalize || delete
  347. elif $is_pass ; then
  348. pick "change the password of" && getpass
  349. elif $is_delete ; then
  350. pick delete && confirm "delete the \`$title\` profile" && delete
  351. elif $is_purge ; then
  352. purge
  353. elif $is_cron ; then
  354. choosecron
  355. else
  356. usage && exit 1
  357. fi