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.
 
 
 
 

392 wiersze
14 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
  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. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  15. emailre=".+@.+\..+" # Regex to confirm valid email address
  16. muttshare="$prefix/share/mutt-wizard"
  17. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  18. mwconfig="$muttshare/mutt-wizard.muttrc"
  19. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  20. muttrc="$muttdir/muttrc"
  21. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  22. msmtplog="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log"
  23. MARKER="# mw-autogenerated"
  24. alias mbsync='mbsync -c "$mbsyncrc"'
  25. 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/"
  26. do
  27. [ -f "$x" ] && sslcert="$x" && break
  28. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  29. getaccounts() { accounts="$(find "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc$//" | sort -n)" ;}
  30. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  31. getprofiles() { \
  32. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  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. $starttlsoff
  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 ${ssltype:-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>mbsync -V $fulladdr<enter>\" \"run mbsync to sync $fulladdr\"
  88. unmailboxes *
  89. "
  90. else
  91. mutt_profile="# vim: filetype=neomuttrc
  92. # muttrc file for account $fulladdr
  93. set realname = \"$realname\"
  94. set from = \"$fulladdr\"
  95. set sendmail = \"msmtp -a $fulladdr\"
  96. alias me $realname <$fulladdr>
  97. set folder = \"imaps://$login@$imap:${iport:-993}\"
  98. set imap_user = \"$login\"
  99. set header_cache = $cachedir/$fulladdr/headers
  100. set message_cachedir = $cachedir/$fulladdr/bodies
  101. set imap_pass = \"\`pass $pass_prefix$fulladdr\`\"
  102. set mbox_type = Maildir
  103. set ssl_starttls = yes
  104. set ssl_force_tls = yes
  105. bind index,pager gg noop
  106. bind index,pager g noop
  107. bind index,pager M noop
  108. bind index,pager C noop
  109. bind index gg first-entry
  110. unmailboxes *
  111. "
  112. fi
  113. printf "DONE.\\n"
  114. }
  115. parsedomains(){ \
  116. # Parses the domains.csv file for server information and sets
  117. # the required variables unless overridden by the user.
  118. serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  119. [ -z "$serverinfo" ] &&
  120. serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  121. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  122. $serverinfo
  123. EOF
  124. imap="${imap:-$imapsugg}"
  125. smtp="${smtp:-$smtpsugg}"
  126. sport="${sport:-$sportsugg}"
  127. iport="${iport:-$iportsugg}"
  128. }
  129. delete() { if [ -z "${fulladdr+x}" ]; then
  130. echo "Select the account your would like to delete (by number):"
  131. list
  132. read -r input
  133. match="^$input:"
  134. else
  135. match=" $fulladdr$"
  136. getaccounts
  137. fi
  138. fulladdr="$(echo "$accounts" | grep "$match" | cut -f2 -d' ')"
  139. [ -z "$fulladdr" ] && echo "Invalid account name given." && return 1
  140. sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" ; rm -rf "$mbsyncrc"bu
  141. rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/"[1-9]"-$fulladdr.muttrc"
  142. sed -ibu "/[0-9]-$fulladdr.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  143. sed -ibu "/account $fulladdr/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  144. }
  145. askinfo() { \
  146. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  147. read -r fulladdr
  148. while ! echo "$fulladdr" | grep -qE "$emailre"; do
  149. echo "\`$fulladdr\` is not a valid email address. Please retype the address:"
  150. read -r fulladdr
  151. done
  152. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  153. [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
  154. read -r imap
  155. [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" &&
  156. read -r smtp
  157. [ "$sport" = 465 ] && starttlsoff="tls_starttls off"
  158. [ -z "$realname" ] && echo "Give the name you would like to be identified by on the email account:" &&
  159. read -r realname
  160. login="${login:-$fulladdr}"
  161. getpass
  162. }
  163. writeinfo() {
  164. # Insert account information into variables.
  165. getprofiles
  166. # Create required directories.
  167. mkdir -p "$muttdir" "$accdir" "$cachedir/$fulladdr/bodies" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$maildir/$fulladdr"
  168. # Get accounts and find the first missing account number (max. 9).
  169. getaccounts
  170. for x in $(seq 1 9); do echo "$accounts" | grep -q "$x" || { export idnum="$x"; break ;}; done
  171. # Create msmtprc file if not present.
  172. [ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
  173. # Add account msmtp settings.
  174. echo "$msmtp_profile" >> "$msmtprc"
  175. # On Ubuntu/Debian, a link is needed since they use an older version.
  176. command -V apt-get >/dev/null 2>&1 && ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null
  177. # Create the individual mutt config file for the account.
  178. echo "$mutt_profile" > "$accdir/$idnum-$fulladdr.muttrc"
  179. # Create the mbsync config file.
  180. mkdir -p "${mbsyncrc%/*}"
  181. echo "$mbsync_profile" >> "$mbsyncrc"
  182. # Create a notmuch config file if not present already.
  183. notmuchauto
  184. # Create a basic muttrc is not present and source the mutt-wizard files
  185. # and add the shortcuts to the account.
  186. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created."
  187. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $mwconfig $MARKER" >> "$muttrc"
  188. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$mwconfig" && echo "source $accdir/$idnum-$fulladdr.muttrc $MARKER" >> "$muttrc"
  189. 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"
  190. }
  191. protonfinger() { printf "Getting Protonmail bridge fingerprint...\\n"
  192. fingerprint="$(msmtp --serverinfo --host=$smtp --port=$sport --tls --tls-certcheck=off | grep SHA256: | sed 's/^.*: //')"
  193. sed -ibu "s/account $fulladdr/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$msmtprc" ; rm -f "$msmtprc"bu ;}
  194. getpass() { while : ; do pass rm -f "$pass_prefix$fulladdr" >/dev/null 2>&1
  195. pass insert "$pass_prefix$fulladdr" && break; done ;}
  196. formatShortcut() { toappend="$toappend
  197. macro index,pager g$1 \"<change-folder>=$3<enter>\" \"go to $2\" $MARKER
  198. macro index,pager M$1 \";<save-message>=$3<enter>\" \"move mail to $2\" $MARKER
  199. macro index,pager C$1 \";<copy-message>=$3<enter>\" \"copy mail to $2\" $MARKER" >> "$accdir/$idnum-$fulladdr.muttrc" ;}
  200. setBox() { toappend="$toappend
  201. set $1 = \"+$2\" $MARKER" ;}
  202. getboxes() { [ -n "${force+x}" ] && mailboxes="INBOX
  203. Drafts
  204. Junk
  205. Trash
  206. Sent
  207. Archive" && return 0
  208. if mailboxes="$(mbsync -l "$fulladdr" | sed 's/\//./')" >/dev/null 2>&1 && [ -n "$mailboxes" ]; then
  209. [ "$accounttype" = "online" ] && sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  210. return 0
  211. else
  212. echo "$mailboxes"
  213. printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or server settings, or there are other requirements for your account out of the control of mutt-wizard.\\n"
  214. return 1
  215. fi ;}
  216. finalize() { echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
  217. sed -ibu "/$MARKER/d" "$accdir/$idnum-$fulladdr.muttrc" ; rm -f "$accdir/$idnum-$fulladdr.muttrcbu"
  218. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' )"
  219. for x in $mailboxes; do
  220. case $x in
  221. *[Ii][Nn][Bb][Oo][Xx]*) formatShortcut i inbox "$x"; setBox spoolfile "$x" ;;
  222. *[Ss][Ee][Nn][Tt]*) setBox record "$x"; formatShortcut s sent "$x" ;;
  223. *[Dd][Rr][Aa][Ff][Tt][Ss]*) setBox postponed "$x"; formatShortcut d drafts "$x" ;;
  224. *[Tt][Rr][Aa][Ss][Hh]*) formatShortcut t trash "$x"; setBox trash "$x" ;;
  225. *[Jj][Uu][Nn][Kk]*) formatShortcut j junk "$x" ;;
  226. *[Aa][Rr][Cc][Hh][Ii][Vv][Ee]*) formatShortcut a archive "$x" ;;
  227. *[Ss][Pp][Aa][Mm]*) formatShortcut S spam "$x" ;;
  228. esac
  229. done
  230. echo "$toappend" >> "$accdir/$idnum-$fulladdr.muttrc"
  231. [ -z "${online+x}" ] && printf "\033[33mYou should now be able to run \`\033[32mmw -y %s\033[33m\` to begin to download your mail.\033[0m\\n" "$fulladdr"
  232. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  233. return 0 ;}
  234. confirm() { printf "Do you want to %s? [yes/N]\\n\t" "$@" && read -r input && ! echo "$input" | grep -qi "^yes$" && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  235. printf "Are you really, really sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -qi "^yes$" && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
  236. return 0 ;}
  237. pick() { [ -n "${fulladdr+x}" ] && return 0
  238. echo "Select the account your would like to $1 (by number):"
  239. list
  240. read -r input
  241. fulladdr="$(echo "$accounts" | grep "^$input:" | cut -f2 -d' ')"
  242. [ -z "$fulladdr" ] && echo "Invalid response." && return 1
  243. return 0 ;}
  244. syncwrapper() { mbsync "${1:--a}" &
  245. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  246. wait
  247. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  248. notmuch new ;}
  249. purge() { confirm "delete all account data" || exit
  250. rm -rf "$mbsyncrc" "$accdir" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$cachedir"
  251. echo "All configs and account settings have been purged."
  252. sed -ibu "/$MARKER/d" "$muttrc" ; rm -f "$muttrc"bu
  253. }
  254. notmuchauto() { \
  255. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  256. [ -f "$NOTMUCH_CONFIG" ] && return 0
  257. nmbasic="[database]
  258. path=$maildir
  259. [user]
  260. name=$realname
  261. primary_email=$fulladdr
  262. [new]
  263. tags=unread;inbox;
  264. ignore=.mbsyncstate;.uidvalidity
  265. [search]
  266. exclude_tags=deleted;spam;
  267. [maildir]
  268. synchronize_flags=true
  269. [crypto]
  270. gpg_path=$GPG"
  271. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  272. trap 'echo -e "\033[0m\n"; exit' INT ABRT
  273. setaction() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  274. echo "Running $1 with $action..."
  275. echo "Incompatible options given. Only one action may be specified per run."
  276. return 1
  277. else
  278. action="$1"
  279. fi; }
  280. while getopts "fplhdYD:y:i:I:s:S:u:a:n:" o; do case "${o}" in
  281. l) setaction list || exit 1 ;;
  282. d) setaction delete || exit 1 ;;
  283. D) setaction delete || exit 1 ; fulladdr="$OPTARG" ;;
  284. y) setaction sync || exit 1 ; fulladdr="$OPTARG" ;;
  285. Y) setaction sync || exit 1 ;;
  286. a) setaction add || exit 1 ; fulladdr="$OPTARG" ;;
  287. i) setaction add || exit 1 ; imap="$OPTARG" ;;
  288. I) setaction add || exit 1 ; iport="$OPTARG" ;;
  289. s) setaction add || exit 1 ; smtp="$OPTARG" ;;
  290. S) setaction add || exit 1 ; sport="$OPTARG" ;;
  291. u) setaction add || exit 1 ; login="$OPTARG" ;;
  292. n) setaction add || exit 1 ; realname="$OPTARG" ;;
  293. m) setaction add || exit 1 ; maxmes="$OPTARG" ;;
  294. o) setaction add || exit 1 ; online=True ;;
  295. f) setaction add || exit 1 ; force=True ;;
  296. p) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work."
  297. proton=True
  298. imap="127.0.0.1"
  299. iport="1143"
  300. smtp="127.0.0.1"
  301. sport="1025"
  302. ssltype="None"
  303. protonfinger
  304. setaction add || exit 1
  305. ;;
  306. *) cat << EOF
  307. mw: mutt-wizard, auto-configure email accounts for mutt
  308. including downloadable mail with \`isync\`.
  309. Main actions:
  310. -a your@email.com Add an email address
  311. -l List email addresses configured
  312. -d Remove an already added address
  313. -D your@email.com Force remove account without confirmation
  314. -y your@email.com Sync mail for account by name
  315. -Y Sync mail for all accounts
  316. Options allowed with -a:
  317. -u Account login name if not full address.
  318. -n "Real name" to be on the email account.
  319. -i IMAP server address
  320. -I IMAP server port
  321. -s SMTP server address
  322. -S SMTP server port
  323. -p Install for a Protonmail account.
  324. -o Configure address, but keep mail online.
  325. -b Assume typical English mailboxes without attempting log-on.
  326. NOTE: Once at least one account is added, you can run
  327. \`mbsync -a\` to begin downloading mail.
  328. To chane an account's password, run \`pass edit ${pass_prefix}your@email.com\`.
  329. EOF
  330. exit 1
  331. ;;
  332. esac done
  333. ! command -v mbsync >/dev/null && [ -z "${force+x}" ] && printf "\`mbsync (isync package)\` must be installed to run mutt-wizard.\\n" && exit
  334. case "$action" in
  335. list) list ;;
  336. add) askinfo && writeinfo && getboxes && finalize || delete ;;
  337. delete) delete $fulladdr ;;
  338. sync) syncwrapper $fulladdr ;;
  339. purge) purge ;;
  340. esac