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.
 
 
 
 

277 lines
14 KiB

  1. #!/usr/bin/env bash
  2. [ "$(uname)" == "Darwin" ] && os=".macos"
  3. muttdir="$HOME/.config/mutt"
  4. namere="^[a-z_][a-z0-9_-]*$"
  5. emailre=".+@.+\..+"
  6. createMailboxes() { \
  7. tmpdir=$(mktemp -d)
  8. offlineimap --info -a "$1" 2&> "$tmpdir"/log
  9. sed -n '/^Folderlist/,/^Folderlist/p' "$tmpdir"/log |
  10. grep "^ " | sed -e "s/\//./g;s/(.*//g;s/^ //g" > "$tmpdir"/lognew
  11. while read box; do mkdir -p "$HOME/.mail/$1/$box"; done <"$tmpdir"/lognew ;}
  12. testSync() { (crontab -l | grep mailsync.sh && removeSync) || addSync ;}
  13. addSync() { min=$(dialog --inputbox "How many minutes should be between mail syncs?" 8 60 3>&1 1>&2 2>&3 3>&-)
  14. (crontab -l; echo "*/$min * * * * eval \"export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME -x i3)/environ)\"; "$muttdir"/etc/mailsync.sh") | crontab - &&
  15. 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 ;}
  16. removeSync() { ((crontab -l | sed -e '/mailsync.sh/d') | crontab - >/dev/null) && dialog --msgbox "Cronjob successfully removed. To reactivate, select this option again." 6 60 ;}
  17. changePassword() { \
  18. 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>&- )
  19. while ! [[ "${gpgemail}" =~ ${emailre} ]]; do
  20. gpgemail=$(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)
  21. done
  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 "$muttdir"/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 $(grep "^accounts =" ~/.offlineimaprc | 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=$(sed -e "s/^\|$/\"/g" /tmp/$1_boxes | tr "\n" " ")
  62. oneline="=$1 $delim $oneline"
  63. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/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 -i /tmp/$1_boxes -e inbox | 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 junk | sed 1q | formatShortcut j junk $1
  72. grep -i /tmp/$1_boxes -e archive | sed 1q | formatShortcut a archive $1
  73. spoolfile=$(grep -i /tmp/$1_boxes -e inbox | sed -e 's/=/+/g' | sed 1q)
  74. record=$(grep -i /tmp/$1_boxes -e sent | sed -e 's/=/+/g' | sed 1q)
  75. postponed=$(grep -i /tmp/$1_boxes -e draft | sed -e 's/=/+/g' | sed 1q)
  76. trash=$(grep -i /tmp/$1_boxes -e trash | sed -e 's/=/+/g' | sed 1q)
  77. echo "set spoolfile = \"$spoolfile\"" >> "$muttdir"/accounts/$1.muttrc
  78. echo "set record = \"$record\"" >> "$muttdir"/accounts/$1.muttrc
  79. echo "set postponed = \"$postponed\"" >> "$muttdir"/accounts/$1.muttrc
  80. echo "set trash = \"$trash\"" >> "$muttdir"/accounts/$1.muttrc ;}
  81. # Get all accounts in ~/.offlineimaprc and load into variable `accounts`.
  82. getAccounts() { \
  83. grep "^accounts =" ~/.offlineimaprc | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;' | nl --number-format=ln > /tmp/numbered
  84. accounts=()
  85. while read n s ; do
  86. accounts+=($n "$s" off)
  87. done < /tmp/numbered ;}
  88. # Yields a menu of available accounts.
  89. inventory() { \
  90. getAccounts
  91. choices=$(dialog --separate-output --checklist "Select all desired email accounts with <SPACE>." 15 40 16 "${accounts[@]}" 2>&1 >/dev/tty)
  92. if [ -z "$choices" ];
  93. then
  94. clear
  95. else
  96. userchoices=$(IFS="|"; keys="${choices[*]}"; keys="${keys//|/\\|}"; grep -w "${keys}" /tmp/numbered | awk '{print $2}')
  97. fi ;}
  98. removeAccount() { sed -ie "
  99. /Account $1]/,/Account/{//!d}
  100. /Account $1]/d
  101. s/ $1\(,\|$\)//g
  102. s/=$1\(,\|$\)/=/g
  103. s/,$//g
  104. " ~/.offlineimaprc
  105. rm "$muttdir"/accounts/$1.muttrc
  106. rm "$muttdir"/credentials/$1.gpg
  107. rm -rf "$muttdir"/accounts/$1
  108. sed -i "/$1.muttrc/d" "$muttdir"/personal.muttrc
  109. # Delete from the line matching the account name, until the next account or empty line
  110. sed -i "/account $1/,/^\(\s*$\|account\)/d" ~/.msmtprc ;}
  111. manual() { \
  112. imap=$( dialog --inputbox "Insert the IMAP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  113. iport=$(dialog --inputbox "What is your server's IMAP port number? (Usually 993)" 10 60 3>&1 1>&2 2>&3 3>&-)
  114. smtp=$( dialog --inputbox "Insert the SMTP server for your email provider (excluding the port number)" 10 60 3>&1 1>&2 2>&3 3>&- )
  115. sport=$( dialog --inputbox "What is your server's SMTP port number? (Usually 587 or 465)" 10 60 3>&1 1>&2 2>&3 3>&- ) ;}
  116. 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>&- )
  117. while ! [[ "${fulladdr}" =~ ${emailre} ]]; do
  118. 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)
  119. done
  120. # Check to see if domain is in domain list
  121. serverinfo=$(grep -w ^${fulladdr##*@} "$muttdir"/autoconf/domains.csv)
  122. if [ -z "$serverinfo" ];
  123. then
  124. manual
  125. else
  126. # Read in server data as variables
  127. IFS=, read service imap iport smtp sport <<EOF
  128. $serverinfo
  129. EOF
  130. fi
  131. 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>&- )
  132. 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) || exit
  133. while ! [[ "${title}" =~ ${namere} ]]; do
  134. 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)
  135. done
  136. 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>&- )
  137. # Sets the repo type and other variables for the sed regex.
  138. if [[ "$service" == "gmail.com" ]];
  139. then
  140. type="Gmail"
  141. delet="remotehost"
  142. else
  143. type="IMAP"
  144. delet="Gmail]\/"
  145. fi
  146. if [[ -z "$login" ]];
  147. then
  148. login=$fulladdr
  149. fi
  150. # The replacements
  151. replacement="
  152. s/\$realname/$realname/g;
  153. s/\$title/$title/g;
  154. s/\$fulladdr/$fulladdr/g;
  155. s/\$imap/$imap/g;
  156. s/\$iport/$iport/g;
  157. s/\$smtp/$smtp/g;
  158. s/\$sport/$sport/g;
  159. s/\$type/$type/g;
  160. s/\$login/$login/g;
  161. /$delet/d"
  162. # Gets the first unused shortcut number in the muttrc and puts it in $idnum.
  163. grep "i[0-9]" "$muttdir/personal.muttrc" | awk '{print $3}' | sed -e 's/i//g' > /tmp/mutt_used
  164. echo -e "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9" > /tmp/mutt_all_possible
  165. idnum=$(diff /tmp/mutt_all_possible /tmp/mutt_used | sed -n 2p | awk '{print $2}')
  166. addAccount \
  167. ;}
  168. addAccount() {
  169. # First, adding the encrypted password.
  170. dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox "Enter the password for the \"$title\" account." 10 60 2> /tmp/$title
  171. 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)
  172. shred -u "/tmp/$title"
  173. mv "/tmp/$title.gpg" "$muttdir"/credentials/
  174. # Adding directory structure for cache.
  175. mkdir -p "$muttdir/accounts/$title/cache/bodies"
  176. # Creating the offlineimaprc if it doesn't exist already.
  177. if [ ! -f ~/.offlineimaprc ]; then cp "$muttdir"/autoconf/offlineimap_header"$os" ~/.offlineimaprc; fi
  178. sed -e "$replacement" "$muttdir"/autoconf/offlineimap_profile"$os" >> ~/.offlineimaprc
  179. mkdir -p "$HOME/.mail/$title"
  180. # Creating msmtprc if it doesn't exist already.
  181. if [ ! -f ~/.msmtprc ]; then cp "$muttdir"/autoconf/msmtprc_header ~/.msmtprc; fi
  182. sed -e "$replacement" "$muttdir"/autoconf/msmtprc_profile >> ~/.msmtprc
  183. # Add the mutt profile.
  184. sed -e "$replacement" "$muttdir"/autoconf/mutt_profile > "$muttdir/accounts/$title.muttrc"
  185. # Add a numbered shortcut in the muttrc
  186. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source \"$muttdir\"/accounts/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir"/personal.muttrc
  187. # Add to offlineimaprc sync list.
  188. sed -i.bu "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\\s*$/accounts = $title/g" ~/.offlineimaprc && rm ~/.offlineimaprc.bu
  189. # Makes account default if there is no default account.
  190. grep "$muttdir"/personal.muttrc -e "^source .*accounts.*" >/dev/null && echo there || \
  191. echo "source ${muttdir}/accounts/$title.muttrc" >> "$muttdir"/personal.muttrc
  192. dialog --title "Finalizing your account." --infobox "The account \"$title\" has been added. Now attempting to configure mail directories...
  193. This may take several seconds..." 10 70
  194. createMailboxes "$title" || (clear && exit)
  195. detectMailboxes "$title"
  196. 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 ;}
  197. # This is run when a user chooses to add an account.
  198. chooseAdd() { \
  199. mkdir -p "$muttdir"/credentials/ "$muttdir"/accounts/
  200. 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>&- )
  201. addloop
  202. while : ;
  203. do
  204. dialog --title "Luke's mutt/offlineIMAP password wizard" --yesno "Would you like to add another email account?" 5 60 || break
  205. addloop
  206. done ;}
  207. wipe () { rm "$HOME/.offlineimaprc"
  208. rm -rf "$muttdir"/accounts
  209. rm -f "$muttdir"/credentials/*gpg
  210. rm "$muttdir"/personal.muttrc ;}
  211. while : ;
  212. do
  213. choice=$(dialog --title "Luke's mutt/offlineIMAP wizard" --nocancel \
  214. --menu "What would you like to do?" 15 45 8 \
  215. 0 "List all email accounts configured." \
  216. 1 "Add an email account." \
  217. 2 "Enable/disable autosync." \
  218. 3 "Redetect mailboxes." \
  219. 4 "Change an account's password." \
  220. 5 "Remove an email account." \
  221. 6 "Remove all email accounts." \
  222. 7 "Exit this wizard." \
  223. 3>&1 1>&2 2>&3 3>&1 )
  224. case $choice in
  225. 0) dialog --title "Accounts detected" --msgbox "The following accounts have been detected:
  226. $(grep ~/.offlineimaprc -e "^accounts =" | sed 's/accounts =//g')
  227. " 6 60;;
  228. 1) chooseAdd;;
  229. 2) testSync;;
  230. 3) detectWarning && chooseDetect;;
  231. 4) inventory && for i in $userchoices; do changePassword "$i" ; done;;
  232. 5) inventory && for i in $userchoices; do removeAccount "$i" ; done;;
  233. 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) ;;
  234. 7) clear && break ;;
  235. *) echo "Unable to read response from dialog. Exiting." >&2; exit 2
  236. esac
  237. done