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.
 
 
 
 

376 líneas
13 KiB

  1. #!/bin/sh
  2. prefix="/usr/local"
  3. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail"
  4. muttshare="$prefix/share/mutt-wizard"
  5. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  6. muttrc="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/muttrc"
  7. accdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/accounts"
  8. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  9. msmtplog="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log"
  10. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  11. mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config"
  12. alias mbsync='mbsync -c "$mbsyncrc"'
  13. # On Ubuntu/Debian, a link is needed since they use an older version.
  14. if command -V apt-get >/dev/null 2>&1; then
  15. ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  16. master="Master"
  17. slave="Slave"
  18. fi
  19. for x in "/etc/ssl/certs/ca-certificates.crt" \
  20. "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \
  21. "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \
  22. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \
  23. "/usr/local/share/ca-certificates/"; do
  24. [ -f "$x" ] && sslcert="$x" && break
  25. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  26. checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  27. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  28. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || {
  29. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  30. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  31. exit 1 ;} ;}
  32. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)" ;}
  33. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1 ;}
  34. prepmsmtp() { echo "account $fulladdr
  35. host $smtp
  36. port ${sport:-465}
  37. from $fulladdr
  38. user $login
  39. passwordeval \"pass $fulladdr\"
  40. auth ${auth:-on}
  41. tls on
  42. tls_trust_file $sslcert
  43. logfile $msmtplog
  44. ${tlsline:-tls_starttls off}
  45. " >> "$msmtprc"
  46. }
  47. prepmbsync() { mkdir -p "${mbsyncrc%/*}"
  48. echo "
  49. IMAPStore $fulladdr-remote
  50. Host $imap
  51. Port ${iport:-993}
  52. User $login
  53. PassCmd \"pass $fulladdr\"
  54. AuthMechs LOGIN
  55. SSLType ${imapssl:-IMAPS}
  56. CertificateFile $sslcert
  57. MaildirStore $fulladdr-local
  58. Subfolders Verbatim
  59. Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/
  60. Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
  61. Channel $fulladdr
  62. Expunge Both
  63. ${master:-Far} :$fulladdr-remote:
  64. ${slave:-Near} :$fulladdr-local:
  65. Patterns * !\"[Gmail]/All Mail\"
  66. Create Both
  67. SyncState *
  68. MaxMessages ${maxmes:-0}
  69. ExpireUnread no
  70. # End profile
  71. " >> "$mbsyncrc" ;}
  72. prepmpop() { mkdir -p "${mpoprc%/*}"
  73. echo "
  74. account $fulladdr
  75. tls on
  76. user $login
  77. host $imap
  78. delivery maildir ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
  79. passwordeval pass $fulladdr
  80. " >> "$mpoprc" ;}
  81. prepmutt() { echo "# vim: filetype=neomuttrc
  82. # muttrc file for account $fulladdr
  83. set realname = \"$realname\"
  84. set from = \"$fulladdr\"
  85. set sendmail = \"msmtp -a $fulladdr\"
  86. alias me $realname <$fulladdr>
  87. set folder = \"$folder\"
  88. set header_cache = $cachedir/$fulladdr/headers
  89. set message_cachedir = $cachedir/$fulladdr/bodies
  90. set mbox_type = Maildir
  91. set hostname = \"$hostname\"
  92. source $muttshare/switch.muttrc
  93. set spoolfile = "+INBOX"
  94. set postponed = "+Drafts"
  95. set trash = "+Trash"
  96. set record = "+Sent"
  97. $extra
  98. $synccmd
  99. macro index \eg \"<enter-command>unset wait_key<enter><shell-escape>gpg --list-secret-keys; printf 'Enter email ID of user to publish: '; read eID; printf 'Enter fingerprint of GPG key to publish: '; read eFGPT; $prefix/libexec/gpg-wks-client --create \\\$eFGPT \\\$eID | msmtp --read-envelope-from --read-recipients -a $fulladdr<enter>\" \"publish GPG key to WKS provider\"
  100. macro index \eh \"<pipe-message>$prefix/libexec/gpg-wks-client --receive | msmtp --read-envelope-from --read-recipients -a $fulladdr<enter>\" \"confirm GPG publication\"
  101. " > "$accdir/$fulladdr.muttrc"
  102. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  103. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >> "$muttrc"
  104. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >> "$muttrc"
  105. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
  106. }
  107. getprofiles() { \
  108. mkdir -p "${muttrc%/*}" "$accdir" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp"
  109. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  110. case "$iport" in
  111. 1143) imapssl=None ;;
  112. 143) imapssl=STARTTLS ;;
  113. esac
  114. case "$type" in
  115. online) folder="imaps://$login@$imap:${iport:-993}"
  116. extra="set imap_user = \"$login\"
  117. set imap_pass = \"\`pass $fulladdr\`\"
  118. set ssl_starttls = yes
  119. set ssl_force_tls = yes"
  120. ;;
  121. pop) synccmd="macro index o \"<shell-escape>mpop $fulladdr<enter>\" \"run mpop to get $fulladdr's mail\""
  122. folder="$maildir/$fulladdr"
  123. prepmpop ;;
  124. *) synccmd="macro index o \"<shell-escape>mw -y $fulladdr<enter>\" \"run mbsync to sync $fulladdr\""
  125. folder="$maildir/$fulladdr"
  126. prepmbsync ;;
  127. esac
  128. prepmsmtp
  129. prepmutt
  130. prepnotmuch # Create a notmuch config file if not present already.
  131. }
  132. parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  133. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  134. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  135. $serverinfo
  136. EOF
  137. imap="${imap:-$imapsugg}"
  138. smtp="${smtp:-$smtpsugg}"
  139. sport="${sport:-$sportsugg}"
  140. iport="${iport:-$iportsugg}"
  141. }
  142. delete() { if [ -z "${fulladdr+x}" ]; then
  143. echo "Select the account you would like to delete (by number):"
  144. list || exit 1
  145. read -r input
  146. match="^\s*$input\s\+"
  147. else
  148. match="\s\+$fulladdr$"
  149. getaccounts
  150. fi
  151. fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"
  152. [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1
  153. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null ; rm -f "$mbsyncrc"bu
  154. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc"
  155. sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null; rm -f "$muttrc"bu
  156. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null; rm -f "$msmtprc"bu
  157. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null; rm -f "$mpoprc"bu
  158. pass rm -f "$fulladdr" >/dev/null 2>&1
  159. [ -n "${purge+x}" ] && rm -rf "${maildir:?}/${fulladdr:?}"
  160. for file in "$msmtprc" "$mbsyncrc" "$mpoprc"; do
  161. sed -ibu 'N;/^\n$/D;P;D;' "$file" 2>/dev/null; rm -f "$file"bu
  162. done
  163. }
  164. askinfo() { \
  165. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  166. read -r fulladdr
  167. while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
  168. echo "$fulladdr is not a valid email address. Please retype the address:"
  169. read -r fulladdr
  170. done
  171. getaccounts; echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  172. { echo "$fulladdr has already been added" && exit 1 ;}
  173. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  174. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  175. read -r imap
  176. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  177. read -r smtp
  178. [ "$sport" = 587 ] && tlsline="# tls_starttls"
  179. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  180. hostname="${fulladdr#*@}"
  181. login="${login:-$fulladdr}"
  182. if [ -n "${password+x}" ]; then
  183. createpass
  184. else
  185. getpass
  186. fi
  187. }
  188. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$fulladdr"
  189. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$fulladdr"
  190. rm -f "$PASSWORD_STORE_DIR/$fulladdr" ;}
  191. getpass() { while : ; do pass rm -f "$fulladdr" >/dev/null 2>&1
  192. pass insert -f "$fulladdr" && break; done ;}
  193. getboxes() { if [ -n "${force+x}" ] ; then
  194. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  195. else
  196. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  197. [ -z "$info" ] && echo "Log-on not successful." && return 1
  198. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  199. fi
  200. [ "$type" = "pop" ] && mailboxes="INBOX"
  201. for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" | sort -u; echo 0); do
  202. idnum=$((idnum + 1))
  203. [ "$idnum" -eq "$x" ] || break
  204. done
  205. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' - )"
  206. }
  207. finalize() { echo "$toappend" >> "$accdir/$fulladdr.muttrc"
  208. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  209. echo "$fulladdr (account #$idnum) added successfully."
  210. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  211. return 0 ;}
  212. prepnotmuch() { \
  213. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  214. [ -f "$NOTMUCH_CONFIG" ] && return 0
  215. echo "[database]
  216. path=$maildir
  217. [user]
  218. name=$realname
  219. primary_email=$fulladdr
  220. [new]
  221. tags=unread;inbox;
  222. ignore=.mbsyncstate;.uidvalidity
  223. [search]
  224. exclude_tags=deleted;spam;
  225. [maildir]
  226. synchronize_flags=true
  227. [crypto]
  228. gpg_path=$GPG" > "$NOTMUCH_CONFIG" ;}
  229. togglecron() { cron="$(mktemp)"
  230. crontab -l > "$cron"
  231. if grep -q mailsync "$cron"; then
  232. echo "Removing automatic mailsync..."
  233. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  234. else
  235. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  236. echo "*/${cronmin-10} * * * * $prefix/bin/mailsync" >> "$cron"
  237. fi &&
  238. crontab "$cron"; rm -f "$cron" ;}
  239. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  240. echo "Running $1 with $action..."
  241. echo "Incompatible options given. Only one action may be specified per run."
  242. return 1
  243. else
  244. action="$1"
  245. fi; }
  246. mwinfo() { cat << EOF
  247. mw: mutt-wizard, auto-configure email accounts for mutt
  248. including downloadable mail with \`isync\`.
  249. Main actions:
  250. -a your@email.com Add an email address
  251. -l List email addresses configured
  252. -d Remove an already added address
  253. -D your@email.com Force remove account without confirmation
  254. -y your@email.com Sync mail for account by name
  255. -Y Sync mail for all accounts
  256. -t number Toggle automatic mailsync every <number> minutes
  257. -T Toggle automatic mailsync
  258. -r Reorder account numbers
  259. Options allowed with -a:
  260. -u Account login name if not full address
  261. -n "Real name" to be on the email account
  262. -i IMAP/POP server address
  263. -I IMAP/POP server port
  264. -s SMTP server address
  265. -S SMTP server port
  266. -x Password for account (recommended to be in double quotes)
  267. -p Install for a Protonmail account.
  268. -P Add for a POP server instead of IMAP.
  269. -X Delete an account's local email too when deleting.
  270. -o Configure address, but keep mail online.
  271. -f Assume typical English mailboxes without attempting log-on.
  272. NOTE: Once at least one account is added, you can run
  273. \`mbsync -a\` to begin downloading mail.
  274. To change an account's password, run \`pass edit your@email.com\`.
  275. EOF
  276. }
  277. reorder(){
  278. tempfile="$(mktemp -u)"
  279. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  280. echo "# Carefully reorder these accounts with the desired numbers." > "$tempfile"
  281. sed -n "
  282. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  283. " "$muttrc" > "$tempfile"
  284. ${EDITOR:-vim} "$tempfile" || exit 1
  285. default="$(sort -n "$tempfile" | head -n 1)"
  286. default="${default#* }"
  287. sed -ibu "/.* i[0-9] .*.muttrc/d" "$muttrc" 2>/dev/null; rm -f "$muttrc"bu
  288. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  289. {
  290. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  291. }
  292. ' "$tempfile" >> "$muttrc"
  293. }
  294. while getopts "rfpPXlhodTYD:y:i:I:s:S:u:a:n:x:m:t:" o; do case "${o}" in
  295. l) setact list || exit 1 ;;
  296. r) setact reorder || exit 1 ;;
  297. d) setact delete || exit 1 ;;
  298. D) setact delete || exit 1 ; fulladdr="$OPTARG" ;;
  299. y) setact sync || exit 1 ; fulladdr="$OPTARG" ;;
  300. Y) setact sync || exit 1 ;;
  301. a) setact add || exit 1 ; fulladdr="$OPTARG" ;;
  302. i) setact add || exit 1 ; imap="$OPTARG" ;;
  303. I) setact add || exit 1 ; iport="$OPTARG" ;;
  304. s) setact add || exit 1 ; smtp="$OPTARG" ;;
  305. S) setact add || exit 1 ; sport="$OPTARG" ;;
  306. u) setact add || exit 1 ; login="$OPTARG" ;;
  307. n) setact add || exit 1 ; realname="$OPTARG" ;;
  308. m) setact add || exit 1 ; maxmes="$OPTARG" ;;
  309. o) setact add || exit 1 ; type="online" ;;
  310. P) setact add || exit 1 ; type="pop"; protocol="pop3s" ; iport="${iport:-995}" ;;
  311. f) setact add || exit 1 ; force=True ;;
  312. x) setact add || exit 1 ; password="$OPTARG" ;;
  313. X) setact delete || exit 1 ; purge=True ;;
  314. t) setact toggle || exit 1 ; cronmin="$OPTARG" ;;
  315. T) setact toggle || exit 1 ;;
  316. p) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work."
  317. protocol="imap"
  318. imap="127.0.0.1"
  319. iport="1143"
  320. smtp="127.0.0.1"
  321. sport="1025"
  322. auth="login"
  323. tlsline="tls_fingerprint $(msmtp --serverinfo --host=$smtp --port=$sport --tls --tls-certcheck=off | sed -n '/SHA256:/ s/^\s*SHA256:\s*// p')"
  324. setact add || exit 1
  325. ;;
  326. *) mwinfo; exit 1 ;;
  327. esac done
  328. case "$action" in
  329. list) list ;;
  330. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  331. delete) delete ;;
  332. sync) mailsync $fulladdr ;;
  333. toggle) togglecron ;;
  334. reorder) reorder ;;
  335. *) mwinfo; exit 1 ;;
  336. esac