1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-10-03 01:21:56 +02:00

Replacing martyr with Pircbot 1.5

This commit is contained in:
snowleo
2011-10-12 05:00:36 +02:00
parent 860d446d28
commit 9312871250
157 changed files with 4166 additions and 10946 deletions

View File

@@ -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());
}
}
}
}