Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

282 lignes
14 KiB

  1. #!/bin/sh
  2. [ ! -f "$HOME/.password-store/.gpg-id" ] && printf "\`pass\` must be installed and initialized to encrypt passwords.\\nBe sure it is installed and run \`pass init <yourgpgemail>\`.\\nIf you don't have a GPG public private key pair, run \`gpg --full-gen-key\` first.\\n" && exit
  3. muttdir="$HOME/.config/mutt" # Main mutt config location
  4. accdir="$muttdir/accounts" # Directory for account settings
  5. maildir="$HOME/.local/share/mail" # Location of mail storage
  6. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  7. emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
  8. muttshare="/usr/share/mutt-wizard"
  9. mbsyncrc="$HOME/.mbsyncrc"
  10. sslcert="/etc/ssl/certs/ca-certificates.crt"
  11. getprofiles() { \
  12. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  13. printf "Creating profiles for \`%s\`..." "$title"
  14. msmtp_header="defaults
  15. auth on
  16. tls on
  17. tls_trust_file $sslcert
  18. logfile ~/.config/msmtp/msmtp.log
  19. "
  20. msmtp_profile="
  21. account $title
  22. host $smtp
  23. port $sport
  24. from $fulladdr
  25. user $login
  26. passwordeval \"pass mutt-wizard-$title\""
  27. mbsync_profile="IMAPStore $title-remote
  28. Host $imap
  29. Port $iport
  30. User $login
  31. PassCmd \"pass mutt-wizard-$title\"
  32. SSLType IMAPS
  33. CertificateFile /etc/ssl/certs/ca-certificates.crt
  34. MaildirStore $title-local
  35. Subfolders Verbatim
  36. Path ~/.local/share/mail/$title/
  37. Inbox ~/.local/share/mail/$title/INBOX
  38. Flatten .
  39. Channel $title
  40. Expunge Both
  41. Master :$title-remote:
  42. Slave :$title-local:
  43. Patterns * !\"[Gmail]/All Mail\"
  44. Create Both
  45. SyncState *
  46. # End profile
  47. "
  48. if [ "$accounttype" = "offline" ]; then
  49. mutt_profile="# vim: filetype=neomuttrc
  50. # muttrc file for account $title
  51. set realname = \"$realname\"
  52. set from = \"$fulladdr\"
  53. set sendmail = \"/usr/bin/msmtp -a $title\"
  54. alias me $realname <$fulladdr>
  55. set folder = \"$maildir/$title\"
  56. set header_cache = $accdir/$title/cache/headers
  57. set message_cachedir = $accdir/$title/cache/bodies
  58. set certificate_file = $accdir/$title/certificates
  59. set mbox_type = Maildir
  60. bind index,pager gg noop
  61. bind index,pager g noop
  62. bind index,pager M noop
  63. bind index,pager C noop
  64. bind index gg first-entry
  65. macro index o \"<shell-escape>mailsync -V $title<enter>\" \"run mbsync to sync $title\"
  66. unmailboxes *
  67. "
  68. else
  69. mutt_profile="# vim: filetype=neomuttrc
  70. # muttrc file for account $title
  71. set realname = \"$realname\"
  72. set from = \"$fulladdr\"
  73. set sendmail = \"/usr/bin/msmtp -a $title\"
  74. alias me $realname <$fulladdr>
  75. set folder = \"imaps://$fulladdr@$imap:$iport\"
  76. set imap_user = \"$login\"
  77. set header_cache = $accdir/$title/cache/headers
  78. set message_cachedir = $accdir/$title/cache/bodies
  79. set certificate_file = $accdir/$title/certificates
  80. set imap_pass = \`pass mutt-wizard-$title\`
  81. set mbox_type = Maildir
  82. set ssl_starttls = yes
  83. set ssl_force_tls = yes
  84. bind index,pager gg noop
  85. bind index,pager g noop
  86. bind index,pager M noop
  87. bind index,pager C noop
  88. bind index gg first-entry
  89. unmailboxes *
  90. "
  91. fi
  92. printf "DONE.\\n"
  93. }
  94. askinfo() { \
  95. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  96. read -r fulladdr
  97. printf "\033[0m"
  98. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  99. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  100. read -r fulladdr
  101. printf "\033[0m"
  102. done
  103. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  104. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  105. serverinfo="$(grep "$domain" "$muttshare/domains.csv" 2>/dev/null)"
  106. if [ -z "$serverinfo" ]; then
  107. printf "Your email domain is not in mutt-wizard's database yet.\\nmutt-wizard will still autoconfigure everything, but you will have to manually type in your service's IMAP and SMTP server information.\\nYou can usually quickly find this by internet searching for it.\\n"
  108. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  109. read -r imap
  110. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  111. read -r iport
  112. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  113. read -r smtp
  114. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  115. read -r sport
  116. printf "\033[0m\\nGreat! If you want to be helpful, copy the line below and you can add it to the \`domains.csv\` file on Github.\\nThis will make things easier for others who use your email provider.\\n\\n%s,%s,%s,%s,%s\\n\\nAlthough be sure to test to see if these settings work first! ;-)\\n" "$domain" "$imap" "$iport" "$smtp" "$sport"
  117. else
  118. IFS=, read -r service imap iport smtp sport <<EOF
  119. $serverinfo
  120. EOF
  121. printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically be found, so you won't have to look anything up!\\n\t\033[1mIMAP server\033[0m: %s\\n\t\033[1mIMAP port\033[0m: %s\\n\t\033[1mSMTP server\033[0m: %s\\n\t\033[1mSMTP port\033[0m: %s\\nThis data will be used by the wizard.\\n" "$imap" "$iport" "$smtp" "$sport"
  122. case "$service" in
  123. gmail.com) printf "\033[31mREMEMBER: Gmail users must enable \"less secure\" (third-party) applications first for the sync to work:\\nhttps://support.google.com/accounts/answer/6010255\\n\033[0m" ;;
  124. protonmail.*) printf "\033[31mREMEMBER: Protonmail users must install and configure Protonmail Bridge first for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" ;;
  125. esac
  126. fi
  127. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  128. read -r realname
  129. printf "Enter a short, \033[36mone-word identifier\033[0m for this email account that will distinguish them from any other accounts you add.\\n\tAccount name: "
  130. read -r title
  131. while ! echo "$title" | grep "$namere" >/dev/null; do
  132. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: \033[36m\t"
  133. read -r title
  134. printf "\033[0m"
  135. done
  136. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\033[34mMost accounts will not have a separate login, so you should probably leave this blank.\033[0m\\n\tLogin(?): \033[36m"
  137. read -r login
  138. printf "\033[0m"
  139. [ -z "$login" ] && login="$fulladdr"
  140. tmpdir="$(mktemp -d)"
  141. grep "i[0-9]" "$muttdir/personal.muttrc" 2>/dev/null | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  142. seq 1 9 > "$tmpdir/mutt_all"
  143. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  144. getpass
  145. getprofiles
  146. mkdir -p "$accdir/$title/cache/bodies" "$HOME/.config/msmtp"
  147. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  148. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  149. echo "$mutt_profile" > "$accdir/$title.muttrc"
  150. echo "$mbsync_profile" >> "$mbsyncrc"
  151. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  152. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$title.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttdir/personal.muttrc"
  153. }
  154. getpass() { while : ; do pass rm -f "mutt-wizard-$title" >/dev/null 2>&1
  155. pass insert "mutt-wizard-$title" && break; done ;}
  156. formatShortcut() { \
  157. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # autogenerated"
  158. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # autogenerated"
  159. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # autogenerated"; } >> "$accdir/$3.muttrc"
  160. done ;}
  161. tryconnect() { ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning \`mbsync %s\` manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" "$title" && return 1
  162. mkdir -p "$maildir/$title"
  163. if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1; then
  164. [ "$accounttype" = "online" ] && sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc"
  165. printf "\033[32mMailboxes detected.\033[0m\\n"
  166. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  167. else
  168. printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or sever settings, or there are other requirements for your account out of the control of mutt-wizard.\\n"
  169. delete "$title"; return 1
  170. fi ;}
  171. finalize() { \
  172. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  173. [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that mbsync has not been successfully run.\\nRun mbsync, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && return
  174. echo "$boxes" > "$tmpdir/title_boxes"
  175. printf "Setting up the mutt sidebar...\\n"
  176. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  177. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  178. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  179. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  180. oneline="=$title $delim $oneline"
  181. spoolfile=$(grep -i -m 1 "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g')
  182. record=$(grep -i -m 1 "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g')
  183. postponed=$(grep -i -m 1 "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g')
  184. trash=$(grep -i -m 1 "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g')
  185. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$accdir/$title.muttrc"
  186. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$title.muttrc"
  187. echo mailboxes "$oneline" >> "$accdir/$title.muttrc"
  188. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  189. sed -i "/# autogenerated/d" "$accdir/$title.muttrc"
  190. grep -i -m 1 inbox "$tmpdir/title_boxes" | formatShortcut i inbox "$title"
  191. grep -i -m 1 sent "$tmpdir/title_boxes" | formatShortcut s sent "$title"
  192. grep -i -m 1 draft "$tmpdir/title_boxes" | formatShortcut d drafts "$title"
  193. grep -i -m 1 trash "$tmpdir/title_boxes" | formatShortcut t trash "$title"
  194. grep -i -m 1 spam "$tmpdir/title_boxes" | formatShortcut S spam "$title"
  195. grep -i -m 1 junk "$tmpdir/title_boxes" | formatShortcut j junk "$title"
  196. grep -i -m 1 archive "$tmpdir/title_boxes" | formatShortcut a archive "$title"
  197. [ "$accounttype" = "offline" ] && printf "All done.\\n\033[33mYou should now be able to run \`\033[32mmbsync %s\033[33m\` to begin to download your mail.\033[0m\\n" "$title"
  198. }
  199. confirm() { printf "Do you want to to %s? [y/N]\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  200. printf "Are you really, really sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  201. return 0 ;}
  202. pick() { numbered="$(find "$accdir" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | nl)"
  203. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to choose from.\\n" && return 1
  204. printf "Select an accounts to %s:\\n" "$1"
  205. echo "$numbered"
  206. printf "\033[36m\t"
  207. read -r input
  208. printf "\033[0m"
  209. [ -z "$input" ] && return 1
  210. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  211. [ -z "$title" ] && printf "Invalid response." && return 1
  212. return 0 ;}
  213. delete() { sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc"
  214. rm -rf "${accdir:?}/${title:?}" "$accdir/$title.muttrc"
  215. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  216. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config"
  217. }
  218. choosecron() { ! pgrep crond >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  219. if crontab -l | grep mailsync >/dev/null; then
  220. echo "Active mail sync cronjob detected. Do you want to remove it?"
  221. printf "\033[36m\t"
  222. read -r rmyn
  223. printf "\033[0m"
  224. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  225. else
  226. echo "How many minutes between each mail sync?"
  227. printf "\033[36m\t"
  228. read -r minnum
  229. printf "\033[0m"
  230. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  231. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  232. read -r minnum
  233. printf "\033[0m"
  234. done
  235. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus; export DISPLAY=:0; . \$HOME/.profile; $(type mailsync | cut -d' ' -f3)") | crontab - &&
  236. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  237. fi ;}
  238. asktype() { while : ; do
  239. printf "Do you want to keep your mail for this account offline with mbsync? [yes/no]\\n\t"
  240. read -r offnot
  241. case "$offnot" in
  242. [Yy][Ee][Ss]) accounttype="offline" && break ;;
  243. [Nn][Oo]) accounttype="online" && break ;;
  244. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  245. esac; done ;}
  246. init() { \
  247. echo "Initializing a mutt-wizard configuration..."
  248. [ -f "$muttdir/muttrc" ] && echo "There is already a preexisting config." && return 1
  249. cp -v "$muttshare/mailcap" "$muttdir"
  250. cp -v "$muttshare/muttrc" "$muttdir"
  251. }
  252. case "$1" in
  253. init) init ;;
  254. new) asktype && askinfo && tryconnect && finalize ;;
  255. passedit) pick "change the password of" && confirm "change \`$title\`'s password" && getpass ;;
  256. delete) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  257. purge) confirm "delete all account data" && rm -rf "$mbsyncrc" "$accdir" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged." ;;
  258. cron) choosecron ;;
  259. *) printf "Invalid input.\\n"
  260. esac
  261. rm -rf "$tmpdir"