-
Notifications
You must be signed in to change notification settings - Fork 1
/
irc_functions.php
128 lines (119 loc) · 4 KB
/
irc_functions.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
<?php
namespace CluebotNG;
/*
* Copyright (C) 2015 Jacobi Carter and Chris Breneman
*
* This file is part of ClueBot NG.
*
* ClueBot NG 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.
*
* ClueBot NG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ClueBot NG. If not, see <http://www.gnu.org/licenses/>.
*/
class IRC
{
private static $chans = array();
public static function split($message)
{
if (!$message) {
return;
}
$return = array();
$i = 0;
$quotes = false;
if ($message[$i] == ':') {
$return['type'] = 'relayed';
++$i;
} else {
$return['type'] = 'direct';
}
$return['rawpieces'] = array();
$temp = '';
for (; $i < strlen($message); ++$i) {
if ($quotes and $message[$i] != '"') {
$temp .= $message[$i];
} else {
switch ($message[$i]) {
case ' ':
$return['rawpieces'][] = $temp;
$temp = '';
break;
case '"':
if ($quotes or $temp == '') {
$quotes = !$quotes;
break;
}
// Ignore
case ':':
if ($temp == '') {
++$i;
$return['rawpieces'][] = substr($message, $i);
$i = strlen($message);
break;
}
// Ignore
default:
$temp .= $message[$i];
}
}
}
if ($temp != '') {
$return['rawpieces'][] = $temp;
}
if ($return['type'] == 'relayed') {
$return['source'] = $return['rawpieces'][0];
$return['command'] = strtolower($return['rawpieces'][1]);
$return['target'] = $return['rawpieces'][2];
$return['pieces'] = array_slice($return['rawpieces'], 3);
} else {
$return['source'] = 'Server';
$return['command'] = strtolower($return['rawpieces'][0]);
$return['target'] = 'You';
$return['pieces'] = array_slice($return['rawpieces'], 1);
}
$return['raw'] = $message;
return $return;
}
public static function init()
{
$ircconfig = explode("\n", Api::$q->getpage('User:' . Config::$owner . '/CBChannels.js'));
$tmp = array();
foreach ($ircconfig as $tmpline) {
if (strlen($tmpline) > 1) {
if ($tmpline[0] != '#') {
$tmpline = explode('=', $tmpline, 2);
if (count($tmpline) == 2) {
$tmp[trim($tmpline[0])] = trim($tmpline[1]);
}
}
}
}
self::$chans = $tmp;
}
public static function spam($message)
{
return self::message('#wikipedia-en-cbngfeed', $message);
}
public static function revert($message)
{
return self::message('#wikipedia-en-cbngrevertfeed', $message);
}
private static function message($channel, $message)
{
global $logger;
$logger->addInfo('Saying to ' . $channel . ': ' . $message);
$udp = fsockopen('udp://' . Config::$relay_host, Config::$relay_port);
if ($udp !== false) {
fwrite($udp, $channel . ':' . $message);
fclose($udp);
}
}
}