This repository has been archived by the owner on Jul 27, 2019. It is now read-only.
/
PurpleBot.java
2915 lines (2726 loc) · 116 KB
/
PurpleBot.java
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
/*
* Copyright (C) 2014 cnaude
*
* 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/>.
*/
package com.cnaude.purpleirc;
import com.cnaude.purpleirc.IRCListeners.ActionListener;
import com.cnaude.purpleirc.IRCListeners.ConnectListener;
import com.cnaude.purpleirc.IRCListeners.DisconnectListener;
import com.cnaude.purpleirc.IRCListeners.JoinListener;
import com.cnaude.purpleirc.IRCListeners.KickListener;
import com.cnaude.purpleirc.IRCListeners.MessageListener;
import com.cnaude.purpleirc.IRCListeners.ModeListener;
import com.cnaude.purpleirc.IRCListeners.MotdListener;
import com.cnaude.purpleirc.IRCListeners.NickChangeListener;
import com.cnaude.purpleirc.IRCListeners.NoticeListener;
import com.cnaude.purpleirc.IRCListeners.PartListener;
import com.cnaude.purpleirc.IRCListeners.PrivateMessageListener;
import com.cnaude.purpleirc.IRCListeners.QuitListener;
import com.cnaude.purpleirc.IRCListeners.ServerResponseListener;
import com.cnaude.purpleirc.IRCListeners.TopicListener;
import com.cnaude.purpleirc.IRCListeners.VersionListener;
import com.cnaude.purpleirc.IRCListeners.WhoisListener;
import com.cnaude.purpleirc.Utilities.CaseInsensitiveMap;
import com.dthielke.herochat.Chatter;
import com.dthielke.herochat.Herochat;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSortedSet;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.UPlayer;
import com.nyancraft.reportrts.data.Ticket;
import com.titankingdoms.dev.titanchat.core.participant.Participant;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import me.botsko.prism.actionlibs.QueryParameters;
import me.botsko.prism.events.BlockStateChange;
import org.bukkit.Achievement;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.pircbotx.Channel;
import org.pircbotx.Configuration;
import org.pircbotx.PircBotX;
import org.pircbotx.User;
import org.pircbotx.UtilSSLSocketFactory;
import org.pircbotx.exception.IrcException;
import org.pircbotx.hooks.ListenerAdapter;
/**
*
* @author Chris Naude
*/
public final class PurpleBot {
private PircBotX bot;
public final PurpleIRC plugin;
private final File file;
private YamlConfiguration config;
private boolean connected;
public boolean autoConnect;
public boolean ssl;
public boolean trustAllCerts;
public boolean sendRawMessageOnConnect;
public boolean showMOTD;
public boolean channelCmdNotifyEnabled;
public boolean relayPrivateChat;
public boolean partInvalidChannels;
public int botServerPort;
public long chatDelay;
public String botServer;
public String bindAddress;
public String botNick;
public List<String> altNicks;
int nickIndex = 0;
public String botLogin;
public String botRealName;
public int ircMaxLineLength;
public String botServerPass;
public String charSet;
public String commandPrefix;
public String quitMessage;
public String botIdentPassword;
public String rawMessage;
public String channelCmdNotifyMode;
public String partInvalidChannelsMsg;
private String connectMessage;
public ArrayList<String> botChannels;
public CaseInsensitiveMap<Collection<String>> channelNicks;
public CaseInsensitiveMap<Collection<String>> tabIgnoreNicks;
public CaseInsensitiveMap<Collection<String>> filters;
public CaseInsensitiveMap<String> channelPassword;
public CaseInsensitiveMap<String> channelTopic;
public CaseInsensitiveMap<Boolean> channelTopicChanserv;
public CaseInsensitiveMap<String> activeTopic;
public CaseInsensitiveMap<String> channelModes;
public CaseInsensitiveMap<String> joinMsg;
public CaseInsensitiveMap<Boolean> msgOnJoin;
public CaseInsensitiveMap<Boolean> channelTopicProtected;
public CaseInsensitiveMap<Boolean> channelAutoJoin;
public long channelAutoJoinDelay;
public CaseInsensitiveMap<Boolean> ignoreIRCChat;
public CaseInsensitiveMap<Boolean> hideJoinWhenVanished;
public CaseInsensitiveMap<Boolean> hideListWhenVanished;
public CaseInsensitiveMap<Boolean> hideQuitWhenVanished;
public CaseInsensitiveMap<Boolean> invalidCommandPrivate;
public CaseInsensitiveMap<Boolean> invalidCommandCTCP;
public CaseInsensitiveMap<Boolean> logIrcToHeroChat;
public CaseInsensitiveMap<Boolean> enableMessageFiltering;
private final CaseInsensitiveMap<Boolean> shortify;
public CaseInsensitiveMap<String> heroChannel;
public CaseInsensitiveMap<String> townyChannel;
public CaseInsensitiveMap<Collection<String>> opsList;
public CaseInsensitiveMap<Collection<String>> voicesList;
public CaseInsensitiveMap<Collection<String>> worldList;
public CaseInsensitiveMap<Collection<String>> muteList;
public CaseInsensitiveMap<Collection<String>> enabledMessages;
public CaseInsensitiveMap<CaseInsensitiveMap<CaseInsensitiveMap<String>>> commandMap;
public CaseInsensitiveMap<Long> joinNoticeCooldownMap;
public ArrayList<CommandSender> whoisSenders;
public List<String> channelCmdNotifyRecipients;
public List<String> channelCmdNotifyIgnore;
private final ArrayList<ListenerAdapter> ircListeners;
public IRCMessageQueueWatcher messageQueue;
private final String fileName;
int joinNoticeCoolDown;
boolean joinNoticeEnabled;
boolean joinNoticePrivate;
boolean joinNoticeCtcp;
String joinNoticeMessage;
/**
*
* @param file
* @param plugin
*/
public PurpleBot(File file, PurpleIRC plugin) {
fileName = file.getName();
this.altNicks = new ArrayList<>();
this.connected = false;
this.botChannels = new ArrayList<>();
this.ircListeners = new ArrayList<>();
this.channelCmdNotifyRecipients = new ArrayList<>();
this.channelCmdNotifyIgnore = new ArrayList<>();
this.commandMap = new CaseInsensitiveMap<>();
this.joinNoticeCooldownMap = new CaseInsensitiveMap<>();
this.enabledMessages = new CaseInsensitiveMap<>();
this.muteList = new CaseInsensitiveMap<>();
this.worldList = new CaseInsensitiveMap<>();
this.opsList = new CaseInsensitiveMap<>();
this.voicesList = new CaseInsensitiveMap<>();
this.heroChannel = new CaseInsensitiveMap<>();
this.townyChannel = new CaseInsensitiveMap<>();
this.invalidCommandCTCP = new CaseInsensitiveMap<>();
this.logIrcToHeroChat = new CaseInsensitiveMap<>();
this.shortify = new CaseInsensitiveMap<>();
this.invalidCommandPrivate = new CaseInsensitiveMap<>();
this.hideQuitWhenVanished = new CaseInsensitiveMap<>();
this.hideListWhenVanished = new CaseInsensitiveMap<>();
this.hideJoinWhenVanished = new CaseInsensitiveMap<>();
this.ignoreIRCChat = new CaseInsensitiveMap<>();
this.channelAutoJoin = new CaseInsensitiveMap<>();
this.channelTopicProtected = new CaseInsensitiveMap<>();
this.channelModes = new CaseInsensitiveMap<>();
this.activeTopic = new CaseInsensitiveMap<>();
this.channelTopic = new CaseInsensitiveMap<>();
this.channelPassword = new CaseInsensitiveMap<>();
this.tabIgnoreNicks = new CaseInsensitiveMap<>();
this.filters = new CaseInsensitiveMap<>();
this.channelNicks = new CaseInsensitiveMap<>();
this.channelTopicChanserv = new CaseInsensitiveMap<>();
this.joinMsg = new CaseInsensitiveMap<>();
this.msgOnJoin = new CaseInsensitiveMap<>();
this.enableMessageFiltering = new CaseInsensitiveMap<>();
this.plugin = plugin;
this.file = file;
whoisSenders = new ArrayList<>();
config = new YamlConfiguration();
loadConfig();
addListeners();
buildBot();
messageQueue = new IRCMessageQueueWatcher(this, plugin);
}
public void buildBot() {
Configuration.Builder configBuilder = new Configuration.Builder()
.setName(botNick)
.setLogin(botLogin)
.setAutoNickChange(true)
.setCapEnabled(true)
.setMessageDelay(chatDelay)
.setRealName(botRealName)
.setMaxLineLength(ircMaxLineLength)
//.setAutoReconnect(autoConnect) // Why doesn't this work?
.setServer(botServer, botServerPort, botServerPass);
//addAutoJoinChannels(configBuilder);
for (ListenerAdapter ll : ircListeners) {
configBuilder.addListener(ll);
}
if (!botIdentPassword.isEmpty()) {
plugin.logInfo("Setting IdentPassword ...");
configBuilder.setNickservPassword(botIdentPassword);
}
if (ssl) {
UtilSSLSocketFactory socketFactory = new UtilSSLSocketFactory();
socketFactory.disableDiffieHellman();
if (trustAllCerts) {
plugin.logInfo("Enabling SSL and trusting all certificates ...");
socketFactory.trustAllCertificates();
} else {
plugin.logInfo("Enabling SSL ...");
}
configBuilder.setSocketFactory(socketFactory);
}
if (charSet.isEmpty()) {
plugin.logInfo("Using default character set: " + Charset.defaultCharset());
} else {
if (Charset.isSupported(charSet)) {
plugin.logInfo("Using character set: " + charSet);
configBuilder.setEncoding(Charset.forName(charSet));
} else {
plugin.logError("Invalid character set: " + charSet);
plugin.logInfo("Available character sets: " + Joiner.on(", ").join(Charset.availableCharsets().keySet()));
plugin.logInfo("Using default character set: " + Charset.defaultCharset());
}
}
if (!bindAddress.isEmpty()) {
plugin.logInfo("Binding to " + bindAddress);
try {
configBuilder.setLocalAddress(InetAddress.getByName(bindAddress));
} catch (UnknownHostException ex) {
plugin.logError(ex.getMessage());
}
}
Configuration configuration = configBuilder.buildConfiguration();
bot = new PircBotX(configuration);
if (autoConnect) {
asyncConnect();
} else {
plugin.logInfo("Auto-connect is disabled. To connect: /irc connect " + bot.getNick());
}
plugin.logInfo("Max line length: " + configBuilder.getMaxLineLength());
}
private void addListeners() {
ircListeners.add(new ActionListener(plugin, this));
ircListeners.add(new ConnectListener(plugin, this));
ircListeners.add(new DisconnectListener(plugin, this));
ircListeners.add(new JoinListener(plugin, this));
ircListeners.add(new KickListener(plugin, this));
ircListeners.add(new MessageListener(plugin, this));
ircListeners.add(new ModeListener(plugin, this));
ircListeners.add(new NickChangeListener(plugin, this));
ircListeners.add(new NoticeListener(plugin, this));
ircListeners.add(new PartListener(plugin, this));
ircListeners.add(new PrivateMessageListener(plugin, this));
ircListeners.add(new QuitListener(plugin, this));
ircListeners.add(new TopicListener(plugin, this));
ircListeners.add(new VersionListener(plugin));
ircListeners.add(new WhoisListener(plugin, this));
ircListeners.add(new MotdListener(plugin, this));
ircListeners.add(new ServerResponseListener(plugin, this));
}
/*
private void addAutoJoinChannels(Configuration.Builder configBuilder) {
for (String channelName : botChannels) {
if (channelAutoJoin.containsKey(channelName)) {
if (channelAutoJoin.get(channelName)) {
if (channelPassword.get(channelName).isEmpty()) {
configBuilder.addAutoJoinChannel(channelName);
} else {
configBuilder.addAutoJoinChannel(channelName, channelPassword.get(channelName));
}
}
}
}
}
*/
public void autoJoinChannels() {
plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() {
@Override
public void run() {
for (String channelName : botChannels) {
if (channelAutoJoin.containsKey(channelName)) {
if (channelAutoJoin.get(channelName)) {
if (bot.isConnected()) {
if (channelPassword.get(channelName).isEmpty()) {
bot.sendIRC().joinChannel(channelName);
} else {
bot.sendIRC().joinChannel(channelName, channelPassword.get(channelName));
}
}
}
}
}
}
}, channelAutoJoinDelay);
}
public void reload(CommandSender sender) {
sender.sendMessage("Reloading bot: " + botNick);
reload();
}
public void reload() {
asyncQuit(true);
}
/**
*
* @param sender
*/
public void reloadConfig(CommandSender sender) {
config = new YamlConfiguration();
loadConfig();
sender.sendMessage("[PurpleIRC] [" + botNick + "] IRC bot configuration reloaded.");
}
/**
*
* @param channelName
* @param sender
* @param user
*/
public void mute(String channelName, CommandSender sender, String user) {
if (muteList.get(channelName).contains(user)) {
sender.sendMessage("User '" + user + "' is already muted.");
} else {
sender.sendMessage("User '" + user + "' is now muted.");
muteList.get(channelName).add(user);
saveConfig();
}
}
/**
*
* @param channelName
* @param sender
*/
public void muteList(String channelName, CommandSender sender) {
if (muteList.get(channelName).isEmpty()) {
sender.sendMessage("There are no users muted for " + channelName);
} else {
sender.sendMessage("Muted users for " + channelName
+ ": " + Joiner.on(", ").join(muteList.get(channelName)));
saveConfig();
}
}
/**
*
* @param channelName
* @param sender
* @param user
*/
public void unMute(String channelName, CommandSender sender, String user) {
if (muteList.get(channelName).contains(user)) {
sender.sendMessage("User '" + user + "' is no longer muted.");
muteList.get(channelName).remove(user);
saveConfig();
} else {
sender.sendMessage("User '" + user + "' is not muted.");
}
}
public void asyncConnect(CommandSender sender) {
sender.sendMessage(connectMessage);
asyncConnect();
}
public boolean isShortifyEnabled(String channelName) {
if (shortify.containsKey(channelName)) {
return shortify.get(channelName);
}
return false;
}
/**
*
*/
public void asyncConnect() {
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
try {
plugin.logInfo(connectMessage);
bot.startBot();
} catch (IOException | IrcException ex) {
plugin.logError("Problem connecting to " + botServer + " => "
+ " as " + botNick + " [Error: " + ex.getMessage() + "]");
}
}
});
}
public void asyncIRCMessage(final String target, final String message) {
plugin.logDebug("Entering aysncIRCMessage");
messageQueue.add(new IRCMessage(target, plugin.colorConverter.
gameColorsToIrc(message), false));
}
public void asyncCTCPMessage(final String target, final String message) {
plugin.logDebug("Entering asyncCTCPMessage");
messageQueue.add(new IRCMessage(target, plugin.colorConverter
.gameColorsToIrc(message), true));
}
public void blockingIRCMessage(final String target, final String message) {
if (!this.isConnected()) {
return;
}
plugin.logDebug("[blockingIRCMessage] About to send IRC message to " + target);
bot.sendIRC().message(target, plugin.colorConverter
.gameColorsToIrc(message));
plugin.logDebug("[blockingIRCMessage] Message sent to " + target);
}
public void blockingCTCPMessage(final String target, final String message) {
if (!this.isConnected()) {
return;
}
plugin.logDebug("[blockingCTCPMessage] About to send IRC message to " + target);
bot.sendIRC().ctcpResponse(target, plugin.colorConverter
.gameColorsToIrc(message));
plugin.logDebug("[blockingCTCPMessage] Message sent to " + target);
}
public void asyncCTCPCommand(final String target, final String command) {
if (!this.isConnected()) {
return;
}
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
bot.sendIRC().ctcpCommand(target, command);
}
});
}
/**
*
* @param sender
*/
public void saveConfig(CommandSender sender) {
try {
config.save(file);
sender.sendMessage(plugin.LOG_HEADER_F
+ " Saving bot \"" + botNick + "\" to " + file.getName());
} catch (IOException ex) {
plugin.logError(ex.getMessage());
sender.sendMessage(ex.getMessage());
}
}
/**
*
*/
public void saveConfig() {
try {
config.save(file);
} catch (IOException ex) {
plugin.logError(ex.getMessage());
}
}
/**
*
* @param sender
* @param newNick
*/
public void asyncChangeNick(final CommandSender sender, final String newNick) {
if (!this.isConnected()) {
return;
}
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
bot.sendIRC().changeNick(newNick);
}
});
sender.sendMessage("Setting nickname to " + newNick);
config.set("nick", newNick);
saveConfig();
}
public void asyncJoinChannel(final String channelName, final String password) {
if (!this.isConnected()) {
return;
}
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
bot.sendIRC().joinChannel(channelName, password);
}
});
}
public void asyncNotice(final String target, final String message) {
if (!this.isConnected()) {
return;
}
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
bot.sendIRC().notice(target, message);
}
});
}
public void asyncRawlineNow(final String message) {
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
bot.sendRaw().rawLineNow(message);
}
});
}
public void asyncIdentify(final String password) {
if (!this.isConnected()) {
return;
}
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
bot.sendIRC().identify(password);
}
});
}
/**
*
* @param sender
* @param newLogin
*/
public void changeLogin(CommandSender sender, String newLogin) {
sender.sendMessage(ChatColor.DARK_PURPLE
+ "Login set to " + ChatColor.WHITE
+ newLogin + ChatColor.DARK_PURPLE
+ ". Reload the bot for the change to take effect.");
config.set("login", newLogin);
saveConfig();
}
private void sanitizeServerName() {
botServer = botServer.replace("^.*\\/\\/", "");
botServer = botServer.replace(":\\d+$", "");
config.set("server", botServer);
saveConfig();
}
private void loadConfig() {
try {
config.load(file);
autoConnect = config.getBoolean("autoconnect", true);
ssl = config.getBoolean("ssl", false);
trustAllCerts = config.getBoolean("trust-all-certs", false);
sendRawMessageOnConnect = config.getBoolean("raw-message-on-connect", false);
rawMessage = config.getString("raw-message", "");
relayPrivateChat = config.getBoolean("relay-private-chat", false);
partInvalidChannels = config.getBoolean("part-invalid-channels", false);
partInvalidChannelsMsg = config.getString("part-invalid-channels-message", "");
botNick = config.getString("nick", "");
altNicks = config.getStringList("alt-nicks");
plugin.loadTemplates(config, botNick);
botLogin = config.getString("login", "PircBot");
botRealName = config.getString("realname", "");
ircMaxLineLength = config.getInt("max-line-length", 512);
if (botRealName.isEmpty()) {
botRealName = plugin.getServer()
.getPluginManager().getPlugin("PurpleIRC")
.getDescription().getWebsite();
}
botServer = config.getString("server", "");
bindAddress = config.getString("bind", "");
channelAutoJoinDelay = config.getLong("channel-auto-join-delay", 20);
charSet = config.getString("charset", "");
sanitizeServerName();
showMOTD = config.getBoolean("show-motd", false);
botServerPort = config.getInt("port");
botServerPass = config.getString("password", "");
botIdentPassword = config.getString("ident-password", "");
commandPrefix = config.getString("command-prefix", ".");
chatDelay = config.getLong("message-delay", 1000);
plugin.logDebug("Message Delay => " + chatDelay);
quitMessage = ChatColor.translateAlternateColorCodes('&', config.getString("quit-message", ""));
plugin.logDebug("Nick => " + botNick);
plugin.logDebug("Login => " + botLogin);
plugin.logDebug("Server => " + botServer);
plugin.logDebug("Channel Auto Join Delay => " + channelAutoJoinDelay);
plugin.logDebug(("Bind => ") + bindAddress);
plugin.logDebug("SSL => " + ssl);
plugin.logDebug("Trust All Certs => " + trustAllCerts);
plugin.logDebug("Port => " + botServerPort);
plugin.logDebug("Command Prefix => " + commandPrefix);
//plugin.logDebug("Server Password => " + botServerPass);
plugin.logDebug("Quit Message => " + quitMessage);
botChannels.clear();
opsList.clear();
voicesList.clear();
muteList.clear();
enabledMessages.clear();
worldList.clear();
commandMap.clear();
channelCmdNotifyEnabled = config.getBoolean("command-notify.enabled", false);
plugin.logDebug(" CommandNotifyEnabled => " + channelCmdNotifyEnabled);
channelCmdNotifyMode = config.getString("command-notify.mode", "msg");
plugin.logDebug(" channelCmdNotifyMode => " + channelCmdNotifyMode);
// build command notify recipient list
for (String recipient : config.getStringList("command-notify.recipients")) {
if (!channelCmdNotifyRecipients.contains(recipient)) {
channelCmdNotifyRecipients.add(recipient);
}
plugin.logDebug(" Command Notify Recipient => " + recipient);
}
if (channelCmdNotifyRecipients.isEmpty()) {
plugin.logInfo(" No command recipients defined.");
}
// build command notify ignore list
for (String command : config.getStringList("command-notify.ignore")) {
if (!channelCmdNotifyIgnore.contains(command)) {
channelCmdNotifyIgnore.add(command);
}
plugin.logDebug(" Command Notify Ignore => " + command);
}
if (channelCmdNotifyIgnore.isEmpty()) {
plugin.logInfo(" No command-notify ignores defined.");
}
for (String enChannelName : config.getConfigurationSection("channels").getKeys(false)) {
String channelName = decodeChannel(enChannelName);
if (isValidChannel(channelName)) {
plugin.logError("Ignoring duplicate channel: " + channelName);
continue;
}
plugin.logDebug("Channel => " + channelName);
botChannels.add(channelName);
channelAutoJoin.put(channelName, config.getBoolean("channels." + enChannelName + ".autojoin", true));
plugin.logDebug(" Autojoin => " + channelAutoJoin.get(channelName));
channelPassword.put(channelName, config.getString("channels." + enChannelName + ".password", ""));
channelTopic.put(channelName, config.getString("channels." + enChannelName + ".topic", ""));
plugin.logDebug(" Topic => " + channelTopic.get(channelName));
channelModes.put(channelName, config.getString("channels." + enChannelName + ".modes", ""));
plugin.logDebug(" Channel Modes => " + channelModes.get(channelName));
channelTopicProtected.put(channelName, config.getBoolean("channels." + enChannelName + ".topic-protect", false));
plugin.logDebug(" Topic Protected => " + channelTopicProtected.get(channelName).toString());
channelTopicChanserv.put(channelName, config.getBoolean("channels." + enChannelName + ".topic-chanserv", false));
plugin.logDebug(" Topic Chanserv Mode => " + channelTopicChanserv.get(channelName).toString());
heroChannel.put(channelName, config.getString("channels." + enChannelName + ".hero-channel", ""));
plugin.logDebug(" HeroChannel => " + heroChannel.get(channelName));
townyChannel.put(channelName, config.getString("channels." + enChannelName + ".towny-channel", ""));
plugin.logDebug(" TownyChannel => " + townyChannel.get(channelName));
logIrcToHeroChat.put(channelName, config.getBoolean("channels." + enChannelName + ".log-irc-to-hero-chat", false));
plugin.logDebug(" LogIrcToHeroChat => " + logIrcToHeroChat.get(channelName));
ignoreIRCChat.put(channelName, config.getBoolean("channels." + enChannelName + ".ignore-irc-chat", false));
plugin.logDebug(" IgnoreIRCChat => " + ignoreIRCChat.get(channelName));
hideJoinWhenVanished.put(channelName, config.getBoolean("channels." + enChannelName + ".hide-join-when-vanished", true));
plugin.logDebug(" HideJoinWhenVanished => " + hideJoinWhenVanished.get(channelName));
hideListWhenVanished.put(channelName, config.getBoolean("channels." + enChannelName + ".hide-list-when-vanished", true));
plugin.logDebug(" HideListWhenVanished => " + hideListWhenVanished.get(channelName));
hideQuitWhenVanished.put(channelName, config.getBoolean("channels." + enChannelName + ".hide-quit-when-vanished", true));
plugin.logDebug(" HideQuitWhenVanished => " + hideQuitWhenVanished.get(channelName));
invalidCommandPrivate.put(channelName, config.getBoolean("channels." + enChannelName + ".invalid-command.private", false));
plugin.logDebug(" InvalidCommandPrivate => " + invalidCommandPrivate.get(channelName));
invalidCommandCTCP.put(channelName, config.getBoolean("channels." + enChannelName + ".invalid-command.ctcp", false));
plugin.logDebug(" InvalidCommandCTCP => " + invalidCommandCTCP.get(channelName));
shortify.put(channelName, config.getBoolean("channels." + enChannelName + ".shortify", true));
plugin.logDebug(" Shortify => " + shortify.get(channelName));
joinMsg.put(channelName, config.getString("channels." + enChannelName + ".raw-message", ""));
plugin.logDebug(" JoinMessage => " + joinMsg.get(channelName));
msgOnJoin.put(channelName, config.getBoolean("channels." + enChannelName + ".raw-message-on-join", false));
plugin.logDebug(" SendMessageOnJoin => " + msgOnJoin.get(channelName));
enableMessageFiltering.put(channelName, config.getBoolean("channels." + enChannelName + ".enable-filtering", false));
plugin.logDebug(" EnableMessageFiltering => " + enableMessageFiltering.get(channelName));
// build channel op list
Collection<String> cOps = new ArrayList<>();
for (String channelOper : config.getStringList("channels." + enChannelName + ".ops")) {
if (!cOps.contains(channelOper)) {
cOps.add(channelOper);
}
plugin.logDebug(" Channel Op => " + channelOper);
}
opsList.put(channelName, cOps);
if (opsList.isEmpty()) {
plugin.logInfo("No channel ops defined.");
}
// build channel voice list
Collection<String> cVoices = new ArrayList<>();
for (String channelVoice : config.getStringList("channels." + enChannelName + ".voices")) {
if (!cVoices.contains(channelVoice)) {
cVoices.add(channelVoice);
}
plugin.logDebug(" Channel Voice => " + channelVoice);
}
voicesList.put(channelName, cVoices);
if (voicesList.isEmpty()) {
plugin.logInfo("No channel voices defined.");
}
// build mute list
Collection<String> m = new ArrayList<>();
for (String mutedUser : config.getStringList("channels." + enChannelName + ".muted")) {
if (!m.contains(mutedUser)) {
m.add(mutedUser);
}
plugin.logDebug(" Channel Mute => " + mutedUser);
}
muteList.put(channelName, m);
if (muteList.isEmpty()) {
plugin.logInfo("IRC mute list is empty.");
}
// build valid chat list
Collection<String> c = new ArrayList<>();
for (String validChat : config.getStringList("channels." + enChannelName + ".enabled-messages")) {
if (!c.contains(validChat)) {
c.add(validChat);
}
plugin.logDebug(" Enabled Message => " + validChat);
}
enabledMessages.put(channelName, c);
if (enabledMessages.isEmpty()) {
plugin.logInfo("There are no enabled messages!");
}
// build valid world list
Collection<String> w = new ArrayList<>();
for (String validWorld : config.getStringList("channels." + enChannelName + ".worlds")) {
if (!w.contains(validWorld)) {
w.add(validWorld);
}
plugin.logDebug(" Enabled World => " + validWorld);
}
worldList.put(channelName, w);
if (worldList.isEmpty()) {
plugin.logInfo("World list is empty!");
}
// build valid world list
Collection<String> t = new ArrayList<>();
for (String name : config.getStringList("channels." + enChannelName + ".custom-tab-ignore-list")) {
if (!t.contains(name)) {
t.add(name);
}
plugin.logDebug(" Tab Ignore => " + name);
}
tabIgnoreNicks.put(channelName, t);
if (tabIgnoreNicks.isEmpty()) {
plugin.logInfo("World list is empty!");
}
// build valid world list
Collection<String> f = new ArrayList<>();
for (String word : config.getStringList("channels." + enChannelName + ".filter-list")) {
if (!f.contains(word)) {
f.add(word);
}
plugin.logDebug(" Filtered From IRC => " + word);
}
filters.put(channelName, f);
if (filters.isEmpty()) {
plugin.logInfo("World list is empty!");
}
// build join notice
joinNoticeCoolDown = config.getInt("channels." + enChannelName + ".join-notice.cooldown", 60);
joinNoticeEnabled = config.getBoolean("channels." + enChannelName + ".join-notice.enabled", false);
joinNoticePrivate = config.getBoolean("channels." + enChannelName + ".join-notice.private", true);
joinNoticeCtcp = config.getBoolean("channels." + enChannelName + ".join-notice.ctcp", true);
joinNoticeMessage = config.getString("channels." + enChannelName + ".join-notice.message", "");
plugin.logDebug("join-notice.cooldown: " + joinNoticeCoolDown);
plugin.logDebug("join-notice.enabled: " + joinNoticeEnabled);
plugin.logDebug("join-notice.private: " + joinNoticePrivate);
plugin.logDebug("join-notice.ctcp: " + joinNoticeCtcp);
plugin.logDebug("join-notice.message: " + joinNoticeMessage);
// build command map
CaseInsensitiveMap<CaseInsensitiveMap<String>> map = new CaseInsensitiveMap<>();
try {
for (String command : config.getConfigurationSection("channels." + enChannelName + ".commands").getKeys(false)) {
plugin.logDebug(" Command => " + command);
CaseInsensitiveMap<String> optionPair = new CaseInsensitiveMap<>();
String commandKey = "channels." + enChannelName + ".commands." + command + ".";
optionPair.put("modes", config.getString(commandKey + "modes", "*"));
optionPair.put("private", config.getString(commandKey + "private", "false"));
optionPair.put("ctcp", config.getString(commandKey + "ctcp", "false"));
optionPair.put("game_command", config.getString(commandKey + "game_command", "@help"));
optionPair.put("private_listen", config.getString(commandKey + "private_listen", "true"));
optionPair.put("channel_listen", config.getString(commandKey + "channel_listen", "true"));
optionPair.put("perm", config.getString(commandKey + "perm", ""));
for (String s : optionPair.keySet()) {
config.set(commandKey + s, optionPair.get(s));
}
map.put(command, optionPair);
}
} catch (Exception ex) {
plugin.logError("No commands found for channel " + enChannelName);
}
commandMap.put(channelName, map);
if (map.isEmpty()) {
plugin.logInfo("No commands specified!");
}
connectMessage = "Connecting to \"" + botServer + ":"
+ botServerPort + "\" as \"" + botNick
+ "\" [SSL: " + ssl + "]" + " [TrustAllCerts: "
+ trustAllCerts + "]";
}
} catch (IOException | InvalidConfigurationException ex) {
plugin.logError(ex.getMessage());
}
}
/**
*
* @param sender
* @param delay
*/
public void setIRCDelay(CommandSender sender, long delay) {
config.set("message-delay", delay);
saveConfig();
sender.sendMessage(ChatColor.DARK_PURPLE
+ "IRC message delay changed to "
+ ChatColor.WHITE + delay + ChatColor.DARK_PURPLE + " ms. "
+ "Reload for the change to take effect.");
}
private boolean isPlayerInValidWorld(Player player, String channelName) {
if (worldList.containsKey(channelName)) {
if (worldList.get(channelName).contains("*")) {
return true;
}
if (worldList.get(channelName).contains(player.getWorld().getName())) {
return true;
}
}
return false;
}
/**
* Called from normal game chat listener
*
* @param player
* @param message
*/
public void gameChat(Player player, String message) {
if (!this.isConnected()) {
return;
}
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
}
if (plugin.fcHook != null) {
String playerChatMode;
String playerFactionName;
try {
playerChatMode = plugin.fcHook.getChatMode(player);
} catch (IllegalAccessError ex) {
plugin.logDebug("FC Error: " + ex.getMessage());
playerChatMode = "public";
}
try {
playerFactionName = plugin.fcHook.getFactionName(player);
} catch (IllegalAccessError ex) {
plugin.logDebug("FC Error: " + ex.getMessage());
playerFactionName = "unknown";
}
String chatName = "faction-" + playerChatMode.toLowerCase() + "-chat";
plugin.logDebug("Faction [Player: " + player.getName()
+ "] [Tag: " + playerFactionName + "] [Mode: "
+ playerChatMode + "]");
if (enabledMessages.get(channelName)
.contains(chatName)) {
asyncIRCMessage(channelName, plugin.tokenizer
.chatFactionTokenizer(player, botNick, message,
playerFactionName, playerChatMode));
} else {
plugin.logDebug("Player " + player.getName() + " is in chat mode \""
+ playerChatMode + "\" but \"" + chatName + "\" is disabled.");
}
} else {
plugin.logDebug("No Factions");
}
if (isMessageEnabled(channelName, TemplateName.GAME_CHAT)) {
plugin.logDebug("[" + TemplateName.GAME_CHAT + "] => "
+ channelName + " => " + message);
asyncIRCMessage(channelName, plugin.tokenizer
.gameChatToIRCTokenizer(player, plugin.getMsgTemplate(botNick, TemplateName.GAME_CHAT), message));
} else {
plugin.logDebug("Ignoring message due to "
+ TemplateName.GAME_CHAT + " not being listed.");
}
}
}
// Called from HeroChat listener
/**
*
* @param chatter
* @param chatColor
* @param message
*/
public void heroChat(Chatter chatter, ChatColor chatColor, String message) {
if (!this.isConnected()) {
return;
}
Player player = chatter.getPlayer();
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
}
String hChannel = chatter.getActiveChannel().getName();
String hNick = chatter.getActiveChannel().getNick();
String hColor = chatColor.toString();
plugin.logDebug("HC Channel: " + hChannel);
if (isMessageEnabled(channelName, "hero-" + hChannel + "-chat")
|| isMessageEnabled(channelName, TemplateName.HERO_CHAT)) {
asyncIRCMessage(channelName, plugin.tokenizer
.chatHeroTokenizer(player, message, hColor, hChannel,
hNick, plugin.getHeroChatChannelTemplate(botNick, hChannel)));
} else {
plugin.logDebug("Player " + player.getName() + " is in \""
+ hChannel + "\" but hero-" + hChannel + "-chat is disabled.");
}
}
}
public void mcMMOAdminChat(Player player, String message) {
if (!this.isConnected()) {
return;
}
for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) {
continue;
}
if (isMessageEnabled(channelName, TemplateName.MCMMO_ADMIN_CHAT)) {
plugin.logDebug("Sending message because " + TemplateName.MCMMO_ADMIN_CHAT + " is enabled.");
asyncIRCMessage(channelName, plugin.tokenizer
.mcMMOChatToIRCTokenizer(player, plugin.getMsgTemplate(
botNick, TemplateName.MCMMO_ADMIN_CHAT), message));
} else {
plugin.logDebug("Player " + player.getName()
+ " is in mcMMO AdminChat but " + TemplateName.MCMMO_ADMIN_CHAT + " is disabled.");
}
}
}