<?php
/*
* IRCConnection.
* This is the main IRC connection class, and holds
* all IRC related functions.
*/
class IRCConnection {
/*
* Connect( Server , Port , Nickname ).
* This is the intial connection function, which connects
* to the given IRC server on the specified port. This needs
* to be called once before any further connection progress is
* made.
*
* This function should also be considered as the initializer
* as to some degree it sets things up for the rest of usage,
* i.e. Server, Port and Nickname.
*/
function Connect($Server, $Port, $tmpNickname) {
/* Define these vatiables globally, so they can be read/written anywhere. */
global $Connection, $ConnectionStatus, $Nickname;
/* Enable unlimited execution time, so php doesn't kill our connection. */
set_time_limit(0);
/* Create a new socket connection to the server on the given port. */
$Connection = fsockopen($Server, $Port);
/* Setup a variable to hold our connection status. */
$ConnectionStatus = 1;
$Nickname = $tmpNickname;
}
/*
* Recieve().
* This function recieves data from our server, once the connection has been mad.e
*/
function Recieve() {
global $Connection, $ConnectionStatus, $Nickname;
/* Assign our recieved data to a vatiable. */
$Message = fgets($Connection, 512);
/* Run a regexp to respond to PING / PONG. */
if (preg_match('/PING :/', $Message)) {
/* Replace PING with PONG. */
$PingPong = str_replace('PING', 'PONG', $Message);
/* Send back our modified PING packet. */
fputs($Connection, $PingPong."\n\r");
}
/* Check to see what our connection status is. */
if ($ConnectionStatus == 1) {
/* Send USER and NICK command to complete our connection. */
fputs($Connection, "USER " . $Nickname . " localhost localhost :Daves Bot\n\r");
$this->SetNickname($Nickname);
/* Update Connection status. */
$ConnectionStatus = 2;
}
/* Return the raw connection data, so the user can add their own matches. */
return $Message;
}
/*
* JoinChannel( Channel to join ).
* Joins the specified IRC channel. Simple, eh ?
*/
function JoinChannel($Channel) {
global $Connection, $ConnectionStatus;
/* Send a join packet to join a channel. - OPTIONALLY part of the connect process. */
fputs($Connection, "JOIN ".$Channel."\n\r");
/* Update connection status. */
$ConnectionStatus = 4;
}
/*
* SetNickname( New Nickname ).
* Set the nickname for the client, can be called after connection to set nickname!
*/
function SetNickname($Nickname) {
global $Connection, $ConnectionStatus;
/* Send nickname packet. */
fputs($Connection, "NICK " . $Nickname ."\n\r");
}
/*
* SendPrivMessage( Recipient , Message ).
* Send the desired message to the specified recipent.
*/
function SendPrivMessage($To, $Message) {
global $Connection, $ConnectionStatus;
fputs($Connection, "PRIVMSG " . $To . " :" . $Message ."\n\r");
}
/*
* SendChannelMessage( Recipient , Message ).
* Send the desired message to the specified recipent.
*/
function SendChannelMessage($Channel, $Massage) {
global $Connection, $ConnectionStatus;
fputs($Connection, "PRIVMSG " . $Channel . " " . $Massage . "\n\r");
}
}
?>