From a581a1843d615023a98680012a0522ee6879a44d Mon Sep 17 00:00:00 2001 From: mxmehl Date: Fri, 29 Jul 2016 19:08:33 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + config.cfg.sample | 11 +++++ shared.so | 19 ++++++++ status-ip.sh | 35 +++++++++++++++ status-misc.sh | 112 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 .gitignore create mode 100644 config.cfg.sample create mode 100644 shared.so create mode 100755 status-ip.sh create mode 100755 status-misc.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac285ce --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.cfg diff --git a/config.cfg.sample b/config.cfg.sample new file mode 100644 index 0000000..25df78e --- /dev/null +++ b/config.cfg.sample @@ -0,0 +1,11 @@ +# Default values when first starting the tool +var_conn="on" +var_mailqueue_runs=9 + +# status-ip.sh +IPSERV4="http://returns.plain-ipv4" +IPSERV6="http://returns.plain-ipv6" +VPNIP="1.2.3.4|90.80.70.60" + +# status-misc.sh +SVNSTATUS = http://website.showing."fin;revision".or.similar diff --git a/shared.so b/shared.so new file mode 100644 index 0000000..d51da04 --- /dev/null +++ b/shared.so @@ -0,0 +1,19 @@ +# SHARED FUNCTIONS AND COMMANDS + +# Test if config.cfg exists and set needed variables +if [ ! -e "$CURDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi +source "$CURDIR"/config.cfg + +# Create temp settings file +TMP=/tmp/statusbars +if [ ! -e "$TMP" ]; then + touch "$TMP" +fi +source "$TMP" + +# WRITE TMP +function wtmp { + > $TMP + echo "var_mailqueue_runs=$var_mailqueue_runs" >> $TMP + echo "var_conn=$var_conn" >> $TMP +} diff --git a/status-ip.sh b/status-ip.sh new file mode 100755 index 0000000..988ccff --- /dev/null +++ b/status-ip.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +CURDIR=$(dirname "$(readlink -f "$0")") +source "$CURDIR"/shared.so + +IP=$(wget -T 5 -q -O - $IPSERV4) +EXIT="$?" + +if [ "$var_conn" == "on" ]; then + if $(echo $IP | grep -qE "$VPNIP"); then + IMAGE="/usr/share/icons/gnome/16x16/apps/gnome-monitor.png" + TEXT="VPN ($IP)" + echo "$IMAGE" + elif ([ "$EXIT" == 4 ] || [ "$EXIT" == 6 ] || [ "$EXIT" == 28 ]); then + EXIT="fail" + TEXT="t/o" + else + TEXT="$IP" + fi + + echo " $TEXT" + + # If request was successful, try IPv6 as well + if [ "$EXIT" != "fail" ]; then + IP6=$(wget -T 5 -q -O - $IPSERV6) + EXIT="$?" + if ([ "$EXIT" == 4 ] || [ "$EXIT" == 6 ] || [ "$EXIT" == 28 ]); then + IP6="t/o" + fi + echo "IPv6: $IP6" + fi +else + echo "offline" + echo "Offline status is set. Turn on to enable checks" +fi diff --git a/status-misc.sh b/status-misc.sh new file mode 100755 index 0000000..6aae89d --- /dev/null +++ b/status-misc.sh @@ -0,0 +1,112 @@ +#!/bin/bash + +CURDIR=$(dirname "$(readlink -f "$0")") +source "$CURDIR"/shared.so + +OUT= +TOOL= + +function exec { + ACTIONS=("send mails in queue" "list mails in queue" "switch connection status" "quit") + PS3="Select action: " + + select action in "${ACTIONS[@]}" + do + echo # empty line after selection + case $REPLY in # $REPLY takes the numbers which is nice + 1) # Show queue; ask for confirmation; if yes, send emails + send_mailqueue + break ;; + 2) # Show queue + list_mailqueue + break ;; + 3) # Switch online status + switch_conn + break ;; + 4|"q") # exit silently, one can also press "q" instead of the number + exit 0 + break ;; + esac + done +} + +function check_mailqueue { + QUEUE=$(msmtp-listqueue.sh | grep -o "^From: " | wc -l) + OUT="$OUT;mq=$QUEUE" + TOOL="$TOOL;conn=$var_conn" + + # If $QUEUE > 0 and >10 runs, show notification + if [ "$QUEUE" -gt 0 ]; then + var_mailqueue_runs=$(($var_mailqueue_runs + 1)) + fi + if [ "$QUEUE" -gt 0 ] && [ "$var_mailqueue_runs" -gt 9 ]; then + notify-send "There are mails in the mailqueue. Remember sending them manually or switch online status." + var_mailqueue_runs=0 # reset counter + fi +} + +# MAIL QUEUE ACTIONS +function send_mailqueue { + msmtp-listqueue.sh + read -p "Send these queued mails? [Y/n]: " YN + if [[ $YN =~ ^(Y|y|)$ ]]; then + msmtp-runqueue.sh + else + echo "Not sending emails" + fi +} +function list_mailqueue { + msmtp-listqueue.sh +} +function switch_conn { + echo "Current connection status: $var_conn" + read -p "Switch connection status? [Y/n]: " YN + if [[ $YN =~ ^(Y|y|)$ ]]; then + if [ "$var_conn" == "off" ]; then + var_conn="on" + echo "Connection status switched to \"on\"" + else + var_conn="off" + echo "Connection status switched to \"off\"" + fi + else + echo "Connection status not changed." + fi +} + +# SVN STATUS +function check_svn { + STATUS=$(wget -T 5 -q -O - $SVNSTATUS) + EXIT="$?" + if ([ "$EXIT" == 4 ] || [ "$EXIT" == 6 ] || [ "$EXIT" == 28 ]); then + SVN="t/o" + else + SVN=$(echo $STATUS | cut -d";" -f1) + SVN_REV=$(echo $STATUS | cut -d";" -f2) + fi + OUT="$OUT;svn=$SVN" + TOOL="$TOOL;svn=$SVN_REV" +} + +# DIRECT EXECUTE, CASE MENU +if [ "$1" == "exec" ]; then + while :; do + exec + wtmp; source "$TMP" + echo + done +else + # Show info on status bar + check_mailqueue + check_svn + OUT=$(echo $OUT | sed -r 's/^;//') + OUT=$(echo $OUT | sed -r 's/;/ \| /g') + TOOL=$(echo $TOOL | sed -r 's/^;//') + TOOL=$(echo $TOOL | sed -r 's/;/ \| /g') + echo "$OUT" + echo "$TOOL" + echo "/usr/share/icons/gnome/16x16/actions/format-justify-fill.png" + echo "xfce4-terminal -x $0 exec &" + + wtmp +fi