Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

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