libirc.php
changeset 32 236fd4f65752
parent 31 d75124700259
child 33 c3179049f670
equal deleted inserted replaced
31:d75124700259 32:236fd4f65752
   212   {
   212   {
   213     $message = str_replace("\r\n", "\n", $message);
   213     $message = str_replace("\r\n", "\n", $message);
   214     $message = explode("\n", $message);
   214     $message = explode("\n", $message);
   215     foreach ( $message as $line )
   215     foreach ( $message as $line )
   216     {
   216     {
       
   217       $line = $this->filter_message($line);
   217       $this->put("PRIVMSG $nick :$line\r\n");
   218       $this->put("PRIVMSG $nick :$line\r\n");
   218     }
   219     }
       
   220   }
       
   221   
       
   222   /**
       
   223   * Parse bold (<b>...</b>) tags and color tags in a text into IRC speak, and process /me commands. Colors are <cyan>...</cyan>, specify background with <fg:bg>...</fgcolor:bgcolor>. Valid colors are white, black, navy, green, red, maroon, purple, orange, yellow, lime, teal, aqua, cyan, blue, pink, grey, and silver
       
   224    * @param string Text to filter
       
   225    * @return string
       
   226    */
       
   227   
       
   228   function filter_message($text)
       
   229   {
       
   230     $text = preg_replace('#<b>(.*?)</b>#is', "\x02$1\x02", $text);
       
   231     if ( preg_match('#^/me #i', $text) )
       
   232     {
       
   233       $text = "\x01ACTION " . preg_replace('#^/me #i', '', $text) . "\x01";
       
   234     }
       
   235     $supportedcolors = array('white', 'black', 'navy', 'green', 'red', 'maroon', 'purple', 'orange', 'yellow', 'lime', 'teal', 'cyan', 'blue', 'pink', 'grey', 'silver');
       
   236     $colors = implode('|', $supportedcolors);
       
   237     $supportedcolors = array_flip($supportedcolors);
       
   238     preg_match_all("#<((?:$colors)(?::(?:$colors))?)>(.*?)</\\1>#is", $text, $matches);
       
   239     foreach ( $matches[0] as $i => $match )
       
   240     {
       
   241       preg_match("#<(?:($colors)(?::($colors))?)>#i", $match, $colordata);
       
   242       $fgcolor = $supportedcolors[$colordata[1]];
       
   243       $bgcolor = $colordata[1] ? $supportedcolors[$colordata[2]] : '';
       
   244       $fgcolor = ( $fgcolor < 10 ) ? "0$fgcolor" : "$fgcolor";
       
   245       if ( is_int($bgcolor) )
       
   246         $bgcolor = ( $bgcolor < 10 ) ? ",0$bgcolor" : ",$bgcolor";
       
   247       $text = str_replace($match, "\x03{$fgcolor}{$bgcolor}{$matches[2][$i]}\x03", $text);
       
   248     }
       
   249     
       
   250     return $text;
   219   }
   251   }
   220   
   252   
   221   /**
   253   /**
   222    * The main event loop.
   254    * The main event loop.
   223    */
   255    */