diff options
author | snowleo <schneeleo@gmail.com> | 2011-10-12 05:00:36 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-10-12 05:00:36 +0200 |
commit | 93128712506bb0d4d422553b0480195cfd2cc9b3 (patch) | |
tree | b43500e52a607ceca0781062ac17570fdf1d4ab7 /EssentialsUpdate/src/com/earth2me | |
parent | 860d446d28776ec842fa53e8e08538d4e093d6e9 (diff) | |
download | Essentials-93128712506bb0d4d422553b0480195cfd2cc9b3.tar Essentials-93128712506bb0d4d422553b0480195cfd2cc9b3.tar.gz Essentials-93128712506bb0d4d422553b0480195cfd2cc9b3.tar.lz Essentials-93128712506bb0d4d422553b0480195cfd2cc9b3.tar.xz Essentials-93128712506bb0d4d422553b0480195cfd2cc9b3.zip |
Replacing martyr with Pircbot 1.5
Diffstat (limited to 'EssentialsUpdate/src/com/earth2me')
-rw-r--r-- | EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java | 267 | ||||
-rw-r--r-- | EssentialsUpdate/src/com/earth2me/essentials/update/IrcBot.java | 163 |
2 files changed, 235 insertions, 195 deletions
diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java b/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java index 5b134b8b8..d70d01a01 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/EssentialsHelp.java @@ -1,29 +1,11 @@ package com.earth2me.essentials.update; -import f00f.net.irc.martyr.GenericAutoService; -import f00f.net.irc.martyr.IRCConnection; -import f00f.net.irc.martyr.InCommand; -import f00f.net.irc.martyr.State; -import f00f.net.irc.martyr.clientstate.Channel; -import f00f.net.irc.martyr.clientstate.Member; -import f00f.net.irc.martyr.commands.InviteCommand; -import f00f.net.irc.martyr.commands.KickCommand; -import f00f.net.irc.martyr.commands.MessageCommand; -import f00f.net.irc.martyr.commands.NoticeCommand; -import f00f.net.irc.martyr.commands.QuitCommand; -import f00f.net.irc.martyr.commands.TopicCommand; -import f00f.net.irc.martyr.errors.GenericJoinError; -import f00f.net.irc.martyr.services.AutoJoin; -import f00f.net.irc.martyr.services.AutoReconnect; -import f00f.net.irc.martyr.services.AutoRegister; -import f00f.net.irc.martyr.services.AutoResponder; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; -import java.util.Enumeration; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; @@ -39,17 +21,16 @@ import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; +import org.jibble.pircbot.User; public class EssentialsHelp extends PlayerListener { private transient Player chatUser; - private transient IRCConnection connection; - private transient AutoReconnect autoReconnect; - private transient boolean shouldQuit = false; private final transient Server server; private final transient Plugin plugin; private final static Charset UTF8 = Charset.forName("utf-8"); + private transient IrcBot ircBot; public EssentialsHelp(Plugin plugin) { @@ -71,7 +52,7 @@ public class EssentialsHelp extends PlayerListener if (chatUser == null) { chatUser = (Player)sender; - connection = null; + ircBot = null; sender.sendMessage("You will be connected to the Essentials Help Chat."); sender.sendMessage("All your chat messages will be forwarded to the channel. You can't chat with other players on your server while in help chat, but you can use commands."); sender.sendMessage("Please be patient, if noone is available, check back later."); @@ -92,62 +73,69 @@ public class EssentialsHelp extends PlayerListener public void onDisable() { - if (autoReconnect != null && connection != null) + if ( ircBot != null) { - autoReconnect.disable(); - shouldQuit = true; - connection.disconnect(); + ircBot.quit(); + ircBot = null; } } - private void sendChatMessage(final Player player, final String message) + private boolean sendChatMessage(final Player player, final String message) { final String messageCleaned = message.trim(); if (messageCleaned.isEmpty()) { - return; + return false; } - if (connection == null) + if (ircBot == null) { if (messageCleaned.equalsIgnoreCase("yes")) { player.sendMessage("Connecting..."); connectToIRC(player); + return true; } if (messageCleaned.equalsIgnoreCase("no") || message.equalsIgnoreCase("!quit")) { chatUser = null; + return true; } + return false; } else { + if (ircBot.isKicked()) { + chatUser = null; + ircBot.quit(); + ircBot = null; + return false; + } final String lowMessage = messageCleaned.toLowerCase(); if (lowMessage.startsWith("!quit")) { chatUser = null; - autoReconnect.disable(); - shouldQuit = true; - connection.sendCommand(new QuitCommand("Connection closed by user.")); + if (ircBot != null) { + ircBot.quit(); + ircBot = null; + } player.sendMessage("Connection closed."); - return; + return true; } - if (!connection.getClientState().getChannels().hasMoreElements()) + if (!ircBot.isConnected() || ircBot.getChannels().length == 0) { - player.sendMessage("Not connected yet!"); - return; + return false; } if (lowMessage.startsWith("!list")) { - final Enumeration members = ((Channel)connection.getClientState().getChannels().nextElement()).getMembers(); + final User[] members = ircBot.getUsers(); final StringBuilder sb = new StringBuilder(); - while (members.hasMoreElements()) + for (User user : members) { if (sb.length() > 0) { sb.append("§f, "); } - final Member member = (Member)members.nextElement(); - if (member.hasOps() || member.hasVoice()) + if (user.isOp() || user.hasVoice()) { sb.append("§6"); } @@ -155,10 +143,10 @@ public class EssentialsHelp extends PlayerListener { sb.append("§7"); } - sb.append(member.getNick()); + sb.append(user.getPrefix()).append(user.getNick()); } player.sendMessage(sb.toString()); - return; + return true; } if (lowMessage.startsWith("!help")) { @@ -168,37 +156,33 @@ public class EssentialsHelp extends PlayerListener player.sendMessage("!config - Sends your Essentials config to the chat."); player.sendMessage("!list - List all players in chat."); player.sendMessage("!quit - Leave chat."); - return; + return true; } if (lowMessage.startsWith("!errors")) { sendErrors(); - return; + return true; } if (lowMessage.startsWith("!startup")) { sendStartup(); - return; + return true; } if (lowMessage.startsWith("!config")) { sendConfig(); - return; + return true; } - final Channel channel = (Channel)connection.getClientState().getChannels().nextElement(); - connection.sendCommand(new MessageCommand(channel.getName(), messageCleaned)); - chatUser.sendMessage("§6" + connection.getClientState().getNick().getNick() + ": §7" + messageCleaned); + ircBot.sendMessage(messageCleaned); + chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + messageCleaned); + return true; } } - private void connectToIRC(final Player player) + private String buildIrcName() { - connection = new IRCConnection(); - // Required services - new AutoResponder(connection); - int versionNumber = 0; final StringBuilder nameBuilder = new StringBuilder(); - nameBuilder.append(player.getName()); + nameBuilder.append(chatUser.getName()); final Matcher versionMatch = Pattern.compile("git-Bukkit-([0-9]+).([0-9]+).([0-9]+)-[0-9]+-[0-9a-z]+-b([0-9]+)jnks.*").matcher(server.getVersion()); if (versionMatch.matches()) @@ -268,28 +252,12 @@ public class EssentialsHelp extends PlayerListener nameBuilder.append(perm.getDescription().getVersion()); } - new AutoRegister(connection, "Ess_" + player.getName(), "esshelp", nameBuilder.toString()); - - autoReconnect = new AutoReconnect(connection); - new KickAutoJoin(connection, "#essentials"); - - new IRCListener(connection); - autoReconnect.go("irc.esper.net", 6667); + return nameBuilder.toString(); } - private void handleIRCmessage(final String nick, final String message) + private void connectToIRC(final Player player) { - - if (chatUser != null) - { - final StringBuilder sb = new StringBuilder(); - sb.append("§6"); - sb.append(nick); - sb.append(": §7"); - final String coloredmessage = message.replace("\u000300", "§f").replace("\u000301", "§0").replace("\u000302", "§1").replace("\u000303", "§2").replace("\u000304", "§c").replace("\u000305", "§4").replace("\u000306", "§5").replace("\u000307", "§6").replace("\u000308", "§e").replace("\u000309", "§a").replace("\u00030", "§f").replace("\u000310", "§b").replace("\u000311", "§f").replace("\u000312", "§9").replace("\u000313", "§d").replace("\u000314", "§8").replace("\u000315", "§7").replace("\u00031", "§0").replace("\u00032", "§1").replace("\u00033", "§2").replace("\u00034", "§c").replace("\u00035", "§4").replace("\u00036", "§5").replace("\u00037", "§6").replace("\u00038", "§e").replace("\u00039", "§a").replace("\u0003", "§7"); - sb.append(coloredmessage); - chatUser.sendMessage(sb.toString()); - } + ircBot = new IrcBot(player, "Ess_" + player.getName(), buildIrcName()); } private void sendErrors() @@ -312,7 +280,7 @@ public class EssentialsHelp extends PlayerListener FileInputStream fis = new FileInputStream(logFile); if (logFile.length() > 1000000) { - fis.skip(logFile.length()-1000000); + fis.skip(logFile.length() - 1000000); } page = new BufferedReader(new InputStreamReader(fis)); final StringBuilder input = new StringBuilder(); @@ -320,19 +288,20 @@ public class EssentialsHelp extends PlayerListener Pattern pattern = Pattern.compile("^[0-9 :-]+\\[INFO\\].*"); while ((line = page.readLine()) != null) { - if (!pattern.matcher(line).matches()) { + if (!pattern.matcher(line).matches()) + { input.append(line).append("\n"); } } - if (input.length()>10000) { - input.delete(0, input.length()-10000); + if (input.length() > 10000) + { + input.delete(0, input.length() - 10000); } final PastieUpload pastie = new PastieUpload(); final String url = pastie.send(input.toString()); - final Channel channel = (Channel)connection.getClientState().getChannels().nextElement(); String message = "Errors: " + url; - chatUser.sendMessage("§6" + connection.getClientState().getNick().getNick() + ": §7" + message); - connection.sendCommand(new MessageCommand(channel.getName(), message)); + chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + message); + ircBot.sendMessage(message); } catch (IOException ex) { @@ -354,7 +323,7 @@ public class EssentialsHelp extends PlayerListener } } } - + private void sendStartup() { BufferedReader page = null; @@ -375,7 +344,7 @@ public class EssentialsHelp extends PlayerListener FileInputStream fis = new FileInputStream(logFile); if (logFile.length() > 1000000) { - fis.skip(logFile.length()-1000000); + fis.skip(logFile.length() - 1000000); } page = new BufferedReader(new InputStreamReader(fis)); final StringBuilder input = new StringBuilder(); @@ -385,28 +354,32 @@ public class EssentialsHelp extends PlayerListener boolean log = false; while ((line = page.readLine()) != null) { - if (patternStart.matcher(line).matches()) { - if (input.length() > 0) { + if (patternStart.matcher(line).matches()) + { + if (input.length() > 0) + { input.delete(0, input.length()); } log = true; } - if (log) { + if (log) + { input.append(line).append("\n"); } - if (patternEnd.matcher(line).matches()) { + if (patternEnd.matcher(line).matches()) + { log = false; } } - if (input.length()>10000) { - input.delete(0, input.length()-10000); + if (input.length() > 10000) + { + input.delete(0, input.length() - 10000); } final PastieUpload pastie = new PastieUpload(); final String url = pastie.send(input.toString()); - final Channel channel = (Channel)connection.getClientState().getChannels().nextElement(); String message = "Startup: " + url; - chatUser.sendMessage("§6" + connection.getClientState().getNick().getNick() + ": §7" + message); - connection.sendCommand(new MessageCommand(channel.getName(), message)); + chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + message); + ircBot.sendMessage(message); } catch (IOException ex) { @@ -455,10 +428,9 @@ public class EssentialsHelp extends PlayerListener } final PastieUpload pastie = new PastieUpload(); final String url = pastie.send(input.toString()); - final Channel channel = (Channel)connection.getClientState().getChannels().nextElement(); String message = "Essentials config.yml: " + url; - chatUser.sendMessage("§6" + connection.getClientState().getNick().getNick() + ": §7" + message); - connection.sendCommand(new MessageCommand(channel.getName(), message)); + chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + message); + ircBot.sendMessage(message); } catch (IOException ex) @@ -487,8 +459,8 @@ public class EssentialsHelp extends PlayerListener { if (event.getPlayer() == chatUser) { - sendChatMessage(event.getPlayer(), event.getMessage()); - event.setCancelled(true); + boolean success = sendChatMessage(event.getPlayer(), event.getMessage()); + event.setCancelled(success); return; } } @@ -497,105 +469,10 @@ public class EssentialsHelp extends PlayerListener public void onPlayerQuit(PlayerQuitEvent event) { chatUser = null; - if (autoReconnect != null) - { - autoReconnect.disable(); - } - shouldQuit = true; - if (connection != null) - { - connection.sendCommand(new QuitCommand("Connection closed by user.")); + if (ircBot != null) { + ircBot.quit(); + ircBot = null; } return; } - - - class KickAutoJoin extends AutoJoin - { - private String channel; - - public KickAutoJoin(IRCConnection connection, String channel) - { - super(connection, channel); - this.channel = channel; - } - - @Override - protected void updateCommand(InCommand command_o) - { - if (command_o instanceof KickCommand) - { - final KickCommand kickCommand = (KickCommand)command_o; - - if (kickCommand.kickedUs(getConnection().getClientState())) - { - if (Channel.areEqual(kickCommand.getChannel(), channel)) - { - chatUser.sendMessage("You have been kicked from the channel: " + kickCommand.getComment()); - chatUser = null; - autoReconnect.disable(); - shouldQuit = true; - connection.sendCommand(new QuitCommand("Connection closed by user.")); - } - } - } - else if (command_o instanceof GenericJoinError) - { - GenericJoinError joinErr = (GenericJoinError)command_o; - - if (Channel.areEqual(joinErr.getChannel(), channel)) - { - scheduleJoin(); - } - } - else if (command_o instanceof InviteCommand) - { - InviteCommand invite = (InviteCommand)command_o; - if (!getConnection().getClientState().isOnChannel(invite.getChannel())) - { - performJoin(); - } - } - } - } - - - class IRCListener extends GenericAutoService - { - public IRCListener(final IRCConnection connection) - { - super(connection); - enable(); - } - - @Override - protected void updateState(final State state) - { - if (state == State.UNCONNECTED && shouldQuit) - { - connection = null; - shouldQuit = false; - } - } - - @Override - protected void updateCommand(final InCommand command) - { - if (command instanceof MessageCommand) - { - final MessageCommand msg = (MessageCommand)command; - EssentialsHelp.this.handleIRCmessage(msg.getSource().getNick(), msg.getMessage()); - } - if (command instanceof TopicCommand) - { - final TopicCommand msg = (TopicCommand)command; - EssentialsHelp.this.handleIRCmessage(msg.getChannel(), msg.getTopic()); - } - if (command instanceof NoticeCommand) - { - final NoticeCommand msg = (NoticeCommand)command; - EssentialsHelp.this.handleIRCmessage(msg.getFrom().getNick(), msg.getNotice()); - } - } - } } diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/IrcBot.java b/EssentialsUpdate/src/com/earth2me/essentials/update/IrcBot.java new file mode 100644 index 000000000..ce6205474 --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/IrcBot.java @@ -0,0 +1,163 @@ +package com.earth2me.essentials.update; + +import java.io.IOException; +import java.util.Enumeration; +import java.util.logging.Level; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.jibble.pircbot.Colors; +import org.jibble.pircbot.IrcException; +import org.jibble.pircbot.PircBot; +import org.jibble.pircbot.User; + + +public class IrcBot extends PircBot +{ + private static final String channel = "#essentials"; + private static final int port = 6667; + private static final String server = "irc.esper.net"; + private transient boolean reconnect = true; + private transient Player player; + private transient boolean kicked = false; + + public IrcBot(Player player, final String nickName, final String versionString) + { + this.player = player; + setName(nickName); + setLogin("esshelp"); + setVersion(versionString); + connect(); + joinChannel(channel); + } + + private void connect() + { + try + { + connect(server, port); + return; + } + catch (IOException ex) + { + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); + } + catch (IrcException ex) + { + Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex); + } + + } + + public void quit() + { + reconnect = false; + disconnect(); + } + + @Override + protected void onConnect() + { + reconnect = true; + } + + @Override + protected void onDisconnect() + { + super.onDisconnect(); + if (reconnect) + { + connect(); + } + } + + @Override + protected void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason) + { + if (recipientNick.equals(getNick())) + { + player.sendMessage("You have been kicked from the channel: " + reason); + quit(); + kicked = true; + } + } + + public boolean isKicked() + { + return kicked; + } + + @Override + protected void onMessage(String channel, String sender, String login, String hostname, String message) + { + player.sendMessage(formatChatMessage(sender, message, false)); + } + + @Override + protected void onAction(String sender, String login, String hostname, String target, String action) + { + player.sendMessage(formatChatMessage(sender, action, true)); + } + + @Override + protected void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice) + { + player.sendMessage(formatChatMessage(sourceNick, notice, false)); + } + + @Override + protected void onTopic(String channel, String topic, String setBy, long date, boolean changed) + { + player.sendMessage(formatChatMessage(channel, topic, false)); + } + + public String formatChatMessage(String nick, String message, boolean action) + { + final StringBuilder sb = new StringBuilder(); + sb.append("§6"); + if (action) + { + sb.append('*'); + } + sb.append(nick); + if (!action) + { + sb.append(':'); + } + sb.append(" §7"); + sb.append(replaceColors(message)); + return sb.toString(); + } + + private String replaceColors(String message) + { + String m = Colors.removeFormatting(message); + m = m.replaceAll("\u000310(,(0?[0-9]|1[0-5]))?", "§b"); + m = m.replaceAll("\u000311(,(0?[0-9]|1[0-5]))?", "§f"); + m = m.replaceAll("\u000312(,(0?[0-9]|1[0-5]))?", "§9"); + m = m.replaceAll("\u000313(,(0?[0-9]|1[0-5]))?", "§d"); + m = m.replaceAll("\u000314(,(0?[0-9]|1[0-5]))?", "§8"); + m = m.replaceAll("\u000315(,(0?[0-9]|1[0-5]))?", "§7"); + m = m.replaceAll("\u00030?1(,(0?[0-9]|1[0-5]))?", "§0"); + m = m.replaceAll("\u00030?2(,(0?[0-9]|1[0-5]))?", "§1"); + m = m.replaceAll("\u00030?3(,(0?[0-9]|1[0-5]))?", "§2"); + m = m.replaceAll("\u00030?4(,(0?[0-9]|1[0-5]))?", "§c"); + m = m.replaceAll("\u00030?5(,(0?[0-9]|1[0-5]))?", "§4"); + m = m.replaceAll("\u00030?6(,(0?[0-9]|1[0-5]))?", "§5"); + m = m.replaceAll("\u00030?7(,(0?[0-9]|1[0-5]))?", "§6"); + m = m.replaceAll("\u00030?8(,(0?[0-9]|1[0-5]))?", "§e"); + m = m.replaceAll("\u00030?9(,(0?[0-9]|1[0-5]))?", "§a"); + m = m.replaceAll("\u00030?0(,(0?[0-9]|1[0-5]))?", "§f"); + m = Colors.removeColors(m); + return m; + } + + public void sendMessage(String message) + { + sendMessage(channel, message); + } + + public User[] getUsers() + { + return getUsers(channel); + } +} |