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.
 
 
 
 

432 líneas
12 KiB

  1. #!/bin/bash
  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. imapnotify="${XDG_CONFIG_HOME:-$HOME/.config}/imapnotify"
  14. mpoptemp="$muttshare/mpop-temp"
  15. mbsynctemp="$muttshare/mbsync-temp"
  16. mutttemp="$muttshare/mutt-temp"
  17. msmtptemp="$muttshare/msmtp-temp"
  18. onlinetemp="$muttshare/online-temp"
  19. notmuchtemp="$muttshare/notmuch-temp"
  20. imapnotifytemp="$muttshare/imapnotify-temp"
  21. # With the use of templates, it's impossible to use parameter substitution.
  22. # Therefore, some default variables that might be otherwise overwritten are set
  23. # here.
  24. iport="993"
  25. sport="465"
  26. imapssl="IMAPS"
  27. tlsline="tls_starttls off"
  28. maxmes="0"
  29. alias mbsync='mbsync -c "$mbsyncrc"'
  30. # mbsync >=1.4.0 requires "Far/Near" rather than "Master/Slave."
  31. mbver="$(mbsync -v)"
  32. mbver="${mbver#* }"
  33. if [ "${mbver%.*}" >= 1.4 ]; then
  34. master="Far"
  35. slave="Near"
  36. else
  37. master="Master"
  38. slave="Slave"
  39. fi
  40. for x in "/etc/ssl/certs/ca-certificates.crt" \
  41. "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \
  42. "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \
  43. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \
  44. "/usr/local/share/ca-certificates/"; do
  45. [ -f "$x" ] && sslcert="$x" && break
  46. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1; }
  47. checkbasics() {
  48. command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  49. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  50. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || {
  51. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  52. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  53. exit 1
  54. }
  55. }
  56. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)"; }
  57. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1; }
  58. prepmsmtp() {
  59. mkdir -p "${msmtprc%/*}" "${msmtplog%/*}"
  60. ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  61. envsubst <"$msmtptemp" >>"$msmtprc"
  62. }
  63. prepmbsync() {
  64. mkdir -p "${mbsyncrc%/*}"
  65. [ -f "$mbsyncrc" ] && echo >>"$mbsyncrc"
  66. envsubst <"$mbsynctemp" >>"$mbsyncrc"
  67. }
  68. prepmpop() {
  69. mkdir -p "${mpoprc%/*}"
  70. envsubst <"$mpoptemp" >>"$mpoprc"
  71. }
  72. prepimapnotify() {
  73. mkdir -p "${imapnotify%/*}" ; envsubst < "$imapnotifytemp" >> "$imapnotify/$fulladdr.conf"
  74. }
  75. prepmutt() {
  76. mkdir -p "${muttrc%/*}" "$accdir"
  77. envsubst <"$mutttemp" >"$accdir/$fulladdr.muttrc"
  78. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" >"$muttrc"
  79. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >>"$muttrc"
  80. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >>"$muttrc"
  81. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >>"$muttrc"
  82. neomutt -v | grep -q lmdb && ! grep -q "^set header_cache_backend.*lmdb" "$muttrc" && echo "set header_cache_backend = \"lmdb\"" >>"$muttrc"
  83. }
  84. getprofiles() {
  85. safename="$(echo $fulladdr | sed 's/@/_/g')"
  86. case "$type" in
  87. online)
  88. folder="imaps://$login@$imap:$iport"
  89. extra="$(envsubst <"$onlinetemp")"
  90. ;;
  91. pop) prepmpop ;;
  92. *)
  93. case "$iport" in
  94. 1143) imapssl=None ;;
  95. 143) imapssl=STARTTLS ;;
  96. esac
  97. prepmbsync
  98. ;;
  99. esac
  100. prepmsmtp
  101. prepmutt
  102. prepnotmuch
  103. prepimapnotify
  104. }
  105. parsedomains() {
  106. serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  107. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  108. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  109. $serverinfo
  110. EOF
  111. imap="${imap:-$imapsugg}"
  112. smtp="${smtp:-$smtpsugg}"
  113. sport="${sport:-$sportsugg}"
  114. iport="${iport:-$iportsugg}"
  115. }
  116. delete() {
  117. if [ -z "${fulladdr+x}" ]; then
  118. echo "Select the account you would like to delete (by number):"
  119. list || exit 1
  120. read -r input
  121. match="^\s*$input\s\+"
  122. else
  123. match="\s\+$fulladdr$"
  124. getaccounts
  125. fi
  126. fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"
  127. [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1
  128. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null
  129. rm -f "$mbsyncrc"bu
  130. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc"
  131. sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null
  132. rm -f "$muttrc"bu
  133. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null
  134. rm -f "$msmtprc"bu
  135. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null
  136. rm -f "$mpoprc"bu
  137. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  138. [ -n "${purge+x}" ] && safename="$(echo $fulladdr | sed 's/@/_/g')" && rm -rf "${cachedir:?}/${safename:?}" "${maildir:?}/${fulladdr:?}"
  139. }
  140. askinfo() {
  141. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  142. read -r fulladdr
  143. while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
  144. echo "$fulladdr is not a valid email address. Please retype the address:"
  145. read -r fulladdr
  146. done
  147. folder="$maildir/$fulladdr"
  148. getaccounts
  149. echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  150. { echo "$fulladdr has already been added" && exit 1; }
  151. { [ -z "$imap" ] || [ -z "$smtp" ]; } && parsedomains
  152. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  153. read -r imap
  154. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  155. read -r smtp
  156. case $sport in
  157. 587) tlsline="# tls_starttls" ;;
  158. esac
  159. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  160. [ -z "$passprefix" ] && passprefix=""
  161. hostname="${fulladdr#*@}"
  162. login="${login:-$fulladdr}"
  163. if [ -n "${password+x}" ] && [ ! -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr.gpg" ]; then
  164. insertpass
  165. elif [ ! -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr.gpg" ]; then
  166. getpass
  167. fi
  168. }
  169. insertpass() {
  170. printf "%s" "$password" | pass insert -fe "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  171. }
  172. errorexit() {
  173. echo "Log-on not successful."
  174. case "$imap" in
  175. imap.gmail.com)
  176. echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
  177. Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
  178. ;;
  179. imap.mail.me.com)
  180. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  181. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  182. ;;
  183. esac
  184. exit 1
  185. }
  186. getpass() { while :; do
  187. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  188. pass insert -f "$passprefix$fulladdr" && break
  189. done; }
  190. getboxes() {
  191. if [ -n "${force+x}" ]; then
  192. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  193. else
  194. info="$(curl --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. NOTE: Once at least one account is added, you can run
  268. \`mbsync -a\` to begin downloading mail.
  269. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  270. EOF
  271. }
  272. reorder() {
  273. tempfile="$(mktemp -u)"
  274. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  275. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  276. # DO NOT reorder rows or rename the accounts in the second column." >"$tempfile"
  277. sed -n "
  278. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  279. " "$muttrc" >>"$tempfile"
  280. ${EDITOR:-vim} "$tempfile" || exit 1
  281. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  282. default="$(sort -n "$tempfile" | head -n 1)"
  283. default="${default#* }"
  284. sed -ibu "
  285. /.* i[0-9] .*.muttrc/d
  286. /^source.*accounts.*.muttrc/d
  287. " "$muttrc" 2>/dev/null
  288. rm -f "$muttrc"bu
  289. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  290. {
  291. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  292. }
  293. ' "$tempfile" >>"$muttrc"
  294. }
  295. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:" o; do case "${o}" in
  296. l) setact list ;;
  297. r) setact reorder ;;
  298. d) setact delete ;;
  299. D)
  300. setact delete
  301. fulladdr="$OPTARG"
  302. ;;
  303. y)
  304. setact sync
  305. fulladdr="$OPTARG"
  306. ;;
  307. Y) setact sync ;;
  308. a)
  309. setact add
  310. fulladdr="$OPTARG"
  311. ;;
  312. i)
  313. setact add
  314. imap="$OPTARG"
  315. ;;
  316. I)
  317. setact add
  318. iport="$OPTARG"
  319. ;;
  320. s)
  321. setact add
  322. smtp="$OPTARG"
  323. ;;
  324. S)
  325. setact add
  326. sport="$OPTARG"
  327. ;;
  328. u)
  329. setact add
  330. login="$OPTARG"
  331. ;;
  332. n)
  333. setact add
  334. realname="$OPTARG"
  335. ;;
  336. P)
  337. setact add
  338. passprefix="$OPTARG"
  339. ;;
  340. m)
  341. setact add
  342. maxmes="$OPTARG"
  343. ;;
  344. o)
  345. setact add
  346. type="online"
  347. ;;
  348. p)
  349. setact add
  350. type="pop"
  351. protocol="pop3s"
  352. iport="${iport:-995}"
  353. ;;
  354. f)
  355. setact add
  356. force=True
  357. ;;
  358. x)
  359. setact add
  360. password="$OPTARG"
  361. ;;
  362. X)
  363. setact delete
  364. purge=True
  365. ;;
  366. t)
  367. setact toggle
  368. cronmin="$OPTARG"
  369. ;;
  370. T) setact toggle ;;
  371. h) setact info ;;
  372. \?)
  373. echo "See \`$(basename $0) -h\` for possible options and help."
  374. exit 1
  375. ;;
  376. esac done
  377. [ -z "$action" ] && action="info"
  378. case "$action" in
  379. list) list ;;
  380. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  381. delete) delete ;;
  382. sync)
  383. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  384. mailsync $fulladdr
  385. ;;
  386. toggle) togglecron ;;
  387. reorder) reorder ;;
  388. info)
  389. mwinfo
  390. exit 1
  391. ;;
  392. esac