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.
 
 
 
 

248 lines
12 KiB

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