Skip to content
Snippets Groups Projects
freedombone-notification 5.63 KiB
Newer Older
#!/bin/bash
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
#                              Freedom in the Cloud
#
# Sends a notification to the administrator user
#
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2018-2019 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

PROJECT_NAME='freedombone'

ADMIN_USERNAME=
SUBJECT=
MESSAGE=
COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
Bob Mottram's avatar
Bob Mottram committed
local_domain=$HOSTNAME
notification_debug=
function kill_xmppsend_process {
    # Sometimes the process can get stuck, so ensure that
    # it gets killed if necessary
    # shellcheck disable=SC2009
    xmppsend_pid=$(ps ax | grep /usr/local/bin/xmppsend | grep -v grep | awk -F ' ' '{print $1}')
    if [ "$xmppsend_pid" ]; then
        kill -9 "$xmppsend_pid"
while [ $# -gt 1 ]
do
    key="$1"

    case $key in
        -e|--email)
            shift
            ADMIN_EMAIL_ADDRESS="$1"
            ;;
        -u|--user|--username)
            shift
            ADMIN_USERNAME="$1"
            ;;
        -s|--subject|--header)
            shift
            SUBJECT="$1"
            if [[ "$SUBJECT" == *'"'* ]]; then
                # shellcheck disable=SC2001
                NEW_SUBJECT=$(echo "$SUBJECT" | sed 's|"||g')
                SUBJECT="$NEW_SUBJECT"
            fi
            ;;
        -m|--message|--msg)
            shift
            MESSAGE="$1"
            if [[ "$MESSAGE" == *'"'* ]]; then
                # shellcheck disable=SC2001
                NEW_MESSAGE=$(echo "$MESSAGE" | sed 's|"||g')
                MESSAGE="$NEW_MESSAGE"
            fi
        -d|--debug)
            shift
            if [[ "$1" == 'y'* || "$1" == 't'*  || "$1" == '1' ]]; then
                notification_debug=1
            fi
            ;;
        *)
            # unknown option
            ;;
    esac
    shift
done

if [ ! "$MESSAGE" ]; then
    exit 0
fi

if [ ! "$ADMIN_USERNAME" ]; then
    ADMIN_USERNAME=$(grep "Admin user" "$COMPLETION_FILE" | awk -F ':' '{print $2}')
fi

if [ ! "$ADMIN_EMAIL_ADDRESS" ]; then
Bob Mottram's avatar
Bob Mottram committed
    ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${local_domain}
notification_image_filename=/tmp/notification.jpg
Bob Mottram's avatar
Bob Mottram committed
if [[ "$SUBJECT" == *"USB"* ]]; then
    if [ -f /usr/bin/fswebcam ]; then
        # Preferentially use a secondary camera which can
        # be set up from an appropriate viewing angle
Bob Mottram's avatar
Bob Mottram committed
        if [ -e /dev/video1 ]; then
            if [ -f $notification_image_filename ]; then
                rm $notification_image_filename
            fi
            fswebcam -d /dev/video1 --jpeg 50 -D 1 $notification_image_filename
        else
            # Otherwise use the built-in camera
Bob Mottram's avatar
Bob Mottram committed
            if [ -e /dev/video0 ]; then
                if [ -f $notification_image_filename ]; then
                    rm $notification_image_filename
                fi
                fswebcam -d /dev/video0 --jpeg 50 -D 1 $notification_image_filename
            fi
        fi
    fi
fi
Bob Mottram's avatar
Bob Mottram committed
image_extra=
if [ -f $notification_image_filename ]; then
Bob Mottram's avatar
Bob Mottram committed
    # extra option for mail command
Bob Mottram's avatar
Bob Mottram committed
    image_extra=" -A $notification_image_filename"

    # If syncthing is installed then copy the image to the admin
    if [ -d "/home/$ADMIN_USERNAME/Sync" ]; then
        cp $notification_image_filename "/home/$ADMIN_USERNAME/Sync/usb_canary.jpg"
    fi
fi
# send email to administrator
if [ "$SUBJECT" ]; then
    # shellcheck disable=SC2086
Bob Mottram's avatar
Bob Mottram committed
    echo "$MESSAGE" | mail -s "$SUBJECT"$image_extra "$ADMIN_EMAIL_ADDRESS"
Bob Mottram's avatar
Bob Mottram committed
if [ -d /etc/prosody ]; then
    if [ -f /usr/local/bin/xmppsend ]; then
        notification_user_password=$(openssl rand -base64 32 | tr -dc A-Za-z0-9 | head -c 30 ; echo -n '')
        if prosodyctl register "notification" "$local_domain" "$notification_user_password"; then
            if [[ "$SUBJECT" == *' Tor '* ]]; then
                MESSAGE="$SUBJECT"
            fi
            kill_xmppsend_process
            torsocks /usr/local/bin/xmppsend "notification@$local_domain" "$notification_user_password" "$ADMIN_EMAIL_ADDRESS" "$MESSAGE" $notification_debug
        fi
        prosodyctl deluser "notification@$local_domain"
Bob Mottram's avatar
Bob Mottram committed
    fi
if [ -d /etc/scuttlebot/notification ]; then
    if grep -q "SSB_NOTIFY=" "/root/${PROJECT_NAME}.cfg"; then
        SSB_NOTIFY=$(grep 'SSB_NOTIFY=' "/root/${PROJECT_NAME}.cfg" | head -n 1 | sed 's|SSB_NOTIFY=||g')
        if [[ "$SSB_NOTIFY" == '@'* && "$SSB_NOTIFY" == *'.ed25519' ]]; then

Bob Mottram's avatar
Bob Mottram committed
            cat <<EOF > /etc/scuttlebot/send_scuttlebutt_notification
#!/bin/bash
cd /etc/scuttlebot/notification || exit 1
./ssb-example-pm.js $SSB_NOTIFY "$MESSAGE"
exit 0
EOF
Bob Mottram's avatar
Bob Mottram committed
            chmod +x /etc/scuttlebot/send_scuttlebutt_notification
            chown scuttlebot:scuttlebot /etc/scuttlebot/send_scuttlebutt_notification
Bob Mottram's avatar
Bob Mottram committed
            #su -c '/etc/scuttlebot/send_scuttlebutt_notification' - scuttlebot
Bob Mottram's avatar
Bob Mottram committed
            rm /etc/scuttlebot/send_scuttlebutt_notification
if [ -f $notification_image_filename ]; then
    rm $notification_image_filename
fi