Skip to content

Commit 67d3a3e

Browse files
committed
First commit
0 parents  commit 67d3a3e

File tree

4 files changed

+258
-0
lines changed

4 files changed

+258
-0
lines changed

README

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
IRC Class
2+
3+
A simple PHP IRC class that is in the workings.
4+
Can be used for many things, including bots.
5+
Do what you want, clone and push if you want changes to be made to this repo or just
6+
work with it on your own.
7+
8+
No license, just do what you want with it.

config.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// This file contains all the constants
4+
define(IRC_HOST, "irc.freenode.org");
5+
define(IRC_PORT, "6667");
6+
7+
?>

irc.php

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
4+
// Set the timeout limit so the page wont timeout
5+
set_time_limit(0);
6+
7+
$channel = "";
8+
9+
// IRC Class
10+
include("irc_class.php");
11+
// Config
12+
include("config.php");
13+
14+
// Create an instance of the IRC class
15+
$irc = new ConnectIrc(IRC_HOST, IRC_PORT);
16+
17+
// Echo out the server and port
18+
echo "IRC Server: {$irc->server}<br />Port: {$irc->port}<br />";
19+
echo "<pre>";
20+
21+
// Open socket
22+
if ( $irc->openSocket() ) {
23+
24+
// Set password (leaving it blank defines no password)
25+
$irc->setPassword();
26+
// Set nick/user
27+
//$username = $_GET['nick'];
28+
if (isset($_GET['nick']))
29+
$username = $_GET['nick'];
30+
else
31+
$username = "LeonBot";
32+
33+
$irc->setNick($username);
34+
$irc->setUser($username);
35+
36+
// While you are connected to the server
37+
while ( $irc->connected() ) {
38+
39+
// Print out the read buffer
40+
$buffer = $irc->showReadBuffer();
41+
//echo $buffer."\n\r";
42+
43+
// Here is where you test for certain conditions
44+
$irc->returnLastSaid($message);
45+
$params = trim($message[PARAMS]);
46+
switch ($message[COMMAND])
47+
{
48+
// Shutting down
49+
case "!gtfo":
50+
echo "Shutting down\n\r";
51+
$irc->closeConnection(); exit;
52+
break;
53+
54+
// Saying hello
55+
case "!hello":
56+
echo "Saying hello to {$message[WHERE]}\n\r";
57+
$irc->say("Hey, {$message[SENDER]}!", $message[WHERE]);
58+
break;
59+
60+
// Handles joining rooms
61+
case "!join":
62+
echo "Joining {$params}\n\r";
63+
$channel = $params;
64+
$irc->joinChannel($channel);
65+
break;
66+
67+
// handles parting rooms
68+
case "!part":
69+
echo "Leaving {$params}\n\r";
70+
$channel = $params;
71+
$irc->partChannel($channel);
72+
break;
73+
74+
// changing nickname
75+
case "!nick":
76+
echo "Changing nick to {$params}\n\r";
77+
$irc->setNick($params);
78+
break;
79+
80+
// grabbing someone's twitter status
81+
case "!twitter":
82+
echo "Grabbing status for {$params}\n\r";
83+
$ch = curl_init();
84+
curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/{$params}.json");
85+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
86+
$json = curl_exec($ch);
87+
curl_close($ch);
88+
$jsonObject = json_decode($json);
89+
$status = $jsonObject[0]->text;
90+
echo "Got latest Tweet from {$params}\n\t-> {$status}";
91+
$irc->say("{$status}", $message[WHERE]);
92+
break;
93+
// for anything else ...
94+
default:
95+
if (strtolower($message[COMMAND]) == strtolower($irc->nick) ||
96+
strstr(strtolower($params), strtolower($irc->nick)))
97+
$irc->say("What the fuck do you want, {$message[SENDER]}?", $message[WHERE]);
98+
break;
99+
}
100+
101+
// Handle the ping pong
102+
$irc->handlePingPong();
103+
104+
// Flush the buffer
105+
$irc->flushIrc();
106+
}
107+
108+
// Close the connection
109+
if ( $irc->closeConnection() ) {
110+
echo "<br />Connection closed... ";
111+
} else {
112+
echo "<br />Connection had a problem closing... wait wtf?";
113+
}
114+
}
115+
116+
?>

irc_class.php

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
define(FULL_STRING, 0);
4+
define(SENDER, 1);
5+
define(CLIENT, 2);
6+
define(HOST_INFO, 3);
7+
define(WHERE, 4);
8+
define(COMMAND, 5);
9+
define(PARAMS, 6);
10+
11+
// ConnectIRC Class
12+
class ConnectIrc {
13+
14+
// Variables
15+
public $server;
16+
public $port;
17+
public $channel;
18+
public $password = "NOPASS";
19+
public $nick;
20+
public $socket;
21+
public $errno;
22+
public $errstr;
23+
public $timeout = 2;
24+
public $rbuffer;
25+
26+
// Constructor
27+
function __construct($server, $port) {
28+
$this->server = $server;
29+
$this->port = $port;
30+
}
31+
32+
// Open socket
33+
function openSocket() {
34+
$this->socket = fsockopen($this->server,
35+
$this->port,
36+
$this->errno,
37+
$this->errstr,
38+
$this->timeout);
39+
return $this->socket;
40+
}
41+
42+
// Set channel
43+
function setChannel($channel) {
44+
$this->channel = $channel;
45+
}
46+
47+
// Set nick
48+
function setNick($nick) {
49+
$this->nick = $nick;
50+
$this->sendCommand("NICK {$this->nick}\n\r");
51+
}
52+
53+
function setUser($nick) {
54+
$this->sendCommand("USER {$this->nick} {$this->nick} {$this->nick} {$this->nick} :{$this->nick} \n\r");
55+
}
56+
57+
// Set password
58+
function setPassword($password=false) {
59+
if ($password == true) {
60+
$this->password = $password;
61+
}
62+
$this->sendCommand("PASS {$this->password}\n\r");
63+
}
64+
65+
// Talk in the chat
66+
function say($message, $who)
67+
{
68+
$this->sendCommand("PRIVMSG {$who} :{$message}\n\r");
69+
}
70+
71+
// Return connected status
72+
function connected() {
73+
return !feof($this->socket);
74+
}
75+
76+
// Send command
77+
function sendCommand($command) {
78+
fputs($this->socket, $command, strlen($command));
79+
}
80+
81+
// Join channel
82+
function joinChannel($channel) {
83+
$this->sendCommand("JOIN {$channel}\n\r");
84+
}
85+
86+
// Part channel
87+
function partChannel($channel) {
88+
$this->sendCommand("PART {$channel}\n\r");
89+
}
90+
91+
// Handle the ping pong
92+
function handlePingPong() {
93+
if (substr($this->rbuffer, 0, 6) == "PING :") {
94+
$this->sendCommand("PONG: " . substr($this->rbuffer, 6) . "\n\r");
95+
}
96+
}
97+
98+
// Return the read buffer
99+
function showReadBuffer() {
100+
$this->rbuffer = fgets($this->socket, 1024);
101+
$line = "[RECIVE] '".$this->rbuffer."'<br />\n\r";
102+
return $line;
103+
}
104+
105+
// Returns an array with the last message
106+
function returnLastSaid(&$message)
107+
{
108+
$needle = "/^:([a-zA-Z0-9_\-]+)!([a-zA-Z0-9_\-~]+)[@]([a-zA-Z0-9_\-\.]+) PRIVMSG ([#a-zA-Z0-9\-_]+) :([a-zA-Z!]+)(.*)$/";
109+
preg_match($needle, $this->rbuffer, $message);
110+
}
111+
112+
// Flush connection
113+
function flushIrc() {
114+
flush();
115+
}
116+
117+
// Close the connection
118+
function closeConnection() {
119+
if (fclose($this->socket)) {
120+
return true;
121+
} else {
122+
return false;
123+
}
124+
}
125+
}
126+
127+
?>

0 commit comments

Comments
 (0)