This repository was archived by the owner on Apr 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirc.class.php
More file actions
414 lines (351 loc) · 10.9 KB
/
irc.class.php
File metadata and controls
414 lines (351 loc) · 10.9 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
/*
* ircc - a handy, portable console irc client
*
* Copyright (C) 2008 Robin Burchell <w00t@inspircd.org>
* Copyright (C) 2003 rainman <rainman@darkwired.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
class irc
{
public $sp; // the socket file pointer
public $output; // everything that is ready to be sent to whoever called us
public $usernick; // the nick of the user
public $line; // the line we read from the irc input
public $sender; // the nick that sent this message
// will be messed up if it's not a normal message but then it's not used
public $ex; // the explode(' ', $line), we use this a lot so it's an attribute
public $msg; // this is everything after the : (reread each line)
public $autonick; // if this is true we auto change nicks
public $chans = array(); // A lookup of channel name to buffer id
public $sCurrentTarget; // which window to send privmsg etc to (set by class torc)
public $torc;
function irc(&$torc)
{
$this->torc = $torc;
}
function connect(
$server, // (string) the server to connect to
$port, // (int) the port to connect to
$ssl, // (bool) wether this connection uses ssl
$username, // (string) username send in USER command
$hostname, // (string) hostname send in USER command
$servername, // (string) servername send in USER command
$realname, // (string) realname send in USER command
$nick, // (string) nick to specify in connect NICK command
$pass = '' // (string) optional password for server
)
{
$this->autonick = 1;
if($ssl)
{
// if we use ssl we must prefix our connect host with ssl://
$cntserver = 'ssl://'.$server;
}
else
{
$cntserver = $server;
}
$this->sp = fsockopen($cntserver, $port, $errno, $errstr, 10);
if(!$this->sp){
$this->torc->output->Output(BUFFER_CURRENT, 'Connect error: '.$errno.' - '.$errstr);
return 1; // in case of error: output the error and exit this function
}
//echo socket_set_blocking($this->sp, 0);
// because PASS, NICK and USER commands are also required for connect
// we'll run them here
// USER and PASS aren't changed later, so we send raw data to socket here
// for NICK we use the setnick() function
if(!empty($pass))
$this->sendline('PASS '.$pass);
$this->sendline('NICK ' . $nick);
$this->sendline('USER '.$username.' '.$hostname.' '.$servername.' :'.$realname);
// well, this should be it
// processing of the data we got back will not be done here
// incorrect USER params are ignored
// incorect PASS params will be seen and will kill the connection
// incorect NICK params will be passed to nick()
// a PING will just be processed by pingreply()
// (we hope)
return 0;
}
/*
* Careful!
* This blocks if called when there is no output to read - of course, this is a bad thing.
* This should only be called from the main polling loop really.
*/
function procline ()
{
$this->line .= fgets($this->sp);
if(substr($this->line, -1) != "\n")
{
return 1;
}
// This is all ugly, really. Backwards compatibility.
$this->line = trim($this->line);
$this->ex = Utils::ParseLine($this->line);
$this->line = "";
$this->msg = $this->ex[count($this->ex) - 1];
$this->sender = explode("!", $this->ex[0]);
$this->sender = $this->sender[0];
if($this->ex[1] == 'NOTICE')
{
$this->procnotice();
}
else if($this->ex[1] == 'PRIVMSG')
{
$this->procprivmsg();
}
else if(((int)$this->ex[1]) != 0)
{
// detects > :Darkwired 375 rian :- Darkwired Message of the Day -
// and messages with other numbers
$this->procnumeric();
}
else if($this->ex[1] == 'MODE')
{
if($this->ex[2] == $this->usernick)
{
// detects > :rian MODE rian :+i
$this->procusermode();
}
else
{
// detects > :rainman__!~rainman@127.0.0.1 MODE #test +o rian
// and > :Darkwired MODE #test2 +nt
$this->procchanmode();
}
}
else if($this->ex[1] == 'JOIN')
$this->procchanjoin();
else if($this->ex[1] == 'PART')
$this->procchanpart();
else if($this->ex[1] == 'KICK')
$this->procchankick();
else if($this->ex[1] == 'QUIT')
$this->procquit();
else if($this->ex[1] == 'NICK')
$this->procnick();
else if($this->ex[0] == 'PING')
$this->procping();
else if($this->ex[0] == 'PONG')
$this->procpong();
else if($this->ex[1] == 'TOPIC')
$this->proctopic();
return 0;
}
function SetUserNick($sNick)
{
$this->usernick = $sNick;
$this->torc->output->SetDisplayVar("nick", $sNick);
}
function skick($chan, $victim, $reason = "Expelled")
{
$this->sendline('KICK '.trim($chan).' '.trim($victim).' :'.trim($target));
}
function smode($target, $mode)
{
$this->sendline('MODE '.trim($target).' '.trim($mode));
}
function spart($chan, $reason = 'Parting')
{
$this->sendline('PART '.trim($chan).' :'.trim($reason));
}
function squit($reason)
{
$reasont = trim($reason);
if(empty($reason))
$reason = 'Leaving';
$this->sendline('QUIT :'.trim($reason));
}
function sprivmsg($target, $msg, $disp = true)
{
$this->sendline('PRIVMSG '.trim($target).' :'.trim($msg));
if($disp)
$this->torc->output->Output(BUFFER_CURRENT, 'msg '.$target.' '.$msg);
}
function snotice($target, $msg)
{
$this->sendline('NOTICE '.trim($target).' :'.trim($msg));
$this->torc->output->Output(BUFFER_CURRENT, 'notice -'.$target.'- '.$msg);
}
function stopic($target, $new = '')
{
if(trim($new) != '')
{
$this->sendline('TOPIC '.$target.' :'.$new);
}
else
{
$this->sendline('TOPIC '.$target);
}
}
function say($msg){
$this->sprivmsg($this->sCurrentTarget, trim($msg), false);
$this->torc->output->Output(BUFFER_CURRENT, '<'.$this->usernick.'> '.$msg);
}
function saction($msg){
$this->sprivmsg($this->sCurrentTarget, chr(1).'ACTION '.trim($msg).chr(1),false);
$this->torc->output->Output(BUFFER_CURRENT, '*'.$this->usernick . ' '.trim($msg).'*');
}
function GetBufferID($sTarget)
{
if (isset($this->chans[$sTarget]))
return $this->chans[$sTarget];
return -1;
}
/*-----------------------------------
BELOW FUNCTIONS FOR INTERNAL USE ONLY
-----------------------------------*/
function proctopic()
{
$this->torc->output->Output($this->GetBufferID($this->ex[2]), $this->sender.' set topic for '.$this->ex[2].' to '.$this->msg);
}
function procpong()
{
}
function procping()
{
$this->sendline('PONG '.$this->ex[1]);
}
function procnick()
{
// We want to update our nick IF: it's us changing nick, or if we haven't yet set a nick.
if ($this->usernick == $this->sender || empty($this->usernick))
$this->SetUserNick($this->ex[2]);
// XXX we need to output this on all buffers, or something.
$this->torc->output->Output(BUFFER_CURRENT, $this->sender.' is now known as '.$this->ex[2]);
}
function procquit()
{
$this->torc->output->Output(BUFFER_CURRENT, 'Quit: '.$this->sender.' ['.$this->msg.']');
}
function procchankick()
{
$this->torc->output->Output($this->GetBufferID($this->ex[2]), $this->ex[3].' was kicked off '.$this->ex[2].' by '.$this->sender.': '.$this->msg);
}
function procchanpart()
{
$this->torc->output->Output($this->GetBufferID($this->ex[2]), $this->sender.' has left channel '.$this->ex[2].': '.$this->msg);
if ($this->sender == $this->usernick)
{
// XXX do we want to do this?
$this->torc->output->DrawBuffer(BUFFER_STATUS);
$this->torc->output->DeleteBuffer($this->chans[$this->ex[2]]);
unset($this->chans[$this->ex[2]]);
}
}
function procchanjoin()
{
$this->ex[2] = trim($this->ex[2]);
$iId = $this->GetBufferID($this->ex[2]);
// If the channel hasn't been created before, do so now.
if ($iId == -1)
{
$this->torc->output->Output(BUFFER_CURRENT, "Creating new buffer as current one doesn't exist for " . $this->ex[2]);
$iId = $this->torc->output->AddBuffer($this->ex[2]);
$this->chans[$this->ex[2]] = $iId;
$this->torc->output->DrawBuffer($iId);
$this->torc->output->Output(BUFFER_CURRENT, "Created a new buffer, ID is " . $this->GetBufferID($this->ex[2]));
}
$this->torc->output->Output($this->GetBufferID($this->ex[2]), $this->sender. ' has joined '.substr($this->ex[2], 1));
}
function procchanmode()
{
$sOut = 'chanmode ' . $this->ex[2] . ' ' . $this->ex[3];
if (isset($this->ex[4]))
$sOut .= ' ' . $this->ex[4];
$sOut .= ' by '.$this->sender;
$this->torc->output->Output($this->GetBufferID($this->ex[2]), $sOut);
}
function procusermode(){
$this->torc->output->Output(BUFFER_CURRENT, 'usermode '.$this->ex[2].' '.$this->msg.' by '.$this->sender);
}
function procnumeric()
{
//we can't use $this->msg here, it can't handle :Darkwired 254 rain- 2 :channels formed
$ar = $this->ex;
$ar[0] = '';
$ar[1] = '';
$ar[2] = '';
$mg = implode(' ', $ar);
$mg = trim($mg);
if($this->autonick)
{
$this->autonick++;
if($this->autonick > 5)
$this->autonick = false;
if($this->ex[1] == 437 || $this->ex[1] == 433)
$this->sendline("NICK " . $this->ex[3].'_');
}
if ($this->ex[1] == "001")
$this->SetUserNick($this->ex[2]);
if(substr($mg, 0, 1) == ':')
$mg = substr($mg, 1);
$this->torc->output->Output(BUFFER_CURRENT, '-'.$this->sender . " (" . $this->ex[1] . ")" . '- '.$mg);
}
function procprivmsg()
{
if ($this->ex[2][0] == "#")
{
if ($this->msg[0] == chr(1))
{
// CTCP request..
$aCTCP = explode(" ", $this->msg);
$sCTCP = substr($aCTCP[0], 1);
$sParams = implode(" ", array_slice($aCTCP, 1));
$sParams = substr($sParams, 0, strlen($sParams) - 1);
if ($sCTCP == "ACTION")
{
$this->torc->output->Output($this->GetBufferID($this->ex[2]), "* " . $this->sender . " " . $sParams);
}
}
else
{
$this->torc->output->Output($this->GetBufferID($this->ex[2]), '<'.$this->sender.'> '.$this->msg);
}
}
else
{
if (preg_match('/^'.chr(1).'VERSION(.*?)'.chr(1).'$/i', $this->msg))
{
$this->torc->output->Output(BUFFER_CURRENT, 'CTCP VERSION request from '.$this->sender);
}
else
{
$this->torc->output->Output($this->GetBufferID($this->sender), '('.$this->sender.') '.$this->msg);
}
}
}
function procnotice()
{
if ($this->ex[2][0] == "#")
{
$this->torc->output->Output($this->GetBufferID($this->ex[2]), '-'.$this->ex[2].'/'.$this->sender.'- '.$this->msg);
}
else
{
if (preg_match('/^'.chr(1).'VERSION(.*?)'.chr(1).'$/i', $this->msg))
{
$this->torc->output->Output(BUFFER_CURRENT, 'CTCP VERSION reply from '.$this->sender.': '.substr($this->msg, 9, strlen($this->msg)-10));
}
else
{
$this->torc->output->Output(BUFFER_CURRENT, '-'.$this->sender.'- '.$this->msg);
}
}
}
function sendline($data)
{
// sends a line to our irc
if(!@fwrite($this->sp, $data."\r\n"))
{
$this->torc->output->Output(BUFFER_CURRENT, 'Warning: error writing ['.$data.']');
}
}
}
?>