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.
 
 
 
 

372 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. muttdir="$HOME/.config/mutt" # Main mutt config location
  15. accdir="$muttdir/accounts" # Directory for account settings
  16. maildir="$HOME/.local/share/mail" # Location of mail storage
  17. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  18. emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
  19. muttshare="$prefix/share/mutt-wizard"
  20. mbsyncrc="$HOME/.mbsyncrc"
  21. mwconfig="$muttshare/mutt-wizard.muttrc"
  22. cachedir="$HOME/.cache/mutt-wizard"
  23. muttrc="$muttdir/muttrc"
  24. msmtprc="$HOME/.config/msmtp/config"
  25. ssltype="IMAPS" # This is later changed to `None` later in the script if using Protonmail
  26. 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/"
  27. do
  28. [ -f "$x" ] && sslcert="$x" && break
  29. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  30. getaccounts() { accounts="$(find "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\..*//" | sort -n)" ;}
  31. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  32. getprofiles() { \
  33. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  34. printf "Creating profiles for \`%s\`..." "$title"
  35. msmtp_header="defaults
  36. auth on
  37. tls on
  38. tls_trust_file $sslcert
  39. logfile ~/.config/msmtp/msmtp.log
  40. "
  41. msmtp_profile="account $title
  42. host $smtp
  43. port $sport
  44. from $fulladdr
  45. user $login
  46. passwordeval \"pass mutt-wizard-$title\"
  47. $starttlsoff
  48. "
  49. mbsync_profile="IMAPStore $title-remote
  50. Host $imap
  51. Port $iport
  52. User $login
  53. PassCmd \"pass mutt-wizard-$title\"
  54. SSLType $ssltype
  55. CertificateFile $sslcert
  56. MaildirStore $title-local
  57. Subfolders Verbatim
  58. Path ~/.local/share/mail/$title/
  59. Inbox ~/.local/share/mail/$title/INBOX
  60. Flatten .
  61. Channel $title
  62. Expunge Both
  63. Master :$title-remote:
  64. Slave :$title-local:
  65. Patterns * !\"[Gmail]/All Mail\"
  66. Create Both
  67. SyncState *
  68. MaxMessages $maxmes
  69. # End profile
  70. "
  71. mbsync_protonmail_profile="IMAPStore $title-remote
  72. Host $imap
  73. Port $iport
  74. User $login
  75. PassCmd \"pass mutt-wizard-$title\"
  76. SSLType $ssltype
  77. AuthMechs LOGIN
  78. CertificateFile $sslcert
  79. MaildirStore $title-local
  80. Subfolders Verbatim
  81. Path ~/.local/share/mail/$title/
  82. Inbox ~/.local/share/mail/$title/INBOX
  83. Flatten .
  84. Channel $title
  85. Expunge Both
  86. Master :$title-remote:
  87. Slave :$title-local:
  88. Patterns * !\"[Gmail]/All Mail\"
  89. Create Both
  90. SyncState *
  91. MaxMessages $maxmes
  92. # End profile
  93. "
  94. if [ "$accounttype" = "offline" ]; then
  95. mutt_profile="# vim: filetype=neomuttrc
  96. # muttrc file for account $title
  97. set realname = \"$realname\"
  98. set from = \"$fulladdr\"
  99. set sendmail = \"msmtp -a $title\"
  100. alias me $realname <$fulladdr>
  101. set folder = \"$maildir/$title\"
  102. set header_cache = $cachedir/$title/headers
  103. set message_cachedir = $cachedir/$title/bodies
  104. set mbox_type = Maildir
  105. bind index,pager gg noop
  106. bind index,pager g noop
  107. bind index,pager M noop
  108. bind index,pager C noop
  109. bind index gg first-entry
  110. macro index o \"<shell-escape>mailsync -V $title<enter>\" \"run mbsync to sync $title\"
  111. unmailboxes *
  112. "
  113. else
  114. mutt_profile="# vim: filetype=neomuttrc
  115. # muttrc file for account $title
  116. set realname = \"$realname\"
  117. set from = \"$fulladdr\"
  118. set sendmail = \"$prefix/bin/msmtp -a $title\"
  119. alias me $realname <$fulladdr>
  120. set folder = \"imaps://$fulladdr@$imap:$iport\"
  121. set imap_user = \"$login\"
  122. set header_cache = $cachedir/$title/headers
  123. set message_cachedir = $cachedir/$title/bodies
  124. set imap_pass = \"\`pass mutt-wizard-$title\`\"
  125. set mbox_type = Maildir
  126. set ssl_starttls = yes
  127. set ssl_force_tls = yes
  128. bind index,pager gg noop
  129. bind index,pager g noop
  130. bind index,pager M noop
  131. bind index,pager C noop
  132. bind index gg first-entry
  133. unmailboxes *
  134. "
  135. fi
  136. printf "DONE.\\n"
  137. }
  138. askinfo() { \
  139. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  140. read -r fulladdr
  141. printf "\033[0m"
  142. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  143. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  144. read -r fulladdr
  145. printf "\033[0m"
  146. done
  147. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  148. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  149. serverinfo="$(grep "^$domain" "$muttshare/domains.csv" 2>/dev/null)"
  150. if [ -z "$serverinfo" ]; then
  151. 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"
  152. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  153. read -r imap
  154. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  155. read -r iport
  156. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  157. read -r smtp
  158. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  159. read -r sport
  160. 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"
  161. else
  162. IFS=, read -r service imap iport smtp sport <<EOF
  163. $serverinfo
  164. EOF
  165. 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"
  166. case "$service" in
  167. 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" ;;
  168. 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" ;;
  169. esac
  170. [ "$sport" = 465 ] && starttlsoff="tls_starttls off"
  171. fi
  172. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  173. read -r realname
  174. 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: "
  175. read -r title
  176. while ! echo "$title" | grep "$namere" >/dev/null || ls "$accdir"/[0-9]"-$title.muttrc" >/dev/null 2>&1; do
  177. 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"
  178. read -r title
  179. printf "\033[0m"
  180. done
  181. 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"
  182. read -r login
  183. printf "\033[0m"
  184. [ -z "$login" ] && login="$fulladdr"
  185. [ "$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
  186. echo "$maxmes" | grep "[1-9]" >/dev/null || maxmes="0"
  187. getpass
  188. getprofiles
  189. mkdir -p "$muttdir" "$accdir" "$cachedir/$title/bodies" "$HOME/.config/msmtp"
  190. getaccounts
  191. for x in $(seq 1 9); do echo "$accounts" | grep "$x" >/dev/null 2>&1 || { export idnum="$x"; break ;}; done
  192. [ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
  193. echo "$msmtp_profile" >> "$msmtprc"
  194. command -V apt-get >/dev/null 2>&1 && ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  195. echo "$mutt_profile" > "$accdir/$idnum-$title.muttrc"
  196. case "$service" in
  197. protonmail.ch|protonmail.com|pm.me) protonfinger || return 1 ;;
  198. *) echo "$mbsync_profile" >> "$mbsyncrc"
  199. esac
  200. notmuchauto
  201. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created."
  202. ! grep "^source.*mutt-wizard.muttrc" "$muttrc" >/dev/null && echo "source $mwconfig # mw-autogenerated" >> "$muttrc"
  203. ! grep "^source.*.muttrc" "$muttrc" | grep -v "$mwconfig" >/dev/null && echo "source $accdir/$idnum-$title.muttrc # mw-autogenerated" >> "$muttrc"
  204. 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"
  205. }
  206. protonfinger() { printf "Getting Protonmail bridge fingerprint...\\n"
  207. fingerprint="$(msmtp --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off|grep SHA256:|awk '{print $2}')" || return 1
  208. sed -ibu "s/account $title/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$msmtprc" ; rm -f "$msmtprc"bu
  209. echo "$mbsync_protonmail_profile" >> "$mbsyncrc"
  210. }
  211. getpass() { while : ; do pass rm -f "mutt-wizard-$title" >/dev/null 2>&1
  212. pass insert "mutt-wizard-$title" && break; done ;}
  213. formatShortcut() { \
  214. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # mw-autogenerated"
  215. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
  216. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$accdir/$idnum-$title.muttrc"
  217. done ;}
  218. tryconnect() { mkdir -p "$maildir/$title"
  219. if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1 && [ -n "$mailboxes" ]; then
  220. [ "$accounttype" = "online" ] && sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  221. printf "\033[32mMailboxes detected.\033[0m\\n"
  222. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  223. return 0
  224. else
  225. 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"
  226. return 1
  227. fi ;}
  228. finalize() { \
  229. boxes="$(find "$maildir/$title/" -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" "$title" && 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 -ibu "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  237. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$idnum-$title.muttrc"
  238. echo "mailboxes =$title ===================== $(echo "$boxes" | sed -e "s/^\|$/\"/g" | tr "\n" " ")" >> "$accdir/$idnum-$title.muttrc"
  239. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  240. sed -ibu "/# mw-autogenerated/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  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. [ "$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"
  249. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  250. return 0
  251. }
  252. 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
  253. 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
  254. return 0 ;}
  255. pick() { printf "Select an accounts to %s:\\n" "$1"
  256. list
  257. read -r input
  258. [ -z "$input" ] && return 1
  259. title="$(echo "$accounts" | grep "$input" | awk '{print $2}')"
  260. [ -z "$title" ] && printf "Invalid response." && return 1
  261. return 0 ;}
  262. delete() { sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -rf "$mbsyncrc"bu
  263. rm -rf "${cachedir:?}/${title:?}" "$accdir/"[1-9]"-$title.muttrc"
  264. sed -ibu "/[0-9]-$title.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  265. sed -ibu "/account $title/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  266. }
  267. choosecron() { ! pgrep cron >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  268. if crontab -l | grep mailsync >/dev/null; then
  269. echo "Active mail sync cronjob detected. Do you want to remove it?"
  270. printf "\033[36m\t"
  271. read -r rmyn
  272. printf "\033[0m"
  273. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  274. else
  275. echo "How many minutes between each mail sync?"
  276. printf "\033[36m\t"
  277. read -r minnum
  278. printf "\033[0m"
  279. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  280. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  281. read -r minnum
  282. printf "\033[0m"
  283. done
  284. (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 - &&
  285. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  286. fi ;}
  287. asktype() { while : ; do
  288. printf "Do you want to keep your mail for this account offline with mbsync? [yes/no]\\n\t"
  289. read -r offnot
  290. case "$offnot" in
  291. [Yy][Ee][Ss]) accounttype="offline" && break ;;
  292. [Nn][Oo]) accounttype="online" && break ;;
  293. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  294. esac; done ;}
  295. purge() { confirm "delete all account data" || exit
  296. rm -rf "$mbsyncrc" "$accdir" "$HOME/.config/msmtp" "$cachedir"
  297. crontab -l | sed '/mailsync/d' | crontab - >/dev/null
  298. echo "All configs and account settings have been purged."
  299. sed -ibu "/\# mw-autogenerated/d" "$muttrc" ; rm -f "$muttrc"bu
  300. }
  301. notmuchauto() { \
  302. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  303. [ -f "$NOTMUCH_CONFIG" ] && return 0
  304. nmbasic="[database]
  305. path=$maildir
  306. [user]
  307. name=$realname
  308. primary_email=$fulladdr
  309. [new]
  310. tags=unread;inbox;
  311. ignore=
  312. [search]
  313. exclude_tags=deleted;spam;
  314. [maildir]
  315. synchronize_flags=true
  316. [crypto]
  317. gpg_path=$GPG"
  318. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  319. trap 'echo -e "\033[0m\n"; exit' STOP INT ABRT KILL
  320. case "$1" in
  321. ls) list ;;
  322. add) asktype && askinfo && tryconnect && finalize || delete ;;
  323. pass) pick "change the password of" && getpass ;;
  324. delete) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  325. purge) purge ;;
  326. cron) choosecron ;;
  327. *) cat << EOF
  328. mw: mutt-wizard, auto-configure email accounts for mutt
  329. including downloadable mail with \`isync\`.
  330. Allowed options:
  331. add Add and autoconfigure an email address (9 max.)
  332. ls List configured accounts
  333. delete Pick an account to delete
  334. purge Delete all accounts and settings
  335. cron Enable or disable an autosync via cronjob
  336. all else Print this message
  337. NOTE: Once at least one account is added, you can run
  338. \`mbsync -a\` to begin downloading mail.
  339. EOF
  340. esac