libirc.php
author Dan
Tue, 20 Jan 2009 22:08:07 -0500
changeset 51 508400fc5282
parent 40 1855846cbdab
permissions -rw-r--r--
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     1
<?php
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     2
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     3
/**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     4
 * PHP IRC Client library
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     5
 * Copyright (C) 2008 Dan Fuhry. All rights reserved.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     6
 */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     7
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     8
/**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
     9
 * Version number
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    10
 * @const string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    11
 */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    12
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    13
define('REQUEST_IRC_VERSION', '0.1');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    14
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    15
/**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    16
 * The base class for an IRC session.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    17
 */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    18
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    19
class Request_IRC
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    20
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    21
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    22
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    23
   * Hostname
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    24
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    25
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    26
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    27
  private $host = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    28
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    29
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    30
   * Port number
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    31
   * @var int
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    32
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    33
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    34
  private $port = 6667;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    35
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    36
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    37
   * The socket for the connection.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    38
   * @var resource
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    39
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    40
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    41
  public $sock = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    42
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    43
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    44
   * Channel objects, associative array
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    45
   * @var array
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    46
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    47
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    48
  public $channels = array();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    49
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    50
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    51
   * The function called when a private message is received.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    52
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    53
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    54
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    55
  private $privmsg_handler = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    56
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    57
  /**
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    58
   * The function called when a timeout is suspected.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    59
   * @var string
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    60
   */
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    61
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    62
  private $timeout_warning_handler = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    63
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    64
  /**
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    65
   * The function called when a timeout is confirmed.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    66
   * @var string
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    67
   */
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    68
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    69
  private $timeout_error_handler = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    70
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
    71
  /**
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    72
   * Switch to track if quitted or not. Helps avoid quitting the connection twice thus causing write errors.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    73
   * @var bool
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    74
   * @access private
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    75
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    76
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    77
  protected $quitted = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    78
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    79
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    80
   * The nickname we're connected as. Not modified once connected.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    81
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    82
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    83
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    84
  public $nick = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    85
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    86
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    87
   * The username we're connected as. Not modified once connected.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    88
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    89
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    90
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    91
  public $user = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    92
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    93
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    94
   * Constructor.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    95
   * @param string Hostname
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    96
   * @param int Port number, defaults to 6667
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    97
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    98
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    99
  public function __construct($host, $port = 6667)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   100
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   101
    // Check hostname
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   102
    if ( !preg_match('/^(([a-z0-9-]+\.)*?)([a-z0-9-]+)$/', $host) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   103
      die(__CLASS__ . ': Invalid hostname');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   104
    $this->host = $host;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   105
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   106
    // Check port
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   107
    if ( is_int($port) && $port >= 1 && $port <= 65535 )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   108
      $this->port = $port;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   109
    else
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   110
      die(__CLASS__ . ': Invalid port');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   111
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   112
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   113
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   114
   * Sets parameters and opens the connection.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   115
   * @param string Nick
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   116
   * @param string User
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   117
   * @param string Real name
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   118
   * @param string NickServ password
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   119
   * @param int Flags, defaults to 0.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   120
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   121
  
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   122
  public function connect($nick, $username, $realname, $pass = false, $flags = 0)
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   123
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   124
    // Init connection
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   125
    $this->sock = fsockopen($this->host, $this->port);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   126
    if ( !$this->sock )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   127
      throw new Exception('Could not make socket connection to host.');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   128
    
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   129
    stream_set_timeout($this->sock, 1);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   130
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   131
    // Send nick and username
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   132
    $this->put("NICK $nick\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   133
    $this->put("USER $username 0 * :$realname\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   134
    
38
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   135
    $need_deghost = false;
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   136
    
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   137
    // wait for a mode +i or end of the motd
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   138
    while ( true )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   139
    {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   140
      $msg = $this->get();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   141
      if ( empty($msg) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   142
        continue;
38
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   143
      if ( strstr($msg, "433") )
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   144
      {
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   145
        // nick already in use - try to ghost
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   146
        $this->change_nick("{$nick}|de-ghosting");
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   147
        $need_deghost = true;
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   148
      }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   149
      if ( ( strstr($msg, 'MODE') && strstr($msg, '+i') ) || strstr(strtolower($msg), 'end of /motd') )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   150
      {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   151
        break;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   152
      }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   153
      if ( preg_match('/^PING :(.+?)$/', $msg, $match) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   154
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   155
        $this->put("PONG :{$match[1]}\r\n");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   156
      }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   157
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   158
    
38
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   159
    if ( $need_deghost )
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   160
    {
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   161
      $this->privmsg('NickServ', "GHOST $nick $pass");
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   162
      while ( $msg = $this->get(10) )
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   163
      {
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   164
        if ( stristr($msg, 'nickserv') && stristr($msg, 'ghost') )
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   165
          break;
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   166
      }
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   167
      $this->change_nick($nick);
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   168
    }
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   169
    
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   170
    // identify to nickserv
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   171
    if ( $pass )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   172
      $this->privmsg('NickServ', "IDENTIFY $pass");
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   173
    
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   174
    $this->nick = $nick;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   175
    $this->user = $username;
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   176
    $this->quitted = false;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   177
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   178
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   179
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   180
   * Writes some data to the socket, abstracted for debugging purposes.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   181
   * @param string Message to send, this should include a CRLF.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   182
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   183
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   184
  public function put($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   185
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   186
    if ( !$this->sock )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   187
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   188
      if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   189
        echo ">>> WRITE FAILED: $message";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   190
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   191
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   192
    if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   193
      echo ">>> $message";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   194
    fwrite($this->sock, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   195
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   196
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   197
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   198
   * Reads from the socket...
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   199
   * @return string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   200
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   201
  
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   202
  public function get($timeout = 1)
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   203
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   204
    if ( !$this->sock )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   205
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   206
      if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   207
        echo "<<< READ FAILED\n";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   208
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   209
    }
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   210
    if ( ($c = stream_select($r = array($this->sock), $w = null, $e = null, $timeout)) !== false )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   211
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   212
      if ( $c > 0 )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   213
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   214
        $out = fgets($this->sock, 4096);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   215
        if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   216
          if ( !empty($out) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   217
            echo "<<< $out";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   218
        return $out;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   219
      }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   220
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   221
    return false;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   222
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   223
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   224
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   225
   * Sends a message to a nick or channel.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   226
   * @param string Nick or channel
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   227
   * @param string Message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   228
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   229
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   230
  public function privmsg($nick, $message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   231
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   232
    $message = str_replace("\r\n", "\n", $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   233
    $message = explode("\n", $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   234
    foreach ( $message as $line )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   235
    {
32
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   236
      $line = $this->filter_message($line);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   237
      $this->put("PRIVMSG $nick :$line\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   238
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   239
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   240
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   241
  /**
40
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   242
   * Sends a notice.
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   243
   * @param string Nick or channel
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   244
   * @param string Message
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   245
   */
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   246
  
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   247
  public function notice($nick, $message)
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   248
  {
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   249
    $message = str_replace("\r\n", "\n", $message);
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   250
    $message = explode("\n", $message);
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   251
    foreach ( $message as $line )
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   252
    {
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   253
      $line = $this->filter_message($line);
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   254
      $this->put("NOTICE $nick :$line\r\n");
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   255
    }
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   256
  }
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   257
  
1855846cbdab Added CTCP support and an associated module
Dan
parents: 38
diff changeset
   258
  /**
51
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   259
   * Returns WHOIS information about a nick.
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   260
   * @param string Nick
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   261
   * @return array
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   262
   */
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   263
  
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   264
  public function whois($nick)
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   265
  {
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   266
    static $cache = array();
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   267
    if ( isset($cache[$nick]) )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   268
    {
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   269
      if ( $cache[$nick]['time'] + 20 >= time() )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   270
      {
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   271
        return $cache[$nick];
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   272
      }
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   273
    }
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   274
    $this->put("WHOIS $nick\r\n");
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   275
    $lines = array();
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   276
    $return = array(
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   277
        'identified' => false,
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   278
        'time' => time()
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   279
      );
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   280
    while ( $line = $this->get(3) )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   281
    {
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   282
      $lines[] = $line;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   283
      list(, $code) = explode(' ', $line);
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   284
      $code = intval($code);
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   285
      switch($code)
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   286
      {
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   287
        case 401:
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   288
          return false;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   289
        case 311:
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   290
          list(, , , $return['nick'], $return['user'], $return['host']) = explode(' ', $line);
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   291
          $return['ircname'] = substr($line, strpos(substr($line, 1), ':') + 2);
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   292
          break;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   293
        case 319:
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   294
          $return['channels'] = explode(' ', substr($line, strpos(substr($line, 1), ':') + 2));
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   295
          break;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   296
        case 307:
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   297
        case 320:
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   298
          if ( strstr($line, 'identified') )
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   299
            $return['identified'] = true;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   300
          break;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   301
        case 318:
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   302
          $cache[$nick] = $return;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   303
          return $return;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   304
      }
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   305
    }
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   306
    $cache[$nick] = $return;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   307
    return $return;
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   308
  }
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   309
  
508400fc5282 Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents: 40
diff changeset
   310
  /**
33
c3179049f670 Spacing typo. Nothing more.
Dan
parents: 32
diff changeset
   311
   * 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
32
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   312
   * @param string Text to filter
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   313
   * @return string
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   314
   */
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   315
  
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   316
  function filter_message($text)
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   317
  {
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   318
    $text = preg_replace('#<b>(.*?)</b>#is', "\x02$1\x02", $text);
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   319
    if ( preg_match('#^/me #i', $text) )
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   320
    {
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   321
      $text = "\x01ACTION " . preg_replace('#^/me #i', '', $text) . "\x01";
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   322
    }
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   323
    $supportedcolors = array('white', 'black', 'navy', 'green', 'red', 'maroon', 'purple', 'orange', 'yellow', 'lime', 'teal', 'cyan', 'blue', 'pink', 'grey', 'silver');
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   324
    $colors = implode('|', $supportedcolors);
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   325
    $supportedcolors = array_flip($supportedcolors);
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   326
    preg_match_all("#<((?:$colors)(?::(?:$colors))?)>(.*?)</\\1>#is", $text, $matches);
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   327
    foreach ( $matches[0] as $i => $match )
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   328
    {
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   329
      preg_match("#<(?:($colors)(?::($colors))?)>#i", $match, $colordata);
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   330
      $fgcolor = $supportedcolors[$colordata[1]];
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   331
      $bgcolor = $colordata[1] ? $supportedcolors[$colordata[2]] : '';
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   332
      $fgcolor = ( $fgcolor < 10 ) ? "0$fgcolor" : "$fgcolor";
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   333
      if ( is_int($bgcolor) )
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   334
        $bgcolor = ( $bgcolor < 10 ) ? ",0$bgcolor" : ",$bgcolor";
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   335
      $text = str_replace($match, "\x03{$fgcolor}{$bgcolor}{$matches[2][$i]}\x03", $text);
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   336
    }
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   337
    
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   338
    return $text;
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   339
  }
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   340
  
236fd4f65752 Added support for bold, /me, and of course, colors.
Dan
parents: 31
diff changeset
   341
  /**
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   342
   * The main event loop.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   343
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   344
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   345
  public function event_loop()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   346
  {
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   347
    $timeout = 180;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   348
    $timeout_warn = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   349
    while ( true )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   350
    {
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   351
      $data = $this->get($timeout);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   352
      $data_trim = $data ? trim($data) : false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   353
      if ( empty($data_trim) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   354
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   355
        if ( $timeout_warn )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   356
        {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   357
          // timeout confirmed :-/
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   358
          if ( $this->timeout_error_handler )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   359
          {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   360
            $result = @call_user_func($this->timeout_error_handler, $this);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   361
            if ( $result == 'BREAK' )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   362
              break;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   363
          }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   364
          $timeout = 180;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   365
          $timeout_warn = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   366
          continue;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   367
        }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   368
        // timeout suspected
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   369
        if ( $this->timeout_warning_handler )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   370
        {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   371
          $result = @call_user_func($this->timeout_warning_handler, $this);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   372
          if ( $result == 'BREAK' )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   373
            break;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   374
        }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   375
        // ping the server
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   376
        $this->put("PING :{$this->nick}\r\n");
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   377
        // set timeout lower to make reconnecting work as fast as possible
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   378
        $timeout = 10;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   379
        $timeout_warn = true;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   380
        continue;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   381
      }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   382
      $match = self::parse_message($data_trim);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   383
      if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   384
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   385
        $this->put("PONG :{$pmatch[1]}\r\n");
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   386
        eval(eb_fetch_hook('event_ping'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   387
      }
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   388
      else if ( preg_match('/^:((?:[a-z0-9-]+\.)*[a-z0-9-]+) PONG \\1 :' . preg_quote($this->nick) .'/', $data_trim) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   389
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   390
        $timeout = 180;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   391
        $timeout_warn = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   392
      }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   393
      else if ( $match )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   394
      {
38
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   395
        if ( $match['action'] == 'KILL' )
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   396
        {
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   397
          // be ethical here and die
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   398
          return 'killed';
e6a4b7f91e91 Built de-ghosting code into libirc core
Dan
parents: 33
diff changeset
   399
        }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   400
        // Received PRIVMSG or other mainstream action
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   401
        if ( $match['action'] == 'JOIN' || $match['action'] == 'PART' )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   402
          $channel =& $match['message'];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   403
        else
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   404
          $channel =& $match['target'];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   405
          
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   406
        if ( !preg_match('/^[#!&\+]/', $channel) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   407
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   408
          // Private message from user
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   409
          $result = $this->handle_privmsg($data);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   410
          @stream_set_timeout($this->sock, 0xFFFFFFFE);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   411
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   412
        else if ( isset($this->channels[strtolower($channel)]) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   413
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   414
          // Message into channel
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   415
          $chan =& $this->channels[strtolower($channel)];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   416
          $func = $chan->get_handler();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   417
          $result = @call_user_func($func, $data, $chan);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   418
          @stream_set_timeout($this->sock, 0xFFFFFFFE);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   419
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   420
        if ( $result == 'BREAK' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   421
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   422
          break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   423
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   424
      }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   425
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   426
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   427
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   428
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   429
   * Processor for when a private message is received.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   430
   * @access private
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   431
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   432
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   433
  private function handle_privmsg($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   434
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   435
    $message = self::parse_message($message);
21
d86ea89358ec Added IP logger module.
Dan
parents: 8
diff changeset
   436
    $message['message'] = preg_replace('/^(.+?):/', '', $message['message']);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   437
    $ph = $this->privmsg_handler;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   438
    if ( @function_exists($ph) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   439
      return @call_user_func($ph, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   440
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   441
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   442
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   443
   * Changes the function called upon receipt of a private message.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   444
   * @param string Function to call, will be passed a parsed message.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   445
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   446
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   447
  function set_privmsg_handler($func)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   448
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   449
    if ( !function_exists($func) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   450
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   451
    $this->privmsg_handler = $func;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   452
    return true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   453
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   454
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   455
  /**
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   456
   * Changes the functions called when IRC connection timeouts occur.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   457
   * @param string Function to call when a warning (no traffic within 3 minutes) occurs. If false, nothing will be called.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   458
   * @param string Function to call if a ping timeout occurs. If false, nothing will be called.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   459
   */
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   460
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   461
  function set_timeout_handlers($warn_func, $error_func)
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   462
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   463
    $this->timeout_warning_handler = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   464
    $this->timeout_error_handler = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   465
    if ( function_exists($warn_func) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   466
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   467
      $this->timeout_warning_handler = $warn_func;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   468
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   469
    if ( function_exists($error_func) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   470
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   471
      $this->timeout_error_handler = $error_func;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   472
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   473
    return true;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   474
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   475
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   476
  /**
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   477
   * Parses a message line.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   478
   * @param string Message text
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   479
   * @return array Associative with keys: nick, user, host, action, target, message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   480
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   481
   
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   482
  public static function parse_message($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   483
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   484
    // Indices:          12       3       4        5        67                         8
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   485
    $mc = preg_match('/^:(([^ ]+)!([^ ]+)@([^ ]+)) ([A-Z]+) (([#!&\+]*[A-z0-9_-]+) )?:?(.*?)$/', $message, $match);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   486
    if ( !$mc )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   487
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   488
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   489
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   490
    // Indices: 0 1 2      3      4      5        6 7        8
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   491
    list(       , , $nick, $user, $host, $action, , $target, $message) = $match;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   492
    return array(
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   493
        'nick' => $nick,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   494
        'user' => $user,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   495
        'host' => $host,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   496
        'action' => $action,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   497
        'target' => $target,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   498
        'message' => trim($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   499
      );
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   500
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   501
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   502
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   503
   * Joins a channel, and returns a Request_IRC_Channel object.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   504
   * @param string Channel name (remember # prefix)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   505
   * @param string Event handler function, will be called with param 0 = socket output and param 1 = channel object
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   506
   * @return object
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   507
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   508
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   509
  function join($channel, $handler)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   510
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   511
    $chan = new Request_IRC_Channel(strtolower($channel), $handler, $this);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   512
    $this->channels[strtolower($channel)] = $chan;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   513
    return $chan;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   514
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   515
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   516
  /**
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   517
   * Changes the current nick.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   518
   * @param string New nick.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   519
   * @param string Password to authenticate to NickServ if needed.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   520
   */
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   521
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   522
  public function change_nick($nick, $pass = false)
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   523
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   524
    $this->put("NICK $nick\r\n");
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   525
    $this->nick = $nick;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   526
    if ( $pass )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   527
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   528
      while ( $data = $this->get(3) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   529
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   530
        if ( strstr($data, 'NickServ') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   531
        {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   532
          $this->privmsg('NickServ', "IDENTIFY $pass");
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   533
          break;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   534
        }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   535
      }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   536
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   537
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   538
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   539
  /**
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   540
   * Closes the connection and quits.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   541
   * @param string Optional part message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   542
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   543
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   544
  public function close($partmsg = false)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   545
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   546
    if ( $this->quitted )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   547
      return true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   548
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   549
    $this->quitted = true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   550
    // Part all channels
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   551
    if ( !$partmsg )
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   552
      $partmsg = 'Closing connection (no reason given)';
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   553
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   554
    foreach ( $this->channels as $channel )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   555
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   556
      $channel->part($partmsg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   557
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   558
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   559
    $this->put("QUIT\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   560
    
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   561
    while ( $msg = $this->get(1) )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   562
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   563
      // Do nothing.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   564
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   565
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   566
    fclose($this->sock);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   567
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   568
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   569
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   570
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   571
/**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   572
 * Wrapper for channels.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   573
 */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   574
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   575
class Request_IRC_Channel extends Request_IRC
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   576
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   577
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   578
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   579
   * The name of the channel
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   580
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   581
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   582
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   583
  private $channel_name = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   584
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   585
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   586
   * The event handler function.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   587
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   588
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   589
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   590
  private $handler = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   591
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   592
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   593
   * The parent connection.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   594
   * @var object
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   595
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   596
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   597
  public $parent = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   598
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   599
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   600
   * Whether the channel has been parted or not, used to kill the destructor.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   601
   * @var bool
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   602
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   603
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   604
  protected $parted = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   605
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   606
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   607
   * Constructor.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   608
   * @param string Channel name
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   609
   * @param string Handler function
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   610
   * @param object IRC connection (Request_IRC object)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   611
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   612
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   613
  function __construct($channel, $handler, $parent)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   614
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   615
    $this->parent = $parent;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   616
    $this->parent->put("JOIN $channel\r\n");
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   617
    // stream_set_timeout($this->parent->sock, 3);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   618
    // while ( $msg = $this->parent->get() )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   619
    // {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   620
    //   // Do nothing
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   621
    // }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   622
    $this->channel_name = $channel;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   623
    $this->handler = $handler;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   624
    eval(eb_fetch_hook('event_self_join'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   625
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   626
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   627
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   628
   * Returns the channel name
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   629
   * @return string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   630
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   631
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   632
  function get_channel_name()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   633
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   634
    return $this->channel_name;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   635
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   636
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   637
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   638
   * Returns the handler function
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   639
   * @return string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   640
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   641
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   642
  function get_handler()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   643
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   644
    return $this->handler;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   645
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   646
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   647
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   648
   * Sends a message.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   649
   * @param string message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   650
   * @param bool If true, will fire a message event when the message is sent.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   651
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   652
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   653
  function msg($msg, $fire_event = false)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   654
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   655
    $this->parent->privmsg($this->channel_name, $msg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   656
    if ( $fire_event )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   657
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   658
      $func = $this->get_handler();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   659
      // format: :nick!user@host PRIVMSG #channel :msg.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   660
      $lines = explode("\n", $msg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   661
      foreach ( $lines as $line )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   662
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   663
        $data = ":{$this->parent->nick}!{$this->parent->user}@localhost PRIVMSG {$this->channel_name} :$line";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   664
        $result = @call_user_func($func, $data, $this);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   665
        stream_set_timeout($this->parent->sock, 0xFFFFFFFE);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   666
      }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   667
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   668
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   669
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   670
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   671
   * Destructor, automatically parts the channel.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   672
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   673
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   674
  function __destruct()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   675
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   676
    if ( !$this->parted )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   677
      $this->part('IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   678
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   679
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   680
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   681
   * Parts the channel.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   682
   * @param string Optional message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   683
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   684
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   685
  function part($msg = '')
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   686
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   687
    $this->parent->put("PART {$this->channel_name} :$msg\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   688
    $this->parted = true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   689
    unset($this->parent->channels[$this->channel_name]);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   690
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   691
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   692
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   693
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   694
?>