選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

255 行
12 KiB

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