Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
freedombone-notification 3.30 KiB
#!/bin/bash
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
#                              Freedom in the Cloud
#
# Sends a notification to the administrator user
#
# License
# =======
#
# Copyright (C) 2018 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=

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"
            ;;
        -m|--message|--msg)
            shift
            MESSAGE="$1"
            ;;
        *)
            # unknown option
            ;;
    esac
    shift
done

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

if [ ! "$COMPLETION_FILE" ]; then
    COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
fi

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

if [ ! "$ADMIN_EMAIL_ADDRESS" ]; then
    ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
fi

# send email to administrator
if [ "$SUBJECT" ]; then
    echo "$MESSAGE" | mail -s "$SUBJECT" "$ADMIN_EMAIL_ADDRESS"
fi

if [ -d /etc/prosody ]; then
    notification_user_password=$(openssl rand -base64 32 | tr -dc A-Za-z0-9 | head -c 30 ; echo -n '')
    prosodyctl deluser "notification@$HOSTNAME" 2> /dev/null
    if prosodyctl register "notification" "$HOSTNAME" "$notification_user_password" 2> /dev/null; then
        { echo '#!/usr/bin/python2';
          echo 'import sys,os,xmpp,time';
          echo 'tojid = sys.argv[1]';
          echo 'data = sys.stdin.readlines()';
          echo "msg = ' '.join(data)";
          echo "username = 'notification@$HOSTNAME'";
          echo "password = '$notification_user_password'";
          echo 'jid=xmpp.protocol.JID(username)';
          echo 'cl=xmpp.Client(jid.getDomain(),debug=[])';
          echo 'con=cl.connect()';
          echo 'if not con:';
          echo '    sys.exit()';
          echo 'auth=cl.auth(jid.getNode(),password,resource=jid.getResource())';
          echo 'if not auth:';
          echo '    sys.exit()';
          echo 'id=cl.send(xmpp.protocol.Message(tojid, msg))';
          echo 'time.sleep(1)';
          echo 'cl.disconnect()'; } > /tmp/xsend
        chmod +x /tmp/xsend

        echo "$MESSAGE" | /tmp/xsend "$ADMIN_EMAIL_ADDRESS"
        rm /tmp/xsend
        prosodyctl deluser "notification@$HOSTNAME" 2> /dev/null
    fi
fi

exit 0