author | Dan |
Tue, 20 Jan 2009 22:08:07 -0500 | |
changeset 51 | 508400fc5282 |
parent 50 | 45164bc2567a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
6 | 2 |
/** |
3 |
* EnanoBot - the Enano CMS IRC logging and help automation bot |
|
4 |
* GPL and no warranty, see the LICENSE file for more info |
|
5 |
*/ |
|
6 |
||
7 | 7 |
// parse command line |
8 |
if ( isset($argv[1]) ) |
|
9 |
{ |
|
10 |
$arg =& $argv[1]; |
|
11 |
if ( $arg == '--daemon' || $arg == '-d' ) |
|
12 |
{ |
|
13 |
// attempt to fork... |
|
14 |
if ( function_exists('pcntl_fork') ) |
|
15 |
{ |
|
16 |
$pid = pcntl_fork(); |
|
17 |
if ( $pid == -1 ) |
|
18 |
{ |
|
19 |
echo "Forking process failed.\n"; |
|
20 |
exit(1); |
|
21 |
} |
|
22 |
else if ( $pid ) |
|
23 |
{ |
|
24 |
echo "EnanoBot daemon started, pid $pid\n"; |
|
25 |
exit(0); |
|
26 |
} |
|
27 |
else |
|
28 |
{ |
|
29 |
// do nothing, just continue. |
|
30 |
} |
|
31 |
} |
|
32 |
else |
|
33 |
{ |
|
34 |
echo "No pcntl support in PHP, continuing in foreground\n"; |
|
35 |
} |
|
36 |
} |
|
37 |
else if ( $arg == '-v' || $arg == '--verbose' ) |
|
38 |
{ |
|
39 |
define('LIBIRC_DEBUG', ''); |
|
40 |
} |
|
41 |
else |
|
42 |
{ |
|
43 |
echo <<<EOF |
|
44 |
Usage: {$argv[0]} |
|
45 |
Options: |
|
46 |
-d, --daemon Run in background (requires pcntl support) |
|
47 |
-v, --verbose Log communication to stdout (ignored if -d specified) |
|
48 |
-h, --help This help message |
|
49 |
||
50 |
EOF; |
|
51 |
exit(1); |
|
52 |
} |
|
53 |
} |
|
6 | 54 |
|
8 | 55 |
$censored_words = array('cock', 'fuck', 'cuck', 'funt', 'cunt', 'bitch'); |
56 |
$_shutdown = false; |
|
57 |
||
58 |
function eb_censor_words($text) |
|
59 |
{ |
|
60 |
// return $text; |
|
61 |
||
62 |
global $censored_words; |
|
63 |
foreach ( $censored_words as $word ) |
|
64 |
{ |
|
65 |
$replacement = substr($word, 0, 1) . preg_replace('/./', '*', substr($word, 1)); |
|
66 |
while ( stristr($text, $word) ) |
|
67 |
{ |
|
68 |
$text = preg_replace("/$word/i", $replacement, $text); |
|
69 |
} |
|
70 |
} |
|
71 |
return $text; |
|
72 |
} |
|
73 |
||
0 | 74 |
require('libirc.php'); |
8 | 75 |
require('hooks.php'); |
0 | 76 |
require('config.php'); |
23
df31a3872d19
Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents:
13
diff
changeset
|
77 |
require('database.php'); |
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
78 |
require('permissions.php'); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
79 |
|
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
80 |
if ( !isset($permissions) ) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
81 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
82 |
foreach ( $privileged_list as $user ) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
83 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
84 |
$permissions[$user] = array('admin'); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
85 |
} |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
86 |
if ( isset($alert_list) ) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
87 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
88 |
foreach ( $alert_list as $user ) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
89 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
90 |
$permissions[$user] = array('admin', 'alert'); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
91 |
} |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
92 |
} |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
93 |
} |
0 | 94 |
|
40 | 95 |
$enanobot_version = '0.5-unstable'; |
96 |
||
0 | 97 |
@ini_set('display_errors', 'on'); |
8 | 98 |
error_reporting(E_ALL); |
99 |
||
100 |
// load modules |
|
101 |
foreach ( $modules as $module ) |
|
102 |
{ |
|
103 |
$modulefile = "modules/$module.php"; |
|
104 |
if ( file_exists($modulefile) ) |
|
105 |
{ |
|
106 |
require($modulefile); |
|
107 |
} |
|
108 |
} |
|
0 | 109 |
|
23
df31a3872d19
Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents:
13
diff
changeset
|
110 |
mysql_reconnect(); |
0 | 111 |
|
23
df31a3872d19
Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents:
13
diff
changeset
|
112 |
eval(eb_fetch_hook('startup_early')); |
0 | 113 |
|
6 | 114 |
$libirc_channels = array(); |
115 |
||
8 | 116 |
$irc = new Request_IRC($server); |
0 | 117 |
$irc->connect($nick, $user, $name, $pass); |
118 |
$irc->set_privmsg_handler('enanobot_privmsg_event'); |
|
30 | 119 |
$irc->set_timeout_handlers(false, 'enanobot_timeout_event'); |
6 | 120 |
|
121 |
foreach ( $channels as $channel ) |
|
122 |
{ |
|
123 |
$libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event'); |
|
124 |
$channel_clean = preg_replace('/^[#&]/', '', $channel); |
|
125 |
$libirc_channels[$channel_clean] =& $libirc_channels[$channel]; |
|
126 |
$irc->privmsg('ChanServ', "OP $channel $nick"); |
|
127 |
} |
|
0 | 128 |
|
129 |
$irc->event_loop(); |
|
130 |
$irc->close(); |
|
131 |
mysql_close($mysql_conn); |
|
132 |
||
6 | 133 |
function enanobot_channel_event($sockdata, $chan) |
0 | 134 |
{ |
135 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
136 |
$sockdata = trim($sockdata); |
|
137 |
$message = Request_IRC::parse_message($sockdata); |
|
6 | 138 |
$channelname = $chan->get_channel_name(); |
8 | 139 |
|
140 |
eval(eb_fetch_hook('event_raw_message')); |
|
141 |
||
0 | 142 |
switch ( $message['action'] ) |
143 |
{ |
|
144 |
case 'JOIN': |
|
8 | 145 |
eval(eb_fetch_hook('event_join')); |
146 |
break; |
|
147 |
case 'PART': |
|
148 |
eval(eb_fetch_hook('event_part')); |
|
0 | 149 |
break; |
150 |
case 'PRIVMSG': |
|
151 |
enanobot_process_channel_message($sockdata, $chan, $message); |
|
152 |
break; |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
function enanobot_process_channel_message($sockdata, $chan, $message) |
|
157 |
{ |
|
158 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
159 |
||
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
160 |
if ( strpos($message['message'], $nick) && !check_permissions($message['nick'], array('context' => 'channel', 'channel' => $chan->get_channel_name())) && $message['nick'] != $nick ) |
0 | 161 |
{ |
9 | 162 |
$target_nick =& $message['nick']; |
13 | 163 |
// $chan->msg("{$target_nick}, I'm only a bot. :-) You should probably rely on the advice of humans if you need further assistance.", true); |
0 | 164 |
} |
8 | 165 |
else |
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
166 |
{ |
8 | 167 |
eval(eb_fetch_hook('event_channel_msg')); |
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
168 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
169 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
170 |
|
0 | 171 |
function enanobot_privmsg_event($message) |
172 |
{ |
|
6 | 173 |
global $privileged_list, $irc, $nick; |
0 | 174 |
static $part_cache = array(); |
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
175 |
if ( $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' && check_permissions($message['nick'], array('context' => 'suspend'), false) ) |
0 | 176 |
{ |
177 |
foreach ( $irc->channels as $channel ) |
|
178 |
{ |
|
179 |
$part_cache[] = array($channel->get_channel_name(), $channel->get_handler()); |
|
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
180 |
$channel->part("suspended by {$message['nick']}", true); |
0 | 181 |
} |
182 |
} |
|
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
183 |
else if ( $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' && check_permissions($message['nick'], array('context' => 'suspend'), false) ) |
0 | 184 |
{ |
185 |
global $nick; |
|
186 |
foreach ( $part_cache as $chan_data ) |
|
187 |
{ |
|
188 |
$chan_name = substr($chan_data[0], 1); |
|
189 |
$GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]); |
|
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
190 |
$GLOBALS[$chan_name]->msg("(resumed by {$message['nick']})", true); |
0 | 191 |
$irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick"); |
192 |
} |
|
193 |
$part_cache = array(); |
|
194 |
} |
|
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
195 |
else if ( preg_match('/^Shutdown(?: (.+))?$/i', $message['message'], $match) && $message['action'] == 'PRIVMSG' && check_permissions($message['nick'], array('context' => 'shutdown'), false) ) |
0 | 196 |
{ |
8 | 197 |
$GLOBALS['_shutdown'] = true; |
41 | 198 |
$quitmessage = empty($match[1]) ? "Remote bot shutdown requested by {$message['nick']}" : $match[1]; |
23
df31a3872d19
Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents:
13
diff
changeset
|
199 |
$irc->close($quitmessage, true); |
0 | 200 |
return 'BREAK'; |
201 |
} |
|
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
202 |
else if ( preg_match('/^re(?:hash|load)?(?:config)?(?: |$)/', $message['message']) && check_permissions($message['nick'], array('context' => 'rehash'), false) ) |
40 | 203 |
{ |
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
204 |
$oldnick = $GLOBALS['nick']; |
40 | 205 |
require('config.php'); |
206 |
$GLOBALS['privileged_list'] = $privileged_list; |
|
207 |
$GLOBALS['alert_list'] = $alert_list; |
|
49 | 208 |
$GLOBALS['channels'] = $channels; |
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
209 |
$GLOBALS['permissions'] = $permissions; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
210 |
if ( $nick != $oldnick ) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
211 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
212 |
$irc->change_nick($nick, $pass); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
213 |
$GLOBALS['nick'] = $nick; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
214 |
$GLOBALS['pass'] = $pass; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
50
diff
changeset
|
215 |
} |
49 | 216 |
$in = array(); |
217 |
foreach ( $irc->channels as $channel ) |
|
218 |
{ |
|
219 |
$channame = $channel->get_channel_name(); |
|
220 |
if ( !in_array($channame, $channels) ) |
|
221 |
{ |
|
222 |
$channel->part("Leaving"); |
|
223 |
} |
|
224 |
else |
|
225 |
{ |
|
226 |
$in[] = $channame; |
|
227 |
} |
|
228 |
} |
|
229 |
unset($channel); |
|
230 |
foreach ( $channels as $channel ) |
|
231 |
{ |
|
232 |
if ( !in_array($channel, $in) ) |
|
233 |
{ |
|
234 |
$GLOBALS[ preg_replace('/^(#|&)/', '', $channel) ] = $irc->join($channel, 'enanobot_channel_event'); |
|
235 |
} |
|
236 |
} |
|
237 |
$irc->privmsg($message['nick'], "Config has been reloaded."); |
|
40 | 238 |
} |
239 |
else if ( substr($message['message'], 0, 1) == "\x01" && substr($message['message'], -1) == "\x01" ) |
|
240 |
{ |
|
241 |
$msg = trim($message['message'], "\x01"); |
|
242 |
list($ctcp) = explode(' ', $msg); |
|
243 |
$params = substr($msg, strlen($ctcp)+1); |
|
244 |
eval(eb_fetch_hook('event_ctcp')); |
|
245 |
} |
|
8 | 246 |
else if ( $message['action'] == 'PRIVMSG' ) |
0 | 247 |
{ |
8 | 248 |
eval(eb_fetch_hook('event_privmsg')); |
249 |
} |
|
250 |
else |
|
251 |
{ |
|
252 |
eval(eb_fetch_hook('event_other')); |
|
0 | 253 |
} |
254 |
} |
|
255 |
||
30 | 256 |
function enanobot_timeout_event($irc) |
257 |
{ |
|
258 |
// uh-oh. |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
259 |
$irc->close('client ping timeout (restarting connection)'); |
30 | 260 |
if ( defined('LIBIRC_DEBUG') ) |
261 |
{ |
|
262 |
$now = date('r'); |
|
263 |
echo "!!! [$now] Connection timed out; waiting 10 seconds and reconnecting\n"; |
|
264 |
} |
|
265 |
||
266 |
// re-init |
|
267 |
global $server, $nick, $user, $name, $pass, $channels, $libirc_channels; |
|
268 |
||
269 |
// wait until we can get into the server |
|
270 |
while ( true ) |
|
271 |
{ |
|
272 |
sleep(10); |
|
273 |
if ( defined('LIBIRC_DEBUG') ) |
|
274 |
{ |
|
275 |
$now = date('r'); |
|
276 |
echo "... [$now] Attempting reconnect\n"; |
|
277 |
} |
|
278 |
$conn = @fsockopen($server, 6667, $errno, $errstr, 5); |
|
279 |
if ( $conn ) |
|
280 |
{ |
|
281 |
if ( defined('LIBIRC_DEBUG') ) |
|
282 |
{ |
|
283 |
$now = date('r'); |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
284 |
echo "!!! [$now] Reconnection successful, ghosting old login (waiting 5 seconds to avoid throttling)\n"; |
30 | 285 |
} |
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
286 |
fputs($conn, "QUIT :This bot needs better exception handling. But until then I'm going to need to make repeated TCP connection attempts when my ISP craps out. Sorry :-/\r\n"); |
30 | 287 |
fclose($conn); |
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
288 |
sleep(5); |
30 | 289 |
break; |
290 |
} |
|
291 |
else |
|
292 |
{ |
|
293 |
if ( defined('LIBIRC_DEBUG') ) |
|
294 |
{ |
|
295 |
$now = date('r'); |
|
296 |
echo "!!! [$now] Still waiting for connection to come back up\n"; |
|
297 |
} |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
$libirc_channels = array(); |
|
302 |
||
303 |
// we were able to get back in; ask NickServ to GHOST the old nick |
|
38 | 304 |
$irc->connect($nick, $user, $name, false); |
30 | 305 |
|
306 |
foreach ( $channels as $channel ) |
|
307 |
{ |
|
308 |
$libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event'); |
|
309 |
$channel_clean = preg_replace('/^[#&]/', '', $channel); |
|
310 |
$libirc_channels[$channel_clean] =& $libirc_channels[$channel]; |
|
311 |
$irc->privmsg('ChanServ', "OP $channel $nick"); |
|
312 |
} |
|
313 |
} |
|
314 |
||
8 | 315 |
if ( $_shutdown ) |
316 |
{ |
|
317 |
exit(2); |
|
318 |
} |