Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.00 KB | None | 0 0
  1. <?php
  2.  
  3. class netIrc {
  4.     // Clearbricks' netSocket
  5.     protected $netSocket = null;            # Instance of netSocket
  6.     protected $netSocketIterator = null;    # Instance of netSocketIterator
  7.    
  8.     // IRC
  9.     protected $ircHost = null;              # Server host
  10.     protected $ircPort = null;              # Server port
  11.     protected $ircNick = null;              # Nickname used
  12.     protected $ircIdent = null;             # Ident used
  13.     protected $ircRealname = null;          # Realname used
  14.     protected $ircChannels = null;          # Channels joined
  15.     protected $ircMotd = null;              # Server MOTD
  16.     protected $ircLoggedIn = false;         # Connected or not
  17.     protected $ircBuffers = null;           # Send queues
  18.     protected $ircLine = null;              # Last line read from the server
  19.     protected $ircChannelModes = array();   # Channel modes
  20.     protected $ircNickPrefixes = array();   # Nicknames prefixes
  21.     protected $ircLastReceived = null;      # Last received time
  22.     protected $ircLoginSent = false;
  23.    
  24.     // Internal
  25.     protected $eventHandlers = array();     # Event handlers
  26.     protected $debugEnabled = true;         # Debug to stdout or not
  27.  
  28.     // Ticker
  29.     protected $tickerInterval = 0;          # Actual ticker interval
  30.     protected $tickerMax = 500000;          # Max ticker interval
  31.     protected $tickerMin = 10000;           # Min ticker interval
  32.     protected $tickerInc = 10000;           # Ticker incrementation
  33.    
  34.    
  35.     public function __construct($host,$port,$nick,$ident,$realname)
  36.     {
  37.         // Setting up vars...
  38.         $this->ircHost = $host;
  39.         $this->ircPort = (int) $port;
  40.         $this->ircNick = $nick;
  41.         $this->ircIdent = $ident;
  42.         $this->ircRealname = $realname;
  43.        
  44.         // Setting up sending queues
  45.         $this->ircBuffers = range(1,6);
  46.         foreach ($this->ircBuffers as &$v) { $v = array(); }
  47.        
  48.         // Setting up handlers
  49.         $this->eventHandlers = array();
  50.        
  51.        
  52.     }
  53.    
  54.     public function connect() {
  55.         //if ($this->ircLoggedIn) { return true; }
  56.        
  57.         unset ($this->netSocketIterator);
  58.         $this->ircLoggedIn = false;
  59.         $this->ircLoginSent = false;
  60.         if (!$this->netSocket instanceof netSocket)
  61.         {
  62.             $this->netSocket = new netSocket($this->ircHost,$this->ircPort);
  63.         }
  64.        
  65.         if ($this->netSocket->isOpen())
  66.         {
  67.             $this->netSocket->close();
  68.             unset($this->netSocketIterator);
  69.         }
  70.        
  71.         $this->netSocketIterator = $this->netSocket->open();
  72.         $this->netSocket->setBlocking(0);
  73.        
  74.         return true;
  75.     }
  76.     #################################
  77.     #       HANDLERS FUNCTIONS      #
  78.     #################################
  79.    
  80.     public function registerHandler($type,$callback)
  81.     {
  82.         if (is_callable($callback))
  83.         {
  84.             if (!isset($this->eventHandlers[$type]))
  85.             {
  86.                 $this->eventHandlers[$type] = array();
  87.             }
  88.            
  89.             $this->eventHandlers[$type][] = $callback;
  90.             $this->__debug('|| INTERNAL: Adding handler for'.$type);
  91.             return true;
  92.         } else {
  93.             $this->__debug('|| INTERNAL: WARNING: Callback not added for '.$type.', invalid callback supplied');
  94.         }
  95.     }
  96.    
  97.     protected function __callHandler($type,$data)
  98.     {
  99.         if (is_callable(array($this,'__on'.$type)))
  100.         {
  101.             $this->__debug('|| INTERNAL: Calling internal handler for '.$type);
  102.             call_user_func(array($this,'__on'.$type),$data);
  103.         }
  104.        
  105.         if (isset($this->eventHandlers[$type]))
  106.         {
  107.             foreach ($this->eventHandlers[$type] as &$v)
  108.             {
  109.                 if (is_callable($v))
  110.                 {
  111.                     $this->__debug('|| INTERNAL: Calling external handler for '.$type);
  112.                     call_user_func($v,$data);
  113.                    
  114.                 }
  115.             }
  116.         }
  117.     }
  118.    
  119.     #################################
  120.     #       PUBLIC IRC COMMANDS     #
  121.     #################################
  122.    
  123.     public function sendUser($ident,$realname,$priority = 0)
  124.     {
  125.         $this->__send('USER '.$ident.' - - :'.$realname,$priority);
  126.     }
  127.    
  128.     public function sendJoin($channel,$key = null,$priority = 2)
  129.     {
  130.         $this->__send('JOIN '.$channel.' '.$key,$priority);
  131.     //  $this->__send('NAMES '.$channel,$priority);
  132.        
  133.         $this->ircChannels[$channel] = array();
  134.     }
  135.    
  136.     public function sendPrivmsg($target,$message,$priority = 3)
  137.     {
  138.         $this->__send('PRIVMSG '.$target.' :'.$message,$priority);
  139.     }
  140.    
  141.     public function sendNotice($target,$message,$priority = 3)
  142.     {
  143.         $this->__send('NOTICE '.$target.' :'.$message,$priority);
  144.     }
  145.    
  146.     public function sendNick($newnick,$priority = 3)
  147.     {
  148.         $this->__send('NICK '.$newnick,$priority);
  149.     }
  150.    
  151.     public function sendRaw($raw,$priority = 3)
  152.     {
  153.         $this ->__send($raw,$priority);
  154.     }
  155.    
  156.     public function sendCtcpReq($target,$message,$priority = 3)
  157.     {
  158.         $this->__send('PRIVMSG '.$target.' :'.$message.'',$priority);
  159.     }
  160.    
  161.     public function sendCtcpRep($target,$message,$priority = 3)
  162.     {
  163.         $this->__send('NOTICE '.$target.' :'.$message.'',$priority);
  164.     }
  165.    
  166.     public function sendAction($target,$message,$priority = 3)
  167.     {
  168.         $this->__send('PRIVMSG '.$target.' :ACTION '.$message.'',$priority);
  169.     }
  170.    
  171.     public function getNick() { return $this->ircNick; }
  172.    
  173.     public function getMotd() { return $this->ircMotd; }
  174.    
  175.     public function getChannels() { return $this->ircChannels; }
  176.    
  177.     public function matchMask($mask,$reg)
  178.     {
  179.         return preg_match('/'.str_replace('\*','(.+)',preg_quote($reg,'/')).'/',$mask);
  180.     }
  181.    
  182.     #################################
  183.     #       MAIN LOOP FUNCTION      #
  184.     #################################
  185.    
  186.     public function listen()
  187.     {
  188.         foreach ($this->netSocketIterator as $v)
  189.         {
  190.             if ($this->__rawReceive($v) || $this->__checkBuffer())
  191.             {
  192.                 $this->tickerInterval = $this->tickerMin;
  193.                 $this->ircLastReceived = time();
  194.             } else {
  195.                 $this->tickerInterval += $this->tickerInc;
  196.                 if ($this->ircLoggedIn)
  197.                 {
  198.                     if ((time() - $this->ircLastReceived) >= 5)
  199.                     {
  200.                         $this->sendCtcpReq($this->ircNick,'PING',1);
  201.                     }
  202.                    
  203.                     if ((time() - $this->ircLastReceived) >= 10) {
  204.                         $this->__debug('|| INTERNAL: WARNING: Seems we\'re not connected... restarting...');
  205.                         $this->connect();
  206.                     }
  207.                 }
  208.             }
  209.             if ($this->tickerInterval >= $this->tickerMax) {
  210.                 $this->tickerInterval = $this->tickerMax;
  211.             }
  212.             usleep($this->tickerInterval);
  213.            
  214.        
  215.         }
  216.     }
  217.    
  218.     #################################
  219.     #       INTERNAL HANDLERS       #
  220.     #################################
  221.    
  222.     protected function __onNOTICEAUTH($data)
  223.     {
  224.         if (!$this->ircLoginSent)
  225.         {
  226.             $this->sendNick($this->ircNick,0);
  227.             $this->sendUser($this->ircIdent,$this->ircRealname);
  228.             $this->ircLoginSent = true;
  229.         }
  230.     }
  231.     protected function __onPING($data)
  232.     {
  233.         $this->__send('PONG :'.$data->args[0],0);
  234.     }
  235.    
  236.     protected function __on005($data)
  237.     {
  238.         /*
  239.         [0] => CMDS=KNOCK,MAP,DCCALLOW,USERIP
  240.         [1] => NAMESX
  241.         [2] => SAFELIST
  242.         [3] => HCN
  243.         [4] => MAXCHANNELS=20
  244.         [5] => CHANLIMIT=#:20
  245.         [6] => MAXLIST=b:60,e:60,I:60
  246.         [7] => NICKLEN=30
  247.         [8] => CHANNELLEN=32
  248.         [9] => TOPICLEN=307
  249.         [10] => KICKLEN=307
  250.         [11] => AWAYLEN=307
  251.         [12] => MAXTARGETS=20
  252.         [13] => are supported by this server
  253.  
  254.         [0] => WALLCHOPS
  255.         [1] => WATCH=128
  256.         [2] => SILENCE=15
  257.         [3] => MODES=12
  258.         [4] => CHANTYPES=#
  259.         [5] => PREFIX=(ohv)@%+
  260.         [6] => CHANMODES=beIqa,kfL,lj,psmntirRcOAQKVCuzNSMTG
  261.         [7] => NETWORK=EpiKnet
  262.         [8] => CASEMAPPING=ascii
  263.         [9] => EXTBAN=~,cqnr
  264.         [10] => ELIST=MNUCT
  265.         [11] => STATUSMSG=@%+
  266.         [12] => EXCEPTS
  267.         [13] => are supported by this server
  268.  
  269.         [0] => INVEX
  270.         [1] => are supported by this server
  271.         */
  272.         $args = $data->args;
  273.         array_pop($args);
  274.         foreach ($args as $arg)
  275.         {
  276.             if (strpos($arg,'=')) {
  277.                 $arg = explode('=',$arg);
  278.                
  279.                 switch ($arg[0]) {
  280.                     case 'PREFIX':                     
  281.                         $modes = str_split(substr($arg[1],1,strpos($arg[1],')')-1));
  282.                         $prefixes = str_split(substr($arg[1],strpos($arg[1],')')+1));
  283.                        
  284.                         while (true) {
  285.                             $prefix = array_shift($prefixes);
  286.                             $mode = array_shift($modes);
  287.                            
  288.                             if (!$prefix || !$mode) { break; }
  289.                             $this->ircNickPrefixes[$prefix] = $mode;
  290.                         }
  291.                         break;
  292.                    
  293.                     case 'CHANMODES':
  294.                         /*
  295.                            o  Type A (0): Modes that add or remove an address to or from a list.
  296.                               These modes always take a parameter when sent by the server to a
  297.                               client; when sent by a client, they may be specified without a
  298.                               parameter, which requests the server to display the current
  299.                               contents of the corresponding list on the channel to the client.
  300.                            o  Type B (1): Modes that change a setting on the channel.  These modes
  301.                               always take a parameter.
  302.                            o  Type C (2): Modes that change a setting on the channel. These modes
  303.                               take a parameter only when set; the parameter is absent when the
  304.                               mode is removed both in the client's and server's MODE command.
  305.                            o  Type D (3): Modes that change a setting on the channel. These modes
  306.                               never take a parameter.
  307.                         */
  308.                         $this->ircChannelModes = explode(',',$arg[1]);
  309.                        
  310.                         break;
  311.                 }
  312.             }
  313.            
  314.         }
  315.     }
  316.    
  317.     protected function __on311($data)
  318.     {
  319.         foreach ($this->ircChannels as &$channel)
  320.         {
  321.             if (isset($channel->users[$data->args[0]]))
  322.             {
  323.                 $channel->users[$data->args[0]]->nick = $data->args[0];
  324.                 $channel->users[$data->args[0]]->ident = $data->args[1];
  325.                 $channel->users[$data->args[0]]->host = $data->args[2];
  326.                 $channel->users[$data->args[0]]->mask = $data->args[0].'!'.$data->args[1].'@'.$data->args[2];
  327.                 $channel->users[$data->args[0]]->realname = $data->args[4];
  328.             }
  329.         }
  330.     }
  331.    
  332.     protected function __on324($data)
  333.     {
  334.         //print_r($data);
  335.         $this->ircChannels[$data->args[0]]->modes = $data->args[1];
  336.     }
  337.    
  338.     protected function __on332($data)
  339.     {
  340.         if (isset($this->ircChannels[$data->args[0]]))
  341.         {
  342.             $this->ircChannels[$data->args[0]]->topic = $data->args[1];
  343.         }
  344.     }
  345.    
  346.     protected function __on333($data)
  347.     {
  348.         $this->ircChannels[$data->args[0]]->topic_by = $data->args[1];
  349.         $this->ircChannels[$data->args[0]]->topic_time = $data->args[2];
  350.     }
  351.    
  352.     protected function __on352($data)
  353.     {
  354.         if ($data->args[0] == '*')
  355.         {
  356.             print_r($data);
  357.         } else {
  358.             $channel = $data->args[0];
  359.             $ident = $data->args[1];
  360.             $host = $data->args[2];
  361.             $nick = $data->args[4];
  362.             $realname = implode(' ',array_slice(explode(' ',$data->args[6],2),1));
  363.            
  364.             $this->ircChannels[$channel]->users[$nick]->nick = $nick;
  365.             $this->ircChannels[$channel]->users[$nick]->ident = $ident;
  366.             $this->ircChannels[$channel]->users[$nick]->host = $host;
  367.             $this->ircChannels[$channel]->users[$nick]->mask = $nick.'!'.$ident.'@'.$host;
  368.             $this->ircChannels[$channel]->users[$nick]->realname = $realname;
  369.         }
  370.     }
  371.    
  372.     protected function __on353($data)
  373.     {
  374.         $users = explode(' ',$data->args[2]);
  375.         $channel = $data->args[1];
  376.         foreach ($users as $user)
  377.         {
  378.             $_user = str_split($user);
  379.             $_modes = '';
  380.             $nick = '';
  381.             foreach ($_user as $pos => $char)
  382.             {
  383.                 if (isset($this->ircNickPrefixes[$char]))
  384.                 {
  385.                     $_modes .= $this->ircNickPrefixes[$char];
  386.                 } else {
  387.                     $nick = implode(array_slice($_user,$pos));
  388.                     break;
  389.                 }
  390.             }
  391.             if (!isset($this->ircChannels[$channel]->users[$nick]))
  392.             {
  393.                 $this->ircChannels[$channel]->users[$nick] = new UserStack();
  394.             }
  395.             $this->ircChannels[$channel]->users[$nick]->nick = $nick;
  396.             $this->ircChannels[$channel]->users[$nick]->modes = $_modes;
  397.         }
  398.     }
  399.    
  400.     protected function __on366($data)
  401.     {
  402.         $this->sendRaw('WHO '.$data->args[0],1);
  403.     }
  404.    
  405.     protected function __on372($data) // MOTD lines
  406.     {
  407.         $this->ircMotd[] = $data->args[0];
  408.     }
  409.    
  410.     protected function __on375($data) // MOTD start
  411.     {
  412.         $this->ircMotd = array();
  413.     }
  414.    
  415.     protected function __on376($data)
  416.     {
  417.         $this->__send('PROTOCTL NAMESX',1); // For enhanced NAMES listings
  418.         $this->ircLoggedIn = true;
  419.     }
  420.    
  421.     protected function __on433($data)
  422.     {
  423.         if (!$this->ircLoggedIn)
  424.         {
  425.             $this->ircNick = $this->ircNick.'`';
  426.             $this->sendNick($this->ircNick,0);
  427.         }
  428.     }
  429.    
  430.     protected function __onERROR($data)
  431.     {
  432.         $this->connect();
  433.     }
  434.    
  435.     protected function __onCTCPREQ($data)
  436.     {
  437.         if ($data->args[0] == 'VERSION')
  438.         {
  439.             $this->sendCtcpRep($data->source->nick,'VERSION WolfStats // PHP NetIrc by Saymonz');
  440.         }
  441.     }
  442.    
  443.     protected function __onNICK($data)
  444.     {
  445.         if ($data->source->nick == $this->ircNick) { $this->ircNick = $data->target; } // Own nick change
  446.         foreach ($this->ircChannels as &$channel)
  447.         {
  448.             if (isset($channel->users[$data->source->nick]))
  449.             {
  450.                 $channel->users[$data->source->nick]->nick = $data->target;
  451.                 $channel->users[$data->target] = $channel->users[$data->source->nick];
  452.                 unset($channel->users[$data->source->nick]);
  453.             }
  454.         }
  455.     }
  456.    
  457.     protected function __onKICK($data)
  458.     {
  459.         if ($data->args[0] == $this->ircNick)
  460.         {
  461.             unset($this->ircChannels[$data->target]);
  462.             $this->sendJoin($data->target);
  463.         } else { unset($this->ircChannels[$data->target]->user[$data->args[0]]); }
  464.     }
  465.    
  466.     protected function __onPART($data)
  467.     {
  468.         if ($data->source->nick == $this->ircNick)
  469.         {
  470.             unset($this->ircChannels[$data->target]);
  471.         } else {
  472.             unset($this->ircChannels[$data->target]->users[$data->source->nick]);
  473.         }
  474.     }
  475.    
  476.     protected function __onJOIN($data)
  477.     {
  478.         if ($data->source->nick == $this->ircNick)
  479.         {
  480.             $this->ircChannels[$data->target] = new ChannelStack();
  481.             $this->ircChannels[$data->target]->users = array();
  482.             $this->sendRaw('WHO '.$data->target,1);
  483.             $this->sendRaw('MODE '.$data->target,1);
  484.         }
  485.        
  486.         $this->ircChannels[$data->target]->users[$data->source->nick] = new UserStack();
  487.         $this->ircChannels[$data->target]->users[$data->source->nick]->nick = $data->source->nick;
  488.         $this->ircChannels[$data->target]->users[$data->source->nick]->ident = $data->source->ident;
  489.         $this->ircChannels[$data->target]->users[$data->source->nick]->host = $data->source->host;
  490.         $this->ircChannels[$data->target]->users[$data->source->nick]->mask = $data->source->mask;
  491.         $this->sendRaw('WHOIS '.$data->source->nick,1);
  492.     }
  493.    
  494.     protected function __onMODE($data)
  495.     {
  496.         if (substr($data->target,0,1) == '#') # FIXME
  497.         {
  498.             $modes = str_split(array_shift($data->args));
  499.             foreach ($modes as $mode)
  500.             {
  501.                 switch ($mode)
  502.                 {
  503.                     case '+':
  504.                         $m = true;
  505.                     break;
  506.                    
  507.                     case '-':
  508.                         $m = false;
  509.                     break;
  510.                    
  511.                     default:
  512.                         if (in_array($mode,$this->ircNickPrefixes)) // mode utilisateur préfixé
  513.                         {
  514.                             $nick = array_shift($data->args);
  515.                             $um = $this->ircChannels[$data->target]->users[$nick]->modes;
  516.                            
  517.                             if ($m)
  518.                             {
  519.                                 $um .= $mode;
  520.                             } else {
  521.                                 $p = strpos($um,$mode);
  522.                                 $um = substr($um,0,$p).substr($um,$p+1);
  523.                             }
  524.                             $this->ircChannels[$data->target]->users[$nick]->modes = $um;
  525.                         } else {
  526.                             foreach ($this->ircChannelModes as $k => $v)
  527.                             {
  528.                                 if (strpos($v,$mode)) { break; }
  529.                             }
  530.                            
  531.                             switch ($k)
  532.                             {
  533.                                 case 0:
  534.                                 case 1:
  535.                                     array_shift($data->args); // Cat. A&B, always shift a parameter
  536.                                 break;
  537.                                
  538.                                 case 2:
  539.                                     if ($m) { array_shift($data->args); } // Cat. C, shift a parameter only when set
  540.                                 break;
  541.                                
  542.                                 case 3:
  543.                                     // Cat. D, never shift a parameter
  544.                                 break;
  545.                                
  546.                                 default:
  547.                                     /*
  548.                                      * If the server sends any additional types after these 4, the client
  549.                                      * MUST ignore them; this is intended to allow future extension of this
  550.                                      * token.
  551.                                      *
  552.                                      * -> DO NOTHING
  553.                                      */
  554.                                 break;
  555.                             }
  556.                         }
  557.                     break;
  558.                 }
  559.             }
  560.         }
  561.     }
  562.    
  563.     protected function __onQUIT($data)
  564.     {
  565.         foreach ($this->ircChannels as &$channel)
  566.         {
  567.             if (isset($channel->users[$data->source->nick]))
  568.             {
  569.                 unset($channel->users[$data->source->nick]);
  570.             }
  571.         }
  572.     }
  573.    
  574.     protected function __onTOPIC($data)
  575.     {
  576.         $this->ircChannels[$data->target]->topic_by = $data->source->nick;
  577.         $this->ircChannels[$data->target]->topic = $data->args[0];
  578.         $this->ircChannels[$data->target]->topic_time = time();
  579.     }
  580.    
  581.     protected function __rawReceive(&$data)
  582.     {
  583.         $parsed = $this->__ircParser($data);
  584.         if (!$parsed) { return false; }
  585.         $this->__debug('<< '.$parsed->raw);
  586.         $this->__callHandler($parsed->command,$parsed);
  587.         return true;
  588.     }
  589.    
  590.     public function __ircParser($in,$strip = false) {
  591.         $in = trim(text::toUTF8($in));
  592.         if ($in == null) { return false; }
  593.        
  594.         $parsed = new stdClass();
  595.         $parsed->source = new stdClass();
  596.         $parsed->command = null;
  597.         $parsed->target = null;
  598.         $parsed->args = array();
  599.        
  600.         if ($strip)
  601.         {
  602.             $in = $this->__stripper($in);
  603.         } else {
  604.             $parsed->stripped = $this->__ircParser($in,true);
  605.         }
  606.        
  607.         $parsed->raw = $in;
  608.        
  609.         $in = explode(' ',$in);
  610.         foreach ($in as $k => $v)
  611.         {
  612.             switch ($k)
  613.             {
  614.                 case 0:
  615.                     if (substr($v,0,1) == ':')
  616.                     {
  617.                         $v = substr($v,1);
  618.                         if (preg_match('#(.+)!(.+)@(.+)#',$v,$m))
  619.                         {
  620.                             $parsed->source->nick = $m[1];
  621.                             $parsed->source->ident = $m[2];
  622.                             $parsed->source->host = $m[3];
  623.                             $parsed->source->mask = $m[0];
  624.                         } else { $parsed->source = $v; }
  625.                     } else {
  626.                         if ($v == 'PING')
  627.                         {
  628.                             $parsed->command = 'PING';
  629.                             $parsed->args[] = substr(implode(' ',array_slice($in,1)),1);
  630.                             break 2;
  631.                            
  632.                         }
  633.                        
  634.                         if ($v == 'ERROR')
  635.                         {
  636.                             $parsed->command = 'ERROR';
  637.                             $parsed->args[] = substr(implode(' ',array_slice($in,1)),1);
  638.                             break 2;
  639.                            
  640.                         }
  641.                     }
  642.                     break;
  643.                    
  644.                 case 1:
  645.                     if ($v == 'NOTICE' && $in[2] == 'AUTH')
  646.                     {
  647.                         $parsed->command = 'NOTICEAUTH';
  648.                         $parsed->args[] = substr(implode(' ',array_slice($in,3)),1);
  649.                         break 2;
  650.                     } elseif ($v == 'QUIT') {
  651.                         $parsed->command = $v;
  652.                         $parsed->args[] = substr(implode(' ',array_slice($in,2)),1);
  653.                         break 2;
  654.                     } else {
  655.                         $parsed->command = $v;
  656.                     }
  657.                     break;
  658.                    
  659.                 case 2:
  660.                     if ($parsed->command == 'NICK') { $parsed->target = substr($v,1); break; }
  661.                     if ($parsed->command == 'JOIN') { $parsed->target = substr($v,1); break; }
  662.                     $parsed->target = $v;
  663.                     break;
  664.                    
  665.                 default:
  666.                     if (substr($v,0,1) == ':')
  667.                     {
  668.                         $parsed->args[] = substr(implode(' ',array_slice($in,$k)),1);
  669.                         if (preg_match('#\x01(.+)\x01#',$parsed->args[0],$m))
  670.                         {
  671.                             if ($parsed->command == 'PRIVMSG')
  672.                             {
  673.                                 if (substr($m[1],0,6) == 'ACTION')
  674.                                 {
  675.                                     $parsed->command = 'ACTION';
  676.                                     $m[1] = substr($m[1],7);
  677.                                 } else {
  678.                                     $parsed->command = 'CTCPREQ';
  679.                                 }
  680.                             } elseif ($parsed->command == 'NOTICE') {
  681.                                 $parsed->command = 'CTCPREP';
  682.                             }
  683.                             $parsed->args[0] = $m[1];
  684.                         }
  685.                         break 2;
  686.                     } else { $parsed->args[] = $v; }
  687.                     break;
  688.             }
  689.         }
  690.        
  691.         return $parsed;
  692.     }
  693.    
  694.     #################################
  695.     #       SEND QUEUES FUNCS       #
  696.     #################################
  697.    
  698.     protected function __send($data,$priority = 3)
  699.     {
  700.         $data = trim(text::toUTF8($data));
  701.         if ($data == null) { return; }
  702.        
  703.         if ($priority == 0)
  704.         {
  705.             $this->__debug('>> '.$data);
  706.             $this->netSocket->write($data."\n");
  707.         } else { array_push($this->ircBuffers[$priority],$data); }
  708.     }
  709.    
  710.     protected function __checkBuffer()
  711.     {
  712.         if (!$this->ircLoggedIn) { return false; }
  713.         foreach ($this->ircBuffers as &$buffer)
  714.         {
  715.             $data = array_shift($buffer);
  716.             if ($data !== null) { $this->__send($data,0); return true; }
  717.         }
  718.         return false;
  719.     }
  720.    
  721.     protected function __flushBuffer() { while ($this->__checkBuffer()) { continue; } }
  722.    
  723.     /*
  724.     * IRC Special Chars :
  725.     *  CTCP delimiter // We don't strip it, used to detect CTCPs and ACTIONs
  726.     *  Bold
  727.     *  Colors
  728.     *  Reverse
  729.     *  Underlined
  730.     *  Italic
  731.     *
  732.     * Original regex: (don't remember where it came from)
  733.     * #\x0f|\x1f|\x02|\x03(?:\d{1,2}(?:,\d{1,2})?)?#
  734.     */
  735.    
  736.     protected function __stripper($input) { return preg_replace("#\x16|\x1d|\x1f|\x02|\x03(?:\d{1,2}(?:,\d{1,2})?)?#",'',$input); }
  737.    
  738.     protected function __debug($x)
  739.     {
  740.         if (!$this->debugEnabled) { return false; }
  741.         if ($this->netSocket instanceof netSocket && $this->netSocket->isOpen())
  742.         {
  743.             $y = $this->netSocketIterator->key();
  744.         } else {
  745.             $y = 0;
  746.         }
  747.        
  748.         echo '#'.$y."\t".$x."\n";
  749.         return true;
  750.     }
  751. }
  752.  
  753. class ChannelStack { // Dummy class for channel informations
  754.     public $topic;
  755.     public $topic_by;
  756.     public $topic_time;
  757.     public $modes;
  758.     public $users = array();
  759.     public $banlist = array();
  760. }
  761.  
  762. class UserStack { // Dummy class for user informations
  763.     public $nick;
  764.     public $ident;
  765.     public $host;
  766.     public $mask;
  767.     public $realname;
  768.     public $modes;
  769. }
  770. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement