02_BaseActiveModule.php - parse_com error

Bug #824707 reported by Thomas Juberg on 2011-08-11
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
BeBot
Undecided
Unassigned

Bug Description

Posted by BarkinMad on the forum.

I'm sorry to say that the function parse_com of BaseActiveModule class does not perform quite right. Extra spaces cause the value of $com['sub'] or $com['args'] to be null, depending on what the pattern is, and where the extra space is. That's another thing, it also doesn't work for multiple args. That said, here's a function, written by yours truly, to address those issues. It DOES have some limitations itself, it requires the first value of the pattern to be a variation on 'com' and try as i might, i couldn't get it to work with tabs or newlines, but it still handles better than the explode() based current function. If anyone can think up a way of removing the aforementioned limitations, go ahead, just be sure to give credit where it's due.

Code:
function comm_parse($msg, $breakdown = array('com', 'sub', 'args')){
  $delims = " \t\n";
  $delims .= $this->bot->commpre;

  $containerTemp = strtok($msg, $delims);
  $count = 0;
  while($containerTemp != FALSE){
   $tokens[$count] = $containerTemp;
   $count += 1;
   $containerTemp = strtok($delims);
  }
  $labelcount = count($breakdown);
  $subPresent = FALSE;
  for($i = 0; $i < $labelcount; $i++){
   if(strtolower($breakdown[$i]) == 'com'){
    $com[$breakdown[$i]] = $tokens[0];
   } else if(strtolower($breakdown[$i]) == 'sub'){
    $com[$breakdown[$i]] = $tokens[1];
    $subPresent = TRUE;
   } else {
    $toks = $this->trimm($tokens, $subPresent);
    $com[$breakdown[$i]] = $toks;
   }
  }
  return $com;
 }
 function trimm($msg, $subflag = TRUE){
  if($subflag){
   for($i = 2; $i < count($msg); $i++){
    $ret[$i-2] = $msg[$i];
   }
  }else{
   for($i = 1; $i < count($msg); $i++){
    $ret[$i-1] = $msg[$i];
   }
  }
  return $ret;
 }

This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers