PHP Classes

PHP IRC Client: Exchange user chat messages using a IRC server

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-05-07 (9 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 46 All time: 10,689 This week: 68Up
Version License PHP version Categories
phpircclient 1.0.0BSD License7Networking, Chat, PHP 7
Description 

Author

This package implements a chat system with an IRC server to exchange messages.

It can connect to an IRC server and can take chat messages sent by one, queue the messages and deliver messages to be sent from one user to another chat user.

The package can be extended by plugin classes that allow other developers to customize the interactions with the IRC server and automate the processing of different types of events notified by the IRC server, like users joining and leaving channels, changing nicknames, sending messages and notices, setting topics and modes, and more.

The package can also handle file transfers using the DCC protocol.

Picture of JImmy Bo
  Performance   Level  
Name: JImmy Bo is available for providing paid consulting. Contact JImmy Bo .
Classes: 14 packages by
Country: United States United States
Age: ???
All time rank: 1214173 in United States United States
Week rank: 21 Up4 in United States United States Up
Innovation award
Innovation award
Nominee: 8x

Winner: 1x

Example

<?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();

?>


  Files folder image Files  
File Role Description
Plain text file class.phpircclient.php Class PHP IRC Client Class
Accessible without login Plain text file example.phpircclient.php Example Example for PHP IRC Client Class

 Version Control Unique User Downloads Download Rankings  
 0%
Total:46
This week:0
All time:10,689
This week:68Up