You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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