Added EssentialsXMPP from newplugins branch.

Updated to work with trunk.

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1564 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
snowleo 2011-06-02 13:24:26 +00:00
parent ba86a338c2
commit 2d038579ba
19 changed files with 1991 additions and 4 deletions

View File

@ -650,6 +650,13 @@ is divided into following sections:
<param name="call.target" value="jar"/>
<param name="transfer.built-jar.properties" value="${built-jar.properties}"/>
</antcall>
<antcall target="-maybe-call-dep">
<param name="call.built.properties" value="${built-jar.properties}"/>
<param location="${project.EssentialsXMPP}" name="call.subproject"/>
<param location="${project.EssentialsXMPP}/build.xml" name="call.script"/>
<param name="call.target" value="jar"/>
<param name="transfer.built-jar.properties" value="${built-jar.properties}"/>
</antcall>
</target>
<target depends="init,-check-automatic-build,-clean-after-automatic-build" name="-verify-automatic-build"/>
<target depends="init" name="-check-automatic-build">
@ -1135,6 +1142,13 @@ is divided into following sections:
<param name="call.target" value="clean"/>
<param name="transfer.built-clean.properties" value="${built-clean.properties}"/>
</antcall>
<antcall target="-maybe-call-dep">
<param name="call.built.properties" value="${built-clean.properties}"/>
<param location="${project.EssentialsXMPP}" name="call.subproject"/>
<param location="${project.EssentialsXMPP}/build.xml" name="call.script"/>
<param name="call.target" value="clean"/>
<param name="transfer.built-clean.properties" value="${built-clean.properties}"/>
</antcall>
</target>
<target depends="init" name="-do-clean">
<delete dir="${build.dir}"/>

View File

@ -1,8 +1,8 @@
build.xml.data.CRC32=c4188209
build.xml.data.CRC32=51b33957
build.xml.script.CRC32=7a797370
build.xml.stylesheet.CRC32=28e38971@1.43.1.45
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=c4188209
nbproject/build-impl.xml.script.CRC32=73a4865f
nbproject/build-impl.xml.data.CRC32=51b33957
nbproject/build-impl.xml.script.CRC32=7a6cee4f
nbproject/build-impl.xml.stylesheet.CRC32=0c01fd8e@1.43.1.45

View File

@ -35,7 +35,8 @@ javac.classpath=\
${reference.EssentialsGroupManager.jar}:\
${reference.EssentialsProtect.jar}:\
${reference.EssentialsSpawn.jar}:\
${reference.EssentialsGeoIP.jar}
${reference.EssentialsGeoIP.jar}:\
${reference.EssentialsXMPP.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
@ -71,6 +72,7 @@ project.EssentialsGroupBridge=../EssentialsGroupBridge
project.EssentialsGroupManager=../EssentialsGroupManager
project.EssentialsProtect=../EssentialsProtect
project.EssentialsSpawn=../EssentialsSpawn
project.EssentialsXMPP=../EssentialsXMPP
reference.Essentials.jar=${project.Essentials}/dist/Essentials.jar
reference.EssentialsChat.jar=${project.EssentialsChat}/dist/EssentialsChat.jar
reference.EssentialsGeoIP.jar=${project.EssentialsGeoIP}/dist/EssentialsGeoIP.jar
@ -78,6 +80,7 @@ reference.EssentialsGroupBridge.jar=${project.EssentialsGroupBridge}/dist/Essent
reference.EssentialsGroupManager.jar=${project.EssentialsGroupManager}/dist/EssentialsGroupManager.jar
reference.EssentialsProtect.jar=${project.EssentialsProtect}/dist/EssentialsProtect.jar
reference.EssentialsSpawn.jar=${project.EssentialsSpawn}/dist/EssentialsSpawn.jar
reference.EssentialsXMPP.jar=${project.EssentialsXMPP}/dist/original-EssentialsXMPP.jar
run.classpath=\
${javac.classpath}:\
${build.classes.dir}

View File

@ -71,6 +71,14 @@
<clean-target>clean</clean-target>
<id>jar</id>
</reference>
<reference>
<foreign-project>EssentialsXMPP</foreign-project>
<artifact-type>jar</artifact-type>
<script>build.xml</script>
<target>jar</target>
<clean-target>clean</clean-target>
<id>jar</id>
</reference>
</references>
</configuration>
</project>

83
EssentialsXMPP/build.xml Normal file
View File

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="EssentialsXMPP" default="default" basedir=".">
<description>Builds, tests, and runs the project EssentialsXMPP.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar-with-manifest: JAR building (if you are using a manifest)
-do-jar-without-manifest: JAR building (if you are not using a manifest)
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="EssentialsXMPP-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
<target name="-post-jar">
<jar jarfile="${dist.dir}/EssentialsXMPP.jar">
<zipfileset src="${dist.jar}" excludes="META-INF/*" />
<zipfileset src="../lib/smack-3.2.0.jar" excludes="META-INF/*" />
<manifest>
<attribute name="Classpath" value="Essentials.jar"/>
</manifest>
</jar>
</target>
</project>

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
build.xml.data.CRC32=1012a5dd
build.xml.script.CRC32=db2fb22f
build.xml.stylesheet.CRC32=28e38971@1.42.1.45
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=1012a5dd
nbproject/build-impl.xml.script.CRC32=04f5fc92
nbproject/build-impl.xml.stylesheet.CRC32=0c01fd8e@1.43.1.45

View File

@ -0,0 +1,81 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=EssentialsXMPP
application.vendor=snowleo
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.test.classpath=\
${run.test.classpath}
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/original-EssentialsXMPP.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
file.reference.craftbukkit-0.0.1-SNAPSHOT.jar=../lib/craftbukkit-0.0.1-SNAPSHOT.jar
file.reference.smack_3.2.0.b2.jar=../lib/smack_3.2.0.b2.jar
includes=**
jar.compress=false
javac.classpath=\
${file.reference.craftbukkit-0.0.1-SNAPSHOT.jar}:\
${reference.Essentials.jar}:\
${file.reference.smack_3.2.0.b2.jar}
# Space-separated list of extra javac options
javac.compilerargs=-Xlint:unchecked
javac.deprecation=false
javac.processorpath=\
${javac.classpath}
javac.source=1.6
javac.target=1.6
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}:\
${libs.junit.classpath}:\
${libs.junit_4.classpath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
project.Essentials=../Essentials
reference.Essentials.jar=${project.Essentials}/dist/Essentials.jar
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project
# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value
# or test-sys-prop.name=value to set system properties for unit tests):
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>EssentialsXMPP</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
<libraries xmlns="http://www.netbeans.org/ns/ant-project-libraries/1">
<definitions>../lib/nblibraries.properties</definitions>
</libraries>
<references xmlns="http://www.netbeans.org/ns/ant-project-references/1">
<reference>
<foreign-project>Essentials</foreign-project>
<artifact-type>jar</artifact-type>
<script>build.xml</script>
<target>jar</target>
<clean-target>clean</clean-target>
<id>jar</id>
</reference>
</references>
</configuration>
</project>

View File

@ -0,0 +1,26 @@
package com.earth2me.essentials.xmpp;
import com.earth2me.essentials.User;
import com.earth2me.essentials.commands.EssentialsCommand;
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
import org.bukkit.Server;
public class Commandsetxmpp extends EssentialsCommand
{
public Commandsetxmpp()
{
super("setxmpp");
}
@Override
protected void run(Server server, User user, String commandLabel, String[] args) throws Exception
{
if (args.length < 1)
{
throw new NotEnoughArgumentsException();
}
EssentialsXMPP.getInstance().setAddress(user, args[0]);
}
}

View File

@ -0,0 +1,39 @@
package com.earth2me.essentials.xmpp;
import com.earth2me.essentials.Console;
import com.earth2me.essentials.commands.EssentialsCommand;
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commandxmpp extends EssentialsCommand
{
public Commandxmpp()
{
super("xmpp");
}
@Override
protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception
{
if (args.length < 2)
{
throw new NotEnoughArgumentsException();
}
final String message = getFinalArg(args, 1);
final String address = EssentialsXMPP.getInstance().getAddress(args[0]);
if (address == null)
{
sender.sendMessage("§cThere are no players matching that name.");
}
else
{
final String senderName = sender instanceof Player ? ess.getUser(sender).getDisplayName() : Console.NAME;
sender.sendMessage("[" + senderName + ">" + address + "] " + message);
EssentialsXMPP.getInstance().sendMessage(address, "[" + senderName + "] " + message);
}
}
}

View File

@ -0,0 +1,46 @@
package com.earth2me.essentials.xmpp;
import com.earth2me.essentials.commands.EssentialsCommand;
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commandxmppspy extends EssentialsCommand
{
public Commandxmppspy()
{
super("xmppspy");
}
@Override
protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception
{
if (args.length < 1)
{
throw new NotEnoughArgumentsException();
}
final List<Player> matches = server.matchPlayer(args[0]);
if (matches.isEmpty())
{
sender.sendMessage("§cThere are no players matching that name.");
}
for (Player p : matches)
{
try
{
final boolean toggle = EssentialsXMPP.getInstance().toggleSpy(p);
sender.sendMessage("XMPP Spy " + (toggle ? "enabled" : "disabled") + " for " + p.getDisplayName());
}
catch (Exception ex)
{
sender.sendMessage("Error: " + ex.getMessage());
}
}
}
}

View File

@ -0,0 +1,115 @@
package com.earth2me.essentials.xmpp;
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.Util;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class EssentialsXMPP extends JavaPlugin implements IEssentialsXMPP
{
private static final Logger LOGGER = Logger.getLogger("Minecraft");
private static EssentialsXMPP instance = null;
private transient UserManager users;
private transient XMPPManager xmpp;
public static IEssentialsXMPP getInstance()
{
return instance;
}
@Override
public void onEnable()
{
instance = this;
final IEssentials ess = Essentials.getStatic();
if (ess == null)
{
LOGGER.log(Level.SEVERE, "Failed to load Essentials before EssentialsXMPP");
}
final PluginManager pluginManager = getServer().getPluginManager();
final EssentialsXMPPPlayerListener playerListener = new EssentialsXMPPPlayerListener(ess);
pluginManager.registerEvent(Type.PLAYER_JOIN, playerListener, Priority.Monitor, this);
pluginManager.registerEvent(Type.PLAYER_CHAT, playerListener, Priority.Monitor, this);
pluginManager.registerEvent(Type.PLAYER_QUIT, playerListener, Priority.Monitor, this);
users = new UserManager(this.getDataFolder());
xmpp = new XMPPManager(this);
ess.addReloadListener(users);
if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion())) {
LOGGER.log(Level.WARNING, Util.i18n("versionMismatchAll"));
}
LOGGER.info(Util.format("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), Essentials.AUTHORS));
}
@Override
public void onDisable()
{
xmpp.disconnect();
}
@Override
public boolean onCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args)
{
return Essentials.getStatic().onCommandEssentials(sender, command, commandLabel, args, EssentialsXMPP.class.getClassLoader(), "com.earth2me.essentials.xmpp.Command");
}
@Override
public void setAddress(final Player user, final String address) throws Exception
{
final String username = user.getName().toLowerCase();
instance.users.setAddress(username, address);
}
@Override
public String getAddress(final String name)
{
return instance.users.getAddress(name);
}
@Override
public boolean toggleSpy(final Player user) throws Exception
{
final String username = user.getName().toLowerCase();
final boolean spy = !instance.users.isSpy(username);
instance.users.setSpy(username, spy);
return spy;
}
@Override
public String getAddress(final Player user)
{
return instance.users.getAddress(user.getName());
}
@Override
public void sendMessage(final Player user, final String message)
{
instance.xmpp.sendMessage(instance.users.getAddress(user.getName()), message);
}
@Override
public void sendMessage(final String address, final String message)
{
instance.xmpp.sendMessage(address, message);
}
@Override
public List<String> getSpyUsers()
{
return instance.users.getSpyUsers();
}
}

View File

@ -0,0 +1,55 @@
package com.earth2me.essentials.xmpp;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerQuitEvent;
class EssentialsXMPPPlayerListener extends PlayerListener
{
private final transient IEssentials ess;
EssentialsXMPPPlayerListener(final IEssentials ess)
{
super();
this.ess = ess;
}
@Override
public void onPlayerJoin(final PlayerJoinEvent event)
{
final User user = ess.getUser(event.getPlayer());
sendMessageToSpyUsers("Player " + user.getDisplayName() + " joined the game");
}
@Override
public void onPlayerChat(final PlayerChatEvent event)
{
final User user = ess.getUser(event.getPlayer());
sendMessageToSpyUsers(String.format(event.getFormat(), user.getDisplayName(), event.getMessage()));
}
@Override
public void onPlayerQuit(final PlayerQuitEvent event)
{
final User user = ess.getUser(event.getPlayer());
sendMessageToSpyUsers("Player " + user.getDisplayName() + " left the game");
}
private void sendMessageToSpyUsers(final String message)
{
try
{
for (String address : EssentialsXMPP.getInstance().getSpyUsers())
{
EssentialsXMPP.getInstance().sendMessage(address, message);
}
}
catch (Exception ex)
{
}
}
}

View File

@ -0,0 +1,78 @@
package com.earth2me.essentials.xmpp;
import com.earth2me.essentials.EssentialsConf;
import com.earth2me.essentials.IConf;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserManager implements IConf
{
private final transient EssentialsConf users;
private final transient List<String> spyusers = new ArrayList<String>();
private final static String ADDRESS = "address";
private final static String SPY = "spy";
public UserManager(final File folder)
{
users = new EssentialsConf(new File(folder, "users.yml"));
reloadConfig();
}
public final boolean isSpy(final String username)
{
return users.getBoolean(username.toLowerCase() + "." + SPY, false);
}
public void setSpy(final String username, boolean spy) throws Exception
{
setUser(username.toLowerCase(), getAddress(username), spy);
}
public final String getAddress(final String username)
{
return users.getString(username.toLowerCase() + "." + ADDRESS, null);
}
public void setAddress(final String username, final String address) throws Exception
{
setUser(username.toLowerCase(), address, isSpy(username));
}
public List<String> getSpyUsers()
{
return spyusers;
}
private void setUser(String username, String address, boolean spy) throws Exception
{
final Map<String, Object> userdata = new HashMap<String, Object>();
userdata.put(ADDRESS, address);
userdata.put(SPY, spy);
users.setProperty(username, userdata);
users.save();
reloadConfig();
}
@Override
public final void reloadConfig()
{
users.load();
spyusers.clear();
final List<String> keys = users.getKeys(null);
for (String key : keys)
{
if (isSpy(key))
{
final String address = getAddress(key);
if (address != null)
{
spyusers.add(address);
}
}
}
}
}

View File

@ -0,0 +1,283 @@
package com.earth2me.essentials.xmpp;
import com.earth2me.essentials.EssentialsConf;
import com.earth2me.essentials.IConf;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster.SubscriptionMode;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
public class XMPPManager extends Handler implements MessageListener, ChatManagerListener, IConf
{
private static final Logger LOGGER = Logger.getLogger("Minecraft");
private final transient EssentialsConf config;
private transient XMPPConnection connection;
private transient ChatManager chatManager;
private final transient Map<String, Chat> chats = Collections.synchronizedMap(new HashMap<String, Chat>());
private final transient JavaPlugin parent;
private transient List<String> logUsers;
private transient Level logLevel;
private transient boolean ignoreLagMessages = true;
public XMPPManager(final JavaPlugin parent)
{
super();
this.parent = parent;
config = new EssentialsConf(new File(parent.getDataFolder(), "config.yml"));
config.setTemplateName("/config.yml", EssentialsXMPP.class);
reloadConfig();
}
public void sendMessage(final String address, final String message)
{
Chat chat = null;
try
{
if (address == null || address.isEmpty())
{
return;
}
startChat(address);
chat = chats.get(address);
if (chat == null)
{
return;
}
chat.sendMessage(message.replaceAll("§[0-9a-f]", ""));
}
catch (XMPPException ex)
{
if (chat != null)
{
chat.removeMessageListener(this);
chats.remove(address);
LOGGER.log(Level.WARNING, "Failed to send xmpp message.", ex);
}
}
}
@Override
public void processMessage(final Chat chat, final Message msg)
{
final String message = msg.getBody();
if (message.length() > 0)
{
switch (message.charAt(0))
{
case '@':
sendPrivateMessage(chat, message);
break;
case '/':
sendCommand(chat, message);
break;
default:
parent.getServer().broadcastMessage("<XMPP:" + chat.getParticipant() + "> " + message);
}
}
}
private void connect()
{
final String server = config.getString("xmpp.server");
if (server == null)
{
LOGGER.log(Level.WARNING, "config broken for xmpp");
return;
}
final int port = config.getInt("xmpp.port", 5222);
final String serviceName = config.getString("xmpp.servicename", server);
final String xmppuser = config.getString("xmpp.user");
final String password = config.getString("xmpp.password");
final ConnectionConfiguration cc = new ConnectionConfiguration(server, port, serviceName);
final StringBuilder sb = new StringBuilder();
sb.append("Connecting to xmpp server ").append(server).append(":").append(port);
sb.append(" as user ").append(xmppuser).append(".");
LOGGER.log(Level.INFO, sb.toString());
cc.setSASLAuthenticationEnabled(config.getBoolean("xmpp.sasl-enabled", false));
cc.setSendPresence(true);
cc.setReconnectionAllowed(true);
connection = new XMPPConnection(cc);
try
{
connection.connect();
connection.login(xmppuser, password);
connection.getRoster().setSubscriptionMode(SubscriptionMode.accept_all);
chatManager = connection.getChatManager();
chatManager.addChatListener(this);
}
catch (XMPPException ex)
{
LOGGER.log(Level.WARNING, "Failed to connect to server: " + server, ex);
}
}
public final void disconnect()
{
if (connection != null)
{
connection.disconnect(new Presence(Presence.Type.unavailable));
}
}
@Override
public void chatCreated(final Chat chat, final boolean createdLocally)
{
if (!createdLocally)
{
chat.addMessageListener(this);
final Chat old = chats.put(chat.getParticipant(), chat);
if (old != null)
{
old.removeMessageListener(this);
}
}
}
@Override
public final void reloadConfig()
{
config.load();
synchronized (chats)
{
disconnect();
chats.clear();
connect();
}
LOGGER.removeHandler(this);
if (config.getBoolean("log-enabled", false))
{
LOGGER.addHandler(this);
logUsers = config.getStringList("log-users", new ArrayList<String>());
final String level = config.getString("log-level", "info");
try
{
logLevel = Level.parse(level.toUpperCase());
}
catch (IllegalArgumentException e)
{
logLevel = Level.INFO;
}
ignoreLagMessages = config.getBoolean("ignore-lag-messages", true);
}
}
@Override
public void publish(final LogRecord logRecord)
{
try
{
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())
{
for (String user : logUsers)
{
startChat(user);
final Chat chat = chats.get(user);
if (chat != null)
{
chat.sendMessage(String.format("[" + logRecord.getLevel().getLocalizedName() + "] " + logRecord.getMessage(), logRecord.getParameters()));
}
}
}
}
catch (Exception e)
{
// Ignore all exception and just print them to the console
// Otherwise we create a loop.
System.out.println(e.getMessage());
e.printStackTrace();
}
}
@Override
public void flush()
{
}
@Override
public void close() throws SecurityException
{
}
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 {
for (Player p : matches)
{
p.sendMessage("[" + chat.getParticipant() + ">" + p.getDisplayName() + "] " + message);
}
}
}
}
private void sendCommand(final Chat chat, final String message)
{
if (config.getStringList("op-users", new ArrayList<String>()).contains(chat.getParticipant()))
{
final CraftServer craftServer = (CraftServer)parent.getServer();
if (craftServer != null)
{
craftServer.dispatchCommand(new ConsoleCommandSender(craftServer), message.substring(1));
}
}
}
}

View File

@ -0,0 +1,17 @@
xmpp:
server: 'example.com'
user: 'name@example.com'
password: 'password'
# servicename: 'example.com'
# port: 5222
# sasl-enabled: false
op-users:
# - 'name@example.com'
log-enabled: false
# Level is minimum level that should be send: info, warning, severe
log-level: warning
log-users:
# - 'name@example.com'

View File

@ -0,0 +1,20 @@
# This determines the command prefix when there are conflicts (/name:home, /name:help, etc.)
name: EssentialsXMPP
main: com.earth2me.essentials.xmpp.EssentialsXMPP
# Note to developers: This next line cannot change, or the automatic versioning system will break.
version: TeamCity
website: http://www.earth2me.net:8001/
description: Provides xmpp communication.
authors:
- snowleo
depend: [Essentials]
commands:
setxmpp:
description: set your xmpp address
usage: /<command> address
xmpp:
description: send a message to a player
usage: /<command> player message
xmppspy:
description: toggle xmpp spy for all message
usage: /<command> player