Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

436 lignes
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. 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.conf"
  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 --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. NOTE: Once at least one account is added, you can run
  272. \`mbsync -a\` to begin downloading mail.
  273. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  274. EOF
  275. }
  276. reorder() {
  277. tempfile="$(mktemp -u)"
  278. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  279. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  280. # DO NOT reorder rows or rename the accounts in the second column." >"$tempfile"
  281. sed -n "
  282. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  283. " "$muttrc" >>"$tempfile"
  284. ${EDITOR:-vim} "$tempfile" || exit 1
  285. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  286. default="$(sort -n "$tempfile" | head -n 1)"
  287. default="${default#* }"
  288. sed -ibu "
  289. /.* i[0-9] .*.muttrc/d
  290. /^source.*accounts.*.muttrc/d
  291. " "$muttrc" 2>/dev/null
  292. rm -f "$muttrc"bu
  293. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  294. {
  295. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  296. }
  297. ' "$tempfile" >>"$muttrc"
  298. }
  299. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:" o; do case "${o}" in
  300. l) setact list ;;
  301. r) setact reorder ;;
  302. d) setact delete ;;
  303. D)
  304. setact delete
  305. fulladdr="$OPTARG"
  306. ;;
  307. y)
  308. setact sync
  309. fulladdr="$OPTARG"
  310. ;;
  311. Y) setact sync ;;
  312. a)
  313. setact add
  314. fulladdr="$OPTARG"
  315. ;;
  316. i)
  317. setact add
  318. imap="$OPTARG"
  319. ;;
  320. I)
  321. setact add
  322. iport="$OPTARG"
  323. ;;
  324. s)
  325. setact add
  326. smtp="$OPTARG"
  327. ;;
  328. S)
  329. setact add
  330. sport="$OPTARG"
  331. ;;
  332. u)
  333. setact add
  334. login="$OPTARG"
  335. ;;
  336. n)
  337. setact add
  338. realname="$OPTARG"
  339. ;;
  340. P)
  341. setact add
  342. passprefix="$OPTARG"
  343. ;;
  344. m)
  345. setact add
  346. maxmes="$OPTARG"
  347. ;;
  348. o)
  349. setact add
  350. type="online"
  351. ;;
  352. p)
  353. setact add
  354. type="pop"
  355. protocol="pop3s"
  356. iport="${iport:-995}"
  357. ;;
  358. f)
  359. setact add
  360. force=True
  361. ;;
  362. x)
  363. setact add
  364. password="$OPTARG"
  365. ;;
  366. X)
  367. setact delete
  368. purge=True
  369. ;;
  370. t)
  371. setact toggle
  372. cronmin="$OPTARG"
  373. ;;
  374. T) setact toggle ;;
  375. h) setact info ;;
  376. \?)
  377. echo "See \`$(basename $0) -h\` for possible options and help."
  378. exit 1
  379. ;;
  380. esac done
  381. [ -z "$action" ] && action="info"
  382. case "$action" in
  383. list) list ;;
  384. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  385. delete) delete ;;
  386. sync)
  387. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  388. mailsync $fulladdr
  389. ;;
  390. toggle) togglecron ;;
  391. reorder) reorder ;;
  392. info)
  393. mwinfo
  394. exit 1
  395. ;;
  396. esac