-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.php
202 lines (172 loc) · 5.64 KB
/
irc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
error_reporting(E_ALL ^ E_NOTICE);
//So the bot doesnt stop.
set_time_limit(0);
// Open PID file
$tmpfilename = "/root/phpwikipedia/ircbot.pid";
if (!($tmpfile = @fopen($tmpfilename,"w")))
{
// Script already running - abort
echo "NekoBot is still active.";
return 0;
}
// Obtain an exlcusive lock on file
// (If script is running this will fail)
if (!@flock( $tmpfile, LOCK_EX | LOCK_NB, &$wouldblock) || $wouldblock)
{
// Script already running - abort
@fclose($tmpfile);
echo "NekoBot is still active.";
return 0;
}
include_once("database.php");
ini_set('display_errors', 'on');
//Example connection stuff.
include("config.php");
class IRCBot {
//This is going to hold our TCP/IP connection
var $socket;
var $intersock;
//This is going to hold all of the messages both server and client
var $ex = array();
var $inter = array();
/*
Construct item, opens the server connection, logs the bot in
@param array
*/
function __construct($config, $interirc)
{
$this->socket = fsockopen($config['server'], $config['port']);
$this->login($config);
$this->main(0, $interirc, 0, $config);
}
/*
Logs the bot in on the server
@param array
*/
function login($config)
{
$this->send_data('USER', $config['nick'].' lynx.crashcraft.co.uk '.$config['nick'].' :'.$config['name']);
$this->send_data('NICK', $config['nick']);
}
/*
This is the workhorse function, grabs the data from the server
and displays on the browser
*/
function main($nextcheck = 0, $interirc = 0, $time_start = 0, $config)
{
$faux = array(); $read=array($this->socket);
if(!stream_select($read, $faux, $faux, 0, 200000)){
if ($nextcheck < time() && $interirc == 1) {
include("bot.php");
} else {
echo "Nothing..\n";
mysql_ping();
sleep(5);
}
return $this->main($nextcheck, $interirc, $time_start, $config);
}
$data = fgets($this->socket, 128);
echo nl2br($data);
flush();
$this->ex = explode(' ', $data);
// Lets get the username, and hostname
$startsplit = explode(':', $data);
$getname = explode('!', $startsplit[1]);
$gethost = explode('@', $startsplit[1]);
$gethost = explode(' ', $gethost[1]);
$ircnickname = $getname[0]; // ta-da the username!
$irchostname = $gethost[0]; // ta-da the host!
if (strpos($data, ":End of /MOTD command.") !== false) {
$this->send_data('PRIVMSG NickServ', ':identify '.$config['nickpass']);
$this->send_data('MODE '.$config['nick'], '-x');
$this->send_data('JOIN', $config['channel']);
$interirc = 1;
}
if($this->ex[0] == 'PING')
{
$this->send_data('PONG', $this->ex[1]); //Plays ping-pong with the server to stay connected.
}
$command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
switch($command) //List of commands the bot responds to from a user.
{
case ':!join':
$this->join_channel($this->ex[4]);
break;
case ':!quit':
$query = mysql_query("SELECT * FROM `staff` WHERE `username` = '$ircnickname' AND `hostname` = '$irchostname'");
if (mysql_num_rows($query) == 0) {
$this->send_data('NOTICE '.$ircnickname, 'Access denied.');
} else {
$this->send_data('QUIT', 'Shutting Down Neko Services');
}
break;
case ':!fcheck':
$query = mysql_query("SELECT * FROM `staff` WHERE `username` = '$ircnickname' AND `hostname` = '$irchostname'");
if (mysql_num_rows($query) == 0) {
$this->send_data('NOTICE '.$ircnickname, ':Access denied.');
} else {
include("bot.php");
}
break;
case ':!check':
$data = explode(":!check ", $data);
$tosearch = rtrim(ltrim($data[1]));
$query = mysql_query("SELECT * FROM `3rrusers` WHERE `username` = '$tosearch'");
if (mysql_num_rows($query) == 0) {
$this->send_data('PRIVMSG '.$config['channel'], ':...Meow.. '.$tosearch.' isn\'t in my database.');
} else {
$getstuff = mysql_fetch_array($query);
$count = $getstuff['count'];
$status = $getstuff['status'];
if ($status == 0) { $status = "Decision Pending."; }
$this->send_data('PRIVMSG '.$config['channel'], ':...Meow! '.$tosearch.': Updated '.$count.' times, with latest status: '.$status.'!');
}
break;
case ':!run':
$query = mysql_query("SELECT * FROM `staff` WHERE `username` = '$ircnickname' AND `hostname` = '$irchostname'");
if (mysql_num_rows($query) == 0) {
$this->send_data('NOTICE '.$ircnickname, ':Access denied.');
} else {
$data = explode(":!run ", $data);
$this->send_data($data[1]);
}
break;
case ':!raw':
$query = mysql_query("SELECT * FROM `staff` WHERE `username` = '$ircnickname' AND `hostname` = '$irchostname'");
if (mysql_num_rows($query) == 0) {
$this->send_data('NOTICE '.$ircnickname, ':Access denied.');
} else {
$data = explode(":!raw ", $data);
$this->send_raw($data[1]);
}
break;
}
$this->main($nextcheck, $interirc, $time_start, $config);
}
function send_data($cmd, $msg = null) //displays stuff to the broswer and sends data to the server.
{
if($msg == null)
{
fputs($this->socket, $cmd."\r\n");
echo '<strong>'.$cmd.'</strong><br />';
} else {
fputs($this->socket, $cmd.' '.$msg."\r\n");
echo '<strong>'.$cmd.' '.$msg.'</strong><br />';
}
}
function join_channel($channel) //Joins a channel, used in the join function.
{
if(is_array($channel))
{
foreach($channel as $chan)
{
$this->send_data('JOIN', $chan);
}
} else {
$this->send_data('JOIN', $channel);
}
}
}
$bot = new IRCBot($config, $interirc);
?>