Add misc stuff master
authorJoerg Jaspert <joerg@debian.org>
Wed, 6 Mar 2019 20:46:22 +0000 (21:46 +0100)
committerJoerg Jaspert <joerg@debian.org>
Wed, 6 Mar 2019 20:46:22 +0000 (21:46 +0100)
bin/kbd.sh [new file with mode: 0755]
bin/lock [new file with mode: 0755]
bin/mails [new file with mode: 0755]
bin/tagchange [new file with mode: 0755]

diff --git a/bin/kbd.sh b/bin/kbd.sh
new file mode 100755 (executable)
index 0000000..005334a
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+xset r rate 200 80
+xmodmap ${HOME}/.xmodmap
+notify-send -t 5000 "Keyboard adjusted"
diff --git a/bin/lock b/bin/lock
new file mode 100755 (executable)
index 0000000..28c5899
--- /dev/null
+++ b/bin/lock
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+xset dpms force off
+killall -HUP gpg-agent
+xset dpms force off
+i3lock || true
diff --git a/bin/mails b/bin/mails
new file mode 100755 (executable)
index 0000000..1a169ea
--- /dev/null
+++ b/bin/mails
@@ -0,0 +1,207 @@
+#!/bin/bash
+
+set -e
+set -u
+set -E
+set -o pipefail
+
+# Function that only cleans tempfiles, but does not exit or otherwise
+# care about any exit status
+function cleantempfiles() {
+    resolvetmpfiles
+    for TEMPFILE in $TMPFILES; do
+        if [ -n "${TEMPFILE}" ] && [ -f "${TEMPFILE}" ]; then
+            rm -f "${TEMPFILE}"
+        elif [ -n "${TEMPFILE}" ] && [ -d "${TEMPFILE}" ]; then
+            if [ "${TEMPFILE}" != "/" ] && [ "${TEMPFILE}" != "/*" ]; then
+                rm -rf "${TEMPFILE}"
+            fi
+        fi
+    done
+    TMPFILES=""
+}
+
+function resolvetmpfiles() {
+    # If you don't understand this better not touch the script
+    for TEMPFILE in $TEMPFILES; do
+        TMPFILES="${TMPFILES} ${!TEMPFILE:-""}"
+    done
+    TEMPFILES=""
+}
+
+# Function cleanup
+# No arguments
+# Cleans up any known tempfile.
+# Just ensure your script sets the variable TEMPFILES to the right names of variables
+# of tempfiles
+# Or TMPFILES to the pathes of tempfiles
+function cleanup() {
+    ERRVAL=$?
+    trap - ERR EXIT TERM HUP INT QUIT
+
+    cleantempfiles
+
+    exit $ERRVAL
+}
+TEMPFILES=${TEMPFILES:-""}
+TMPFILES=${TMPFILES:-""}
+
+function hamspam() {
+    local search="tag:spam and not tag:learnham and not folder:ganneff/.junk and not folder:ganneff/.INBOX and not folder:ganneff/.SPAM"
+    local spamcount=$( notmuch count ${search} )
+
+    local SSH="ssh -o ConnectTimeout=120 -o Setuptimeout=120 -o BatchMode=yes -o ControlMaster=no -T -i ${HAMSPAMTRAINID} mail.ganneff.de "
+
+    if [[ ${spamcount} -gt 0 ]]; then
+        echo "Preparing ${spamcount} new spam messages for training, then mark for deletion"
+        # Create a tempdir to hold them
+        tmpspam=$(mktemp -d -p ${TMPDIR} hammailsspam.XXXXXXXXXXXX)
+        mkdir -p ${tmpspam}/{cur,new,tmp}
+        # Got a tempdir, ensure that our cleanup kills it
+        TMPFILES="${TMPFILES} ${tmpspam}"
+
+        #touch "${HOME}/Mail/archive/Spam"
+        notmuch search --output=files ${search} \
+            | while read file; do
+            cp "${file}" ${tmpspam}/cur/
+            # cat "$file" | formail -ds >> "${HOME}/Mail/archive/Spam"
+        done
+        notmuch tag +deleted -- ${search}
+    fi
+
+    search="tag:learnham"
+    local hamcount=$( notmuch count ${search} )
+    if [[ ${hamcount} -gt 0 ]]; then
+        echo "Preparing ${hamcount} learnham messages for ham training, then remove learnham tag"
+        tmpham=$(mktemp -d -p ${TMPDIR} hammailsspam.XXXXXXXXXXXX)
+        mkdir -p ${tmpham}/{cur,new,tmp}
+        notmuch search --output=files ${search} \
+            | while read file; do
+            cp "${file}" ${tmpham}/cur/
+            #cat "$file" | formail -ds >> "${HOME}/Mail/archive/Ham"
+        done
+        notmuch tag -learnham -spam -deleted ${search}
+    fi
+
+    if [[ ${spamcount} -ge 1 ]]; then
+        echo "Training ${spamcount} new spam mails into rspamd"
+        set +e
+        (
+            unset SSH_AUTH_SOCK
+            tar czf - -C ${tmpspam} . | ${SSH} spamtrain
+        )
+        ret=$?
+        set -e
+        if [[ ${ret} -ne 0 ]]; then
+            echo "Some problem with spamtraining."
+        fi
+    fi
+
+    if [[ ${hamcount} -ge 1 ]]; then
+        echo "Training ${hamcount} new ham mails into rspamd"
+        set +e
+        (
+            unset SSH_AUTH_SOCK
+            tar czf - -C ${tmpham} . | ${SSH} hamtrain
+        )
+        ret=$?
+        set -e
+        if [[ ${ret} -ne 0 ]]; then
+            echo "Some problem with hamtraining."
+        fi
+    fi
+    rm -rf ${tmpham:-""} ${tmpspam:-""}
+}
+
+function delmail() {
+    local search="tag:deleted OR (tag:deleted AND tag:spam)"
+    local count=$( notmuch count ${search} )
+    if [[ ${count} -gt 0 ]]; then
+        echo "unconditionnally deleting ${count} deleted mails"
+        notmuch search --output=files ${search} | xargs -r rm -f
+    fi
+}
+
+function notmuchnew() {
+    echo "Updating notmuch"
+    notmuch new
+}
+
+function dombsync() {
+    channel=${1:-"all"}
+    echo "Now syncing mail with remote server, channel ${channel}"
+    mbsync ${channel} || echo "Failure"
+}
+
+function runafew() {
+    afew --tag --new -v
+}
+
+function ts() {
+    date
+}
+what=${1:-"n"}
+mailbox=${2:-""}
+
+TMPDIR=${TMPDIR:-${TMP:-"/tmp"}}
+HAMSPAMTRAINID="${HOME}/.ssh/hamspamsync"
+
+while :; do
+    clear
+
+    case ${what} in
+        [mM])
+            channel="g"
+            read -t 30 -e -p "Channel (g Ganneff, f NSB): " -i "g" channel || true
+            read -t 30 -e -p "Enter mailbox name: " mailbox || true
+            if [[ -z ${mailbox} ]]; then
+                continue
+            fi
+            case ${channel} in
+                [gG])
+                    channel="ganneff"
+                    ;;
+                [fF])
+                    channel="nsb"
+                    ;;
+                *)
+                    continue
+                    ;;
+            esac
+            # Ensure that . are / for IMAP
+            mailbox=${mailbox//.//}
+            dombsync "${channel}:${mailbox}"
+            ;;&
+        [nN]|[gG]|[fF]|[dD])
+            hamspam
+            delmail
+            notmuchnew
+            ;;&
+        [nN])
+            dombsync
+            ;;&
+        [sS])
+            dombsync all-small
+            notmuchnew
+            runafew
+            ts
+            ;;
+        [gG])
+            dombsync ganneff
+            ;;&
+        [fF])
+            dombsync nsb
+            ;;&
+        [nN]|[gG]|[fF]|[mM])
+            notmuchnew
+            runafew
+            ts
+            ;;
+    esac
+
+    echo ""
+    read -t 1800 -n 1 -s -p "n|RET - Normal, s - Short, g - Ganneff only, f - NSB only, d - Deletions, m - Enter mailbox name" what || true
+    if [[ -z ${what} ]]; then
+        what="n"
+    fi
+done
diff --git a/bin/tagchange b/bin/tagchange
new file mode 100755 (executable)
index 0000000..d640869
--- /dev/null
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+set -e
+set -u
+set -E
+
+old=""
+new=""
+yesno=""
+
+while :; do
+    read -ep "Old Tag: " old
+    notmuch count tag:${old}
+    read -ep "New Tag: " new
+    notmuch count tag:${new}
+
+    read -n 1 -s -p "Should I run notmuch tag -${old} +${new} tag:${old} ?" -i "y" yesno
+    if [[ ${yesno} == y ]]; then
+        notmuch tag -${old} +${new} tag:${old}
+    else
+        echo "Not running"
+    fi
+    echo ""
+done