-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcussbot.php
More file actions
executable file
·500 lines (476 loc) · 19.8 KB
/
Copy pathcussbot.php
File metadata and controls
executable file
·500 lines (476 loc) · 19.8 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#!/usr/bin/php
<?php
/* cussbot.php - An IRC bot to track how much users cuss.
Copyright (C) 2013 Michael Marley <michael@michaelmarley.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
$ircserver="localhost";
$ircport=6667;
$ircpassword="youwish";
$sqlserver="localhost";
$sqlusername="you";
$sqlpassword="wish";
$sqldb="cussbot";
$botnick="CussBot";
$botname="Cuss Statistics Bot";
$searches=array(
"mostcommonword" =>array('lookup'=>"word" ,'order'=>"DESC",'what'=>"most common cuss word" ,'item'=>"use" ,'itemplural'=>"s"),
"leastcommonword" =>array('lookup'=>"word" ,'order'=>"ASC" ,'what'=>"least common cuss word" ,'item'=>"use" ,'itemplural'=>"s"),
"mostprolific" =>array('lookup'=>"nick" ,'order'=>"DESC",'what'=>"most prolific cusser" ,'item'=>"cuss",'itemplural'=>"es"),
"leastprolific" =>array('lookup'=>"nick" ,'order'=>"ASC" ,'what'=>"least prolific cusser" ,'item'=>"cuss",'itemplural'=>"es"),
"mostcommoncategory" =>array('lookup'=>"fullname",'order'=>"DESC",'what'=>"most common cuss category" ,'item'=>"use" ,'itemplural'=>"s"),
"leastcommoncategory"=>array('lookup'=>"fullname",'order'=>"ASC" ,'what'=>"least common cuss category",'item'=>"use" ,'itemplural'=>"s")
);
sqlconnect();
ircconnect();
$cusswords=array();
$sqlresult=sqlquery("SELECT word FROM words");
for($i=0;$i<sqlnumrows($sqlresult);$i++){
array_push($cusswords,sqlresult($sqlresult,$i,"word"));
}
while(true){
if(trim($line=ircreadline())==""){
checkircconnection();
continue;
}
$linearray=explode(" :",$line,2);
if(trim($linearray[0])=="PING"){
irccommand("PONG :$linearray[1]");
checkmysqlconnection();
continue;
}
$prefixarray=explode(" ",trim($linearray[0]));
$senderarray=explode("!",$prefixarray[0]);
if(count($senderarray)==1){
continue;
}
$sender=substr($senderarray[0],1);
if(count($prefixarray)>=3){
$target=$prefixarray[2];
}else{
continue;
}
if($target==$botnick){
$target=$sender;
}
if($prefixarray[1]=="PRIVMSG"){
if(count($linearray)==2){
$message=trim($linearray[1]);
$sender=trim(strtolower(preg_replace('/[^a-zA-Z0-9\s]/','',$sender)));
if(startsWith(trim(strtolower($message)),strtolower($botnick) . ":")){
$command=str_ireplace("$botnick:","",$message);
}else if(startsWith(trim(strtolower($message)),strtolower($botnick) . ",")){
$command=str_ireplace("$botnick,","",$message);
}else if($target==$sender){
$command=$message;
}else{
$command=null;
}
if(isset($command)){
$commandarray=explode(" ",trim($command));
if($commandarray[0]=="help"){
if(count($commandarray)==1){
ircprivmessage("Valid commands are \"help\", \"search\" (see \"search help\" for arguments), \"stats\", \"stats <nick>\", \"stats <#channel>\", \"webstats\", \"listadmins\", \"addmin <nick>\", \"removemin <nick>\", \"listchannels\", \"join <#channel>\", \"part <#channel>\", \"addword <category> <word>\", \"removeword <word>\", \"listcategories\", \"addcategory <shortname> <fullname>\", \"removecategory <name>\", \"refreshwords\", \"source\".");
}else{
ircprivmessage("Invalid command! Syntax is \"help\"");
}
}else if($commandarray[0]=="source"){
if(count($commandarray)==1){
ircprivmessage("https://github.com/mamarley/CussBot");
}else{
ircprivmessage("Invalid command! Syntax is \"source\"");
}
}else if($commandarray[0]=="search"){
if(count($commandarray)>=2){
$conditions=array();
$conditionstring="";
for($i=2;$i<count($commandarray);$i++){
if($commandarray[$i]=="--nick"){
if(isset($commandarray[$i+1])){
array_push($conditions,"nick=\"" . sqlescape(trim(strtolower(preg_replace('/[^a-zA-Z0-9\s]/','',$commandarray[$i+1])))) . "\" ");
$i++;
}else{
ircprivmessage("Invalid value for argument \"--nick\", ignoring!");
}
}else if($commandarray[$i]=="--channel"){
if(isset($commandarray[$i+1])){
array_push($conditions,"name=\"" . sqlescape($commandarray[$i+1]) . "\" ");
$i++;
}else{
ircprivmessage("Invalid value for argument \"--channel\", ignoring!");
}
}else if($commandarray[$i]=="--word"){
if(isset($commandarray[$i+1])){
array_push($conditions,"word=\"" . sqlescape($commandarray[$i+1]) . "\" ");
$i++;
}else{
ircprivmessage("Invalid value for argument \"--word\", ignoring!");
}
}else if($commandarray[$i]=="--category"){
if(isset($commandarray[$i+1])){
array_push($conditions,"shortname=\"" . sqlescape(trim(strtolower(preg_replace('/[^a-zA-Z0-9\s]/','',$commandarray[$i+1])))) . "\" ");
$i++;
}else{
ircprivmessage("Invalid value for argument \"--category\", ignoring!");
}
}else{
ircprivmessage("Invalid argument $commandarray[$i], ignoring!");
}
}
for($i=0;$i<count($conditions);$i++){
$conditionstring.=$conditions[$i];
if($i<(count($conditions)-1)){
$conditionstring.="AND ";
}
}
if($commandarray[1]=="help"){
$valid_searches = array_keys($searches);
foreach($valid_searches as &$this_search) {
$this_search = "\"$this_search\"";
}
if (sizeof($valid_searches) > 1) {
$this_search = "and $this_search"; // referece to last element leaked by var_dump
}
undef($last_search);
ircprivmessage("Valid searches are \"" . implode(", ", $valid_searches) . "\".");
ircprivmessage("Valid arguments are \"--nick <nick>\", \"--channel <#channel>\", \"--word <word>\", and \"--category <category>\".");
continue;
}else if(isset($searches[$commandarray[1]])){
extract($searches[$commandarray[1]]);
$sqlresult=sqlquery("SELECT $lookup, COUNT($lookup) AS count FROM uses JOIN words ON uses.wordid=words.id JOIN categories ON words.categoryid=categories.id JOIN channels ON uses.channelid=channels.id" . ($conditionstring!="" ? " WHERE $conditionstring" : "") . " GROUP BY $lookup ORDER BY count $order LIMIT 1");
if(sqlnumrows($sqlresult)>0){
ircprivmessage("The $what was \"" . sqlresult($sqlresult,0,$lookup) . "\" with " . sqlresult($sqlresult,0,"count") . " $item" . ((int)sqlresult($sqlresult,0,"count")!=1 ? $itemplural:"") . ".");
}else{
ircprivmessage("Your query returns no results. Please check any conditions and try again.");
}
}else{
ircprivmessage("Invalid search! Try \"search help\" for a list of valid searches.");
continue;
}
}else{
ircprivmessage("Invalid search! Try \"search help\" for a list of valid searches.");
}
}else if($commandarray[0]=="stats"){
if((count($commandarray)==2)&&(substr($commandarray[1],0,1)=="#")){
$condition="WHERE name=\"" . sqlescape($commandarray[1]) . "\"";
}else if(count($commandarray)==2){
$condition="WHERE nick=\"" . sqlescape(trim(strtolower(preg_replace('/[^a-zA-Z0-9\s]/','',$commandarray[1])))) . "\"";
}else if(count($commandarray)>2){
ircprivmessage("Invalid command! Syntax is \"stats\" or \"stats <nick>\" or \"stats <#channel>\"");
continue;
}else{
$condition="";
}
$sqlresult=sqlquery("SELECT fullname, COUNT(fullname) AS count FROM uses JOIN words ON uses.wordid=words.id JOIN categories ON words.categoryid=categories.id JOIN channels ON uses.channelid=channels.id $condition GROUP BY fullname");
if(sqlnumrows($sqlresult)>0){
$response="";
for($i=0;$i<sqlnumrows($sqlresult);$i++){
$response.=sqlresult($sqlresult,$i,"count") . " " . sqlresult($sqlresult,$i,"fullname") . ((int)sqlresult($sqlresult,$i,"count")!=1 ? "s":"");
if($i<(sqlnumrows($sqlresult)-1)){
$response.=", ";
}
}
ircprivmessage($response);
}else{
ircprivmessage("Your query returns no results. Please check any conditions and try again.");
}
}else if($commandarray[0]=="webstats"){
if(count($commandarray)==1){
ircprivmessage("http://michaelmarley.com/cussbotstatistics/index.php");
}else{
ircprivmessage("Invalid command! Syntax is \"webstats\"");
}
}else if($commandarray[0]=="listadmins"){
if(count($commandarray)==1){
$response="Admins are: ";
$sqlresult=sqlquery("SELECT nick FROM admins");
for($i=0;$i<sqlnumrows($sqlresult);$i++){
$response.=sqlresult($sqlresult,$i,"nick") . " ";
}
ircprivmessage($response);
}else{
ircprivmessage("Invalid command! Syntax is \"listadmins\"");
}
}else if($commandarray[0]=="addmin"){
if(count($commandarray)==2){
if(senderisadmin($sender)){
$sqlresult=sqlquery("SELECT nick FROM admins WHERE nick=\"" . sqlescape($commandarray[1]) . "\"");
if(sqlnumrows($sqlresult)==0){
sqlquery("INSERT INTO admins (nick) VALUES ('" . sqlescape($commandarray[1]) . "')");
ircprivmessage("User \"$commandarray[1]\" is now an admin.");
}else{
ircprivmessage("User \"$commandarray[1]\" is already an admin.");
}
}
}else{
ircprivmessage("Invalid command! Syntax is \"addmin <nick>\"");
}
}else if($commandarray[0]=="removemin"){
if(count($commandarray)==2){
if(senderisadmin($sender)){
$sqlresult=sqlquery("SELECT nick, immutable FROM admins WHERE nick=\"" . sqlescape($commandarray[1]) . "\"");
if(sqlnumrows($sqlresult)>0){
$immutable=sqlresult($sqlresult,0,"immutable");
$sqlresult=sqlquery("SELECT nick FROM `admins`");
if(sqlnumrows($sqlresult)==1){
ircprivmessage("You are the last admin remaining. You cannot remove your privileges.");
}else if($immutable){
ircprivmessage("$commandarray[1]'s admin privileges cannot be removed!");
}else{
sqlquery("DELETE FROM admins WHERE nick='" . sqlescape($commandarray[1]) . "'");
ircprivmessage("User \"$commandarray[1]\" is no longer an admin.");
}
}else{
ircprivmessage("User \"$commandarray[1]\" is not an admin.");
}
}
}else{
ircprivmessage("Invalid command! Syntax is \"removemin <nick>\"");
}
}else if($commandarray[0]=="listchannels"){
if(count($commandarray)==1){
$response="I am currently a member of: ";
$sqlresult=sqlquery("SELECT name FROM channels");
for($i=0;$i<sqlnumrows($sqlresult);$i++){
$response.=sqlresult($sqlresult,$i,"name") . " ";
}
ircprivmessage($response);
}else{
ircprivmessage("Invalid command! Syntax is \"listchannels\"");
}
}else if($commandarray[0]=="join"){
if(count($commandarray)==2){
if(senderisadmin($sender)){
if(substr($commandarray[1],0,1)=="#"){
$sqlresult=sqlquery("SELECT name FROM channels WHERE name=\"" . sqlescape($commandarray[1]) . "\"");
if(sqlnumrows($sqlresult)==0){
sqlquery("INSERT INTO channels (name) VALUES ('" . sqlescape($commandarray[1]) . "')");
irccommand("JOIN " . $commandarray[1]);
ircprivmessage("I will now join $commandarray[1].");
}else{
ircprivmessage("I have already joined $commandarray[1].");
}
}else{
ircprivmessage("Invalid channel name!");
}
}
}else{
ircprivmessage("Invalid command! Syntax is \"join <#channel>\"");
}
}else if($commandarray[0]=="part"){
if(count($commandarray)==2){
if(senderisadmin($sender)){
$sqlresult=sqlquery("SELECT name FROM channels WHERE name=\"" . sqlescape($commandarray[1]) . "\"");
if(sqlnumrows($sqlresult)>0){
$sqlresult=sqlquery("SELECT name FROM channels");
if(sqlnumrows($sqlresult)==1){
ircprivmessage("$commandarray[1] is the last remaining channel that I am a member of. I cannot part.");
}else{
sqlquery("DELETE FROM channels WHERE name='" . sqlescape($commandarray[1]) . "'");
ircprivmessage("I will now part $commandarray[1].");
irccommand("PART " . $commandarray[1]);
}
}else{
ircprivmessage("I am not a member of $commandarray[1].");
}
}
}else{
ircprivmessage("Invalid command! Syntax is \"part <#channel>\"");
}
}else if($commandarray[0]=="addword"){
if(count($commandarray)==3){
if(senderisadmin($sender)){
$sqlresult=sqlquery("SELECT word FROM words WHERE word=\"" . sqlescape($commandarray[2]) . "\"");
if(sqlnumrows($sqlresult)==0){
$sqlresult=sqlquery("SELECT id, shortname FROM categories WHERE shortname=\"" . sqlescape($commandarray[1]) . "\"");
if(sqlnumrows($sqlresult)!=0){
$categoryid=sqlresult($sqlresult,0,"id");
sqlquery("INSERT INTO words (categoryid, word) VALUES (" . sqlescape($categoryid) . ", '" . sqlescape($commandarray[2]) . "')");
array_push($cusswords,$commandarray[2]);
ircprivmessage("I now recognize \"$commandarray[2]\" as a cuss word.");
}else{
ircprivmessage("Error! The category \"$commandarray[1]\" does not exist.");
}
}else{
ircprivmessage("I already recognize \"$commandarray[2]\" as a cuss word.");
}
}
}else{
ircprivmessage("Invalid command! Syntax is \"addword <category> <word>\"");
}
}else if($commandarray[0]=="removeword"){
if(count($commandarray)==2){
if(senderisadmin($sender)){
$sqlresult=sqlquery("SELECT word FROM words WHERE word=\"" . sqlescape($commandarray[1]) . "\"");
if(sqlnumrows($sqlresult)>0){
sqlquery("DELETE FROM words WHERE word='" . sqlescape($commandarray[1]) . "'");
foreach ($cusswords as $key => $value) {
if($value==$commandarray[1]) {
unset($cusswords[$key]);
}
}
ircprivmessage("I no longer recognize \"$commandarray[1]\" as a cuss word.");
}else{
ircprivmessage("I do not recognize \"$commandarray[1]\" as a cuss word.");
}
}
}else{
ircprivmessage("Invalid command! Syntax is \"removeword <word>\"");
}
}else if($commandarray[0]=="refreshwords"){
if(count($commandarray)==1){
$cusswords=array();
$sqlresult=sqlquery("SELECT word FROM words");
for($i=0;$i<sqlnumrows($sqlresult);$i++){
array_push($cusswords,sqlresult($sqlresult,$i,"word"));
}
ircprivmessage("I have refreshed my word list successfully!");
}else{
ircprivmessage("Invalid command! Syntax is \"refreshwords\"");
}
}else if($commandarray[0]=="listcategories"){
if(count($commandarray)==1){
$response="The cuss categories I know are: ";
$sqlresult=sqlquery("SELECT shortname FROM categories");
for($i=0;$i<sqlnumrows($sqlresult);$i++){
$response.="\"" . sqlresult($sqlresult,$i,"shortname") . "\" ";
}
ircprivmessage($response);
}else{
ircprivmessage("Invalid command! Syntax is \"listcategories\"");
}
}else if($commandarray[0]=="addcategory"){
if(count($commandarray)>=3){
if(senderisadmin($sender)){
$sqlresult=sqlquery("SELECT shortname FROM categories WHERE shortname=\"" . sqlescape($commandarray[1]) . "\"");
if(sqlnumrows($sqlresult)==0){
$fullname="";
for($i=2;$i<count($commandarray);$i++){
$fullname.=$commandarray[$i];
if($i<(count($commandarray)-1)){
$fullname.=" ";
}
}
sqlquery("INSERT INTO categories (shortname, fullname) VALUES ('" . sqlescape($commandarray[1]) . "', '" . sqlescape($fullname) . "')");
ircprivmessage("I now recognize \"$commandarray[1]\" as a cuss category.");
}else{
ircprivmessage("I already recognize \"$commandarray[1]\" as a cuss category.");
}
}
}else{
ircprivmessage("Invalid command! Syntax is \"addcategory <shortname> <fullname>\"");
}
}else if($commandarray[0]=="removecategory"){
if(count($commandarray)==2){
if(senderisadmin($sender)){
$sqlresult=sqlquery("SELECT shortname FROM categories WHERE shortname=\"" . sqlescape($commandarray[1]) . "\"");
if(sqlnumrows($sqlresult)>0){
sqlquery("DELETE FROM categories WHERE shortname='" . sqlescape($commandarray[1]) . "'");
ircprivmessage("I no longer recognize \"$commandarray[1]\" as a cuss category.");
}else{
ircprivmessage("I do not recognize \"$commandarray[1]\" as a cuss category.");
}
}
}else{
ircprivmessage("Invalid command! Syntax is \"removecategory <shortname>\"");
}
}else{
ircprivmessage("Invalid command! Try \"help\"");
}
}else{
if(($sender!="MCLUGBridge")&&($sender!="TeeBridge")){
foreach($cusswords as $cussword){
if(stristr($message,$cussword)){
sqlquery("INSERT INTO uses (nick,channelid,wordid,message) VALUES ('" . sqlescape(trim(strtolower(preg_replace('/[^a-zA-Z0-9\s]/','',$sender)))) . "',(SELECT id FROM channels WHERE name='" . sqlescape($target) . "'),(SELECT id FROM words WHERE word='" . sqlescape($cussword) . "'),'" . sqlescape($message) . "');");
}
}
}
}
}
}
}
function ircconnect($host){
do{
$GLOBALS['ircconnection']=fsockopen($GLOBALS['ircserver'],$GLOBALS['ircport']);
}while(($GLOBALS['ircconnection']==false)&&(sleep(2)||true));
irccommand("NICK " . $GLOBALS['botnick']);
irccommand("USER " . $GLOBALS['botnick'] . " 8 * : " . $GLOBALS['botname']);
irccommand("PASS " . $GLOBALS['botnick'] . ":" . $GLOBALS['ircpassword']);
$sqlresult=sqlquery("SELECT * FROM channels",$GLOBALS['mysqlconnection']);
for($i=0;$i<sqlnumrows($sqlresult);$i++){
irccommand("JOIN " . sqlresult($sqlresult,$i,"name"));
}
if(sqlnumrows($sqlresult)==0){
print("WARNING: Channels list is empty. Joining ##bottest.\n");
irccommand("JOIN ##bottest");
}
}
function checkircconnection(){
fwrite($GLOBALS['ircconnection'],"\n");
if(fwrite($GLOBALS['ircconnection'],"\n")==false){
fclose($GLOBALS['ircconnection']);
ircconnect($GLOBALS['ircserver']);
}
}
function irccommand($commandarray){
print($commandarray . "\n");
fwrite($GLOBALS['ircconnection'],"$commandarray\n");
}
function ircprivmessage($message){
if($message!=""){
irccommand("PRIVMSG " . $GLOBALS['target'] ." :$message");
}
}
function ircreadline(){
$line=fgets($GLOBALS['ircconnection']);
print($line);
return $line;
}
function sqlconnect(){
do{
$GLOBALS['mysqlconnection']=mysqli_connect($GLOBALS['sqlserver'],$GLOBALS['sqlusername'],$GLOBALS['sqlpassword']);
}while((!$GLOBALS['mysqlconnection'])&&(sleep(2)||TRUE));
mysqli_select_db($GLOBALS['mysqlconnection'],$GLOBALS['sqldb']);
}
function checkmysqlconnection(){
if(!mysqli_ping($GLOBALS['mysqlconnection'])){
mysqli_close($GLOBALS['mysqlconnection']);
sqlconnect();
}
}
function sqlquery($query){
checkmysqlconnection();
return mysqli_query($GLOBALS['mysqlconnection'],$query);
}
function sqlresult($res,$row,$field=0){
$res->data_seek($row);
$datarow=$res->fetch_array();
return $datarow[$field];
}
function sqlescape($string){
return mysqli_real_escape_string($GLOBALS['mysqlconnection'],$string);
}
function sqlnumrows($result){
return mysqli_num_rows($result);
}
function senderisadmin($sender){
if(sqlnumrows(sqlquery("SELECT * FROM admins WHERE nick=\"" . sqlescape($sender) . "\""))>0){
return true;
}else{
ircprivmessage("Insufficient privileges!");
return false;
}
}
function startsWith($haystack,$needle){
return !strncmp($haystack,$needle,strlen($needle));
}
?>