No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

430 líneas
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}" ] && [ ! -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr.gpg" ]; then
  156. insertpass
  157. elif [ ! -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr.gpg" ]; then
  158. getpass
  159. fi
  160. }
  161. insertpass() {
  162. printf "%s" "$password" | pass insert -fe "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  163. }
  164. errorexit() {
  165. echo "Log-on not successful."
  166. case "$imap" in
  167. imap.gmail.com)
  168. echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
  169. Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
  170. ;;
  171. imap.mail.me.com)
  172. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  173. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  174. ;;
  175. esac
  176. exit 1
  177. }
  178. getpass() { while :; do
  179. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  180. pass insert -f "$passprefix$fulladdr" && break
  181. done; }
  182. getboxes() {
  183. if [ -n "${force+x}" ]; then
  184. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  185. else
  186. [ -n "$sslcert" ] \
  187. && info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")" \
  188. || info="$(curl -k --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  189. [ -z "$info" ] && errorexit
  190. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  191. fi
  192. [ "$type" = "pop" ] && mailboxes="INBOX"
  193. for x in $(
  194. sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u
  195. echo 0
  196. ); do
  197. idnum=$((idnum + 1))
  198. [ "$idnum" -eq "$x" ] || break
  199. done
  200. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' -)"
  201. }
  202. finalize() {
  203. echo "$toappend" >>"$accdir/$fulladdr.muttrc"
  204. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  205. mkdir -p "$cachedir/$safename/bodies"
  206. echo "$fulladdr (account #$idnum) added successfully."
  207. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" >"$HOME/.urlview"
  208. return 0
  209. }
  210. prepnotmuch() {
  211. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  212. [ -f "$NOTMUCH_CONFIG" ] && return 0
  213. envsubst <"$notmuchtemp" >"$NOTMUCH_CONFIG"
  214. }
  215. togglecron() {
  216. cron="$(mktemp)"
  217. crontab -l >"$cron"
  218. if grep -q mailsync "$cron"; then
  219. echo "Removing automatic mailsync..."
  220. sed -ibu /mailsync/d "$cron"
  221. rm -f "$cron"bu
  222. else
  223. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  224. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync" >>"$cron"
  225. fi &&
  226. crontab "$cron"
  227. rm -f "$cron"
  228. }
  229. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  230. echo "Running $1 with $action..."
  231. echo "Incompatible options given. Only one action may be specified per run."
  232. exit 1
  233. else
  234. action="$1"
  235. fi; }
  236. mwinfo() {
  237. cat <<EOF
  238. mw: mutt-wizard, auto-configure email accounts for mutt
  239. including downloadable mail with \`isync\`.
  240. Main actions:
  241. -a your@email.com Add an email address
  242. -l List email addresses configured
  243. -d Remove an already added address
  244. -D your@email.com Force remove account without confirmation
  245. -t number Toggle automatic mailsync every <number> minutes
  246. -T Toggle automatic mailsync
  247. -r Reorder account numbers
  248. Options allowed with -a:
  249. -u Account login name if not full address
  250. -n "Real name" to be on the email account
  251. -i IMAP/POP server address
  252. -I IMAP/POP server port
  253. -s SMTP server address
  254. -S SMTP server port
  255. -x Password for account (recommended to be in double quotes)
  256. -p Add for a POP server instead of IMAP.
  257. -P Pass Prefix (prefix of the file where password is stored)
  258. -X Delete an account's local email too when deleting.
  259. -o Configure address, but keep mail online.
  260. -f Assume typical English mailboxes without attempting log-on.
  261. -c Path to self-signed TLS certificate.
  262. NOTE: Once at least one account is added, you can run
  263. \`mbsync -a\` to begin downloading mail.
  264. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  265. EOF
  266. }
  267. reorder() {
  268. tempfile="$(mktemp -u)"
  269. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  270. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  271. # DO NOT reorder rows or rename the accounts in the second column." >"$tempfile"
  272. sed -n "
  273. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  274. " "$muttrc" >>"$tempfile"
  275. ${EDITOR:-vim} "$tempfile" || exit 1
  276. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  277. default="$(sort -n "$tempfile" | head -n 1)"
  278. default="${default#* }"
  279. sed -ibu "
  280. /.* i[0-9] .*.muttrc/d
  281. /^source.*accounts.*.muttrc/d
  282. " "$muttrc" 2>/dev/null
  283. rm -f "$muttrc"bu
  284. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  285. {
  286. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  287. }
  288. ' "$tempfile" >>"$muttrc"
  289. }
  290. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:c:" o; do case "${o}" in
  291. l) setact list ;;
  292. r) setact reorder ;;
  293. d) setact delete ;;
  294. D)
  295. setact delete
  296. fulladdr="$OPTARG"
  297. ;;
  298. y)
  299. setact sync
  300. fulladdr="$OPTARG"
  301. ;;
  302. Y) setact sync ;;
  303. a)
  304. setact add
  305. fulladdr="$OPTARG"
  306. ;;
  307. i)
  308. setact add
  309. imap="$OPTARG"
  310. ;;
  311. I)
  312. setact add
  313. iport="$OPTARG"
  314. ;;
  315. s)
  316. setact add
  317. smtp="$OPTARG"
  318. ;;
  319. S)
  320. setact add
  321. sport="$OPTARG"
  322. ;;
  323. u)
  324. setact add
  325. login="$OPTARG"
  326. ;;
  327. n)
  328. setact add
  329. realname="$OPTARG"
  330. ;;
  331. P)
  332. setact add
  333. passprefix="$OPTARG"
  334. ;;
  335. m)
  336. setact add
  337. maxmes="$OPTARG"
  338. ;;
  339. o)
  340. setact add
  341. type="online"
  342. ;;
  343. p)
  344. setact add
  345. type="pop"
  346. protocol="pop3s"
  347. iport="${iport:-995}"
  348. ;;
  349. f)
  350. setact add
  351. force=True
  352. ;;
  353. x)
  354. setact add
  355. password="$OPTARG"
  356. ;;
  357. c)
  358. setact add
  359. sslcert="$OPTARG"
  360. ;;
  361. X)
  362. setact delete
  363. purge=True
  364. ;;
  365. t)
  366. setact toggle
  367. cronmin="$OPTARG"
  368. ;;
  369. T) setact toggle ;;
  370. h) setact info ;;
  371. \?)
  372. echo "See \`$(basename $0) -h\` for possible options and help."
  373. exit 1
  374. ;;
  375. esac done
  376. [ -z "$action" ] && action="info"
  377. case "$action" in
  378. list) list ;;
  379. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  380. delete) delete ;;
  381. sync)
  382. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  383. mailsync $fulladdr
  384. ;;
  385. toggle) togglecron ;;
  386. reorder) reorder ;;
  387. info)
  388. mwinfo
  389. exit 1
  390. ;;
  391. esac