author | Dan |
Sat, 10 Jan 2009 14:08:11 -0500 | |
changeset 49 | 4c4d69b2cd4d |
parent 41 | 1d854f22ac5a |
child 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'); |
0 | 78 |
|
40 | 79 |
$enanobot_version = '0.5-unstable'; |
80 |
||
0 | 81 |
@ini_set('display_errors', 'on'); |
8 | 82 |
error_reporting(E_ALL); |
83 |
||
84 |
// load modules |
|
85 |
foreach ( $modules as $module ) |
|
86 |
{ |
|
87 |
$modulefile = "modules/$module.php"; |
|
88 |
if ( file_exists($modulefile) ) |
|
89 |
{ |
|
90 |
require($modulefile); |
|
91 |
} |
|
92 |
} |
|
0 | 93 |
|
23
df31a3872d19
Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents:
13
diff
changeset
|
94 |
mysql_reconnect(); |
0 | 95 |
|
23
df31a3872d19
Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents:
13
diff
changeset
|
96 |
eval(eb_fetch_hook('startup_early')); |
0 | 97 |
|
6 | 98 |
$libirc_channels = array(); |
99 |
||
8 | 100 |
$irc = new Request_IRC($server); |
0 | 101 |
$irc->connect($nick, $user, $name, $pass); |
102 |
$irc->set_privmsg_handler('enanobot_privmsg_event'); |
|
30 | 103 |
$irc->set_timeout_handlers(false, 'enanobot_timeout_event'); |
6 | 104 |
|
105 |
foreach ( $channels as $channel ) |
|
106 |
{ |
|
107 |
$libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event'); |
|
108 |
$channel_clean = preg_replace('/^[#&]/', '', $channel); |
|
109 |
$libirc_channels[$channel_clean] =& $libirc_channels[$channel]; |
|
110 |
$irc->privmsg('ChanServ', "OP $channel $nick"); |
|
111 |
} |
|
0 | 112 |
|
113 |
$irc->event_loop(); |
|
114 |
$irc->close(); |
|
115 |
mysql_close($mysql_conn); |
|
116 |
||
6 | 117 |
function enanobot_channel_event($sockdata, $chan) |
0 | 118 |
{ |
119 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
120 |
$sockdata = trim($sockdata); |
|
121 |
$message = Request_IRC::parse_message($sockdata); |
|
6 | 122 |
$channelname = $chan->get_channel_name(); |
8 | 123 |
|
124 |
eval(eb_fetch_hook('event_raw_message')); |
|
125 |
||
0 | 126 |
switch ( $message['action'] ) |
127 |
{ |
|
128 |
case 'JOIN': |
|
8 | 129 |
eval(eb_fetch_hook('event_join')); |
130 |
break; |
|
131 |
case 'PART': |
|
132 |
eval(eb_fetch_hook('event_part')); |
|
0 | 133 |
break; |
134 |
case 'PRIVMSG': |
|
135 |
enanobot_process_channel_message($sockdata, $chan, $message); |
|
136 |
break; |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
function enanobot_process_channel_message($sockdata, $chan, $message) |
|
141 |
{ |
|
142 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
143 |
||
8 | 144 |
if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick ) |
0 | 145 |
{ |
9 | 146 |
$target_nick =& $message['nick']; |
13 | 147 |
// $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 | 148 |
} |
8 | 149 |
else |
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
150 |
{ |
8 | 151 |
eval(eb_fetch_hook('event_channel_msg')); |
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
152 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
153 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
154 |
|
0 | 155 |
function enanobot_privmsg_event($message) |
156 |
{ |
|
6 | 157 |
global $privileged_list, $irc, $nick; |
0 | 158 |
static $part_cache = array(); |
159 |
if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' ) |
|
160 |
{ |
|
161 |
foreach ( $irc->channels as $channel ) |
|
162 |
{ |
|
163 |
$part_cache[] = array($channel->get_channel_name(), $channel->get_handler()); |
|
8 | 164 |
$channel->msg("I've received a request from {$message['nick']} to stop responding to requests, messages, and activities. Don't forget to unsuspend me with /msg $nick Resume when finished.", true); |
0 | 165 |
$channel->part("Logging and presence suspended by {$message['nick']}", true); |
166 |
} |
|
167 |
} |
|
168 |
else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' ) |
|
169 |
{ |
|
170 |
global $nick; |
|
171 |
foreach ( $part_cache as $chan_data ) |
|
172 |
{ |
|
173 |
$chan_name = substr($chan_data[0], 1); |
|
174 |
$GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]); |
|
175 |
$GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true); |
|
176 |
$irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick"); |
|
177 |
} |
|
178 |
$part_cache = array(); |
|
179 |
} |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
180 |
else if ( in_array($message['nick'], $privileged_list) && preg_match('/^Shutdown(?: (.+))?$/i', $message['message'], $match) && $message['action'] == 'PRIVMSG' ) |
0 | 181 |
{ |
8 | 182 |
$GLOBALS['_shutdown'] = true; |
41 | 183 |
$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
|
184 |
$irc->close($quitmessage, true); |
0 | 185 |
return 'BREAK'; |
186 |
} |
|
40 | 187 |
else if ( in_array($message['nick'], $privileged_list) && preg_match('/^re(?:hash|load)?(?:config)?(?: |$)/', $message['message']) ) |
188 |
{ |
|
189 |
require('config.php'); |
|
190 |
$GLOBALS['privileged_list'] = $privileged_list; |
|
191 |
$GLOBALS['alert_list'] = $alert_list; |
|
49 | 192 |
$GLOBALS['channels'] = $channels; |
193 |
print_r($channels); |
|
194 |
$in = array(); |
|
195 |
foreach ( $irc->channels as $channel ) |
|
196 |
{ |
|
197 |
$channame = $channel->get_channel_name(); |
|
198 |
if ( !in_array($channame, $channels) ) |
|
199 |
{ |
|
200 |
$channel->part("Leaving"); |
|
201 |
} |
|
202 |
else |
|
203 |
{ |
|
204 |
$in[] = $channame; |
|
205 |
} |
|
206 |
} |
|
207 |
unset($channel); |
|
208 |
foreach ( $channels as $channel ) |
|
209 |
{ |
|
210 |
if ( !in_array($channel, $in) ) |
|
211 |
{ |
|
212 |
$GLOBALS[ preg_replace('/^(#|&)/', '', $channel) ] = $irc->join($channel, 'enanobot_channel_event'); |
|
213 |
} |
|
214 |
} |
|
215 |
$irc->privmsg($message['nick'], "Config has been reloaded."); |
|
40 | 216 |
} |
217 |
else if ( substr($message['message'], 0, 1) == "\x01" && substr($message['message'], -1) == "\x01" ) |
|
218 |
{ |
|
219 |
$msg = trim($message['message'], "\x01"); |
|
220 |
list($ctcp) = explode(' ', $msg); |
|
221 |
$params = substr($msg, strlen($ctcp)+1); |
|
222 |
eval(eb_fetch_hook('event_ctcp')); |
|
223 |
} |
|
8 | 224 |
else if ( $message['action'] == 'PRIVMSG' ) |
0 | 225 |
{ |
8 | 226 |
eval(eb_fetch_hook('event_privmsg')); |
227 |
} |
|
228 |
else |
|
229 |
{ |
|
230 |
eval(eb_fetch_hook('event_other')); |
|
0 | 231 |
} |
232 |
} |
|
233 |
||
30 | 234 |
function enanobot_timeout_event($irc) |
235 |
{ |
|
236 |
// uh-oh. |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
237 |
$irc->close('client ping timeout (restarting connection)'); |
30 | 238 |
if ( defined('LIBIRC_DEBUG') ) |
239 |
{ |
|
240 |
$now = date('r'); |
|
241 |
echo "!!! [$now] Connection timed out; waiting 10 seconds and reconnecting\n"; |
|
242 |
} |
|
243 |
||
244 |
// re-init |
|
245 |
global $server, $nick, $user, $name, $pass, $channels, $libirc_channels; |
|
246 |
||
247 |
// wait until we can get into the server |
|
248 |
while ( true ) |
|
249 |
{ |
|
250 |
sleep(10); |
|
251 |
if ( defined('LIBIRC_DEBUG') ) |
|
252 |
{ |
|
253 |
$now = date('r'); |
|
254 |
echo "... [$now] Attempting reconnect\n"; |
|
255 |
} |
|
256 |
$conn = @fsockopen($server, 6667, $errno, $errstr, 5); |
|
257 |
if ( $conn ) |
|
258 |
{ |
|
259 |
if ( defined('LIBIRC_DEBUG') ) |
|
260 |
{ |
|
261 |
$now = date('r'); |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
262 |
echo "!!! [$now] Reconnection successful, ghosting old login (waiting 5 seconds to avoid throttling)\n"; |
30 | 263 |
} |
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
264 |
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 | 265 |
fclose($conn); |
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
266 |
sleep(5); |
30 | 267 |
break; |
268 |
} |
|
269 |
else |
|
270 |
{ |
|
271 |
if ( defined('LIBIRC_DEBUG') ) |
|
272 |
{ |
|
273 |
$now = date('r'); |
|
274 |
echo "!!! [$now] Still waiting for connection to come back up\n"; |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
$libirc_channels = array(); |
|
280 |
||
281 |
// we were able to get back in; ask NickServ to GHOST the old nick |
|
38 | 282 |
$irc->connect($nick, $user, $name, false); |
30 | 283 |
|
284 |
foreach ( $channels as $channel ) |
|
285 |
{ |
|
286 |
$libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event'); |
|
287 |
$channel_clean = preg_replace('/^[#&]/', '', $channel); |
|
288 |
$libirc_channels[$channel_clean] =& $libirc_channels[$channel]; |
|
289 |
$irc->privmsg('ChanServ', "OP $channel $nick"); |
|
290 |
} |
|
291 |
} |
|
292 |
||
8 | 293 |
if ( $_shutdown ) |
294 |
{ |
|
295 |
exit(2); |
|
296 |
} |