author | Dan |
Sun, 04 Jan 2009 16:42:52 -0500 | |
changeset 45 | 53a50ba4d690 |
parent 41 | 1d854f22ac5a |
child 49 | 4c4d69b2cd4d |
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; |
|
192 |
$irc->privmsg($message['nick'], "Reloaded privileged_list and alert_list. privileged = " . str_replace("\n", '', print_r($privileged_list, true)) . "; alert = " . str_replace("\n", '', print_r($alert_list, true))); |
|
193 |
} |
|
194 |
else if ( substr($message['message'], 0, 1) == "\x01" && substr($message['message'], -1) == "\x01" ) |
|
195 |
{ |
|
196 |
$msg = trim($message['message'], "\x01"); |
|
197 |
list($ctcp) = explode(' ', $msg); |
|
198 |
$params = substr($msg, strlen($ctcp)+1); |
|
199 |
eval(eb_fetch_hook('event_ctcp')); |
|
200 |
} |
|
8 | 201 |
else if ( $message['action'] == 'PRIVMSG' ) |
0 | 202 |
{ |
8 | 203 |
eval(eb_fetch_hook('event_privmsg')); |
204 |
} |
|
205 |
else |
|
206 |
{ |
|
207 |
eval(eb_fetch_hook('event_other')); |
|
0 | 208 |
} |
209 |
} |
|
210 |
||
30 | 211 |
function enanobot_timeout_event($irc) |
212 |
{ |
|
213 |
// uh-oh. |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
214 |
$irc->close('client ping timeout (restarting connection)'); |
30 | 215 |
if ( defined('LIBIRC_DEBUG') ) |
216 |
{ |
|
217 |
$now = date('r'); |
|
218 |
echo "!!! [$now] Connection timed out; waiting 10 seconds and reconnecting\n"; |
|
219 |
} |
|
220 |
||
221 |
// re-init |
|
222 |
global $server, $nick, $user, $name, $pass, $channels, $libirc_channels; |
|
223 |
||
224 |
// wait until we can get into the server |
|
225 |
while ( true ) |
|
226 |
{ |
|
227 |
sleep(10); |
|
228 |
if ( defined('LIBIRC_DEBUG') ) |
|
229 |
{ |
|
230 |
$now = date('r'); |
|
231 |
echo "... [$now] Attempting reconnect\n"; |
|
232 |
} |
|
233 |
$conn = @fsockopen($server, 6667, $errno, $errstr, 5); |
|
234 |
if ( $conn ) |
|
235 |
{ |
|
236 |
if ( defined('LIBIRC_DEBUG') ) |
|
237 |
{ |
|
238 |
$now = date('r'); |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
239 |
echo "!!! [$now] Reconnection successful, ghosting old login (waiting 5 seconds to avoid throttling)\n"; |
30 | 240 |
} |
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
241 |
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 | 242 |
fclose($conn); |
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
243 |
sleep(5); |
30 | 244 |
break; |
245 |
} |
|
246 |
else |
|
247 |
{ |
|
248 |
if ( defined('LIBIRC_DEBUG') ) |
|
249 |
{ |
|
250 |
$now = date('r'); |
|
251 |
echo "!!! [$now] Still waiting for connection to come back up\n"; |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
$libirc_channels = array(); |
|
257 |
||
258 |
// we were able to get back in; ask NickServ to GHOST the old nick |
|
38 | 259 |
$irc->connect($nick, $user, $name, false); |
30 | 260 |
|
261 |
foreach ( $channels as $channel ) |
|
262 |
{ |
|
263 |
$libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event'); |
|
264 |
$channel_clean = preg_replace('/^[#&]/', '', $channel); |
|
265 |
$libirc_channels[$channel_clean] =& $libirc_channels[$channel]; |
|
266 |
$irc->privmsg('ChanServ', "OP $channel $nick"); |
|
267 |
} |
|
268 |
} |
|
269 |
||
8 | 270 |
if ( $_shutdown ) |
271 |
{ |
|
272 |
exit(2); |
|
273 |
} |