You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mutt-wizard.sh 11 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/bin/bash
  2. muttdir="$HOME/.config/mutt/"
  3. chooseSync() { (crontab -l && testSync) || dialog --msgbox "No cronjob manager detected. Please install one and return to enable automatic mailsyncing" 10 60 ;}
  4. testSync() { (crontab -l | grep .config/mutt/etc/mailsync && removeSync) || addSync ;}
  5. addSync() { min=$(dialog --inputbox "How many minutes should be between mail syncs?" 8 60 3>&1 1>&2 2>&3 3>&-)
  6. (crontab -l; echo "*/$min * * * * /home/$(whoami)/.config/mutt/etc/mailsync.sh") | crontab - &&
  7. dialog --msgbox "Cronjob successfully added. Remember you may need to restart or tell systemd/etc. to start your cron manager for this to take effect." 7 60 ;}
  8. removeSync() { ((crontab -l | sed -e '/.config\/mutt\/etc\/mailsync/d') | crontab - >/dev/null) && dialog --msgbox "Cronjob successfully removed. To reactivate, select this option again." 6 60 ;}
  9. changePassword() { \
  10. gpgemail=$( dialog --title "Luke's mutt/offlineIMAP password wizard" --inputbox "Insert the email address with which you originally created your GPG key pair. This is NOT necessarily the email you want to configure." 10 60 3>&1 1>&2 2>&3 3>&- )
  11. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the new password for the \"$1\" account." 10 60 2> /tmp/$1
  12. gpg2 -r $gpgemail --encrypt /tmp/$1 || (dialog --title "GPG decryption failed." --msgbox "GPG decryption failed. This is either because you do not have a GPG key pair or because your distro uses GPG1 and you thus need to symlink /usr/bin/gpg2 to /usr/bin/gpg." 7 60 && break)
  13. shred -u /tmp/$1
  14. mv /tmp/$1.gpg ~/.config/mutt/credentials/ ;}
  15. chooseDetect() { for x in $(cat ~/.offlineimaprc | grep "^accounts =" | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;'); do detectMailboxes $x; done && detectSuccess ;}
  16. detectWarning() { \
  17. dialog --title "Mailbox detect requirement" --yesno "In order for the mailbox detection system to work, you must have
  18. 1) already have added the email account with this wizard, and
  19. 2) already have run offlineimap at least once to synchronize your mail.
  20. This detection system only works if you already have begun your first sync. If you have, press 'yes' to continue." 12 70 ;}
  21. detectSuccess() { dialog --title "Mailbox detect complete." --msgbox "The script has now smartly detected your different mailboxes and has enabled them in the sidebar and given you keyboard shortcuts as below:
  22. gi for move to the Inbox.
  23. gs for move to Sent Mail.
  24. gd for move to Drafts
  25. gS for move to Spam
  26. ga for move to the Archive.
  27. gj for move to Junk
  28. gt for move to Trash
  29. These shortcuts will only work if your email system does have that particular folder (i.e. if your email system has a Junk folder, but not a Trash folder, 'gt' will not work, etc." 20 60 ;}
  30. formatShortcut() { \
  31. while read data; do
  32. echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\"" >> "$muttdir"accounts/$3.muttrc
  33. done ;}
  34. gen_delim() { \
  35. delim="="
  36. for i in `seq $(( $1 - 1 ))`
  37. do
  38. delim="$delim-"
  39. done
  40. echo $delim ;}
  41. detectMailboxes() { \
  42. find ~/.mail/$1 -maxdepth 1 -mindepth 1 -type d | sed -e "s/.*\///g;s/^/=/g" > /tmp/$1_boxes
  43. sidebar_width=$(sed -n -e '/^set sidebar_width/p' "$muttdir"/muttrc | awk -F'=' '{print $2}')
  44. delim=$(gen_delim $sidebar_width)
  45. oneline=$(cat /tmp/$1_boxes | sed -e "s/^\|$/\"/g" | tr "\n" " ")
  46. oneline="=$1 $delim $oneline"
  47. sed -i "/^mailboxes\|^set spoolfile\|^set record\|^set postponed/d" "$muttdir"accounts/$1.muttrc
  48. echo mailboxes $oneline >> "$muttdir"accounts/$1.muttrc
  49. sed -i "/^macro index,pager g/d" "$muttdir"accounts/$1.muttrc
  50. grep -vi /tmp/$1_boxes -e "trash\|drafts\|sent\|trash\|spam\|junk\|archive\|chat\|old\|new\|gmail\|sms\|call" | sort -n | sed 1q | formatShortcut i inbox $1
  51. grep -i /tmp/$1_boxes -e sent | sed 1q | formatShortcut s sent $1
  52. grep -i /tmp/$1_boxes -e draft | sed 1q | formatShortcut d drafts $1
  53. grep -i /tmp/$1_boxes -e trash | sed 1q | formatShortcut t trash $1
  54. grep -i /tmp/$1_boxes -e spam | sed 1q | formatShortcut S spam $1
  55. grep -i /tmp/$1_boxes -e archive | sed 1q | formatShortcut a archive $1
  56. spoolfile=$(grep -vi /tmp/$1_boxes -e "trash\|drafts\|sent\|trash\|spam\|junk\|archive\|chat\|old\|new\|gmail\|sms\|call" | sort -n | sed 1q | sed -e 's/=/+/g')
  57. record=$(grep -i /tmp/$1_boxes -e sent | sed -e 's/=/+/g' | sed 1q)
  58. postponed=$(grep -i /tmp/$1_boxes -e draft | sed -e 's/=/+/g' | sed 1q)
  59. echo "set spoolfile = \"$spoolfile\"" >> "$muttdir"accounts/$1.muttrc
  60. echo "set record = \"$record\"" >> "$muttdir"accounts/$1.muttrc
  61. echo "set postponed = \"$postponed\"" >> "$muttdir"accounts/$1.muttrc ;}
  62. # Get all accounts in ~/.offlineimaprc and load into variable `accounts`.
  63. getAccounts() { \
  64. cat ~/.offlineimaprc | grep "^accounts =" | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;' | nl --number-format=ln > /tmp/numbered
  65. accounts=()
  66. while read n s ; do
  67. accounts+=($n "$s" off)
  68. done < /tmp/numbered ;}
  69. # Yields a menu of available accounts.
  70. inventory() { \
  71. getAccounts
  72. choices=$(dialog --separate-output --checklist "Select all desired email accounts with <SPACE>." 15 40 16 "${accounts[@]}" 2>&1 >/dev/tty)
  73. if [ -z "$choices" ];
  74. then
  75. clear
  76. else
  77. userchoices=$(IFS="|"; keys="${choices[*]}"; keys="${keys//|/\\|}"; grep -w "${keys}" /tmp/numbered | awk '{print $2}')
  78. fi ;}
  79. removeAccount() { sed -ie "
  80. /Account $1]/,/Account/{//!d}
  81. /Account $1]/d
  82. s/ $1\(,\|$\)//g
  83. s/=$1\(,\|$\)/=/g
  84. s/,$//g
  85. " ~/.offlineimaprc
  86. rm "$muttdir"accounts/$1.muttrc
  87. rm "$muttdir"credentials/$1.gpg
  88. rm -rf "$muttdir"accounts/$1
  89. sed -i '/$1.muttrc/d' "$muttdir"personal.muttrc ;}
  90. manual() { \
  91. imap=$( dialog --inputbox "Insert the IMAP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  92. iport=$(dialog --inputbox "What is your server's IMAP port number? (Usually 993)" 10 60 3>&1 1>&2 2>&3 3>&-)
  93. smtp=$( dialog --inputbox "Insert the SMTP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  94. sport=$( dialog --inputbox "What is your server's SMTP port number? (Usually 587 or 465)" 10 60 3>&1 1>&2 2>&3 3>&- ) ;}
  95. addloop() { 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>&- )
  96. # Check to see if domain is in domain list
  97. serverinfo=$(cat "$muttdir"autoconf/domains.csv | grep -w ^${fulladdr##*@})
  98. if [ -z "$serverinfo" ];
  99. then
  100. manual
  101. else
  102. # Read in server data as variables
  103. IFS=, read service imap iport smtp sport <<EOF
  104. $serverinfo
  105. EOF
  106. fi
  107. 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>&- )
  108. 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>&- )
  109. login=$(dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Enter your login for the \"$title\" account.\n(If left empty, the full email address will be used instead.)" 10 60 3>&1 1>&2 2>&3 3>&- )
  110. # Sets the repo type and other variables for the sed regex.
  111. if [[ "$service" == "gmail.com" ]];
  112. then
  113. type="Gmail"
  114. delet="remotehost"
  115. else
  116. type="IMAP"
  117. delet="Gmail]\/"
  118. fi
  119. if [[ -z "$login" ]];
  120. then
  121. login=$fulladdr
  122. fi
  123. # The replacements
  124. replacement="
  125. s/\$realname/$realname/g;
  126. s/\$title/$title/g;
  127. s/\$fulladdr/$fulladdr/g;
  128. s/\$imap/$imap/g;
  129. s/\$iport/$iport/g;
  130. s/\$smtp/$smtp/g;
  131. s/\$sport/$sport/g;
  132. s/\$type/$type/g;
  133. s/\$login/$login/g;
  134. /$delet/d"
  135. # Gets the first unused shortcut number in the muttrc and puts it in $idnum.
  136. cat "$muttdir"personal.muttrc | grep i[0-9] | awk '{print $3}' | sed -e 's/i//g' > /tmp/mutt_used
  137. echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9" > /tmp/mutt_all_possible
  138. idnum=$(diff /tmp/mutt_all_possible /tmp/mutt_used | sed -n 2p | awk '{print $2}')
  139. addAccount \
  140. ;}
  141. addAccount() {
  142. # First, adding the encrypted password.
  143. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the password for the \"$title\" account." 10 60 2> /tmp/$title
  144. gpg2 -r $gpgemail --encrypt /tmp/$title || (dialog --title "GPG decryption failed." --msgbox "GPG decryption failed. This is either because you do not have a GPG key pair or because your distro uses GPG1 and you thus need to symlink /usr/bin/gpg2 to /usr/bin/gpg." 7 60 && break)
  145. shred -u /tmp/$title
  146. mv /tmp/$title.gpg ~/.config/mutt/credentials/
  147. # Adding directory structure for cache.
  148. mkdir -p "$muttdir"accounts/$title/cache/bodies
  149. # Creating the offlineimaprc if it doesn't exist already.
  150. if [ ! -f ~/.offlineimaprc ]; then cp "$muttdir"autoconf/offlineimap_header ~/.offlineimaprc; fi
  151. cat "$muttdir"autoconf/offlineimap_profile | sed -e "$replacement" >> ~/.offlineimaprc
  152. mkdir -p ~/.mail/$title
  153. # Add the mutt profile.
  154. cat "$muttdir"autoconf/mutt_profile | sed -e "$replacement" > "$muttdir"accounts/$title.muttrc
  155. # Add a numbered shortcut in the muttrc
  156. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source "$muttdir"accounts/$title.muttrc<enter><change-folder>!<enter>'" >> "$muttdir"personal.muttrc
  157. # Add to offlineimaprc sync list.
  158. sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =$/accounts = $title/g" ~/.offlineimaprc
  159. # Makes account default if there is no default account.
  160. grep "$muttdir"personal.muttrc -e "^source .*accounts.*" >/dev/null && echo there || \
  161. echo "source ${muttdir}accounts/$title.muttrc" >> "$muttdir"personal.muttrc
  162. dialog --title "Finalizing your account." --msgbox "The account \"$title\" has been added. Now to finalize installation, do the following:
  163. 1) Run offlineimap to start the sync. This will start your mail sync.
  164. 2) After or while running offlineimap, choose the \"autodetect mailboxes\" option, which will finalize your config files based on the directory structure of the downloaded mailbox.
  165. After that, you will be able to open neomutt to your email account." 13 80 ;}
  166. # This is run when a user chooses to add an account.
  167. chooseAdd() { \
  168. mkdir -p "$muttdir"credentials/ "$muttdir"accounts/
  169. gpgemail=$( dialog --title "Luke's mutt/offlineIMAP password wizard" --inputbox "Insert the email address with which you originally created your GPG key pair. This is NOT necessarily the email you want to configure." 10 60 3>&1 1>&2 2>&3 3>&- )
  170. addloop
  171. while : ;
  172. do
  173. dialog --title "Luke's mutt/offlineIMAP password wizard" --yesno "Would you like to add another email account?" 5 60 || break
  174. addloop
  175. done ;}
  176. wipe () { rm $HOME/.offlineimaprc
  177. rm -rf "$muttdir"/accounts
  178. rm -f "$muttdir"credentials/*gpg
  179. rm "$muttdir"personal.muttrc ;}
  180. while : ;
  181. do
  182. choice=$(dialog --title "Luke's mutt/offlineIMAP wizard" --nocancel \
  183. --menu "What would you like to do?" 16 45 8 \
  184. 0 "List all email accounts configured." \
  185. 1 "Add an email account." \
  186. 2 "Auto-detect mailboxes for an account." \
  187. 3 "Enable/disable autosync." \
  188. 4 "Change an account's password." \
  189. 5 "Remove an email account." \
  190. 6 "Remove all email accounts." \
  191. 7 "Exit this wizard." \
  192. 3>&1 1>&2 2>&3 3>&1 )
  193. case $choice in
  194. 0) dialog --title "Accounts detected" --msgbox "The following accounts have been detected:
  195. $(grep ~/.offlineimaprc -e "^accounts =" | sed 's/accounts =//g')
  196. " 6 60;;
  197. 1) chooseAdd;;
  198. 2) detectWarning && chooseDetect ;;
  199. 3) chooseSync;;
  200. 4) inventory && for i in $userchoices; do changePassword $i ; done;;
  201. 5) inventory && for i in $userchoices; do removeAccount $i ; done;;
  202. 6) (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) ;;
  203. 7) clear && break ;;
  204. esac
  205. done