Client/Bouncer

This commit is contained in:
Neale Pickett 2012-01-07 01:10:49 -06:00
parent ffa7d64249
commit b90f1739f2
17 changed files with 165 additions and 351 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
*~ *~
*.o *.o
dispatch dispatch
irc irc-filter
irc-esc

24
bot
View File

@ -1,24 +0,0 @@
#! /bin/sh -e
botdir=$1
d=$(dirname $0)
nickname=$(cat $botdir/nickname)
realname=$(cat $botdir/realname 2>/dev/null || \
echo "I'm a little printf, short and stdout.")
export nickname realname
(
# UCSPI wants input on FD 7, and sets $PROTO
[ -n "$PROTO" ] && exec 1>&7
if [ -x $botdir/login ]; then
$botdir/login
else
echo "NICK $nickname"
echo "USER $nickname $nickname $nickname :$realname"
fi
)
mkfifo -m 0600 $botdir/fifo
exec $d/dispatch -f $botdir/fifo $d/irc $botdir/handler

View File

@ -1,3 +0,0 @@
#! /bin/sh
exec socat OPENSSL:127.0.0.1:6697,verify=0,keepalive EXEC:"$*"

View File

@ -1,83 +0,0 @@
#! /bin/sh
prefix=$1; export prefix; shift
command=$1; export command; shift
sender=$1; export sender; shift
forum=$1; export forum; shift
text=$1; export text; shift
# $* is now args
# Debug output
#echo '>>>' ${prefix:+:}$prefix $command "$@" ${text:+:}"$text" 1>&2
raw () {
fmt="\007$1\n"; shift
printf "$fmt" "$@"
}
join () {
raw "JOIN $1"
}
cobalt () {
case "$1" in
air)
w3m -dump -cols 9999 'http://environweb.lanl.gov/Teom/teom30s.asp?MasterSiteID=211&offset=0' 2> /dev/null | \
awk '/.:..:.. .M/ {print "Los Alamos Air: " $5 "μg/m³ at " $2; exit}'
;;
nachos)
echo "aieeeee"
;;
*)
return 1
;;
esac
}
out=$(tempfile)
case $command in
001)
for i in $botdir/channels/*; do
join $(basename $i)
done
;;
433)
raw "NICK bot_$(shuf -c 1 /usr/share/dict/words)"
;;
PRIVMSG)
case "$forum" in
\#*)
cobalt "$text" || \
$ircdir/firebot "$text" || \
$ircdir/whuffie $botdir/whuffie.cdb "$text" || \
$ircdir/infobot $botdir/factoids.cdb "$text"
;;
esac
$ircdir/notes $botdir/notes "$text"
;;
INVITE)
join "$forum"
raw "PRIVMSG %s :Thanks for the invitation, %s." "$forum" "$sender"
touch $botdir/channels/$forum
;;
esac > $out
if [ "$forum" != "$sender" ] && [ $(wc -l < $out) -gt 5 ]; then
printf "PRIVMSG %s :Too many lines, sending privately.\r\n" "$forum"
forum=$sender
fi
while read -r line; do
case "$line" in
*)
printf "%s\r\n" "${line#}"
;;
*)
printf "PRIVMSG %s :%s\r\n" "$forum" "$line"
;;
esac
done < $out
rm -f $out

View File

@ -1,4 +0,0 @@
#! /bin/sh
echo "NICK cobalt"
echo "USER cobalt cobalt cobalt :I'm a little printf, short and stdout."

3
connect Executable file
View File

@ -0,0 +1,3 @@
#! /bin/sh
exec socat TCP:"$server":6667 EXEC:"$*"

View File

@ -1,9 +1,5 @@
#! /bin/sh -e #! /bin/sh -e
botdir=$1
ircdir=$(dirname $0)
export botdir ircdir
# UCSPI wants communication on fd7 # UCSPI wants communication on fd7
if [ -n "$PROTO" ]; then if [ -n "$PROTO" ]; then
fd=7 fd=7
@ -11,10 +7,12 @@ else
fd=1 fd=1
fi fi
$botdir/login 1>& $fd # Kick things off by logging in
if [ -x "$srvdir/login" ]; then
$srvdir/login
else
echo NICK $nick
echo USER $nick $nick $nick :$nick
fi 1>& $fd
if [ -p $botdir/fifo ]; then exec $ircdir/dispatch -f fifo $ircdir/irc-filter $ircdir/handler
fifo="-f $botdir/fifo"
fi
exec $ircdir/dispatch $fifo $ircdir/irc-filter $botdir/handler

23
firebot
View File

@ -1,23 +0,0 @@
#! /bin/sh
exec 2>&1
cmd=${1%% *}
[ "$cmd" = "$1" ] || args=${1#* }
case $cmd in
calc)
printf "%s = " "$args"
echo "$args" | bc -l
;;
units)
src=$(printf "%s" "$args" | sed 's/ ->.*//')
dst=$(printf "%s" "$args" | sed 's/.*-> //')
units -1 -v -- "$src" "$dst"
;;
*)
exit 1
;;
esac

55
handler Executable file
View File

@ -0,0 +1,55 @@
#! /bin/sh
prefix=$1; export prefix; shift
command=$1; export command; shift
sender=$1; export sender; shift
forum=$1; export forum; shift
text=$1; export text; shift
# $* is now args
mkdir -p $srvdir/$forum
now=$(date '+%Y-%m-%d %H:%M'); export now
(
flock 3
for h in $srvdir/handler $srvdir/$forum/handler; do
[ -x $h ] && $h "$@"
done
[ -x $srvdir/$command-hook ] && $srvdir/$command-hook
case $command in
PING)
;;
001)
for chan in \#* \&* \+*; do
if [ -d "$chan" ]; then
echo "JOIN $chan"
fi
done
;;
433)
echo "NICK u$$"
;;
PRIVMSG)
case "$text" in
"ACTION "*)
txt=${text#ACTION }
txt=${txt%}
echo "$now * $sender $txt" 1>&3
;;
*)
txt=${text#}
txt=${txt%}
echo "$now - $sender CTCP $txt" 1>&3
;;
*)
echo "$now <$sender> $text" 1>&3
;;
esac
;;
*)
echo "$now" ${prefix:+:}$prefix $command "$@" ${text:+:}"$text" 1>&3
;;
esac
) 3>> $srvdir/${forum:-.}/log

90
infobot
View File

@ -1,90 +0,0 @@
#! /bin/sh
db=$1; shift
text="$1"
[ -f $db ] || echo | cdb -c $db
lookup () {
if ! cdb -q -m $db "$1"; then
t="$1"
while [ "$t" != "$n" ]; do
n=$t
t=${n%[?!. ]}
done
if [ "$t" != "$1" ]; then
cdb -q -m $db "$t"
fi
fi
}
db_append () {
(printf "+%d,%d:%s->%s\n" ${#1} ${#2} "$1" "$2"; cdb -d $db) | cdb -c $db
echo "Okay, $sender, I added a factoid to $1."
}
nickname=${nickname:-infobot}
args=${text#* }
case "$text" in
!h*)
cat <<EOF
!stats Show statistics
!list KEY List all factoids stored for KEY
!append KEY += VALUE Add VALUE to KEY's factoids
!remove KEY -= VALUE Remove *VALUE* from KEY's factoids
!forget KEY Remove all factoids for KEY
EOF
;;
!s*)
cdb -s $db | head -n 1
;;
!l*)
printf "factoids for \"%s\": " "$args"
cdb -q -m $db "$args" | awk '{printf("|%s", $0);}'
echo
;;
$nickname:\ *\ is\ *)
s=${text#$nickname: }
db_append "${s%% is *}" "${s#* is }"
;;
!a*)
db_append "${args% +=*}" "${args#*+= }"
;;
!r*)
key=${args% -=*}
val=${args#*-= }
re=":$key->.*$val"
n=$(cdb -d $db | grep -c "$re")
if [ $n -gt 0 ]; then
cdb -d $db | grep -a -v ":$key->.*$val" | cdb -c $db
echo "Okay, $sender, I removed $n factoids from $key"
else
echo "Nothing matched, $sender."
fi
;;
!forget\ *)
cdb -d $db | grep -a -F -v ":$args->" | cdb -c $db
echo "I removed all factoids from $args"
;;
*)
resp=$(lookup "$text" | shuf -n 1 | sed "s/\$sender/$sender/")
case "$resp" in
"")
exit 1
;;
''*)
echo "Someone's up to no good!"
;;
'\'*)
printf "%s\n" "${resp#\\}"
;;
:*)
printf '\001ACTION %s\001\n' "${resp#:}"
;;
*)
echo "It's been said that $text is $resp"
;;
esac
;;
esac

45
irc Executable file
View File

@ -0,0 +1,45 @@
#! /bin/sh -e
if [ $# -gt 1 ]; then
nick=$1
shift
else
nick=$LOGNAME
fi
export nick
if [ $# -ne 1 ]; then
exec 1>&2
echo "Usage: $0 BOTDIRECTORY"
exit 1
fi
server=$1
ircdir=$(cd $(dirname $0); pwd)
export server ircdir
if [ -d "$server" ]; then
cd $server
else
mkdir -p $HOME/chat/$server
cd $HOME/chat/$server
fi
srvdir=$(pwd)
export srvdir
mkdir -p $srvdir
if [ ! -p $srvdir/fifo ]; then
mkfifo $srvdir/fifo
fi
if [ -x $srvdir/connect ]; then
connect=$srvdir/connect
else
connect=$ircdir/connect
fi
while true; do
$connect $ircdir/connect-handler $srvdir
sleep 5
done

52
m Executable file
View File

@ -0,0 +1,52 @@
#! /bin/sh
if [ ! -p ../fifo ]; then
echo "You need to be in a forum directory."
exit
fi
forum=${PWD##*/}
nick=${nick:-?}
privmsg () {
(
flock 1
now=$(date '+%Y-%m-%d %H:%M')
case "$1" in
//*)
printf "PRIVMSG %s :%s\n" "$forum" "${1#/}" > ../fifo
echo "$now <$nick> ${1#/}"
;;
\:*)
printf "PRIVMSG %s :\001ACTION %s\001\n" "$forum" "${1#:}" > ../fifo
echo "$now * $nick ${1#:}"
;;
/*)
cmd=${1%% *}
args=${1#* }
case "${1%% *}" in
/msg)
echo "PRIVMSG ${args%% *} :${args#* }" > ../fifo
;;
*)
echo "${1#/}" > ../fifo
;;
esac
;;
*)
printf "PRIVMSG %s :%s\n" "$forum" "$1" > ../fifo
echo "$now <$nick> $1"
;;
esac
) >> log
}
if [ "$*" ]; then
privmsg "$*"
else
while true; do
printf "$forum: "
read -r msg || break
privmsg "$msg"
done
fi

27
notes
View File

@ -1,27 +0,0 @@
#! /bin/sh
db=$1; shift
text=$1
lc () {
printf "%s" "$1" | tr A-Z a-z
}
sender=$(lc "$sender")
if [ -f $db/$sender ]; then
echo "Welcome back, $sender. Your messages:"
cat $db/$sender
rm $db/$sender
fi
case "$text" in
note\ *)
args=${text#note }
who=$(lc "${args%% *}")
what=${args#* }
when=$(date)
echo "($when) <$prefix> $what" >> $db/$who
echo "Okay, $sender, I've left $who a note."
;;
esac

View File

@ -1,29 +0,0 @@
#!/usr/bin/env python
import re
import sys
import random
if __name__ == '__main__':
roll = sys.argv[1]
m = re.match('^(?P<rolls>\d+)d(?P<sides>\d+)(x(?P<multiplier>\d+))?$', roll)
if m:
rolls = int(m.group('rolls'))
sides = int(m.group('sides'))
if m.group('multiplier'):
multiplier = int(m.group('multiplier'))
else:
multiplier = 1
dice = []
acc = 0
for i in range(rolls):
n = random.randint(1, sides)
dice.append(n)
acc += n
acc *= multiplier
if rolls > 1:
print '%s: %d %r' % (roll, acc, dice)
else:
print '%s: %d' % (roll, acc)

16
run
View File

@ -1,16 +0,0 @@
#! /bin/sh -e
botdir=$1
ircdir=$(dirname $0)
export botdir ircdir
if [ ! -d "$botdir" ]; then
exec 1>&2
echo "Usage: $0 BOTDIRECTORY"
exit 1
fi
while true; do
$botdir/connect $ircdir/connect-handler $botdir
sleep 5
done

41
whuffie
View File

@ -1,41 +0,0 @@
#! /bin/sh
db=$1; shift
text="$1"
get () {
cdb -q $db "$1" || echo 0
}
put () {
(printf "+%d,%d:%s->%s\n" ${#1} ${#2} "$1" $2;
cdb -d $db) | cdb -c -u $db
}
adj () {
who=${text%%$1$1*}
if [ "$who" = "$sender" ]; then
echo "Nice try, $sender."
else
put "$who" $(expr $(get "$who") $1 1)
fi
}
case "$text" in
whuffie\ *)
who=${text#whuffie }
amt=$(get "$who")
echo "$who has whuffie score of $amt"
;;
*++|*++\ *)
adj +
;;
*--|*--\ *)
adj -
;;
*)
exit 1
;;
esac