libirc.php
author Dan
Sat, 15 Mar 2008 20:45:32 -0400
changeset 2 c474904ed707
parent 0 d02690a8552c
child 8 0acb8d9a3194
permissions -rw-r--r--
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
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
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    58
   * 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
    59
   * @var bool
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    60
   * @access private
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    61
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    62
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    63
  protected $quitted = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    64
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    65
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    66
   * The nickname we're connected as. Not modified once connected.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    67
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    68
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    69
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    70
  public $nick = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    71
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    72
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    73
   * The username we're connected as. Not modified once connected.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    74
   * @var string
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
  public $user = '';
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
   * Constructor.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    81
   * @param string Hostname
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    82
   * @param int Port number, defaults to 6667
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    83
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    84
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    85
  public function __construct($host, $port = 6667)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    86
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    87
    // Check hostname
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    88
    if ( !preg_match('/^(([a-z0-9-]+\.)*?)([a-z0-9-]+)$/', $host) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    89
      die(__CLASS__ . ': Invalid hostname');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    90
    $this->host = $host;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    91
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    92
    // Check port
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    93
    if ( is_int($port) && $port >= 1 && $port <= 65535 )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    94
      $this->port = $port;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    95
    else
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
    96
      die(__CLASS__ . ': Invalid port');
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
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   100
   * Sets parameters and opens the connection.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   101
   * @param string Nick
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   102
   * @param string User
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   103
   * @param string Real name
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   104
   * @param string NickServ password
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   105
   * @param int Flags, defaults to 0.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   106
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   107
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   108
  public function connect($nick, $username, $realname, $pass, $flags = 0)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   109
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   110
    // Init connection
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   111
    $this->sock = fsockopen($this->host, $this->port);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   112
    if ( !$this->sock )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   113
      throw new Exception('Could not make socket connection to host.');
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   114
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   115
    stream_set_timeout($this->sock, 5);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   116
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   117
    // Wait for initial ident messages
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   118
    while ( $msg = $this->get() )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   119
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   120
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   121
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   122
    // Send nick and username
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   123
    $this->put("NICK $nick\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   124
    $this->put("USER $username 0 * :$realname\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   125
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   126
    // Wait for response and end of motd
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   127
    $motd = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   128
    while ( $msg = $this->get() )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   129
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   130
      // Match particles
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   131
      $msg = trim($msg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   132
      $mc = preg_match('/^:([A-z0-9\.-]+) ([0-9]+) [A-z0-9_-]+ :(.+)$/', $msg, $match);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   133
      if ( !$mc )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   134
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   135
        $mc = preg_match('/^:([A-z0-9_-]+)!([A-z0-9_-]+)@([A-z0-9_\.-]+) NOTICE [A-z0-9_-]+ :(.+)$/', $msg, $match);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   136
        if ( !$mc )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   137
          continue;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   138
        // Look for a response from NickServ
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   139
        if ( $match[1] == 'NickServ' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   140
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   141
          // Asking for auth?
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   142
          if ( strpos($match[4], 'IDENTIFY') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   143
          {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   144
            // Yes, send password
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   145
            $this->privmsg('NickServ', "IDENTIFY $pass");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   146
          }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   147
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   148
      }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   149
      list(, $host, $stat, $msg) = $match;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   150
      $motd .= "$msg";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   151
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   152
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   153
    $this->nick = $nick;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   154
    $this->user = $username;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   155
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   156
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   157
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   158
   * Writes some data to the socket, abstracted for debugging purposes.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   159
   * @param string Message to send, this should include a CRLF.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   160
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   161
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   162
  public function put($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   163
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   164
    if ( !$this->sock )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   165
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   166
      if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   167
        echo ">>> WRITE FAILED: $message";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   168
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   169
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   170
    if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   171
      echo ">>> $message";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   172
    fwrite($this->sock, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   173
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   174
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   175
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   176
   * Reads from the socket...
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   177
   * @return string
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
  public function get()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   181
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   182
    if ( !$this->sock )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   183
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   184
      if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   185
        echo "<<< READ FAILED\n";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   186
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   187
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   188
    $out = fgets($this->sock, 4096);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   189
    if ( defined('LIBIRC_DEBUG') )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   190
      if ( !empty($out) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   191
        echo "<<< $out";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   192
    return $out;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   193
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   194
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   195
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   196
   * Sends a message to a nick or channel.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   197
   * @param string Nick or channel
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   198
   * @param string Message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   199
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   200
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   201
  public function privmsg($nick, $message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   202
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   203
    $message = str_replace("\r\n", "\n", $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   204
    $message = explode("\n", $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   205
    foreach ( $message as $line )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   206
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   207
      $this->put("PRIVMSG $nick :$line\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   208
    }
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
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   212
   * The main event loop.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   213
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   214
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   215
  public function event_loop()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   216
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   217
    stream_set_timeout($this->sock, 0xFFFFFFFE);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   218
    while ( $data = $this->get() )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   219
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   220
      $data_trim = trim($data);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   221
      $match = self::parse_message($data_trim);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   222
      if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   223
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   224
        $this->put("PONG :{$pmatch[1]}\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   225
      }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   226
      else if ( $match )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   227
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   228
        // Received PRIVMSG or other mainstream action
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   229
        if ( $match['action'] == 'JOIN' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   230
          $channel =& $match['message'];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   231
        else
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   232
          $channel =& $match['target'];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   233
          
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   234
        if ( !preg_match('/^[#!&\+]/', $channel) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   235
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   236
          // Private message from user
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   237
          $result = $this->handle_privmsg($data);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   238
          stream_set_timeout($this->sock, 0xFFFFFFFE);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   239
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   240
        else if ( isset($this->channels[strtolower($channel)]) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   241
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   242
          // Message into channel
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   243
          $chan =& $this->channels[strtolower($channel)];
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   244
          $func = $chan->get_handler();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   245
          $result = @call_user_func($func, $data, $chan);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   246
          stream_set_timeout($this->sock, 0xFFFFFFFE);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   247
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   248
        if ( $result == 'BREAK' )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   249
        {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   250
          break;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   251
        }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   252
      }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   253
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   254
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   255
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   256
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   257
   * Processor for when a private message is received.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   258
   * @access private
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   259
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   260
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   261
  private function handle_privmsg($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   262
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   263
    $message = self::parse_message($message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   264
    $ph = $this->privmsg_handler;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   265
    if ( @function_exists($ph) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   266
      return @call_user_func($ph, $message);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   267
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   268
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   269
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   270
   * Changes the function called upon receipt of a private message.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   271
   * @param string Function to call, will be passed a parsed message.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   272
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   273
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   274
  function set_privmsg_handler($func)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   275
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   276
    if ( !function_exists($func) )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   277
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   278
    $this->privmsg_handler = $func;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   279
    return true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   280
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   281
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   282
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   283
   * Parses a message line.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   284
   * @param string Message text
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   285
   * @return array Associative with keys: nick, user, host, action, target, message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   286
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   287
   
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   288
  public static function parse_message($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   289
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   290
    // Indices:          12       3       4        5        67                         8
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   291
    $mc = preg_match('/^:(([^ ]+)!([^ ]+)@([^ ]+)) ([A-Z]+) (([#!&\+]*[A-z0-9_-]+) )?:?(.*?)$/', $message, $match);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   292
    if ( !$mc )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   293
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   294
      return false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   295
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   296
    // Indices: 0 1 2      3      4      5        6 7        8
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   297
    list(       , , $nick, $user, $host, $action, , $target, $message) = $match;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   298
    return array(
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   299
        'nick' => $nick,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   300
        'user' => $user,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   301
        'host' => $host,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   302
        'action' => $action,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   303
        'target' => $target,
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   304
        'message' => trim($message)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   305
      );
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
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   309
   * Joins a channel, and returns a Request_IRC_Channel object.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   310
   * @param string Channel name (remember # prefix)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   311
   * @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
   312
   * @return object
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   313
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   314
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   315
  function join($channel, $handler)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   316
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   317
    $chan = new Request_IRC_Channel(strtolower($channel), $handler, $this);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   318
    $this->channels[strtolower($channel)] = $chan;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   319
    return $chan;
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
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   323
   * Closes the connection and quits.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   324
   * @param string Optional part message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   325
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   326
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   327
  public function close($partmsg = false)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   328
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   329
    if ( $this->quitted )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   330
      return true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   331
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   332
    $this->quitted = true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   333
    // Part all channels
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   334
    if ( !$partmsg )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   335
      $partmsg = 'IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   336
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   337
    foreach ( $this->channels as $channel )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   338
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   339
      $channel->part($partmsg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   340
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   341
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   342
    $this->put("QUIT\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   343
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   344
    while ( $msg = $this->get() )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   345
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   346
      // Do nothing.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   347
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   348
    
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   349
    fclose($this->sock);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   350
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   351
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   352
}
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   353
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   354
/**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   355
 * Wrapper for channels.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   356
 */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   357
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   358
class Request_IRC_Channel extends Request_IRC
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   359
{
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   360
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   361
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   362
   * The name of the channel
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   363
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   364
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   365
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   366
  private $channel_name = '';
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   367
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   368
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   369
   * The event handler function.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   370
   * @var string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   371
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   372
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   373
  private $handler = '';
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
   * The parent connection.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   377
   * @var object
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   378
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   379
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   380
  public $parent = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   381
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   382
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   383
   * Whether the channel has been parted or not, used to kill the destructor.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   384
   * @var bool
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   385
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   386
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   387
  protected $parted = false;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   388
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   389
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   390
   * Constructor.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   391
   * @param string Channel name
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   392
   * @param string Handler function
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   393
   * @param object IRC connection (Request_IRC object)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   394
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   395
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   396
  function __construct($channel, $handler, $parent)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   397
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   398
    $this->parent = $parent;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   399
    $this->parent->put("JOIN $channel\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   400
    stream_set_timeout($this->parent->sock, 3);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   401
    while ( $msg = $this->parent->get() )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   402
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   403
      // Do nothing
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   404
    }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   405
    $this->channel_name = $channel;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   406
    $this->handler = $handler;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   407
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   408
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   409
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   410
   * Returns the channel name
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   411
   * @return string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   412
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   413
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   414
  function get_channel_name()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   415
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   416
    return $this->channel_name;
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
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   420
   * Returns the handler function
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   421
   * @return string
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   422
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   423
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   424
  function get_handler()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   425
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   426
    return $this->handler;
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
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   430
   * Sends a message.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   431
   * @param string message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   432
   * @param bool If true, will fire a message event when the message is sent.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   433
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   434
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   435
  function msg($msg, $fire_event = false)
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   436
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   437
    $this->parent->privmsg($this->channel_name, $msg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   438
    if ( $fire_event )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   439
    {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   440
      $func = $this->get_handler();
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   441
      // format: :nick!user@host PRIVMSG #channel :msg.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   442
      $lines = explode("\n", $msg);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   443
      foreach ( $lines as $line )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   444
      {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   445
        $data = ":{$this->parent->nick}!{$this->parent->user}@localhost PRIVMSG {$this->channel_name} :$line";
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   446
        $result = @call_user_func($func, $data, $this);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   447
        stream_set_timeout($this->parent->sock, 0xFFFFFFFE);
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
  }
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
   * Destructor, automatically parts the channel.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   454
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   455
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   456
  function __destruct()
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   457
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   458
    if ( !$this->parted )
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   459
      $this->part('IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   460
  }
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   461
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   462
  /**
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   463
   * Parts the channel.
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   464
   * @param string Optional message
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   465
   */
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   466
  
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   467
  function part($msg = '')
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   468
  {
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   469
    $this->parent->put("PART {$this->channel_name} :$msg\r\n");
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   470
    $this->parted = true;
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   471
    unset($this->parent->channels[$this->channel_name]);
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   472
  }
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
d02690a8552c Initial population. EnanoBot v0.1.
Dan
parents:
diff changeset
   476
?>