Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

327 rindas
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_CACHE_HOME:-$HOME/.cache}/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() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  45. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  46. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || {
  47. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  48. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  49. exit 1 ;} ;}
  50. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)" ;}
  51. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1 ;}
  52. prepmsmtp() { mkdir -p "${msmtprc%/*}" "${msmtplog%/*}" ; ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null ; envsubst < "$msmtptemp" >> "$msmtprc" ;}
  53. prepmbsync() { mkdir -p "${mbsyncrc%/*}" ; [ -f "$mbsyncrc" ] && echo >> "$mbsyncrc" ; envsubst < "$mbsynctemp" >> "$mbsyncrc" ;}
  54. prepmpop() { mkdir -p "${mpoprc%/*}" ; envsubst < "$mpoptemp" >> "$mpoprc" ;}
  55. prepmutt() { mkdir -p "${muttrc%/*}" "$accdir"
  56. envsubst < "$mutttemp" > "$accdir/$fulladdr.muttrc"
  57. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  58. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >> "$muttrc"
  59. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >> "$muttrc"
  60. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
  61. }
  62. getprofiles() { \
  63. case "$type" in
  64. online)
  65. folder="imaps://$login@$imap:$iport"
  66. extra="$(envsubst < "$onlinetemp")"
  67. ;;
  68. pop) prepmpop ;;
  69. *)
  70. case "$iport" in
  71. 1143) imapssl=None ;;
  72. 143) imapssl=STARTTLS ;;
  73. esac
  74. prepmbsync ;;
  75. esac
  76. prepmsmtp
  77. prepmutt
  78. prepnotmuch
  79. }
  80. parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  81. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  82. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  83. $serverinfo
  84. EOF
  85. imap="${imap:-$imapsugg}"
  86. smtp="${smtp:-$smtpsugg}"
  87. sport="${sport:-$sportsugg}"
  88. iport="${iport:-$iportsugg}"
  89. }
  90. delete() { if [ -z "${fulladdr+x}" ]; then
  91. echo "Select the account you would like to delete (by number):"
  92. list || exit 1
  93. read -r input
  94. match="^\s*$input\s\+"
  95. else
  96. match="\s\+$fulladdr$"
  97. getaccounts
  98. fi
  99. fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")"
  100. [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1
  101. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null ; rm -f "$mbsyncrc"bu
  102. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc"
  103. sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null; rm -f "$muttrc"bu
  104. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null; rm -f "$msmtprc"bu
  105. sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null; rm -f "$mpoprc"bu
  106. pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  107. [ -n "${purge+x}" ] && rm -rf "${maildir:?}/${fulladdr:?}"
  108. }
  109. askinfo() { \
  110. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  111. read -r fulladdr
  112. while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do
  113. echo "$fulladdr is not a valid email address. Please retype the address:"
  114. read -r fulladdr
  115. done
  116. folder="$maildir/$fulladdr"
  117. getaccounts; echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null &&
  118. { echo "$fulladdr has already been added" && exit 1 ;}
  119. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  120. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  121. read -r imap
  122. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  123. read -r smtp
  124. case $sport in
  125. 587) tlsline="# tls_starttls" ;;
  126. esac
  127. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  128. [ -z "$passprefix" ] && passprefix=""
  129. hostname="${fulladdr#*@}"
  130. login="${login:-$fulladdr}"
  131. if [ -n "${password+x}" ]; then
  132. createpass
  133. else
  134. getpass
  135. fi
  136. }
  137. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  138. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  139. case "$(uname)" in
  140. Darwin|*BSD) rm -P "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  141. *) shred -u "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;;
  142. esac
  143. rm -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;}
  144. errorexit() {
  145. echo "Log-on not successful."
  146. case "$imap" in
  147. imap.gmail.com)
  148. echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password.
  149. Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail."
  150. ;;
  151. imap.mail.me.com)
  152. echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default.
  153. Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple."
  154. ;;
  155. esac
  156. exit 1
  157. }
  158. getpass() { while : ; do pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  159. pass insert -f "$passprefix$fulladdr" && break; done ;}
  160. getboxes() { if [ -n "${force+x}" ] ; then
  161. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  162. else
  163. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  164. [ -z "$info" ] && errorexit
  165. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  166. fi
  167. [ "$type" = "pop" ] && mailboxes="INBOX"
  168. for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u; echo 0); do
  169. idnum=$((idnum + 1))
  170. [ "$idnum" -eq "$x" ] || break
  171. done
  172. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' - )"
  173. }
  174. finalize() { echo "$toappend" >> "$accdir/$fulladdr.muttrc"
  175. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  176. echo "$fulladdr (account #$idnum) added successfully."
  177. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  178. return 0 ;}
  179. prepnotmuch() { \
  180. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  181. [ -f "$NOTMUCH_CONFIG" ] && return 0
  182. envsubst < "$notmuchtemp" > "$NOTMUCH_CONFIG" ;}
  183. togglecron() { cron="$(mktemp)"
  184. crontab -l > "$cron"
  185. if grep -q mailsync "$cron"; then
  186. echo "Removing automatic mailsync..."
  187. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  188. else
  189. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  190. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync -q" >> "$cron"
  191. fi &&
  192. crontab "$cron"; rm -f "$cron" ;}
  193. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  194. echo "Running $1 with $action..."
  195. echo "Incompatible options given. Only one action may be specified per run."
  196. exit 1
  197. else
  198. action="$1"
  199. fi; }
  200. mwinfo() { cat << EOF
  201. mw: mutt-wizard, auto-configure email accounts for mutt
  202. including downloadable mail with \`isync\`.
  203. Main actions:
  204. -a your@email.com Add an email address
  205. -l List email addresses configured
  206. -d Remove an already added address
  207. -D your@email.com Force remove account without confirmation
  208. -y your@email.com Sync mail for account by name
  209. -Y Sync mail for all accounts
  210. -t number Toggle automatic mailsync every <number> minutes
  211. -T Toggle automatic mailsync
  212. -r Reorder account numbers
  213. Options allowed with -a:
  214. -u Account login name if not full address
  215. -n "Real name" to be on the email account
  216. -i IMAP/POP server address
  217. -I IMAP/POP server port
  218. -s SMTP server address
  219. -S SMTP server port
  220. -x Password for account (recommended to be in double quotes)
  221. -P Pass Prefix (prefix of the file where password is stored)
  222. -p Add for a POP server instead of IMAP.
  223. -X Delete an account's local email too when deleting.
  224. -o Configure address, but keep mail online.
  225. -f Assume typical English mailboxes without attempting log-on.
  226. NOTE: Once at least one account is added, you can run
  227. \`mbsync -a\` to begin downloading mail.
  228. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  229. EOF
  230. }
  231. reorder() {
  232. tempfile="$(mktemp -u)"
  233. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  234. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  235. # DO NOT reorder rows or rename the accounts in the second column." > "$tempfile"
  236. sed -n "
  237. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  238. " "$muttrc" >> "$tempfile"
  239. ${EDITOR:-vim} "$tempfile" || exit 1
  240. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  241. default="$(sort -n "$tempfile" | head -n 1)"
  242. default="${default#* }"
  243. sed -ibu "
  244. /.* i[0-9] .*.muttrc/d
  245. /^source.*accounts.*.muttrc/d
  246. " "$muttrc" 2>/dev/null; rm -f "$muttrc"bu
  247. awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" }
  248. {
  249. print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\""
  250. }
  251. ' "$tempfile" >> "$muttrc"
  252. }
  253. while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:" o; do case "${o}" in
  254. l) setact list ;;
  255. r) setact reorder1 ;;
  256. d) setact delete ;;
  257. D) setact delete ; fulladdr="$OPTARG" ;;
  258. y) setact sync ; fulladdr="$OPTARG" ;;
  259. Y) setact sync ;;
  260. a) setact add ; fulladdr="$OPTARG" ;;
  261. i) setact add ; imap="$OPTARG" ;;
  262. I) setact add ; iport="$OPTARG" ;;
  263. s) setact add ; smtp="$OPTARG" ;;
  264. S) setact add ; sport="$OPTARG" ;;
  265. u) setact add ; login="$OPTARG" ;;
  266. n) setact add ; realname="$OPTARG" ;;
  267. P) setact add ; passprefix="$OPTARG" ;;
  268. m) setact add ; maxmes="$OPTARG" ;;
  269. o) setact add ; type="online" ;;
  270. p) setact add ; type="pop"; protocol="pop3s" ; iport="${iport:-995}" ;;
  271. f) setact add ; force=True ;;
  272. x) setact add ; password="$OPTARG" ;;
  273. X) setact delete ; purge=True ;;
  274. t) setact toggle ; cronmin="$OPTARG" ;;
  275. T) setact toggle ;;
  276. *) mwinfo ;;
  277. esac done
  278. case "$action" in
  279. list) list ;;
  280. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  281. delete) delete ;;
  282. sync)
  283. echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`."
  284. mailsync $fulladdr
  285. ;;
  286. toggle) togglecron ;;
  287. reorder) reorder ;;
  288. *) mwinfo; exit 1 ;;
  289. esac