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.
 
 
 
 

325 wiersze
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. printf "\033[1mGoogle\033[0m mail account detected. Remember to check the README to make sure of Google-specific settings you must enable.\\n"
  140. else
  141. type="IMAP"; ifgoogleline="remotehost = $imap"
  142. fi
  143. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  144. seq 1 9 > "$tmpdir/mutt_all"
  145. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  146. getpass
  147. getprofiles
  148. mkdir -p "$accdir/$title/cache/bodies"
  149. mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp"
  150. [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
  151. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  152. echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
  153. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  154. echo "$mutt_profile" > "$accdir/$title.muttrc"
  155. sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\\s*$/accounts = $title/g" "$HOME/.config/offlineimap/config"
  156. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source \"$muttdir\"/accounts/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
  157. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  158. trysync && finalize
  159. }
  160. getpass() { \
  161. 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"
  162. stty -echo
  163. read -r password
  164. stty echo
  165. echo "$password" > "$tmpdir/$title"
  166. printf "Encrypting your password with %s..." "$GPG"
  167. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$title"
  168. printf "DONE\\nShredding all memory of your password for safety's sake..."
  169. unset password
  170. shred -u "$tmpdir/$title"
  171. mkdir -p "$creddir"
  172. mv "$tmpdir/$title.gpg" "$creddir/"
  173. printf "DONE.\\n"
  174. }
  175. askgpg() { \
  176. 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: "
  177. printf "\033[36m\t"
  178. read -r gpgemail
  179. printf "\033[0m"
  180. while ! echo "$gpgemail" | grep "$emailre" >/dev/null; do
  181. printf "That is not a valid email address. Please try again.\\nEmail: "
  182. printf "\033[36m\t"
  183. read -r gpgemail
  184. printf "\033[0m"
  185. done
  186. if "$GPG" -K | grep "<$gpgemail>" >/dev/null; then
  187. mkdir -p "$creddir"
  188. echo "$gpgemail" > "$creddir/gpgemail"
  189. else
  190. 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"
  191. exit 1
  192. fi
  193. }
  194. formatShortcut() { \
  195. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  196. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  197. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  198. done ;}
  199. trysync() { \
  200. ! 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
  201. 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."
  202. stty -echo
  203. read -r
  204. stty echo
  205. (sleep 15; killall offlineimap; killall offlineimap; killall offlineimap)>/dev/null 2>&1 &
  206. mkdir -p "$maildir"
  207. offlineimap -qoa "$title"
  208. if ls -d "$maildir/$title/"* >/dev/null 2>&1; then
  209. printf "\033[32mSync successful.\033[0m\\n"; return
  210. else
  211. 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
  212. fi
  213. }
  214. finalize() { \
  215. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  216. [ -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
  217. echo "$boxes" > "$tmpdir/title_boxes"
  218. printf "Setting up the mutt sidebar...\\n"
  219. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  220. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  221. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  222. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  223. oneline="=$title $delim $oneline"
  224. spoolfile=$(grep -i "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  225. record=$(grep -i "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  226. postponed=$(grep -i "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  227. trash=$(grep -i "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  228. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
  229. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
  230. echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
  231. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  232. sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
  233. grep -i "$tmpdir/title_boxes" -e inbox | sed 1q | formatShortcut i inbox "$title"
  234. grep -i "$tmpdir/title_boxes" -e sent | sed 1q | formatShortcut s sent "$title"
  235. grep -i "$tmpdir/title_boxes" -e draft | sed 1q | formatShortcut d drafts "$title"
  236. grep -i "$tmpdir/title_boxes" -e trash | sed 1q | formatShortcut t trash "$title"
  237. grep -i "$tmpdir/title_boxes" -e spam | sed 1q | formatShortcut S spam "$title"
  238. grep -i "$tmpdir/title_boxes" -e junk | sed 1q | formatShortcut j junk "$title"
  239. grep -i "$tmpdir/title_boxes" -e archive | sed 1q | formatShortcut a archive "$title"
  240. printf "All done.\\n"
  241. }
  242. wipe () { \
  243. 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
  244. 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
  245. #rm -rf "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc"
  246. echo deleted
  247. }
  248. pick() { \
  249. numbered="$(grep "^accounts *=" "$HOME/.config/offlineimap/config" | sed 's/accounts *= *//g;s/,/ /g;s/ \+/\n/g' | nl)"
  250. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to delete.\\n" && return 1
  251. printf "Select an accounts to %s:\\n" "$1"
  252. echo "$numbered"
  253. printf "\033[36m\t"
  254. read -r input
  255. printf "\033[0m"
  256. [ -z "$input" ] && return 1
  257. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  258. [ -z "$title" ] && printf "Invalid response." && return 1
  259. [ -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
  260. }
  261. delete() { sed -i "
  262. /Account $title]/,/Account/{//!d}
  263. /Account $title]/d
  264. s/ $title\(,\|$\)//g
  265. s/=$title\(,\|$\)/=/g
  266. s/,$//g
  267. " "$HOME/.config/offlineimap/config"
  268. rm -rf "${accdir:?}/${title:?}" "$creddir/$title.gpg" "$accdir/$title.muttrc"
  269. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  270. # Delete from the line matching the account name, until the next account or empty line
  271. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config";}
  272. main() { \
  273. while : ; do
  274. [ -z "$gpgemail" ] && askgpg
  275. printf "What would you like \033[32mmutt-wizard\033[0m to do?
  276. \033[31m1 Add an email account\033[0m
  277. 2 Autodetect mailboxes
  278. 3 Change an account's password
  279. 4 Remove an account
  280. 5 Change GPG key pair used for encryption
  281. 6 Delete all account data
  282. 0 Exit
  283. Input a number to continue or press ctrl-c.\\n"
  284. printf "\033[36m\t"
  285. read -r choice
  286. printf "\033[0m"
  287. case "$choice" in
  288. 1) addaccount ;;
  289. 2) pick finalize && finalize ;;
  290. 3) pick "change the password of" yes && getpass ;;
  291. 4) pick delete yes && delete ;;
  292. 5) askgpg ;;
  293. 6) wipe && printf "Account data purged." ;;
  294. 0) break ;;
  295. *) printf "Invalid input.\\n"
  296. esac
  297. done
  298. }
  299. main
  300. rm -rf "$tmpdir"