You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 4 година
пре 4 година
пре 5 година
пре 5 година
пре 3 година
пре 3 година
пре 3 година
пре 4 година
пре 5 година
пре 3 година
пре 5 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 4 година
пре 5 година
пре 4 година
пре 4 година
пре 4 година
пре 5 година
пре 4 година
пре 5 година
пре 5 година
пре 4 година
пре 5 година
пре 4 година
пре 5 година
пре 4 година
пре 5 година
пре 3 година
пре 5 година
пре 3 година
пре 5 година
пре 3 година
пре 5 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 3 година
пре 3 година
пре 3 година
пре 4 година
пре 4 година
пре 5 година
пре 4 година
пре 3 година
пре 3 година
пре 5 година
пре 5 година
пре 4 година
пре 4 година
пре 3 година
пре 3 година
пре 4 година
пре 4 година
пре 4 година
пре 5 година
пре 3 година
пре 5 година
пре 4 година
пре 5 година
пре 4 година
пре 4 година
пре 3 година
пре 5 година
пре 5 година
пре 5 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 5 година
пре 4 година
пре 5 година
пре 3 година
пре 4 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 4 година
пре 4 година
пре 3 година
пре 3 година
пре 5 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_CACHE_HOME:-$HOME/.cache}/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. master="Master"
  16. slave="Slave"
  17. fi
  18. for x in "/etc/ssl/certs/ca-certificates.crt" \
  19. "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \
  20. "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \
  21. "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \
  22. "/usr/local/share/ca-certificates/"; do
  23. [ -f "$x" ] && sslcert="$x" && break
  24. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  25. checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  26. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  27. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || {
  28. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  29. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
  30. exit 1 ;} ;}
  31. getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)" ;}
  32. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1 ;}
  33. prepmsmtp() { mkdir -p "${msmtprc%/*}" "${msmtplog%/*}"
  34. ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  35. echo "account $fulladdr
  36. host $smtp
  37. port ${sport:-465}
  38. from $fulladdr
  39. user $login
  40. passwordeval \"pass $passprefix$fulladdr\"
  41. auth ${auth:-on}
  42. tls on
  43. tls_trust_file $sslcert
  44. logfile $msmtplog
  45. ${tlsline:-tls_starttls off}
  46. " >> "$msmtprc"
  47. }
  48. prepmbsync() { mkdir -p "${mbsyncrc%/*}"
  49. echo "
  50. IMAPStore $fulladdr-remote
  51. Host $imap
  52. Port ${iport:-993}
  53. User $login
  54. PassCmd \"pass $passprefix$fulladdr\"
  55. AuthMechs LOGIN
  56. SSLType ${imapssl:-IMAPS}
  57. CertificateFile $sslcert
  58. MaildirStore $fulladdr-local
  59. Subfolders Verbatim
  60. Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/
  61. Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
  62. Channel $fulladdr
  63. Expunge Both
  64. ${master:-Far} :$fulladdr-remote:
  65. ${slave:-Near} :$fulladdr-local:
  66. Patterns * !\"[Gmail]/All Mail\"
  67. Create Both
  68. SyncState *
  69. MaxMessages ${maxmes:-0}
  70. ExpireUnread no
  71. # End profile
  72. " >> "$mbsyncrc" ;}
  73. prepmpop() { mkdir -p "${mpoprc%/*}"
  74. echo "
  75. account $fulladdr
  76. tls on
  77. user $login
  78. host $imap
  79. delivery maildir ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
  80. passwordeval pass $passprefix$fulladdr
  81. " >> "$mpoprc" ;}
  82. prepmutt() { mkdir -p "${muttrc%/*}" "$accdir"
  83. echo "# vim: filetype=neomuttrc
  84. # muttrc file for account $fulladdr
  85. set realname = \"$realname\"
  86. set from = \"$fulladdr\"
  87. set sendmail = \"msmtp -a $fulladdr\"
  88. alias me $realname <$fulladdr>
  89. set folder = \"$folder\"
  90. set header_cache = $cachedir/$fulladdr/headers
  91. set message_cachedir = $cachedir/$fulladdr/bodies
  92. set mbox_type = Maildir
  93. set hostname = \"$hostname\"
  94. source $muttshare/switch.muttrc
  95. set spoolfile = "+INBOX"
  96. set postponed = "+Drafts"
  97. set trash = "+Trash"
  98. set record = "+Sent"
  99. $extra
  100. $synccmd
  101. 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\"
  102. macro index \eh \"<pipe-message>$prefix/libexec/gpg-wks-client --receive | msmtp --read-envelope-from --read-recipients -a $fulladdr<enter>\" \"confirm GPG publication\"
  103. " > "$accdir/$fulladdr.muttrc"
  104. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  105. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >> "$muttrc"
  106. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >> "$muttrc"
  107. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
  108. }
  109. getprofiles() { \
  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 $passprefix$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 "$passprefix$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. [ -z "$passprefix" ] && passprefix=""
  181. hostname="${fulladdr#*@}"
  182. login="${login:-$fulladdr}"
  183. if [ -n "${password+x}" ]; then
  184. createpass
  185. else
  186. getpass
  187. fi
  188. }
  189. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  190. "$GPG" -qe $(printf -- " -r %s" $(cat "$PASSWORD_STORE_DIR/.gpg-id")) "$PASSWORD_STORE_DIR/$passprefix$fulladdr"
  191. rm -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr" ;}
  192. getpass() { while : ; do pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1
  193. pass insert -f "$passprefix$fulladdr" && break; done ;}
  194. getboxes() { if [ -n "${force+x}" ] ; then
  195. mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
  196. else
  197. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")"
  198. [ -z "$info" ] && echo "Log-on not successful." && return 1
  199. mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')"
  200. fi
  201. [ "$type" = "pop" ] && mailboxes="INBOX"
  202. for x in $(sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u; echo 0); do
  203. idnum=$((idnum + 1))
  204. [ "$idnum" -eq "$x" ] || break
  205. done
  206. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' - )"
  207. }
  208. finalize() { echo "$toappend" >> "$accdir/$fulladdr.muttrc"
  209. [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  210. echo "$fulladdr (account #$idnum) added successfully."
  211. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  212. return 0 ;}
  213. prepnotmuch() { \
  214. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  215. [ -f "$NOTMUCH_CONFIG" ] && return 0
  216. echo "[database]
  217. path=$maildir
  218. [user]
  219. name=$realname
  220. primary_email=$fulladdr
  221. [new]
  222. tags=unread;inbox;
  223. ignore=.mbsyncstate;.uidvalidity
  224. [search]
  225. exclude_tags=deleted;spam;
  226. [maildir]
  227. synchronize_flags=true
  228. [crypto]
  229. gpg_path=$GPG" > "$NOTMUCH_CONFIG" ;}
  230. togglecron() { cron="$(mktemp)"
  231. crontab -l > "$cron"
  232. if grep -q mailsync "$cron"; then
  233. echo "Removing automatic mailsync..."
  234. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  235. else
  236. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  237. echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync -q" >> "$cron"
  238. fi &&
  239. crontab "$cron"; rm -f "$cron" ;}
  240. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  241. echo "Running $1 with $action..."
  242. echo "Incompatible options given. Only one action may be specified per run."
  243. return 1
  244. else
  245. action="$1"
  246. fi; }
  247. mwinfo() { 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. -y your@email.com Sync mail for account by name
  256. -Y Sync mail for all accounts
  257. -t number Toggle automatic mailsync every <number> minutes
  258. -T Toggle automatic mailsync
  259. -r Reorder account numbers
  260. Options allowed with -a:
  261. -u Account login name if not full address
  262. -n "Real name" to be on the email account
  263. -i IMAP/POP server address
  264. -I IMAP/POP server port
  265. -s SMTP server address
  266. -S SMTP server port
  267. -x Password for account (recommended to be in double quotes)
  268. -P Pass Prefix (prefix of the file where password is stored)
  269. -p Add for a POP server instead of IMAP.
  270. -X Delete an account's local email too when deleting.
  271. -o Configure address, but keep mail online.
  272. -f Assume typical English mailboxes without attempting log-on.
  273. NOTE: Once at least one account is added, you can run
  274. \`mbsync -a\` to begin downloading mail.
  275. To change an account's password, run \`pass edit '$passprefix'your@email.com\`.
  276. EOF
  277. }
  278. reorder() {
  279. tempfile="$(mktemp -u)"
  280. trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT
  281. echo "# Carefully reorder these accounts with the desired numbers in the first column.
  282. # DO NOT reorder rows or rename the accounts in the second column." > "$tempfile"
  283. sed -n "
  284. / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p
  285. " "$muttrc" >> "$tempfile"
  286. ${EDITOR:-vim} "$tempfile" || exit 1
  287. sed -i -e 's/#.*//' -e '/^$/d' "$tempfile"
  288. default="$(sort -n "$tempfile" | head -n 1)"
  289. default="${default#* }"
  290. sed -ibu "
  291. /.* i[0-9] .*.muttrc/d
  292. /^source.*accounts.*.muttrc/d
  293. " "$muttrc" 2>/dev/null; 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:" o; do case "${o}" in
  301. l) setact list || exit 1 ;;
  302. r) setact reorder || exit 1 ;;
  303. d) setact delete || exit 1 ;;
  304. D) setact delete || exit 1 ; fulladdr="$OPTARG" ;;
  305. y) setact sync || exit 1 ; fulladdr="$OPTARG" ;;
  306. Y) setact sync || exit 1 ;;
  307. a) setact add || exit 1 ; fulladdr="$OPTARG" ;;
  308. i) setact add || exit 1 ; imap="$OPTARG" ;;
  309. I) setact add || exit 1 ; iport="$OPTARG" ;;
  310. s) setact add || exit 1 ; smtp="$OPTARG" ;;
  311. S) setact add || exit 1 ; sport="$OPTARG" ;;
  312. u) setact add || exit 1 ; login="$OPTARG" ;;
  313. n) setact add || exit 1 ; realname="$OPTARG" ;;
  314. P) setact add || exit 1 ; passprefix="$OPTARG" ;;
  315. m) setact add || exit 1 ; maxmes="$OPTARG" ;;
  316. o) setact add || exit 1 ; type="online" ;;
  317. p) setact add || exit 1 ; type="pop"; protocol="pop3s" ; iport="${iport:-995}" ;;
  318. f) setact add || exit 1 ; force=True ;;
  319. x) setact add || exit 1 ; password="$OPTARG" ;;
  320. X) setact delete || exit 1 ; purge=True ;;
  321. t) setact toggle || exit 1 ; cronmin="$OPTARG" ;;
  322. T) setact toggle || exit 1 ;;
  323. *) mwinfo; exit 1 ;;
  324. esac done
  325. case "$action" in
  326. list) list ;;
  327. add) checkbasics && askinfo && getboxes && getprofiles && finalize ;;
  328. delete) delete ;;
  329. sync) mailsync $fulladdr ;;
  330. toggle) togglecron ;;
  331. reorder) reorder ;;
  332. *) mwinfo; exit 1 ;;
  333. esac