From d439fbd646061af8123d65f6d15ad0147e9e8029 Mon Sep 17 00:00:00 2001 From: Luke Smith Date: Mon, 22 Apr 2019 12:14:33 -0400 Subject: [PATCH] a bunch of stuff lmao --- bin/mw | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/bin/mw b/bin/mw index 143e187..21de272 100755 --- a/bin/mw +++ b/bin/mw @@ -9,6 +9,8 @@ emailre=".\+@.\+\\..\+" # Regex to confirm valid email address muttshare="/usr/share/mutt-wizard" mbsyncrc="$HOME/.mbsyncrc" sslcert="/etc/ssl/certs/ca-certificates.crt" +mwconfig="$muttdir/mutt-wizard.muttrc" +muttrc="$muttdir/muttrc" getprofiles() { \ unset msmtp_header msmtp_profile mutt_profile mbsync_profile @@ -147,7 +149,7 @@ EOF printf "\033[0m" [ -z "$login" ] && login="$fulladdr" 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" idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}') getpass @@ -157,28 +159,27 @@ EOF echo "$msmtp_profile" >> "$HOME/.config/msmtp/config" echo "$mutt_profile" > "$accdir/$title.muttrc" echo "$mbsync_profile" >> "$mbsyncrc" - ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc" - echo "macro index,pager i$idnum 'source $accdir/$title.muttrc!;' \"switch to $fulladdr\"" >> "$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 'source $accdir/$title.muttrc!;' \"switch to $fulladdr\" # mw-autogenerated" >> "$muttrc" } getpass() { while : ; do pass rm -f "mutt-wizard-$title" >/dev/null 2>&1 pass insert "mutt-wizard-$title" && break; done ;} formatShortcut() { \ - while read -r data; do { echo "macro index,pager g$1 \"$data\" \"go to $2\" # autogenerated" - echo "macro index,pager M$1 \"$data\" \"move mail to $2\" # autogenerated" - echo "macro index,pager C$1 \"$data\" \"copy mail to $2\" # autogenerated"; } >> "$accdir/$3.muttrc" + while read -r data; do { echo "macro index,pager g$1 \"$data\" \"go to $2\" # mw-autogenerated" + echo "macro index,pager M$1 \"$data\" \"move mail to $2\" # mw-autogenerated" + echo "macro index,pager C$1 \"$data\" \"copy mail to $2\" # mw-autogenerated"; } >> "$accdir/$3.muttrc" 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 - mkdir -p "$maildir/$title" +tryconnect() { mkdir -p "$maildir/$title" if mailboxes="$(mbsync -l "$title" | sed 's/\//./')" >/dev/null 2>&1; then [ "$accounttype" = "online" ] && sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" printf "\033[32mMailboxes detected.\033[0m\\n" echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}" 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" - delete "$title"; return 1 + return 1 fi ;} 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 mailboxes "$oneline" >> "$accdir/$title.muttrc" 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 sent "$tmpdir/title_boxes" | formatShortcut s sent "$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 return 0 ;} -list() { numbered="$(find "$accdir" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" 2>/dev/null | nl)" - [ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No configured accounts.\\n" && return 1 +list() { numbered="$(find "$accdir" -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | nl)" + echo "$numbered" + [ "$(echo "$numbered" | wc -l)" = 0 ] && printf "No configured accounts.\\n" && return 1 } -pick() { list - printf "Select an accounts to %s:\\n" "$1" - echo "$numbered" +pick() { printf "Select an accounts to %s:\\n" "$1" + list printf "\033[36m\t" read -r input printf "\033[0m" @@ -231,7 +232,7 @@ pick() { list delete() { sed -i "/IMAPStore $title-remote$/,/# End profile/d" "$mbsyncrc" 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" } @@ -266,19 +267,26 @@ asktype() { while : ; do esac; done ;} 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" - cp -v "$muttshare/mailcap" "$muttdir" - cp -v "$muttshare/muttrc" "$muttdir" + mkdir -p "$muttdir" "$accdir" && echo "Required directories created." + [ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc" && echo "muttrc created." + cp -v "$muttshare/mailcap" "$muttdir" && echo "Mailcap file added." + 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 init) init ;; ls) list ;; - add) asktype && askinfo && tryconnect && finalize ;; + add) asktype && askinfo && tryconnect && finalize || delete ;; pass) pick "change the password of" && getpass ;; 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 ;; *) cat << EOF mw: mutt-wizard, auto-configure email accounts for mutt