From e69e2570914de82af98d6b76a023a327372bdfce Mon Sep 17 00:00:00 2001
From: dms <dms@c11ca15a-4712-0410-83d8-924469b57eb5>
Date: Fri, 22 Aug 2003 11:32:11 +0000
Subject: [PATCH] - support multiple bots on one host.   seems to work with one
 bot on the machine.   requested by timriker.

git-svn-id: https://svn.code.sf.net/p/infobot/code/trunk/blootbot@814 c11ca15a-4712-0410-83d8-924469b57eb5
---
 src/Shm.pl | 43 ++++++++++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 15 deletions(-)

diff --git a/src/Shm.pl b/src/Shm.pl
index 5ab3d18..ae7e842 100644
--- a/src/Shm.pl
+++ b/src/Shm.pl
@@ -64,22 +64,29 @@ sub shmWrite {
 
     return if (&IsParam("noSHM"));
 
-    # NULL hack.
-    ### TODO: create shmClear to deal with this.
-    if ($str !~ /^$/) {
-	my $read = &shmRead($key);
-	$read =~ s/\0+//g;
-
-	if ($str eq "") {
-	    $str = time().": ";		# time stamping, null.
-	} elsif ($read eq "") {
-	    $str = time().": ";		# timestamping.
-	} else {
-	    $str = $read ."||". $str;
+    if (length($str) > $size) {
+	&status("ERROR: length(str) (..)>$size...");
+	return;
+    }
+
+    if (length($str) == 0) {
+	# does $size overwrite the whole lot?
+	# if not, set to 2000.
+	if (!shmwrite($key, '', $position, $size)) {
+	    &ERROR("shmWrite: failed: $!");
 	}
+	return;
     }
 
-    if (!shmwrite($key,$str,$position,$size)) {
+    my $read = &shmRead($key);
+    $read =~ s/\0+//g;
+    if ($read eq "") {
+	$str = sprintf("%s:%d:%d: ", $param{ircNick}, $bot_pid, time());
+    } else {
+	$str = $read ."||". $str;
+    }
+
+    if (!shmwrite($key, '', $position, $size)) {
 	&ERROR("shmWrite: failed: $!");
     }
 }
@@ -227,8 +234,14 @@ sub shmFlush {
     my $time;
     my $shmmsg = &shmRead($shm);
     $shmmsg =~ s/\0//g;         # remove padded \0's.
-    if ($shmmsg =~ s/^(\d+): //) {
-	$time	= $1;
+    return if (length($shmmsg) == 0);
+    if ($shmmsg =~ s/^(\S+):(\d+):(\d+): //) {
+	my $n	= $1;
+	my $pid	= $2;
+	$time	= $3;
+    } else {
+	&status("warn: shmmsg='$shmmsg'.");
+	return;
     }
 
     foreach (split '\|\|', $shmmsg) {
-- 
2.39.5