PHP Classes

File: example.phpircclient.php

Recommend this page to a friend!
  Classes of JImmy Bo   PHP IRC Client   example.phpircclient.php   Download  
File: example.phpircclient.php
Role: Example script
Content type: text/plain
Description: Example for PHP IRC Client Class
Class: PHP IRC Client
Exchange user chat messages using a IRC server
Author: By
Last change: simplified
Date: 1 year ago
Size: 6,556 bytes
 

Contents

Class file image Download
<?php

// example containing a trivia plugin and a command line non-blocking client that supports input/output

   
require_once('class.phpircclient.php');

   
// create a plugin
   
class MyPlugin {
        private
$pClient; // parent client

        // send messages //
       
public function send($message) {
           
$this->pClient->send($message);
        }

       
// send messages with delay //
       
public function send_delayed($message, $delay) {
           
$this->pClient->send_delayed($message, $delay);
        }
       
       
// construct // set parent
       
public function __construct($client) {
           
$this->pClient = $client;
        }

       
// every plugin must have this function // it processes incoming lines
       
public function process($line) {
            echo
"Plugin: $line\n";
        }
    }
// end plugin example






   
class TriviaPlugin {
       
       
// someone starts a private message with our chatclient and types: !trivia
        // the chatclient will then start a trivia game with that person
        // very simple... but you can make it more complex if you want

       
private $pClient; // parent client

       
private $questions = array(
           
"What is the capital of France?", // Question 1
           
"What is the highest mountain in the world?", // Question 2
            // Add more questions here...
       
);
        private
$answers = array(
           
"Paris", // Answer to Question 1
           
"Mount Everest", // Answer to Question 2
            // Add more answers here...
       
);
        private
$current_question = 0;
        private
$waiting_for_answer = false;

       
       
// construct // set parent
       
public function __construct($client) {
           
$this->pClient = $client;
        }

        public function
get_nick()
        {
            return
$this->pClient->nick;
        }

       
// send messages //
       
public function send($message) {
           
$this->pClient->send($message); // sends a raw irc message
       
}
       
// send messages with delay //
       
public function send_delayed($message, $delay) {
           
$this->pClient->send_delayed($message, $delay); // send a raw message with delay
       
}

        public function
msg_user($user, $message) {
           
$str = $this->pClient->str_send_message($user, $message); // make the string to send to a user
           
$this->pClient->send($str); // send the strings
       
}

       
// every plugin must have this function // it processes incoming lines
       
public function process($line) {
            echo
"Plugin: $line\n";

           
// Check if the incoming message is a private message
           
if (preg_match('/^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)/', $line, $matches)) {
               
$sender = $matches[1];
               
$channel = $matches[4];
               
$message = $matches[5];

               
print_r($matches);
                print(
$sender."===");
                print(
$this->get_nick());
                echo
"===\r\n";
           
                if (
$sender == $this->get_nick()) {
                    return;
                }

               
// Check if the message is a command to start the trivia game
               
if (strtolower($message) == "!trivia") {
                   
// Check if the game is already in progress
                   
if ($this->waiting_for_answer) {
                       
$this->msg_user($sender, "A game of trivia is already in progress!");
                    } else {
                       
$this->msg_user($sender, "Starting a new game of trivia!");
                       
$this->current_question = 0;
                       
$this->waiting_for_answer = true;

                       
# send user the question
                       
$this->msg_user($sender, "Question 1- " . $this->questions[$this->current_question]);
                        return;
                    }
                }

               
// Check if the plugin is currently waiting for an answer to a trivia question
               
if ($this->waiting_for_answer) {
                   
// Check if the message is the correct answer to the current question
                   
if (strtolower($message) == strtolower($this->answers[$this->current_question])) {
                       
$this->msg_user($sender, "Correct!");
                       
$this->current_question++;

                       
// now if the current question is the last question, reset the game
                       
if($this->current_question == count($this->questions)) {
                           
$this->current_question = 0;
                           
$this->waiting_for_answer = false;
                           
$this->msg_user($sender, "Game over!");
                            return;
                        }

                       
// Check if there are more questions
                       
if ($this->current_question < count($this->questions)) {
                           
$this->msg_user($sender, "Question " . ($this->current_question + 1) . "- " . $this->questions[$this->current_question]);
                        } else {
                           
// $this->send("Game over!");
                           
$this->waiting_for_answer = false;
                        }
                    } else {
                       
$this->msg_user($sender, "Incorrect! Try again.");
                       
// repeat question
                       
$this->msg_user($sender, "Question " . ($this->current_question + 1) . "- " . $this->questions[$this->current_question]);
                    }
                }
// end waiting for answer
           
}
        }
    }
// end trivia bot example


   
$nick = 'yournickhere';
   
$user = 'yourusernamehere';
   
$realname = 'a name';

   
$channel = "#ircclientexample";

   
$client = new IRCClient('irc.defineya.com', 6667, $channel); #doesn't connect to channel off the hop.

    // attach plugins //
   
$client->plugins[] = new MyPlugin($client);
   
$client->plugins[] = new TriviaPlugin($client);

   
$client->connect();
   
// now send login and join channel commands
   
$client->send_delayed($client->str_login($nick, $user, $realname), 2);
   
$client->send_delayed($client->str_join_channel($channel), 3);

   
$client->startLoop();
   
$client->disconnect();

?>