No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

246 líneas
10 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")" # 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. offlineimap_profile="
  77. [Account $title]
  78. localrepository = $title-local
  79. remoterepository = $title-remote
  80. [Repository $title-remote]
  81. auth_mechanisms = LOGIN
  82. type = $type
  83. remoteuser = $login
  84. remotepasseval = mailpasswd(\"$title\")
  85. remotehost = $imap
  86. remoteport = $iport
  87. folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']
  88. sslcacertfile = /etc/ssl/certs/ca-certificates.crt
  89. [Repository $title-local]
  90. type = Maildir
  91. localfolders = $maildir/$title
  92. "
  93. }
  94. userexit() { clear; exit ;}
  95. chooseadd() { \
  96. fulladdr=$( dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Insert the full email address for the account you want to configure." 10 60 3>&1 1>&2 2>&3 3>&- ) || userexit
  97. while ! echo "$fulladdr" | grep "emailre" >/dev/null; do
  98. fulladdr=$(dialog --no-cancel --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "That's not a valid email address. Please input the entire address." 10 60 3>&1 1>&2 2>&3 3>&1) || userexit
  99. done
  100. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  101. serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
  102. if [ -z "$serverinfo" ]; then
  103. imap="$( dialog --inputbox "Insert the IMAP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )"
  104. iport="$(dialog --inputbox "What is your server's IMAP port number? (Usually 993)" 10 60 3>&1 1>&2 2>&3 3>&-)"
  105. smtp="$(dialog --inputbox "Insert the SMTP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )"
  106. sport="$(dialog --inputbox "What is your server's SMTP port number? (Usually 587 or 465)" 10 60 3>&1 1>&2 2>&3 3>&- )"
  107. else
  108. IFS=, read service imap iport smtp sport <<EOF
  109. $serverinfo
  110. EOF
  111. fi
  112. realname=$( dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Enter the full name you'd like to be identified by on this email account." 10 60 3>&1 1>&2 2>&3 3>&- ) || userexit
  113. title=$(dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Give a short, one-word name for this email account that will differentiate it from other email accounts." 10 60 3>&1 1>&2 2>&3 3>&1) || userexit
  114. while ! echo "$title" | grep "$namere" >/dev/null; do
  115. title=$(dialog --no-cancel --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Account title not valid. Give a username beginning with a letter, with only lowercase letters, - or _." 10 60 3>&1 1>&2 2>&3 3>&1) || userexit
  116. done
  117. login=$(dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "If you have a username for the \"$title\" account which is different from your email address, enter it here. Otherwise leave this prompt blank." 10 60 3>&1 1>&2 2>&3 3>&- ) || userexit
  118. [ -z "$login" ] && login="$fulladdr"
  119. # service
  120. # ifgoogleline
  121. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  122. printf "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9" > "$tmpdir/mutt_all_possible"
  123. idnum=$(diff "$tmpdir/mutt_all_possible" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  124. getpass "$title" || userexit
  125. getprofiles
  126. mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp"
  127. [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
  128. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  129. echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
  130. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  131. echo "$mutt_profile" > "$accdir/$title.muttrc"
  132. }
  133. getpass() { \
  134. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the password for the \"$1\" account." 10 60 2> "$tmpdir/$1"
  135. "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$1"
  136. shred -u "$tmpdir/$1"
  137. mv "$tmpdir/$1.gpg" "$creddir"
  138. }
  139. askgpg() { \
  140. gpgemail=$(dialog --title "Luke's mutt/offlineIMAP wizard" --inputbox "To safely encrypt passwords, mutt-wizard requires that you have a GPG public/private key pair.\n\nPlease insert the email address for your key pair here, or generate one first with \`$GPG --full-gen-key\` and come back." 12 60 3>&1 1>&2 2>&3 3>&- ) || userexit
  141. while ! echo "$gpgemail" | grep "$emailre"; do
  142. gpgemail=$(dialog --title "Luke's mutt/offlineIMAP wizard" --inputbox "That's not a valid email address. Please input the entire address." 10 60 3>&1 1>&2 2>&3 3>&1) || userexit
  143. done
  144. if "$GPG" -K | grep "<$gpgemail>"; then
  145. echo "$gpgemail" > "$creddir/gpgemail"
  146. else
  147. dialog --title "Luke's mutt/offlineIMAP wizard" --msgbox "You do not appear to have a private key with that email address.\n\nPlease be sure to create your key with \`$GPG --full-gen-key\` and come back." 8 70
  148. clear; exit
  149. fi
  150. }
  151. formatShortcut() { \
  152. while read data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  153. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  154. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  155. done ;}
  156. gen_delim() { \
  157. delim="="
  158. for i in $(seq $(( $1 - 1 )))
  159. do
  160. delim="$delim-"
  161. done
  162. echo $delim ;}
  163. detectMailboxes() { \
  164. ls -d "$maildir/$1/"* | sed "s/.*\///;s/^/=/" > "$tmpdir/$1_boxes"
  165. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  166. delim="$(gen_delim "$sidebar_width")"
  167. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/$1_boxes" | tr "\n" " ")"
  168. oneline="=$1 $delim $oneline"
  169. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$1.muttrc"
  170. echo mailboxes "$oneline" >> "$muttdir/accounts/$1.muttrc"
  171. sed -i "/# autogenerated/d" "$muttdir/accounts/$1.muttrc"
  172. grep -i "$tmpdir/$1_boxes" -e inbox | sed 1q | formatShortcut i inbox "$1"
  173. grep -i "$tmpdir/$1_boxes" -e sent | sed 1q | formatShortcut s sent "$1"
  174. grep -i "$tmpdir/$1_boxes" -e draft | sed 1q | formatShortcut d drafts "$1"
  175. grep -i "$tmpdir/$1_boxes" -e trash | sed 1q | formatShortcut t trash "$1"
  176. grep -i "$tmpdir/$1_boxes" -e spam | sed 1q | formatShortcut S spam "$1"
  177. grep -i "$tmpdir/$1_boxes" -e junk | sed 1q | formatShortcut j junk "$1"
  178. grep -i "$tmpdir/$1_boxes" -e archive | sed 1q | formatShortcut a archive "$1"
  179. spoolfile=$(grep -i "$tmpdir/$1_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
  180. record=$(grep -i "$tmpdir/$1_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
  181. postponed=$(grep -i "$tmpdir/$1_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
  182. trash=$(grep -i "$tmpdir/$1_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
  183. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$1.muttrc"
  184. }
  185. #wipe () { rm "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc" ;}
  186. [ -z "$gpgemail" ] && askgpg
  187. while : ;
  188. do
  189. choice=$(dialog --title "Luke's mutt/offlineIMAP wizard" --nocancel \
  190. --menu "What would you like to do?" 15 50 8 \
  191. 0 "Add email account (Begin installtion)" \
  192. 1 "Autodetect mailboxes (Finalize installation)" \
  193. 2 "Enable/disable autosync." \
  194. 3 "Change an account's password" \
  195. 4 "Remove an account" \
  196. 5 "Remove all accounts" \
  197. 6 "Change your GPG email" \
  198. 7 "Exit this wizard." \
  199. 3>&1 1>&2 2>&3 3>&1 )
  200. case $choice in
  201. 0) dialog --title "Accounts detected" --msgbox "The following accounts have been detected:
  202. $(grep ~/.offlineimaprc -e "^accounts =" | sed 's/accounts =//g')
  203. " 6 60;;
  204. 1) chooseadd ;;
  205. 2) detectWarning && chooseDetect;;
  206. 3) inventory && for i in $userchoices; do getpass "$i" ; done;;
  207. 4) inventory && for i in $userchoices; do removeAccount "$i" ; done;;
  208. 5) (dialog --defaultno --title "Wipe all custom neomutt/offlineIMAP settings?" --yesno "Would you like to wipe all of the mutt/offlineIMAP settings generated by the system?" 6 60 && wipe) ;;
  209. 6) askgpg ;;
  210. 7) clear && break ;;
  211. *) echo "Error. Are you sure you have dialog installed?" >&2; exit 2
  212. esac
  213. done
  214. rm -rf "$tmpdir"