/src/IRCBot.java

https://github.com/mbax/IRCanary · Java · 115 lines · 105 code · 10 blank · 0 comment · 34 complexity · e2d94d57d05e560f95e86817b22b241c MD5 · raw file

  1. import java.util.logging.Logger;
  2. import org.jibble.pircbot.PircBot;
  3. public class IRCBot extends PircBot {
  4. private final char commandPrefix;
  5. private final int ircCharLim;
  6. private final boolean ircEcho;
  7. private final String[] ircSeparator;
  8. private final String ircUserColor;
  9. private final Logger log;
  10. private final boolean msgCommandRequired;
  11. private final IRCanary plugin;
  12. public IRCBot(String mah_name, boolean msgenabled, int charlim, String usercolor, boolean echo, String[] sep, IRCanary ip, char startCommand) {
  13. this.setName(mah_name);
  14. this.setAutoNickChange(true);
  15. this.msgCommandRequired = msgenabled;
  16. this.ircCharLim = charlim;
  17. this.ircUserColor = usercolor;
  18. this.ircEcho = echo;
  19. this.ircSeparator = sep;
  20. this.log = Logger.getLogger("Minecraft");
  21. this.plugin = ip;
  22. this.commandPrefix = startCommand;
  23. }
  24. private boolean addMsg(String thenewmsg, String theuser) {
  25. final String combined = this.ircSeparator[0] + "§" + this.ircUserColor + theuser + "§f" + this.ircSeparator[1] + " " + thenewmsg;
  26. if (combined.length() > this.ircCharLim) {
  27. return false;
  28. }
  29. this.log.info("IRC <" + theuser + "> " + thenewmsg);
  30. for (final Player p : etc.getServer().getPlayerList()) {
  31. if (p != null) {
  32. p.sendMessage(combined);
  33. }
  34. }
  35. return true;
  36. }
  37. private void messageToGame(String channel, String sender, String message) {
  38. if (this.addMsg(message, sender)) {
  39. if (this.ircEcho) {
  40. this.sendMessage(channel, "[IRC] <" + sender + "> " + message);
  41. }
  42. } else {
  43. this.sendMessage(channel, sender + ": Your message was too long. The limit's " + this.ircCharLim + " characters");
  44. }
  45. }
  46. @Override
  47. protected void onDisconnect() {
  48. this.plugin.resetBot();
  49. }
  50. @Override
  51. protected void onMessage(String channel, String sender, String login, String hostname, String message) {
  52. if (message.charAt(0) == '!') {
  53. final String[] split = message.split(" ");
  54. if (message.equalsIgnoreCase("!help")) {
  55. this.sendMessage(channel, sender + ": I am here to set you free.");
  56. } else if (message.equalsIgnoreCase("!players")) {
  57. int currentPlayersCount = 0;
  58. final StringBuilder currentPlayers = new StringBuilder();
  59. for (final Player player : etc.getServer().getPlayerList()) {
  60. if (player != null) {
  61. if (currentPlayers.length() != 0) {
  62. currentPlayers.append(", ");
  63. }
  64. currentPlayersCount++;
  65. }
  66. }
  67. if (currentPlayers.length() == 0) {
  68. this.sendMessage(channel, "No players online.");
  69. } else {
  70. this.sendMessage(channel, "Players (" + currentPlayersCount + "/" + etc.getInstance().getPlayerLimit() + "):" + currentPlayers.toString());
  71. }
  72. } else if ((this.msgCommandRequired) && (split[0].equalsIgnoreCase("!msg"))) {
  73. final String messageToSend = etc.combineSplit(1, split, " ");
  74. this.messageToGame(channel, sender, messageToSend);
  75. }
  76. return;
  77. }
  78. if (message.charAt(0) == this.commandPrefix) {
  79. if (this.plugin.ircCommandAttempt(hostname, message.split(" "))) {
  80. this.sendMessage(sender, "Done :)");
  81. } else {
  82. this.sendMessage(sender, "You don't have access to that command :(");
  83. }
  84. return;
  85. }
  86. if (!this.msgCommandRequired) {
  87. this.messageToGame(channel, sender, message);
  88. }
  89. }
  90. @Override
  91. protected void onPrivateMessage(String sender, String login, String hostname, String message) {
  92. final String[] split = message.split(" ");
  93. if (split[0].equalsIgnoreCase("auth")) {
  94. if (this.plugin.authenticate(sender, split[1], split[2], hostname)) {
  95. this.sendMessage(sender, "Authenticated :)");
  96. } else {
  97. this.sendMessage(sender, "Authentication failed. Bad username or password");
  98. }
  99. } else if (this.plugin.ircCommandAttempt(hostname, message.split(" "))) {
  100. this.sendMessage(sender, "Done :)");
  101. } else {
  102. this.sendMessage(sender, "You don't have access to that command :(");
  103. }
  104. }
  105. }