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