marvvin

IRC bot
Log | Files | Refs | README | LICENSE

commit 416175de671e057232d0c87aea1bfcf035cceb19
parent 7a909f009c34cfb87c785347ab31bc82f6086319
Author: hhvn <dev@hhvn.uk>
Date:   Mon, 24 May 2021 22:36:49 +0100

functions.sh filters.sh b/calc: use timeout() for calc

Diffstat:
Dbin/calc | 3---
Mfilters.sh | 5+++++
Mfunctions.sh | 31+++++++++++++++++++++++++++++++
3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/bin/calc b/bin/calc @@ -1,3 +0,0 @@ -#!/bin/sh - -echo "$@" | bc diff --git a/filters.sh b/filters.sh @@ -177,6 +177,10 @@ say(){ ) | prepl_msg } +calc(){ + printf '%s\n' "$*" | timeout 5 bc | 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 @@ -187,6 +191,7 @@ match_action "(gives|passes|hands).*$nick.*job" job match_action "(gives|passes|hands).*$nick.*(food|cookie|biscuit|leftovers|dinner|scraps)" food match_msg "$nick[:,] (wttr|weather)" weather match_msg "$nick[:,] say" say +match_msg "$nick[:,] calc" 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 diff --git a/functions.sh b/functions.sh @@ -50,6 +50,37 @@ match_raw(){ } } +# timeout(time, command) +timeout(){ + tmpfile="$tmpdir/$(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 $! + + "$@" + ret=$? + rm "$tmpfile" + ) & + pid=$! + + sleep "$time" + [ -e "$tmpfile" ] && { + # tmpfile hasn't been removed, should be + # safe to kill now.. + + printf "Timeout, killing: %s\n" "$@" + kill -9 "$pid" + rm "$tmpfile" + return 1 + } + + return ${ret:-0} +} + debug(){ return 0 #return 1