-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.php
383 lines (344 loc) · 11 KB
/
bot.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
// Standalone modules
include("mod_httpstatus.php");
include("mod_imdb.php");
include("mod_google.php");
include("mod_karma.php");
include("mod_tld.php");
include("mod_info.php");
include("mod_quote.php");
include("mod_urbandictionary.php");
include("mod_ripe.php");
include("mod_coin.php");
include("mod_rate.php");
include("mod_meeteta.php");
include("mod_manpage.php");
// Modules that need an API key
include("mod_discourse.php");
include("mod_weather.php");
include("mod_wolfram.php");
include("mod_youtube.php");
$controlchar = "!";
$args = "";
$action = "";
if(isset($_GET['data'])) {
// Raw data is coming in
preg_match("/^:([^\ ]+)\ ([^\ ]+)\ ([^\ ]+)\ :(.*)$/",$_GET['data'],$data);
$user = $data[1];
$command = $data[2];
$channel = $data[3];
$message = $data[4];
if(substr($message,0,1)===$controlchar) {
// Handle command like !command args
$firstspace = strpos($message," ");
if(!$firstspace) {
$action = strtolower(trim(substr($message,1)));
} else {
$action = strtolower(trim(substr($message,1,$firstspace)));
$args = trim(substr($message,strlen($action)+1));
}
} else {
// If no command was given, check if there are any HTTP URI's we can grab titles from
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $message, $match);
if(isset($match[0][0])) {
$action = "gettitle";
$args = $match[0][0];
}
// Random chat triggers
$message = trim($message);
if(($message=="ok"||$message=="oke") && rand(1,5)==4) output("is goed");
if(($message=="zeg dat wel")) output("dat wel");
if(($message=="zeg het maar")) output("het maar");
}
} else {
// No raw data but specific command coming in, such as scheduled job
if(isset($_GET['action'])) {
$action = strtolower($_GET['action']);
}
}
// Weird chars being added by python script?
$args = str_replace("%0D%0A","",$args);
// Remove any extra whitespace around the incoming data before parsing
$args = trim(preg_replace('/\s\s+/', ' ', $args));
switch($action)
{
case 'version':
output("HKJL Bot - Laatste Versie || Eigenaar: Sling || Help: !help");
break;
case 'base64encode':
output(base64_encode($args));
break;
case 'base64decode':
output(base64_decode($args));;
break;
case 'imdb':
output(imdb($args));
break;
case 'discourse':
output(discourse_search($args));
break;
case 'google':
output(google($args));
break;
case 'lmgtfy':
output("http://lmgtfy.com/?q=".urlencode($args));
break;
case 'help':
output("Handleiding van deze bot: https://community.hackenkunjeleren.nl/t/handleiding-hkjl-irc-bot/199");
break;
case 'wolfram':
output(wolfram($args));
break;
case 'random':
output('4'); // chosen by fair dice roll. guaranteed to be random.
break;
case 'yt':
case 'youtube':
output(youtube($args));
break;
case 'md5':
output(md5($args));
break;
case 'echo':
output($args);
break;
case 'bin2hex':
output(dechex(bindec($args)));
break;
case 'reverse':
output(strrev($args));
break;
case 'rot13':
output(str_rot13($args));
break;
case 'ascii2bin':
for($i = 0; $i != strlen($args); $i++) {
$value = unpack('H*', $args[$i]);
$output .= base_convert($value[1], 16, 2).' ';
}
output($output);
break;
case 'ascii2oct':
for($i = 0; $i != strlen($args); $i++) {
$value = unpack('H*', $args[$i]);
$output .= base_convert($value[1], 16, 8).' ';
}
output($output);
break;
case 'ascii2dec':
for($i = 0; $i != strlen($args); $i++) {
$output .= ord($args[$i]).' ';
}
output($output);
break;
case 'ascii2hex':
for($i = 0; $i != strlen($args); $i++) {
$output .= dechex(ord($args[$i])).' ';
}
output($output);
break;
case 'levenshtein':
$args = explode(' ',$args,2);
output(levenshtein($args[0],$args[1]));
break;
case 'dice':
case 'dobbelsteen':
output("The dice rolls.... ".rand(1,6));
break;
case 'w':
case 'weer':
output(weather($args));
break;
case 'httpbanner':
output(http_banner($args));
break;
case 'httpstatus':
output(httpstatus($args));
break;
case 'minutecron':
output(discourse_latest());
break;
case 'gettitle':
output(get_title($args));
break;
case 'tld':
output(tld($args));
break;
case 'karma':
case 'karma?':
output(karmalookup($args));
break;
case 'karma+':
output(karmaup($args));
break;
case 'karma-':
output(karmadown($args));
break;
case 'def':
case 'info':
case 'info?':
output(getinfo($args));
break;
case 'deflist':
case 'infolist':
output(getkeys());
break;
case 'def-':
case 'info-':
output(delinfo($args));
break;
case 'def+':
case 'info+':
output(setinfo($args));
break;
case 'kies':
output(choose($args));
break;
case 'quote+':
output(addquote($args));
break;
case 'quote-':
output(delquote($args));
break;
case 'quote#':
output(getquote($args));
break;
case 'quote':
output(quote($args));
break;
case 'shrug':
output("¯\_(ツ)_/¯");
case 'leet':
output(convert_leet($args));
break;
case 'urban':
case 'ud':
output(urban($args));
break;
case 'ripe':
case 'whois':
case 'lookup':
case 'ip':
output(ripe($args));
break;
case 'wind':
case 'windkracht':
output(windkracht($args));
break;
case 'price':
case 'coin':
output(coin($args));
break;
case 'rate':
output(modRate::rate($args));
break;
case 'meeteta':
output(meet_eta());
break;
case 'man':
output(manpage($args));
break;
default:
// Silently ignore invalid actions to prevent unwanted spamming
// Handle shorthand karma commands separately, since they are normally parsed as action and args:
if(endsWith($args,"--")) { // Example: !two words--
output(karmadown(substr($action." ".$args,0,-2)));
} else if(endsWith($args,"++")) { // Example: !two words++
output(karmaup(substr($action." ".$args,0,-2)));
} else if(endsWith($action,"--")) { // Example: !word--
output(karmadown(substr($action,0,-2)));
} else if(endsWith($action,"++")) { // Example: !word++
output(karmaup(substr($action,0,-2)));
}
break;
}
function get_title($args, $prefix = 1) {
// If user did not supply a HTTP or HTTPS scheme, add a HTTP scheme.
if(strpos($args,"http://")!==0 && strpos($args,"https://")!==0) {
$args = "http://".$args;
}
// Take the first 'word' from the supplied input
$url = explode(" ", $args)[0];
// Remove chars that are not supposed to be in a URL
$url = filter_var($url, FILTER_SANITIZE_URL);
// Check if what remains is a valid URL
if(filter_var($url, FILTER_VALIDATE_URL)) {
// Fetch the contents of the URL, and limit http body length to mitigate some possible abuse
$content = file_get_contents($url,false,NULL,0,65536);
// Replace any additional whitespace and newlines so we can use regex and not run into multiline texts
$content = trim(preg_replace('/\s+/', ' ', $content));
// Use a regex to parse for the title. We're not using DOM here since our input could be truncated malformed HTML
if(preg_match("/\<title\>(.*?(?=\<\/title\>))\<\/title\>/i",$content,$title)) {
// Convert stuff like to proper chars
$input = html_entity_decode($title[1]);
// Convert stuff like ' to proper chars
$output = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $input);
if($prefix) {
if(preg_match("/(youtube\.com\/watch\?v=[0-9A-Za-z_-]{11})/",$url, $yturl)) addYoutubeURL($yturl[1]);
return "[Title] ".$output;
} else {
return $output;
}
} else {
return "";
}
} else {
return "";
}
}
function http_banner($args) {
// If user did not supply a HTTP or HTTPS scheme, add a HTTP scheme.
if(strpos($args,"http://")!==0 && strpos($args,"https://")!==0) {
$args = "http://".$args;
}
// We're going to make a HEAD request, instead of the default GET, since we're not interested in a response body.
stream_context_set_default(array('http'=>array('method'=>'HEAD')));
// If the user gave us a valid URL, retrieve the HTTP headers.
if(filter_var($args, FILTER_VALIDATE_URL)) {
$headers = get_headers($args);
if(!$headers) {
return "This host looks down to me...";
} else {
return array_values(array_filter($headers, function($v) { return strpos($v, 'Server:') !== false; }))[0];
}
} else {
return "That doesn't look like a valid URL...";
}
}
function choose($args) {
$choices = explode(',', $args);
if(count($choices)==1) {
return "[kies] Ben je kort ofzo? Geef me meerdere opties, gescheiden door komma's.";
} else {
return trim($choices[array_rand($choices)]);
}
}
function output($output) {
// Remove any spaces around output and remove non-printable characters such as LF, CR, SOH, etc.
$output = trim($output);
$output = preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', $output);
// Limit output length
if(strlen($output) > 450) {
$output = substr($output,0,445)."(...)";
}
echo htmlspecialchars($output);
}
function endsWith($haystack, $needle) {
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}
function convert_leet($args) {
// Converts string to l33t speak
$normal = array("a", "e", "s", "S", "A", "o", "O", "t", "l", "H", "W", "M", "V", "x");
$leet = array("4", "3", "z", "Z", "4", "0", "0", "7", "1", "|-|", "\\/\\/", "|\\/|", "\\/", "><");
$result = '';
for ($i = 0; $i < strlen($args); $i++)
{
$char = $args[$i];
if (false !== ($pos = array_search($char, $normal)))
{
$char = $leet[$pos]; // Change the char to l33t.
}
$result .= $char;
}
return $result;
}