commit 7f04dc4e2f2cddb207ea9a914bcdfdbff3dcafe4
parent c8b433b38842510c45715c54bea5768177e198a1
Author: hhvn <dev@hhvn.uk>
Date: Mon, 28 Feb 2022 19:39:31 +0000
Reimplement moar
Diffstat:
M | handler.rc | | | 103 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- |
1 file changed, 87 insertions(+), 16 deletions(-)
diff --git a/handler.rc b/handler.rc
@@ -1,8 +1,11 @@
#!/bin/rc
+#
+# uses GNU timeout(1), because lazy.
-LINE = `$nl{echo $LINE | tr -d '\r'}
+mkdir -p data/locks
# parameterize
+LINE = `$nl{echo $LINE | tr -d '\r'}
PARAMS = `' '$nl{echo $LINE | sed 's/ :.*//'}
PARAMS = ($PARAMS `$nl{echo $LINE | sed 's/.* ://'})
@@ -11,13 +14,28 @@ fn match_raw {
}
fn match_msg {
- if (!~ $PARAMS(1) 'PRIVMSG')
+ if (!~ $PARAMS(1) 'PRIVMSG') {
return 1;
- CHANNEL = $PARAMS(2)
- MSG = $PARAMS(3)
+ }
+ if (~ $PARAMS(2) $nick) {
+ TARGET = $NICK
+ } else {
+ TARGET = $PARAMS(2)
+ }
+ MSG = $PARAMS(3)
echo $PARAMS(3) | grep $* >/dev/null
}
+fn match_hilight {
+ if (~ $PARAMS(1) 'PRIVMSG' && match_msg -iE '^' ^ $nick ^ '[:,]|[[:punct:]] *' ^ $nick ^ '$') {
+ # TARGET already set by match_msg
+ MSG = `$nl{echo $PARAMS(3) | sed 's/^' ^ $nick ^ '[:,] //;s/[[:punct:]] *' ^ $nick ^'$//'}
+ echo $MSG | grep $* >/dev/null
+ } else {
+ return 1
+ }
+}
+
fn send_raw {
printf $* >> $input
}
@@ -30,23 +48,76 @@ fn send_msg {
send_raw 'PRIVMSG %s :%s\r\n' $1 $2
}
+fn lock {
+ echo `{date +%s} ^ ' + ' ^ $2 | bc > data/locks/$1
+}
+
+fn unlock {
+ rm -f data/locks/$1
+}
+
+fn islocked {
+ for (f in data/locks/*) {
+ d = `{cat $f}
+ if (expr $d '<' `{date +%s}) {
+ rm $f
+ }
+ }
+ test -f data/locks/$1
+}
+
+fn randomize {
+ n = `{awk -vmax=$#* -vmin=1 'BEGIN{srand(); print int(min+rand()*(max))}'}
+ echo $*($n)
+}
+
if (~ $PARAMS(1) 'PING') {
send_raw 'PONG :%s\r\n' $PARAMS(2)
} else if (~ $PARAMS(1) '376') {
# end of MOTD
send_join '#hlircnet'
send_join '#test'
-} else if (match_msg -iE '^' ^ $nick ^ '[:,] welcome back|welcome back, ' ^ $nick) {
- send_msg $CHANNEL 'Thanks, ' ^ $NICK
-} else if (match_msg -iE '^' ^ $nick ^ '[:,] say hi to') {
- send_msg $CHANNEL 'Hi' ^ `$nl{echo $MSG | sed 's/.*say hi to//'}
-} else if (match_msg -iE '^' ^ $nick ^ '[:,] say') {
- send_msg $CHANNEL `$nl{echo $MSG | sed 's/[^ ]* [^ ]* *//'}
-} else if (match_msg -iE '^' ^ $nick ^ '[:,] topic') {
+} else if (match_hilight -iE '^(calc|bc) ') {
+ exp = `$nl{echo $MSG | sed 's/[^ ]* *//'}
+ if (islocked bc) {
+ send_msg $TARGET 'bc is currently locked'
+ } else {
+ lock bc 6
+ ret = `$nl{echo $exp | timeout 5 bc >[2=1] | head -n5}
+ for (r in $ret) {
+ send_msg $TARGET $r
+ }
+ unlock bc
+ }
+} else if (match_hilight -i '^say hi to') {
+ send_msg $TARGET 'Hi' ^ `$nl{echo $MSG | sed 's/.*say hi to//'}
+} else if (match_hilight -i '^say') {
+ send_msg $TARGET `$nl{echo $MSG | sed 's/[^ ]* *//'}
+} else if (match_hilight -i '^topic') {
send_raw 'TOPIC #hlircnet :--== #hlircnet ==-- ::: --== gopher://hlirc.net/0/metachannel.txt ==-- ::: --== <%s> %s ==--\r\n' \
- $NICK `$nl{echo $MSG | sed 's/[^ ]* [^ ]* *//'}
-} else if (match_msg -iE '^' ^ $nick ^ '[:,] ' ^ `$nl{ls bin/ | tr '\n' '|' | sed 's/|*$//g'}) {
- CMD = `{echo $MSG | sed 's/[^ ]* //;s/ .*//'}
- ARGS = `{echo $MSG | sed 's/[^ ]* [^ ]*//'}
- send_msg $CHANNEL `$nl{./bin/$CMD $ARGS}
+ $NICK `$nl{echo $MSG | sed 's/[^ ]* *//'}
+} else if (match_hilight -iE '^(' ^ `$nl{builtin ls bin/ | tr '\n' '|' | sed 's/|*$//'} ^ ')') {
+ CMD = `{echo $MSG | sed 's/ .*//'}
+ ARGS = `{echo $MSG | sed 's/[^ ]*//'}
+ send_msg $TARGET `$nl{./bin/$CMD $ARGS}
+} else if (match_hilight -E '^welcome back') {
+ send_msg $TARGET 'Thanks, ' ^ $NICK
+} else if (match_hilight '^suggest') {
+ categories = (response)
+ splitmsg = `{echo $MSG}
+ if (~ $splitmsg(2) $categories) {
+ mkdir -p data/suggest/
+ echo $MSG | sed 's/[^ ]* *[^ ]* *//' > data/suggest/$splitmsg(2)
+ send_msg $TARGET $NICK ^ ': suggestion submitted to category ''' ^ $splitmsg(2) ^ ''''
+ } else {
+ send_msg $TARGET $NICK ^ ': invalid category. Accepted categories are: ' ^ `$nl{echo $^categories | sed 's/ /, /g'}
+ }
+} else if (match_hilight '.*') {
+ send_msg $TARGET `$nl{randomize \
+ $NICK ^ ', huh?' \
+ 'I don''t get it' \
+ 'That makes no sense' \
+ 'Try something else..' \
+ 'I''m a fucking bot man, cut me some slack' \
+ '*yawn* RTFM'}
}