Commit 061414b8 authored by Ryan Graham's avatar Ryan Graham Committed by ryang

updated to use UserEventListener rather than PacketInterceptor


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1214 b35dd754-fafc-0310-a699-88a17e54d16e
parent 39a5e528
...@@ -8,37 +8,31 @@ ...@@ -8,37 +8,31 @@
package org.jivesoftware.messenger.plugin; package org.jivesoftware.messenger.plugin;
import java.io.File; import java.io.File;
import java.util.Iterator; import java.util.Map;
import org.dom4j.Element;
import org.jivesoftware.messenger.JiveGlobals; import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.messenger.Session;
import org.jivesoftware.messenger.SessionManager; import org.jivesoftware.messenger.SessionManager;
import org.jivesoftware.messenger.SessionNotFoundException; import org.jivesoftware.messenger.SessionNotFoundException;
import org.jivesoftware.messenger.XMPPServer; import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.container.Plugin; import org.jivesoftware.messenger.container.Plugin;
import org.jivesoftware.messenger.container.PluginManager; import org.jivesoftware.messenger.container.PluginManager;
import org.jivesoftware.messenger.forms.spi.XDataFormImpl; import org.jivesoftware.messenger.event.UserEventDispatcher;
import org.jivesoftware.messenger.interceptor.InterceptorManager; import org.jivesoftware.messenger.event.UserEventListener;
import org.jivesoftware.messenger.interceptor.PacketInterceptor; import org.jivesoftware.messenger.user.User;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.Packet; import org.xmpp.packet.Packet;
public class RegistrationNotificationPlugin implements Plugin { public class RegistrationNotificationPlugin implements Plugin {
private RegistrationInterceptor interceptor;
private PluginManager pluginManager; private PluginManager pluginManager;
private SessionManager sessionManager; private SessionManager sessionManager;
private RegistrationUserEventListener listener = new RegistrationUserEventListener();
private static String serverName; private static String serverName;
private boolean serviceEnabled; private boolean serviceEnabled;
private String contact; private String contact;
public RegistrationNotificationPlugin() { public RegistrationNotificationPlugin() {
interceptor = new RegistrationInterceptor();
sessionManager = SessionManager.getInstance(); sessionManager = SessionManager.getInstance();
serverName = XMPPServer.getInstance().getServerInfo().getName(); serverName = XMPPServer.getInstance().getServerInfo().getName();
...@@ -50,6 +44,8 @@ public class RegistrationNotificationPlugin implements Plugin { ...@@ -50,6 +44,8 @@ public class RegistrationNotificationPlugin implements Plugin {
contact = "admin"; contact = "admin";
JiveGlobals.setProperty("registration.notification.contact", contact); JiveGlobals.setProperty("registration.notification.contact", contact);
} }
UserEventDispatcher.addListener(listener);
} }
public String getName() { public String getName() {
...@@ -76,7 +72,7 @@ public class RegistrationNotificationPlugin implements Plugin { ...@@ -76,7 +72,7 @@ public class RegistrationNotificationPlugin implements Plugin {
} }
public void destroyPlugin() { public void destroyPlugin() {
InterceptorManager.getInstance().removeInterceptor(interceptor); UserEventDispatcher.removeListener(listener);
pluginManager = null; pluginManager = null;
sessionManager = null; sessionManager = null;
} }
...@@ -84,12 +80,6 @@ public class RegistrationNotificationPlugin implements Plugin { ...@@ -84,12 +80,6 @@ public class RegistrationNotificationPlugin implements Plugin {
public void setServiceEnabled(boolean enable) { public void setServiceEnabled(boolean enable) {
serviceEnabled = enable; serviceEnabled = enable;
JiveGlobals.setProperty("registration.notification.enabled", serviceEnabled ? "true" : "false"); JiveGlobals.setProperty("registration.notification.enabled", serviceEnabled ? "true" : "false");
if (enable) {
InterceptorManager.getInstance().addInterceptor(interceptor);
} else {
InterceptorManager.getInstance().removeInterceptor(interceptor);
}
} }
public boolean serviceEnabled() { public boolean serviceEnabled() {
...@@ -104,56 +94,29 @@ public class RegistrationNotificationPlugin implements Plugin { ...@@ -104,56 +94,29 @@ public class RegistrationNotificationPlugin implements Plugin {
public String getContact() { public String getContact() {
return contact; return contact;
} }
private void interceptRegistration(Packet packet) { //TODO JM-170
if (packet instanceof IQ) { //TODO add the ability to have a admin configurable messange sent to newly registered user
IQ iqPacket = (IQ) packet; //TODO add the ability for the admin to monitor when users are created and/or deleted?
private class RegistrationUserEventListener implements UserEventListener {
if (IQ.Type.set.equals(iqPacket.getType())) { public void userCreated(User user, Map params) {
Element childElement = iqPacket.getChildElement(); String msg = " A new user with the username of '" + user.getUsername() + "' just registered";
String namespace = null; try {
if (childElement != null) { sessionManager.sendServerMessage(new JID(getContact() + "@" + serverName),
namespace = childElement.getNamespaceURI(); "Registration Notification",
} msg);
if ("jabber:iq:register".equals(namespace)) {
//this is similiar to the logic used in IQRegisterHandler
XDataFormImpl registrationForm = null;
Element iqElement = iqPacket.getChildElement();
Element formElement = iqElement.element("x");
String username = null;;
// Check if a form was used to provide the registration info
if (formElement != null) {
Iterator<String> values = registrationForm.getField("username").getValues();
username = (values.hasNext() ? values.next() : " ");
}
else {
// Get the registration info from the query elements
username = iqElement.elementText("username");
}
String msg = " A new user with the username of '" + username + "' just attempted to register";
try {
sessionManager.sendServerMessage(new JID(getContact() + "@" + serverName),
"Registration Notification",
msg);
}
catch (SessionNotFoundException e) {
Log.error("SessionNotFoundException: could not send the following message to: "
+ getContact()
+ msg);
}
}
} }
} catch (SessionNotFoundException e) {
} Log.error("SessionNotFoundException: could not send the following message to: "
+ getContact()
private class RegistrationInterceptor implements PacketInterceptor { + msg);
public void interceptPacket(Packet packet, Session session, boolean read, boolean processed) {
if (serviceEnabled()) {
interceptRegistration(packet);
} }
} }
}
public void userDeleting(User user, Map params) {
}
public void userModified(User user, Map params) {
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment