Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

444 linhas
16 KiB

  1. #!/bin/sh
  2. if [ "$#" -gt 1 ]; then
  3. echo "To many arguments. You will be asked."
  4. exit
  5. fi
  6. if [ -z "$prefix" ]; then
  7. case "$(uname)" in
  8. Linux) prefix="/usr" ;;
  9. *) prefix="/usr/local" ;;
  10. esac
  11. fi
  12. command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  13. [ -z "$PASSWORD_STORE_DIR" ] && PASSWORD_STORE_DIR="$HOME/.password-store"
  14. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  15. "$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || {
  16. 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"
  17. exit
  18. }
  19. ! command -v "$prefix/bin/mbsync" >/dev/null && printf "\`mbsync\` must be installed to run mutt-wizard.\\n" && exit
  20. ! command -v "$prefix/bin/msmtp" >/dev/null && printf "\`msmtp\` must be installed.\\n" && exit
  21. ! ( command -v mutt >/dev/null || command -v neomutt >/dev/null ) && printf "\`mutt\` must be installed.\\n" && exit
  22. mwconfigdir=${XDG_CONFIG_HOME:-$HOME/.config}
  23. # Main mutt config location
  24. mwmuttdir="$mwconfigdir/mutt"
  25. # Directory for account settings
  26. mwaccmuttdir="$mwmuttdir/accounts"
  27. # Location of mail storage
  28. mwmaildir="${MAILDIR:-$HOME/mail}"
  29. # Regex to confirm valid email address
  30. mwemailre=".\+@.\+\\..\+"
  31. mwshare="$prefix/share/mutt-wizard"
  32. if [ -n "$XDG_CONFIG_HOME" ]; then
  33. mwmbsyncrc="$mwconfigdir/isync/mbsyncrc"
  34. else
  35. mwmbsyncrc="$HOME/.mbsyncrc"
  36. fi
  37. mwsharerc="$mwshare/mutt-wizard.muttrc"
  38. mwcachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  39. mwmuttrc="$mwmuttdir/muttrc"
  40. mwmsmtprc="$mwconfigdir/msmtp/config"
  41. mwssltype="IMAPS"
  42. mbsyncbin="$prefix/bin/mbsync -c $mwmbsyncrc"
  43. msmtpbin="$prefix/bin/msmtp"
  44. takemwaddrmwtype(){
  45. mwacc="$mwaddr" # let the user always just deal with his email
  46. mwaccmutt="${mwaddr//[.@]/_}" # but mutt would not show it with an @ or .
  47. mwacccachedir=$mwcachedir/${mwaddr//[.@]/_} # @ cannot stay because of mutt, . could
  48. mwaccmaildir="$mwmaildir/$mwaccmutt" # folder name as shown by mutt and opens with gf in vim
  49. mwaccmuttrc="$mwaccmuttdir/$idnum-$mwacc.mwonofftype.$mwtype.muttrc"
  50. mwpass=mutt-wizard-$mwaddr
  51. [ -n "$idnum" ] && mwaccmuttrc="$mwaccmuttdir/$idnum-$mwacc.mwonofftype.$mwtype.muttrc"
  52. }
  53. 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" "/etc/ssl/cert.pem" "/usr/local/share/ca-certificates/"
  54. do
  55. [ -f "$x" ] && sslcert="$x" && break
  56. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  57. getaccounts() {
  58. accounts="$(find "$mwaccmuttdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc//" | sort -n)"
  59. }
  60. mwlist() {
  61. getaccounts && [ -n "$accounts" ] && echo "${accounts//.mwonofftype./ }"
  62. }
  63. mwadd() {
  64. asktype && askinfo && tryconnect && finalize || mwdelete
  65. }
  66. getprofiles() {
  67. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  68. printf "Creating profiles for \`%s\`..." "$mwaddr"
  69. msmtp_header="defaults
  70. auth on
  71. tls on
  72. tls_trust_file $sslcert
  73. logfile ${XDG_LOG_HOME:-$HOME}/msmtp.log
  74. "
  75. msmtp_profile="account $mwacc
  76. host $mwsmtp
  77. port $mwsport
  78. from $mwaddr
  79. user $mwlogin
  80. passwordeval \"pass $mwpass\"
  81. $starttlsoff
  82. "
  83. mbsync_profile="IMAPStore $mwacc-remote
  84. Host $mwimap
  85. Port $mwiport
  86. User $mwlogin
  87. PassCmd \"pass $mwpass\"
  88. SSLType $mwssltype
  89. CertificateFile $sslcert
  90. MaildirStore $mwacc-local
  91. Subfolders Verbatim
  92. Path $mwaccmaildir/
  93. Inbox $mwaccmaildir/INBOX
  94. Flatten .
  95. Channel $mwacc
  96. Expunge Both
  97. Master :$mwacc-remote:
  98. Slave :$mwacc-local:
  99. Patterns * !\"[Gmail]/All Mail\"
  100. Create Both
  101. SyncState *
  102. MaxMessages 0
  103. # End profile
  104. "
  105. if [ "$mwtype" = "offline" ]; then
  106. mutt_profile="# vim: filetype=neomuttrc
  107. # muttrc file for account $mwaddr
  108. set realname = \"$mwname\"
  109. set from = \"$mwaddr\"
  110. set sendmail = \"$msmtpbin -a $mwacc\"
  111. alias me $mwname <$mwaddr>
  112. set folder = \"$mwaccmaildir\"
  113. set mbox_type = Maildir
  114. macro index gm \"<shell-escape>mailsync $mwacc<enter>\" \"sync mail $mwaddr\"
  115. unmailboxes *
  116. "
  117. else
  118. mutt_profile="# vim: filetype=neomuttrc
  119. # muttrc file for account $mwaddr
  120. set realname = \"$mwname\"
  121. set from = \"$mwaddr\"
  122. set sendmail = \"$msmtpbin -a $mwacc\"
  123. alias me $mwname <$mwaddr>
  124. set folder = \"imaps://$mwaddr@$mwimap:$mwiport\"
  125. set header_cache = \"$mwacccachedir\"
  126. set message_cachedir = \$header_cache
  127. set imap_user = \"$mwlogin\"
  128. set imap_pass = \"\`pass $mwpass\`\"
  129. account-hook \$folder 'set imap_user=\"$mwlogin\" imap_pass=\"\`pass $mwpass\`\"'
  130. set mbox_type = Maildir
  131. set ssl_starttls = yes
  132. set ssl_force_tls = yes
  133. unmailboxes *
  134. "
  135. fi
  136. printf "DONE.\\n"
  137. }
  138. askinfo() {
  139. if [ -z "$mwaddr" ]; then
  140. printf "Type the \033[31memail address\033[0m\\n\t\033[36m"
  141. read -r mwaddr
  142. printf "\033[0m"
  143. while ! echo "$mwaddr" | grep "$mwemailre" >/dev/null; do
  144. printf "That is not a valid \033[31memail address\033[0m, please retype\\n\t\033[36m"
  145. read -r mwaddr
  146. printf "\033[0m"
  147. done
  148. fi
  149. mwdomain="$(echo "$mwaddr" | sed "s/.*@//")"
  150. printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$mwdomain"
  151. mwserverinfo="$(grep "^$mwdomain" "$mwshare/domains.csv" 2>/dev/null)"
  152. if [ -z "$mwserverinfo" ]; then
  153. printf "Your email domain is not known to mutt-wizard.\\nType in your settings.\\nUsually you find them by an internet search.\\n"
  154. printf "Type the IMAP server (excluding the port number)\\n\033[36m\t"
  155. read -r mwimap
  156. printf "\033[0mIMAP port number (usually 993)\\n\033[36m\t"
  157. read -r mwiport
  158. printf "\033[0mSMTP server (excluding the port number)\\n\033[36m\t"
  159. read -r mwsmtp
  160. printf "\033[0mSMTP port number (usually 587 or 465)\\n\033[36m\t"
  161. read -r mwsport
  162. printf "\033[0m\\nIf you want, you can copy the line below and add it to the \`domains.csv\` file on Github, for others.\\n\\n%s,%s,%s,%s,%s\\n\\nBut be sure the setting works, first! ;-)\\n" "$mwdomain" "$mwimap" "$mwiport" "$mwsmtp" "$mwsport"
  163. else
  164. IFS=, read -r mwservice mwimap mwiport mwsmtp mwsport <<EOF
  165. $mwserverinfo
  166. EOF
  167. printf "\\n\033[3;33mCongrats!\033[0m Server info is known, so you don't need to look it 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\\n" "$mwimap" "$mwiport" "$mwsmtp" "$mwsport"
  168. case "$mwservice" in
  169. gmail.com) printf "\033[31mGmail: \"less secure\" must be enabled before you continue.\\nDo it now, if you have not done it already:\\nhttps://support.google.com/accounts/answer/6010255\\n\033[0m" ;;
  170. protonmail.ch|protonmail.com|pm.me) printf "\033[31mProtonmail: Users must install and configure Protonmail Bridge for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" && ssltype="None" ;;
  171. esac
  172. [ "$mwsport" = 465 ] && starttlsoff="tls_starttls off"
  173. fi
  174. if [ -z "$mwname" ]; then
  175. printf "Name to associate to email.\\n\t"
  176. read -r mwname
  177. fi
  178. if [ -z "$mwlogin" ]; then
  179. printf "Type your account username if different from your email address.\\n\033[34mFor most accounts you can probably leave this blank.\033[0m\\n\tLogin(?): \033[36m"
  180. read -r mwlogin
  181. printf "\033[0m"
  182. [ -z "$mwlogin" ] && mwlogin="$mwaddr"
  183. fi
  184. # if the user has a pass entry he could provide it via mwpass
  185. if [ -z "$mwpass" ]; then
  186. mwpass=mutt-wizard-$mwaddr
  187. getpass
  188. fi
  189. unset idnum
  190. takemwaddrmwtype
  191. getprofiles
  192. mkdir -p "$mwmuttdir" "$mwaccmuttdir" "$mwconfigdir/msmtp" "${mwmbsyncrc%/*}"
  193. if [ ! -f "$mwmsmtprc" ]; then
  194. echo "$msmtp_header" > "$mwmsmtprc"
  195. else
  196. sed -i "/account $mwacc/,/^\(\s*$\|account\)/d" "$mwmsmtprc"
  197. fi
  198. echo "$msmtp_profile" >> "$mwmsmtprc"
  199. case "$mwservice" in
  200. protonmail.ch|protonmail.com|pm.me) protonfinger || return 1 ;;
  201. esac
  202. [ -f "$mwmbsyncrc" ] && sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
  203. echo "$mbsync_profile" >> "$mwmbsyncrc"
  204. #mwaccmuttrc
  205. mwaccmuttrc="$mwaccmuttdir/$(find "$mwaccmuttdir" -type f | grep -m 1 -o "[0-9]-$mwacc.mwonofftype.$mwtype.muttrc")"
  206. if [[ ! -f "$mwaccmuttrc" ]]; then
  207. # new idnum = first one missing
  208. getaccounts
  209. for x in $(seq 1 9); do echo "$accounts" | grep "$x:" >/dev/null 2>&1 || { export idnum="$x"; break ;}; done
  210. mwaccmuttrc="$mwaccmuttdir/$idnum-$mwacc.mwonofftype.$mwtype.muttrc"
  211. else
  212. idnum=${mwaccmuttrc%%-*}
  213. idnum=${idnum##*/}
  214. fi
  215. echo "$mutt_profile" > "$mwaccmuttrc"
  216. [ ! -f "$mwmuttrc" ] && echo "# vim: filetype=neomuttrc" > "$mwmuttrc" && echo "muttrc created."
  217. ! grep "source.*mutt-wizard.muttrc" "$mwmuttrc" >/dev/null && echo "source $mwsharerc # mw-autogenerated" >> "$mwmuttrc"
  218. if [ "$mwtype" = "offline" ]; then
  219. ! grep "^macro .* gM .*" "$mwmuttrc" >/dev/null && echo "macro index gM '<shell-escape>mailsync -Va<enter>' \"sync all mail\" # mw-autogenerated" >> "$mwmuttrc"
  220. fi
  221. ! grep "^source.*$mwaccmuttrc" "$mwmuttrc" >/dev/null && echo "source $mwaccmuttrc # mw-autogenerated" >> "$mwmuttrc"
  222. return 0
  223. }
  224. protonfinger() {
  225. printf "Getting Protonmail bridge fingerprint...\\n"
  226. fingerprint="$($msmtpbin --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off)" || return 1
  227. sed -i "s/account $mwacc/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$mwmsmtprc"
  228. }
  229. getpass() {
  230. while : ; do pass rm -f "$mwpass" >/dev/null 2>&1
  231. pass insert "$mwpass" && break; done ;}
  232. tryconnect() {
  233. if [ ! -d "$mwaccmaildir" ]; then
  234. mwaccmaildirWasThere="NO" # we need to remove again for "online"
  235. mkdir -p "$mwaccmaildir"
  236. fi
  237. if [ -z "$mailboxes" ]; then
  238. mailboxes="$($mbsyncbin -l $mwacc | sed 's/\//./')" >/dev/null 2>&1
  239. fi
  240. if [ -n "$mailboxes" ]; then
  241. spoolfile=$(echo "$mailboxes" | grep -i -m 1 inbox | sed -ne 's/.*/+\0/p')
  242. [ -z "$spoolfile" ] && return 1
  243. #make directories
  244. printf "\033[32mMailboxes detected.\033[0m\\n"
  245. echo "$mailboxes" | xargs -I {} mkdir -p "$mwaccmaildir/{}/"{cur,new,tmp}
  246. record=$(echo "$mailboxes" | grep -i -m 1 sent | sed -ne 's/.*/+\0/p')
  247. [ -z "$record" ] && mkdir -p "$mwaccmaildir/Sent/"{cur,new,tmp} && record="Sent"
  248. postponed=$(echo "$mailboxes" | grep -i -m 1 draft | sed -ne 's/.*/+\0/p')
  249. [ -z "$postponed" ] && mkdir -p "$mwaccmaildir/Drafts/"{cur,new,tmp} && postponed="Drafts"
  250. trash=$(echo "$mailboxes" | grep -i -m 1 trash | sed -ne 's/.*/+\0/p')
  251. [ -z "$trash" ] && mkdir -p "$mwaccmaildir/Trash/"{cur,new,tmp} && trash="Trash"
  252. return 0
  253. else
  254. 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"
  255. return 1
  256. fi
  257. }
  258. finalize() { # new mwaccmuttrc
  259. boxes="$(find "$mwaccmaildir/" -name cur | sed "s,$mwaccmaildir/,,g;s,/cur,,")"
  260. if [[ "$boxes" =~ ^[[:space:]]*$ ]]; then
  261. 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" "$mwacc"
  262. return
  263. fi
  264. mwaccmutt="${email//[.@]/_}"
  265. muttsync=$(printf '<sync-mailbox><enter-command>source %s<enter><change-folder>!<enter>;<check-stats>' $mwaccmuttrc)
  266. cat >> "$mwaccmuttrc" <<EOF
  267. set spoolfile = "$spoolfile"
  268. set record = "$record"
  269. set postponed = "$postponed"
  270. set trash = "$trash"
  271. folder-hook \$folder '$muttsync'
  272. macro index,pager i$idnum '$muttsync' "switch to $email"
  273. mailboxes =$mwaccmutt ===================== $(echo "$boxes" | sed -e "s/ //g;s/.*/\"=\0\"/g" | tr "\n" " ")
  274. EOF
  275. for eqbox in $boxes; do
  276. box=${eqbox##*/}
  277. box=${box##*.}
  278. boxi=${box::2}
  279. boxi=${boxi,,}
  280. cat >> $mwaccmuttrc <<EOF
  281. macro index,pager i$boxi "<change-folder>=$eqbox<enter>" "go to $box"
  282. macro index,pager M$boxi "<save-message>=$eqbox<enter>" "move mail to $box"
  283. macro index,pager C$boxi "<copy-message>=$eqbox<enter>" "copy mail to $box"
  284. EOF
  285. done
  286. if [ "$mwtype" = "offline" ]; then
  287. notmuchauto
  288. printf "All done.\\n\033[33mYou can now run \`\033[32mmailsync [%s]\033[33m\` to sync your mail.\033[0m\\n" "$mwacc"
  289. else
  290. mkdir -p "$mwacccachedir"
  291. sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
  292. [ "$mwaccmaildirWasThere" = "NO" ] && rm -rf "$mwaccmaildir/"
  293. fi
  294. return 0
  295. }
  296. confirm() {
  297. printf "[y/N]: Do 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
  298. return 0 ;
  299. }
  300. mwpick() {
  301. printf "Select an accounts to %s:\\n" "$1"
  302. mwlist
  303. read -r idnum
  304. [ -z "$idnum" ] && return 1
  305. mwaddr="$(echo "$accounts" | grep "$idnum:" | sed "s/\.mwonofftype\./ /" | awk '{print $2}')"
  306. mwtype="$(echo "$accounts" | grep "$idnum:" | sed "s/\.mwonofftype\./ /" | awk '{print $3}')"
  307. takemwaddrmwtype
  308. [ -z "$mwacc" ] && printf "Invalid response." && return 1
  309. return 0 ;
  310. }
  311. mwdelete() {
  312. sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
  313. rm -rf "$mwacccachedir"
  314. rm -rf "$mwaccmuttdir/"[1-9]"-$mwacc.mwonofftype.$mwtype.muttrc"
  315. sed -i "/[0-9]-$mwacc.mwonofftype.$mwtype.muttrc/d" "$mwmuttrc"
  316. sed -i "/account $mwacc/,/^\(\s*$\|account\)/d" "$mwmsmtprc"
  317. }
  318. mwcron() {
  319. ! pgrep cron >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
  320. if crontab -l | grep mailsync >/dev/null; then
  321. echo "Active mail sync cronjob detected. Do you want to remove it?"
  322. printf "\033[36m\t"
  323. read -r rmyn
  324. printf "\033[0m"
  325. echo "$rmyn" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
  326. else
  327. echo "How many minutes between each mail sync?"
  328. printf "\033[36m\t"
  329. read -r minnum
  330. printf "\033[0m"
  331. while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
  332. printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
  333. read -r minnum
  334. printf "\033[0m"
  335. done
  336. (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; $(type mailsync | cut -d' ' -f3)") | crontab - &&
  337. echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
  338. fi
  339. }
  340. asktype() {
  341. if [ -z "$mwtype" ]; then
  342. while : ; do
  343. printf "[yes/no]: Local mail via mbsync? No: Mutt remotes (slower)\\n\t"
  344. read -r offnot
  345. case "$offnot" in
  346. [Yy][Ee][Ss]) mwtype="offline" && break ;;
  347. [Nn][Oo]) mwtype="online" && break ;;
  348. *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
  349. esac
  350. done
  351. fi
  352. }
  353. mwpurge() {
  354. confirm "delete all account data" || exit
  355. rm -rf "$mwmbsyncrc" "$mwaccmuttdir" "$mwconfigdir/msmtp" "${mwmbsyncrc%/*}" "$mwcachedir"
  356. pgrep cron >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null
  357. sed -i "/\# mw-autogenerated/d" "$mwmuttrc"
  358. echo "All configs and account settings have been purged."
  359. }
  360. notmuchauto() {
  361. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  362. [ -f "$NOTMUCH_CONFIG" ] && return 0
  363. nmbasic="[database]
  364. path=$mwmaildir
  365. [user]
  366. name=$mwname
  367. primary_email=$mwaddr
  368. [new]
  369. tags=unread;inbox;
  370. ignore=
  371. [search]
  372. exclude_tags=deleted;spam;
  373. [maildir]
  374. synchronize_flags=true
  375. [crypto]
  376. gpg_path=$GPG"
  377. mkdir -p "${NOTMUCH_CONFIG%/*}"
  378. echo "$nmbasic" > "$NOTMUCH_CONFIG"
  379. }
  380. trap 'echo -e "\033[0m\n"; exit' STOP INT ABRT KILL
  381. if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
  382. case "$1" in
  383. ls) mwlist ;;
  384. add) mwadd ;;
  385. pass) mwpick "change the password of" && getpass ;;
  386. delete) mwpick delete && confirm "delete the \`$mwacc\` profile" && mwdelete ;;
  387. purge) mwpurge ;;
  388. cron) mwcron ;;
  389. *) cat << EOF
  390. mw: mutt-wizard, auto-configure email accounts for mutt
  391. including downloadable mail with \`isync\`.
  392. Allowed options:
  393. add Add and autoconfigure an email address (9 max.)
  394. ls List configured accounts
  395. delete Pick an account to delete
  396. purge Delete all accounts and settings
  397. cron Enable or disable an autosync via cronjob
  398. all else Print this message
  399. NOTE: Once at least one account is added, you can run
  400. \`mailsync -a\` to begin downloading mail.
  401. EOF
  402. esac
  403. fi