Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

372 строки
13 KiB

  1. #!/bin/sh
  2. prefix="/usr/local"
  3. pass_prefix="mw-"
  4. muttdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt" # Main mutt config location
  5. accdir="$muttdir/accounts" # Directory for account settings
  6. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail" # Location of mail storage
  7. emailre=".+@.+\..+" # Regex to confirm valid email address
  8. muttshare="$prefix/share/mutt-wizard"
  9. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  10. mwconfig="$muttshare/mutt-wizard.muttrc"
  11. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  12. muttrc="$muttdir/muttrc"
  13. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  14. msmtplog="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log"
  15. marker="# mw-autogenerated"
  16. alias mbsync='mbsync -c "$mbsyncrc"'
  17. for x in "/etc/ssl/certs/ca-certificates.crt" "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" "/etc/ssl/cert.pem" "/usr/local/share/ca-certificates/"; do
  18. [ -f "$x" ] && sslcert="$x" && break
  19. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  20. checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  21. PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
  22. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  23. "$GPG" --list-secret-keys "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" >/dev/null 2>&1 || {
  24. echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
  25. echo "(If you don't already have a GPG key pair, first run \`$GPG --full-gen-key\`.)"
  26. exit 1 ;} ;}
  27. getaccounts() { accounts="$(find -L "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc$//" | sort -n)" ;}
  28. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  29. getprofiles() { \
  30. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  31. case "$iport" in
  32. 1143) imapssl=None ;;
  33. 143) imapssl=STARTTLS ;;
  34. esac
  35. msmtp_header="defaults
  36. auth on
  37. tls on
  38. tls_trust_file $sslcert
  39. logfile $msmtplog
  40. "
  41. msmtp_profile="account $fulladdr
  42. host $smtp
  43. port ${sport:-587}
  44. from $fulladdr
  45. user $login
  46. passwordeval \"pass $pass_prefix$fulladdr\"
  47. $tlsline
  48. "
  49. mbsync_profile="IMAPStore $fulladdr-remote
  50. Host $imap
  51. Port ${iport:-993}
  52. User $login
  53. PassCmd \"pass $pass_prefix$fulladdr\"
  54. AuthMechs LOGIN
  55. SSLType ${imapssl:-IMAPS}
  56. CertificateFile $sslcert
  57. MaildirStore $fulladdr-local
  58. Subfolders Verbatim
  59. Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/
  60. Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/INBOX
  61. Flatten .
  62. Channel $fulladdr
  63. Expunge Both
  64. Master :$fulladdr-remote:
  65. Slave :$fulladdr-local:
  66. Patterns * !\"[Gmail]/All Mail\"
  67. Create Both
  68. SyncState *
  69. MaxMessages ${maxmes:-0}
  70. ExpireUnread no
  71. # End profile
  72. "
  73. if [ -z "${online+x}" ]; then
  74. mutt_profile="# vim: filetype=neomuttrc
  75. # muttrc file for account $fulladdr
  76. set realname = \"$realname\"
  77. set from = \"$fulladdr\"
  78. set sendmail = \"msmtp -a $fulladdr\"
  79. alias me $realname <$fulladdr>
  80. set folder = \"$maildir/$fulladdr\"
  81. set header_cache = $cachedir/$fulladdr/headers
  82. set message_cachedir = $cachedir/$fulladdr/bodies
  83. set mbox_type = Maildir
  84. bind index,pager gg noop
  85. bind index,pager g noop
  86. bind index,pager M noop
  87. bind index,pager C noop
  88. bind index gg first-entry
  89. macro index o \"<shell-escape>mw -y $fulladdr<enter>\" \"run mbsync to sync $fulladdr\"
  90. unmailboxes *
  91. unalternates *
  92. "
  93. else
  94. mutt_profile="# vim: filetype=neomuttrc
  95. # muttrc file for account $fulladdr
  96. set realname = \"$realname\"
  97. set from = \"$fulladdr\"
  98. set sendmail = \"msmtp -a $fulladdr\"
  99. alias me $realname <$fulladdr>
  100. set folder = \"imaps://$login@$imap:${iport:-993}\"
  101. set imap_user = \"$login\"
  102. set header_cache = $cachedir/$fulladdr/headers
  103. set message_cachedir = $cachedir/$fulladdr/bodies
  104. set imap_pass = \"\`pass $pass_prefix$fulladdr\`\"
  105. set mbox_type = Maildir
  106. set ssl_starttls = yes
  107. set ssl_force_tls = yes
  108. bind index,pager gg noop
  109. bind index,pager g noop
  110. bind index,pager M noop
  111. bind index,pager C noop
  112. bind index gg first-entry
  113. unmailboxes *
  114. "
  115. fi
  116. }
  117. parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  118. [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  119. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  120. $serverinfo
  121. EOF
  122. imap="${imap:-$imapsugg}"
  123. smtp="${smtp:-$smtpsugg}"
  124. sport="${sport:-$sportsugg}"
  125. iport="${iport:-$iportsugg}"
  126. }
  127. delete() { if [ -z "${fulladdr+x}" ]; then
  128. echo "Select the account your would like to delete (by number):"
  129. list
  130. read -r input
  131. match="^$input:"
  132. else
  133. match=" $fulladdr$"
  134. getaccounts
  135. fi
  136. fulladdr="$(echo "$accounts" | grep "$match" | cut -f2 -d' ')"
  137. [ -z "$fulladdr" ] && echo "Invalid account name given." && return 1
  138. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  139. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/"[1-9]"-$fulladdr.muttrc"
  140. sed -ibu "/[0-9]-$fulladdr.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  141. sed -ibu "/account $fulladdr/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  142. pass rm -f "$pass_prefix$fulladdr" >/dev/null 2>&1
  143. # Get rid of those multiple newlines because I don't know awk well enough to do it by default lol.
  144. for file in "$msmtprc" "$mbsyncrc"; do
  145. tr '\n' '|' < "$file" | sed "s/||\+/||/g" | tr '|' '\n' >> "$file"bu
  146. mv -f "$file"bu "$file"
  147. done
  148. }
  149. askinfo() { \
  150. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  151. read -r fulladdr
  152. while ! echo "$fulladdr" | grep -qE "$emailre"; do
  153. echo "\`$fulladdr\` is not a valid email address. Please retype the address:"
  154. read -r fulladdr
  155. done
  156. grep -q "Channel\s*$fulladdr$" "$mbsyncrc" &&
  157. { echo "$fulladdr has already been added" && exit 1 ;}
  158. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  159. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  160. read -r imap
  161. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  162. read -r smtp
  163. [ "$sport" = 465 ] && tlsline="tls_starttls off"
  164. [ -z "$realname" ] && realname="${fulladdr%%@*}"
  165. login="${login:-$fulladdr}"
  166. if [ -n "${password+x}" ]; then
  167. createpass
  168. else
  169. getpass
  170. fi
  171. }
  172. createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$pass_prefix$fulladdr"
  173. "$GPG" -qer "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" "$PASSWORD_STORE_DIR/$pass_prefix$fulladdr"
  174. rm -f "$PASSWORD_STORE_DIR/$pass_prefix$fulladdr" ;}
  175. writeinfo() { mkdir -p "$muttdir" "$accdir" "$cachedir/$fulladdr/bodies" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$maildir/$fulladdr"
  176. # Get accounts and find the first missing account number (max. 9).
  177. getaccounts; for x in $(seq 1 9); do echo "$accounts" | grep -q "$x" || { export idnum="$x"; break ;}; done
  178. # Configure msmtprc for sending mail.
  179. [ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
  180. echo "$msmtp_profile" >> "$msmtprc"
  181. # On Ubuntu/Debian, a link is needed since they use an older version.
  182. command -V apt-get >/dev/null 2>&1 && ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  183. # Create the mbsync config file if local account.
  184. [ -z "${online+x}" ] && mkdir -p "${mbsyncrc%/*}" && echo "$mbsync_profile" >> "$mbsyncrc"
  185. # Create a muttrc for viewing mail.
  186. echo "$mutt_profile" > "$accdir/$idnum-$fulladdr.muttrc"
  187. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
  188. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $mwconfig $marker" >> "$muttrc"
  189. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$mwconfig" && echo "source $accdir/$idnum-$fulladdr.muttrc $marker" >> "$muttrc"
  190. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\" $marker" >> "$muttrc"
  191. notmuchauto # Create a notmuch config file if not present already.
  192. }
  193. getpass() { while : ; do pass rm -f "$pass_prefix$fulladdr" >/dev/null 2>&1
  194. pass insert "$pass_prefix$fulladdr" && break; done ;}
  195. formatShortcut() { toappend="$toappend
  196. macro index,pager g$1 \"<change-folder>=$3<enter>\" \"go to $2\" $marker
  197. macro index,pager M$1 \";<save-message>=$3<enter>\" \"move mail to $2\" $marker
  198. macro index,pager C$1 \";<copy-message>=$3<enter>\" \"copy mail to $2\" $marker" >> "$accdir/$idnum-$fulladdr.muttrc" ;}
  199. setBox() { toappend="$toappend
  200. set $1 = \"+$2\" $marker" ;}
  201. getboxes() { [ -n "${force+x}" ] && mailboxes="INBOX
  202. Drafts
  203. Junk
  204. Trash
  205. Sent
  206. Archive" && return 0
  207. info="$(curl --location-trusted -s -m 5 --user "$login:$(pass $pass_prefix$fulladdr)" --url "${protocol:-imaps}://$imap")"
  208. [ -z "$info" ] && echo "Log-on not successful." && return 1
  209. mailboxes="$(echo "$info" | sed "s/.*\" //;s/\"//g" | tr -d ' ')"
  210. }
  211. finalize() { echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  212. sed -ibu "/$marker/d" "$accdir/$idnum-$fulladdr.muttrc" ; rm -f "$accdir/$idnum-$fulladdr.muttrcbu"
  213. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' - )"
  214. for x in $mailboxes; do
  215. case "$x" in
  216. *[Ss][Ee][Nn][Tt]*) setBox record "$x"; formatShortcut s sent "$x" ;;
  217. *[Dd][Rr][Aa][Ff][Tt][Ss]*) setBox postponed "$x"; formatShortcut d drafts "$x" ;;
  218. *[Tt][Rr][Aa][Ss][Hh]*) formatShortcut t trash "$x"; setBox trash "$x" ;;
  219. *[Jj][Uu][Nn][Kk]*) formatShortcut j junk "$x" ;;
  220. *[Aa][Rr][Cc][Hh][Ii][Vv][Ee]*) formatShortcut a archive "$x" ;;
  221. *[Ss][Pp][Aa][Mm]*) formatShortcut S spam "$x" ;;
  222. *[Ii][Nn][Bb][Oo][Xx]*) formatShortcut i inbox "$x"; setBox spoolfile "$x" ;;
  223. esac
  224. done
  225. echo "$toappend" >> "$accdir/$idnum-$fulladdr.muttrc"
  226. [ -z "${online+x}" ] && echo "$fulladdr (account #$idnum) added successfully."
  227. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  228. return 0 ;}
  229. notmuchauto() { \
  230. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  231. [ -f "$NOTMUCH_CONFIG" ] && return 0
  232. nmbasic="[database]
  233. path=$maildir
  234. [user]
  235. name=$realname
  236. primary_email=$fulladdr
  237. [new]
  238. tags=unread;inbox;
  239. ignore=.mbsyncstate;.uidvalidity
  240. [search]
  241. exclude_tags=deleted;spam;
  242. [maildir]
  243. synchronize_flags=true
  244. [crypto]
  245. gpg_path=$GPG"
  246. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  247. togglecron() { cron="$(mktemp)"
  248. crontab -l > "$cron"
  249. if grep -q mailsync "$cron"; then
  250. echo "Removing automatic mailsync..."
  251. sed -ibu /mailsync/d "$cron"; rm -f "$cron"bu
  252. else
  253. echo "Adding automatic mailsync every ${cronmin:-10} minutes..."
  254. echo "*/${cronmin-10} * * * * $prefix/bin/mailsync" >> "$cron"
  255. fi &&
  256. crontab "$cron"; rm -f "$cron" ;}
  257. setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  258. echo "Running $1 with $action..."
  259. echo "Incompatible options given. Only one action may be specified per run."
  260. return 1
  261. else
  262. action="$1"
  263. fi; }
  264. mwinfo() { cat << EOF
  265. mw: mutt-wizard, auto-configure email accounts for mutt
  266. including downloadable mail with \`isync\`.
  267. Main actions:
  268. -a your@email.com Add an email address
  269. -l List email addresses configured
  270. -d Remove an already added address
  271. -D your@email.com Force remove account without confirmation
  272. -y your@email.com Sync mail for account by name
  273. -Y Sync mail for all accounts
  274. -t number Toggle automatic mailsync every <number> minutes
  275. -T Toggle automatic mailsync
  276. Options allowed with -a:
  277. -u Account login name if not full address
  278. -n "Real name" to be on the email account
  279. -i IMAP server address
  280. -I IMAP server port
  281. -s SMTP server address
  282. -S SMTP server port
  283. -x Password for account (recommended to be in double quotes)
  284. -p Install for a Protonmail account.
  285. -o Configure address, but keep mail online.
  286. -f Assume typical English mailboxes without attempting log-on.
  287. NOTE: Once at least one account is added, you can run
  288. \`mbsync -a\` to begin downloading mail.
  289. To change an account's password, run \`pass edit ${pass_prefix}your@email.com\`.
  290. EOF
  291. }
  292. while getopts "fplhodTYD:y:i:I:s:S:u:a:n:x:m:t:" o; do case "${o}" in
  293. l) setact list || exit 1 ;;
  294. d) setact delete || exit 1 ;;
  295. D) setact delete || exit 1 ; fulladdr="$OPTARG" ;;
  296. y) setact sync || exit 1 ; fulladdr="$OPTARG" ;;
  297. Y) setact sync || exit 1 ;;
  298. a) setact add || exit 1 ; fulladdr="$OPTARG" ;;
  299. i) setact add || exit 1 ; imap="$OPTARG" ;;
  300. I) setact add || exit 1 ; iport="$OPTARG" ;;
  301. s) setact add || exit 1 ; smtp="$OPTARG" ;;
  302. S) setact add || exit 1 ; sport="$OPTARG" ;;
  303. u) setact add || exit 1 ; login="$OPTARG" ;;
  304. n) setact add || exit 1 ; realname="$OPTARG" ;;
  305. m) setact add || exit 1 ; maxmes="$OPTARG" ;;
  306. o) setact add || exit 1 ; online=True ;;
  307. f) setact add || exit 1 ; force=True ;;
  308. x) setact add || exit 1 ; password="$OPTARG" ;;
  309. t) setact toggle || exit 1 ; cronmin="$OPTARG" ;;
  310. T) setact toggle || exit 1 ;;
  311. p) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work."
  312. imap="127.0.0.1"
  313. iport="1143"
  314. smtp="127.0.0.1"
  315. sport="1025"
  316. tlsline="tls_fingerprint $(msmtp --serverinfo --host=$smtp --port=$sport --tls --tls-certcheck=off | awk '/SHA256:/ {print $2}')"
  317. setact add || exit 1
  318. ;;
  319. *) mwinfo; exit 1 ;;
  320. esac done
  321. case "$action" in
  322. list) list ;;
  323. add) checkbasics && askinfo && getprofiles && writeinfo && getboxes && finalize || { delete ; exit 1 ;} ;;
  324. delete) delete ;;
  325. sync) mailsync $fulladdr ;;
  326. toggle) togglecron ;;
  327. *) mwinfo; exit 1 ;;
  328. esac