290 lines
7.5 KiB
Bash
Executable File
290 lines
7.5 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
BOTNICK="uugbot"
|
|
SERVER="irc.uugrn.org"
|
|
PORT="6670"
|
|
CHANNEL="#uugrn"
|
|
|
|
BOTDIR="$PWD"
|
|
|
|
IRCIN="$BOTDIR/irc/$SERVER/$CHANNEL/in"
|
|
IRCOUT="$BOTDIR/irc/$SERVER/$CHANNEL/out"
|
|
|
|
###
|
|
### FUNCTIONS ###
|
|
###
|
|
|
|
# $1 message
|
|
say() {
|
|
echo "out> $1"
|
|
echo "$1" > $IRCIN
|
|
sleep 1
|
|
}
|
|
|
|
settopic() {
|
|
curl -s "https://vorstand.uugrn.org/Kalender/Termine.rem" \
|
|
| remind -b1 -n - \
|
|
| sort \
|
|
| grep -vi vorstand \
|
|
| head -1 \
|
|
| while read -r line
|
|
do
|
|
date=$(echo "$line" \
|
|
| cut -d" " -f 1 \
|
|
| sed 's,\(....\)/\(..\)/\(..\),\3.\2.\1,g')
|
|
|
|
time=$(echo "$line" \
|
|
| cut -d" " -f 2)
|
|
|
|
text=$(echo "$line" \
|
|
| cut -d" " -f 3- \
|
|
| cut -d"|" -f1 \
|
|
| sed 's/ $//g')
|
|
|
|
say "/topic $CHANNEL :Willkommen bei der UUGRN | Nächste Veranstaltung: $text am $date um $time Uhr."
|
|
done
|
|
}
|
|
|
|
kalender() {
|
|
case "$1" in
|
|
[12345]) NUM=$1; ;;
|
|
*) say "Dieses Argument verstehe ich nicht (ignored)."; NUM=1; ;;
|
|
esac
|
|
curl -s "https://vorstand.uugrn.org/Kalender/Termine.rem" \
|
|
| remind -b1 -n - \
|
|
| sort \
|
|
| head -$NUM \
|
|
| while read -r line
|
|
do
|
|
date=$(echo "$line" \
|
|
| cut -d" " -f 1 \
|
|
| sed 's,\(....\)/\(..\)/\(..\),\3.\2.\1,g')
|
|
|
|
time=$(echo "$line" \
|
|
| cut -d" " -f 2)
|
|
|
|
text=$(echo "$line" \
|
|
| cut -d" " -f 3- \
|
|
| cut -d"|" -f1 \
|
|
| sed 's/ $//g')
|
|
|
|
loc=$(echo "$line" \
|
|
| cut -d" " -f 3- \
|
|
| cut -d"|" -f 2-)
|
|
|
|
if echo "$line" | fgrep -q ' |'
|
|
then
|
|
say "Am $date um $time Uhr findet \"$text\" im \"$loc\" statt."
|
|
else
|
|
say "Am $date um $time Uhr findet \"$text\" statt."
|
|
fi
|
|
done
|
|
}
|
|
|
|
# $1 nick
|
|
# $2 -1 decrease, +1 increase
|
|
savekarma() {
|
|
[ ! -f "$BOTDIR/karma/$1" ] \
|
|
&& echo 0 > "$BOTDIR/karma/$1"
|
|
|
|
old=$(cat "$BOTDIR/karma/$1")
|
|
new=$(( old $2 ))
|
|
echo $new > "$BOTDIR/karma/$1"
|
|
say "$1 hat nun $new Gummipunkt(e)"
|
|
}
|
|
|
|
# $1 nick
|
|
reportkarma() {
|
|
n=$1
|
|
[ -z "$n" ] \
|
|
&& n=$NICK
|
|
|
|
if [ ! -f "$BOTDIR/karma/$n" ]
|
|
then
|
|
[ "$n" == "$NICK" ] \
|
|
&& say "Du hast bisher leider noch keine Gummipunkte gesammelt, $n." \
|
|
|| say "$n hat leider noch keine Gummipunkte gesammelt."
|
|
else
|
|
cur=$(cat "$BOTDIR/karma/$n")
|
|
[ "$n" == "$NICK" ] \
|
|
&& say "Du hast momentan $cur Gummipunkt(e), $n." \
|
|
|| say "$n hat momentan $cur Gummipunkt(e)."
|
|
fi
|
|
}
|
|
|
|
karma() {
|
|
echo "karma: $1"
|
|
n="$(echo "$1" | tr -d "+-")"
|
|
[ -z "$n" ] && return
|
|
|
|
[ "$n" == "$(echo "$NICK" | tr '[:upper:]' '[:lower:]' )" ] \
|
|
&& say "So aber nicht mein lieber $NICK!" && return
|
|
|
|
echo "$1" | fgrep -q -- "++" \
|
|
&& savekarma $n "+1"
|
|
echo "$1" | fgrep -q -- "--" \
|
|
&& savekarma $n "-1"
|
|
echo "$1" | fgrep -q -- "+-" \
|
|
&& say "Echt jetzt, $NICK?"
|
|
echo "$1" | fgrep -q -- "-+" \
|
|
&& say "Echt jetzt, $NICK?"
|
|
}
|
|
transparenzbericht() {
|
|
cd $BOTDIR/karma
|
|
say "Gummipunkte Transparenzbericht:"
|
|
for i in $(ls)
|
|
do
|
|
say "$i: $(cat "$i") Gummipunkte"
|
|
done
|
|
}
|
|
|
|
seen() {
|
|
[ -z "$1" ] && return
|
|
if [ -f "$BOTDIR/seen/$1" ]
|
|
then
|
|
_l="$(cat "$BOTDIR/seen/$1")"
|
|
_ts="$(echo "$_l" | cut -d" " -f1)"
|
|
_msg="$(echo "$_l" | cut -d" " -f2-)"
|
|
_dt="$(date -r "$_ts" +"%Y-%m-%d %H:%M")"
|
|
if [ -z $_dt ]
|
|
then
|
|
say "$_l"
|
|
else
|
|
say "$_dt: $_msg"
|
|
fi
|
|
else
|
|
say "Hmm, ich habe keine Ahnung wann $1 zum letzten mal online war."
|
|
fi
|
|
}
|
|
|
|
usage() {
|
|
say "!kalender <anzahl> - zeigt die nächsten Termine im UUGRN Kalender (max 5)"
|
|
say "!wetter <ort> - zeigt das Wetter an <ort> oder Heidelberg"
|
|
say "!punkte <nick> - zeigt den Gummipunktlevel von <nick> oder dir selbst an"
|
|
say "!transparenzbericht - zeigt den Gummipunkte Transparenzbericht an"
|
|
say "<nick>++ - vergibt einen Gummipunkt an <nick>"
|
|
say "<nick>-- - nimmt <nick> einen Gummipunkt weg"
|
|
say "!seen <nick> - zeigt die letzte Nachricht von <nick> an"
|
|
say "!settopic - setzt das topic mit dem nächsten Event im Kalender"
|
|
say "!hilfe - zeigt diesen hilfetext an"
|
|
|
|
}
|
|
|
|
# $1 loc
|
|
weather() {
|
|
param=$1
|
|
|
|
[ -z $param ] \
|
|
&& param=Heidelberg,DE
|
|
|
|
echo "$param" | fgrep -q "," \
|
|
|| param="$param,DE"
|
|
|
|
|
|
DATA=$(curl -s "https://api.openweathermap.org/data/2.5/weather?q=$param&lang=de&units=metric&appid=a4c6e6d6beba2ff733ed81b7e2cac530")
|
|
#DATA=$(cat test.json)
|
|
|
|
RESPONSE=$(echo "$DATA" \
|
|
| jq -rc '.name,[.main.temp, .weather[].description][], .main.humidity, .wind.speed' 2> /dev/null \
|
|
| awk ' \
|
|
NR == 1 { printf("Wetter in %s: ", $0) } \
|
|
NR == 2 { printf("%.0d°C, ", $0) } \
|
|
NR == 3 { printf("%s", $0) } \
|
|
NR == 4 { printf(" (%d%%), ", $0) } \
|
|
NR == 5 { printf("Wind: %.0dkm/h", $0) } \
|
|
')
|
|
|
|
case "$RESPONSE" in
|
|
*null:*) RESPONSE="$param konnte nicht gefunden werden. Usage: !wetter <stadt>[,<länderkürzel>]"
|
|
esac
|
|
|
|
say "$RESPONSE"
|
|
|
|
}
|
|
|
|
|
|
|
|
###
|
|
### MAIN PROGRAM ###
|
|
###
|
|
|
|
mkdir -p "$BOTDIR/irc"
|
|
mkdir -p "$BOTDIR/seen"
|
|
mkdir -p "$BOTDIR/karma"
|
|
cd "$BOTDIR"
|
|
|
|
# Connect ii and join channel. Afterwards $IRCIN and $IRCOUT are
|
|
# available
|
|
|
|
echo "connecting."
|
|
ii -i $BOTDIR/irc -s $SERVER -t -p $PORT -n $BOTNICK &
|
|
PID=$!
|
|
|
|
# wait until the background process is ready
|
|
sleep 6
|
|
echo "/join $CHANNEL" > $BOTDIR/irc/$SERVER/in
|
|
sleep 1
|
|
echo "ready."
|
|
|
|
# Listen on $IRCOUT, parse input and call functions once something
|
|
# of interest gets posted.
|
|
tail -n 0 -f "$IRCOUT" \
|
|
| while read -r line
|
|
do
|
|
NICK=$(echo "$line" \
|
|
| sed -n 's/^....-..-.. ..:.. <\([^>]*\).*/\1/p')
|
|
|
|
if [ -z "$NICK" ]
|
|
then
|
|
|
|
# the newer ii is using unix timestamps
|
|
NICK=$(echo "$line" \
|
|
| sed -n 's/^[0-9]* <\([^>]*\).*/\1/p')
|
|
fi
|
|
|
|
# prevent loops; so the bot can't trigger itself.
|
|
[ "$NICK" == "$BOTNICK" ] \
|
|
&& continue
|
|
|
|
# if we still have no nick here, we better stop to prevent excuting
|
|
# events from non-users (actions, IRC events etc...)
|
|
[ -z "$NICK" ] \
|
|
&& continue
|
|
|
|
COMMAND=$(echo "$line" \
|
|
| tr '[:upper:]' '[:lower:]' \
|
|
| sed -n 's|.*\(![a-z]*\).*|\1|p')
|
|
|
|
# intentionally limiting the argument to one and also limit the
|
|
# character set for safety reasons.
|
|
ARG=$(echo "$line" \
|
|
| tr '[:upper:]' '[:lower:]' \
|
|
| sed -n 's|.*![a-z]* \([a-zA-Z0-9,\-_+]*\).*|\1|p')
|
|
|
|
# We call it gummipunkte now.
|
|
KARMA=$(echo "$line" \
|
|
| tr '[:upper:]' '[:lower:]' \
|
|
| sed -n 's|.* \([a-zA-Z0-9\-_]*[+-][+-]\)$|\1|p;s|.* \([a-zA-Z0-9\-_]*[+-][+-]\) .*|\1|p')
|
|
|
|
echo "in> $line"
|
|
echo "in> NICK=$NICK, COMMAND=$COMMAND, ARG=$ARG, KARMA=$KARMA"
|
|
|
|
# we just save the line and replay it on the !seen command
|
|
[ ! -z "$NICK" ] \
|
|
&& echo "$line" > "$BOTDIR/seen/$NICK"
|
|
|
|
case "$COMMAND" in
|
|
"!kalender") kalender $ARG; ;;
|
|
"!punkte") reportkarma $ARG; ;;
|
|
"!transparenzbericht") transparenzbericht; ;;
|
|
"!wetter") weather $ARG; ;;
|
|
"!hilfe") usage; ;;
|
|
"!settopic") settopic; ;;
|
|
"!seen") seen $ARG; ;;
|
|
esac
|
|
|
|
case "$KARMA" in
|
|
*[+-][+-]) karma $KARMA; ;;
|
|
esac
|
|
done
|