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