Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

364 рядки
13 KiB

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