marvvin

IRC bot
Log | Files | Refs | README | LICENSE

commit b896ea4a9f1d1cd76377f4467298bb569f249d10
parent ba1e80aa327f6d8a99fd9adac18ce97e4b1eb5d3
Author: hhvn <dev@hhvn.uk>
Date:   Sun, 27 Feb 2022 13:21:27 +0000

Use rc script for handler

Diffstat:
Dfilters.sh | 217-------------------------------------------------------------------------------
Dfunctions.sh | 322-------------------------------------------------------------------------------
Ahandler.rc | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmain.sh | 31++++++++++++++++++++++++-------
4 files changed, 76 insertions(+), 546 deletions(-)

diff --git a/filters.sh b/filters.sh @@ -1,217 +0,0 @@ -#!/bin/sh -. ./functions.sh - -oww(){ - repl_msg $(rand "oww" \ - "bloody hell man" \ - "what is wrong with you‽" \ - "stop it!" \ - "meanie" \ - "I'll tell hhvn about this" \ - "go to hell" \ - ) -} - -greet(){ - islocked autogreet && return - lock autogreet 1500 - greet=$(sed "s/ $nick.*//;s/^$nick[:,] //") - repl_msg "$greet $NICK" -} - -rand(){ - n=$(awk -vmax=$# 'BEGIN{srand(); print int(min+rand()*(max))}') - shift $n - echo "$1" -} - - -bin(){ - set -- $(cat -) - shift 1 - bin/$@ | while IFS= read -r line - do - repl_msg "$line" - done -} - -binsu(){ - touch /tmp/sudo - set -- $(cat -) - shift 2 - bin/$@ | while IFS= read -r line - do - repl_msg "$line" - done -} - -html2txt(){ - sed 's~\&amp;~\&~g;s~\&nbsp~ ~g;s~\&reg;~®~g' -} - -url_title(){ - export in=$(head -n 1) - url=$(echo "$in" | sed 's/invidio.us/www.youtube.com/g;s/m.imdb.com/www.imdb.com/g' | tr -d '\r' | sed 's~embed/~watch?v=~g' | grep -o '[a-z]*://[^ ]*' | sed 's/[\.?>,]$//') - set -- $url - headers=$($tor curl -qsLD - "$1" | tr -d '\r' | sed '/^[^[:print:]]*$/q' | tr '[:upper:]' '[:lower:]') - echo "$1" | grep 'http[s]*://[w.]*youtu[.]*be' >/dev/null && { - content=$($tor curl -L "$1" | tr -d '\r') - repl_notice $( - printf "05You05,16Tube ::: " - echo "Title: $(echo "$content" | grep '\[{\"videoPrimaryInfoRenderer\":{\"title\":{\"runs\"\:\[{\"text\":' | sed 's/.*\[{\"videoPrimaryInfoRenderer\":{\"title\":{\"runs\"\:\[{\"text\":// ; s/}\]}.*// ; s/\\//g ; s/^\"// ; s/.$//') \ -::: Author: $(echo "$content" | grep -o 'author":[^}]*}' | sed -E 's/.*:"([^"]*).*/\1/g') \ -::: Upload Date: $(echo "$content" | grep -o 'uploadDate":[^}]*}' | sed -E 's/.*:"([^"]*).*/\1/g;s/([0-9]*)-([0-9]*)-([0-9]*)/\3-\2-\1/')" - ) - return - } - echo "$1" | grep 'http[s]*://[w.]*imdb.com' >/dev/null && { - content=$($tor curl -L "$1" | tr -d '\r') - repl_notice $( - printf "88,53 IMDb \nTitle: " - echo "$content" | grep -o '<title[^>]*>[^<]*</title>' | sed 's~.*<title[^>]*>~~g;s~</title>.*~~g' | head -n 1 | sed 's/ - IMDb//g' - printf "Synopsis: " - echo "$content" | grep -io '<meta[^>]*"description[^>]*>' | sed -E 's/.*content="([^"]*)".*/\1/' | cut -zc-300 - printf "Run-time: " - echo "$content" | tr '\n' '\t' | grep -o "<time datetime=\"[^>]*>[^<]*" | awk -F">" '{print $2}' | sed 's/[[:space:]]*//;s/[[:space:]]*$//' | head -n 1 | tr -d '\n' - printf " ::: Genres: " - echo "$content" | tr '\n' '\t' | grep -o "\"genre\".*\]," | sed 's/\],.*//g' | tr -d '"\n' | sed 's/genre:[^[:alnum:]]*//' | tr '\t' ' ' | tr -s ' ' - printf " ::: Stars: " - echo "$content" | grep "\"ratingValue\"" | head -n 1 | sed -E 's/.*"([^"]*)"$/\1/g' | tr -d '\n' - printf "/10\n" - ) - return - } - - code=$(echo "$headers" | head -n 1) - ctype=$(echo "$headers" | awk -F":" '$1 == "content-type" {print $2}') - server=$(echo "$headers" | awk -F":" '$1 == "server" {print $2}') - - echo "$ctype" | grep -v 'text/html' >/dev/null && { - echo "$server" | grep -i 'Cloudflare' >/dev/null && repl_notice "Shit: cloudflare." && return - echo "$ctype" | grep '[[:alnum:]]' >/dev/null && out="${out}Content-type:$ctype " - echo "$server" | grep '[[:alnum:]]' >/dev/null && out="${out}Server:$server " - echo "$out" | grep '[[:alnum:]]' >/dev/null && repl_notice "$(echo "$out" | html2txt | cut -zc-200)" - return - } - content=$($tor curl -L "$1" | tr -d '\r') - title=$(echo "$content" | grep -o '<title[^>]*>[^<]*</title>' | sed 's~.*<title[^>]*>~~g;s~</title>.*~~g' | head -n 1) - published=$(echo "$content" | grep -o '<meta[^>]*"published_time[^>]*>' | sed -E 's/.*content="([^"]*)".*/\1/;s/T.*//;s~([0-9]*)-([0-9]*)-([0-9]*)~\3/\2/\1~') - modified=$(echo "$content" | grep -o '<meta[^>]*"modified_time[^>]*>' | sed -E 's/.*content="([^"]*)".*/\1/;s/T.*//;s~([0-9]*)-([0-9]*)-([0-9]*)~\3/\2/\1~') - author=$(echo "$content" | grep -o '<meta[^>]*"author[^>]*>' | sed -E 's/.*content="([^"]*)".*/\1/') - generator=$(echo "$content" | grep -io '<meta[^>]*"generator[^>]*>' | sed -E 's/.*content="([^"]*)".*/\1/') - description=$(echo "$content" | grep -io '<meta[^>]*"description[^>]*>' | sed -E 's/.*content="([^"]*)".*/\1/;s/\&amp\;(#39\;|)/\&/g' | cut -zc-200) - - echo "$server" | grep -i 'Cloudflare' >/dev/null && repl_notice "Shit: cloudflare." && return - echo "$title" | grep '[[:alnum:]]' >/dev/null && out="Title: $title " - echo "$published" | grep '[[:alnum:]]' >/dev/null && out="${out}Published: $published " - echo "$modified" | grep '[[:alnum:]]' >/dev/null && out="${out}Last modified: $modified " - echo "$author" | grep '[[:alnum:]]' >/dev/null && out="${out}Author: $author " - echo "$generator" | grep '[[:alnum:]]' >/dev/null && out="${out}Page generator: $generator " - echo "$description" | grep '[[:alnum:]]' >/dev/null && out="${out}Description: $description... " - echo "$ctype" | grep '[[:alnum:]]' >/dev/null && out="${out}Content-type:$ctype " - echo "$server" | grep '[[:alnum:]]' >/dev/null && out="${out}Server:$server " - echo "$out" | grep '[[:alnum:]]' >/dev/null && repl_notice "$(echo "$out" | html2txt | cut -zc-200)" -} - -puma(){ - repl_msg "Aha, beware the puma, $NAME" -} - -weed(){ - repl_action $(rand \ - "puffs" \ - "inhales" \ - "is high as a kite" \ - ) -} - -job(){ - repl_msg $(rand \ - "How much are we talking?" \ - "$NICK, robots do not require much maintainance" \ - ) -} - -food(){ - repl_action $(rand \ - "scoffs it down" \ - "shoves it in his mouth" \ - "noms" \ - ) -} - -huh(){ - repl_msg $(rand \ - "$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" \ - ) -} - -stupid(){ - repl_msg $(rand \ - "s s sorry" \ - "I know.. :(" \ - "it was an accident! :|" \ - ) -} - -sand(){ - islocked sand && return - lock sand 240 - repl_msg "I don't like sand. It's coarse and rough and irritating and it gets everywhere." -} - -shrug(){ - repl_action "shrugs" - repl_msg "$nick, it is what it is, and that's all it is" -} - -say(){ - ( - head | tr ' ' '\n' | awk 'BEGIN {p=0}; /say|Say|say,|Say,/ {p=1; next}; // {if (p == 1) print}' | tr '\n' ' ' | sed 's/^[Ssay],* //' - printf "\r\n" - ) | prepl_msg -} - -topic(){ - islocked topic && { - echo "$NICK: topic function locked temporarily" | prepl_msg - return - } - lock topic 5 - - send_raw "TOPIC #hlircnet :--== #HLIRCnet ==-- ::: --== gopher://hlirc.net/0/metachannel.txt ==-- ::: --== <$NICK> $(head -n 1 /dev/stdin | sed 's/^[^ ]* [^ ]* //') ==--" -} - -calc(){ - islocked bc && return - lock bc 6 - ( sed 's/.*calc //' | timeout 5 bc | head -n5; ) 2>&1 | prepl_msg -} - - -match_motd send_join "#hlircnet" -match_motd send_join "#test" -# match_msg "^($nick[:,] |)(good |)(hi|hello|greetings|sup|morning|afternoon|evening|day)(.*$nick.*|)$" greet -match_msg "^(dumb|stupid|retard|useless|pointless).*(bot|$nick)" stupid -match_action "(slaps|smacks|whacks|hits|strikes|punches|shoots|stabs|wounds) $nick" oww -match_action "(gives|passes|hands).*$nick.*(blunt|bong|joint)" weed -match_action "(gives|passes|hands).*$nick.*job" job -match_action "(gives|passes|hands).*$nick.*(food|cookie|biscuit|leftovers|dinner|scraps)" food -match_msg "^$nick[:,] say" say -match_msg "^$nick[:,] topic" topic -match_msg "^$nick[:,] (calc|bc)" calc -match_msg "^$nick[:,] good |)night(.*$nick.*|)$" night -match_msg "^$nick[,:] ($(ls bin/ | tr '\n' '|' | sed 's/|$//g'))" bin -match_msg "^$nick[,:] (sudo|doas) ($(ls bin/ | tr '\n' '|' | sed 's/|$//g'))" binsu -clshit() { repl_msg "Calling clownflare shit, $NICK"; } -match_msg "^$nick[,:] what is your purpose" clshit -match_msg "^pooo*p" sand -match_msg "http[s]*://.*" url_title -# match_msg "^(lul|lol|hah|hehe|heh|haha).*wut\?" shrug -match_msg "^marvvin[:,].*" huh -match_msg "puma" puma diff --git a/functions.sh b/functions.sh @@ -1,322 +0,0 @@ -#!/bin/sh - -# echo(string...) -echo(){ - printf "%s\n" "$*" -} - -# _mktemp(prefix-default:tmp) -_mktemp(){ - dir="/tmp/${1:-tmp}.$(head -n 5 /dev/urandom | base64 | tr '/' '+' | tr -d '\n' | cut -c-10)" - while [ -e $dir ] - do - dir="/tmp/${1:-tmp}.$(head -n 5 /dev/urandom | base64 | tr '/' '+' | tr -d '\n' | cut -c-10)" - done - mkdir -p "$dir" - echo "$dir" -} - -# _mkfifo(void) -_mkfifo(){ - rm $input - mkfifo $input -} - -# _checkstop(void) -_checkstop(){ - [ -f stop ] && { - rm stop - kill -1 $$ - } -} - -# connect(host, port) -[ "$tls" = "y" ] && { - connect(){ - openssl s_client -connect "$1:$2" - } -} || { - connect(){ - nc "$1" "$2" - } -} - -# match_raw(regex, command...) -match_raw(){ - echo "$LINE" | grep -E "$1" >/dev/null && { - shift - echo "$LINE" | $@ & - undefine_match - } -} - -# timeout(time, command) -timeout(){ - stdin=$(cat) - mkdir -p "$tmpdir/timeout" - tmpfile="$tmpdir/timeout/$(printf '%s%s\n' "$(date +%s)" "$(head -n 1 /dev/urandom | base64 | head -n 1 | cut -c-10)" | tr '/' '%')" - time="$1" - shift - - touch "$tmpfile" - - ( - sleep 0.1 # for $! - - printf '%s\n' "$stdin" | "$@" - ret=$? - rm "$tmpfile" - ) & - pid=$! - - sleep "$time" - [ -e "$tmpfile" ] && { - # tmpfile hasn't been removed, should be - # safe to kill now.. - - printf "Timeout, killing: %s (%s)\n" "$pid" "$*" - kill -9 "$pid" - rm "$tmpfile" - return 1 - } - - return ${ret:-0} -} - -debug(){ - return 0 - #return 1 -} - -# send_raw(line) -send_raw(){ - printf '%s\r\n' "$(echo "$@" | tr '\n\r' ' ')" >> $input -} - -# | psend_raw(void) -psend_raw(){ - while IFS= read -r pline - do - printf '%s\r\n' "$(echo "$pline" | tr '\n\r' ' ')" >> $input - done -} - -# match_msg(regex, command...) -match_msg(){ - regex="$1" && shift - cmd="$@" - set -- $LINE - - [ "$2" = "PRIVMSG" ] && { - sline=$(echo "$@" | sed 's/:[^:]*://' | tr -d '\r') - echo "$sline" | grep -Ei "$regex" >/dev/null && { - echo "$3" | grep '^[#!&]' >/dev/null && REPL="$3" || REPL="$NICK" - export REPL - echo "$sline" | $cmd & - undefine_match - } - } -} - -# send_msg(dest, line) -send_msg(){ - dest=$(echo "$1" | tr -d '\r\n') - shift - sline=$(echo "$@" | tr -d '\r\n') - printf "PRIVMSG %s :%s\r\n" "$dest" "$sline" >> $input -} - -# | psend_msg(dest) -psend_msg(){ - dest=$(echo "$1" | tr -d '\r\n') - while IFS= read -r pline - do - printf "PRIVMSG %s :%s\r\n" "$dest" "$pline" >> $input - done -} - -# repl_msg(line) -repl_msg(){ - sline=$(echo "$@" | tr -d '\r\n') - printf "PRIVMSG %s :%s\r\n" "$REPL" "$sline" >> $input -} - -# | prepl_msg(void) -prepl_msg(){ - while IFS= read -r pline - do - sline=$(echo "$pline" | tr -d '\r\n') - printf "PRIVMSG %s :%s\r\n" "$REPL" "$sline" >> $input - done -} - -# match_notice(regex, command...) -match_notice(){ - regex="$1" && shift - cmd="$@" - set -- $LINE - - [ "$2" = "NOTICE" ] && { - sline=$(echo "$@" | sed 's/:[^:]*://' | tr -d '\r') - echo "$sline" | grep -Ei "$regex" >/dev/null && { - echo "$3" | grep '^[#!&]' >/dev/null && REPL="$3" || REPL="$NICK" - export REPL - echo "$sline" | $cmd & - undefine_match - } - } -} - -# send_notice(dest, line) -send_notice(){ - dest=$(echo "$1" | tr -d '\r\n') - shift - sline=$(echo "$@" | tr -d '\r\n') - printf "NOTICE %s :%s\r\n" "$dest" "$sline" >> $input -} - -# | psend_notice(dest) -psend_notice(){ - dest=$(echo "$1" | tr -d '\r\n') - while IFS= read -r pline - do - printf "NOTICE %s :%s\r\n" "$dest" "$pline" >> $input - done -} - -# repl_notice(line) -repl_notice(){ - sline=$(echo "$@" | tr -d '\r\n') - printf "NOTICE %s :%s\r\n" "$REPL" "$sline" >> $input -} - -# | prepl_notice(void) -prepl_notice(){ - while IFS= read -r pline - do - sline=$(echo "$pline" | tr -d '\r\n') - printf "NOTICE %s :%s\r\n" "$REPL" "$sline" >> $input - done -} - -# match_action(regex, command...) -match_action(){ - regex="$1" && shift - cmd="$@" - set -- $LINE - - [ "$2" = "PRIVMSG" ] && { - echo "$@" | grep 'ACTION.*' >/dev/null && { - sline=$(echo "$@" | sed 's/:[^:]*:ACTION//;s///g' | tr -d '\r') - echo "$sline" | grep -Ei "$regex" >/dev/null && { - echo "$3" | grep '^[#!&]' >/dev/null && REPL="$3" || REPL="$NICK" - export REPL - echo "$sline" | $cmd & - undefine_match - } - } - } -} - -# send_action(dest, line) -send_action(){ - dest=$(echo "$1" | tr -d '\r\n') - shift - sline=$(echo "$@" | tr -d '\r\n') - printf "PRIVMSG %s :ACTION %s\r\n" "$dest" "$sline" >> $input -} - -# | psend_action(dest) -psend_action(){ - dest=$(echo "$1" | tr -d '\r\n') - while IFS= read -r pline - do - printf "PRIVMSG %s :ACTION %s\r\n" "$dest" "$pline" >> $input - done -} - -# repl_action(line) -repl_action(){ - sline=$(echo "$@" | tr -d '\r\n') - printf "PRIVMSG %s :ACTION %s\r\n" "$REPL" "$sline" >> $input -} - -# | prepl_action(void) -prepl_action(){ - while IFS= read -r pline - do - sline=$(echo "$pline" | tr -d '\r\n') - printf "PRIVMSG %s :ACTION %s\r\n" "$REPL" "$sline" >> $input - done -} - -# match_motd(command...) -match_motd(){ - echo "$LINE" | grep '^[^ ]* 376' >/dev/null && { - $@ & - } -} - -# match_join(channel, nick_regex, command...) -match_join(){ - channel=$1 && shift - nick_regex=$2 && shift - cmd=$@ - set -- $LINE - - [ "$2" = "JOIN" ] && { - echo "$NICK" | grep -E "$nick_regex" >/dev/null && { - export CHANNEL=$channel - $cmd & - undefine_match - } - } -} - -# send_join(channel) -send_join(){ - printf 'JOIN %s\r\n' "$(echo "$1" | tr -d '\r\n')" >> $input -} - -# undefine_match(void) -# map match_* functions to stubs -undefine_match(){ - match_raw(){ true; } - match_msg(){ true; } - match_action(){ true; } -} - -# lock(name, time-default:15) -lock(){ - mkdir -p $tmpdir/lock - touch $tmpdir/lock/$1 - - ( - sleep ${2:-15} - rm $tmpdir/lock/$1 - ) & -} - -# islocked(name) -islocked(){ - [ -f $tmpdir/lock/$1 ]; return $? -} - -# isnlocked(name) -isnlocked(){ - [ ! -f $tmpdir/lock/$1 ]; return $? -} - -# filter(line) -filter(){ - export LINE="$1" - set -- $LINE - - echo "$1" | grep '^:' >/dev/null && { - export FROM=$1 - export NICK=$(echo "$1" | sed -E 's/[:!]*([^:!]*).*/\1/') - } - - match_raw "^PING.*" send_raw "PONG $2" - sh filters.sh -} diff --git a/handler.rc b/handler.rc @@ -0,0 +1,52 @@ +#!/bin/rc + +LINE = `$nl{echo $LINE | tr -d '\r'} + +# parameterize +PARAMS = `' '$nl{echo $LINE | sed 's/ :.*//'} +PARAMS = ($PARAMS `$nl{echo $LINE | sed 's/.* ://'}) + +fn match_raw { + echo $LINE | grep $* >/dev/null +} + +fn match_msg { + if (!~ $PARAMS(1) 'PRIVMSG') + return 1; + CHANNEL = $PARAMS(2) + MSG = $PARAMS(3) + echo $PARAMS(3) | grep $* >/dev/null +} + +fn send_raw { + printf $* >> $input +} + +fn send_join { + send_raw 'JOIN %s\r\n' $1 +} + +fn send_msg { + send_raw 'PRIVMSG %s :%s\r\n' $1 $2 +} + +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') { + 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} +} diff --git a/main.sh b/main.sh @@ -14,11 +14,25 @@ outlog=out.log trap "kill -1 $$" 2 trap "kill -1 $$" 15 cd $(dirname $0) -. ./functions.sh -tmpdir=$(_mktemp $nick) export nick server port input inlog outlog comment tmpdir +_mkfifo(){ + rm "$input" + mkfifo "$input" +} + +# connect(host, port) +[ "$tls" = "y" ] && { + connect(){ + openssl s_client -connect "$1:$2" + } +} || { + connect(){ + nc "$1" "$2" + } +} + while _mkfifo do sleep 1 @@ -32,11 +46,14 @@ do tee $inlog /dev/stderr | \ while read -r line do - . ./functions.sh - filter "$line" + export LINE="$(echo "$line" | sed 's/:[^ ]* //')" + export FROM="$(echo "$line" | grep -o '^:[^ ]*')" + export NICK="$(echo "$FROM" | sed -E 's/[:!]*([^:!]*).*/\1/')" + ./handler.rc done >/dev/null | tee $input - _checkstop - sleep 10 - _checkstop + [ -f stop ] && { + rm stop + kill -1 $$ + } done