epicyon/scripts/epicyon-notification

384 lines
17 KiB
Plaintext
Raw Normal View History

2020-06-21 11:40:46 +00:00
#!/bin/bash
#
# This can be called from a crontab entry to send notifications
2020-07-14 13:52:47 +00:00
# when Epicyon events occur. You will need to have
# sendxmpp+prosody or Synapse (matrix) installed.
#
# Something like:
#
2021-03-09 10:42:11 +00:00
# */1 * * * * root /usr/local/bin/epicyon-notification
2020-06-21 11:40:46 +00:00
#
# License
# =======
#
2021-09-10 16:14:50 +00:00
# Copyright (C) 2020-2021 Bob Mottram <bob@libreserver.org>
2020-06-21 11:40:46 +00:00
#
# 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=epicyon
epicyonInstallDir=/opt/${PROJECT_NAME}
2021-03-09 10:50:47 +00:00
MY_EMAIL_ADDRESS="username@domain"
2020-06-21 14:45:20 +00:00
local_domain=$HOSTNAME
if [ -f /var/lib/tor/hidden_service_epicyon/hostname ]; then
local_domain=$(cat /var/lib/tor/hidden_service_epicyon/hostname)
fi
2020-06-21 11:40:46 +00:00
function notification_translate_text {
text="$1"
if ! grep -q '"language":' "${epicyonInstallDir}/config.json"; then
2021-03-09 10:50:47 +00:00
echo "$text"
return
2020-06-21 11:40:46 +00:00
fi
language=$(cat "${epicyonInstallDir}/config.json" | awk -F '"language":' '{print $2}' | awk -F '"' '{print $2}')
translationsFilename="${epicyonInstallDir}/translations/${language}.json"
if [ ! -f "$translationsFilename" ]; then
2021-03-09 10:50:47 +00:00
echo "$text"
return
2020-06-21 11:40:46 +00:00
fi
if ! grep -q "\"$text\":" "$translationsFilename"; then
2021-03-09 10:50:47 +00:00
echo "$text"
return
2020-06-21 11:40:46 +00:00
fi
grep "\"$text\":" "$translationsFilename" | awk -F '"' '{print $4}'
}
2020-06-21 14:45:20 +00:00
function kill_sendxmpp_process {
# Sometimes the process can get stuck, so ensure that
# it gets killed if necessary
# shellcheck disable=SC2009
sendxmpp_pid=$(ps ax | grep /usr/bin/sendxmpp | grep -v grep | awk -F ' ' '{print $1}')
if [ "$sendxmpp_pid" ]; then
kill -9 "$sendxmpp_pid"
fi
}
function matrix_server_message {
admin_username="$1"
notifications_username="$2"
message="$3"
MATRIX_DATA_DIR='/var/lib/matrix'
homeserver_config="${MATRIX_DATA_DIR}/homeserver.yaml"
2021-03-09 10:50:47 +00:00
2020-06-21 14:45:20 +00:00
# shellcheck disable=SC2002
MATRIX_DOMAIN_NAME=$(cat "$homeserver_config" | grep "server_name:" | head -n 1 | awk -F '"' '{print $2}')
if [ ! "$MATRIX_DOMAIN_NAME" ]; then
return
fi
# get the curl command and domain to send to
curl_command='curl'
homebase="https://$MATRIX_DOMAIN_NAME"
if [ -f /var/lib/tor/hidden_service_matrix/hostname ]; then
curl_command='torsocks curl'
homebase="http://$(cat /var/lib/tor/hidden_service_matrix/hostname)"
fi
# get the token for the matrix admin user
MATRIXADMIN="@${admin_username}:$MATRIX_DOMAIN_NAME"
MATRIXUSER="@${notifications_username}:$MATRIX_DOMAIN_NAME"
cd "$MATRIX_DATA_DIR" || return
TOKEN=$(sqlite3 homeserver.db "select token from access_tokens where user_id like '$MATRIXADMIN' order by id desc limit 1;")
if [ ! "$TOKEN" ]; then
admin_username="${notifications_username}"
TOKEN=$(sqlite3 homeserver.db "select token from access_tokens where user_id like '$MATRIXUSER' order by id desc limit 1;")
if [ ! "$TOKEN" ]; then
echo "No matrix token for $MATRIXADMIN"
return
fi
fi
# send server notice
MATRIXPOST="${homebase}/_synapse/admin/v1/send_server_notice?access_token=${TOKEN}"
MATRIXMESSAGE="{\"user_id\": \"${MATRIXUSER}\",\"content\": { \"msgtype\": \"m.text\",\"body\": \"${message}\" }}"
# shellcheck disable=SC2086
${curl_command} --request POST --silent --header "Content-Type: application/json" --data "${MATRIXMESSAGE}" ${MATRIXPOST} > /dev/null
}
2020-06-21 11:40:46 +00:00
function sendNotification {
USERNAME="$1"
SUBJECT="$2"
MESSAGE="$3"
2021-03-09 10:50:47 +00:00
hasSent=
2022-08-18 18:05:52 +00:00
# see https://ntfy.sh
# You will need to create these two files containing the ntfy
# service url and topic
2022-08-19 09:15:37 +00:00
ntfy_url_file=/home/${USERNAME}/.ntfy_url
ntfy_topic_file=/home/${USERNAME}/.ntfy_topic
# get ntfy settings from the account directory
epicyon_config_file=${epicyonInstallDir}/config.json
if [ -f "${epicyon_config_file}" ]; then
epicyon_domain=$(cat "$epicyon_config_file" | awk -F '"domain": "' '{print $2}' | awk -F '"' '{print $1}')
if [ "${epicyon_domain}" ]; then
epicyon_account_dir="${epicyonInstallDir}/accounts/${USERNAME}@${epicyon_domain}"
if [ -d "${epicyon_account_dir}" ]; then
ntfy_url_file=${epicyon_account_dir}/.ntfy_url
ntfy_topic_file=${epicyon_account_dir}/.ntfy_topic
fi
fi
fi
2022-08-18 18:05:52 +00:00
if [ "$MESSAGE" ]; then
if [ -f "$ntfy_topic_file" ]; then
ntfy_topic=$(cat "$ntfy_topic_file")
if [ "$ntfy_topic" ]; then
if [ -f "$ntfy_url_file" ]; then
ntfy_url=$(cat "$ntfy_url_file")
else
# use the default service url
ntfy_url="ntfy.sh"
fi
curl_command='curl'
if [ -f /var/lib/tor/hidden_service_matrix/hostname ]; then
curl_command='torsocks curl'
fi
if [ ! "$SUBJECT" ]; then
SUBJECT="$PROJECT_NAME"
fi
${curl_command} -H "Title: ${SUBJECT}" -H "Priority: default" -H "Tags: loudspeaker" -d "${MESSAGE}" "${ntfy_url}/${ntfy_topic}"
hasSent=1
fi
fi
fi
2020-06-21 14:45:20 +00:00
if [ -d /etc/prosody ]; then
if [ -f /usr/bin/sendxmpp ]; then
2021-03-09 10:50:47 +00:00
# generate a random password for a temporary user account
2020-06-21 14:45:20 +00:00
notification_user_password=$(openssl rand -base64 32 | tr -dc A-Za-z0-9 | head -c 30 ; echo -n '')
2021-03-09 10:50:47 +00:00
# register a temporary xmpp user account to send the message
2020-06-21 14:45:20 +00:00
if prosodyctl register "notification" "$local_domain" "$notification_user_password"; then
if [[ "$SUBJECT" == *' Tor '* ]]; then
MESSAGE="$SUBJECT"
fi
if [ -f /usr/bin/sendxmpp ]; then
2021-03-09 10:50:47 +00:00
# kill any existing message which hasn't sent
2020-06-21 14:45:20 +00:00
kill_sendxmpp_process
2021-03-09 10:50:47 +00:00
# send the xmpp notification using the temporary account
2020-06-21 14:45:20 +00:00
echo "${MESSAGE}" | /usr/bin/sendxmpp -u notification -p "${notification_user_password}" -j localhost -o ${local_domain} --message-type=headline -n -t -s ${PROJECT_NAME} ${USERNAME}@${local_domain}
2021-03-09 10:50:47 +00:00
hasSent=1
2020-06-21 14:45:20 +00:00
fi
fi
2021-03-09 10:50:47 +00:00
# remove the temporary xmpp account
2020-06-21 14:45:20 +00:00
prosodyctl deluser "notification@$local_domain"
fi
fi
if [ -d /etc/matrix ]; then
matrix_server_message "${USERNAME}" "${USERNAME}" "$MESSAGE"
2021-03-09 10:50:47 +00:00
hasSent=1
fi
if [ ! "$hasSent" ]; then
if [[ "$MY_EMAIL_ADDRESS" != "username@domain" ]]; then
# send to a fixed email address for a single user instance
echo "$MESSAGE" | /usr/bin/mail -s "$SUBJECT" "$MY_EMAIL_ADDRESS"
fi
2020-06-21 14:45:20 +00:00
fi
2020-06-21 11:40:46 +00:00
}
function notifications {
# checks if DMs or replies have arrived and sends notifications to users
if [ ! -f "$epicyonInstallDir/config.json" ]; then
return
fi
if [ ! -f "${epicyonInstallDir}/config.json" ]; then
2021-03-09 10:50:47 +00:00
return
2020-06-21 11:40:46 +00:00
fi
# shellcheck disable=SC2002
EPICYON_DOMAIN_NAME=$(cat "${epicyonInstallDir}/config.json" | awk -F '"domain":' '{print $2}' | awk -F '"' '{print $2}')
for d in ${epicyonInstallDir}/accounts/*/ ; do
2021-03-09 10:50:47 +00:00
if [[ "$d" != *'@'* ]]; then
continue
fi
epicyonDir="${d::-1}"
USERNAME=$(echo "$epicyonDir" | awk -F '/' '{print $5}' | awk -F '@' '{print $1}')
2020-06-21 11:40:46 +00:00
# send notifications for calendar events to XMPP/email users
epicyonCalendarfile="$epicyonDir/.newCalendar"
if [ -f "$epicyonCalendarfile" ]; then
if ! grep -q "##sent##" "$epicyonCalendarfile"; then
2021-03-09 11:01:15 +00:00
epicyonCalendarmessage=$(notification_translate_text 'Calendar')
2020-06-21 11:40:46 +00:00
epicyonCalendarfileContent=$(echo "$epicyonCalendarmessage")" "$(cat "$epicyonCalendarfile")
if [[ "$epicyonCalendarfileContent" == '/calendar'* ]]; then
epicyonCalendarmessage="Epicyon: ${EPICYON_DOMAIN_NAME}/users/${USERNAME}${epicyonCalendarfileContent}"
fi
2021-03-09 10:50:47 +00:00
sendNotification "$USERNAME" "Epicyon" "$epicyonCalendarmessage"
2020-06-21 11:40:46 +00:00
echo "##sent##" >> "$epicyonCalendarfile"
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonCalendarfile"
fi
fi
# send notifications for DMs to XMPP/email users
epicyonDMfile="$epicyonDir/.newDM"
if [ -f "$epicyonDMfile" ]; then
if ! grep -q "##sent##" "$epicyonDMfile"; then
2021-03-09 11:01:15 +00:00
epicyonDMmessage=$(notification_translate_text 'DM')
2020-06-21 11:40:46 +00:00
epicyonDMfileContent=$(echo "$epicyonDMmessage")" "$(cat "$epicyonDMfile")
if [[ "$epicyonDMfileContent" == *':'* ]]; then
epicyonDMmessage="Epicyon: $epicyonDMfileContent"
fi
2021-03-09 10:50:47 +00:00
sendNotification "$USERNAME" "Epicyon" "$epicyonDMmessage"
2020-06-21 11:40:46 +00:00
echo "##sent##" > "$epicyonDMfile"
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonDMfile"
fi
fi
2020-07-08 20:28:54 +00:00
# send notifications for likes to XMPP/email users
epicyonLikeFile="$epicyonDir/.newLike"
if [ -f "$epicyonLikeFile" ]; then
if ! grep -q "##sent##" "$epicyonLikeFile"; then
2021-03-09 11:01:15 +00:00
epicyonLikeMessage=$(notification_translate_text 'Liked by')
2020-07-08 20:28:54 +00:00
epicyonLikeFileContent=$(cat "$epicyonLikeFile" | awk -F ' ' '{print $1}')" "$(echo "$epicyonLikeMessage")" "$(cat "$epicyonLikeFile" | awk -F ' ' '{print $2}')
if [[ "$epicyonLikeFileContent" == *':'* ]]; then
epicyonLikeMessage="Epicyon: $epicyonLikeFileContent"
fi
2021-11-14 20:42:27 +00:00
sendNotification "$USERNAME" "Epicyon" "$epicyonLikeMessage"
2020-07-08 20:28:54 +00:00
echo "##sent##" > "$epicyonLikeFile"
2021-03-09 15:01:35 +00:00
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonLikeFile"
2020-07-08 20:28:54 +00:00
fi
fi
2021-11-10 19:13:23 +00:00
# send notifications for emoji reactions to XMPP/email users
2021-11-14 20:42:27 +00:00
epicyonReactionFile="$epicyonDir/.newReaction"
2021-11-10 19:13:23 +00:00
if [ -f "$epicyonReactionFile" ]; then
if ! grep -q "##sent##" "$epicyonReactionFile"; then
epicyonReactionMessage=$(notification_translate_text 'Reaction by')
epicyonReactionFileContent=$(cat "$epicyonReactionFile" | awk -F ' ' '{print $1}')" "$(echo "$epicyonReactionMessage")" "$(cat "$epicyonReactionFile" | awk -F ' ' '{print $2}')
if [[ "$epicyonReactionFileContent" == *':'* ]]; then
epicyonReactionMessage="Epicyon: $epicyonReactionFileContent"
fi
2021-11-14 20:42:27 +00:00
sendNotification "$USERNAME" "Epicyon" "$epicyonReactionMessage"
2021-11-10 19:13:23 +00:00
echo "##sent##" > "$epicyonReactionFile"
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonReactionFile"
fi
fi
# send notifications for posts arriving from a particular person
epicyonNotifyFile="$epicyonDir/.newNotifiedPost"
if [ -f "$epicyonNotifyFile" ]; then
if ! grep -q "##sent##" "$epicyonNotifyFile"; then
2021-07-06 21:53:55 +00:00
epicyonNotifyMessage=$(notification_translate_text 'New post')
epicyonNotifyFileContent=$(echo "$epicyonNotifyMessage")" "$(cat "$epicyonNotifyFile")
if [[ "$epicyonNotifyFileContent" == *':'* ]]; then
2021-07-06 21:53:55 +00:00
epicyonNotifyMessage="Epicyon: $epicyonNotifyFileContent"
fi
2021-07-06 21:53:55 +00:00
sendNotification "$USERNAME" "Epicyon" "$epicyonNotifyMessage"
echo "##sent##" > "$epicyonNotifyFile"
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonNotifyFile"
fi
fi
2020-06-21 11:40:46 +00:00
# send notifications for replies to XMPP/email users
epicyonReplyFile="$epicyonDir/.newReply"
if [ -f "$epicyonReplyFile" ]; then
if ! grep -q "##sent##" "$epicyonReplyFile"; then
2021-03-09 11:01:15 +00:00
epicyonReplyMessage=$(notification_translate_text 'Replies')
2020-06-21 11:40:46 +00:00
epicyonReplyFileContent=$(echo "$epicyonReplyMessage")" "$(cat "$epicyonReplyFile")
if [[ "$epicyonReplyFileContent" == *':'* ]]; then
epicyonReplyMessage="Epicyon: $epicyonReplyFileContent"
fi
2021-03-09 10:50:47 +00:00
sendNotification "$USERNAME" "Epicyon" "$epicyonReplyMessage"
2020-06-21 11:40:46 +00:00
echo "##sent##" > "$epicyonReplyFile"
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonReplyFile"
fi
fi
# send notifications for git patches to XMPP/email users
epicyonPatchFile="$epicyonDir/.newPatch"
if [ -f "$epicyonPatchFile" ]; then
if [ -f "${epicyonPatchFile}Content" ]; then
2021-03-09 10:50:47 +00:00
if ! grep -q "##sent##" "$epicyonPatchFile"; then
epicyonPatchMessage=$(cat "$epicyonPatchFile")
if [ "$epicyonPatchMessage" ]; then
# notify the member
sendNotification "$USERNAME" "Epicyon" "$epicyonPatchMessage"
echo "##sent##" > "$epicyonPatchFile"
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonPatchFile"
# send the patch to them by email
cat "${epicyonPatchFile}Content" | mail -s "[Epicyon] $epicyonPatchMessage" "${USERNAME}@${HOSTNAME}"
rm "${epicyonPatchFile}Content"
fi
fi
2020-06-21 11:40:46 +00:00
fi
fi
# send notifications for new shared items to XMPP/email users
epicyonShareFile="$epicyonDir/.newShare"
if [ -f "$epicyonShareFile" ]; then
if ! grep -q "##sent##" "$epicyonShareFile"; then
2021-03-09 11:01:15 +00:00
epicyonShareMessage=$(notification_translate_text 'Shares')
2020-06-21 11:40:46 +00:00
epicyonShareFileContent=$(echo "$epicyonShareMessage")" "$(cat "$epicyonShareFile")
if [[ "$epicyonShareFileContent" == *':'* ]]; then
epicyonShareMessage="Epicyon: $epicyonShareFileContent"
fi
2021-03-09 10:50:47 +00:00
sendNotification "$USERNAME" "Epicyon" "$epicyonShareMessage"
2020-06-21 11:40:46 +00:00
echo "##sent##" > "$epicyonShareFile"
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonShareFile"
fi
fi
2021-08-09 18:41:05 +00:00
# send notifications for new wanted items to XMPP/email users
epicyonWantedFile="$epicyonDir/.newWanted"
if [ -f "$epicyonWantedFile" ]; then
if ! grep -q "##sent##" "$epicyonWantedFile"; then
epicyonWantedMessage=$(notification_translate_text 'Wanted')
epicyonWantedFileContent=$(echo "$epicyonWantedMessage")" "$(cat "$epicyonWantedFile")
if [[ "$epicyonWantedFileContent" == *':'* ]]; then
epicyonWantedMessage="Epicyon: $epicyonWantedFileContent"
fi
sendNotification "$USERNAME" "Epicyon" "$epicyonWantedMessage"
echo "##sent##" > "$epicyonWantedFile"
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonWantedFile"
fi
fi
2020-06-21 11:40:46 +00:00
# send notifications for follow requests to XMPP/email users
epicyonFollowFile="$epicyonDir/followrequests.txt"
epicyonFollowNotificationsFile="$epicyonDir/follownotifications.txt"
if [ -f "$epicyonFollowFile" ]; then
if [ -s "$epicyonFollowFile" ]; then
epicyonNotify=
if [ -f "$epicyonFollowNotificationsFile" ]; then
hash1=$(sha256sum "$epicyonFollowFile" | awk -F ' ' '{print $1}')
hash2=$(sha256sum "$epicyonFollowNotificationsFile" | awk -F ' ' '{print $1}')
if [[ "$hash1" != "$hash2" ]]; then
epicyonNotify=1
fi
else
epicyonNotify=1
fi
if [ $epicyonNotify ]; then
cp "$epicyonFollowFile" "$epicyonFollowNotificationsFile"
chown ${PROJECT_NAME}:${PROJECT_NAME} "$epicyonFollowNotificationsFile"
2021-03-09 10:50:47 +00:00
2021-03-09 11:01:15 +00:00
epicyonFollowMessage=$(notification_translate_text 'Approve follower requests')" ${EPICYON_DOMAIN_NAME}/users/${USERNAME}/followers"
2021-03-09 10:50:47 +00:00
sendNotification "$USERNAME" "Epicyon" "$epicyonFollowMessage"
2020-06-21 11:40:46 +00:00
fi
fi
fi
2021-03-09 10:50:47 +00:00
done
2020-06-21 11:40:46 +00:00
}
2020-06-21 14:45:20 +00:00
notifications