libirc.php
author Dan
Sun, 07 Dec 2008 08:45:54 -0500
changeset 31 d75124700259
parent 30 2cfcd2801e5a
child 32 236fd4f65752
permissions -rw-r--r--
Timeout recovery should avoid getting the bot throttled now :)
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
    
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   135
    // wait for a mode +i or end of the motd
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   136
    while ( true )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   137
    {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   138
      $msg = $this->get();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   139
      if ( empty($msg) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   140
        continue;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   141
      if ( ( strstr($msg, 'MODE') && strstr($msg, '+i') ) || strstr(strtolower($msg), 'end of /motd') )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   142
      {
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   143
        break;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   144
      }
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   145
      if ( preg_match('/^PING :(.+?)$/', $msg, $match) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   146
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   147
        $this->put("PONG :{$match[1]}\r\n");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   148
      }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   149
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   150
    
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   151
    // identify to nickserv
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   152
    if ( $pass )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   153
      $this->privmsg('NickServ', "IDENTIFY $pass");
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   154
    
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   155
    $this->nick = $nick;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   156
    $this->user = $username;
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   157
    $this->quitted = false;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   158
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   159
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   160
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   161
   * Writes some data to the socket, abstracted for debugging purposes.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   162
   * @param string Message to send, this should include a CRLF.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   163
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   164
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   165
  public function put($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   166
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   167
    if ( !$this->sock )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   168
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   169
      if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   170
        echo ">>> WRITE FAILED: $message";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   171
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   172
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   173
    if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   174
      echo ">>> $message";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   175
    fwrite($this->sock, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   176
  }
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
   * Reads from the socket...
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   180
   * @return string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   181
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   182
  
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   183
  public function get($timeout = 1)
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   184
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   185
    if ( !$this->sock )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   186
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   187
      if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   188
        echo "<<< READ FAILED\n";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   189
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   190
    }
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   191
    if ( ($c = stream_select($r = array($this->sock), $w = null, $e = null, $timeout)) !== false )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   192
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   193
      if ( $c > 0 )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   194
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   195
        $out = fgets($this->sock, 4096);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   196
        if ( defined('LIBIRC_DEBUG') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   197
          if ( !empty($out) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   198
            echo "<<< $out";
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   199
        return $out;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   200
      }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   201
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   202
    return false;
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   203
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   204
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   205
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   206
   * Sends a message to a nick or channel.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   207
   * @param string Nick or channel
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   208
   * @param string Message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   209
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   210
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   211
  public function privmsg($nick, $message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   212
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   213
    $message = str_replace("\r\n", "\n", $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   214
    $message = explode("\n", $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   215
    foreach ( $message as $line )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   216
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   217
      $this->put("PRIVMSG $nick :$line\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   218
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   219
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   220
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   221
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   222
   * The main event loop.
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
  public function event_loop()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   226
  {
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   227
    $timeout = 180;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   228
    $timeout_warn = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   229
    while ( true )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   230
    {
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   231
      $data = $this->get($timeout);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   232
      $data_trim = $data ? trim($data) : false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   233
      if ( empty($data_trim) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   234
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   235
        if ( $timeout_warn )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   236
        {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   237
          // timeout confirmed :-/
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   238
          if ( $this->timeout_error_handler )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   239
          {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   240
            $result = @call_user_func($this->timeout_error_handler, $this);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   241
            if ( $result == 'BREAK' )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   242
              break;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   243
          }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   244
          $timeout = 180;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   245
          $timeout_warn = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   246
          continue;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   247
        }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   248
        // timeout suspected
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   249
        if ( $this->timeout_warning_handler )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   250
        {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   251
          $result = @call_user_func($this->timeout_warning_handler, $this);
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   252
          if ( $result == 'BREAK' )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   253
            break;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   254
        }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   255
        // ping the server
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   256
        $this->put("PING :{$this->nick}\r\n");
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   257
        // set timeout lower to make reconnecting work as fast as possible
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   258
        $timeout = 10;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   259
        $timeout_warn = true;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   260
        continue;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   261
      }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   262
      $match = self::parse_message($data_trim);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   263
      if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   264
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   265
        $this->put("PONG :{$pmatch[1]}\r\n");
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   266
        eval(eb_fetch_hook('event_ping'));
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   267
      }
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   268
      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
   269
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   270
        $timeout = 180;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   271
        $timeout_warn = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   272
      }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   273
      else if ( $match )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   274
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   275
        // Received PRIVMSG or other mainstream action
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   276
        if ( $match['action'] == 'JOIN' || $match['action'] == 'PART' )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   277
          $channel =& $match['message'];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   278
        else
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   279
          $channel =& $match['target'];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   280
          
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   281
        if ( !preg_match('/^[#!&\+]/', $channel) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   282
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   283
          // Private message from user
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   284
          $result = $this->handle_privmsg($data);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   285
          @stream_set_timeout($this->sock, 0xFFFFFFFE);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   286
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   287
        else if ( isset($this->channels[strtolower($channel)]) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   288
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   289
          // Message into channel
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   290
          $chan =& $this->channels[strtolower($channel)];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   291
          $func = $chan->get_handler();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   292
          $result = @call_user_func($func, $data, $chan);
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   293
          @stream_set_timeout($this->sock, 0xFFFFFFFE);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   294
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   295
        if ( $result == 'BREAK' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   296
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   297
          break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   298
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   299
      }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   300
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   301
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   302
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   303
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   304
   * Processor for when a private message is received.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   305
   * @access private
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   306
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   307
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   308
  private function handle_privmsg($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   309
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   310
    $message = self::parse_message($message);
21
d86ea89358ec Added IP logger module.
Dan
parents: 8
diff changeset
   311
    $message['message'] = preg_replace('/^(.+?):/', '', $message['message']);
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   312
    $ph = $this->privmsg_handler;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   313
    if ( @function_exists($ph) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   314
      return @call_user_func($ph, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   315
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   316
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   317
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   318
   * Changes the function called upon receipt of a private message.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   319
   * @param string Function to call, will be passed a parsed message.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   320
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   321
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   322
  function set_privmsg_handler($func)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   323
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   324
    if ( !function_exists($func) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   325
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   326
    $this->privmsg_handler = $func;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   327
    return true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   328
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   329
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   330
  /**
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   331
   * Changes the functions called when IRC connection timeouts occur.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   332
   * @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
   333
   * @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
   334
   */
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   335
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   336
  function set_timeout_handlers($warn_func, $error_func)
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   337
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   338
    $this->timeout_warning_handler = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   339
    $this->timeout_error_handler = false;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   340
    if ( function_exists($warn_func) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   341
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   342
      $this->timeout_warning_handler = $warn_func;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   343
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   344
    if ( function_exists($error_func) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   345
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   346
      $this->timeout_error_handler = $error_func;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   347
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   348
    return true;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   349
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   350
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   351
  /**
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   352
   * Parses a message line.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   353
   * @param string Message text
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   354
   * @return array Associative with keys: nick, user, host, action, target, message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   355
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   356
   
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   357
  public static function parse_message($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   358
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   359
    // Indices:          12       3       4        5        67                         8
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   360
    $mc = preg_match('/^:(([^ ]+)!([^ ]+)@([^ ]+)) ([A-Z]+) (([#!&\+]*[A-z0-9_-]+) )?:?(.*?)$/', $message, $match);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   361
    if ( !$mc )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   362
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   363
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   364
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   365
    // Indices: 0 1 2      3      4      5        6 7        8
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   366
    list(       , , $nick, $user, $host, $action, , $target, $message) = $match;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   367
    return array(
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   368
        'nick' => $nick,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   369
        'user' => $user,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   370
        'host' => $host,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   371
        'action' => $action,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   372
        'target' => $target,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   373
        'message' => trim($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   374
      );
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   375
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   376
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   377
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   378
   * Joins a channel, and returns a Request_IRC_Channel object.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   379
   * @param string Channel name (remember # prefix)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   380
   * @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
   381
   * @return object
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   382
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   383
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   384
  function join($channel, $handler)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   385
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   386
    $chan = new Request_IRC_Channel(strtolower($channel), $handler, $this);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   387
    $this->channels[strtolower($channel)] = $chan;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   388
    return $chan;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   389
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   390
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   391
  /**
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   392
   * Changes the current nick.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   393
   * @param string New nick.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   394
   * @param string Password to authenticate to NickServ if needed.
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   395
   */
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   396
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   397
  public function change_nick($nick, $pass = false)
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   398
  {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   399
    $this->put("NICK $nick\r\n");
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   400
    $this->nick = $nick;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   401
    if ( $pass )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   402
    {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   403
      while ( $data = $this->get(3) )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   404
      {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   405
        if ( strstr($data, 'NickServ') )
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   406
        {
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   407
          $this->privmsg('NickServ', "IDENTIFY $pass");
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   408
          break;
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   409
        }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   410
      }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   411
    }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   412
  }
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   413
  
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   414
  /**
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   415
   * Closes the connection and quits.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   416
   * @param string Optional part message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   417
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   418
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   419
  public function close($partmsg = false)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   420
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   421
    if ( $this->quitted )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   422
      return true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   423
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   424
    $this->quitted = true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   425
    // Part all channels
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   426
    if ( !$partmsg )
31
d75124700259 Timeout recovery should avoid getting the bot throttled now :)
Dan
parents: 30
diff changeset
   427
      $partmsg = 'Closing connection (no reason given)';
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   428
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   429
    foreach ( $this->channels as $channel )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   430
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   431
      $channel->part($partmsg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   432
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   433
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   434
    $this->put("QUIT\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   435
    
30
2cfcd2801e5a Added auto reconnect support.
Dan
parents: 21
diff changeset
   436
    while ( $msg = $this->get(1) )
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   437
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   438
      // Do nothing.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   439
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   440
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   441
    fclose($this->sock);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   442
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   443
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   444
}
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
 * Wrapper for channels.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   448
 */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   449
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   450
class Request_IRC_Channel extends Request_IRC
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   451
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   452
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   453
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   454
   * The name of the channel
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   455
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   456
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   457
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   458
  private $channel_name = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   459
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   460
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   461
   * The event handler function.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   462
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   463
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   464
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   465
  private $handler = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   466
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   467
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   468
   * The parent connection.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   469
   * @var object
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   470
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   471
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   472
  public $parent = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   473
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   474
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   475
   * Whether the channel has been parted or not, used to kill the destructor.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   476
   * @var bool
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   477
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   478
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   479
  protected $parted = false;
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
   * Constructor.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   483
   * @param string Channel name
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   484
   * @param string Handler function
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   485
   * @param object IRC connection (Request_IRC object)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   486
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   487
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   488
  function __construct($channel, $handler, $parent)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   489
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   490
    $this->parent = $parent;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   491
    $this->parent->put("JOIN $channel\r\n");
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   492
    // stream_set_timeout($this->parent->sock, 3);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   493
    // while ( $msg = $this->parent->get() )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   494
    // {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   495
    //   // Do nothing
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   496
    // }
0
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   497
    $this->channel_name = $channel;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   498
    $this->handler = $handler;
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents: 0
diff changeset
   499
    eval(eb_fetch_hook('event_self_join'));
0
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
   * Returns the channel name
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   504
   * @return string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   505
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   506
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   507
  function get_channel_name()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   508
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   509
    return $this->channel_name;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   510
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   511
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   512
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   513
   * Returns the handler function
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   514
   * @return string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   515
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   516
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   517
  function get_handler()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   518
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   519
    return $this->handler;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   520
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   521
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   522
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   523
   * Sends a message.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   524
   * @param string message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   525
   * @param bool If true, will fire a message event when the message is sent.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   526
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   527
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   528
  function msg($msg, $fire_event = false)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   529
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   530
    $this->parent->privmsg($this->channel_name, $msg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   531
    if ( $fire_event )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   532
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   533
      $func = $this->get_handler();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   534
      // format: :nick!user@host PRIVMSG #channel :msg.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   535
      $lines = explode("\n", $msg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   536
      foreach ( $lines as $line )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   537
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   538
        $data = ":{$this->parent->nick}!{$this->parent->user}@localhost PRIVMSG {$this->channel_name} :$line";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   539
        $result = @call_user_func($func, $data, $this);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   540
        stream_set_timeout($this->parent->sock, 0xFFFFFFFE);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   541
      }
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
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   545
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   546
   * Destructor, automatically parts the channel.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   547
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   548
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   549
  function __destruct()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   550
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   551
    if ( !$this->parted )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   552
      $this->part('IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   553
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   554
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   555
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   556
   * Parts the channel.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   557
   * @param string Optional message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   558
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   559
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   560
  function part($msg = '')
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   561
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   562
    $this->parent->put("PART {$this->channel_name} :$msg\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   563
    $this->parted = true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   564
    unset($this->parent->channels[$this->channel_name]);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   565
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   566
  
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
?>