[Phpfreechat-svn] SF.net SVN: phpfreechat: [892] trunk
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2006-12-07 17:56:02
|
Revision: 892 http://svn.sourceforge.net/phpfreechat/?rev=892&view=rev Author: kerphi Date: 2006-12-07 09:55:46 -0800 (Thu, 07 Dec 2006) Log Message: ----------- integrate the new command parsing function http://www.phpfreechat.net/forum/viewtopic.php?id=872 Modified Paths: -------------- trunk/src/pfccommand.class.php trunk/src/phpfreechat.class.php Added Paths: ----------- trunk/testcase/parsecommand.php Modified: trunk/src/pfccommand.class.php =================================================================== --- trunk/src/pfccommand.class.php 2006-12-07 14:38:34 UTC (rev 891) +++ trunk/src/pfccommand.class.php 2006-12-07 17:55:46 UTC (rev 892) @@ -190,7 +190,50 @@ $xml_reponse->addScript("trace('".$msg."');"); } + + function ParseCommand($cmd_str) + { + $pattern_quote = '/([^\\\]|^)"([^"]+[^\\\])"/'; + $pattern_quote = '/"([^"]+)"/'; + $pattern_noquote = '/([^"\s]+)/'; + $pattern_command = '/^\/([a-z0-9]+)\s*(.*)/'; + $result = array(); + // parse the command name (ex: '/invite') + if (preg_match($pattern_command, $cmd_str, $res)) + { + $cmd = $res[1]; + $params_str = $res[2]; + // parse the quotted parameters (ex: '/invite "nickname with spaces"') + preg_match_all($pattern_quote,$params_str,$res1,PREG_OFFSET_CAPTURE); + $params_res = $res1[1]; + // split the parameters string + $nospaces = preg_split($pattern_quote,$params_str,-1,PREG_SPLIT_OFFSET_CAPTURE|PREG_SPLIT_NO_EMPTY); + foreach($nospaces as $p) + { + // parse the splited blocks with unquotted parameter pattern (ex: '/invite nicknamewithoutspace') + preg_match_all($pattern_noquote,$p[0],$res2,PREG_OFFSET_CAPTURE); + foreach( $res2[1] as $p2 ) + { + $p2[1] += $p[1]; + $params_res[] = $p2; + } + } + + // order the array by offset + $params = array(); + foreach($params_res as $p) $params[$p[1]] = $p[0]; + ksort($params); + $params = array_values($params); + $params = array_map("trim",$params); + + $result['cmdstr'] = $cmd_str; + $result['cmdname'] = $cmd; + $result['params'] = $params; + } + return $result; + } + } -?> +?> \ No newline at end of file Modified: trunk/src/phpfreechat.class.php =================================================================== --- trunk/src/phpfreechat.class.php 2006-12-07 14:38:34 UTC (rev 891) +++ trunk/src/phpfreechat.class.php 2006-12-07 17:55:46 UTC (rev 892) @@ -321,37 +321,28 @@ $u =& pfcUserConfig::Instance(); if ($c->debug) ob_start(); // capture output - + $xml_reponse = new xajaxResponse(); // check the command - $rawcmd = ""; - $clientid = ""; + $cmdstr = ""; + $cmdname = ""; + $clientid = ""; $recipient = ""; $recipientid = ""; - $param = ""; - $sender = ""; - //if (preg_match("/^\/([a-z]*) ([0-9a-f]*) ([0-9a-f]*)( (.*)|)/", $request, $res)) - //if (preg_match("/^\/([a-z]+) ([0-9a-f]+) ([0-9a-f]+) (.*)/", $request, $res)) - if (preg_match("/^\/([a-zA-Z0-9]+) ([0-9a-f]+) ([0-9a-f]+)( (.*)|)/", $request, $res)) - { - - $rawcmd = strtolower(isset($res[1]) ? $res[1] : ""); - $clientid = isset($res[2]) ? $res[2] : ""; - $recipientid = isset($res[3]) ? $res[3] : ""; - $param = isset($res[5]) ? $res[5] : ""; - $sender = $u->nick; - // $recipient = "home"; + $param = ""; + $sender = ""; - //if ($rawcmd == "join") - // trigger_error(var_export($res)); - - } - - //if ($rawcmd == "join") - //trigger_error("channels=".var_export($u->channels)); - //trigger_error("pvs=".var_export($u->privmsg)); + $res = pfcCommand::ParseCommand($request); + $cmdstr = isset($res['cmdstr']) ? $res['cmdstr'] : $request; + $cmdname = strtolower(isset($res['cmdname']) ? $res['cmdname'] : ''); + $clientid = isset($res['params'][0]) ? $res['params'][0] : ''; + $recipientid = isset($res['params'][1]) ? $res['params'][1] : ""; + $params = array_slice($res['params'],2); + $param = implode(" ",$params); // to keep compatibility (will be removed) + $sender = $u->nick; + // translate the recipientid to the channel name if (isset($u->channels[$recipientid])) { @@ -363,10 +354,10 @@ // @todo: move this code in a proxy - if ($rawcmd != "update" && - $rawcmd != "leave" && // do not open the pv tab when other user close the tab - $rawcmd != "quit" && - $rawcmd != "privmsg2") + if ($cmdname != "update" && + $cmdname != "leave" && // do not open the pv tab when other user close the tab + $cmdname != "quit" && + $cmdname != "privmsg2") { // alert the other from the new pv // (warn other user that someone talk to him) @@ -435,10 +426,11 @@ } - $cmd =& pfcCommand::Factory($rawcmd); + $cmd =& pfcCommand::Factory($cmdname); $cmdp = array(); $cmdp["clientid"] = $clientid; $cmdp["param"] = $param; + $cmdp["params"] = $params; $cmdp["sender"] = $sender; $cmdp["recipient"] = $recipient; $cmdp["recipientid"] = $recipientid; @@ -455,7 +447,7 @@ $cmd =& pfcCommand::Factory("error"); $cmdp = array(); $cmdp["clientid"] = $clientid; - $cmdp["param"] = _pfc("Unknown command [%s]",stripslashes("/".$rawcmd." ".$param)); + $cmdp["param"] = _pfc("Unknown command [%s]",stripslashes("/".$cmdname." ".$param)); $cmdp["sender"] = $sender; $cmdp["recipient"] = $recipient; $cmdp["recipientid"] = $recipientid; @@ -467,8 +459,8 @@ // do not update twice // do not update when the user just quit - if ($rawcmd != "update" && - $rawcmd != "quit" && + if ($cmdname != "update" && + $cmdname != "quit" && (!isset($u->nick) || $u->nick != "")) { // force an update just after a command is sent Added: trunk/testcase/parsecommand.php =================================================================== --- trunk/testcase/parsecommand.php (rev 0) +++ trunk/testcase/parsecommand.php 2006-12-07 17:55:46 UTC (rev 892) @@ -0,0 +1,51 @@ +<?php + +require_once dirname(__FILE__).'/../src/pfccommand.class.php'; + +$results = array(); +$results[] = array('cmdstr' => '/cmdname', + 'cmdname' => 'cmdname', + 'params' => array()); +$results[] = array('cmdstr' => '/cmdname "param1" "param2"', + 'cmdname' => 'cmdname', + 'params' => array('param1','param2')); +$results[] = array('cmdstr' => '/cmdname "param1" "param2" "param3"', + 'cmdname' => 'cmdname', + 'params' => array('param1','param2','param3')); +$results[] = array('cmdstr' => '/cmdname "param1 with spaces" "param2 with spaces"', + 'cmdname' => 'cmdname', + 'params' => array('param1 with spaces','param2 with spaces')); +$results[] = array('cmdstr' => '/cmdname000 "param1" "param2"', + 'cmdname' => 'cmdname000', + 'params' => array('param1','param2')); +$results[] = array('cmdstr' => '/cmdname param1 param2', + 'cmdname' => 'cmdname', + 'params' => array('param1','param2')); +$results[] = array('cmdstr' => '/cmdname "param1 with spaces" param2 param3', + 'cmdname' => 'cmdname', + 'params' => array('param1 with spaces','param2','param3')); +$results[] = array('cmdstr' => '/cmdname "param1" param2 "param3 with spaces" param4', + 'cmdname' => 'cmdname', + 'params' => array('param1', 'param2', 'param3 with spaces', 'param4')); +$results[] = array('cmdstr' => '/cmdname "param1""param2"', + 'cmdname' => 'cmdname', + 'params' => array('param1', 'param2')); +$results[] = array('cmdstr' => '/cmdname "param1withoutspace"', + 'cmdname' => 'cmdname', + 'params' => array('param1withoutspace')); +echo '<pre>'; +for($i = 0; $i<count($results); $i++) +{ + $command = $results[$i]['cmdstr']; + $result = pfcCommand::ParseCommand($command); + if ($result == $results[$i]) + echo "OK => $command\n"; + else + { + print_r($result); + echo "KO => $command\n"; + } +} +echo '</pre>'; + +?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |