author | Dan |
Sat, 15 Nov 2008 15:02:29 -0500 | |
changeset 22 | 04c2f743c6ec |
parent 13 | f073c94a1477 |
child 23 | df31a3872d19 |
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'); |
77 |
||
78 |
@ini_set('display_errors', 'on'); |
|
8 | 79 |
error_reporting(E_ALL); |
80 |
||
81 |
// load modules |
|
82 |
foreach ( $modules as $module ) |
|
83 |
{ |
|
84 |
$modulefile = "modules/$module.php"; |
|
85 |
if ( file_exists($modulefile) ) |
|
86 |
{ |
|
87 |
require($modulefile); |
|
88 |
} |
|
89 |
} |
|
0 | 90 |
|
91 |
$mysql_conn = false; |
|
8 | 92 |
$doctor = array(); |
0 | 93 |
|
94 |
function mysql_reconnect() |
|
95 |
{ |
|
2
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
96 |
global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass, $mysql_dbname; |
0 | 97 |
if ( $mysql_conn ) |
4
8f62a406d9d9
Added debug message for MySQL reconnection; it's tested and confirmed working now
Dan
parents:
3
diff
changeset
|
98 |
{ |
0 | 99 |
@mysql_close($mysql_conn); |
4
8f62a406d9d9
Added debug message for MySQL reconnection; it's tested and confirmed working now
Dan
parents:
3
diff
changeset
|
100 |
if ( defined('LIBIRC_DEBUG') ) |
8f62a406d9d9
Added debug message for MySQL reconnection; it's tested and confirmed working now
Dan
parents:
3
diff
changeset
|
101 |
{ |
8f62a406d9d9
Added debug message for MySQL reconnection; it's tested and confirmed working now
Dan
parents:
3
diff
changeset
|
102 |
echo "< > Reconnecting to MySQL\n"; |
8f62a406d9d9
Added debug message for MySQL reconnection; it's tested and confirmed working now
Dan
parents:
3
diff
changeset
|
103 |
} |
8f62a406d9d9
Added debug message for MySQL reconnection; it's tested and confirmed working now
Dan
parents:
3
diff
changeset
|
104 |
} |
0 | 105 |
// connect to MySQL |
106 |
$mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass); |
|
107 |
if ( !$mysql_conn ) |
|
108 |
{ |
|
109 |
$m_e = mysql_error(); |
|
110 |
echo "Error connecting to MySQL: $m_e\n"; |
|
111 |
exit(1); |
|
112 |
} |
|
2
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
113 |
$q = @mysql_query("USE `$mysql_dbname`;", $mysql_conn); |
0 | 114 |
if ( !$q ) |
115 |
{ |
|
116 |
$m_e = mysql_error(); |
|
117 |
echo "Error selecting database: $m_e\n"; |
|
118 |
exit(1); |
|
119 |
} |
|
120 |
} |
|
121 |
||
2
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
122 |
function eb_mysql_query($sql, $conn = false) |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
123 |
{ |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
124 |
global $mysql_conn, $irc; |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
125 |
$m_et = false; |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
126 |
while ( true ) |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
127 |
{ |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
128 |
$q = mysql_query($sql, $mysql_conn); |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
129 |
if ( !$q ) |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
130 |
{ |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
131 |
$m_e = mysql_error(); |
3 | 132 |
if ( strpos($m_e, 'gone away') && !$m_et ) |
2
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
133 |
{ |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
134 |
mysql_reconnect(); |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
135 |
continue; |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
136 |
} |
3 | 137 |
$m_et = true; |
2
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
138 |
$irc->close("MySQL query error: $m_e"); |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
139 |
exit(1); |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
140 |
} |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
141 |
break; |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
142 |
} |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
143 |
return $q; |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
144 |
} |
c474904ed707
Moved db_name to config.php; added !snippets virtual snippet that lists all snippet codes in database; abstracted database query and error checking code
Dan
parents:
1
diff
changeset
|
145 |
|
0 | 146 |
mysql_reconnect(); |
147 |
||
6 | 148 |
$libirc_channels = array(); |
149 |
||
8 | 150 |
$irc = new Request_IRC($server); |
0 | 151 |
$irc->connect($nick, $user, $name, $pass); |
152 |
$irc->set_privmsg_handler('enanobot_privmsg_event'); |
|
6 | 153 |
|
154 |
foreach ( $channels as $channel ) |
|
155 |
{ |
|
156 |
$libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event'); |
|
157 |
$channel_clean = preg_replace('/^[#&]/', '', $channel); |
|
158 |
$libirc_channels[$channel_clean] =& $libirc_channels[$channel]; |
|
159 |
$irc->privmsg('ChanServ', "OP $channel $nick"); |
|
160 |
} |
|
0 | 161 |
|
162 |
$irc->event_loop(); |
|
163 |
$irc->close(); |
|
164 |
mysql_close($mysql_conn); |
|
165 |
||
6 | 166 |
function enanobot_channel_event($sockdata, $chan) |
0 | 167 |
{ |
168 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
169 |
$sockdata = trim($sockdata); |
|
170 |
$message = Request_IRC::parse_message($sockdata); |
|
6 | 171 |
$channelname = $chan->get_channel_name(); |
8 | 172 |
|
173 |
eval(eb_fetch_hook('event_raw_message')); |
|
174 |
||
0 | 175 |
switch ( $message['action'] ) |
176 |
{ |
|
177 |
case 'JOIN': |
|
8 | 178 |
eval(eb_fetch_hook('event_join')); |
179 |
break; |
|
180 |
case 'PART': |
|
181 |
eval(eb_fetch_hook('event_part')); |
|
0 | 182 |
break; |
183 |
case 'PRIVMSG': |
|
184 |
enanobot_process_channel_message($sockdata, $chan, $message); |
|
185 |
break; |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
function enanobot_process_channel_message($sockdata, $chan, $message) |
|
190 |
{ |
|
191 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
192 |
||
8 | 193 |
if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick ) |
0 | 194 |
{ |
9 | 195 |
$target_nick =& $message['nick']; |
13 | 196 |
// $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 | 197 |
} |
8 | 198 |
else |
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
199 |
{ |
8 | 200 |
eval(eb_fetch_hook('event_channel_msg')); |
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
201 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
202 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
203 |
|
0 | 204 |
function enanobot_privmsg_event($message) |
205 |
{ |
|
6 | 206 |
global $privileged_list, $irc, $nick; |
0 | 207 |
static $part_cache = array(); |
208 |
if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' ) |
|
209 |
{ |
|
210 |
foreach ( $irc->channels as $channel ) |
|
211 |
{ |
|
212 |
$part_cache[] = array($channel->get_channel_name(), $channel->get_handler()); |
|
8 | 213 |
$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 | 214 |
$channel->part("Logging and presence suspended by {$message['nick']}", true); |
215 |
} |
|
216 |
} |
|
217 |
else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' ) |
|
218 |
{ |
|
219 |
global $nick; |
|
220 |
foreach ( $part_cache as $chan_data ) |
|
221 |
{ |
|
222 |
$chan_name = substr($chan_data[0], 1); |
|
223 |
$GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]); |
|
224 |
$GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true); |
|
225 |
$irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick"); |
|
226 |
} |
|
227 |
$part_cache = array(); |
|
228 |
} |
|
229 |
else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' ) |
|
230 |
{ |
|
8 | 231 |
$GLOBALS['_shutdown'] = true; |
0 | 232 |
$irc->close("Remote bot shutdown ordered by {$message['nick']}", true); |
233 |
return 'BREAK'; |
|
234 |
} |
|
8 | 235 |
else if ( $message['action'] == 'PRIVMSG' ) |
0 | 236 |
{ |
8 | 237 |
eval(eb_fetch_hook('event_privmsg')); |
238 |
} |
|
239 |
else |
|
240 |
{ |
|
241 |
eval(eb_fetch_hook('event_other')); |
|
0 | 242 |
} |
243 |
} |
|
244 |
||
8 | 245 |
if ( $_shutdown ) |
246 |
{ |
|
247 |
exit(2); |
|
248 |
} |