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