Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

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