[Phpfreechat-svn] SF.net SVN: phpfreechat: [492] trunk/src
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2006-05-12 13:55:25
|
Revision: 492 Author: kerphi Date: 2006-05-12 06:55:14 -0700 (Fri, 12 May 2006) ViewCVS: http://svn.sourceforge.net/phpfreechat/?rev=492&view=rev Log Message: ----------- - Bug fix: the cached nickname list was not correctly updated - add a new function in the container API : isNickOnline Modified Paths: -------------- trunk/src/containers/file.class.php trunk/src/pfctools.php trunk/testcase/container_file.php trunk/testcase/container_generic.php Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2006-05-11 16:56:06 UTC (rev 491) +++ trunk/src/containers/file.class.php 2006-05-12 13:55:14 UTC (rev 492) @@ -118,6 +118,15 @@ flock ($fp, LOCK_UN); // unlock fclose($fp); + // append the nickname to the cached nickname list + $id = $this->isNickOnline($chan, $nick); + $_chan = ($chan == NULL) ? "SERVER" : $chan; + if ($id<0) + { + $this->_users[$_chan][] = array("nick" => $nick, + "timestamp" => filemtime($nick_filename)); + } + return true; } @@ -126,6 +135,7 @@ * Notice: The caller must take care to update all joined channels. * @param $chan if NULL then remove the user on the server (disconnect), otherwise just remove the user from the given channel (quit) * @param $nick the nickname to remove + * @return true if the nickname was correctly removed */ function removeNick($chan, $nick) { @@ -144,27 +154,21 @@ pxlog("removeNick(".$nick.") - Error: the nickname data file to remove doesn't exists", "chat", $c->getId()); } - @unlink($nick_filename); + $ok = @unlink($nick_filename); - // remove the nickname from the cache list - if (isset($this->_users[$chan])) + if ($c->debug) { - $uid = 0; $isonline = false; - while($uid < count($this->_users[$chan]) && !$isonline) - { - if ($this->_users[$chan][$uid]["nick"] == $nick) - $isonline = true; - else - $uid++; - } - if ($isonline) - { - $key = $uid; - unset($this->_users[$chan][$key]); - } + // check the nickname file is correctly deleted + if (file_exists($nick_filename)) + pxlog("removeNick(".$nick.") - Error: the nickname data file yet exists", "chat", $c->getId()); } + + // remove the nickname from the cache list + $id = $this->isNickOnline($chan, $nick); + $_chan = ($chan == NULL) ? "SERVER" : $chan; + if ($id >= 0) unset($this->_users[$_chan][$id]); - return true; + return $ok; } /** @@ -189,22 +193,18 @@ @chmod($nick_filename, 0777); // append the nickname to the cache list - if (isset($this->_users[$chan])) + $_chan = ($chan == NULL) ? "SERVER" : $chan; + $id = $this->isNickOnline($chan, $nick); + if ($id < 0) { - $uid = 0; $isonline = false; - while($uid < count($this->_users[$chan]) && !$isonline) - { - if ($this->_users[$chan][$uid]["nick"] == $nick) - $isonline = true; - else - $uid++; - } - if (!$isonline) - { - $this->_users[$chan][] = array("nick" => $nick, - "timestamp" => filemtime($nick_filename)); - } + $this->_users[$_chan][] = array("nick" => $nick, + "timestamp" => filemtime($nick_filename)); } + else + { + // just update the timestamp if the nickname is allready present in the cached list + $this->_users[$_chan][$id]["timestamp"] = filemtime($nick_filename); + } return $there; } @@ -231,22 +231,12 @@ // update the nick cache list if($ok) { - if (isset($this->_users[$chan])) + $_chan = ($chan == NULL) ? "SERVER" : $chan; + $id = $this->isNickOnline($chan, $oldnick); + if ($id >= 0) { - $uid = 0; $isonline = false; - while($uid < count($this->_users[$chan]) && !$isonline) - { - if ($this->_users[$chan][$uid]["nick"] == $oldnick) - $isonline = true; - else - $uid++; - } - if ($isonline) - { - $key = $uid; - $this->_users[$chan][$key]["nick"] = $newnick; - $this->_users[$chan][$key]["timestamp"] = filemtime($newnick_filename); - } + $this->_users[$_chan][$id]["nick"] = $newnick; + $this->_users[$_chan][$id]["timestamp"] = filemtime($newnick_filename); } } @@ -323,7 +313,8 @@ } // cache the updated user list - $this->_users[$chan] =& $users; + $_chan = ($chan == NULL) ? "SERVER" : $chan; + $this->_users[$_chan] =& $users; return $deleted_user; } @@ -331,14 +322,14 @@ /** * Returns the nickname list on the given channel or on the whole server * @param $chan if NULL then returns all connected user, otherwise just returns the channel nicknames - * @return array() contains a nickname list + * @return array(array("nick"=>???,"timestamp"=>???) contains the nickname list with the associated timestamp (laste update time) */ function getOnlineNick($chan) { // return the cached user list if it exists - if (isset($this->_users[$chan]) && - is_array($this->_users[$chan])) - return $this->_users[$chan]; + $_chan = ($chan == NULL) ? "SERVER" : $chan; + if (isset($this->_users[$_chan]) && is_array($this->_users[$_chan])) + return $this->_users[$_chan]; $c =& $this->c; @@ -356,11 +347,37 @@ } // cache the user list - $this->_users[$chan] =& $users; + $this->_users[$_chan] =& $users; - return $users; + return $this->_users[$_chan]; } + + /** + * Returns returns a positive number if the nick is online + * @param $chan if NULL then check if the user is online on the server, otherwise check if the user has joined the channel + * @return -1 if the user is off line, a positive (>=0) if the user is online + */ + function isNickOnline($chan, $nick) + { + // get the nickname list + $_chan = ($chan == NULL) ? "SERVER" : $chan; + $online_users = isset($this->_users[$_chan]) ? $this->_users[$_chan] : $this->getOnlineNick($chan); + $uid = 0; + $isonline = false; + while($uid < count($online_users) && !$isonline) + { + if ($online_users[$uid]["nick"] == $nick) + $isonline = true; + else + $uid++; + } + if ($isonline) + return $uid; + else + return -1; + } + /** * Write a command to the given channel or to the server * Notice: a message is very generic, it can be a misc command (notice, me, ...) @@ -401,10 +418,10 @@ /** * Read the last posted commands from a channel or from the server + * Notice: the returned array must be ordered by id * @param $chan if NULL then read from the server, otherwise read from the given channel * @param $from_id read all message with a greater id * @return array() contains the command list - * @todo use one file (filename = msgid) for one message */ function read($chan, $from_id) { @@ -428,7 +445,8 @@ if ($file == "." || $file == "..") continue; // skip . and .. generic files if ($file>$from_id) { - $new_from_id = $file; + if ($file > $new_from_id) + $new_from_id = $file; $newmsg[] = $file; } } @@ -448,10 +466,11 @@ $data["sender"]= $formated_line[3]; $data["cmd"] = $formated_line[4]; $data["param"] = $formated_line[5]; - $datalist[] = $data; + $datalist[$data["id"]] = $data; } } - + ksort($datalist); + return array("data" => $datalist, "new_from_id" => $new_from_id ); } Modified: trunk/src/pfctools.php =================================================================== --- trunk/src/pfctools.php 2006-05-11 16:56:06 UTC (rev 491) +++ trunk/src/pfctools.php 2006-05-12 13:55:14 UTC (rev 492) @@ -125,7 +125,7 @@ if($obj=='.' || $obj=='..') continue; if (!@unlink($dir.'/'.$obj)) rm_r($dir.'/'.$obj); } - rmdir($dir); + @rmdir($dir); } /** Modified: trunk/testcase/container_file.php =================================================================== --- trunk/testcase/container_file.php 2006-05-11 16:56:06 UTC (rev 491) +++ trunk/testcase/container_file.php 2006-05-12 13:55:14 UTC (rev 492) @@ -26,7 +26,7 @@ { pfcContainerTestcase::tearDown(); } - + // this is a specific test for the File container function testCreateNick_File() { @@ -47,7 +47,7 @@ } // on desactive le timeout car se script peut mettre bcp de temps a s'executer -ini_set('max_execution_time', -1); +ini_set('max_execution_time', 0); $suite = new PHPUnit_TestSuite(); $suite->addTestSuite("pfcContainerTestcase_File"); Modified: trunk/testcase/container_generic.php =================================================================== --- trunk/testcase/container_generic.php 2006-05-11 16:56:06 UTC (rev 491) +++ trunk/testcase/container_generic.php 2006-05-12 13:55:14 UTC (rev 492) @@ -45,7 +45,7 @@ // remove the created files and directories $this->ct->clear(); } - + function testCreateNick_Generic() { $c =& $this->c; @@ -56,14 +56,14 @@ // create on the channel $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the channel"); + $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->assertTrue($isonline, "nickname should be online on the channel"); // create on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the server"); + $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->assertTrue($isonline, "nickname should be online on the server"); } function testRemoveNick_Generic() @@ -76,22 +76,18 @@ // on the channel $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the channel"); - $this->ct->removeNick($chan,$nick); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertFalse(in_array($nick, $online_nick), "nickname should not be online on the channel"); + $this->ct->removeNick($chan, $nick); + $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->assertFalse($isonline, "nickname shouldn't be online on the channel"); // on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the server"); - $this->ct->removeNick($chan,$nick); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertFalse(in_array($nick, $online_nick), "nickname should not be online on the server"); + $this->ct->removeNick($chan, $nick); + $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->assertFalse($isonline, "nickname shouldn't be online on the server"); } - + function testGetNickId_Generic() { $c =& $this->c; @@ -104,7 +100,7 @@ $ret = $this->ct->getNickId($nick); $this->assertEquals($nickid, $ret, "created nickname doesn't have a correct nickid"); } - + function testRemoveObsoleteNick_Generic() { $c =& $this->c; @@ -115,24 +111,20 @@ // on the channel $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the channel"); sleep(2); $ret = $this->ct->removeObsoleteNick($chan, "1000"); $this->assertTrue(in_array($nick, $ret), "nickname should be removed from the channel"); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertFalse(in_array($nick, $online_nick), "nickname should not be online on the channel"); - + $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->assertFalse($isonline, "nickname shouldn't be online on the channel"); + // on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the server"); sleep(2); $ret = $this->ct->removeObsoleteNick($chan, "1000"); $this->assertTrue(in_array($nick, $ret), "nickname should be removed from the server"); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertFalse(in_array($nick, $online_nick), "nickname should not be online on the server"); + $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->assertFalse($isonline, "nickname shouldn't be online on the server"); } function testSetGetRmMeta_Generic() @@ -177,61 +169,55 @@ // on the channel $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "1-nickname should be online on the channel"); sleep(2); $ret = $this->ct->updateNick($chan, $nick); - $this->assertTrue($ret, "2-nickname should be correctly updated on the channel"); + $this->assertTrue($ret, "nickname should be correctly updated"); $ret = $this->ct->removeObsoleteNick($chan, "1000"); - $this->assertFalse(in_array($nick, $ret), "3-nickname should not be removed from the channel because it has been updated"); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "4-nickname should be online on the channel"); - + $this->assertFalse(in_array($nick, $ret), "nickname shouldn't be removed because it has been updated"); + $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->assertTrue($isonline, "nickname should be online"); + // on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "5-nickname should be online on the server"); sleep(2); $ret = $this->ct->updateNick($chan, $nick); - $this->assertTrue($ret, "6-nickname should be correctly updated on the server"); + $this->assertTrue($ret, "nickname should be correctly updated"); $ret = $this->ct->removeObsoleteNick($chan, "1000"); - $this->assertFalse(in_array($nick, $ret), "7-nickname should not be removed from the server because it has been updated"); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "8-nickname should be online on the server"); + $this->assertFalse(in_array($nick, $ret), "nickname shouldn't be removed because it has been updated"); + $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->assertTrue($isonline, "nickname should be online"); } function testchangeNick_Generic() { $c =& $this->c; $ct =& $this->ct; - $nick = $this->nick; + $nick1 = $this->nick; $nick2 = $this->nick."2"; $nickid = $this->nickid; $chan = $this->chan; // create on the channel - $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "1-nickname should be online on the channel"); - $ret = $this->ct->changeNick($chan, $nick2, $nick); - $this->assertTrue($ret, "2-nickname change function should returns true (succes)"); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertFalse(in_array($nick, $online_nick), "3-nickname should not be online on the channel"); - $this->assertTrue(in_array($nick2, $online_nick), "4-nickname should be online on the channel"); - + $this->ct->createNick($chan, $nick1, $nickid); + $ret = $this->ct->changeNick($chan, $nick2, $nick1); + $this->assertTrue($ret, "nickname change function should returns true (success)"); + $isonline1 = ($this->ct->isNickOnline($chan, $nick1) >= 0); + $isonline2 = ($this->ct->isNickOnline($chan, $nick2) >= 0); + $this->assertFalse($isonline1, "nickname shouldn't be online"); + $this->assertTrue($isonline2, "nickname shouldn't be online"); + // create on the server $chan = NULL; - $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "5-nickname should be online"); - $ret = $this->ct->changeNick($chan, $nick2, $nick); - $this->assertTrue($ret, "6-nickname change function should returns true (succes)"); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertFalse(in_array($nick, $online_nick), "7-nickname should not be online"); - $this->assertTrue(in_array($nick2, $online_nick), "8-nickname should be online on the channel"); + $this->ct->createNick($chan, $nick1, $nickid); + $ret = $this->ct->changeNick($chan, $nick2, $nick1); + $this->assertTrue($ret, "nickname change function should returns true (success)"); + $isonline1 = ($this->ct->isNickOnline($chan, $nick1) >= 0); + $isonline2 = ($this->ct->isNickOnline($chan, $nick2) >= 0); + $this->assertFalse($isonline1, "nickname shouldn't be online"); + $this->assertTrue($isonline2, "nickname shouldn't be online"); } - + function testwrite_Generic() { $c =& $this->c; @@ -244,28 +230,24 @@ // create message on the channel $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "1-nickname should be online on the channel"); $msgid = $this->ct->write($chan, $nick, $cmd, $msg); - $this->assertEquals($msgid, 1,"2- generated msg_id is not correct"); + $this->assertEquals($msgid, 1,"generated msg_id is not correct"); $res = $this->ct->read($chan, 0); - $this->assertEquals(1, count($res["data"]), "3- 1 messages should be read"); - $this->assertEquals($msg, $res["data"][0]["param"] ,"4- messages data is not the same as the sent one"); - $this->assertEquals($res["new_from_id"], 1 ,"6- new_from_id is not correct"); + $this->assertEquals(1, count($res["data"]), "1 messages should be read"); + $this->assertEquals($msg, $res["data"][1]["param"] ,"messages data is not the same as the sent one"); + $this->assertEquals($res["new_from_id"], 1 ,"new_from_id is not correct"); // create message on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "7-nickname should be online on the channel"); $msgid = $this->ct->write($chan, $nick, $cmd, $msg); - $this->assertEquals($msgid, 1,"8- generated msg_id is not correct"); + $this->assertEquals($msgid, 1,"generated msg_id is not correct"); $res = $this->ct->read($chan, 0); - $this->assertEquals(1, count($res["data"]), "9- 1 messages should be read"); - $this->assertEquals($msg, $res["data"][0]["param"] ,"10- messages data is not the same as the sent one"); - $this->assertEquals($res["new_from_id"], 1 ,"11- new_from_id is not correct"); + $this->assertEquals(1, count($res["data"]), "1 messages should be read"); + $this->assertEquals($msg, $res["data"][1]["param"] ,"messages data is not the same as the sent one"); + $this->assertEquals($res["new_from_id"], 1 ,"new_from_id is not correct"); } - + function testread_Generic() { $c =& $this->c; @@ -278,28 +260,25 @@ // create on the channel $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "1-nickname should be online on the channel"); for($i = 0; $i < 10; $i++) { $msgid = $this->ct->write($chan, $nick, $cmd ,$msg . $i); - $this->assertEquals($msgid, $i+1,"2- generated msg_id is not correct"); + $this->assertEquals($msgid, $i+1, "generated msg_id is not correct"); } $res = $this->ct->read($chan, 0); - $this->assertEquals(10, count($res["data"]), "3- 10 messages should be read"); - $this->assertEquals($msg."0", $res["data"][0]["param"] ,"4- messages data is not the same as the sent one"); - $this->assertEquals($msg."9", $res["data"][9]["param"] ,"5- messages data is not the same as the sent one"); - $this->assertEquals($res["new_from_id"], 10 ,"6- new_from_id is not correct"); - + $this->assertEquals(10, count($res["data"]), "10 messages should be read"); + $this->assertEquals($msg."0", $res["data"][1]["param"] ,"messages data is not the same as the sent one"); + $this->assertEquals($msg."8", $res["data"][9]["param"] ,"messages data is not the same as the sent one"); + $this->assertEquals($res["new_from_id"], 10 ,"new_from_id is not correct"); + $res = $this->ct->read($chan, 5); - $this->assertEquals(5, count($res["data"]), "7- 5 messages should be read"); - $this->assertEquals($msg."5", $res["data"][0]["param"] ,"8- messages data is not the same as the sent one"); - $this->assertEquals($msg."9", $res["data"][4]["param"] ,"9- messages data is not the same as the sent one"); - $this->assertEquals($res["new_from_id"], 10 ,"10- new_from_id is not correct"); + $this->assertEquals(5, count($res["data"]), "5 messages should be read"); + $this->assertEquals($msg."5", $res["data"][6]["param"] ,"messages data is not the same as the sent one"); + $this->assertEquals($msg."9", $res["data"][10]["param"] ,"messages data is not the same as the sent one"); + $this->assertEquals($res["new_from_id"], 10 ,"new_from_id is not correct"); } - function testgetLastId_Generic() { $c =& $this->c; @@ -312,28 +291,24 @@ // on the channel $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "1-nickname should be online on the channel"); for($i = 0; $i < 10; $i++) { $msgid = $this->ct->write($chan, $nick, $cmd ,$msg . $i); - $this->assertEquals($msgid, $i+1,"2- generated msg_id is not correct"); + $this->assertEquals($msgid, $i+1,"generated msg_id is not correct"); } $msgid = $this->ct->getLastId($chan); - $this->assertEquals(10, $msgid, "3- last msgid is not correct"); + $this->assertEquals(10, $msgid, "last msgid is not correct"); // on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); - $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "4-nickname should be online on the channel"); for($i = 0; $i < 10; $i++) { $msgid = $this->ct->write($chan, $nick, $cmd ,$msg . $i); - $this->assertEquals($msgid, $i+1,"5- generated msg_id is not correct"); + $this->assertEquals($msgid, $i+1,"generated msg_id is not correct"); } $msgid = $this->ct->getLastId($chan); - $this->assertEquals(10, $msgid, "6- last msgid is not correct"); + $this->assertEquals(10, $msgid, "last msgid is not correct"); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |