選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

424 行
15 KiB

  1. #!/bin/sh
  2. debug() {
  3. echo "fulladdr: $fulladdr"
  4. echo "login: $login"
  5. echo "title: $title"
  6. echo "imap: $imap"
  7. echo "iport: $iport"
  8. echo "smtp: $smtp"
  9. echo "sport: $sport"
  10. echo "proton: $proton"
  11. echo "tls: $tls"
  12. echo "force: $force"
  13. echo "online: $online"
  14. echo "action: $action"
  15. }
  16. command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
  17. [ -z ${PASSWORD_STORE_DIR+x} ] && PASSWORD_STORE_DIR="$HOME/.password-store"
  18. [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
  19. "$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || {
  20. 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"
  21. exit
  22. }
  23. ! command -v mbsync >/dev/null && printf "\`mbsync (isync package)\` must be installed to run mutt-wizard.\\n" && exit
  24. prefix="/usr/local"
  25. pass_prefix="mw-"
  26. muttdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt" # Main mutt config location
  27. accdir="$muttdir/accounts" # Directory for account settings
  28. maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail" # Location of mail storage
  29. namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
  30. emailre=".+@.+\..+" # Regex to confirm valid email address
  31. muttshare="$prefix/share/mutt-wizard"
  32. mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
  33. mwconfig="$muttshare/mutt-wizard.muttrc"
  34. cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
  35. muttrc="$muttdir/muttrc"
  36. msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
  37. msmtplog="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log"
  38. ssltype="IMAPS" # This is later changed to `None` later in the script if using Protonmail
  39. MARKER="# mw-autogenerated"
  40. alias mbsync='mbsync -c "$mbsyncrc"'
  41. 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/"
  42. do
  43. [ -f "$x" ] && sslcert="$x" && break
  44. done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}
  45. getaccounts() { accounts="$(find "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\..*//" | sort -n)" ;}
  46. list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
  47. getprofiles() { \
  48. unset msmtp_header msmtp_profile mutt_profile mbsync_profile
  49. printf "Creating profiles for \`%s\`..." "$title"
  50. msmtp_header="defaults
  51. auth on
  52. tls on
  53. tls_trust_file $sslcert
  54. logfile $msmtplog
  55. "
  56. msmtp_profile="account $title
  57. host $smtp
  58. port ${sport:-587}
  59. from $fulladdr
  60. user $login
  61. passwordeval \"pass $pass_prefix$title\"
  62. $starttlsoff
  63. "
  64. mbsync_profile="IMAPStore $title-remote
  65. Host $imap
  66. Port ${iport:-993}
  67. User $login
  68. PassCmd \"pass $pass_prefix$title\"
  69. AuthMechs LOGIN
  70. SSLType $ssltype
  71. CertificateFile $sslcert
  72. MaildirStore $title-local
  73. Subfolders Verbatim
  74. Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$title/
  75. Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$title/INBOX
  76. Flatten .
  77. Channel $title
  78. Expunge Both
  79. Master :$title-remote:
  80. Slave :$title-local:
  81. Patterns * !\"[Gmail]/All Mail\"
  82. Create Both
  83. SyncState *
  84. MaxMessages $maxmes
  85. ExpireUnread no
  86. # End profile
  87. "
  88. if [ -z "${online+x}" ]; then
  89. mutt_profile="# vim: filetype=neomuttrc
  90. # muttrc file for account $title
  91. set realname = \"$realname\"
  92. set from = \"$fulladdr\"
  93. set sendmail = \"msmtp -a $title\"
  94. alias me $realname <$fulladdr>
  95. set folder = \"$maildir/$title\"
  96. set header_cache = $cachedir/$title/headers
  97. set message_cachedir = $cachedir/$title/bodies
  98. set mbox_type = Maildir
  99. bind index,pager gg noop
  100. bind index,pager g noop
  101. bind index,pager M noop
  102. bind index,pager C noop
  103. bind index gg first-entry
  104. macro index o \"<shell-escape>mbsync -V $title<enter>\" \"run mbsync to sync $title\"
  105. unmailboxes *
  106. "
  107. else
  108. mutt_profile="# vim: filetype=neomuttrc
  109. # muttrc file for account $title
  110. set realname = \"$realname\"
  111. set from = \"$fulladdr\"
  112. set sendmail = \"msmtp -a $title\"
  113. alias me $realname <$fulladdr>
  114. set folder = \"imaps://$login@$imap:${iport:-993}\"
  115. set imap_user = \"$login\"
  116. set header_cache = $cachedir/$title/headers
  117. set message_cachedir = $cachedir/$title/bodies
  118. set imap_pass = \"\`pass $pass_prefix$title\`\"
  119. set mbox_type = Maildir
  120. set ssl_starttls = yes
  121. set ssl_force_tls = yes
  122. bind index,pager gg noop
  123. bind index,pager g noop
  124. bind index,pager M noop
  125. bind index,pager C noop
  126. bind index gg first-entry
  127. unmailboxes *
  128. "
  129. fi
  130. printf "DONE.\\n"
  131. }
  132. parsedomains(){ serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)"
  133. [ -z "$serverinfo" ] &&
  134. serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)"
  135. IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF
  136. $serverinfo
  137. EOF
  138. imap="${imap:-$imapsugg}"
  139. smtp="${smtp:-$smtpsugg}"
  140. sport="${sport:-$sportsugg}"
  141. iport="${iport:-$iportsugg}"
  142. }
  143. askinfo() { \
  144. [ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
  145. read -r fulladdr
  146. while ! echo "$fulladdr" | grep -qE "$emailre"; do
  147. echo "\`$fulladdr\` is not a valid email address. Please retype the address:"
  148. read -r fulladdr
  149. done
  150. # If we don't have either the IMAP or SMTP servers, look for them and ports.
  151. { [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
  152. [ "$sport" = 465 ] && starttlsoff="tls_starttls off"
  153. [ -z "$realname" ] && echo "Give the name you would like to be identified by on the email account:" &&
  154. read -r realname
  155. title="$fulladdr"
  156. #while ! echo "$title" | grep -q "$namere" || ls "$accdir"/[0-9]"-$title.muttrc" >/dev/null 2>&1; do
  157. #printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or - and that you have \033[1mnot\033[0m used before.\\n\tAccount name: \033[36m\t"
  158. #read -r title
  159. #printf "\033[0m"
  160. #done
  161. [ -z "$login" ] && echo "Give the account log-on/username for this address:" && read -r login
  162. login="${login:-$fulladdr}"
  163. #case "$service" in
  164. #gmail.com) printf "\033[31mREMEMBER: Gmail users must enable \"less secure\" (third-party) applications first for the sync to work:\\nhttps://support.google.com/accounts/answer/6010255\\n\033[0m" ;;
  165. #protonmail.ch|protonmail.com|pm.me) printf "\033[31mREMEMBER: Protonmail users must install and configure Protonmail Bridge first for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" && ssltype="None" ;;
  166. #esac
  167. getpass
  168. }
  169. writeinfo() {
  170. # Insert account information into variables.
  171. getprofiles
  172. # Create required directories.
  173. mkdir -p "$muttdir" "$accdir" "$cachedir/$title/bodies" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$maildir/$title"
  174. # Get accounts and find the first missing account number (max. 9).
  175. getaccounts
  176. for x in $(seq 1 9); do echo "$accounts" | grep -q "$x" || { export idnum="$x"; break ;}; done
  177. # Create msmtprc file if not present.
  178. [ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
  179. # Add account msmtp settings.
  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 individual mutt config file for the account.
  184. echo "$mutt_profile" > "$accdir/$idnum-$title.muttrc"
  185. # Create the mbsync config file.
  186. mkdir -p "${mbsyncrc%/*}"
  187. echo "$mbsync_profile" >> "$mbsyncrc"
  188. # Create a notmuch config file if not present already.
  189. notmuchauto
  190. # Create a basic muttrc is not present and source the mutt-wizard files
  191. # and add the shortcuts to the account.
  192. [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created."
  193. ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $mwconfig $MARKER" >> "$muttrc"
  194. ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$mwconfig" && echo "source $accdir/$idnum-$title.muttrc $MARKER" >> "$muttrc"
  195. echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$title.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\" $MARKER" >> "$muttrc"
  196. }
  197. protonfinger() { printf "Getting Protonmail bridge fingerprint...\\n"
  198. fingerprint="$(msmtp --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off | grep SHA256: | sed 's/^.*: //')"
  199. sed -ibu "s/account $title/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$msmtprc" ; rm -f "$msmtprc"bu
  200. }
  201. getpass() { while : ; do pass rm -f "$pass_prefix$title" >/dev/null 2>&1
  202. pass insert "$pass_prefix$title" && break; done ;}
  203. formatShortcut() { toappend="$toappend
  204. macro index,pager g$1 \"<change-folder>=$3<enter>\" \"go to $2\" $MARKER
  205. getpass
  206. macro index,pager M$1 \";<save-message>=$3<enter>\" \"move mail to $2\" $MARKER
  207. getpass
  208. macro index,pager C$1 \";<copy-message>=$3<enter>\" \"copy mail to $2\" $MARKER" >> "$accdir/$idnum-$title.muttrc"
  209. }
  210. setBox() { toappend="$toappend
  211. set $1 = \"+$2\" $MARKER" ;}
  212. getboxes() { [ -n "${force+x}" ] && mailboxes="INBOX
  213. Drafts
  214. Junk
  215. Sent
  216. Archive" && return 0
  217. if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1 && [ -n "$mailboxes" ]; then
  218. [ "$accounttype" = "online" ] && sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -f "$mbsyncrc"bu
  219. printf "\033[32mMailboxes detected.\033[0m\\n"
  220. return 0
  221. else
  222. 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"
  223. return 1
  224. fi ;}
  225. finalize() { \
  226. # Create the required mailbox structure.
  227. echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
  228. printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
  229. sed -ibu "/$MARKER/d" "$accdir/$idnum-$title.muttrc" ; rm -f "$accdir/$idnum-$title.muttrcbu"
  230. toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' )"
  231. for x in $mailboxes; do
  232. case $x in
  233. *[Ii][Nn][Bb][Oo][Xx]*) formatShortcut i inbox "$x"; setBox spoolfile "$x" ;;
  234. *[Ss][Ee][Nn][Tt]*) setBox record "$x"; formatShortcut s sent "$x" ;;
  235. *[Dd][Rr][Aa][Ff][Tt][Ss]*) setBox postponed "$x"; formatShortcut d drafts "$x" ;;
  236. *[Tt][Rr][Aa][Ss][Hh]*|*[Jj][Uu][Nn][Kk]*) formatShortcut t trash "$x"; setBox trash "$x" ;;
  237. *[Aa][Rr][Cc][Hh][Ii][Vv][Ee]*) formatShortcut a archive "$x" ;;
  238. *[Ss][Pp][Aa][Mm]*) formatShortcut S spam "$x" ;;
  239. esac
  240. done
  241. echo "$toappend" >> "$accdir/$idnum-$title.muttrc"
  242. [ -z "${online+x}" ] && printf "All done.\\n\033[33mYou should now be able to run \`\033[32mmbsync %s\033[33m\` to begin to download your mail.\033[0m\\n" "$title"
  243. # Create a urlview config file if non-existent.
  244. command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
  245. return 0
  246. }
  247. 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
  248. 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
  249. return 0 ;}
  250. pick() { printf "Select an accounts to %s:\\n" "$1"
  251. list
  252. read -r input
  253. [ -z "$input" ] && return 1
  254. title="$(echo "$accounts" | grep "$input" | awk '{print $2}')"
  255. [ -z "$title" ] && printf "Invalid response." && return 1
  256. return 0 ;}
  257. delete() { sed -ibu "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" ; rm -rf "$mbsyncrc"bu
  258. rm -rf "${cachedir:?}/${title:?}" "$accdir/"[1-9]"-$title.muttrc"
  259. sed -ibu "/[0-9]-$title.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
  260. sed -ibu "/account $title/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
  261. }
  262. syncwrapper() { mbsync "${1:--a}" &
  263. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  264. wait
  265. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  266. notmuch new
  267. }
  268. purge() { confirm "delete all account data" || exit
  269. rm -rf "$mbsyncrc" "$accdir" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$cachedir"
  270. echo "All configs and account settings have been purged."
  271. sed -ibu "/$MARKER/d" "$muttrc" ; rm -f "$muttrc"bu
  272. }
  273. notmuchauto() { \
  274. [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
  275. [ -f "$NOTMUCH_CONFIG" ] && return 0
  276. nmbasic="[database]
  277. path=$maildir
  278. [user]
  279. name=$realname
  280. primary_email=$fulladdr
  281. [new]
  282. tags=unread;inbox;
  283. ignore=.mbsyncstate;.uidvalidity
  284. [search]
  285. exclude_tags=deleted;spam;
  286. [maildir]
  287. synchronize_flags=true
  288. [crypto]
  289. gpg_path=$GPG"
  290. echo "$nmbasic" > "$NOTMUCH_CONFIG" ;}
  291. trap 'echo -e "\033[0m\n"; exit' INT ABRT
  292. setaction() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then
  293. echo "Running $1 with $action..."
  294. echo "Incompatible options given. Only one action may be specified per run."
  295. return 1
  296. else
  297. action="$1"
  298. fi; }
  299. while getopts "gbpPlhdYD:y:i:I:s:S:u:a:" o; do case "${o}" in
  300. l) setaction list || exit 1 ;;
  301. p) setaction pass || exit 1 ;;
  302. d) setaction delete || exit 1 ;;
  303. D) setaction delete || exit 1 ; title="$OPTARG" ;;
  304. y) setaction sync || exit 1 ; title="$OPTARG" ;;
  305. Y) setaction sync || exit 1 ;;
  306. a) setaction add || exit 1 ; fulladdr="$OPTARG" ;;
  307. i) setaction add || exit 1 ; imap="$OPTARG" ;;
  308. I) setaction add || exit 1 ; iport="$OPTARG" ;;
  309. s) setaction add || exit 1 ; smtp="$OPTARG" ;;
  310. S) setaction add || exit 1 ; sport="$OPTARG" ;;
  311. u) setaction add || exit 1 ; login="$OPTARG" ;;
  312. o) setaction add || exit 1 ; online=True ;;
  313. b) setaction add || exit 1 ; force=True ;;
  314. P) echo "NOTE: Protonmail users must install and configure Protonmail Bridge first for the first sync to work."
  315. proton=True
  316. imap="127.0.0.1"
  317. iport=1143
  318. smtp="127.0.0.1"
  319. sport=1025
  320. setaction add || exit 1
  321. ;;
  322. g) debug ;;
  323. *) cat << EOF
  324. mw: mutt-wizard, auto-configure email accounts for mutt
  325. including downloadable mail with \`isync\`.
  326. Main actions:
  327. -a your@email.com Add an email address
  328. -d Remove an already added address
  329. -D nameofaccount Force remove account without confirmation
  330. -l List email addresses configured
  331. -y nameofaccount Sync mail for account by name
  332. -Y Sync mail for all accounts
  333. Options allowed with -a:
  334. -u Account login name if not full address.
  335. -i IMAP server address
  336. -I IMAP server port
  337. -s SMTP server address
  338. -S SMTP server port
  339. -p Install for a Protonmail account.
  340. -o Configure address, but keep mail online.
  341. -b Assume typical English mailboxes without attempting log-on.
  342. NOTE: Once at least one account is added, you can run
  343. \`mbsync -a\` to begin downloading mail.
  344. EOF
  345. ;;
  346. esac done
  347. case "$action" in
  348. list) list ;;
  349. add) askinfo && writeinfo && getboxes && finalize || delete ;;
  350. pass) pick "change the password of" && getpass ;;
  351. delete) pick delete && confirm "delete the \`$title\` profile" && delete ;;
  352. sync) syncwrapper $title ;;
  353. purge) purge ;;
  354. esac