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