Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

327 Zeilen
15 KiB

  1. #!/bin/sh
  2. muttdir="$HOME/.config/mutt" # Main mutt config location
  3. accdir="$muttdir/accounts" # Directory for account settings
  4. maildir="$HOME/.local/share/mail" # Location of mail storage
  5. bindir="$HOME/.config/mutt/bin" # Location of scripts run by mutt or the wizard
  6. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  7. emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
  8. mbsyncrc="$HOME/.mbsyncrc"
  9. [ ! -f "$muttdir/domains.csv" ] || [ ! -d "$bindir" ] && printf "Read the README. Be sure to put the repo in the right place before running.\\n" && exit 1
  10. tmpdir="$(mktemp -d)"
  11. sslcert="/etc/ssl/certs/ca-certificates.crt"
  12. getprofiles() { \
  13. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  14. printf "Creating profiles for \`%s\`..." "$title"
  15. msmtp_header="defaults
  16. auth on
  17. tls on
  18. tls_trust_file $sslcert
  19. logfile ~/.config/msmtp/msmtp.log
  20. "
  21. msmtp_profile="
  22. account $title
  23. host $smtp
  24. port $sport
  25. from $login
  26. user $login
  27. passwordeval \"pass mutt-wizard-$title\""
  28. mbsync_profile="IMAPStore $title-remote
  29. Host $imap
  30. Port $iport
  31. User $login
  32. PassCmd \"pass mutt-wizard-$title\"
  33. SSLType IMAPS
  34. CertificateFile /etc/ssl/certs/ca-certificates.crt
  35. MaildirStore $title-local
  36. Subfolders Verbatim
  37. Path ~/.local/share/mail/$title/
  38. Inbox ~/.local/share/mail/$title/INBOX
  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. if [ "$accounttype" = "offline" ]; then
  48. mutt_profile="# vim: filetype=neomuttrc
  49. # muttrc file for account $title
  50. set realname = \"$realname\"
  51. set from = \"$fulladdr\"
  52. set sendmail = \"/usr/bin/msmtp -a $title\"
  53. set folder = \"$maildir/$title\"
  54. set header_cache = $accdir/$title/cache/headers
  55. set message_cachedir = $accdir/$title/cache/bodies
  56. set certificate_file = $accdir/$title/certificates
  57. set imap_pass = \`pass mutt-wizard-$title\`
  58. set smtp_pass = \`pass mutt-wizard-$title\`
  59. alias me $realname <$fulladdr>
  60. set mbox_type = Maildir
  61. set ssl_starttls = yes
  62. set ssl_force_tls = yes
  63. bind index,pager gg noop
  64. bind index,pager g noop
  65. bind index,pager M noop
  66. bind index,pager C noop
  67. bind index gg first-entry
  68. unmailboxes *
  69. set imap_check_subscribed
  70. "
  71. else
  72. mutt_profile="# vim: filetype=neomuttrc
  73. # muttrc file for account $title
  74. set realname = \"$realname\"
  75. set from = \"$fulladdr\"
  76. set sendmail = \"/usr/bin/msmtp -a $title\"
  77. set folder = \"imaps://$fulladdr@$imap:$iport\"
  78. set imap_user = \"$login\"
  79. set header_cache = $accdir/$title/cache/headers
  80. set message_cachedir = $accdir/$title/cache/bodies
  81. set certificate_file = $accdir/$title/certificates
  82. source \"$bindir/getmuttpass $title |\"
  83. alias me $realname <$fulladdr>
  84. set mbox_type = Maildir
  85. set ssl_starttls = yes
  86. set ssl_force_tls = yes
  87. bind index,pager gg noop
  88. bind index,pager g noop
  89. bind index,pager M noop
  90. bind index,pager C noop
  91. bind index gg first-entry
  92. unmailboxes *
  93. "
  94. fi
  95. printf "DONE.\\n"
  96. }
  97. addaccount() { \
  98. printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\\nEmail: \033[36m\t"
  99. read -r fulladdr
  100. printf "\033[0m"
  101. while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
  102. printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
  103. read -r fulladdr
  104. printf "\033[0m"
  105. done
  106. domain="$(echo "$fulladdr" | sed "s/.*@//")"
  107. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
  108. serverinfo="$(grep "$domain" "$muttdir/domains.csv" 2>/dev/null)"
  109. if [ -z "$serverinfo" ]; then
  110. 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"
  111. printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
  112. read -r imap
  113. printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
  114. read -r iport
  115. printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
  116. read -r smtp
  117. printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
  118. read -r sport
  119. 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"
  120. else
  121. IFS=, read -r service imap iport smtp sport <<EOF
  122. $serverinfo
  123. EOF
  124. 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"
  125. case "$service" in
  126. 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" ;;
  127. 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" ;;
  128. esac
  129. fi
  130. cont
  131. printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
  132. read -r realname
  133. 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: "
  134. read -r title
  135. while ! echo "$title" | grep "$namere" >/dev/null; do
  136. 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"
  137. read -r title
  138. printf "\033[0m"
  139. done
  140. printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): \033[36m\t"
  141. read -r login
  142. printf "\033[0m"
  143. [ -z "$login" ] && login="$fulladdr"
  144. grep "i[0-9]" "$muttdir/personal.muttrc" 2>/dev/null | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
  145. seq 1 9 > "$tmpdir/mutt_all"
  146. idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
  147. getpass
  148. getprofiles
  149. mkdir -p "$accdir/$title/cache/bodies" "$HOME/.config/msmtp"
  150. [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
  151. echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
  152. echo "$mutt_profile" > "$accdir/$title.muttrc"
  153. echo "$mbsync_profile" >> "$mbsyncrc"
  154. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
  155. ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
  156. trysync && finalize
  157. [ "$accounttype" = "offline" ] || sed -i "/IMAPStore $title$/,/# End profile/d" "$mbsyncrc"
  158. }
  159. getpass() { pass rm -f "mutt-wizard-$title" 2>/dev/null ; pass insert "mutt-wizard-$title" ;}
  160. formatShortcut() { \
  161. while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
  162. echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
  163. echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
  164. done ;}
  165. trysync() { \
  166. ! 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
  167. printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run mbsync briefly to (1) ensure that login details are functional and (2) allow mbsync to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run mbsync manually to finish the mail sync later.\\n"
  168. cont
  169. mkdir -p "$maildir/$title"
  170. if mailboxes="$(mbsync -l "$title")" >/dev/null 2>&1; then
  171. printf "\033[32mMailboxes detected.\033[0m\\n"
  172. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  173. else
  174. printf "\033[31m\033[31mSync not successful.\033[0m Try running \`mbsync %s\` manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n" "$title"; return 1
  175. fi
  176. }
  177. finalize() { \
  178. boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
  179. [ -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
  180. echo "$boxes" > "$tmpdir/title_boxes"
  181. printf "Setting up the mutt sidebar...\\n"
  182. sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
  183. delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
  184. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  185. oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
  186. oneline="=$title $delim $oneline"
  187. spoolfile=$(grep -i -m 1 "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g')
  188. record=$(grep -i -m 1 "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g')
  189. postponed=$(grep -i -m 1 "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g')
  190. trash=$(grep -i -m 1 "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g')
  191. sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
  192. { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
  193. echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
  194. printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
  195. sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
  196. grep -i -m 1 inbox "$tmpdir/title_boxes" | formatShortcut i inbox "$title"
  197. grep -i -m 1 sent "$tmpdir/title_boxes" | formatShortcut s sent "$title"
  198. grep -i -m 1 draft "$tmpdir/title_boxes" | formatShortcut d drafts "$title"
  199. grep -i -m 1 trash "$tmpdir/title_boxes" | formatShortcut t trash "$title"
  200. grep -i -m 1 spam "$tmpdir/title_boxes" | formatShortcut S spam "$title"
  201. grep -i -m 1 junk "$tmpdir/title_boxes" | formatShortcut j junk "$title"
  202. grep -i -m 1 archive "$tmpdir/title_boxes" | formatShortcut a archive "$title"
  203. printf "All done.\\n"
  204. }
  205. confirm() { printf "Are you \033[31;1mreally\033[0m sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  206. printf "Are you really, really sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  207. return 0 ;}
  208. pick() { numbered="$(ls $accdir/*muttrc | sed "s/.*\///;s/\..*//" | nl)"
  209. [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to choose from.\\n" && return 1
  210. printf "Select an accounts to %s:\\n" "$1"
  211. echo "$numbered"
  212. printf "\033[36m\t"
  213. read -r input
  214. printf "\033[0m"
  215. [ -z "$input" ] && return 1
  216. title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
  217. [ -z "$title" ] && printf "Invalid response." && return 1
  218. return 0 ;}
  219. delete() { sed -i "/IMAPAccount $title$/,/# End profile/d" "$mbsyncrc"
  220. rm -rf "${accdir:?}/${title:?}" "$accdir/$title.muttrc"
  221. sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
  222. sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config"
  223. #confirm "delete the local mail repository as well?" && rm -rf "${maildir:?}/${title:?}"
  224. }
  225. choosecron() { ! pgrep 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/1000/bus; export DISPLAY=:0; . \$HOME/.profile; $muttdir/bin/mailsync -f INBOX") | crontab - &&
  243. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  244. fi
  245. }
  246. cont() { printf "Press Enter to continue.\\n"
  247. stty -echo
  248. read -r null
  249. stty echo ; }
  250. addtype() { \
  251. ! command -v mbsync >/dev/null && printf "You must have the \`mbsync\` command to add an account.\\nIt syncs mail and detects remote mailboxes.\\nYou can typically get it by installing \`isync\`.\\n" && return 1
  252. while : ; do
  253. echo "Do you want to keep your mail for this account offline with mbsync? [yes/no]"
  254. read -r offnot
  255. case "$offnot" in
  256. [Yy]es) accounttype="offline" && break ;;
  257. [Nn]o) accounttype="online" && break ;;
  258. *) echo "I don't know what you mean. Try again or press ctrl-c to quit." ;;
  259. esac
  260. done
  261. addaccount
  262. }
  263. main() { \
  264. while : ; do
  265. [ ! -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
  266. printf "Welcome to:\\n\033[34m"
  267. cat << "EOF"
  268. _ __ ___ _ _| |_| |_ __ _(_)______ _ _ __ __| |
  269. | '_ ` _ \| | | | __| __|___\ \ /\ / / |_ / _` | '__/ _` |
  270. | | | | | | |_| | |_| ||_____\ V V /| |/ / (_| | | | (_| |
  271. |_| |_| |_|\__,_|\__|\__| \_/\_/ |_/___\__,_|_| \__,_|
  272. EOF
  273. printf "\033[0mMade and maintained by Luke Smith <https://lukesmith.xyz>
  274. What would you like \033[32mmutt-wizard\033[0m to do?
  275. 1 Add an email account
  276. 2 Change an account's password
  277. 3 Remove an account
  278. 4 Change GPG key pair used for encryption
  279. 5 Delete all account data
  280. 6 Enable/disable autosync
  281. 0 Exit
  282. Input a number to continue or press ctrl-c.\\n"
  283. printf "\033[36m\t"
  284. read -r choice
  285. printf "\033[0m"
  286. case "$choice" in
  287. 1) addtype ;;
  288. 2) pick "change the password of" && confirm "change the password of" && getpass ;;
  289. 3) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  290. 4) askgpg ;;
  291. 5) confirm "delete all account data" && rm -ri "$mbsyncrc" "$accdir" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged." ;;
  292. 6) choosecron ;;
  293. 0) break ;;
  294. *) printf "Invalid input.\\n"
  295. esac
  296. cont
  297. done
  298. }
  299. main
  300. rm -rf "$tmpdir"