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.
 
 
 
 

436 linhas
12 KiB

  1. #!/bin/sh
  2. set -a
  3. prefix="/usr/local"
  4. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail"
  5. muttshare="$prefix/share/mutt-wizard"
  6. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  7. muttrc="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/muttrc"
  8. accdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/accounts"
  9. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  10. msmtplog="${XDG_STATE_HOME:-$HOME/.local/state}/msmtp/msmtp.log"
  11. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  12. mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config"
  13. mpoptemp="$muttshare/mpop-temp"
  14. mbsynctemp="$muttshare/mbsync-temp"
  15. mutttemp="$muttshare/mutt-temp"
  16. msmtptemp="$muttshare/msmtp-temp"
  17. onlinetemp="$muttshare/online-temp"
  18. notmuchtemp="$muttshare/notmuch-temp"
  19. # With the use of templates, it's impossible to use parameter substitution.
  20. # Therefore, some default variables that might be otherwise overwritten are set
  21. # here.
  22. iport="993"
  23. sport="465"
  24. imapssl="IMAPS"
  25. tlsline="tls_starttls off"
  26. maxmes="0"
  27. alias mbsync='mbsync -c "$mbsyncrc"'
  28. # mbsync now requires "Far/Near" rather than "Master/Slave", but Ubuntu/Debian
  29. # have the older version.
  30. if command -V apt-get >/dev/null 2>&1; then
  31. master="Master"
  32. slave="Slave"
  33. else
  34. master="Far"
  35. slave="Near"
  36. fi
  37. for x in "/etc/ssl/certs/ca-certificates.crt" \
  38. "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \
  39. "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \
  40. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \
  41. "/usr/local/share/ca-certificates/"; do
  42. [ -f "$x" ] && sslcert="$x" && break
  43. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1; }
  44. checkbasics() {
  45. command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  46. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  47. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || {
  48. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  49. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  50. exit 1
  51. }
  52. }
  53. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)"; }
  54. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1; }
  55. prepmsmtp() {
  56. mkdir -p "${msmtprc%/*}" "${msmtplog%/*}"
  57. ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  58. envsubst <"$msmtptemp" >>"$msmtprc"
  59. }
  60. prepmbsync() {
  61. mkdir -p "${mbsyncrc%/*}"
  62. [ -f "$mbsyncrc" ] && echo >>"$mbsyncrc"
  63. envsubst <"$mbsynctemp" >>"$mbsyncrc"
  64. }
  65. prepmpop() {
  66. mkdir -p "${mpoprc%/*}"
  67. envsubst <"$mpoptemp" >>"$mpoprc"
  68. }
  69. prepmutt() {
  70. mkdir -p "${muttrc%/*}" "$accdir"
  71. envsubst <"$mutttemp" >"$accdir/$fulladdr.muttrc"
  72. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" >"$muttrc"
  73. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >>"$muttrc"
  74. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >>"$muttrc"
  75. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >>"$muttrc"
  76. }
  77. getprofiles() {
  78. safename="$(echo $fulladdr | sed 's/@/_/g')"
  79. case "$type" in
  80. online)
  81. folder="imaps://$login@$imap:$iport"
  82. extra="$(envsubst <"$onlinetemp")"
  83. ;;
  84. pop) prepmpop ;;
  85. *)
  86. case "$iport" in
  87. 1143) imapssl=None ;;
  88. 143) imapssl=STARTTLS ;;
  89. esac
  90. prepmbsync
  91. ;;
  92. esac
  93. prepmsmtp
  94. prepmutt
  95. prepnotmuch
  96. }
  97. parsedomains() {
  98. serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  99. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  100. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  101. $serverinfo
  102. EOF
  103. imap="${imap:-$imapsugg}"
  104. smtp="${smtp:-$smtpsugg}"
  105. sport="${sport:-$sportsugg}"
  106. iport="${iport:-$iportsugg}"
  107. }
  108. delete() {
  109. if [ -z "${fulladdr+x}" ]; then
  110. echo "Select the account you would like to delete (by number):"
  111. list || exit 1
  112. read -r input
  113. match="^\s*$input\s\+"
  114. else
  115. match="\s\+$fulladdr$"
  116. getaccounts
  117. fi
  118. fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"
  119. [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1
  120. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null
  121. rm -f "$mbsyncrc"bu
  122. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc"
  123. sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null
  124. rm -f "$muttrc"bu
  125. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null
  126. rm -f "$msmtprc"bu
  127. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null
  128. rm -f "$mpoprc"bu
  129. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  130. [ -n "${purge+x}" ] && safename="$(echo $fulladdr | sed 's/@/_/g')" && rm -rf "${cachedir:?}/${safename:?}" "${maildir:?}/${fulladdr:?}"
  131. }
  132. askinfo() {
  133. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  134. read -r fulladdr
  135. while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
  136. echo "$fulladdr is not a valid email address. Please retype the address:"
  137. read -r fulladdr
  138. done
  139. folder="$maildir/$fulladdr"
  140. getaccounts
  141. echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  142. { echo "$fulladdr has already been added" && exit 1; }
  143. { [ -z "$imap" ] || [ -z "$smtp" ]; } && parsedomains
  144. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  145. read -r imap
  146. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  147. read -r smtp
  148. case $sport in
  149. 587) tlsline="# tls_starttls" ;;
  150. esac
  151. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  152. [ -z "$passprefix" ] && passprefix=""
  153. hostname="${fulladdr#*@}"
  154. login="${login:-$fulladdr}"
  155. if [ -n "${password+x}" ]; then
  156. createpass
  157. else
  158. getpass
  159. fi
  160. }
  161. createpass() {
  162. echo "$password" >"$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  163. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  164. case "$(uname)" in
  165. Darwin | *BSD) rm -P "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  166. *) shred -u "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  167. esac
  168. rm -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  169. }
  170. errorexit() {
  171. echo "Log-on not successful."
  172. case "$imap" in
  173. imap.gmail.com)
  174. echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
  175. Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
  176. ;;
  177. imap.mail.me.com)
  178. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  179. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  180. ;;
  181. esac
  182. exit 1
  183. }
  184. getpass() { while :; do
  185. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  186. pass insert -f "$passprefix$fulladdr" && break
  187. done; }
  188. getboxes() {
  189. if [ -n "${force+x}" ]; then
  190. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  191. else
  192. [ -n "$sslcert" ] \
  193. && info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")" \
  194. || info="$(curl -k --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  195. [ -z "$info" ] && errorexit
  196. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  197. fi
  198. [ "$type" = "pop" ] && mailboxes="INBOX"
  199. for x in $(
  200. sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u
  201. echo 0
  202. ); do
  203. idnum=$((idnum + 1))
  204. [ "$idnum" -eq "$x" ] || break
  205. done
  206. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' -)"
  207. }
  208. finalize() {
  209. echo "$toappend" >>"$accdir/$fulladdr.muttrc"
  210. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  211. mkdir -p "$cachedir/$safename/bodies"
  212. echo "$fulladdr (account #$idnum) added successfully."
  213. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" >"$HOME/.urlview"
  214. return 0
  215. }
  216. prepnotmuch() {
  217. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  218. [ -f "$NOTMUCH_CONFIG" ] && return 0
  219. envsubst <"$notmuchtemp" >"$NOTMUCH_CONFIG"
  220. }
  221. togglecron() {
  222. cron="$(mktemp)"
  223. crontab -l >"$cron"
  224. if grep -q mailsync "$cron"; then
  225. echo "Removing automatic mailsync..."
  226. sed -ibu /mailsync/d "$cron"
  227. rm -f "$cron"bu
  228. else
  229. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  230. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync" >>"$cron"
  231. fi &&
  232. crontab "$cron"
  233. rm -f "$cron"
  234. }
  235. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  236. echo "Running $1 with $action..."
  237. echo "Incompatible options given. Only one action may be specified per run."
  238. exit 1
  239. else
  240. action="$1"
  241. fi; }
  242. mwinfo() {
  243. cat <<EOF
  244. mw: mutt-wizard, auto-configure email accounts for mutt
  245. including downloadable mail with \`isync\`.
  246. Main actions:
  247. -a your@email.com Add an email address
  248. -l List email addresses configured
  249. -d Remove an already added address
  250. -D your@email.com Force remove account without confirmation
  251. -t number Toggle automatic mailsync every <number> minutes
  252. -T Toggle automatic mailsync
  253. -r Reorder account numbers
  254. Options allowed with -a:
  255. -u Account login name if not full address
  256. -n "Real name" to be on the email account
  257. -i IMAP/POP server address
  258. -I IMAP/POP server port
  259. -s SMTP server address
  260. -S SMTP server port
  261. -x Password for account (recommended to be in double quotes)
  262. -p Add for a POP server instead of IMAP.
  263. -P Pass Prefix (prefix of the file where password is stored)
  264. -X Delete an account's local email too when deleting.
  265. -o Configure address, but keep mail online.
  266. -f Assume typical English mailboxes without attempting log-on.
  267. -c Path to self-signed TLS certificate.
  268. NOTE: Once at least one account is added, you can run
  269. \`mbsync -a\` to begin downloading mail.
  270. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  271. EOF
  272. }
  273. reorder() {
  274. tempfile="$(mktemp -u)"
  275. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  276. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  277. # DO NOT reorder rows or rename the accounts in the second column." >"$tempfile"
  278. sed -n "
  279. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  280. " "$muttrc" >>"$tempfile"
  281. ${EDITOR:-vim} "$tempfile" || exit 1
  282. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  283. default="$(sort -n "$tempfile" | head -n 1)"
  284. default="${default#* }"
  285. sed -ibu "
  286. /.* i[0-9] .*.muttrc/d
  287. /^source.*accounts.*.muttrc/d
  288. " "$muttrc" 2>/dev/null
  289. rm -f "$muttrc"bu
  290. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  291. {
  292. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  293. }
  294. ' "$tempfile" >>"$muttrc"
  295. }
  296. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:c:" o; do case "${o}" in
  297. l) setact list ;;
  298. r) setact reorder1 ;;
  299. d) setact delete ;;
  300. D)
  301. setact delete
  302. fulladdr="$OPTARG"
  303. ;;
  304. y)
  305. setact sync
  306. fulladdr="$OPTARG"
  307. ;;
  308. Y) setact sync ;;
  309. a)
  310. setact add
  311. fulladdr="$OPTARG"
  312. ;;
  313. i)
  314. setact add
  315. imap="$OPTARG"
  316. ;;
  317. I)
  318. setact add
  319. iport="$OPTARG"
  320. ;;
  321. s)
  322. setact add
  323. smtp="$OPTARG"
  324. ;;
  325. S)
  326. setact add
  327. sport="$OPTARG"
  328. ;;
  329. u)
  330. setact add
  331. login="$OPTARG"
  332. ;;
  333. n)
  334. setact add
  335. realname="$OPTARG"
  336. ;;
  337. P)
  338. setact add
  339. passprefix="$OPTARG"
  340. ;;
  341. m)
  342. setact add
  343. maxmes="$OPTARG"
  344. ;;
  345. o)
  346. setact add
  347. type="online"
  348. ;;
  349. p)
  350. setact add
  351. type="pop"
  352. protocol="pop3s"
  353. iport="${iport:-995}"
  354. ;;
  355. f)
  356. setact add
  357. force=True
  358. ;;
  359. x)
  360. setact add
  361. password="$OPTARG"
  362. ;;
  363. c)
  364. setact add
  365. sslcert="$OPTARG"
  366. ;;
  367. X)
  368. setact delete
  369. purge=True
  370. ;;
  371. t)
  372. setact toggle
  373. cronmin="$OPTARG"
  374. ;;
  375. T) setact toggle ;;
  376. h) setact info ;;
  377. \?)
  378. echo "See \`$(basename $0) -h\` for possible options and help."
  379. exit 1
  380. ;;
  381. esac done
  382. [ -z "$action" ] && action="info"
  383. case "$action" in
  384. list) list ;;
  385. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  386. delete) delete ;;
  387. sync)
  388. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  389. mailsync $fulladdr
  390. ;;
  391. toggle) togglecron ;;
  392. reorder) reorder ;;
  393. info)
  394. mwinfo
  395. exit 1
  396. ;;
  397. esac