a bunch of stuff lmao

This commit is contained in:
Luke Smith 2019-04-22 12:14:33 -04:00
parent 2de55b8779
commit d439fbd646
No known key found for this signature in database
GPG Key ID: 4C50B54A911F6252

52
bin/mw
View File

@ -9,6 +9,8 @@ emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
muttshare="/usr/share/mutt-wizard" muttshare="/usr/share/mutt-wizard"
mbsyncrc="$HOME/.mbsyncrc" mbsyncrc="$HOME/.mbsyncrc"
sslcert="/etc/ssl/certs/ca-certificates.crt" sslcert="/etc/ssl/certs/ca-certificates.crt"
mwconfig="$muttdir/mutt-wizard.muttrc"
muttrc="$muttdir/muttrc"
getprofiles() { \ getprofiles() { \
unset msmtp_header msmtp_profile mutt_profile mbsync_profile unset msmtp_header msmtp_profile mutt_profile mbsync_profile
@ -147,7 +149,7 @@ EOF
printf "\033[0m" printf "\033[0m"
[ -z "$login" ] && login="$fulladdr" [ -z "$login" ] && login="$fulladdr"
tmpdir="$(mktemp -d)" tmpdir="$(mktemp -d)"
grep "i[0-9]" "$muttdir/personal.muttrc" 2>/dev/null | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used" grep "i[0-9]" "$muttrc" 2>/dev/null | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
seq 1 9 > "$tmpdir/mutt_all" seq 1 9 > "$tmpdir/mutt_all"
idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}') idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
getpass getpass
@ -157,28 +159,27 @@ EOF
echo "$msmtp_profile" >> "$HOME/.config/msmtp/config" echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
echo "$mutt_profile" > "$accdir/$title.muttrc" echo "$mutt_profile" > "$accdir/$title.muttrc"
echo "$mbsync_profile" >> "$mbsyncrc" echo "$mbsync_profile" >> "$mbsyncrc"
! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc" ! grep "^source.*.muttrc" "$muttrc" | grep -v "$mwconfig" >/dev/null && echo "source $accdir/$title.muttrc # mw-autogenerated" >> "$muttrc"
echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$title.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttdir/personal.muttrc" echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$title.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\" # mw-autogenerated" >> "$muttrc"
} }
getpass() { while : ; do pass rm -f "mutt-wizard-$title" >/dev/null 2>&1 getpass() { while : ; do pass rm -f "mutt-wizard-$title" >/dev/null 2>&1
pass insert "mutt-wizard-$title" && break; done ;} pass insert "mutt-wizard-$title" && break; done ;}
formatShortcut() { \ formatShortcut() { \
while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # autogenerated" while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # mw-autogenerated"
echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # autogenerated" echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # autogenerated"; } >> "$accdir/$3.muttrc" echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$accdir/$3.muttrc"
done ;} done ;}
tryconnect() { ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning \`mbsync %s\` manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" "$title" && return 1 tryconnect() { mkdir -p "$maildir/$title"
mkdir -p "$maildir/$title"
if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1; then if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1; then
[ "$accounttype" = "online" ] && sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" [ "$accounttype" = "online" ] && sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc"
printf "\033[32mMailboxes detected.\033[0m\\n" printf "\033[32mMailboxes detected.\033[0m\\n"
echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}" echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
else else
printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or sever settings, or there are other requirements for your account out of the control of mutt-wizard.\\n" printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or sever settings, or there are other requirements for your account out of the control of mutt-wizard.\\n"
delete "$title"; return 1 return 1
fi ;} fi ;}
finalize() { \ finalize() { \
@ -199,7 +200,7 @@ finalize() { \
{ echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$title.muttrc" { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$accdir/$title.muttrc"
echo mailboxes "$oneline" >> "$accdir/$title.muttrc" echo mailboxes "$oneline" >> "$accdir/$title.muttrc"
printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n" printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
sed -i "/# autogenerated/d" "$accdir/$title.muttrc" sed -i "/# mw-autogenerated/d" "$accdir/$title.muttrc"
grep -i -m 1 inbox "$tmpdir/title_boxes" | formatShortcut i inbox "$title" grep -i -m 1 inbox "$tmpdir/title_boxes" | formatShortcut i inbox "$title"
grep -i -m 1 sent "$tmpdir/title_boxes" | formatShortcut s sent "$title" grep -i -m 1 sent "$tmpdir/title_boxes" | formatShortcut s sent "$title"
grep -i -m 1 draft "$tmpdir/title_boxes" | formatShortcut d drafts "$title" grep -i -m 1 draft "$tmpdir/title_boxes" | formatShortcut d drafts "$title"
@ -214,13 +215,13 @@ confirm() { printf "Do you want to to %s? [y/N]\\n\t" "$@" && read -r input && !
printf "Are you really, really sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1 printf "Are you really, really sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
return 0 ;} return 0 ;}
list() { numbered="$(find "$accdir" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" 2>/dev/null | nl)" list() { numbered="$(find "$accdir" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | nl)"
[ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No configured accounts.\\n" && return 1 echo "$numbered"
[ "$(echo "$numbered" | wc -l)" = 0 ] && printf "No configured accounts.\\n" && return 1
} }
pick() { list pick() { printf "Select an accounts to %s:\\n" "$1"
printf "Select an accounts to %s:\\n" "$1" list
echo "$numbered"
printf "\033[36m\t" printf "\033[36m\t"
read -r input read -r input
printf "\033[0m" printf "\033[0m"
@ -231,7 +232,7 @@ pick() { list
delete() { sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" delete() { sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc"
rm -rf "${accdir:?}/${title:?}" "$accdir/$title.muttrc" rm -rf "${accdir:?}/${title:?}" "$accdir/$title.muttrc"
sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc" sed -i "/$title.muttrc/d" "$muttrc"
sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config" sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config"
} }
@ -266,19 +267,26 @@ asktype() { while : ; do
esac; done ;} esac; done ;}
init() { echo "Initializing a mutt-wizard configuration..." init() { echo "Initializing a mutt-wizard configuration..."
[ -f "$accdir" ] && echo "There is already a preexisting config. Purge the previous to reinit." && return 1 mkdir -p "$muttdir" "$accdir" && echo "Required directories created."
mkdir -p "$muttdir" "$accdir" [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created."
cp -v "$muttshare/mailcap" "$muttdir" cp -v "$muttshare/mailcap" "$muttdir" && echo "Mailcap file added."
cp -v "$muttshare/muttrc" "$muttdir" cp -v "$muttshare/mutt-wizard.muttrc" "$muttdir" && echo "mutt-wizard config file added."
! grep "^source.*mutt-wizard.muttrc" "$muttrc" >/dev/null && echo "source $mwconfig" >> "$muttrc" && echo "muttrc will now source mutt-wizard."
}
purge() { confirm "delete all account data" || exit
rm -rf "$mbsyncrc" "$accdir" "$mwconfig" "$HOME/.config/msmtp" "$mwconfig"
echo "All configs and account settings have been purged."
sed -i "\|$mwconfig|d" "$muttrc"
} }
case "$1" in case "$1" in
init) init ;; init) init ;;
ls) list ;; ls) list ;;
add) asktype && askinfo && tryconnect && finalize ;; add) asktype && askinfo && tryconnect && finalize || delete ;;
pass) pick "change the password of" && getpass ;; pass) pick "change the password of" && getpass ;;
delete) pick delete && confirm "delete the \`$title\` profile" && delete ;; delete) pick delete && confirm "delete the \`$title\` profile" && delete ;;
purge) confirm "delete all account data" && rm -rf "$mbsyncrc" "$accdir" "$HOME/.config/msmtp" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged." ;; purge) purge ;;
cron) choosecron ;; cron) choosecron ;;
*) cat << EOF *) cat << EOF
mw: mutt-wizard, auto-configure email accounts for mutt mw: mutt-wizard, auto-configure email accounts for mutt