25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

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