-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyBot.php
320 lines (305 loc) · 9.54 KB
/
MyBot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
class MyBot extends PHPBot{
/**
* Returns the help document as a string
* @return string Help message for users on IRC
*/
private function get_help_doc(){
// $nick will replace references in help.php
$nick = $this->my_data->nick;
// start sending output to buffer
ob_start();
include('help.php');
// get text from buffer and close
$help_doc = ob_get_clean();
return $help_doc;
}
private function get_priv_msg_parts($parsed_response){
return explode(" ", trim($parsed_response->args[1]), 3);
}
private function check_is_cmd($parsed_response, $cmd){
if($parsed_response->args[0] != $this->my_data->nick){
return false;
}
$msg_parts = $this->get_priv_msg_parts($parsed_response);
if($msg_parts[0] != ":".$cmd){
return false;
}
return true;
}
private function check_is_cmd_2($msg_parts, $cmd){
if($msg_parts[0] != ":".$this->my_data->nick){
return false;
}
if($msg_parts[1] != $cmd){
return false;
}
return true;
}
private function _help($room_or_user){
$help_lines = explode(PHP_EOL, $this->get_help_doc());
foreach($help_lines as $help_line){
$this->_say($room_or_user, $help_line);
}
}
protected function help($parsed_response){
if(!$this->check_is_cmd($parsed_response, "HELP")){
return false;
}
$this->_help($parsed_response->from->nick);
return true;
}
protected function help_2($parsed_response){
$msg_parts = $this->get_priv_msg_parts($parsed_response);
if(!$this->check_is_cmd_2($msg_parts, "HELP")){
return false;
}
//TODO: ensure that this is a room
$room_or_user = $parsed_response->args[0];
$this->_help($room_or_user);
return true;
}
private function _irc_die($msg = ""){
$this->log_out($msg);
$this->disconnect();
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function irc_die($parsed_response){
if(!$this->check_is_cmd($parsed_response, "DIE")){
return false;
}
$msg_parts = $this->get_priv_msg_parts($parsed_response);
$this->_irc_die($msg_parts[1]);
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function irc_die_2($parsed_response){
$msg_parts = $this->get_priv_msg_parts($parsed_response);
if(!$this->check_is_cmd_2($msg_parts, "DIE")){
return false;
}
$this->_irc_die($msg_parts[2]);
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function leave($parsed_response){
if(!$this->check_is_cmd($parsed_response, "LEAVE")){
return false;
}
$msg_parts = $this->get_priv_msg_parts($parsed_response);
$this->log_out($msg_parts[1]);
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function leave_2($parsed_response){
$msg_parts = $this->get_priv_msg_parts($parsed_response);
if(!$this->check_is_cmd_2($msg_parts, "LEAVE")){
return false;
}
$this->log_out($msg_parts[2]);
return true;
}
private function _recon($msg){
$this->log_out($msg);
$this->connect();
$this->log_in();
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function recon($parsed_response){
if(!$this->check_is_cmd($parsed_response, "RECONNECT")){
return false;
}
$msg_parts = $this->get_priv_msg_parts($parsed_response);
$this->_recon($msg_parts[1]);
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function recon_2($parsed_response){
$msg_parts = $this->get_priv_msg_parts($parsed_response);
if(!$this->check_is_cmd_2($msg_parts, "RECONNECT")){
return false;
}
$this->_recon($msg_parts[2]);
return true;
}
private function _say($destination, $message){
$this->send_command("PRIVMSG", array($destination, $message));
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function say($parsed_response){
if(!$this->check_is_cmd($parsed_response, "SAY")){
return false;
}
$msg_parts = $this->get_priv_msg_parts($parsed_response);
$this->_say($msg_parts[1], $msg_parts[2]);
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function say_2($parsed_response){
$msg_parts = $this->get_priv_msg_parts($parsed_response);
if(!$this->check_is_cmd_2($msg_parts, "SAY")){
return false;
}
//TODO: ensure that this is a room
$room_or_user = $parsed_response->args[0];
$this->_say($room_or_user, $msg_parts[2]);
return true;
}
private function _wisdom(){
$quote_data = file_get_contents("http://www.swanandmokashi.com/Homepage/Webservices/QuoteOfTheDay.asmx/GetQuote");
if(!$quote_data){
$this->errors->set_errors("No quote data received.");
return "Invalid response recieved for quote request.";
}
$xml = simplexml_load_string($quote_data);
if(!$xml){
$this->errors->set_errors("", libxml_get_errors());
return "Could not load xml.";
}
try{
$quote = $xml->QuoteOfTheDay->__toString();
$author = $xml->Author->__toString();
}catch (Exception $e){
$this->errors->set_errors($e->getMessage(), libxml_get_errors());
return $e->getMessage();
}
$message = BOLD.COLOR.GREY."Quote of the day: ".COLOR.BOLD.UNDERLINE.$author.UNDERLINE." - \"".ITALICIZE.$quote.ITALICIZE."\"";
//sometimes, html formatting is specified. replace this with IRC control chars
$message = str_replace(array("<strong>", "</strong>", "<b>", "</b>"), BOLD, $message);
$message = str_replace(array("<em>", "</em>", "<i>", "</i>"), ITALICIZE, $message);
$message = str_replace(array("<u>", "</u>"), UNDERLINE, $message);
// strip remaining HTML tags
$message = strip_tags($message);
return $message;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function wisdom($parsed_response){
if(!$this->check_is_cmd($parsed_response, "WISDOM")){
return false;
}
$msg_parts = $this->get_priv_msg_parts($parsed_response);
$this->_say($msg_parts[1], $this->_wisdom());
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function wisdom_2($parsed_response){
$msg_parts = $this->get_priv_msg_parts($parsed_response);
if(!$this->check_is_cmd_2($msg_parts, "WISDOM")){
return false;
}
//TODO: ensure that this is a room
$room_or_user = $parsed_response->args[0];
$this->_say($room_or_user, $this->_wisdom());
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function ask_for_unban($parsed_response){
foreach($this->my_data->channels as $channel){
$this->_say($channel, "Please unban me from ".$parsed_response->args[1]."!");
}
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function join_chans($parsed_response){
$this->join_channels();
// set +B (bot) mode (For nick)
if(!$this->send_command("MODE", array($this->my_data->nick, "+B"))){
return false;
}
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function random_nick($parsed_response){
$rnd_nick = $this->my_data->nick.(string)rand();
$this->my_data->nick = $rnd_nick;
$this->send_command("NICK", array($rnd_nick));
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function change_nick($parsed_response){
if(!$this->check_is_cmd($parsed_response, "NICK")){
return false;
}
$msg_parts = $this->get_priv_msg_parts($parsed_response);
$this->send_command("NICK", array($msg_parts[1]));
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean
*/
protected function change_nick_2($parsed_response){
$msg_parts = $this->get_priv_msg_parts($parsed_response);
if(!$this->check_is_cmd_2($msg_parts, "NICK")){
return false;
}
$this->send_command("NICK", array($msg_parts[2]));
return true;
}
/**
*
* @param ParsedResponse $parsed_response
* @return boolean Success of PONG response
*/
protected function pong($parsed_response){
$this->send_command("PONG", $parsed_response->args);
return true;
}
}