author | Dan |
Sat, 10 Jan 2009 14:12:36 -0500 | |
changeset 50 | 45164bc2567a |
parent 49 | 4c4d69b2cd4d |
child 51 | 508400fc5282 |
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 |
$in = array(); |
|
194 |
foreach ( $irc->channels as $channel ) |
|
195 |
{ |
|
196 |
$channame = $channel->get_channel_name(); |
|
197 |
if ( !in_array($channame, $channels) ) |
|
198 |
{ |
|
199 |
$channel->part("Leaving"); |
|
200 |
} |
|
201 |
else |
|
202 |
{ |
|
203 |
$in[] = $channame; |
|
204 |
} |
|
205 |
} |
|
206 |
unset($channel); |
|
207 |
foreach ( $channels as $channel ) |
|
208 |
{ |
|
209 |
if ( !in_array($channel, $in) ) |
|
210 |
{ |
|
211 |
$GLOBALS[ preg_replace('/^(#|&)/', '', $channel) ] = $irc->join($channel, 'enanobot_channel_event'); |
|
212 |
} |
|
213 |
} |
|
214 |
$irc->privmsg($message['nick'], "Config has been reloaded."); |
|
40 | 215 |
} |
216 |
else if ( substr($message['message'], 0, 1) == "\x01" && substr($message['message'], -1) == "\x01" ) |
|
217 |
{ |
|
218 |
$msg = trim($message['message'], "\x01"); |
|
219 |
list($ctcp) = explode(' ', $msg); |
|
220 |
$params = substr($msg, strlen($ctcp)+1); |
|
221 |
eval(eb_fetch_hook('event_ctcp')); |
|
222 |
} |
|
8 | 223 |
else if ( $message['action'] == 'PRIVMSG' ) |
0 | 224 |
{ |
8 | 225 |
eval(eb_fetch_hook('event_privmsg')); |
226 |
} |
|
227 |
else |
|
228 |
{ |
|
229 |
eval(eb_fetch_hook('event_other')); |
|
0 | 230 |
} |
231 |
} |
|
232 |
||
30 | 233 |
function enanobot_timeout_event($irc) |
234 |
{ |
|
235 |
// uh-oh. |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
236 |
$irc->close('client ping timeout (restarting connection)'); |
30 | 237 |
if ( defined('LIBIRC_DEBUG') ) |
238 |
{ |
|
239 |
$now = date('r'); |
|
240 |
echo "!!! [$now] Connection timed out; waiting 10 seconds and reconnecting\n"; |
|
241 |
} |
|
242 |
||
243 |
// re-init |
|
244 |
global $server, $nick, $user, $name, $pass, $channels, $libirc_channels; |
|
245 |
||
246 |
// wait until we can get into the server |
|
247 |
while ( true ) |
|
248 |
{ |
|
249 |
sleep(10); |
|
250 |
if ( defined('LIBIRC_DEBUG') ) |
|
251 |
{ |
|
252 |
$now = date('r'); |
|
253 |
echo "... [$now] Attempting reconnect\n"; |
|
254 |
} |
|
255 |
$conn = @fsockopen($server, 6667, $errno, $errstr, 5); |
|
256 |
if ( $conn ) |
|
257 |
{ |
|
258 |
if ( defined('LIBIRC_DEBUG') ) |
|
259 |
{ |
|
260 |
$now = date('r'); |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
261 |
echo "!!! [$now] Reconnection successful, ghosting old login (waiting 5 seconds to avoid throttling)\n"; |
30 | 262 |
} |
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
263 |
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 | 264 |
fclose($conn); |
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
265 |
sleep(5); |
30 | 266 |
break; |
267 |
} |
|
268 |
else |
|
269 |
{ |
|
270 |
if ( defined('LIBIRC_DEBUG') ) |
|
271 |
{ |
|
272 |
$now = date('r'); |
|
273 |
echo "!!! [$now] Still waiting for connection to come back up\n"; |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
$libirc_channels = array(); |
|
279 |
||
280 |
// we were able to get back in; ask NickServ to GHOST the old nick |
|
38 | 281 |
$irc->connect($nick, $user, $name, false); |
30 | 282 |
|
283 |
foreach ( $channels as $channel ) |
|
284 |
{ |
|
285 |
$libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event'); |
|
286 |
$channel_clean = preg_replace('/^[#&]/', '', $channel); |
|
287 |
$libirc_channels[$channel_clean] =& $libirc_channels[$channel]; |
|
288 |
$irc->privmsg('ChanServ', "OP $channel $nick"); |
|
289 |
} |
|
290 |
} |
|
291 |
||
8 | 292 |
if ( $_shutdown ) |
293 |
{ |
|
294 |
exit(2); |
|
295 |
} |