您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
5 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #!/bin/sh
  2. muttdir="$HOME/.config/mutt" # Main mutt config location
  3. accdir="$muttdir/accounts" # Directory for account settings
  4. maildir="$HOME/.local/share/mail" # Location of mail storage
  5. creddir="$HOME/.local/share/muttwizard" # Location of encrypted credentials
  6. bindir="$HOME/.config/mutt/bin" # Location of scripts run by mutt or the wizard
  7. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  8. emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
  9. mbsyncrc="$HOME/.mbsyncrc"
  10. [ ! -f "$muttdir/domains.csv" ] || [ ! -d "$bindir" ] && printf "Read the README. Be sure to put the repo in the right place before running.\\n" && exit 1
  11. gpgemail="$(grep "$emailre" "$creddir/gpgemail")" # Get previously set gpg email address
  12. tmpdir="$(mktemp -d)"
  13. GPG="gpg"; command -v gpg >/dev/null || GPG="gpg2" # Ensure proper gpg command
  14. # Get certificate location depending on OS. Linux is elsewhere condition.
  15. case "$(uname)" in
  16. "Darwin") sslcert="/usr/local/etc/openssl/cert.pem" ;;
  17. *) sslcert="/etc/ssl/certs/ca-certificates.crt" ;;
  18. esac
  19. getprofiles() { \
  20. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  21. printf "Creating profiles for \`%s\`..." "$title"
  22. msmtp_header="defaults
  23. auth on
  24. tls on
  25. tls_trust_file $sslcert
  26. logfile ~/.config/msmtp/msmtp.log
  27. "
  28. msmtp_profile="
  29. account $title
  30. host $smtp
  31. port $sport
  32. from $login
  33. user $login
  34. passwordeval \"$GPG -d --quiet --for-your-eyes-only --no-tty $creddir/$title.gpg | sed -e '\$a\\'\"
  35. "
  36. if [ "$accounttype" = "offline" ]; then
  37. mutt_profile="# vim: filetype=neomuttrc
  38. # muttrc file for account $title
  39. set realname = \"$realname\"
  40. set from = \"$fulladdr\"
  41. set sendmail = \"/usr/bin/msmtp -a $title\"
  42. set folder = \"$maildir/$title\"
  43. set header_cache = $accdir/$title/cache/headers
  44. set message_cachedir = $accdir/$title/cache/bodies
  45. set certificate_file = $accdir/$title/certificates
  46. source \"$bindir/getmuttpass $title |\"
  47. alias me $realname <$fulladdr>
  48. set mbox_type = Maildir
  49. set ssl_starttls = yes
  50. set ssl_force_tls = yes
  51. bind index,pager gg noop
  52. bind index,pager g noop
  53. bind index,pager M noop
  54. bind index,pager C noop
  55. bind index gg first-entry
  56. unmailboxes *
  57. set imap_check_subscribed
  58. "
  59. mbsync_profile="IMAPAccount $title
  60. Host $imap
  61. User $login
  62. Port $iport
  63. PassCmd \"gpg -q --for-your-eyes-only --no-tty -d ~/.local/share/muttwizard/$title.gpg\"
  64. SSLType IMAPS
  65. CertificateFile /etc/ssl/certs/ca-certificates.crt
  66. IMAPStore $title-remote
  67. Account $title
  68. MaildirStore $title-local
  69. Subfolders Verbatim
  70. Path ~/.local/share/mail/$title/
  71. Inbox ~/.local/share/mail/$title/INBOX
  72. Channel $title
  73. Master :$title-remote:
  74. Slave :$title-local:
  75. Patterns * !\"[Gmail]/All Mail\"
  76. Create Both
  77. SyncState *
  78. # End profile"
  79. else
  80. mutt_profile="# vim: filetype=neomuttrc
  81. # muttrc file for account $title
  82. set realname = \"$realname\"
  83. set from = \"$fulladdr\"
  84. set sendmail = \"/usr/bin/msmtp -a $title\"
  85. set folder = \"imaps://$imap:$iport\"
  86. set imap_user = $login
  87. set header_cache = $accdir/$title/cache/headers
  88. set message_cachedir = $accdir/$title/cache/bodies
  89. set certificate_file = $accdir/$title/certificates
  90. source \"$bindir/getmuttpass $title |\"
  91. alias me $realname <$fulladdr>
  92. set mbox_type = Maildir
  93. set ssl_starttls = yes
  94. set ssl_force_tls = yes
  95. bind index,pager gg noop
  96. bind index,pager g noop
  97. bind index,pager M noop
  98. bind index,pager C noop
  99. bind index gg first-entry
  100. unmailboxes *
  101. "
  102. fi
  103. printf "DONE.\\n"
  104. }
  105. addaccount() { \
  106. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\\nEmail: "
  107. printf "\033[36m\t"
  108. read -r fulladdr
  109. printf "\033[0m"
  110. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  111. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: "
  112. printf "\033[36m\t"
  113. read -r fulladdr
  114. printf "\033[0m"
  115. done
  116. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  117. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  118. serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
  119. if [ -z "$serverinfo" ]; then
  120. 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"
  121. printf "Insert the IMAP server for your email provider (excluding the port number)\\n"
  122. printf "\033[36m\t"
  123. read -r imap
  124. printf "\033[0m"
  125. printf "What is your server's IMAP port number? (Usually something like 993)\\n"
  126. printf "\033[36m\t"
  127. read -r iport
  128. printf "\033[0m"
  129. printf "Insert the SMTP server for your email provider (excluding the port number)\\n"
  130. printf "\033[36m\t"
  131. read -r smtp
  132. printf "\033[0m"
  133. printf "What is your server's SMTP port number? (Usually 587 or 465)\\n"
  134. printf "\033[36m\t"
  135. read -r sport
  136. printf "\033[0m"
  137. printf "\\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"
  138. else
  139. IFS=, read -r service imap iport smtp sport <<EOF
  140. $serverinfo
  141. EOF
  142. printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically be 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"
  143. fi
  144. cont
  145. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  146. read -r realname
  147. 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: "
  148. read -r title
  149. while ! echo "$title" | grep "$namere" >/dev/null; do
  150. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: "
  151. printf "\033[36m\t"
  152. read -r title
  153. printf "\033[0m"
  154. done
  155. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): "
  156. printf "\033[36m\t"
  157. read -r login
  158. printf "\033[0m"
  159. [ -z "$login" ] && login="$fulladdr"
  160. if [ "$service" = "gmail.com" ]; then
  161. type="Gmail"; ifgoogleline="folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']
  162. ssl_version = tls1_2"
  163. printf "\033[1mGoogle\033[0m mail account detected. Remember to check the README to make sure of Google-specific settings you must enable.\\n"
  164. else
  165. type="IMAP"; ifgoogleline="remotehost = $imap"
  166. fi
  167. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  168. seq 1 9 > "$tmpdir/mutt_all"
  169. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  170. getpass
  171. getprofiles
  172. mkdir -p "$accdir/$title/cache/bodies" "$HOME/.config/msmtp"
  173. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  174. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  175. echo "$mutt_profile" > "$accdir/$title.muttrc"
  176. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
  177. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  178. [ "$accounttype" = "offline" ] && echo "$mbsync_profile" >> "$mbsyncrc" && trysync && finalize
  179. }
  180. getpass() { \
  181. printf "Now enter your password for the \"%s\" account. Don't worry, this will be encrypted and only you with your GPG key can view it.\\n\tPassword: " "$title"
  182. stty -echo
  183. read -r password
  184. stty echo
  185. echo "$password" > "$tmpdir/$title"
  186. printf "Encrypting your password with %s..." "$GPG"
  187. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$title"
  188. printf "DONE\\nShredding all memory of your password for safety's sake..."
  189. unset password
  190. shred -u "$tmpdir/$title"
  191. mkdir -p "$creddir"
  192. mv "$tmpdir/$title.gpg" "$creddir/"
  193. printf "DONE.\\n"
  194. }
  195. askgpg() { \
  196. printf "To safely encrypt passwords, mutt-wizard requires that you have a GPG public/private key pair.\\n\\nPlease input the email address of your GPG key pair below.\\nEmail: "
  197. printf "\033[36m\t"
  198. read -r gpgemail
  199. printf "\033[0m"
  200. while ! echo "$gpgemail" | grep "$emailre" >/dev/null; do
  201. printf "That is not a valid email address. Please try again.\\nEmail: "
  202. printf "\033[36m\t"
  203. read -r gpgemail
  204. printf "\033[0m"
  205. done
  206. if "$GPG" -K | grep "<$gpgemail>" >/dev/null; then
  207. mkdir -p "$creddir"
  208. echo "$gpgemail" > "$creddir/gpgemail"
  209. else
  210. printf "You do not appear to have a private key associated with \033[33m%s\033[0m.\\nPlease generate a GPG key pair by running \`\033[32m%s --full-gen-key\033[0m\` and rerun the wizard.\\n" "$gpgemail" "$GPG"
  211. exit 1
  212. fi
  213. }
  214. formatShortcut() { \
  215. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  216. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  217. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  218. done ;}
  219. trysync() { \
  220. ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning \`mbsync $title\` manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && return 1
  221. printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run mbsync briefly to (1) ensure that login details are functional and (2) allow mbsync to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run mbsync manually to finish the mail sync later.\\n"
  222. cont
  223. (sleep 15; killall mbsync)>/dev/null 2>&1 &
  224. mkdir -p "$maildir/$title"
  225. mbsync "$title"
  226. if ls -d "$maildir/$title/"* >/dev/null 2>&1; then
  227. printf "\033[32mSync successful.\033[0m\\n"; return
  228. else
  229. printf "\033[31m\033[31mSync not successful.\033[0m Try running \`mbsync $title\` manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n"; return 1
  230. fi
  231. }
  232. finalize() { \
  233. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  234. [ -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
  235. echo "$boxes" > "$tmpdir/title_boxes"
  236. printf "Setting up the mutt sidebar...\\n"
  237. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  238. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  239. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  240. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  241. oneline="=$title $delim $oneline"
  242. spoolfile=$(grep -i "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  243. record=$(grep -i "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  244. postponed=$(grep -i "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  245. trash=$(grep -i "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  246. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
  247. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
  248. echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
  249. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  250. sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
  251. grep -i "$tmpdir/title_boxes" -e inbox | sed 1q | formatShortcut i inbox "$title"
  252. grep -i "$tmpdir/title_boxes" -e sent | sed 1q | formatShortcut s sent "$title"
  253. grep -i "$tmpdir/title_boxes" -e draft | sed 1q | formatShortcut d drafts "$title"
  254. grep -i "$tmpdir/title_boxes" -e trash | sed 1q | formatShortcut t trash "$title"
  255. grep -i "$tmpdir/title_boxes" -e spam | sed 1q | formatShortcut S spam "$title"
  256. grep -i "$tmpdir/title_boxes" -e junk | sed 1q | formatShortcut j junk "$title"
  257. grep -i "$tmpdir/title_boxes" -e archive | sed 1q | formatShortcut a archive "$title"
  258. printf "All done.\\n"
  259. }
  260. wipe () { \
  261. printf "Are you \033[31;1mreally\033[0m sure you want to delete all email accounts?\\n" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  262. printf "Are you really, really sure?" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  263. rm -rf "$mbsyncrc" "$accdir" "$creddir" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged."
  264. }
  265. pick() { \
  266. numbered="$(ls $accdir/*muttrc | sed "s/.*\///;s/\..*//" | nl)"
  267. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to delete.\\n" && return 1
  268. printf "Select an accounts to %s:\\n" "$1"
  269. echo "$numbered"
  270. printf "\033[36m\t"
  271. read -r input
  272. printf "\033[0m"
  273. [ -z "$input" ] && return 1
  274. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  275. [ -z "$title" ] && printf "Invalid response." && return 1
  276. [ -n "$2" ] && printf "Are you sure you want to %s the \`%s\` account?\\n" "$1" "$title" && read -r input && echo "$input" | grep -i "y\(es\)*" >/dev/null
  277. }
  278. delete() { sed -i "/IMAPAccount $title/,/# End profile/d" "$mbsyncrc"
  279. rm -rf "${accdir:?}/${title:?}" "$creddir/$title.gpg" "$accdir/$title.muttrc"
  280. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  281. # Delete from the line matching the account name, until the next account or empty line
  282. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config";}
  283. choosecron() { ! pgrep crond >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  284. if crontab -l | grep mailsync >/dev/null; then
  285. echo "Active mail sync cronjob detected. Do you want to remove it?"
  286. printf "\033[36m\t"
  287. read -r rmyn
  288. printf "\033[0m"
  289. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  290. else
  291. echo "How many minutes between each mail sync?"
  292. printf "\033[36m\t"
  293. read -r minnum
  294. printf "\033[0m"
  295. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  296. printf "That doesn't look like a number. How many minutes between each mail sync?"
  297. printf "\033[36m\t"
  298. read -r minnum
  299. printf "\033[0m"
  300. done
  301. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus; export DISPLAY=:0; . \$HOME/.profile; $muttdir/bin/mailsync -f INBOX") | crontab - &&
  302. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  303. fi
  304. }
  305. cont() { [ -n "$1" ] && printf "%s\\n" "$1"
  306. printf "Press Enter to continue.\\n"
  307. stty -echo
  308. read -r null
  309. stty echo
  310. }
  311. addtype() { \
  312. while : ; do
  313. echo "Do you want to keep your mail for this account offline with mbsync? [yes/no]"
  314. read -r offnot
  315. case "$offnot" in
  316. [Yy]es) accounttype="offline" && break ;;
  317. [Nn]o) accounttype="online" && break ;;
  318. *) echo "I don't know what you mean. Try again or press ctrl-c to quit." ;;
  319. esac
  320. done
  321. addaccount
  322. }
  323. main() { \
  324. while : ; do
  325. [ -z "$gpgemail" ] && askgpg
  326. printf "Current GPG key email: %s\\n\033[34m" "$gpgemail"
  327. cat << "EOF"
  328. _ __ ___ _ _| |_| |_ __ _(_)______ _ _ __ __| |
  329. | '_ ` _ \| | | | __| __|___\ \ /\ / / |_ / _` | '__/ _` |
  330. | | | | | | |_| | |_| ||_____\ V V /| |/ / (_| | | | (_| |
  331. |_| |_| |_|\__,_|\__|\__| \_/\_/ |_/___\__,_|_| \__,_|
  332. EOF
  333. printf "\033[0mMade and maintained by Luke Smith <https://lukesmith.xyz>
  334. What would you like \033[32mmutt-wizard\033[0m to do?
  335. \033[31m1 Add an email account\033[0m
  336. 2 Autodetect mailboxes
  337. 3 Change an account's password
  338. 4 Remove an account
  339. 5 Change GPG key pair used for encryption
  340. 6 Delete all account data
  341. 7 Enable/disable autosync
  342. 9 Edit this script
  343. 0 Exit
  344. Input a number to continue or press ctrl-c.\\n"
  345. printf "\033[36m\t"
  346. read -r choice
  347. printf "\033[0m"
  348. case "$choice" in
  349. 1) addtype ;;
  350. 2) pick finalize && finalize ;;
  351. 3) pick "change the password of" yes && getpass ;;
  352. 4) pick delete yes && delete ;;
  353. 5) askgpg ;;
  354. 6) wipe && printf "Account data purged." ;;
  355. 7) choosecron ;;
  356. 9) "$EDITOR" "$0"; exit ;;
  357. 0) break ;;
  358. *) printf "Invalid input.\\n"
  359. esac
  360. cont
  361. done
  362. }
  363. main
  364. rm -rf "$tmpdir"