PageRenderTime 40ms CodeModel.GetById 21ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/IRCanary.java

https://github.com/mbax/IRCanary
Java | 270 lines | 252 code | 18 blank | 0 comment | 45 complexity | 9a8ff2474cb933c6df46ccad05d470e5 MD5 | raw file
  1import java.io.File;
  2import java.io.FileWriter;
  3import java.io.IOException;
  4import java.util.ArrayList;
  5import java.util.Scanner;
  6import java.util.logging.Level;
  7import java.util.logging.Logger;
  8
  9public class IRCanary extends Plugin {
 10    private class IRCPluginListener extends PluginListener {
 11        private final IRCanary plugin;
 12
 13        public IRCPluginListener(IRCanary plugin) {
 14            this.plugin = plugin;
 15        }
 16
 17        @Override
 18        public void onBan(Player admin, Player player, String reason) {
 19            if (IRCanary.this.allowKickBanUpdates) {
 20                this.plugin.sendIRCMessage(IRCanary.this.banMessage.replace("%admin", admin.getName()).replace("%player", player.getName()).replace("%reason", reason));
 21            }
 22        }
 23
 24        @Override
 25        public boolean onChat(Player player, String message) {
 26            this.plugin.sendIRCMessage("<" + player.getName() + ">" + " " + message);
 27            return false;
 28        }
 29
 30        @Override
 31        public boolean onCommand(Player player, String[] split) {
 32            if (split[0].equalsIgnoreCase("/me")) {
 33                String message = "";
 34                for (int $x = 1; $x < split.length; $x++) {
 35                    message = message + " " + split[$x];
 36                }
 37                this.plugin.sendIRCMessage("* " + player.getName() + message);
 38            }
 39            if ((split[0].equalsIgnoreCase("/ircreload")) && (player.canUseCommand("/ircreload"))) {
 40                this.plugin.loadAdmins();
 41                player.sendMessage("§c[IRCanary] IRC admins reloaded");
 42                return true;
 43            }
 44            return false;
 45        }
 46
 47        @Override
 48        public void onDisconnect(Player player) {
 49            if (IRCanary.this.allowJoinQuitUpdates) {
 50                this.plugin.sendIRCMessage(player.getName() + " left the server");
 51            }
 52        }
 53
 54        @Override
 55        public void onKick(Player admin, Player player, String reason) {
 56            if (IRCanary.this.allowKickBanUpdates) {
 57                this.plugin.sendIRCMessage(IRCanary.this.kickMessage.replace("%admin", admin.getName()).replace("%player", player.getName()).replace("%reason", reason));
 58            }
 59        }
 60
 61        @Override
 62        public void onLogin(Player player) {
 63            if (IRCanary.this.allowJoinQuitUpdates) {
 64                this.plugin.sendIRCMessage(player.getName() + " logged in");
 65            }
 66        }
 67    }
 68
 69    private ArrayList<IRCAdmin> admins;
 70    private boolean allowJoinQuitUpdates;
 71    private boolean allowKickBanUpdates;
 72    private String banMessage;
 73    private IRCBot bot;
 74    private String botAuthLine;
 75    private boolean botEnabled;
 76    private int characterLimit;
 77    private char commandPrefix;
 78    private boolean debugMode;
 79    private boolean echoIRCMessages;
 80    private String ircChannel;
 81    private String[] ircNameSeparator;
 82    private String ircUserColor;
 83    private String kickMessage;
 84    private final IRCPluginListener listener = new IRCPluginListener(this);
 85    private Logger log;
 86    private boolean msgCommandRequired;
 87    private String nickname;
 88    private String serverHost;
 89    private int serverPort;
 90    private final Object syncAdmins = new Object();
 91    private final String version = "0.9.9";
 92
 93    @Override
 94    public void disable() {
 95        if (this.bot != null) {
 96            this.botEnabled = false;
 97            this.bot.disconnect();
 98        }
 99    }
100
101    @Override
102    public void enable() {
103        this.log = Logger.getLogger("Minecraft");
104        final File dir = new File("IRCanary");
105        if (!dir.exists()) {
106            dir.mkdir();
107        }
108        try {
109            final PropertiesFile ircProperties = new PropertiesFile("IRCanary/config.properties");
110            this.serverHost = ircProperties.getString("server-host", "localhost");
111            this.serverPort = ircProperties.getInt("server-port", 6667);
112            this.nickname = ircProperties.getString("bot-nickname", "aMinecraftBot");
113            this.ircChannel = ircProperties.getString("irc-channel", "#minecraftbot");
114            this.ircUserColor = ircProperties.getString("irc-usercolor", "f");
115            this.ircNameSeparator = ircProperties.getString("irc-separator", "<,>").split(",");
116            this.characterLimit = ircProperties.getInt("charlimit", 390);
117            this.msgCommandRequired = ircProperties.getBoolean("msg-command-required", false);
118            this.echoIRCMessages = ircProperties.getBoolean("repeat-relay-back", false);
119            this.debugMode = ircProperties.getBoolean("debug-spam-mode", false);
120            this.botAuthLine = ircProperties.getString("auth-message", "");
121            this.commandPrefix = ircProperties.getString("irc-command-prefix", ".").charAt(0);
122            this.allowJoinQuitUpdates = ircProperties.getBoolean("send-join-quit-to-IRC", true);
123            this.allowKickBanUpdates = ircProperties.getBoolean("send-kick-ban-to-IRC", true);
124            this.kickMessage = ircProperties.getString("kick-formatting", "%player kicked (\"%reason%\")");
125            this.banMessage = ircProperties.getString("ban-formatting", "%player banned (\"%reason%\")");
126        } catch (final Exception e) {
127            this.log.log(Level.SEVERE, "[IRCanary] Exception while reading from irc.properties", e);
128        }
129        this.botEnabled = true;
130        this.bot = new IRCBot(this.nickname, this.msgCommandRequired, this.characterLimit, this.ircUserColor, this.echoIRCMessages, this.ircNameSeparator, this, this.commandPrefix);
131        if (this.debugMode) {
132            this.bot.setVerbose(true);
133        }
134        this.resetBot();
135        this.loadAdmins();
136        this.log.log(Level.INFO, "[IRCanary] Version " + this.version + " enabled!");
137    }
138
139    @Override
140    public void initialize() {
141        etc.getLoader().addListener(PluginLoader.Hook.CHAT, this.listener, this, PluginListener.Priority.MEDIUM);
142        etc.getLoader().addListener(PluginLoader.Hook.COMMAND, this.listener, this, PluginListener.Priority.MEDIUM);
143        etc.getLoader().addListener(PluginLoader.Hook.LOGIN, this.listener, this, PluginListener.Priority.MEDIUM);
144        etc.getLoader().addListener(PluginLoader.Hook.DISCONNECT, this.listener, this, PluginListener.Priority.MEDIUM);
145        etc.getLoader().addListener(PluginLoader.Hook.KICK, this.listener, this, PluginListener.Priority.MEDIUM);
146        etc.getLoader().addListener(PluginLoader.Hook.BAN, this.listener, this, PluginListener.Priority.MEDIUM);
147    }
148
149    private void loadAdmins() {
150        final String adminFileName = "IRCanary/IRCAdmins.txt";
151        if (!new File(adminFileName).exists()) {
152            FileWriter writer = null;
153            try {
154                writer = new FileWriter(adminFileName);
155                writer.write("#Add IRC admins here.\r\n");
156                writer.write("#The format is:\r\n");
157                writer.write("#NAME:PASSWORD:ACCESSLEVEL\r\n");
158                writer.write("#Access levels: 2=kick,ban 3=everything");
159                writer.write("#Example:\r\n");
160                writer.write("#notch:iminurbox:3\r\n");
161            } catch (final Exception e) {
162                this.log.log(Level.SEVERE, "[IRCanary] Exception while creating " + adminFileName, e);
163                try {
164                    if (writer != null) {
165                        writer.close();
166                    }
167                } catch (final IOException e2) {
168                    this.log.log(Level.SEVERE, "[IRCanary] Exception while closing writer for " + adminFileName, e);
169                }
170            } finally {
171                try {
172                    if (writer != null) {
173                        writer.close();
174                    }
175                } catch (final IOException e) {
176                    this.log.log(Level.SEVERE, "[IRCanary] Exception while closing writer for " + adminFileName, e);
177                }
178            }
179        }
180        synchronized (this.syncAdmins) {
181            this.admins = new ArrayList<IRCAdmin>();
182            try {
183                final Scanner scanner = new Scanner(new File(adminFileName));
184                while (scanner.hasNextLine()) {
185                    final String line = scanner.nextLine();
186                    if ((line.startsWith("#")) || (line.equals("")) || (line.startsWith(""))) {
187                        continue;
188                    }
189                    final String[] split = line.split(":");
190                    if (split.length != 3) {
191                        continue;
192                    }
193                    final IRCAdmin admin = new IRCAdmin(split[0], split[1], Integer.parseInt(split[2]));
194                    this.admins.add(admin);
195                }
196                scanner.close();
197            } catch (final Exception e) {
198                this.log.log(Level.SEVERE, "[IRCanary] Exception while reading " + adminFileName + " (Are you sure you formatted it correctly?)", e);
199            }
200        }
201    }
202
203    private void sendIRCMessage(String message) {
204        this.bot.sendMessage(this.ircChannel, message);
205    }
206
207    protected boolean authenticate(String sender, String name, String pass, String host) {
208        boolean success = false;
209        synchronized (this.syncAdmins) {
210            for (final IRCAdmin admin : this.admins) {
211                if ((admin != null) && (admin.getUsername().equalsIgnoreCase(name)) && (admin.auth(pass, host))) {
212                    this.log.log(Level.INFO, "[IRCanary] IRC admin " + admin.getUsername() + " logged in  (" + host + ")");
213                    success = true;
214                } else {
215                    this.log.log(Level.INFO, "[IRCanary] IRC admin failed login. user[" + name + "] pass[" + pass + "] nick[" + sender + "] host[" + host + "]");
216                }
217            }
218        }
219        return success;
220    }
221
222    protected boolean ircCommandAttempt(String host, String[] command) {
223        int lvl = 0;
224        String adminName = "";
225        synchronized (this.syncAdmins) {
226            for (final IRCAdmin admin : this.admins) {
227                if ((admin != null) && (admin.getHostname().equals(host))) {
228                    lvl = admin.getAccessLevel();
229                    adminName = admin.getUsername();
230                }
231            }
232        }
233        if (command[0].charAt(0) == this.commandPrefix) {
234            command[0] = command[0].substring(1);
235        }
236        if ((lvl == 0) || ((lvl == 2) && (!command[0].equalsIgnoreCase("kick")) && (!command[0].equalsIgnoreCase("ban")))) {
237            return false;
238        }
239        final String commands = etc.combineSplit(0, command, " ");
240        if (etc.getInstance().parseConsoleCommand(commands, etc.getMCServer())) {
241            return true;
242        }
243        etc.getServer().useConsoleCommand(commands);
244        this.log.log(Level.INFO, "[IRCanary] IRC admin " + adminName + "(" + host + ") used command: " + commands);
245        return true;
246    }
247
248    protected void resetBot() {
249        if (this.botEnabled) {
250            this.bot.disconnect();
251            System.out.println("[IRCanary] Joining " + this.ircChannel + " on " + this.serverHost + ":" + this.serverPort + " as " + this.nickname);
252            try {
253                this.bot.connect(this.serverHost, this.serverPort);
254            } catch (final Exception e) {
255                e.printStackTrace();
256            }
257            if (!this.botAuthLine.equals("")) {
258                final String[] split = this.botAuthLine.split(" ");
259                if (split.length > 1) {
260                    final String to = split[0];
261                    final String msg = etc.combineSplit(1, split, " ");
262                    this.bot.sendMessage(to, msg);
263                }
264            }
265            this.bot.joinChannel(this.ircChannel);
266            this.bot.sendMessage(this.ircChannel, "Never fear, a minecraft bot is here!");
267        }
268    }
269
270}