選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

304 行
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. printf "Creating profiles for \`$title\`..."
  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. sslcacertfile = $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 ~/.config/msmtp/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. printf "DONE.\\n"
  77. }
  78. addaccount() { \
  79. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/offlineIMAP\\n\\nEmail: "
  80. read -r fulladdr
  81. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  82. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: "
  83. read -r fulladdr
  84. done
  85. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  86. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  87. serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
  88. if [ -z "$serverinfo" ]; then
  89. 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"
  90. printf "Insert the IMAP server for your email provider (excluding the port number)\\n"
  91. read -r imap
  92. printf "What is your server's IMAP port number? (Usually something like 993)\\n"
  93. read -r iport
  94. printf "Insert the SMTP server for your email provider (excluding the port number)\\n"
  95. read -r smtp
  96. printf "What is your server's SMTP port number? (Usually 587 or 465)\\n"
  97. read -r sport
  98. 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"
  99. else
  100. IFS=, read service imap iport smtp sport <<EOF
  101. $serverinfo
  102. EOF
  103. 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"
  104. fi
  105. printf "\\nPress enter to continue.\\n"
  106. stty -echo
  107. read wait
  108. stty echo
  109. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  110. read -r realname
  111. 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: "
  112. read -r title
  113. while ! echo "$title" | grep "$namere" >/dev/null; do
  114. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: "
  115. read -r title
  116. done
  117. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): "
  118. read -r login
  119. [ -z "$login" ] && login="$fulladdr"
  120. if [ "$service" = "gmail.com" ]; then
  121. type="Gmail"; ifgoogleline="folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']"
  122. printf "\033[1mGoogle\033[0m mail account detected. Remember to check the README to make sure of Google-specific settings you must enable.\\n"
  123. else
  124. type="IMAP"; ifgoogleline="remotehost = $imap"
  125. fi
  126. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  127. seq 1 9 > "$tmpdir/mutt_all"
  128. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  129. getpass "$title"
  130. getprofiles
  131. mkdir -p "$accdir"
  132. mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp"
  133. [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
  134. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  135. echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
  136. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  137. echo "$mutt_profile" > "$accdir/$title.muttrc"
  138. sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\\s*$/accounts = $title/g" "$HOME/.config/offlineimap/config"
  139. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source \"$muttdir\"/accounts/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
  140. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  141. trysync && finalize
  142. }
  143. getpass() { \
  144. 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"
  145. stty -echo
  146. read -r password
  147. stty echo
  148. echo "$password" > "$tmpdir/$1"
  149. printf "Encrypting your password with %s..."
  150. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$1"
  151. printf "DONE\\nShredding all memory of your password for safety's sake..."
  152. unset password
  153. shred -u "$tmpdir/$1" #| rm -f "$tmpdir/$1"
  154. mkdir -p "$creddir"
  155. mv "$tmpdir/$1.gpg" "$creddir/"
  156. printf "DONE.\\n"
  157. }
  158. askgpg() { \
  159. 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: "
  160. read -r gpgemail
  161. while ! echo "$gpgemail" | grep "$emailre" >/dev/null; do
  162. printf "That is not a valid email address. Please try again.\\nEmail: "
  163. read -r gpgemail
  164. done
  165. if "$GPG" -K | grep "<$gpgemail>" >/dev/null; then
  166. mkdir -p "$creddir"
  167. echo "$gpgemail" > "$creddir/gpgemail"
  168. else
  169. 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"
  170. exit 1
  171. fi
  172. }
  173. formatShortcut() { \
  174. while read data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  175. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  176. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  177. done ;}
  178. gen_delim() { \
  179. delim="="
  180. for i in $(seq $(( $1 - 1 )))
  181. do
  182. delim="$delim-"
  183. done
  184. echo $delim ;}
  185. trysync() { \
  186. ! 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
  187. 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."
  188. stty -echo
  189. read wait
  190. stty echo
  191. mkdir -p "$maildir"
  192. (sleep 15; killall offlineimap; killall offlineimap; killall offlineimap)>/dev/null 2>&1 &
  193. offlineimap -qoa "$title"
  194. if ls -d "$maildir/$1/"* >/dev/null 2>&1; then
  195. printf "\033[32mSync successful.\033[0m\\n"; return
  196. else
  197. 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
  198. fi
  199. }
  200. finalize() { \
  201. boxes="$(ls -d "$maildir/$title/"* 2>/dev/null | sed "s/.*\///;s/^/=/")"
  202. [ -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
  203. echo "$boxes" > "$tmpdir/title_boxes"
  204. printf "Setting up the mutt sidebar...\\n"
  205. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  206. delim="$(gen_delim "$sidebar_width")"
  207. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  208. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  209. oneline="=$title $delim $oneline"
  210. spoolfile=$(grep -i "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  211. record=$(grep -i "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  212. postponed=$(grep -i "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  213. trash=$(grep -i "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  214. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
  215. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
  216. echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
  217. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  218. sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
  219. grep -i "$tmpdir/title_boxes" -e inbox | sed 1q | formatShortcut i inbox "$title"
  220. grep -i "$tmpdir/title_boxes" -e sent | sed 1q | formatShortcut s sent "$title"
  221. grep -i "$tmpdir/title_boxes" -e draft | sed 1q | formatShortcut d drafts "$title"
  222. grep -i "$tmpdir/title_boxes" -e trash | sed 1q | formatShortcut t trash "$title"
  223. grep -i "$tmpdir/title_boxes" -e spam | sed 1q | formatShortcut S spam "$title"
  224. grep -i "$tmpdir/title_boxes" -e junk | sed 1q | formatShortcut j junk "$title"
  225. grep -i "$tmpdir/title_boxes" -e archive | sed 1q | formatShortcut a archive "$title"
  226. printf "All done.\\n"
  227. }
  228. wipe () { rm -rf "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc" ;}
  229. [ -z "$gpgemail" ] && askgpg
  230. pick() { \
  231. numbered="$(grep "^accounts *=" "$HOME/.config/offlineimap/config" | sed 's/accounts *= *//g;s/,/ /g;s/ \+/\n/g' | nl)"# > "$tmpdir/numbered"
  232. printf "Select (an) account(s) to %s.\\n" "$1"
  233. [ -z "$numbered" ] || return 1
  234. echo "$numbered"
  235. read -r input
  236. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  237. [ -z "$title" ] && printf "Invalid response." && return 1
  238. [ ! -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
  239. }
  240. delete() { sed -i "
  241. /Account $1]/,/Account/{//!d}
  242. /Account $1]/d
  243. s/ $1\(,\|$\)//g
  244. s/=$1\(,\|$\)/=/g
  245. s/,$//g
  246. " "$HOME/.config/offlineimap/config"
  247. rm -rf "$accdir/$1" "$creddir/$1.gpg" "$accdir/$1.muttrc"
  248. sed -i "/$1.muttrc/d" "$muttdir/personal.muttrc"
  249. # Delete from the line matching the account name, until the next account or empty line
  250. sed -i "/account $1/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config";}
  251. main() { \
  252. while : ; do
  253. printf "What would you like \033[32mmutt-wizard\033[0m to do?
  254. \033[31m1 Add an email account\033[0m
  255. 2 Autodetect mailboxes
  256. 3 Change an account's password
  257. 4 Remove an account
  258. 5 Change GPG key pair used for encryption
  259. 6 Delete all account data
  260. 0 Exit
  261. Input a number to continue or press ctrl-c.\\n"
  262. read -r choice
  263. case "$choice" in
  264. 1) addaccount ;;
  265. 2) pick finalize && finalize "$title" ;;
  266. 2) break ;;
  267. 4) pick delete yes && delete "$title" ;;
  268. 5) askgpg ;;
  269. 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." ;;
  270. 0) break ;;
  271. *) printf "Invalid input.\\n"
  272. esac
  273. done
  274. }
  275. main
  276. rm -rf "$tmpdir"