Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

244 linhas
9.3 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. userexit() { clear; exit ;}
  78. addaccount() { \
  79. printf "Insert the email address 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 valud email address, please retype the desired email.\\n\\nEmail: "
  83. read -r fulladdr
  84. done
  85. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  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. exit
  99. else
  100. IFS=, read service imap iport smtp sport <<EOF
  101. $serverinfo
  102. EOF
  103. printf "Congrats! Server info has automatically be found, so you won't have to look anything up!\\nIMAP server: %s\\nIMAP port: %s\\nSMTP server: %s\\nSMTP port: %s\\nThis data will be used by the wizard.\\n" "$imap" "$iport" "$smtp" "$sport"
  104. exit
  105. fi
  106. printf "Enter the full name you want to be identified by on this account.\\nReal name: "
  107. read -r realname
  108. printf "Enter a short, one-word identifier for this email account that will distinguish them from any other accounts you add.\\nAccount name: "
  109. read -r title
  110. while ! echo "$title" | grep "$namere" >/dev/null; do
  111. printf "Try again. Pick a nickname that is one word only including lowercase letters and _ or -.\\nAccount name: "
  112. read -r title
  113. done
  114. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\nLogin(?): "
  115. read -r login
  116. [ -z "$login" ] && login="$fulladdr"
  117. if [ "$service" = "gmail.com" ]; then
  118. type="Gmail"; ifgoogleline="folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']"
  119. printf "Google mail account detected. Remember to check the README to make sure of Google-specific settings you must enable.\\n"
  120. else
  121. type="IMAP"; ifgoogleline="remotehost = $imap"
  122. fi
  123. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  124. printf "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9" > "$tmpdir/mutt_all_possible"
  125. idnum=$(diff "$tmpdir/mutt_all_possible" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  126. getpass "$title" || userexit
  127. getprofiles
  128. mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp"
  129. [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
  130. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  131. echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
  132. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  133. echo "$mutt_profile" > "$accdir/$title.muttrc"
  134. }
  135. getpass() { \
  136. 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.\\nPassword: " "$1"
  137. stty -echo
  138. read -r password
  139. stty echo
  140. echo "$password" > "$tmpdir/$1"
  141. printf "Encrypting your password with %s..."
  142. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$1"
  143. print "DONE\\nShredding all memory of your password for safety's sake..."
  144. unset password
  145. shred -u "$tmpdir/$1" | rm -f "$tmpdir/$1"
  146. mv "$tmpdir/$1.gpg" "$creddir"
  147. printf "DONE.\\n"
  148. }
  149. askgpg() { \
  150. 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: "
  151. read -r gpgemail
  152. while ! echo "$gpgemail" | grep "$emailre"; do
  153. printf "That is not a valud email address. Please try again.\\nEmail: "
  154. read -r gpgemail
  155. done
  156. if "$GPG" -K | grep "<$gpgemail>"; then
  157. echo "$gpgemail" > "$creddir/gpgemail"
  158. else
  159. 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"
  160. exit 1
  161. fi
  162. }
  163. formatShortcut() { \
  164. while read data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  165. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  166. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  167. done ;}
  168. gen_delim() { \
  169. delim="="
  170. for i in $(seq $(( $1 - 1 )))
  171. do
  172. delim="$delim-"
  173. done
  174. echo $delim ;}
  175. detectMailboxes() { \
  176. ls -d "$maildir/$1/"* | sed "s/.*\///;s/^/=/" > "$tmpdir/$1_boxes"
  177. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  178. delim="$(gen_delim "$sidebar_width")"
  179. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/$1_boxes" | tr "\n" " ")"
  180. oneline="=$1 $delim $oneline"
  181. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$1.muttrc"
  182. echo mailboxes "$oneline" >> "$muttdir/accounts/$1.muttrc"
  183. sed -i "/# autogenerated/d" "$muttdir/accounts/$1.muttrc"
  184. grep -i "$tmpdir/$1_boxes" -e inbox | sed 1q | formatShortcut i inbox "$1"
  185. grep -i "$tmpdir/$1_boxes" -e sent | sed 1q | formatShortcut s sent "$1"
  186. grep -i "$tmpdir/$1_boxes" -e draft | sed 1q | formatShortcut d drafts "$1"
  187. grep -i "$tmpdir/$1_boxes" -e trash | sed 1q | formatShortcut t trash "$1"
  188. grep -i "$tmpdir/$1_boxes" -e spam | sed 1q | formatShortcut S spam "$1"
  189. grep -i "$tmpdir/$1_boxes" -e junk | sed 1q | formatShortcut j junk "$1"
  190. grep -i "$tmpdir/$1_boxes" -e archive | sed 1q | formatShortcut a archive "$1"
  191. spoolfile=$(grep -i "$tmpdir/$1_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  192. record=$(grep -i "$tmpdir/$1_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  193. postponed=$(grep -i "$tmpdir/$1_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  194. trash=$(grep -i "$tmpdir/$1_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  195. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$1.muttrc"
  196. }
  197. #wipe () { rm "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc" ;}
  198. [ -z "$gpgemail" ] && askgpg
  199. main() { \
  200. while : ; do
  201. printf "What would you like mutt-wizard to do?
  202. 1 Add an email account
  203. 2 Autodetect mailboxes
  204. 3 Change an account's password
  205. 0 Exit
  206. Input a number to continue or press ctrl-c.\\n"
  207. read -r choice
  208. case "$choice" in
  209. 1) addaccount ;;
  210. 0) break ;;
  211. *) printf "Invalid input.\\n"
  212. esac
  213. done
  214. }
  215. main
  216. rm -rf "$tmpdir"