author | Dan |
Sat, 15 Mar 2008 20:15:16 -0400 | |
changeset 1 | 739423b66116 |
parent 0 | d02690a8552c |
child 2 | c474904ed707 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
// define('LIBIRC_DEBUG', ''); |
|
3 |
require('libirc.php'); |
|
4 |
require('config.php'); |
|
5 |
||
6 |
@ini_set('display_errors', 'on'); |
|
7 |
||
8 |
$mysql_conn = false; |
|
9 |
||
10 |
function mysql_reconnect() |
|
11 |
{ |
|
12 |
global $mysql_conn, $mysql_host, $mysql_user, $mysql_pass; |
|
13 |
if ( $mysql_conn ) |
|
14 |
@mysql_close($mysql_conn); |
|
15 |
// connect to MySQL |
|
16 |
$mysql_conn = @mysql_connect($mysql_host, $mysql_user, $mysql_pass); |
|
17 |
if ( !$mysql_conn ) |
|
18 |
{ |
|
19 |
$m_e = mysql_error(); |
|
20 |
echo "Error connecting to MySQL: $m_e\n"; |
|
21 |
exit(1); |
|
22 |
} |
|
23 |
$q = @mysql_query('USE enanobot;', $mysql_conn); |
|
24 |
if ( !$q ) |
|
25 |
{ |
|
26 |
$m_e = mysql_error(); |
|
27 |
echo "Error selecting database: $m_e\n"; |
|
28 |
exit(1); |
|
29 |
} |
|
30 |
} |
|
31 |
||
32 |
mysql_reconnect(); |
|
33 |
||
34 |
$irc = new Request_IRC('irc.freenode.net'); |
|
35 |
$irc->connect($nick, $user, $name, $pass); |
|
36 |
$irc->set_privmsg_handler('enanobot_privmsg_event'); |
|
37 |
$enano = $irc->join('#enano', 'enanobot_channel_event_enano'); |
|
38 |
$enano_dev = $irc->join('#enano-dev', 'enanobot_channel_event_enanodev'); |
|
39 |
$irc->privmsg('ChanServ', 'OP #enano EnanoBot'); |
|
40 |
$irc->privmsg('ChanServ', 'OP #enano-dev EnanoBot'); |
|
41 |
||
42 |
$irc->event_loop(); |
|
43 |
$irc->close(); |
|
44 |
mysql_close($mysql_conn); |
|
45 |
||
46 |
function enanobot_channel_event_enano($sockdata, $chan) |
|
47 |
{ |
|
48 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
49 |
$sockdata = trim($sockdata); |
|
50 |
$message = Request_IRC::parse_message($sockdata); |
|
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
51 |
enanobot_log_message($chan, $message); |
0 | 52 |
switch ( $message['action'] ) |
53 |
{ |
|
54 |
case 'JOIN': |
|
55 |
// if a known op joins the channel, send mode +o |
|
56 |
if ( in_array($message['nick'], $privileged_list) ) |
|
57 |
{ |
|
58 |
$chan->parent->put("MODE #enano +o {$message['nick']}\r\n"); |
|
59 |
} |
|
60 |
break; |
|
61 |
case 'PRIVMSG': |
|
62 |
enanobot_process_channel_message($sockdata, $chan, $message); |
|
63 |
break; |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
function enanobot_channel_event_enanodev($sockdata, $chan) |
|
68 |
{ |
|
69 |
global $irc, $privileged_list; |
|
70 |
$sockdata = trim($sockdata); |
|
71 |
$message = Request_IRC::parse_message($sockdata); |
|
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
72 |
enanobot_log_message($chan, $message); |
0 | 73 |
switch ( $message['action'] ) |
74 |
{ |
|
75 |
case 'JOIN': |
|
76 |
// if dandaman32 joins the channel, use mode +o |
|
77 |
if ( in_array($message['nick'], $privileged_list) ) |
|
78 |
$chan->parent->put("MODE #enano-dev +o {$message['nick']}\r\n"); |
|
79 |
break; |
|
80 |
case 'PRIVMSG': |
|
81 |
enanobot_process_channel_message($sockdata, $chan, $message); |
|
82 |
break; |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
function enanobot_process_channel_message($sockdata, $chan, $message) |
|
87 |
{ |
|
88 |
global $irc, $nick, $mysql_conn, $privileged_list; |
|
89 |
||
90 |
if ( preg_match('/^\!echo /', $message['message']) && in_array($message['nick'], $privileged_list) ) |
|
91 |
{ |
|
92 |
$chan->msg(preg_replace('/^\!echo /', '', $message['message']), true); |
|
93 |
} |
|
94 |
else if ( preg_match('/^\![\s]*([a-z0-9_-]+)([\s]*\|[\s]*([^ ]+))?$/', $message['message'], $match) ) |
|
95 |
{ |
|
96 |
$snippet =& $match[1]; |
|
97 |
if ( @$match[3] === 'me' ) |
|
98 |
$match[3] = $message['nick']; |
|
99 |
$target_nick = ( !empty($match[3]) ) ? "{$match[3]}, " : "{$message['nick']}, "; |
|
100 |
// Look for the snippet... |
|
101 |
$m_et = false; |
|
102 |
while ( true ) |
|
103 |
{ |
|
104 |
$q = mysql_query('SELECT snippet_text, snippet_channels FROM snippets WHERE snippet_code = \'' . mysql_real_escape_string($snippet) . '\';', $mysql_conn); |
|
105 |
if ( !$q ) |
|
106 |
{ |
|
107 |
$m_e = mysql_error(); |
|
108 |
$m_et = true; |
|
109 |
if ( $m_e == 'MySQL server has gone away' && !$m_et ) |
|
110 |
{ |
|
111 |
mysql_reconnect(); |
|
112 |
continue; |
|
113 |
} |
|
114 |
$irc->close("MySQL query error: $m_e"); |
|
115 |
exit(1); |
|
116 |
} |
|
117 |
break; |
|
118 |
} |
|
119 |
if ( mysql_num_rows($q) < 1 ) |
|
120 |
{ |
|
121 |
$chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true); |
|
122 |
} |
|
123 |
else |
|
124 |
{ |
|
125 |
$row = mysql_fetch_assoc($q); |
|
126 |
$channels = explode('|', $row['snippet_channels']); |
|
127 |
if ( in_array($chan->get_channel_name(), $channels) ) |
|
128 |
{ |
|
129 |
$chan->msg("{$target_nick}{$row['snippet_text']}", true); |
|
130 |
} |
|
131 |
else |
|
132 |
{ |
|
133 |
$chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true); |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
else if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick ) |
|
138 |
{ |
|
139 |
$target_nick =& $message['nick']; |
|
140 |
$chan->msg("{$target_nick}, I'm only a bot. :-) You should probably rely on the advice of humans if you need further assistance.", true); |
|
141 |
} |
|
142 |
} |
|
143 |
||
1
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
144 |
function enanobot_log_message($chan, $message) |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
145 |
{ |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
146 |
global $nick; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
147 |
|
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
148 |
// Log the message |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
149 |
$chan_db = mysql_real_escape_string($chan->get_channel_name()); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
150 |
$nick_db = mysql_real_escape_string($message['nick']); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
151 |
$line_db = mysql_real_escape_string($message['message']); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
152 |
$day = date('Y-m-d'); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
153 |
$time = time(); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
154 |
$m_et = false; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
155 |
$sql = false; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
156 |
switch($message['action']) |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
157 |
{ |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
158 |
case 'PRIVMSG': |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
159 |
$sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
160 |
( '$chan_db', '$day', '$nick_db', '$time', '$line_db' );"; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
161 |
break; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
162 |
case 'JOIN': |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
163 |
$sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
164 |
( '$chan_db', '$day', '', '$time', '$nick_db has joined $chan_db' );"; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
165 |
break; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
166 |
case 'PART': |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
167 |
$sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
168 |
( '$chan_db', '$day', '', '$time', '$nick_db has left $chan_db' );"; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
169 |
break; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
170 |
case 'MODE': |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
171 |
list($mode, $target_nick) = explode(' ', $line_db); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
172 |
if ( $message['nick'] != 'ChanServ' && $target_nick != $nick ) |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
173 |
{ |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
174 |
$sql = "INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
175 |
( '$chan_db', '$day', '', '$time', '$nick_db set mode $mode on $target_nick' );"; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
176 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
177 |
break; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
178 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
179 |
while ( true && $sql ) |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
180 |
{ |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
181 |
$q = @mysql_query($sql); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
182 |
if ( !$q ) |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
183 |
{ |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
184 |
$m_e = mysql_error(); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
185 |
$m_et = true; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
186 |
if ( $m_e == 'MySQL server has gone away' && !$m_et ) |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
187 |
{ |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
188 |
mysql_reconnect(); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
189 |
continue; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
190 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
191 |
$irc->close("MySQL query error: $m_e"); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
192 |
exit(1); |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
193 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
194 |
break; |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
195 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
196 |
} |
739423b66116
Added support for logging mode messages and join/part messages
Dan
parents:
0
diff
changeset
|
197 |
|
0 | 198 |
function enanobot_privmsg_event($message) |
199 |
{ |
|
200 |
global $privileged_list, $irc; |
|
201 |
static $part_cache = array(); |
|
202 |
if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' ) |
|
203 |
{ |
|
204 |
foreach ( $irc->channels as $channel ) |
|
205 |
{ |
|
206 |
$part_cache[] = array($channel->get_channel_name(), $channel->get_handler()); |
|
207 |
$channel->msg("I've received a request to stop logging messages and responding to requests from {$message['nick']}. Don't forget to unsuspend me with /msg EnanoBot Resume when finished.", true); |
|
208 |
$channel->part("Logging and presence suspended by {$message['nick']}", true); |
|
209 |
} |
|
210 |
} |
|
211 |
else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' ) |
|
212 |
{ |
|
213 |
global $nick; |
|
214 |
foreach ( $part_cache as $chan_data ) |
|
215 |
{ |
|
216 |
$chan_name = substr($chan_data[0], 1); |
|
217 |
$GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]); |
|
218 |
$GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true); |
|
219 |
$irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick"); |
|
220 |
} |
|
221 |
$part_cache = array(); |
|
222 |
} |
|
223 |
else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' ) |
|
224 |
{ |
|
225 |
$irc->close("Remote bot shutdown ordered by {$message['nick']}", true); |
|
226 |
return 'BREAK'; |
|
227 |
} |
|
228 |
else if ( in_array($message['nick'], $privileged_list) && preg_match('/^\!echo-enano /', $message['message']) ) |
|
229 |
{ |
|
230 |
global $enano; |
|
231 |
$enano->msg(preg_replace('/^\!echo-enano /', '', $message['message']), true); |
|
232 |
} |
|
233 |
} |
|
234 |