Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

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