Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

370 wiersze
13 KiB

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