commit 74cef5396ca42a4e3201742e4c5fc190e2534b19
parent 345670f95611b39991332444c482ca0f7cfd4301
Author: Hayden Hamilton <hayden@haydenvh.com>
Date: Sun, 11 Apr 2021 20:52:12 +0100
main.sh functions.sh filters.sh: add time-based locking
Diffstat:
3 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/filters.sh b/filters.sh
@@ -13,6 +13,8 @@ oww(){
}
greet(){
+ islocked autogreet && return
+ lock autogreet 240
greet=$(sed "s/ $nick.*//;s/^$nick[:,] //")
repl_msg "$greet $NICK"
}
diff --git a/functions.sh b/functions.sh
@@ -1,5 +1,16 @@
#!/bin/sh
+# _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
@@ -180,6 +191,27 @@ undefine_match(){
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"
diff --git a/main.sh b/main.sh
@@ -11,11 +11,14 @@ outlog=out.log
####
-export nick server port input inlog outlog comment
-
+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
+
while _mkfifo
do
sleep 1