Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

326 рядки
14 KiB

  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. [ ! -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
  10. gpgemail="$(cat "$creddir/gpgemail" 2>/dev/null)" # Get previously set gpg email address
  11. tmpdir="$(mktemp -d)"
  12. GPG="gpg"; command -v gpg >/dev/null || GPG="gpg2" # Ensure proper gpg command
  13. # Get certificate location depending on OS. Linux is elsewhere condition.
  14. case "$(uname)" in
  15. "Darwin") sslcert="/usr/local/etc/openssl/cert.pem" ;;
  16. *) sslcert="/etc/ssl/certs/ca-certificates.crt" ;;
  17. esac
  18. getprofiles() { \
  19. printf "Creating profiles for \`%s\`..." "$title"
  20. offlineimap_header="[general]
  21. accounts =
  22. starttls = yes
  23. ssl = true
  24. pythonfile = $bindir/imappwd.py
  25. "
  26. offlineimap_profile="
  27. [Account $title]
  28. localrepository = $title-local
  29. remoterepository = $title-remote
  30. [Repository $title-remote]
  31. auth_mechanisms = LOGIN
  32. type = $type
  33. remoteuser = $login
  34. remotepasseval = mailpasswd(\"$title\")
  35. remoteport = $iport
  36. sslcacertfile = $sslcert
  37. $ifgoogleline
  38. [Repository $title-local]
  39. type = Maildir
  40. localfolders = $maildir/$title
  41. "
  42. msmtp_header="defaults
  43. auth on
  44. tls on
  45. tls_trust_file $sslcert
  46. logfile ~/.config/msmtp/msmtp.log
  47. "
  48. msmtp_profile="
  49. account $title
  50. host $smtp
  51. port $sport
  52. from $login
  53. user $login
  54. passwordeval \"$GPG -d --quiet --for-your-eyes-only --no-tty $creddir/$title.gpg | sed -e '\$a\\'\"
  55. "
  56. mutt_profile="# vim: filetype=neomuttrc
  57. # muttrc file for account $title
  58. set realname = \"$realname\"
  59. set from = \"$fulladdr\"
  60. set sendmail = \"/usr/bin/msmtp -a $title\"
  61. set folder = \"$maildir/$title\"
  62. set header_cache = $accdir/$title/cache/headers
  63. set message_cachedir = $accdir/$title/cache/bodies
  64. set certificate_file = $accdir/$title/certificates
  65. source \"$bindir/getmuttpass $title |\"
  66. alias me $realname <$fulladdr>
  67. set mbox_type = Maildir
  68. set ssl_starttls = yes
  69. set ssl_force_tls = yes
  70. bind index,pager gg noop
  71. bind index,pager g noop
  72. bind index,pager M noop
  73. bind index,pager C noop
  74. bind index gg first-entry
  75. unmailboxes *
  76. "
  77. printf "DONE.\\n"
  78. }
  79. addaccount() { \
  80. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/offlineIMAP\\n\\nEmail: "
  81. printf "\033[36m\t"
  82. read -r fulladdr
  83. printf "\033[0m"
  84. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  85. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: "
  86. printf "\033[36m\t"
  87. read -r fulladdr
  88. printf "\033[0m"
  89. done
  90. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  91. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  92. serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
  93. if [ -z "$serverinfo" ]; then
  94. 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"
  95. printf "Insert the IMAP server for your email provider (excluding the port number)\\n"
  96. printf "\033[36m\t"
  97. read -r imap
  98. printf "\033[0m"
  99. printf "What is your server's IMAP port number? (Usually something like 993)\\n"
  100. printf "\033[36m\t"
  101. read -r iport
  102. printf "\033[0m"
  103. printf "Insert the SMTP server for your email provider (excluding the port number)\\n"
  104. printf "\033[36m\t"
  105. read -r smtp
  106. printf "\033[0m"
  107. printf "What is your server's SMTP port number? (Usually 587 or 465)\\n"
  108. printf "\033[36m\t"
  109. read -r sport
  110. printf "\033[0m"
  111. 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"
  112. else
  113. IFS=, read -r service imap iport smtp sport <<EOF
  114. $serverinfo
  115. EOF
  116. 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"
  117. fi
  118. printf "\\nPress enter to continue.\\n"
  119. stty -echo
  120. read -r
  121. stty echo
  122. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  123. read -r realname
  124. 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: "
  125. read -r title
  126. while ! echo "$title" | grep "$namere" >/dev/null; do
  127. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: "
  128. printf "\033[36m\t"
  129. read -r title
  130. printf "\033[0m"
  131. done
  132. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): "
  133. printf "\033[36m\t"
  134. read -r login
  135. printf "\033[0m"
  136. [ -z "$login" ] && login="$fulladdr"
  137. if [ "$service" = "gmail.com" ]; then
  138. type="Gmail"; ifgoogleline="folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']
  139. ssl_version = tls1_2"
  140. printf "\033[1mGoogle\033[0m mail account detected. Remember to check the README to make sure of Google-specific settings you must enable.\\n"
  141. else
  142. type="IMAP"; ifgoogleline="remotehost = $imap"
  143. fi
  144. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  145. seq 1 9 > "$tmpdir/mutt_all"
  146. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  147. getpass
  148. getprofiles
  149. mkdir -p "$accdir/$title/cache/bodies"
  150. mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp"
  151. [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
  152. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  153. echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
  154. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  155. echo "$mutt_profile" > "$accdir/$title.muttrc"
  156. sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\\s*$/accounts = $title/g" "$HOME/.config/offlineimap/config"
  157. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source \"$muttdir\"/accounts/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
  158. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  159. trysync && finalize
  160. }
  161. getpass() { \
  162. 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"
  163. stty -echo
  164. read -r password
  165. stty echo
  166. echo "$password" > "$tmpdir/$title"
  167. printf "Encrypting your password with %s..." "$GPG"
  168. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$title"
  169. printf "DONE\\nShredding all memory of your password for safety's sake..."
  170. unset password
  171. shred -u "$tmpdir/$title"
  172. mkdir -p "$creddir"
  173. mv "$tmpdir/$title.gpg" "$creddir/"
  174. printf "DONE.\\n"
  175. }
  176. askgpg() { \
  177. 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: "
  178. printf "\033[36m\t"
  179. read -r gpgemail
  180. printf "\033[0m"
  181. while ! echo "$gpgemail" | grep "$emailre" >/dev/null; do
  182. printf "That is not a valid email address. Please try again.\\nEmail: "
  183. printf "\033[36m\t"
  184. read -r gpgemail
  185. printf "\033[0m"
  186. done
  187. if "$GPG" -K | grep "<$gpgemail>" >/dev/null; then
  188. mkdir -p "$creddir"
  189. echo "$gpgemail" > "$creddir/gpgemail"
  190. else
  191. printf "You do not appear to have a private key associated with %s.\\nPlease generate a GPG key pair by running \`%s --full-gen-key\` and rerun the wizard.\\n" "$gpgemail" "$GPG"
  192. exit 1
  193. fi
  194. }
  195. formatShortcut() { \
  196. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  197. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  198. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  199. done ;}
  200. trysync() { \
  201. ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning offlineimap manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && return 1
  202. printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run offlineimap briefly to (1) ensure that login details are functional and (2) allow offlineimap to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run offlineimap manually to finish the mail sync later.\\nPress enter to continue.\\n."
  203. stty -echo
  204. read -r
  205. stty echo
  206. (sleep 15; killall offlineimap; killall offlineimap; killall offlineimap)>/dev/null 2>&1 &
  207. mkdir -p "$maildir"
  208. offlineimap -qoa "$title"
  209. if ls -d "$maildir/$title/"* >/dev/null 2>&1; then
  210. printf "\033[32mSync successful.\033[0m\\n"; return
  211. else
  212. printf "\033[31m\033[31mSync not successful.\033[0m Try running offlineimap manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n"; return 1
  213. fi
  214. }
  215. finalize() { \
  216. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  217. [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that offlineimap has not been successfully run.\\nRun offlineimap, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && return
  218. echo "$boxes" > "$tmpdir/title_boxes"
  219. printf "Setting up the mutt sidebar...\\n"
  220. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  221. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  222. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  223. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  224. oneline="=$title $delim $oneline"
  225. spoolfile=$(grep -i "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  226. record=$(grep -i "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  227. postponed=$(grep -i "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  228. trash=$(grep -i "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  229. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
  230. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
  231. echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
  232. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  233. sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
  234. grep -i "$tmpdir/title_boxes" -e inbox | sed 1q | formatShortcut i inbox "$title"
  235. grep -i "$tmpdir/title_boxes" -e sent | sed 1q | formatShortcut s sent "$title"
  236. grep -i "$tmpdir/title_boxes" -e draft | sed 1q | formatShortcut d drafts "$title"
  237. grep -i "$tmpdir/title_boxes" -e trash | sed 1q | formatShortcut t trash "$title"
  238. grep -i "$tmpdir/title_boxes" -e spam | sed 1q | formatShortcut S spam "$title"
  239. grep -i "$tmpdir/title_boxes" -e junk | sed 1q | formatShortcut j junk "$title"
  240. grep -i "$tmpdir/title_boxes" -e archive | sed 1q | formatShortcut a archive "$title"
  241. printf "All done.\\n"
  242. }
  243. wipe () { \
  244. 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
  245. 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
  246. #rm -rf "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc"
  247. echo deleted
  248. }
  249. pick() { \
  250. numbered="$(grep "^accounts *=" "$HOME/.config/offlineimap/config" | sed 's/accounts *= *//g;s/,/ /g;s/ \+/\n/g' | nl)"
  251. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to delete.\\n" && return 1
  252. printf "Select an accounts to %s:\\n" "$1"
  253. echo "$numbered"
  254. printf "\033[36m\t"
  255. read -r input
  256. printf "\033[0m"
  257. [ -z "$input" ] && return 1
  258. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  259. [ -z "$title" ] && printf "Invalid response." && return 1
  260. [ -n "$2" ] && printf "Are you sure you want to %s the \`%s\` account?" "$1" "$title" && read -r input && echo "$input" | grep -i "y\(es\)*" >/dev/null
  261. }
  262. delete() { sed -i "
  263. /Account $title]/,/Account/{//!d}
  264. /Account $title]/d
  265. s/ $title\(,\|$\)//g
  266. s/=$title\(,\|$\)/=/g
  267. s/,$//g
  268. " "$HOME/.config/offlineimap/config"
  269. rm -rf "${accdir:?}/${title:?}" "$creddir/$title.gpg" "$accdir/$title.muttrc"
  270. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  271. # Delete from the line matching the account name, until the next account or empty line
  272. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config";}
  273. main() { \
  274. while : ; do
  275. [ -z "$gpgemail" ] && askgpg
  276. printf "What would you like \033[32mmutt-wizard\033[0m to do?
  277. \033[31m1 Add an email account\033[0m
  278. 2 Autodetect mailboxes
  279. 3 Change an account's password
  280. 4 Remove an account
  281. 5 Change GPG key pair used for encryption
  282. 6 Delete all account data
  283. 0 Exit
  284. Input a number to continue or press ctrl-c.\\n"
  285. printf "\033[36m\t"
  286. read -r choice
  287. printf "\033[0m"
  288. case "$choice" in
  289. 1) addaccount ;;
  290. 2) pick finalize && finalize ;;
  291. 3) pick "change the password of" yes && getpass ;;
  292. 4) pick delete yes && delete ;;
  293. 5) askgpg ;;
  294. 6) wipe && printf "Account data purged." ;;
  295. 0) break ;;
  296. *) printf "Invalid input.\\n"
  297. esac
  298. done
  299. }
  300. main
  301. rm -rf "$tmpdir"