Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

268 righe
13 KiB

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