Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

321 строка
15 KiB

  1. #!/bin/sh
  2. [ ! -f "$HOME/.password-store/.gpg-id" ] && printf "\`pass\` or \`gopass\` must be installed and initialized to encrypt passwords.\\nBe sure it is installed and run \`(go)pass init <yourgpgemail>\`.\\nIf you don't have a GPG public private key pair, run \`gpg --full-gen-key\` first.\\n" && exit
  3. ! command -v mbsync >/dev/null && printf "\`mbsync\` must be installed to run mutt-wizard.\\n" && exit
  4. pass="pass"
  5. ifinstalled gopass
  6. if [ $? -eq 0 ]; then
  7. pass="gopass"
  8. fi
  9. muttdir="$HOME/.config/mutt" # Main mutt config location
  10. accdir="$muttdir/accounts" # Directory for account settings
  11. maildir="$HOME/.local/share/mail" # Location of mail storage
  12. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  13. emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
  14. muttshare="/usr/share/mutt-wizard"
  15. mbsyncrc="$HOME/.mbsyncrc"
  16. mwconfig="$muttshare/mutt-wizard.muttrc"
  17. cachedir="$HOME/.cache/mutt-wizard"
  18. muttrc="$muttdir/muttrc"
  19. ssltype="IMAPS" # This is later changed to `None` later in the script if using Protonmail
  20. for x in "/etc/ssl/certs/ca-certificates.crt" "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"
  21. do
  22. [ -f "$x" ] && sslcert="$x" && break
  23. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  24. getaccounts() { accounts="$(find "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\..*//" | sort -n)" ;}
  25. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  26. getprofiles() { \
  27. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  28. printf "Creating profiles for \`%s\`..." "$title"
  29. msmtp_header="defaults
  30. auth on
  31. tls on
  32. tls_trust_file $sslcert
  33. logfile ~/.config/msmtp/msmtp.log
  34. "
  35. msmtp_profile="account $title
  36. host $smtp
  37. port $sport
  38. from $fulladdr
  39. user $login
  40. passwordeval \"$pass mutt-wizard-$title\""
  41. mbsync_profile="IMAPStore $title-remote
  42. Host $imap
  43. Port $iport
  44. User $login
  45. PassCmd \"$pass mutt-wizard-$title\"
  46. SSLType $ssltype
  47. CertificateFile $sslcert
  48. MaildirStore $title-local
  49. Subfolders Verbatim
  50. Path ~/.local/share/mail/$title/
  51. Inbox ~/.local/share/mail/$title/INBOX
  52. Flatten .
  53. Channel $title
  54. Expunge Both
  55. Master :$title-remote:
  56. Slave :$title-local:
  57. Patterns * !\"[Gmail]/All Mail\"
  58. Create Both
  59. SyncState *
  60. MaxMessages $maxmes
  61. # End profile
  62. "
  63. if [ "$accounttype" = "offline" ]; then
  64. mutt_profile="# vim: filetype=neomuttrc
  65. # muttrc file for account $title
  66. set realname = \"$realname\"
  67. set from = \"$fulladdr\"
  68. set sendmail = \"msmtp -a $title\"
  69. alias me $realname <$fulladdr>
  70. set folder = \"$maildir/$title\"
  71. set header_cache = $cachedir/$title/headers
  72. set message_cachedir = $cachedir/$title/bodies
  73. set mbox_type = Maildir
  74. bind index,pager gg noop
  75. bind index,pager g noop
  76. bind index,pager M noop
  77. bind index,pager C noop
  78. bind index gg first-entry
  79. macro index o \"<shell-escape>mailsync $title<enter>\" \"run mbsync to sync $title\"
  80. unmailboxes *
  81. "
  82. else
  83. mutt_profile="# vim: filetype=neomuttrc
  84. # muttrc file for account $title
  85. set realname = \"$realname\"
  86. set from = \"$fulladdr\"
  87. set sendmail = \"/usr/bin/msmtp -a $title\"
  88. alias me $realname <$fulladdr>
  89. set folder = \"imaps://$fulladdr@$imap:$iport\"
  90. set imap_user = \"$login\"
  91. set header_cache = $cachedir/$title/headers
  92. set message_cachedir = $cachedir/$title/bodies
  93. set imap_pass = \`$pass mutt-wizard-$title\`
  94. set mbox_type = Maildir
  95. set ssl_starttls = yes
  96. set ssl_force_tls = yes
  97. bind index,pager gg noop
  98. bind index,pager g noop
  99. bind index,pager M noop
  100. bind index,pager C noop
  101. bind index gg first-entry
  102. unmailboxes *
  103. "
  104. fi
  105. printf "DONE.\\n"
  106. }
  107. askinfo() { \
  108. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\tEmail: \033[36m"
  109. read -r fulladdr
  110. printf "\033[0m"
  111. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  112. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  113. read -r fulladdr
  114. printf "\033[0m"
  115. done
  116. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  117. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  118. serverinfo="$(grep "^$domain" "$muttshare/domains.csv" 2>/dev/null)"
  119. if [ -z "$serverinfo" ]; then
  120. 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"
  121. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  122. read -r imap
  123. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  124. read -r iport
  125. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  126. read -r smtp
  127. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  128. read -r sport
  129. 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"
  130. else
  131. IFS=, read -r service imap iport smtp sport <<EOF
  132. $serverinfo
  133. EOF
  134. printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically been 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"
  135. case "$service" in
  136. 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" ;;
  137. 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" && ssltype="None" ;;
  138. esac
  139. fi
  140. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  141. read -r realname
  142. 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: "
  143. read -r title
  144. while ! echo "$title" | grep "$namere" >/dev/null || ls "$accdir"/[0-9]"-$title.muttrc" >/dev/null 2>&1; do
  145. printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or - and that you have \033[1mnot\033[0m used before.\\n\tAccount name: \033[36m\t"
  146. read -r title
  147. printf "\033[0m"
  148. done
  149. 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"
  150. read -r login
  151. printf "\033[0m"
  152. [ -z "$login" ] && login="$fulladdr"
  153. [ "$accounttype" = "offline" ] && printf "If you want to limit the number of messages kept offline to a number, enter that number below. If you do not want to limit your mail and would like \`mbsync\` to sync all mail, press enter without typing a number.\\n\t" && read -r maxmes
  154. echo "$maxmes" | grep "[1-9]" >/dev/null || maxmes="0"
  155. getpass
  156. getprofiles
  157. mkdir -p "$muttdir" "$accdir" "$cachedir/$title/bodies" "$HOME/.config/msmtp"
  158. getaccounts
  159. for x in $(seq 1 9); do echo "$accounts" | grep "$x" >/dev/null 2>&1 || { export idnum="$x"; break ;}; done
  160. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  161. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  162. echo "$mutt_profile" > "$accdir/$idnum-$title.muttrc"
  163. echo "$mbsync_profile" >> "$mbsyncrc"
  164. notmuchauto
  165. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created."
  166. ! grep "^source.*mutt-wizard.muttrc" "$muttrc" >/dev/null && echo "source $mwconfig # mw-autogenerated" >> "$muttrc"
  167. ! grep "^source.*.muttrc" "$muttrc" | grep -v "$mwconfig" >/dev/null && echo "source $accdir/$idnum-$title.muttrc # mw-autogenerated" >> "$muttrc"
  168. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$title.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\" # mw-autogenerated" >> "$muttrc"
  169. }
  170. getpass() { while : ; do $pass rm -f "mutt-wizard-$title" >/dev/null 2>&1
  171. $pass insert "mutt-wizard-$title" && break; done ;}
  172. formatShortcut() { \
  173. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # mw-autogenerated"
  174. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
  175. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$accdir/$idnum-$title.muttrc"
  176. done ;}
  177. tryconnect() { mkdir -p "$maildir/$title"
  178. if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1 && [ -n "$mailboxes" ]; then
  179. [ "$accounttype" = "online" ] && sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  180. printf "\033[32mMailboxes detected.\033[0m\\n"
  181. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  182. return 0
  183. else
  184. printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or server settings, or there are other requirements for your account out of the control of mutt-wizard.\\n"
  185. return 1
  186. fi ;}
  187. finalize() { \
  188. boxes="$(find "$maildir/$title/" -mindepth 1 -maxdepth 1 | sed "s/^.*\//=/")"
  189. [ -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
  190. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  191. spoolfile=$(echo "$boxes" | grep -i -m 1 inbox | sed 's/=/+/g')
  192. record=$(echo "$boxes" | grep -i -m 1 sent | sed 's/=/+/g')
  193. postponed=$(echo "$boxes" | grep -i -m 1 draft | sed 's/=/+/g')
  194. trash=$(echo "$boxes" | grep -i -m 1 trash | sed 's/=/+/g')
  195. sed -ibu "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  196. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$idnum-$title.muttrc"
  197. echo "mailboxes =$title ===================== $(echo "$boxes" | sed -e "s/^\|$/\"/g" | tr "\n" " ")" >> "$accdir/$idnum-$title.muttrc"
  198. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  199. sed -ibu "/# mw-autogenerated/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  200. echo "$boxes" | grep -i inbox | head -n 1 | formatShortcut i inbox
  201. echo "$boxes" | grep -i sent | head -n 1 | formatShortcut s sent
  202. echo "$boxes" | grep -i draft | head -n 1 | formatShortcut d drafts
  203. echo "$boxes" | grep -i trash | head -n 1 | formatShortcut t trash
  204. echo "$boxes" | grep -i spam | head -n 1 | formatShortcut S spam
  205. echo "$boxes" | grep -i junk | head -n 1 | formatShortcut j junk
  206. echo "$boxes" | grep -i archive | head -n 1 | formatShortcut a archive
  207. [ "$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"
  208. return 0
  209. }
  210. confirm() { printf "Do you want 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
  211. 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
  212. return 0 ;}
  213. pick() { printf "Select an accounts to %s:\\n" "$1"
  214. list
  215. read -r input
  216. [ -z "$input" ] && return 1
  217. title="$(echo "$accounts" | grep "$input" | awk '{print $2}')"
  218. [ -z "$title" ] && printf "Invalid response." && return 1
  219. return 0 ;}
  220. delete() { sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -rf "$mbsyncrc"bu
  221. rm -rf "${cachedir:?}/${title:?}" "$accdir/"[1-9]"-$title.muttrc"
  222. sed -ibu "/[0-9]-$title.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  223. sed -ibu "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config"; rm -f "$HOME/.config/msmtp/configbu"
  224. }
  225. choosecron() { ! pgrep -x cron\|crond >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  226. if crontab -l | grep mailsync >/dev/null; then
  227. echo "Active mail sync cronjob detected. Do you want to remove it?"
  228. printf "\033[36m\t"
  229. read -r rmyn
  230. printf "\033[0m"
  231. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  232. else
  233. echo "How many minutes between each mail sync?"
  234. printf "\033[36m\t"
  235. read -r minnum
  236. printf "\033[0m"
  237. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  238. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  239. read -r minnum
  240. printf "\033[0m"
  241. done
  242. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; . \$HOME/.profile; $(type mailsync | cut -d' ' -f3)") | crontab - &&
  243. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  244. fi ;}
  245. asktype() { while : ; do
  246. printf "Do you want to keep your mail for this account offline with mbsync? [yes/no]\\n\t"
  247. read -r offnot
  248. case "$offnot" in
  249. [Yy][Ee][Ss]) accounttype="offline" && break ;;
  250. [Nn][Oo]) accounttype="online" && break ;;
  251. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  252. esac; done ;}
  253. purge() { confirm "delete all account data" || exit
  254. rm -rf "$mbsyncrc" "$accdir" "$HOME/.config/msmtp" "$cachedir"
  255. echo "All configs and account settings have been purged."
  256. sed -ibu "/\# mw-autogenerated/d" "$muttrc" ; rm -f "$muttrc"bu
  257. }
  258. notmuchauto() { \
  259. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  260. [ -f "$NOTMUCH_CONFIG" ] && return 0
  261. nmbasic="[database]
  262. path=$maildir
  263. [user]
  264. name=$realname
  265. primary_email=$fulladdr
  266. [new]
  267. tags=unread;inbox;
  268. ignore=
  269. [search]
  270. exclude_tags=deleted;spam;
  271. [maildir]
  272. synchronize_flags=true
  273. [crypto]
  274. gpg_path=gpg"
  275. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  276. case "$1" in
  277. ls) list ;;
  278. add) asktype && askinfo && tryconnect && finalize || delete ;;
  279. pass) pick "change the password of" && getpass ;;
  280. delete) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  281. purge) purge ;;
  282. cron) choosecron ;;
  283. *) cat << EOF
  284. mw: mutt-wizard, auto-configure email accounts for mutt
  285. including downloadable mail with \`isync\`.
  286. Allowed options:
  287. add Add and autoconfigure an email address (9 max.)
  288. ls List configured accounts
  289. delete Pick an account to delete
  290. purge Delete all accounts and settings
  291. cron Enable or disable an autosync via cronjob
  292. all else Print this message
  293. NOTE: Once at least one account is added, you can run
  294. \`mbsync -a\` to begin downloading mail.
  295. EOF
  296. esac