Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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