25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

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