Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 67d3a3e248be075e5b7141579a9c51a5496754f2 @LeonBlade committed Mar 19, 2011
Showing with 258 additions and 0 deletions.
  1. +8 −0 README
  2. +7 −0 config.php
  3. +116 −0 irc.php
  4. +127 −0 irc_class.php
8 README
@@ -0,0 +1,8 @@
+IRC Class
+
+A simple PHP IRC class that is in the workings.
+Can be used for many things, including bots.
+Do what you want, clone and push if you want changes to be made to this repo or just
+work with it on your own.
+
+No license, just do what you want with it.
@@ -0,0 +1,7 @@
+<?php
+
+// This file contains all the constants
+define(IRC_HOST, "irc.freenode.org");
+define(IRC_PORT, "6667");
+
+?>
116 irc.php
@@ -0,0 +1,116 @@
+<?php
+
+
+// Set the timeout limit so the page wont timeout
+set_time_limit(0);
+
+$channel = "";
+
+// IRC Class
+include("irc_class.php");
+// Config
+include("config.php");
+
+ // Create an instance of the IRC class
+ $irc = new ConnectIrc(IRC_HOST, IRC_PORT);
+
+ // Echo out the server and port
+ echo "IRC Server: {$irc->server}<br />Port: {$irc->port}<br />";
+ echo "<pre>";
+
+ // Open socket
+ if ( $irc->openSocket() ) {
+
+ // Set password (leaving it blank defines no password)
+ $irc->setPassword();
+ // Set nick/user
+ //$username = $_GET['nick'];
+ if (isset($_GET['nick']))
+ $username = $_GET['nick'];
+ else
+ $username = "LeonBot";
+
+ $irc->setNick($username);
+ $irc->setUser($username);
+
+ // While you are connected to the server
+ while ( $irc->connected() ) {
+
+ // Print out the read buffer
+ $buffer = $irc->showReadBuffer();
+ //echo $buffer."\n\r";
+
+ // Here is where you test for certain conditions
+ $irc->returnLastSaid($message);
+ $params = trim($message[PARAMS]);
+ switch ($message[COMMAND])
+ {
+ // Shutting down
+ case "!gtfo":
+ echo "Shutting down\n\r";
+ $irc->closeConnection(); exit;
+ break;
+
+ // Saying hello
+ case "!hello":
+ echo "Saying hello to {$message[WHERE]}\n\r";
+ $irc->say("Hey, {$message[SENDER]}!", $message[WHERE]);
+ break;
+
+ // Handles joining rooms
+ case "!join":
+ echo "Joining {$params}\n\r";
+ $channel = $params;
+ $irc->joinChannel($channel);
+ break;
+
+ // handles parting rooms
+ case "!part":
+ echo "Leaving {$params}\n\r";
+ $channel = $params;
+ $irc->partChannel($channel);
+ break;
+
+ // changing nickname
+ case "!nick":
+ echo "Changing nick to {$params}\n\r";
+ $irc->setNick($params);
+ break;
+
+ // grabbing someone's twitter status
+ case "!twitter":
+ echo "Grabbing status for {$params}\n\r";
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/{$params}.json");
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ $json = curl_exec($ch);
+ curl_close($ch);
+ $jsonObject = json_decode($json);
+ $status = $jsonObject[0]->text;
+ echo "Got latest Tweet from {$params}\n\t-> {$status}";
+ $irc->say("{$status}", $message[WHERE]);
+ break;
+ // for anything else ...
+ default:
+ if (strtolower($message[COMMAND]) == strtolower($irc->nick) ||
+ strstr(strtolower($params), strtolower($irc->nick)))
+ $irc->say("What the fuck do you want, {$message[SENDER]}?", $message[WHERE]);
+ break;
+ }
+
+ // Handle the ping pong
+ $irc->handlePingPong();
+
+ // Flush the buffer
+ $irc->flushIrc();
+ }
+
+ // Close the connection
+ if ( $irc->closeConnection() ) {
+ echo "<br />Connection closed... ";
+ } else {
+ echo "<br />Connection had a problem closing... wait wtf?";
+ }
+ }
+
+?>
@@ -0,0 +1,127 @@
+<?php
+
+define(FULL_STRING, 0);
+define(SENDER, 1);
+define(CLIENT, 2);
+define(HOST_INFO, 3);
+define(WHERE, 4);
+define(COMMAND, 5);
+define(PARAMS, 6);
+
+// ConnectIRC Class
+class ConnectIrc {
+
+ // Variables
+ public $server;
+ public $port;
+ public $channel;
+ public $password = "NOPASS";
+ public $nick;
+ public $socket;
+ public $errno;
+ public $errstr;
+ public $timeout = 2;
+ public $rbuffer;
+
+ // Constructor
+ function __construct($server, $port) {
+ $this->server = $server;
+ $this->port = $port;
+ }
+
+ // Open socket
+ function openSocket() {
+ $this->socket = fsockopen($this->server,
+ $this->port,
+ $this->errno,
+ $this->errstr,
+ $this->timeout);
+ return $this->socket;
+ }
+
+ // Set channel
+ function setChannel($channel) {
+ $this->channel = $channel;
+ }
+
+ // Set nick
+ function setNick($nick) {
+ $this->nick = $nick;
+ $this->sendCommand("NICK {$this->nick}\n\r");
+ }
+
+ function setUser($nick) {
+ $this->sendCommand("USER {$this->nick} {$this->nick} {$this->nick} {$this->nick} :{$this->nick} \n\r");
+ }
+
+ // Set password
+ function setPassword($password=false) {
+ if ($password == true) {
+ $this->password = $password;
+ }
+ $this->sendCommand("PASS {$this->password}\n\r");
+ }
+
+ // Talk in the chat
+ function say($message, $who)
+ {
+ $this->sendCommand("PRIVMSG {$who} :{$message}\n\r");
+ }
+
+ // Return connected status
+ function connected() {
+ return !feof($this->socket);
+ }
+
+ // Send command
+ function sendCommand($command) {
+ fputs($this->socket, $command, strlen($command));
+ }
+
+ // Join channel
+ function joinChannel($channel) {
+ $this->sendCommand("JOIN {$channel}\n\r");
+ }
+
+ // Part channel
+ function partChannel($channel) {
+ $this->sendCommand("PART {$channel}\n\r");
+ }
+
+ // Handle the ping pong
+ function handlePingPong() {
+ if (substr($this->rbuffer, 0, 6) == "PING :") {
+ $this->sendCommand("PONG: " . substr($this->rbuffer, 6) . "\n\r");
+ }
+ }
+
+ // Return the read buffer
+ function showReadBuffer() {
+ $this->rbuffer = fgets($this->socket, 1024);
+ $line = "[RECIVE] '".$this->rbuffer."'<br />\n\r";
+ return $line;
+ }
+
+ // Returns an array with the last message
+ function returnLastSaid(&$message)
+ {
+ $needle = "/^:([a-zA-Z0-9_\-]+)!([a-zA-Z0-9_\-~]+)[@]([a-zA-Z0-9_\-\.]+) PRIVMSG ([#a-zA-Z0-9\-_]+) :([a-zA-Z!]+)(.*)$/";
+ preg_match($needle, $this->rbuffer, $message);
+ }
+
+ // Flush connection
+ function flushIrc() {
+ flush();
+ }
+
+ // Close the connection
+ function closeConnection() {
+ if (fclose($this->socket)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
+
+?>

0 comments on commit 67d3a3e

Please sign in to comment.