-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPPbot.java
More file actions
1835 lines (1565 loc) · 66.7 KB
/
Copy pathPPbot.java
File metadata and controls
1835 lines (1565 loc) · 66.7 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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import org.jibble.pircbot.*;
import java.util.*;
import java.text.*;
import java.util.regex.*;
import java.net.*;
import java.io.*;
public class PPbot extends PircBot
{
// for indexing into the mess below
final static int T_NICK = 0;
final static int T_EXACT = 1;
final static int T_KEYWORD = 2;
final static int T_VARIABLE = 3;
final static int T_DELTA = 4;
final String[][] triggers = {
// {string nick, bool exact, string match, string variable, int delta}
{"danyell", "false", "hah", "danyell.says.hah", "1"},
{"BungoDanderfluff","true", "meow", "meow", "1"},
{"xx3nvyxx", "true", "meow", "meow", "1"},
{"jtb", "false", "show", "jonthebastard.mentions.a.show", "1"},
{"jonthebastard", "false", "show", "jonthebastard.mentions.a.show", "1"},
{"jtb", "false", "shows", "jonthebastard.mentions.a.show", "1"},
{"jonthebastard", "false", "shows", "jonthebastard.mentions.a.show", "1"},
{"jtb", "false", "concert", "jonthebastard.mentions.a.show", "1"},
{"jonthebastard", "false", "concert", "jonthebastard.mentions.a.show", "1"},
{"jtb", "false", "concerts", "jonthebastard.mentions.a.show", "1"},
{"jonthebastard", "false", "concerts", "jonthebastard.mentions.a.show", "1"},
{"jtb", "false", "gig", "jonthebastard.mentions.a.show", "1"},
{"jonthebastard", "false", "gig", "jonthebastard.mentions.a.show", "1"},
{"jtb", "false", "gigs", "jonthebastard.mentions.a.show", "1"},
{"jonthebastard", "false", "gigs", "jonthebastard.mentions.a.show", "1"},
{"jtb", "false", "ticket", "jonthebastard.almost.mentions.a.show", "1"},
{"jonthebastard", "false", "ticket", "jonthebastard.almost.mentions.a.show", "1"},
{"jtb", "false", "tickets", "jonthebastard.almost.mentions.a.show", "1"},
{"jonthebastard", "false", "tickets", "jonthebastard.almost.mentions.a.show", "1"},
{"danyell", "false", "hah", "danyell.says.hah", "1"},
{"beatsake", "false", "hotpot", "beatsake.mentions.hot.pot", "1"},
{"beatsake", "false", "hot pot", "beatsake.mentions.hot.pot", "1"},
{"beatsake", "false", "hot.pot", "beatsake.mentions.hot.pot", "1"},
{"corioliss", "false", "ducks", "corioliss.ducks", "1"},
{"", "false", "seems good bro", "bro.yin", "1"},
{"", "false", "seems bad bro", "bro.yang", "1"},
{"", "false", "tolerance break", "lies", "1"},
{"", "false", "sgb", "bro.yin", "1"},
{"", "false", "sbb", "bro.yang", "1"} };
final String MAGIC_RESPONSE_CATEGORY = "magic8ball";
final String[] blacklistUsers = {"dongbot"};
final String[] blacklistKeys = {"gogurt"};
final String[] FILTERED_STRINGS = {"saagar", "ted"};
static final int MAX_MESSAGE_LEN = 400;
static final String KEY_REGEX = "[\\[\\]\\w\\._\\-|\\{\\}]{2,}";
static final String FRIDAY = " _____ ____ ___ ____ _ __ ___ _ _ \n" +
"| ___| _ \\|_ _| _ \\ / \\ \\ / / | | |\n" +
"| |_ | |_) || || | | |/ _ \\ V /| | | |\n" +
"| _| | _ < | || |_| / ___ \\| | |_|_|_|\n" +
"|_| |_| \\_\\___|____/_/ \\_\\_| (_|_|_)";
class Parse
{
public String channel;
public String sender;
public String key;
public long when;
}
class EntryComparator implements Comparator<Map.Entry<String, Integer> >
{
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b)
{
Integer value1 = a.getValue();
Integer value2 = b.getValue();
if(value1.compareTo(value2) == 0)
{
String word1 = a.getKey();
String word2 = b.getKey();
return word1.compareToIgnoreCase(word2);
} else
{
return value2.compareTo(value1);
}
}
}
static final long RECENT_WINDOW_MILLISECONDS = 5 * 60 * 1000; // 5 minutes
static final long MAX_PARSE_WINDOW_COUNT = 5;
static final long KICK_MAX_PARSE_WINDOW_COUNT = 8;
Hashtable<String, Integer> values = new Hashtable<String, Integer>();
Vector<Parse> recentParses = new Vector<Parse>();
Hashtable<String, Vector<String> > links = new Hashtable<String, Vector<String> >();
Hashtable<String, Vector<String> > facts = new Hashtable<String, Vector<String> >();
Vector<Parse> pendingParseResults = new Vector<Parse>();
Timer pendingResultsTimer;
static final long PENDING_RESULTS_TIMER_MILLIS = 15 * 1000; // 15 seconds
long lastFridayMessage = 0;
static final long FRIDAY_TIMEOUT_MILLIS = 15 * 60 * 1000; // 15 minutes
class LastSeen
{
long time;
String message;
}
Hashtable<String, LastSeen> seenInfo = new Hashtable<String, LastSeen>();
static final long SEEN_WINDOW_MILLISECONDS = 48 * 60 * 60 * 1000; // 24 hours
class Reminder
{
long created;
long when;
long when_expired;
String sender ;
String destination;
String message;
String channel;
public String toString()
{
SimpleDateFormat fmt = new SimpleDateFormat("hh:mm aa 'at' MM/dd/yyyy ");
String ret = "";
ret += "on " + fmt.format(new Date(created)) + ", ";
ret += sender + " asked me to remind you to ";
ret += "\"" + message + "\" ";
if(when != 0)
ret += "at precisely " + fmt.format(new Date(when));
else if (when_expired != 0)
ret += ". I reminded you on " + fmt.format(new Date(when_expired)) + ", but you weren't active then. This is your final reminder.";
return ret;
}
}
static final long RECENT_ACTIVITY_MILLISECONDS = 60 * 1000; // 60 seconds
static final long REMINDER_TIMER_PERIOD = 15 * 1000; // 15 seconds
Hashtable<String, Long> activityTracker = new Hashtable<String, Long>();
Hashtable<String, Vector<Reminder> > reminders = new Hashtable<String, Vector<Reminder> >();
Timer reminderTimer;
Vector<String> sunglassesWaitlist = new Vector<String>();
class ReminderTask extends TimerTask
{
public void run()
{
checkTimedReminders();
}
};
class PendingResultsTask extends TimerTask
{
public void run()
{
postPendingResults();
}
};
String channel;
String channelpw;
String data_file;
String data_file_backup;
String link_file;
String link_file_backup;
String fact_file;
String fact_file_backup;
public PPbot(String channel, String channelpw, String name, String password) {
this.channel = channel;
this.channelpw = channelpw;
data_file = channel + ".dat";
data_file_backup = channel + ".dat.bak";
link_file = channel + ".link";
link_file_backup = channel + ".link.bak";
fact_file = channel + ".fact";
fact_file_backup = channel + ".fact.bak";
this.setAutoNickChange(true);
this.setName(name);
this.identify(password);
restoreData();
onDisconnect();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
String[] chans = getChannels();
for(int i=0; i < chans.length; i++)
{
local_sendMessage(chans[i], line_header() + "oh shit, they're trying to shu$ d#wn $!@# )%()!#@%) !)!) 10928q$)(!@*$)moo");
while(getOutgoingQueueSize() > 0)
{
try { Thread.sleep(100); } catch (InterruptedException ie) {}
}
partChannel(chans[i], "ctrl-c, bitches");
}
}
});
reminderTimer = new Timer();
reminderTimer.schedule(new ReminderTask(), 0, REMINDER_TIMER_PERIOD);
pendingResultsTimer = new Timer();
}
public Vector<String> getMatches(String regex, String text)
{
Vector<String> retval = new Vector<String>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
while(m.find())
{
retval.add(m.group(1).toLowerCase());
}
return retval;
}
public void blacklistCallOut(String sender, String key)
{
local_sendMessage("#"+channel, line_header() + " hey guys, guess who's a dick by trying to parse with " + key + "? oh, it's just " + sender + "--");
applyMatch(getNick(), "#"+channel, sender, -1, false);
}
public void applyMatch(String sender, String channel, String key, int delta, boolean checkExpiry)
{
for(int i = 0; i < blacklistKeys.length; i++)
{
if(key.equalsIgnoreCase(blacklistKeys[i]))
{
local_sendMessage(sender, line_header() + "sorry, but " + key +" has been identified as a topic of great contention and been blacklisted");
blacklistCallOut(sender, key);
return;
}
}
if(checkExpiry && (sender != getNick()))
{
// expire anything before expiry_millis
long expiry_millis = ((new Date()).getTime() - RECENT_WINDOW_MILLISECONDS);
int parseCountBySender = 0;
// check the recent parses list to make sure it's not too soon
Iterator<Parse> i = recentParses.iterator();
while(i.hasNext())
{
Parse current = i.next();
if(current.when < expiry_millis)
{
System.out.println("parse for " + current.sender + " for " + current.key + " expired");
i.remove();
} else if(current.sender.equalsIgnoreCase(sender) && current.key.equalsIgnoreCase(key))
{
Date expiry = new Date(current.when - expiry_millis);
SimpleDateFormat format = new SimpleDateFormat("mm:ss", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
local_sendMessage(sender, line_header() + "sorry, " + sender + ", but you can't change \"" + key + "\" for another " + format.format(expiry));
return;
} else if(current.sender.equalsIgnoreCase(sender))
{
parseCountBySender++;
}
}
if(parseCountBySender >= KICK_MAX_PARSE_WINDOW_COUNT)
{
kick(channel, sender, "stop abusing the bot");
} else if(parseCountBySender >= MAX_PARSE_WINDOW_COUNT)
{
sendMessage(sender, line_header() + "sorry, you are limited to " + MAX_PARSE_WINDOW_COUNT + " parses within a " + (RECENT_WINDOW_MILLISECONDS / 1000 / 60) + " minute period. you're at " + parseCountBySender + ", and at " + KICK_MAX_PARSE_WINDOW_COUNT + ", I'll start being a dick.");
}
// it's valid! add it to recent parses
Parse p = new Parse();
p.sender = sender;
p.key = key;
p.when = (new Date()).getTime();
recentParses.add(p);
if(parseCountBySender >= MAX_PARSE_WINDOW_COUNT)
return;
}
if(!values.containsKey(key))
values.put(key, new Integer(delta));
else
values.put(key, new Integer(values.get(key).intValue() + delta));
if(channel.equalsIgnoreCase("#" + this.channel))
{
synchronized(pendingParseResults)
{
Parse p = new Parse();
p.sender = sender;
p.channel = channel;
p.key = key;
pendingParseResults.addElement(p);
pendingResultsTimer.cancel();
pendingResultsTimer = new Timer();
pendingResultsTimer.schedule(new PendingResultsTask(), PENDING_RESULTS_TIMER_MILLIS);
}
} else
{
displayValue(channel, sender, key);
}
}
// returns true if keys is not a unique set
public boolean applyMatches(String sender, String channel, Vector<String> keys, int delta, boolean checkExpiry)
{
HashSet<String> tmp = new HashSet<String>(keys);
for(String key : tmp)
{
applyMatch(sender, channel, key, delta, checkExpiry);
}
return (tmp.size() != keys.size());
}
public void processCommands(String channel, String sender, String message)
{
// process commands only if we're specifically targetted
String commandHeader = getNick() + ":";
String command = "";
String commandCase = "";
if(message.toLowerCase().startsWith(commandHeader.toLowerCase()))
{
command = message.substring(commandHeader.length()+1).trim().toLowerCase();
commandCase = message.substring(commandHeader.length()+1).trim();
}
// if privmsg and no header, try parsing it as a command
if(channel.equals(sender) && (command.length() == 0))
{
// no need for header in privmsg
command = message.toLowerCase();
commandCase = message;
}
if(command.length() == 0)
{
return;
}
// top or '?' => default query
System.out.println("command = " + command);
if(command.equals("?") || command.equals("top"))
{
sendStatistics(channel, 5);
} else if(command.startsWith("top")) //top N
{
String arg = command.substring(3).trim();
try
{
int n = Integer.parseInt(arg);
if(n > 25)
n = 25;
sendStatistics(channel, n);
} catch(Exception e)
{
local_sendMessage(sender, line_header() + "sorry, but I didn't understand the argument to that command!");
e.printStackTrace();
}
} else if(command.equals("friday!"))
{
long time = (new Date()).getTime();
if(time < (lastFridayMessage + FRIDAY_TIMEOUT_MILLIS))
{
kick(channel, sender, "too soon");
} else if((new Date()).getDay() != 5)
{
kick(channel, sender, "it's not Friday, fuck off");
} else
{
lastFridayMessage = time;
// send lines sequentually
StringTokenizer st = new StringTokenizer(FRIDAY, "\n");
while(st.hasMoreTokens())
{
sendMessage(channel, st.nextToken());
}
}
} else if(command.startsWith("??"))
{
String arg = command.substring(2).trim().toLowerCase();
sendKeyedLinkStatistics(channel, sender, arg);
} else if(command.startsWith("?++") || command.startsWith("?--"))
{
String arg = command.substring(3).trim().toLowerCase();
Vector<String> options = new Vector<String>();
String key;
Enumeration<String> keys = values.keys();
while(keys.hasMoreElements())
{
key = keys.nextElement();
if(key.toLowerCase().contains(arg.toLowerCase()))
options.addElement(key);
}
if(options.size() == 0)
{
String result = line_header() + "no results found, sorry";
local_sendMessage_carefully(channel, sender, result);
} else
{
// pick one and upvote randomly
key = options.elementAt((int)(options.size() * Math.random()));
if(command.startsWith("?++"))
{
String result = line_header() + options.size() + " found; we'll go with " + key + "++";
applyMatch(sender, channel, key, 1, true);
// local_sendMessage_carefully(channel, sender, result);
} else
{
String result = line_header() + options.size() + " found; we'll go with " + key + "--";
applyMatch(sender, channel, key, -1, true);
// local_sendMessage_carefully(channel, sender, result);
}
}
} else if(command.startsWith("?"))
{
String arg = command.substring(1).trim().toLowerCase();
sendKeyedStatistics(channel, sender, arg);
} else if(command.contains("+="))
{
Vector<String> postUpdates = new Vector<String>();
// parse out the two strings
Pattern p = Pattern.compile("(" + KEY_REGEX + ")\\s*\\+=\\s*(" + KEY_REGEX + ")");
Matcher m = p.matcher(message);
match_add: while(m.find())
{
System.out.println("linking " + m.group(1) + " to " + m.group(2));
String dest = m.group(1).toLowerCase();
String src = m.group(2).toLowerCase();
for(int i = 0; i < blacklistKeys.length; i++)
{
if(src.equalsIgnoreCase(blacklistKeys[i]))
{
local_sendMessage(sender, line_header() + "sorry, but " + src +" has been identified as a topic of great contention and been blacklisted");
blacklistCallOut(sender, src);
continue match_add;
}
}
if(dest.equals(src))
{
local_sendMessage(sender, line_header() + "sorry, but " + src + " can't be linked to itself!");
continue;
} else if(dest.equalsIgnoreCase(sender))
{
local_sendMessage(sender, line_header() + "sorry, but you can't link things to yourself!");
continue;
}
// make sure source exists
if(values.get(src) == null)
values.put(src, new Integer(0));
if(values.get(dest) == null)
values.put(dest, new Integer(0));
if(links.get(dest) == null)
{
Vector<String> targets = new Vector<String>();
targets.add(src);
links.put(dest, targets);
} else
{
Vector<String> targets = links.get(dest);
if(targets.contains(src))
{
local_sendMessage(sender, line_header() + "sorry, but " + src + " is already linked to " + dest);
continue;
}
targets.add(src);
links.put(dest, targets);
}
if(!postUpdates.contains(dest))
postUpdates.add(dest);
local_sendMessage(sender, line_header() + "I have linked the key \"" + dest + "\" so that it is now dependent on \"" + src + "\"!");
}
for(int i = 0; i < postUpdates.size(); i++)
{
displayValue(sender, sender, postUpdates.elementAt(i));
}
} else if(command.contains("-="))
{
Vector<String> postUpdates = new Vector<String>();
// parse out the two strings
Pattern p = Pattern.compile("(" + KEY_REGEX + ")\\s*\\-=\\s*(" + KEY_REGEX + ")");
Matcher m = p.matcher(message);
while(m.find())
{
System.out.println("unlinking " + m.group(1) + " from " + m.group(2));
String dest = m.group(1).toLowerCase();
String src = m.group(2).toLowerCase();
if(dest.equals(src))
{
local_sendMessage(sender, line_header() + "sorry, but " + src + " can't be linked to itself!");
continue;
} else if(dest.equalsIgnoreCase(sender))
{
local_sendMessage(sender, line_header() + "sorry, but you can't unlink things to yourself!");
continue;
}
if(links.get(dest) == null)
{
local_sendMessage(sender, line_header() + "sorry, but " + src + " isn't linked to " + dest);
} else
{
Vector<String> targets = links.get(dest);
if(targets.contains(src))
{
targets.remove(src);
links.put(dest, targets);
} else
{
local_sendMessage(sender, line_header() + "sorry, but " + src + " isn't linked to " + dest);
continue;
}
}
if(!postUpdates.contains(dest))
postUpdates.add(dest);
local_sendMessage(sender, line_header() + "I have unlinked the key \"" + dest + "\" so that it is no longer dependent on \"" + src + "\"!");
}
for(int i = 0; i < postUpdates.size(); i++)
{
displayValue(sender, sender, postUpdates.elementAt(i));
}
} else if(command.startsWith("what the fuck is the score of") || command.startsWith("what the fuck is the value of"))
{
String arg = command.substring((new String("what the fuck is the score of")).length()).trim().toLowerCase();
int delim = arg.indexOf("?");
if(delim != -1)
{
arg = arg.substring(0, delim);
}
sendKeyedStatistics(channel, sender, arg);
} else if(command.startsWith("who the fuck cares about"))
{
String arg = command.substring((new String("who the fuck cares about")).length()).trim().toLowerCase();
int delim = arg.indexOf("?");
if(delim != -1)
{
arg = arg.substring(0, delim);
}
sendKeyedLinkStatistics(channel, sender, arg);
} else if(command.startsWith("remind"))
{
String patterns_dated_prefix[] = {"remind (\\S+) at (.+) to (.+)", "remind (\\S+) at (.+) that (.+)"};
String patterns_dated_postfix[] = {"remind (\\S+) to (.+) at (.+)$", "remind (\\S+) that (.+) at (.+)"};
String patterns_undated[] = {"remind (\\S+) to (.+)", "remind (\\S+) that (.+)"};
Reminder reminder = null;
try
{
if(reminder == null)
reminder = parseReminder(patterns_dated_prefix, sender, command, 1, 2, 3);
if(reminder == null)
reminder = parseReminder(patterns_dated_postfix, sender, command, 1, 3, 2);
if(reminder == null)
reminder = parseReminder(patterns_undated, sender, command, 1, 0, 2);
if(reminder == null)
{
local_sendMessage(sender, line_header() + "sorry, but I couldn't parse your reminder. You should probably consult the manual (or bitch in IRC).");
return;
}
if(reminder.destination.equalsIgnoreCase(getNick()))
{
local_sendMessage(sender, line_header() + "Thanks, but I don't need reminding.");
return;
}
reminder.destination = reminder.destination.toLowerCase();
if(sender.equalsIgnoreCase(channel))
reminder.channel = reminder.destination;
else
reminder.channel = channel;
if((reminder.when == 0) && activityTracker.containsKey(reminder.destination))
{
Long timestamp = activityTracker.get(reminder.destination);
if(timestamp.longValue() > ((new Date()).getTime() - RECENT_ACTIVITY_MILLISECONDS))
{
local_sendMessage(sender, line_header() + "sorry, but " + reminder.destination + " has been active recently. Fucking tell them yourself like a grownup.");
return;
}
}
if((reminder.when != 0) && (reminder.when < (new Date()).getTime()))
{
local_sendMessage(sender, line_header() + "I can't remind people of things in the past yet. Feature pending invention of time travel.");
return;
}
synchronized(reminders)
{
// only one reminder per src,dst pair
if(!sender.equalsIgnoreCase(reminder.destination))
{
Vector<Reminder> destReminders = reminders.get(reminder.destination);
if(destReminders != null)
{
for(int i = 0; i < destReminders.size(); i++)
{
Reminder r = destReminders.elementAt(i);
if(r.sender.toLowerCase().contains(sender.toLowerCase()) ||
sender.toLowerCase().contains(r.sender.toLowerCase()))
{
local_sendMessage(sender, line_header() + " you can only have one active reminder per person, so I am removing your previous reminder: " + r.toString());
destReminders.removeElement(r);
break;
}
}
reminders.put(reminder.destination, destReminders);
}
}
Vector<Reminder> tmp = reminders.get(reminder.destination);
if(tmp == null)
tmp = new Vector<Reminder> ();
tmp.addElement(reminder);
reminders.put(reminder.destination, tmp);
}
String out = "okay, ";
if(reminder.when != 0)
{
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mm aa");
out += "on or after " + fmt.format(new Date(reminder.when)) + ", ";
}
out += "I will remind " + reminder.destination + " of that when I see them. Keep in mind that reminders disappear if the bot crashes or is shut down, so don't rely on this for *super* important things.";
local_sendMessage(sender, line_header() + out);
} catch(ParseException pe)
{
local_sendMessage(sender, line_header() + "sorry, I couldn't parse your date!");
System.out.println(pe);
}
} else if(command.equalsIgnoreCase("rimshot"))
{
local_sendMessage(channel, line_header() + "ba-dum-tish!");
} else if(command.equalsIgnoreCase("rimjob"))
{
local_sendMessage(channel, line_header() + "ba-dum-tush!");
} else if(command.equalsIgnoreCase("date"))
{
local_sendMessage(channel, line_header() + "yo, it's " + (new Date().toString()));
} else if(command.endsWith("..."))
{
sendAction(channel, "puts on sunglasses");
if(!sunglassesWaitlist.contains(sender.toLowerCase()))
sunglassesWaitlist.addElement(sender.toLowerCase());
} else if(command.startsWith("form of... ") && isChannelOp(sender))
{
String arg = command.substring((new String("form of... ")).length()).trim().toLowerCase();
changeNick(arg);
} else if(command.startsWith("facts about ") || command.startsWith("facts."))
{
String topic = "";
// subtopic
if(command.startsWith("facts about "))
{
topic = command.substring(command.indexOf("facts about ") + ("facts about ").length());
topic = topic.trim();
} else if(command.startsWith("facts."))
{
topic = command.substring(command.indexOf("facts.") + ("facts.").length());
topic = topic.trim();
}
if(topic.isEmpty())
{
local_sendMessage(sender, line_header() + "sorry, but you need to specify a topic for your query!");
} else
{
Vector<String> tmp = facts.get(topic);
if((tmp == null) || (tmp.size() == 0))
{
local_sendMessage(channel, line_header() + "Sorry, but unfortunately I don't know anything about " + topic + ". :(");
} else
{
String factString = "Wait, you want to know everything about " + topic + "? Well, I know " + tmp.size() + " things. Here goes. ";
for(int i = 0; i <tmp.size(); i++)
factString += (i+1) + ") " + tmp.elementAt(i) + (((i+1) < tmp.size()) ? "; " : "");
local_sendMessage_carefully(channel, sender, line_header() + factString);
}
}
} else if(command.startsWith("fact"))
{
String topic = "", factIndex = "";
int whichFact = -1;
// is there a subtopic?
if(command.startsWith("fact."))
{
String tmp = command.substring(command.indexOf("fact.") + ("fact.").length());
topic = tmp.trim();
} else if(command.startsWith("fact about "))
{
String tmp = command.substring(command.indexOf("fact about ") + ("fact about ").length());
topic = tmp.trim();
}
// if there's following text, try and parse it to a number
if(topic.contains(" "))
{
factIndex = topic.substring(topic.indexOf(" ") + 1);
topic = topic.substring(0, topic.indexOf(" "));
System.out.println("fact index is " + factIndex);
}
try
{
whichFact = Integer.parseInt(factIndex) - 1;
} catch(Exception e) {};
if(topic.isEmpty())
{
sendRandomFact(channel);
} else
{
Vector<String> tmp = facts.get(topic);
if((tmp == null) || (tmp.size() == 0))
{
local_sendMessage(channel, line_header() + "Sorry, but unfortunately I don't know anything about " + topic + ". :(");
} else
{
if(whichFact == -1)
{
whichFact = (int)(tmp.size()*Math.random());
local_sendMessage_carefully(channel, sender, line_header() + "Let me tell you something random about " + topic + "! Fact #" + (whichFact+1) + ": " + tmp.elementAt(whichFact));
} else
{
if(whichFact < 0)
whichFact = 0;
if(whichFact >= tmp.size())
whichFact = tmp.size()-1;
local_sendMessage_carefully(channel, sender, line_header() + "Let me tell you fact #" + (whichFact+1) + " about " + topic + ": " + tmp.elementAt(whichFact));
}
}
}
} else if(command.startsWith("addfact"))
{
String topic = command.substring(command.indexOf("addfact.") + ("addfact.").length());
topic = topic.substring(0, topic.indexOf(" "));
if(topic.length() == 0)
{
local_sendMessage(sender, line_header() + "sorry, but you need to specify a topic for your fact! Something like:");
local_sendMessage(sender, getNick() + ": addfact.cats Cats have nine lives.");
} else
{
String fact = commandCase.substring(command.indexOf("addfact"));
fact = fact.substring(fact.indexOf(" ")).trim();
if(facts.get(topic) == null)
{
Vector<String> tmp = new Vector<String>();
tmp.add(fact);
facts.put(topic, tmp);
} else
{
Vector<String> tmp = facts.get(topic);
tmp.add(fact);
facts.put(topic, tmp);
}
local_sendMessage(sender, line_header() + "Thanks! I now know " + facts.get(topic).size() + " thing[s] about " + topic + "!");
}
} else if(command.startsWith("deletefact"))
{
String topic = command.substring(command.indexOf("deletefact.") + ("deletefact.").length());
topic = topic.substring(0, topic.indexOf(" "));
if(topic.length() == 0)
{
local_sendMessage(sender, line_header() + "sorry, but you need to specify a topic for your fact! Something like:");
local_sendMessage(sender, getNick() + ": deletefact.cats 3");
} else
{
String whichFact = commandCase.substring(command.indexOf("deletefact"));
whichFact = whichFact.substring(whichFact.indexOf(" ")).trim();
int x = 0;
try
{
x = Integer.parseInt(whichFact) - 1;
if(facts.get(topic) == null)
{
local_sendMessage(sender, line_header() + "sorry, but I don't know any facts about that topic!");
} else
{
Vector<String> tmp = facts.get(topic);
if((x < 0) || (x > tmp.size()))
{
local_sendMessage(sender, line_header() + "sorry, but the fact you want me to delete doesn't exist. I only know " + tmp.size() + " things about " + topic);
} else
{
local_sendMessage(sender, line_header() + "I have removed the fact \"" + tmp.elementAt(x) + "\" from topic " + topic + ". Hope you're not changing history for the worse. I now know " + (tmp.size() - 1) + " thing[s] about " + topic + ".");
tmp.removeElementAt(x);
facts.put(topic, tmp);
}
}
} catch(Exception e)
{
local_sendMessage(sender, line_header() + "sorry, but you need to specify a fact number to remove! Something like:");
local_sendMessage(sender, getNick() + ": deletefact.cats 3");
}
}
} else if(command.equals("stats") || command.equals("statistics"))
{
String tmp = "Let me tell you what I know. I am keeping track of " + values.size() + " individual scores. ";
{
int nlinks = 0;
Enumeration<String> key = links.keys();
while(key.hasMoreElements())
{
String k = key.nextElement();
nlinks += links.get(k).size();
}
tmp += "I am also keeping track of " + nlinks + " dependencies between scores. ";
}
{
int nfacts = 0;
Enumeration<String> key = facts.keys();
while(key.hasMoreElements())
{
String k = key.nextElement();
nfacts += facts.get(k).size();
}
tmp += "Finally, I have been trained to recite " + nfacts + " facts about " + facts.size() + " topics! Isn't THAT impressive?";
local_sendMessage(channel, line_header() + tmp);
}
} else if(command.startsWith("where's") || command.startsWith("wheres") || command.startsWith("seen"))
{
boolean hit = false;
LastSeen latest = new LastSeen();
String latestName = "";
latest.time = 0;
String sub = "";
if(command.startsWith("where's"))
{
sub = command.substring(8).trim();
} else if(command.startsWith("wheres"))
{
sub = command.substring(7).trim();
} else if(command.startsWith("seen"))
{
sub = command.substring(5).trim();
}
if(sub.endsWith("?"))
{
sub = sub.substring(0, sub.length()-1).trim();
}
Enumeration<String> keys = seenInfo.keys();
while(keys.hasMoreElements())
{
String name = keys.nextElement();
if(name.toLowerCase().contains(sub.toLowerCase()))
{
hit = true;
if(seenInfo.get(name).time > latest.time)
{
latest = seenInfo.get(name);
latestName = name;
}
}
}
if(!hit)
{
local_sendMessage(channel, line_header() + "haven't seen " + sub + ", sorry bro");
} else
{
SimpleDateFormat fmt = new SimpleDateFormat("hh:mm aa 'at' MM/dd/yyyy ");
local_sendMessage(channel, line_header() + "saw " + latestName + " at " + fmt.format(new Date(latest.time)) + ", when they said... \"" + latest.message + "\"");
}
} else if(command.endsWith("?"))
{
// magic 8-ball response
Random gen = new Random();
Vector<String> responses = facts.get(MAGIC_RESPONSE_CATEGORY);
if(responses.size() == 0)
{
local_sendMessage(channel, line_header() + sender + ": I'm out of responses. :( try adding some to fact category " + MAGIC_RESPONSE_CATEGORY);
} else
{
String response = responses.elementAt(gen.nextInt(responses.size()));
local_sendMessage(channel, line_header() + sender + ": " + response);
}
} else
{
local_sendMessage(sender, line_header() + "sorry, but I didn't understand your command!");
}
}
public void onPrivateMessage(String sender, String login, String hostname, String message)
{
onMessage(sender, sender, login, hostname, message);
}
public void onAction(String sender, String login, String hostname, String target, String action)
{
onMessage(target, sender, login, hostname, action);
}
public void onMessage(String channel, String sender, String login, String hostname, String message)
{
if(sender.equals(getNick()))
return;
// process sunglasses
if(sunglassesWaitlist.contains(sender.toLowerCase()))
{
sunglassesWaitlist.removeElement(sender.toLowerCase());
local_sendMessage("#" + this.channel, line_header() + sender + ": YEAAAAAAAAAAAAHHHHH");
}
checkReminders(sender);
// update LastSeen
{
LastSeen record = new LastSeen();
record.time = (new Date()).getTime();
record.message = message;
seenInfo.put(sender, record);
// drop all old records
Enumeration<String> keys = seenInfo.keys();
while(keys.hasMoreElements())