libirc.php
changeset 8 0acb8d9a3194
parent 0 d02690a8552c
child 21 d86ea89358ec
equal deleted inserted replaced
7:1d6e762433fe 8:0acb8d9a3194
   110     // Init connection
   110     // Init connection
   111     $this->sock = fsockopen($this->host, $this->port);
   111     $this->sock = fsockopen($this->host, $this->port);
   112     if ( !$this->sock )
   112     if ( !$this->sock )
   113       throw new Exception('Could not make socket connection to host.');
   113       throw new Exception('Could not make socket connection to host.');
   114     
   114     
   115     stream_set_timeout($this->sock, 5);
   115     stream_set_timeout($this->sock, 1);
   116     
       
   117     // Wait for initial ident messages
       
   118     while ( $msg = $this->get() )
       
   119     {
       
   120     }
       
   121     
   116     
   122     // Send nick and username
   117     // Send nick and username
   123     $this->put("NICK $nick\r\n");
   118     $this->put("NICK $nick\r\n");
   124     $this->put("USER $username 0 * :$realname\r\n");
   119     $this->put("USER $username 0 * :$realname\r\n");
   125     
   120     
   126     // Wait for response and end of motd
   121     // wait for a mode +i or end of the motd
   127     $motd = '';
   122     while ( true )
   128     while ( $msg = $this->get() )
   123     {
   129     {
   124       $msg = $this->get();
   130       // Match particles
   125       if ( empty($msg) )
   131       $msg = trim($msg);
   126         continue;
   132       $mc = preg_match('/^:([A-z0-9\.-]+) ([0-9]+) [A-z0-9_-]+ :(.+)$/', $msg, $match);
   127       if ( ( strstr($msg, 'MODE') && strstr($msg, '+i') ) || strstr(strtolower($msg), 'end of /motd') )
   133       if ( !$mc )
       
   134       {
   128       {
   135         $mc = preg_match('/^:([A-z0-9_-]+)!([A-z0-9_-]+)@([A-z0-9_\.-]+) NOTICE [A-z0-9_-]+ :(.+)$/', $msg, $match);
   129         break;
   136         if ( !$mc )
       
   137           continue;
       
   138         // Look for a response from NickServ
       
   139         if ( $match[1] == 'NickServ' )
       
   140         {
       
   141           // Asking for auth?
       
   142           if ( strpos($match[4], 'IDENTIFY') )
       
   143           {
       
   144             // Yes, send password
       
   145             $this->privmsg('NickServ', "IDENTIFY $pass");
       
   146           }
       
   147         }
       
   148       }
   130       }
   149       list(, $host, $stat, $msg) = $match;
   131       if ( preg_match('/^PING :(.+?)$/', $msg, $match) )
   150       $motd .= "$msg";
   132       {
   151     }
   133         $this->put("PONG :{$match[1]}\r\n");
       
   134       }
       
   135     }
       
   136     
       
   137     // identify to nickserv
       
   138     $this->privmsg('NickServ', "IDENTIFY $pass");
   152     
   139     
   153     $this->nick = $nick;
   140     $this->nick = $nick;
   154     $this->user = $username;
   141     $this->user = $username;
   155   }
   142   }
   156   
   143   
   220       $data_trim = trim($data);
   207       $data_trim = trim($data);
   221       $match = self::parse_message($data_trim);
   208       $match = self::parse_message($data_trim);
   222       if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) )
   209       if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) )
   223       {
   210       {
   224         $this->put("PONG :{$pmatch[1]}\r\n");
   211         $this->put("PONG :{$pmatch[1]}\r\n");
       
   212         eval(eb_fetch_hook('event_ping'));
   225       }
   213       }
   226       else if ( $match )
   214       else if ( $match )
   227       {
   215       {
   228         // Received PRIVMSG or other mainstream action
   216         // Received PRIVMSG or other mainstream action
   229         if ( $match['action'] == 'JOIN' )
   217         if ( $match['action'] == 'JOIN' || $match['action'] == 'PART' )
   230           $channel =& $match['message'];
   218           $channel =& $match['message'];
   231         else
   219         else
   232           $channel =& $match['target'];
   220           $channel =& $match['target'];
   233           
   221           
   234         if ( !preg_match('/^[#!&\+]/', $channel) )
   222         if ( !preg_match('/^[#!&\+]/', $channel) )
   235         {
   223         {
   236           // Private message from user
   224           // Private message from user
   237           $result = $this->handle_privmsg($data);
   225           $result = $this->handle_privmsg($data);
   238           stream_set_timeout($this->sock, 0xFFFFFFFE);
   226           @stream_set_timeout($this->sock, 0xFFFFFFFE);
   239         }
   227         }
   240         else if ( isset($this->channels[strtolower($channel)]) )
   228         else if ( isset($this->channels[strtolower($channel)]) )
   241         {
   229         {
   242           // Message into channel
   230           // Message into channel
   243           $chan =& $this->channels[strtolower($channel)];
   231           $chan =& $this->channels[strtolower($channel)];
   244           $func = $chan->get_handler();
   232           $func = $chan->get_handler();
   245           $result = @call_user_func($func, $data, $chan);
   233           $result = @call_user_func($func, $data, $chan);
   246           stream_set_timeout($this->sock, 0xFFFFFFFE);
   234           @stream_set_timeout($this->sock, 0xFFFFFFFE);
   247         }
   235         }
   248         if ( $result == 'BREAK' )
   236         if ( $result == 'BREAK' )
   249         {
   237         {
   250           break;
   238           break;
   251         }
   239         }
   395   
   383   
   396   function __construct($channel, $handler, $parent)
   384   function __construct($channel, $handler, $parent)
   397   {
   385   {
   398     $this->parent = $parent;
   386     $this->parent = $parent;
   399     $this->parent->put("JOIN $channel\r\n");
   387     $this->parent->put("JOIN $channel\r\n");
   400     stream_set_timeout($this->parent->sock, 3);
   388     // stream_set_timeout($this->parent->sock, 3);
   401     while ( $msg = $this->parent->get() )
   389     // while ( $msg = $this->parent->get() )
   402     {
   390     // {
   403       // Do nothing
   391     //   // Do nothing
   404     }
   392     // }
   405     $this->channel_name = $channel;
   393     $this->channel_name = $channel;
   406     $this->handler = $handler;
   394     $this->handler = $handler;
       
   395     eval(eb_fetch_hook('event_self_join'));
   407   }
   396   }
   408   
   397   
   409   /**
   398   /**
   410    * Returns the channel name
   399    * Returns the channel name
   411    * @return string
   400    * @return string