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);
|
|
51 |
switch ( $message['action'] )
|
|
52 |
{
|
|
53 |
case 'JOIN':
|
|
54 |
// if a known op joins the channel, send mode +o
|
|
55 |
if ( in_array($message['nick'], $privileged_list) )
|
|
56 |
{
|
|
57 |
$chan->parent->put("MODE #enano +o {$message['nick']}\r\n");
|
|
58 |
}
|
|
59 |
break;
|
|
60 |
case 'PRIVMSG':
|
|
61 |
enanobot_process_channel_message($sockdata, $chan, $message);
|
|
62 |
break;
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
|
66 |
function enanobot_channel_event_enanodev($sockdata, $chan)
|
|
67 |
{
|
|
68 |
global $irc, $privileged_list;
|
|
69 |
$sockdata = trim($sockdata);
|
|
70 |
$message = Request_IRC::parse_message($sockdata);
|
|
71 |
switch ( $message['action'] )
|
|
72 |
{
|
|
73 |
case 'JOIN':
|
|
74 |
// if dandaman32 joins the channel, use mode +o
|
|
75 |
if ( in_array($message['nick'], $privileged_list) )
|
|
76 |
$chan->parent->put("MODE #enano-dev +o {$message['nick']}\r\n");
|
|
77 |
break;
|
|
78 |
case 'PRIVMSG':
|
|
79 |
enanobot_process_channel_message($sockdata, $chan, $message);
|
|
80 |
break;
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
function enanobot_process_channel_message($sockdata, $chan, $message)
|
|
85 |
{
|
|
86 |
global $irc, $nick, $mysql_conn, $privileged_list;
|
|
87 |
|
|
88 |
// Log the message
|
|
89 |
$chan_db = mysql_real_escape_string($chan->get_channel_name());
|
|
90 |
$nick_db = mysql_real_escape_string($message['nick']);
|
|
91 |
$line_db = mysql_real_escape_string($message['message']);
|
|
92 |
$day = date('Y-m-d');
|
|
93 |
$time = time();
|
|
94 |
$m_et = false;
|
|
95 |
while ( true )
|
|
96 |
{
|
|
97 |
$q = @mysql_query("INSERT INTO irclog(channel, day, nick, timestamp, line) VALUES
|
|
98 |
( '$chan_db', '$day', '$nick_db', '$time', '$line_db' );");
|
|
99 |
if ( !$q )
|
|
100 |
{
|
|
101 |
$m_e = mysql_error();
|
|
102 |
$m_et = true;
|
|
103 |
if ( $m_e == 'MySQL server has gone away' && !$m_et )
|
|
104 |
{
|
|
105 |
mysql_reconnect();
|
|
106 |
continue;
|
|
107 |
}
|
|
108 |
$irc->close("MySQL query error: $m_e");
|
|
109 |
exit(1);
|
|
110 |
}
|
|
111 |
break;
|
|
112 |
}
|
|
113 |
|
|
114 |
if ( preg_match('/^\!echo /', $message['message']) && in_array($message['nick'], $privileged_list) )
|
|
115 |
{
|
|
116 |
$chan->msg(preg_replace('/^\!echo /', '', $message['message']), true);
|
|
117 |
}
|
|
118 |
else if ( preg_match('/^\![\s]*([a-z0-9_-]+)([\s]*\|[\s]*([^ ]+))?$/', $message['message'], $match) )
|
|
119 |
{
|
|
120 |
$snippet =& $match[1];
|
|
121 |
if ( @$match[3] === 'me' )
|
|
122 |
$match[3] = $message['nick'];
|
|
123 |
$target_nick = ( !empty($match[3]) ) ? "{$match[3]}, " : "{$message['nick']}, ";
|
|
124 |
// Look for the snippet...
|
|
125 |
$m_et = false;
|
|
126 |
while ( true )
|
|
127 |
{
|
|
128 |
$q = mysql_query('SELECT snippet_text, snippet_channels FROM snippets WHERE snippet_code = \'' . mysql_real_escape_string($snippet) . '\';', $mysql_conn);
|
|
129 |
if ( !$q )
|
|
130 |
{
|
|
131 |
$m_e = mysql_error();
|
|
132 |
$m_et = true;
|
|
133 |
if ( $m_e == 'MySQL server has gone away' && !$m_et )
|
|
134 |
{
|
|
135 |
mysql_reconnect();
|
|
136 |
continue;
|
|
137 |
}
|
|
138 |
$irc->close("MySQL query error: $m_e");
|
|
139 |
exit(1);
|
|
140 |
}
|
|
141 |
break;
|
|
142 |
}
|
|
143 |
if ( mysql_num_rows($q) < 1 )
|
|
144 |
{
|
|
145 |
$chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
|
|
146 |
}
|
|
147 |
else
|
|
148 |
{
|
|
149 |
$row = mysql_fetch_assoc($q);
|
|
150 |
$channels = explode('|', $row['snippet_channels']);
|
|
151 |
if ( in_array($chan->get_channel_name(), $channels) )
|
|
152 |
{
|
|
153 |
$chan->msg("{$target_nick}{$row['snippet_text']}", true);
|
|
154 |
}
|
|
155 |
else
|
|
156 |
{
|
|
157 |
$chan->msg("{$message['nick']}, I couldn't find that snippet (\"$snippet\") in the database.", true);
|
|
158 |
}
|
|
159 |
}
|
|
160 |
}
|
|
161 |
else if ( strpos($message['message'], $nick) && !in_array($message['nick'], $privileged_list) && $message['nick'] != $nick )
|
|
162 |
{
|
|
163 |
$target_nick =& $message['nick'];
|
|
164 |
$chan->msg("{$target_nick}, I'm only a bot. :-) You should probably rely on the advice of humans if you need further assistance.", true);
|
|
165 |
}
|
|
166 |
}
|
|
167 |
|
|
168 |
function enanobot_privmsg_event($message)
|
|
169 |
{
|
|
170 |
global $privileged_list, $irc;
|
|
171 |
static $part_cache = array();
|
|
172 |
if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Suspend' && $message['action'] == 'PRIVMSG' )
|
|
173 |
{
|
|
174 |
foreach ( $irc->channels as $channel )
|
|
175 |
{
|
|
176 |
$part_cache[] = array($channel->get_channel_name(), $channel->get_handler());
|
|
177 |
$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);
|
|
178 |
$channel->part("Logging and presence suspended by {$message['nick']}", true);
|
|
179 |
}
|
|
180 |
}
|
|
181 |
else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Resume' && $message['action'] == 'PRIVMSG' )
|
|
182 |
{
|
|
183 |
global $nick;
|
|
184 |
foreach ( $part_cache as $chan_data )
|
|
185 |
{
|
|
186 |
$chan_name = substr($chan_data[0], 1);
|
|
187 |
$GLOBALS[$chan_name] = $irc->join($chan_data[0], $chan_data[1]);
|
|
188 |
$GLOBALS[$chan_name]->msg("Bot resumed by {$message['nick']}.", true);
|
|
189 |
$irc->privmsg('ChanServ', "OP {$chan_data[0]} $nick");
|
|
190 |
}
|
|
191 |
$part_cache = array();
|
|
192 |
}
|
|
193 |
else if ( in_array($message['nick'], $privileged_list) && $message['message'] == 'Shutdown' && $message['action'] == 'PRIVMSG' )
|
|
194 |
{
|
|
195 |
$irc->close("Remote bot shutdown ordered by {$message['nick']}", true);
|
|
196 |
return 'BREAK';
|
|
197 |
}
|
|
198 |
else if ( in_array($message['nick'], $privileged_list) && preg_match('/^\!echo-enano /', $message['message']) )
|
|
199 |
{
|
|
200 |
global $enano;
|
|
201 |
$enano->msg(preg_replace('/^\!echo-enano /', '', $message['message']), true);
|
|
202 |
}
|
|
203 |
}
|
|
204 |
|