author | Dan |
Sat, 15 Nov 2008 15:02:59 -0500 | |
changeset 23 | df31a3872d19 |
parent 13 | f073c94a1477 |
child 30 | 2cfcd2801e5a |
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'); |
|
6 | 101 |
|
102 |
foreach ( $channels as $channel ) |
|
103 |
{ |
|
104 |
$libirc_channels[$channel] = $irc->join($channel, 'enanobot_channel_event'); |
|
105 |
$channel_clean = preg_replace('/^[#&]/', '', $channel); |
|
106 |
$libirc_channels[$channel_clean] =& $libirc_channels[$channel]; |
|
107 |
$irc->privmsg('ChanServ', "OP $channel $nick"); |
|
108 |
} |
|
0 | 109 |
|
110 |
$irc->event_loop(); |
|
111 |
$irc->close(); |
|
112 |
mysql_close($mysql_conn); |
|
113 |
||
6 | 114 |
function enanobot_channel_event($sockdata, $chan) |
0 | 115 |
{ |
116 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
117 |
$sockdata = trim($sockdata); |
|
118 |
$message = Request_IRC::parse_message($sockdata); |
|
6 | 119 |
$channelname = $chan->get_channel_name(); |
8 | 120 |
|
121 |
eval(eb_fetch_hook('event_raw_message')); |
|
122 |
||
0 | 123 |
switch ( $message['action'] ) |
124 |
{ |
|
125 |
case 'JOIN': |
|
8 | 126 |
eval(eb_fetch_hook('event_join')); |
127 |
break; |
|
128 |
case 'PART': |
|
129 |
eval(eb_fetch_hook('event_part')); |
|
0 | 130 |
break; |
131 |
case 'PRIVMSG': |
|
132 |
enanobot_process_channel_message($sockdata, $chan, $message); |
|
133 |
break; |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
function enanobot_process_channel_message($sockdata, $chan, $message) |
|
138 |
{ |
|
139 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
140 |
||
8 | 141 |
if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick ) |
0 | 142 |
{ |
9 | 143 |
$target_nick =& $message['nick']; |
13 | 144 |
// $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 | 145 |
} |
8 | 146 |
else |
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
147 |
{ |
8 | 148 |
eval(eb_fetch_hook('event_channel_msg')); |
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
149 |
} |
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 |
|
0 | 152 |
function enanobot_privmsg_event($message) |
153 |
{ |
|
6 | 154 |
global $privileged_list, $irc, $nick; |
0 | 155 |
static $part_cache = array(); |
156 |
if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' ) |
|
157 |
{ |
|
158 |
foreach ( $irc->channels as $channel ) |
|
159 |
{ |
|
160 |
$part_cache[] = array($channel->get_channel_name(), $channel->get_handler()); |
|
8 | 161 |
$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 | 162 |
$channel->part("Logging and presence suspended by {$message['nick']}", true); |
163 |
} |
|
164 |
} |
|
165 |
else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' ) |
|
166 |
{ |
|
167 |
global $nick; |
|
168 |
foreach ( $part_cache as $chan_data ) |
|
169 |
{ |
|
170 |
$chan_name = substr($chan_data[0], 1); |
|
171 |
$GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]); |
|
172 |
$GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true); |
|
173 |
$irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick"); |
|
174 |
} |
|
175 |
$part_cache = array(); |
|
176 |
} |
|
23
df31a3872d19
Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents:
13
diff
changeset
|
177 |
else if ( in_array($message['nick'], $privileged_list) && preg_match('/^Shutdown(?: (.+))$/i', $message['message'], $match) && $message['action'] == 'PRIVMSG' ) |
0 | 178 |
{ |
8 | 179 |
$GLOBALS['_shutdown'] = true; |
23
df31a3872d19
Made error handling for MySQL better; added ability to use custom shutdown messages
Dan
parents:
13
diff
changeset
|
180 |
$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
|
181 |
$irc->close($quitmessage, true); |
0 | 182 |
return 'BREAK'; |
183 |
} |
|
8 | 184 |
else if ( $message['action'] == 'PRIVMSG' ) |
0 | 185 |
{ |
8 | 186 |
eval(eb_fetch_hook('event_privmsg')); |
187 |
} |
|
188 |
else |
|
189 |
{ |
|
190 |
eval(eb_fetch_hook('event_other')); |
|
0 | 191 |
} |
192 |
} |
|
193 |
||
8 | 194 |
if ( $_shutdown ) |
195 |
{ |
|
196 |
exit(2); |
|
197 |
} |