mw (12783B)
1 #!/bin/bash 2 3 set -a 4 5 prefix="/usr/local" 6 maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail" 7 muttshare="$prefix/share/mutt-wizard" 8 cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard" 9 muttrc="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/muttrc" 10 accdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/accounts" 11 msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config" 12 msmtplog="${XDG_STATE_HOME:-$HOME/.local/state}/msmtp/msmtp.log" 13 mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}" 14 mpoprc="${XDG_CONFIG_HOME:-$HOME/.config}/mpop/config" 15 imapnotify="${XDG_CONFIG_HOME:-$HOME/.config}/imapnotify" 16 mpoptemp="$muttshare/mpop-temp" 17 mbsynctemp="$muttshare/mbsync-temp" 18 mutttemp="$muttshare/mutt-temp" 19 msmtptemp="$muttshare/msmtp-temp" 20 onlinetemp="$muttshare/online-temp" 21 notmuchtemp="$muttshare/notmuch-temp" 22 imapnotifytemp="$muttshare/imapnotify-temp" 23 # With the use of templates, it's impossible to use parameter substitution. 24 # Therefore, some default variables that might be otherwise overwritten are set 25 # here. 26 iport="993" 27 sport="465" 28 imapssl="IMAPS" 29 tlsline="tls_starttls off" 30 maxmes="0" 31 32 alias mbsync='mbsync -c "$mbsyncrc"' 33 34 # mbsync >=1.4.0 requires "Far/Near" rather than "Master/Slave." 35 mbver="$(mbsync -v)" 36 mbver="${mbver#* }" 37 mbver="${mbver%.*}" 38 # Comparing two float numbers in bash is a pain in the butt, so we get rid of 39 # dots and turn them into nice integers 40 mbver="${mbver/\./}" 41 if [ "${mbver}" -gt 14 ]; then 42 master="Far" 43 slave="Near" 44 else 45 master="Master" 46 slave="Slave" 47 fi 48 49 for x in "/etc/ssl/certs/ca-certificates.crt" \ 50 "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/cert.pem" \ 51 "/etc/ssl/ca-bundle.pem" "/etc/pki/tls/cacert.pem" \ 52 "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" \ 53 "/usr/local/share/ca-certificates/"; do 54 [ -f "$x" ] && sslcert="$x" && break 55 done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1; } 56 57 checkbasics() { 58 command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2" 59 PASSWORD_STORE_DIR="${PASSWORD_STORE_DIR:-$HOME/.password-store}" 60 [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] || { 61 echo "First run \`pass init <yourgpgemail>\` to set up a password archive." 62 echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)" 63 exit 1 64 } 65 } 66 67 getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "\S*.muttrc" | sed "s|.*/\([0-9]-\)*||;s/\.muttrc$//" | nl)"; } 68 69 list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" || exit 1; } 70 71 prepmsmtp() { 72 mkdir -p "${msmtprc%/*}" "${msmtplog%/*}" 73 ln -s "$msmtprc" "$HOME/.msmtprc" 2>/dev/null 74 envsubst <"$msmtptemp" >>"$msmtprc" 75 } 76 77 prepmbsync() { 78 mkdir -p "${mbsyncrc%/*}" 79 [ -f "$mbsyncrc" ] && echo >>"$mbsyncrc" 80 envsubst <"$mbsynctemp" >>"$mbsyncrc" 81 } 82 83 prepmpop() { 84 mkdir -p "${mpoprc%/*}" 85 envsubst <"$mpoptemp" >>"$mpoprc" 86 } 87 88 prepimapnotify() { 89 mkdir -p "$imapnotify" ; envsubst < "$imapnotifytemp" >> "$imapnotify/$fulladdr.conf" 90 } 91 92 prepmutt() { 93 mkdir -p "${muttrc%/*}" "$accdir" 94 envsubst <"$mutttemp" >"$accdir/$fulladdr.muttrc" 95 [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" >"$muttrc" 96 ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $muttshare/mutt-wizard.muttrc" >>"$muttrc" 97 ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$muttshare/mutt-wizard.muttrc" && echo "source $accdir/$fulladdr.muttrc" >>"$muttrc" 98 echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >>"$muttrc" 99 neomutt -v | grep -q lmdb && ! grep -q "^set header_cache_backend.*lmdb" "$muttrc" && echo "set header_cache_backend = \"lmdb\"" >>"$muttrc" 100 } 101 102 getprofiles() { 103 safename="$(echo $fulladdr | sed 's/@/_/g')" 104 case "$type" in 105 online) 106 folder="imaps://$login@$imap:$iport" 107 extra="$(envsubst <"$onlinetemp")" 108 ;; 109 pop) prepmpop ;; 110 *) 111 case "$iport" in 112 1143) imapssl=None ;; 113 143) imapssl=STARTTLS ;; 114 esac 115 prepmbsync 116 ;; 117 esac 118 prepmsmtp 119 prepmutt 120 prepnotmuch 121 prepimapnotify 122 } 123 124 parsedomains() { 125 serverinfo="$(grep "^${fulladdr#*@}" "$muttshare/domains.csv" 2>/dev/null)" 126 127 [ -z "$serverinfo" ] && serverinfo="$(grep "$(echo "${fulladdr#*@}" | sed "s/\.[^\.]*$/\.\\\*/")" "$muttshare/domains.csv" 2>/dev/null)" 128 129 IFS=, read -r service imapsugg iportsugg smtpsugg sportsugg <<EOF 130 $serverinfo 131 EOF 132 imap="${imap:-$imapsugg}" 133 smtp="${smtp:-$smtpsugg}" 134 sport="${sport:-$sportsugg}" 135 iport="${iport:-$iportsugg}" 136 } 137 138 delete() { 139 if [ -z "${fulladdr+x}" ]; then 140 echo "Select the account you would like to delete (by number):" 141 list || exit 1 142 read -r input 143 match="^\s*$input\s\+" 144 else 145 match="\s\+$fulladdr$" 146 getaccounts 147 fi 148 149 fulladdr="$(echo "$accounts" | grep "$match" | grep -o "\S*@\S*")" 150 151 [ -z "$fulladdr" ] && echo "$fulladdr is not a valid account name." && return 1 152 153 sed -ibu "/IMAPStore $fulladdr-remote$/,/# End profile/d" "$mbsyncrc" 2>/dev/null 154 rm -f "$mbsyncrc"bu 155 rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/$fulladdr.muttrc" "$accdir/"[0-9]-"$fulladdr.muttrc" 156 sed -ibu "/\([0-9]-\)\?$fulladdr.muttrc/d" "$muttrc" 2>/dev/null 157 rm -f "$muttrc"bu 158 sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$msmtprc" 2>/dev/null 159 rm -f "$msmtprc"bu 160 sed -ibu "/account $fulladdr$/,/^\(\s*$\|account\)/d" "$mpoprc" 2>/dev/null 161 rm -f "$mpoprc"bu 162 pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1 163 [ -n "${purge+x}" ] && safename="$(echo $fulladdr | sed 's/@/_/g')" && rm -rf "${cachedir:?}/${safename:?}" "${maildir:?}/${fulladdr:?}" 164 } 165 166 askinfo() { 167 [ -z "$fulladdr" ] && echo "Give the full email address to add:" && 168 read -r fulladdr 169 while ! echo "$fulladdr" | grep -qE "^.+@.+\.[A-Za-z]+$"; do 170 echo "$fulladdr is not a valid email address. Please retype the address:" 171 read -r fulladdr 172 done 173 folder="$maildir/$fulladdr" 174 getaccounts 175 echo "$accounts" | grep -q "\s$fulladdr$" 2>/dev/null && 176 { echo "$fulladdr has already been added" && exit 1; } 177 { [ -z "$imap" ] || [ -z "$smtp" ]; } && parsedomains 178 [ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" && 179 read -r imap 180 [ -z "$smtp" ] && echo "Give your email server's SMTP address (excluding the port number):" && 181 read -r smtp 182 case $sport in 183 587) tlsline="# tls_starttls" ;; 184 esac 185 [ -z "$realname" ] && realname="${fulladdr%%@*}" 186 [ -z "$passprefix" ] && passprefix="" 187 hostname="${fulladdr#*@}" 188 login="${login:-$fulladdr}" 189 if [ -n "${password+x}" ] && [ ! -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr.gpg" ]; then 190 insertpass 191 elif [ ! -f "$PASSWORD_STORE_DIR/$passprefix$fulladdr.gpg" ]; then 192 getpass 193 fi 194 } 195 196 insertpass() { 197 printf "%s" "$password" | pass insert -fe "$passprefix$fulladdr" 198 } 199 200 errorexit() { 201 echo "Log-on not successful." 202 case "$imap" in 203 imap.gmail.com) 204 echo "This account with $service is using Google's Gmail servers, which disable all third-party applications without an application-specific password. 205 Please be sure you are using OAUTH with your Gmail account, or better yet, stop using Gmail." 206 ;; 207 imap.mail.me.com) 208 echo "This account with $service is using Apple's iCloud servers, which disable all non-Apple applications by default. 209 Please be sure you either enable third-party applications, or create an app-specific password, or best of all, stop using Apple." 210 ;; 211 esac 212 exit 1 213 } 214 215 getpass() { while :; do 216 pass rm -f "$passprefix$fulladdr" >/dev/null 2>&1 217 pass insert -f "$passprefix$fulladdr" && break 218 done; } 219 220 getboxes() { 221 if [ -n "${force+x}" ]; then 222 mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")" 223 else 224 info="$(curl --location-trusted -s -m 5 --user "$login:$(pass "$passprefix$fulladdr")" --url "${protocol:-imaps}://$imap:${iport:-993}")" 225 [ -z "$info" ] && errorexit 226 mailboxes="$(echo "$info" | grep -v HasChildren | sed "s/.*\" //;s/\"//g" | tr -d '\r')" 227 fi 228 [ "$type" = "pop" ] && mailboxes="INBOX" 229 for x in $( 230 sed -n "/^macro.* i[0-9] / s/\(^macro.* i\| .*\)//gp " "$muttrc" 2>/dev/null | sort -u 231 echo 0 232 ); do 233 idnum=$((idnum + 1)) 234 [ "$idnum" -eq "$x" ] || break 235 done 236 toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/;s/'/\\\'/g" | paste -sd ' ' -)" 237 } 238 239 finalize() { 240 echo "$toappend" >>"$accdir/$fulladdr.muttrc" 241 [ "$type" != "online" ] && echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new" 242 mkdir -p "$cachedir/$safename/bodies" 243 echo "$fulladdr (account #$idnum) added successfully." 244 command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" >"$HOME/.urlview" 245 return 0 246 } 247 248 prepnotmuch() { 249 [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config" 250 [ -f "$NOTMUCH_CONFIG" ] && return 0 251 envsubst <"$notmuchtemp" >"$NOTMUCH_CONFIG" 252 } 253 254 togglecron() { 255 cron="$(mktemp)" 256 crontab -l >"$cron" 257 if grep -q mailsync "$cron"; then 258 echo "Removing automatic mailsync..." 259 sed -ibu /mailsync/d "$cron" 260 rm -f "$cron"bu 261 else 262 echo "Adding automatic mailsync every ${cronmin:-10} minutes..." 263 echo "*/${cronmin:-10} * * * * $prefix/bin/mailsync" >>"$cron" 264 fi && 265 crontab "$cron" 266 rm -f "$cron" 267 } 268 269 setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then 270 echo "Running $1 with $action..." 271 echo "Incompatible options given. Only one action may be specified per run." 272 exit 1 273 else 274 action="$1" 275 fi; } 276 277 mwinfo() { 278 cat <<EOF 279 mw: mutt-wizard, auto-configure email accounts for mutt 280 including downloadable mail with \`isync\`. 281 282 Main actions: 283 -a your@email.com Add an email address 284 -l List email addresses configured 285 -d Remove an already added address 286 -D your@email.com Force remove account without confirmation 287 -t number Toggle automatic mailsync every <number> minutes 288 -T Toggle automatic mailsync 289 -r Reorder account numbers 290 291 Options allowed with -a: 292 -u Account login name if not full address 293 -n "Real name" to be on the email account 294 -i IMAP/POP server address 295 -I IMAP/POP server port 296 -s SMTP server address 297 -S SMTP server port 298 -x Password for account (recommended to be in double quotes) 299 -p Add for a POP server instead of IMAP. 300 -P Pass Prefix (prefix of the file where password is stored) 301 -X Delete an account's local email too when deleting. 302 -o Configure address, but keep mail online. 303 -f Assume typical English mailboxes without attempting log-on. 304 305 NOTE: Once at least one account is added, you can run 306 \`mbsync -a\` to begin downloading mail. 307 308 To change an account's password, run \`pass edit '$passprefix'your@email.com\`. 309 EOF 310 } 311 312 reorder() { 313 tempfile="$(mktemp -u)" 314 trap 'rm -f $tempfile' HUP INT QUIT TERM PWR EXIT 315 echo "# Carefully reorder these accounts with the desired numbers in the first column. 316 # DO NOT reorder rows or rename the accounts in the second column." >"$tempfile" 317 sed -n " 318 / i[0-9] / s?\(.* i\|'<sync.*/\|\.muttrc.*\)??g p 319 " "$muttrc" >>"$tempfile" 320 ${EDITOR:-vim} "$tempfile" || exit 1 321 sed -i -e 's/#.*//' -e '/^$/d' "$tempfile" 322 default="$(sort -n "$tempfile" | head -n 1)" 323 default="${default#* }" 324 sed -ibu " 325 /.* i[0-9] .*.muttrc/d 326 /^source.*accounts.*.muttrc/d 327 " "$muttrc" 2>/dev/null 328 rm -f "$muttrc"bu 329 awk -v a="$accdir" -v d="$default" ' BEGIN { print "source "a"/"d".muttrc" } 330 { 331 print "macro index,pager i"$1" '\''<sync-mailbox><enter-command>source "a"/"$2".muttrc<enter><change-folder>!<enter>;<check-stats>'\'' \"switch to "$2"\"" 332 } 333 ' "$tempfile" >>"$muttrc" 334 } 335 336 while getopts "rfpXlhodTYD:y:i:I:s:S:u:a:n:P:x:m:t:" o; do case "${o}" in 337 l) setact list ;; 338 r) setact reorder ;; 339 d) setact delete ;; 340 D) 341 setact delete 342 fulladdr="$OPTARG" 343 ;; 344 y) 345 setact sync 346 fulladdr="$OPTARG" 347 ;; 348 Y) setact sync ;; 349 a) 350 setact add 351 fulladdr="$OPTARG" 352 ;; 353 i) 354 setact add 355 imap="$OPTARG" 356 ;; 357 I) 358 setact add 359 iport="$OPTARG" 360 ;; 361 s) 362 setact add 363 smtp="$OPTARG" 364 ;; 365 S) 366 setact add 367 sport="$OPTARG" 368 ;; 369 u) 370 setact add 371 login="$OPTARG" 372 ;; 373 n) 374 setact add 375 realname="$OPTARG" 376 ;; 377 P) 378 setact add 379 passprefix="$OPTARG" 380 ;; 381 m) 382 setact add 383 maxmes="$OPTARG" 384 ;; 385 o) 386 setact add 387 type="online" 388 ;; 389 p) 390 setact add 391 type="pop" 392 protocol="pop3s" 393 iport="${iport:-995}" 394 ;; 395 f) 396 setact add 397 force=True 398 ;; 399 x) 400 setact add 401 password="$OPTARG" 402 ;; 403 X) 404 setact delete 405 purge=True 406 ;; 407 t) 408 setact toggle 409 cronmin="$OPTARG" 410 ;; 411 T) setact toggle ;; 412 h) setact info ;; 413 \?) 414 echo "See \`$(basename $0) -h\` for possible options and help." 415 exit 1 416 ;; 417 esac done 418 419 [ -z "$action" ] && action="info" 420 421 case "$action" in 422 list) list ;; 423 add) checkbasics && askinfo && getboxes && getprofiles && finalize ;; 424 delete) delete ;; 425 sync) 426 echo "\`mw -y\` and \`mw -Y\` are now deprecated and will be removed in a future update. Please switch to using \`mailsync\`." 427 mailsync $fulladdr 428 ;; 429 toggle) togglecron ;; 430 reorder) reorder ;; 431 info) 432 mwinfo 433 exit 1 434 ;; 435 esac