mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-02-11 20:00:46 +00:00
add XMPP presence management stack
This commit is contained in:
parent
12bf4e9dcf
commit
601bdbff46
1 changed files with 365 additions and 337 deletions
|
@ -1,5 +1,6 @@
|
||||||
package com.earth2me.essentials.xmpp;
|
package com.earth2me.essentials.xmpp;
|
||||||
|
|
||||||
|
import com.earth2me.essentials.IEssentials;
|
||||||
import com.earth2me.essentials.Console;
|
import com.earth2me.essentials.Console;
|
||||||
import com.earth2me.essentials.EssentialsConf;
|
import com.earth2me.essentials.EssentialsConf;
|
||||||
import com.earth2me.essentials.IConf;
|
import com.earth2me.essentials.IConf;
|
||||||
|
@ -12,369 +13,396 @@ import java.util.logging.Level;
|
||||||
import java.util.logging.LogRecord;
|
import java.util.logging.LogRecord;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.logging.SimpleFormatter;
|
import java.util.logging.SimpleFormatter;
|
||||||
|
import org.bukkit.Server;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.jivesoftware.smack.*;
|
import org.jivesoftware.smack.*;
|
||||||
import org.jivesoftware.smack.Roster.SubscriptionMode;
|
import org.jivesoftware.smack.Roster.SubscriptionMode;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.packet.Presence;
|
import org.jivesoftware.smack.packet.Presence;
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
|
||||||
public class XMPPManager extends Handler implements MessageListener, ChatManagerListener, IConf
|
public class XMPPManager extends Handler implements MessageListener, ChatManagerListener, IConf
|
||||||
{
|
{
|
||||||
private static final Logger LOGGER = Logger.getLogger("Minecraft");
|
private static final Logger LOGGER = Logger.getLogger("Minecraft");
|
||||||
private static final SimpleFormatter formatter = new SimpleFormatter();
|
private static final SimpleFormatter formatter = new SimpleFormatter();
|
||||||
private final transient EssentialsConf config;
|
private final transient EssentialsConf config;
|
||||||
private transient XMPPConnection connection;
|
private transient XMPPConnection connection;
|
||||||
private transient ChatManager chatManager;
|
private transient ChatManager chatManager;
|
||||||
private final transient Map<String, Chat> chats = Collections.synchronizedMap(new HashMap<String, Chat>());
|
private final transient Map<String, Chat> chats = Collections.synchronizedMap(new HashMap<String, Chat>());
|
||||||
private final transient Set<LogRecord> logrecords = Collections.synchronizedSet(new HashSet<LogRecord>());
|
private final transient Set<LogRecord> logrecords = Collections.synchronizedSet(new HashSet<LogRecord>());
|
||||||
private final transient IEssentialsXMPP parent;
|
private final transient IEssentialsXMPP parent;
|
||||||
private transient List<String> logUsers;
|
private transient List<String> logUsers;
|
||||||
private transient Level logLevel;
|
private transient Level logLevel;
|
||||||
private transient boolean ignoreLagMessages = true;
|
private transient boolean ignoreLagMessages = true;
|
||||||
private transient Thread loggerThread;
|
private transient Thread loggerThread;
|
||||||
private transient boolean threadrunning = true;
|
private transient boolean threadrunning = true;
|
||||||
|
|
||||||
public XMPPManager(final IEssentialsXMPP parent)
|
public XMPPManager(final IEssentialsXMPP parent)
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
config = new EssentialsConf(new File(parent.getDataFolder(), "config.yml"));
|
config = new EssentialsConf(new File(parent.getDataFolder(), "config.yml"));
|
||||||
config.setTemplateName("/config.yml", EssentialsXMPP.class);
|
config.setTemplateName("/config.yml", EssentialsXMPP.class);
|
||||||
reloadConfig();
|
reloadConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean sendMessage(final String address, final String message)
|
public boolean sendMessage(final String address, final String message)
|
||||||
{
|
{
|
||||||
if (address != null && !address.isEmpty())
|
if (address != null && !address.isEmpty())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
startChat(address);
|
startChat(address);
|
||||||
final Chat chat;
|
final Chat chat;
|
||||||
synchronized (chats)
|
synchronized (chats)
|
||||||
{
|
{
|
||||||
chat = chats.get(address);
|
chat = chats.get(address);
|
||||||
}
|
}
|
||||||
if (chat != null)
|
if (chat != null)
|
||||||
{
|
{
|
||||||
if (!connection.isConnected())
|
if (!connection.isConnected())
|
||||||
{
|
{
|
||||||
disconnect();
|
disconnect();
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
chat.sendMessage(FormatUtil.stripFormat(message));
|
chat.sendMessage(FormatUtil.stripFormat(message));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (XMPPException ex)
|
catch (XMPPException ex)
|
||||||
{
|
{
|
||||||
disableChat(address);
|
disableChat(address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void processMessage(final Chat chat, final Message msg)
|
public void processMessage(final Chat chat, final Message msg)
|
||||||
{
|
{
|
||||||
// Normally we should log the error message
|
// Normally we should log the error message
|
||||||
// But we would create a loop if the connection to a log-user fails.
|
// But we would create a loop if the connection to a log-user fails.
|
||||||
if (msg.getType() != Message.Type.error && msg.getBody().length() > 0)
|
if (msg.getType() != Message.Type.error && msg.getBody().length() > 0)
|
||||||
{
|
{
|
||||||
final String message = msg.getBody();
|
final String message = msg.getBody();
|
||||||
switch (message.charAt(0))
|
switch (message.charAt(0))
|
||||||
{
|
{
|
||||||
case '@':
|
case '@':
|
||||||
sendPrivateMessage(chat, message);
|
sendPrivateMessage(chat, message);
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
sendCommand(chat, message);
|
sendCommand(chat, message);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
final IUser sender = parent.getUserByAddress(StringUtils.parseBareAddress(chat.getParticipant()));
|
final IUser sender = parent.getUserByAddress(StringUtils.parseBareAddress(chat.getParticipant()));
|
||||||
parent.broadcastMessage(sender, "=" + sender.getBase().getDisplayName() + ": " + message, StringUtils.parseBareAddress(chat.getParticipant()));
|
parent.broadcastMessage(sender, "=" + sender.getBase().getDisplayName() + ": " + message, StringUtils.parseBareAddress(chat.getParticipant()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean connect()
|
private boolean connect()
|
||||||
{
|
{
|
||||||
final String server = config.getString("xmpp.server");
|
final String server = config.getString("xmpp.server");
|
||||||
if (server == null || server.equals("example.com"))
|
if (server == null || server.equals("example.com"))
|
||||||
{
|
{
|
||||||
LOGGER.log(Level.WARNING, "config broken for xmpp");
|
LOGGER.log(Level.WARNING, "config broken for xmpp");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
final int port = config.getInt("xmpp.port", 5222);
|
final int port = config.getInt("xmpp.port", 5222);
|
||||||
final String serviceName = config.getString("xmpp.servicename", server);
|
final String serviceName = config.getString("xmpp.servicename", server);
|
||||||
final String xmppuser = config.getString("xmpp.user");
|
final String xmppuser = config.getString("xmpp.user");
|
||||||
final String password = config.getString("xmpp.password");
|
final String password = config.getString("xmpp.password");
|
||||||
final ConnectionConfiguration connConf = new ConnectionConfiguration(server, port, serviceName);
|
final ConnectionConfiguration connConf = new ConnectionConfiguration(server, port, serviceName);
|
||||||
final StringBuilder stringBuilder = new StringBuilder();
|
final StringBuilder stringBuilder = new StringBuilder();
|
||||||
stringBuilder.append("Connecting to xmpp server ").append(server).append(":").append(port);
|
stringBuilder.append("Connecting to xmpp server ").append(server).append(":").append(port);
|
||||||
stringBuilder.append(" as user ").append(xmppuser).append(".");
|
stringBuilder.append(" as user ").append(xmppuser).append(".");
|
||||||
LOGGER.log(Level.INFO, stringBuilder.toString());
|
LOGGER.log(Level.INFO, stringBuilder.toString());
|
||||||
connConf.setSASLAuthenticationEnabled(config.getBoolean("xmpp.sasl-enabled", false));
|
connConf.setSASLAuthenticationEnabled(config.getBoolean("xmpp.sasl-enabled", false));
|
||||||
connConf.setSendPresence(true);
|
connConf.setSendPresence(true);
|
||||||
connConf.setReconnectionAllowed(true);
|
connConf.setReconnectionAllowed(true);
|
||||||
connConf.setDebuggerEnabled(config.getBoolean("debug", false));
|
connConf.setDebuggerEnabled(config.getBoolean("debug", false));
|
||||||
connection = new XMPPConnection(connConf);
|
connection = new XMPPConnection(connConf);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
connection.connect();
|
connection.connect();
|
||||||
connection.login(xmppuser, password);
|
|
||||||
connection.getRoster().setSubscriptionMode(SubscriptionMode.accept_all);
|
|
||||||
chatManager = connection.getChatManager();
|
|
||||||
chatManager.addChatListener(this);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (XMPPException ex)
|
|
||||||
{
|
|
||||||
LOGGER.log(Level.WARNING, "Failed to connect to server: " + server, ex);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void disconnect()
|
connection.login(xmppuser, password, "Essentials-XMPP");
|
||||||
{
|
connection.sendPacket(new Presence(Presence.Type.available, "No one online.", 2, Presence.Mode.available));
|
||||||
if (loggerThread != null)
|
|
||||||
{
|
|
||||||
loggerThread.interrupt();
|
|
||||||
}
|
|
||||||
if (chatManager != null)
|
|
||||||
{
|
|
||||||
chatManager.removeChatListener(this);
|
|
||||||
chatManager = null;
|
|
||||||
}
|
|
||||||
if (connection != null)
|
|
||||||
{
|
|
||||||
connection.disconnect(new Presence(Presence.Type.unavailable));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
connection.getRoster().setSubscriptionMode(SubscriptionMode.accept_all);
|
||||||
|
chatManager = connection.getChatManager();
|
||||||
|
chatManager.addChatListener(this);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (XMPPException ex)
|
||||||
|
{
|
||||||
|
LOGGER.log(Level.WARNING, "Failed to connect to server: " + server, ex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
public final void disconnect()
|
||||||
public void chatCreated(final Chat chat, final boolean createdLocally)
|
{
|
||||||
{
|
if (loggerThread != null)
|
||||||
if (!createdLocally)
|
{
|
||||||
{
|
loggerThread.interrupt();
|
||||||
chat.addMessageListener(this);
|
}
|
||||||
final Chat old = chats.put(StringUtils.parseBareAddress(chat.getParticipant()), chat);
|
if (chatManager != null)
|
||||||
if (old != null)
|
{
|
||||||
{
|
chatManager.removeChatListener(this);
|
||||||
old.removeMessageListener(this);
|
chatManager = null;
|
||||||
}
|
}
|
||||||
}
|
if (connection != null)
|
||||||
}
|
{
|
||||||
|
connection.disconnect(new Presence(Presence.Type.unavailable));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
}
|
||||||
public final void reloadConfig()
|
|
||||||
{
|
|
||||||
LOGGER.removeHandler(this);
|
|
||||||
config.load();
|
|
||||||
synchronized (chats)
|
|
||||||
{
|
|
||||||
disconnect();
|
|
||||||
chats.clear();
|
|
||||||
if (!connect())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
startLoggerThread();
|
|
||||||
}
|
|
||||||
if (config.getBoolean("log-enabled", false))
|
|
||||||
{
|
|
||||||
LOGGER.addHandler(this);
|
|
||||||
logUsers = config.getStringList("log-users");
|
|
||||||
final String level = config.getString("log-level", "info");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
logLevel = Level.parse(level.toUpperCase(Locale.ENGLISH));
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException e)
|
|
||||||
{
|
|
||||||
logLevel = Level.INFO;
|
|
||||||
}
|
|
||||||
ignoreLagMessages = config.getBoolean("ignore-lag-messages", true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public final void updatePresence()
|
||||||
public void publish(final LogRecord logRecord)
|
{
|
||||||
{
|
final int usercount;
|
||||||
try
|
final StringBuilder stringBuilder = new StringBuilder();
|
||||||
{
|
|
||||||
if (ignoreLagMessages && logRecord.getMessage().equals("Can't keep up! Did the system time change, or is the server overloaded?"))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (logRecord.getLevel().intValue() >= logLevel.intValue())
|
|
||||||
{
|
|
||||||
synchronized (logrecords)
|
|
||||||
{
|
|
||||||
logrecords.add(logRecord);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
// Ignore all exceptions
|
|
||||||
// Otherwise we create a loop.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
usercount = parent.getServer().getOnlinePlayers().length;
|
||||||
public void flush()
|
|
||||||
{
|
|
||||||
// Ignore this
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
if (usercount == 0) {
|
||||||
public void close() throws SecurityException
|
final String presenceMsg = "No one online.";
|
||||||
{
|
connection.sendPacket(new Presence(Presence.Type.available, presenceMsg, 2, Presence.Mode.available));
|
||||||
// Ignore this
|
}
|
||||||
}
|
if (usercount == 1) {
|
||||||
|
final String presenceMsg = "1 player online.";
|
||||||
|
connection.sendPacket(new Presence(Presence.Type.available, presenceMsg, 2, Presence.Mode.dnd));
|
||||||
|
}
|
||||||
|
if (usercount > 1) {
|
||||||
|
stringBuilder.append(usercount).append(" players online.");
|
||||||
|
connection.sendPacket(new Presence(Presence.Type.available, stringBuilder.toString(), 2, Presence.Mode.dnd));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void startLoggerThread()
|
@Override
|
||||||
{
|
public void chatCreated(final Chat chat, final boolean createdLocally)
|
||||||
loggerThread = new Thread(new Runnable()
|
{
|
||||||
{
|
if (!createdLocally)
|
||||||
@Override
|
{
|
||||||
public void run()
|
chat.addMessageListener(this);
|
||||||
{
|
final Chat old = chats.put(StringUtils.parseBareAddress(chat.getParticipant()), chat);
|
||||||
final Set<LogRecord> copy = new HashSet<LogRecord>();
|
if (old != null)
|
||||||
final Set<String> failedUsers = new HashSet<String>();
|
{
|
||||||
while (threadrunning)
|
old.removeMessageListener(this);
|
||||||
{
|
}
|
||||||
synchronized (logrecords)
|
}
|
||||||
{
|
}
|
||||||
if (!logrecords.isEmpty())
|
|
||||||
{
|
|
||||||
copy.addAll(logrecords);
|
|
||||||
logrecords.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!copy.isEmpty())
|
|
||||||
{
|
|
||||||
for (String user : logUsers)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
XMPPManager.this.startChat(user);
|
|
||||||
for (LogRecord logRecord : copy)
|
|
||||||
{
|
|
||||||
final String message = formatter.format(logRecord);
|
|
||||||
if (!XMPPManager.this.sendMessage(user, FormatUtil.stripLogColorFormat(message)))
|
|
||||||
{
|
|
||||||
failedUsers.add(user);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
@Override
|
||||||
}
|
public final void reloadConfig()
|
||||||
catch (XMPPException ex)
|
{
|
||||||
{
|
LOGGER.removeHandler(this);
|
||||||
failedUsers.add(user);
|
config.load();
|
||||||
LOGGER.removeHandler(XMPPManager.this);
|
synchronized (chats)
|
||||||
LOGGER.log(Level.SEVERE, "Failed to deliver log message! Disabling logging to XMPP.", ex);
|
{
|
||||||
}
|
disconnect();
|
||||||
}
|
chats.clear();
|
||||||
logUsers.removeAll(failedUsers);
|
if (!connect())
|
||||||
if (logUsers.isEmpty())
|
{
|
||||||
{
|
return;
|
||||||
LOGGER.removeHandler(XMPPManager.this);
|
}
|
||||||
threadrunning = false;
|
startLoggerThread();
|
||||||
}
|
}
|
||||||
copy.clear();
|
if (config.getBoolean("log-enabled", false))
|
||||||
}
|
{
|
||||||
try
|
LOGGER.addHandler(this);
|
||||||
{
|
logUsers = config.getStringList("log-users");
|
||||||
Thread.sleep(2000);
|
final String level = config.getString("log-level", "info");
|
||||||
}
|
try
|
||||||
catch (InterruptedException ex)
|
{
|
||||||
{
|
logLevel = Level.parse(level.toUpperCase(Locale.ENGLISH));
|
||||||
threadrunning = false;
|
}
|
||||||
}
|
catch (IllegalArgumentException e)
|
||||||
}
|
{
|
||||||
LOGGER.removeHandler(XMPPManager.this);
|
logLevel = Level.INFO;
|
||||||
}
|
}
|
||||||
});
|
ignoreLagMessages = config.getBoolean("ignore-lag-messages", true);
|
||||||
loggerThread.start();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startChat(final String address) throws XMPPException
|
@Override
|
||||||
{
|
public void publish(final LogRecord logRecord)
|
||||||
if (chatManager == null)
|
{
|
||||||
{
|
try
|
||||||
return;
|
{
|
||||||
}
|
if (ignoreLagMessages && logRecord.getMessage().equals("Can't keep up! Did the system time change, or is the server overloaded?"))
|
||||||
synchronized (chats)
|
{
|
||||||
{
|
return;
|
||||||
if (!chats.containsKey(address))
|
}
|
||||||
{
|
if (logRecord.getLevel().intValue() >= logLevel.intValue())
|
||||||
final Chat chat = chatManager.createChat(address, this);
|
{
|
||||||
if (chat == null)
|
synchronized (logrecords)
|
||||||
{
|
{
|
||||||
throw new XMPPException("Could not start Chat with " + address);
|
logrecords.add(logRecord);
|
||||||
}
|
}
|
||||||
chats.put(address, chat);
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (Exception e)
|
||||||
}
|
{
|
||||||
|
// Ignore all exceptions
|
||||||
|
// Otherwise we create a loop.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void sendPrivateMessage(final Chat chat, final String message)
|
@Override
|
||||||
{
|
public void flush()
|
||||||
final String[] parts = message.split(" ", 2);
|
{
|
||||||
if (parts.length == 2)
|
// Ignore this
|
||||||
{
|
}
|
||||||
final List<Player> matches = parent.getServer().matchPlayer(parts[0].substring(1));
|
|
||||||
|
|
||||||
if (matches.isEmpty())
|
@Override
|
||||||
{
|
public void close() throws SecurityException
|
||||||
try
|
{
|
||||||
{
|
// Ignore this
|
||||||
chat.sendMessage("User " + parts[0] + " not found");
|
}
|
||||||
}
|
|
||||||
catch (XMPPException ex)
|
|
||||||
{
|
|
||||||
LOGGER.log(Level.WARNING, "Failed to send xmpp message.", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
final String from = "[" + parent.getUserByAddress(StringUtils.parseBareAddress(chat.getParticipant())) + ">";
|
|
||||||
for (Player p : matches)
|
|
||||||
{
|
|
||||||
p.sendMessage(from + p.getDisplayName() + "] " + message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendCommand(final Chat chat, final String message)
|
private void startLoggerThread()
|
||||||
{
|
{
|
||||||
if (config.getStringList("op-users").contains(StringUtils.parseBareAddress(chat.getParticipant())))
|
loggerThread = new Thread(new Runnable()
|
||||||
{
|
{
|
||||||
try
|
@Override
|
||||||
{
|
public void run()
|
||||||
parent.getServer().dispatchCommand(Console.getCommandSender(parent.getServer()), message.substring(1));
|
{
|
||||||
}
|
final Set<LogRecord> copy = new HashSet<LogRecord>();
|
||||||
catch (Exception ex)
|
final Set<String> failedUsers = new HashSet<String>();
|
||||||
{
|
while (threadrunning)
|
||||||
LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
|
{
|
||||||
}
|
synchronized (logrecords)
|
||||||
}
|
{
|
||||||
}
|
if (!logrecords.isEmpty())
|
||||||
|
{
|
||||||
|
copy.addAll(logrecords);
|
||||||
|
logrecords.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!copy.isEmpty())
|
||||||
|
{
|
||||||
|
for (String user : logUsers)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
XMPPManager.this.startChat(user);
|
||||||
|
for (LogRecord logRecord : copy)
|
||||||
|
{
|
||||||
|
final String message = formatter.format(logRecord);
|
||||||
|
if (!XMPPManager.this.sendMessage(user, FormatUtil.stripLogColorFormat(message)))
|
||||||
|
{
|
||||||
|
failedUsers.add(user);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
private void disableChat(final String address)
|
}
|
||||||
{
|
}
|
||||||
final Chat chat = chats.get(address);
|
catch (XMPPException ex)
|
||||||
if (chat != null)
|
{
|
||||||
{
|
failedUsers.add(user);
|
||||||
chat.removeMessageListener(this);
|
LOGGER.removeHandler(XMPPManager.this);
|
||||||
chats.remove(address);
|
LOGGER.log(Level.SEVERE, "Failed to deliver log message! Disabling logging to XMPP.", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logUsers.removeAll(failedUsers);
|
||||||
|
if (logUsers.isEmpty())
|
||||||
|
{
|
||||||
|
LOGGER.removeHandler(XMPPManager.this);
|
||||||
|
threadrunning = false;
|
||||||
|
}
|
||||||
|
copy.clear();
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Thread.sleep(2000);
|
||||||
|
}
|
||||||
|
catch (InterruptedException ex)
|
||||||
|
{
|
||||||
|
threadrunning = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOGGER.removeHandler(XMPPManager.this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
loggerThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startChat(final String address) throws XMPPException
|
||||||
|
{
|
||||||
|
if (chatManager == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
synchronized (chats)
|
||||||
|
{
|
||||||
|
if (!chats.containsKey(address))
|
||||||
|
{
|
||||||
|
final Chat chat = chatManager.createChat(address, this);
|
||||||
|
if (chat == null)
|
||||||
|
{
|
||||||
|
throw new XMPPException("Could not start Chat with " + address);
|
||||||
|
}
|
||||||
|
chats.put(address, chat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendPrivateMessage(final Chat chat, final String message)
|
||||||
|
{
|
||||||
|
final String[] parts = message.split(" ", 2);
|
||||||
|
if (parts.length == 2)
|
||||||
|
{
|
||||||
|
final List<Player> matches = parent.getServer().matchPlayer(parts[0].substring(1));
|
||||||
|
|
||||||
|
if (matches.isEmpty())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
chat.sendMessage("User " + parts[0] + " not found");
|
||||||
|
}
|
||||||
|
catch (XMPPException ex)
|
||||||
|
{
|
||||||
|
LOGGER.log(Level.WARNING, "Failed to send xmpp message.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
final String from = "[" + parent.getUserByAddress(StringUtils.parseBareAddress(chat.getParticipant())) + ">";
|
||||||
|
for (Player p : matches)
|
||||||
|
{
|
||||||
|
p.sendMessage(from + p.getDisplayName() + "] " + message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendCommand(final Chat chat, final String message)
|
||||||
|
{
|
||||||
|
if (config.getStringList("op-users").contains(StringUtils.parseBareAddress(chat.getParticipant())))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
parent.getServer().dispatchCommand(Console.getCommandSender(parent.getServer()), message.substring(1));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void disableChat(final String address)
|
||||||
|
{
|
||||||
|
final Chat chat = chats.get(address);
|
||||||
|
if (chat != null)
|
||||||
|
{
|
||||||
|
chat.removeMessageListener(this);
|
||||||
|
chats.remove(address);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue