use of net.sf.kraken.session.TransportSession in project Openfire by igniterealtime.
the class RegistrationHandler method addNewRegistration.
/**
* Adds a registration with this transport, or updates an existing one.
*
* @param jid JID of user to add registration to.
* @param username Legacy username of registration.
* @param password Legacy password of registration.
* @param nickname Legacy nickname of registration.
* @param noRosterItem True if the transport is not to show up in the user's roster.
* @throws UserNotFoundException if registration or roster not found.
* @throws IllegalAccessException if jid is not from this server.
* @throws IllegalArgumentException if username is not valid for this transport type.
*/
public void addNewRegistration(JID jid, String username, String password, String nickname, Boolean noRosterItem) throws UserNotFoundException, IllegalAccessException {
Log.debug("Adding or updating registration for : " + jid.toString() + " / " + username);
if (!XMPPServer.getInstance().isLocal(jid)) {
throw new IllegalAccessException("Domain of jid registering does not match domain of server.");
}
if (!parent.isUsernameValid(username)) {
throw new IllegalArgumentException("Username specified is not valid for this transport type.");
}
final Collection<Registration> registrations = RegistrationManager.getInstance().getRegistrations(jid, parent.transportType);
boolean foundReg = false;
boolean triggerRestart = false;
for (final Registration registration : registrations) {
if (!registration.getUsername().equals(username)) {
Log.debug("Deleting existing registration before" + " creating a new one: " + registration);
RegistrationManager.getInstance().deleteRegistration(registration);
} else {
Log.debug("Existing registration found that can be updated: " + registration);
if ((registration.getPassword() != null && password == null) || (registration.getPassword() == null && password != null) || (registration.getPassword() != null && password != null && !registration.getPassword().equals(password))) {
Log.debug("Updating password for existing registration: " + registration);
registration.setPassword(password);
triggerRestart = true;
}
if ((registration.getNickname() != null && nickname == null) || (registration.getNickname() == null && nickname != null) || (registration.getNickname() != null && nickname != null && !registration.getNickname().equals(nickname))) {
Log.debug("Updating nickname for existing registration: " + registration);
registration.setNickname(nickname);
triggerRestart = true;
}
foundReg = true;
}
// if a change was made to the registration, restart it.
if (triggerRestart) {
try {
Log.debug("An existing registration was " + "updated. Restarting the related session: " + registration);
final TransportSession relatedSession = parent.sessionManager.getSession(registration.getJID().getNode());
parent.registrationLoggedOut(relatedSession);
} catch (NotFoundException e) {
// No worries, move on.
}
}
}
if (!foundReg) {
RegistrationManager.getInstance().createRegistration(jid, parent.transportType, username, password, nickname);
triggerRestart = true;
}
if (triggerRestart) {
Log.debug("Clean up any leftover roster items " + "from other transports for: " + jid);
try {
parent.cleanUpRoster(jid, !noRosterItem);
} catch (UserNotFoundException ee) {
throw new UserNotFoundException("Unable to find roster.");
}
}
if (!noRosterItem) {
try {
Log.debug("Adding Transport roster item to the roster of: " + jid);
parent.addOrUpdateRosterItem(jid, parent.getJID(), parent.getDescription(), "Transports");
} catch (UserNotFoundException e) {
throw new UserNotFoundException("User not registered with server.");
}
} else {
Log.debug("Not adding Transport roster item to the roster of: " + jid + " (as this was explicitly requested).");
}
}
use of net.sf.kraken.session.TransportSession in project Openfire by igniterealtime.
the class ConfigManager method logoutSession.
/**
* Logs out session via the web interface.
*
*
* @param registrationID ID number associated with registration to log off.
* @return registration ID on success, -1 on failure (-1 so that js cb_logoutSession knows which Div to edit)
*/
public Integer logoutSession(Integer registrationID) {
try {
PluginManager pluginManager = XMPPServer.getInstance().getPluginManager();
KrakenPlugin plugin = (KrakenPlugin) pluginManager.getPlugin("kraken");
Registration registration = new Registration(registrationID);
if (!plugin.getTransportInstance(registration.getTransportType().toString()).isEnabled()) {
return -1;
}
JID jid = registration.getJID();
TransportSession session = plugin.getTransportInstance(registration.getTransportType().toString()).getTransport().getSessionManager().getSession(jid);
plugin.getTransportInstance(registration.getTransportType().toString()).getTransport().registrationLoggedOut(session);
return registrationID;
} catch (NotFoundException e) {
return -1;
}
}
use of net.sf.kraken.session.TransportSession in project Openfire by igniterealtime.
the class BaseTransport method interceptPacket.
/**
* Intercepts disco items packets to filter out users who aren't allowed to register.
*
* @see org.jivesoftware.openfire.interceptor.PacketInterceptor#interceptPacket(org.xmpp.packet.Packet, org.jivesoftware.openfire.session.Session, boolean, boolean)
*/
@SuppressWarnings("unchecked")
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) {
// If not IQ, return immediately.
if (!(packet instanceof IQ)) {
return;
}
// If it's a result IQ, process for possible filtering.
if (((IQ) packet).getType().equals(IQ.Type.result)) {
// If the packet is not outgoing back to the user or not processed yet, we don't care.
if (processed || incoming) {
return;
}
// If not query, return immediately.
Element child = packet.getElement().element("query");
if (child == null) {
return;
}
// If no namespace uri, return immediately.
if (child.getNamespaceURI() == null) {
return;
}
// If not disco#items, return immediately.
if (!child.getNamespaceURI().equals(NameSpace.DISCO_ITEMS)) {
return;
}
// If the node is null, we don't care, not directly related to a user.
JID to = packet.getTo();
if (to.getNode() == null) {
return;
}
JID from = packet.getFrom();
// If not from server itself, return immediately.
if (!XMPPServer.getInstance().isLocal(from)) {
return;
}
// If user registered, return immediately.
if (RegistrationManager.getInstance().isRegistered(to, transportType)) {
return;
}
// Check if allowed, if so return immediately.
if (permissionManager.hasAccess(to)) {
return;
}
// Filter out item associated with transport.
Iterator iter = child.elementIterator();
while (iter.hasNext()) {
Element elem = (Element) iter.next();
try {
if (elem.attribute("jid").getText().equals(this.jid.toString())) {
child.remove(elem);
}
} catch (Exception e) {
// No worries. Wasn't what we were looking for.
}
}
return;
}
// If it's a set IQ, process for possible roster activity.
if (((IQ) packet).getType().equals(IQ.Type.set)) {
// If the packet is not coming from the user, we don't care.
if (!incoming) {
return;
}
// If not query, return immediately.
Element child = packet.getElement().element("query");
if (child == null) {
return;
}
// If not jabber:iq:roster, return immediately.
if (!child.getNamespaceURI().equals(NameSpace.IQ_ROSTER)) {
return;
}
// Example items in roster modification.
Iterator iter = child.elementIterator();
while (iter.hasNext()) {
Element elem = (Element) iter.next();
if (!elem.getName().equals("item")) {
continue;
}
String jidStr;
String nickname = null;
String sub = null;
ArrayList<String> groups = new ArrayList<String>();
try {
jidStr = elem.attributeValue("jid");
} catch (Exception e) {
// No JID found, we don't want this then.
continue;
}
JID jid = new JID(jidStr);
if (!jid.getDomain().equals(this.getJID().toString())) {
// Not for our domain, moving on.
continue;
}
if (jid.getNode() == null) {
// Gateway itself, don't care.
return;
}
try {
nickname = elem.attributeValue("name");
} catch (Exception e) {
// No nickname, ok then.
}
try {
sub = elem.attributeValue("subscription");
} catch (Exception e) {
// No subscription, no worries.
}
Iterator groupIter = elem.elementIterator();
while (groupIter.hasNext()) {
Element groupElem = (Element) groupIter.next();
if (!groupElem.getName().equals("group")) {
continue;
}
groups.add(groupElem.getText());
}
if (sub != null && sub.equals("remove")) {
try {
TransportSession trSession = sessionManager.getSession(session.getAddress().getNode());
if (!trSession.isRosterLocked(jid.toString())) {
Log.debug(getType().toString() + ": contact delete " + session.getAddress().getNode() + ":" + jid);
trSession.getBuddyManager().removeBuddy(convertJIDToID(jid));
}
} catch (NotFoundException e) {
// Well we just don't care then.
}
} else {
try {
TransportSession trSession = sessionManager.getSession(session.getAddress().getNode());
if (!trSession.isRosterLocked(jid.toString())) {
try {
TransportBuddy buddy = trSession.getBuddyManager().getBuddy(jid);
Log.debug(getType().toString() + ": contact update " + session.getAddress().getNode() + ":" + jid + ":" + nickname + ":" + groups);
buddy.setNicknameAndGroups(nickname, groups);
} catch (NotFoundException e) {
Log.debug(getType().toString() + ": contact add " + session.getAddress().getNode() + ":" + jid + ":" + nickname + ":" + groups);
trSession.addContact(jid, nickname, groups);
}
}
} catch (NotFoundException e) {
// Well we just don't care then.
}
}
}
}
}
use of net.sf.kraken.session.TransportSession in project Openfire by igniterealtime.
the class BaseTransport method sendMessage.
/**
* Sends a simple message through he component manager.
*
* @param to Who the message is for.
* @param from Who the message is from.
* @param msg Message to be send.
* @param type Type of message to be sent.
*/
public void sendMessage(JID to, JID from, String msg, Message.Type type) {
Message m = new Message();
m.setType(type);
m.setFrom(from);
m.setTo(to);
m.setBody(net.sf.kraken.util.StringUtils.removeInvalidXMLCharacters(msg));
if (msg.length() == 0) {
Log.debug("Dropping empty message packet.");
return;
}
if (type.equals(Message.Type.chat) || type.equals(Message.Type.normal)) {
chatStateEventSource.isActive(from, to);
m.addChildElement("active", NameSpace.CHATSTATES);
if (JiveGlobals.getBooleanProperty("plugin.gateway.globsl.messageeventing", true)) {
Element xEvent = m.addChildElement("x", "jabber:x:event");
// xEvent.addElement("id");
xEvent.addElement("composing");
}
} else if (type.equals(Message.Type.error)) {
// Error responses require error elements, even if we aren't going to do it "right" yet
// TODO: All -real- error handling
m.setError(Condition.undefined_condition);
}
try {
TransportSession session = sessionManager.getSession(to);
if (session.getDetachTimestamp() != 0) {
// This is a detached session then, so lets store the packet instead of delivering.
session.storePendingPacket(m);
return;
}
} catch (NotFoundException e) {
// No session? That's "fine", allow it through, it's probably something from the transport itself.
}
sendPacket(m);
}
use of net.sf.kraken.session.TransportSession in project Openfire by igniterealtime.
the class BaseTransport method processPacket.
/**
* Handles all incoming presence stanzas.
*
* @param packet The presence packet to be processed.
* @return list of packets that will be sent back to the presence requester.
*/
private List<Packet> processPacket(Presence packet) {
Log.debug("Received presence packet: " + packet.toXML());
List<Packet> reply = new ArrayList<Packet>();
JID from = packet.getFrom();
JID to = packet.getTo();
if (to == null) {
// Well clearly it's for us or it wouldn't have gotten here...
packet.setTo(getJID());
to = getJID();
}
if (packet.getType() == Presence.Type.error) {
// We don't want to do anything with this. Ignore it.
return reply;
}
try {
if (to.getNode() == null) {
Collection<Registration> registrations = RegistrationManager.getInstance().getRegistrations(from, this.transportType);
if (registrations.isEmpty()) {
// User is not registered with us.
Log.debug("Unable to find registration.");
Presence p = new Presence();
p.setTo(from);
p.setFrom(to);
p.setError(PacketError.Condition.forbidden);
p.setType(Presence.Type.unavailable);
reply.add(p);
return reply;
}
if (JiveGlobals.getBooleanProperty("plugin.gateway." + getType() + ".registrationstrict", false) && !permissionManager.hasAccess(from)) {
Log.debug("Attempt to log in by restricted account: " + from);
Presence p = new Presence();
p.setTo(from);
p.setFrom(to);
p.setError(PacketError.Condition.forbidden);
p.setType(Presence.Type.unavailable);
reply.add(p);
return reply;
}
Registration registration = registrations.iterator().next();
// This packet is to the transport itself.
if (packet.getType() == null) {
// A user's resource has come online.
TransportSession<B> session;
Lock l = CacheFactory.getLock(registration.getJID() + "@" + transportType.toString() + "ns", sessionRouter.sessionLocations);
try {
l.lock();
session = sessionManager.getSession(from);
if (session.hasResource(from.getResource())) {
Log.debug("An existing resource has changed status: " + from);
if (session.getPriority(from.getResource()) != packet.getPriority()) {
session.updatePriority(from.getResource(), packet.getPriority());
}
if (session.isHighestPriority(from.getResource())) {
// Well, this could represent a status change.
session.updateStatus(getPresenceType(packet), packet.getStatus());
}
} else {
Log.debug("A new resource has come online: " + from);
// This is a new resource, lets send them what we know.
session.addResource(from.getResource(), packet.getPriority());
// Tell the new resource what the state of their buddy list is.
session.getBuddyManager().sendAllAvailablePresences(from);
// If this priority is the highest, treat it's status as golden
if (session.isHighestPriority(from.getResource())) {
session.updateStatus(getPresenceType(packet), packet.getStatus());
}
}
// Attach the session
session.attachSession();
} catch (NotFoundException e) {
Log.debug("A new session has come online: " + from);
session = this.registrationLoggedIn(registration, from, getPresenceType(packet), packet.getStatus(), packet.getPriority());
sessionManager.storeSession(from, session);
} finally {
l.unlock();
}
} else if (packet.getType() == Presence.Type.unavailable) {
// A user's resource has gone offline.
TransportSession<B> session;
try {
session = sessionManager.getSession(from);
String resource = from.getResource();
if (session.hasResource(resource)) {
if (session.getResourceCount() > 1) {
// Just one of the resources, lets adjust accordingly.
if (session.isHighestPriority(resource)) {
Log.debug("A high priority resource (of multiple) has gone offline: " + from);
// Ooh, the highest resource went offline, drop to next highest.
session.removeResource(resource);
// Lets ask the next highest resource what it's presence is.
Presence p = new Presence(Presence.Type.probe);
p.setTo(session.getJIDWithHighestPriority());
p.setFrom(this.getJID());
sendPacket(p);
} else {
Log.debug("A low priority resource (of multiple) has gone offline: " + from);
// Meh, lower priority, big whoop.
session.removeResource(resource);
}
} else {
Log.debug("A final resource has gone offline: " + from);
// No more resources, byebye.
this.registrationLoggedOut(session);
sessionManager.removeSession(from);
}
}
} catch (NotFoundException e) {
Log.debug("Ignoring unavailable presence for inactive seession.");
}
} else if (packet.getType() == Presence.Type.probe) {
// Client is asking for presence status.
TransportSession<B> session;
try {
session = sessionManager.getSession(from);
session.sendPresence(from);
} catch (NotFoundException e) {
Log.debug("Ignoring probe presence for inactive session.");
}
} else {
Log.debug("Ignoring this packet:" + packet.toString());
// Anything else we will ignore for now.
}
} else {
// This packet is to a user at the transport.
try {
TransportSession<B> session = sessionManager.getSession(from);
if (packet.getType() == Presence.Type.probe) {
// Presence probe, lets try to answer appropriately.
if (session.isLoggedIn()) {
try {
TransportBuddy buddy = session.getBuddyManager().getBuddy(to);
buddy.sendPresence(from);
} catch (NotFoundException e) {
// User was not found so send an error presence
Presence p = new Presence();
p.setTo(from);
p.setFrom(to);
// TODO: this causes some ugliness in some clients
// p.setError(PacketError.Condition.forbidden);
// If the user tries to check on a buddy before we are totally logged in
// and have the full list, this gets thrown for legit contacts.
// We'll send unavailable for now.
p.setType(Presence.Type.unavailable);
sendPacket(p);
}
}
} else if (packet.getType() == Presence.Type.subscribe) {
// For the time being, we are going to lie to the end user that the subscription has worked.
Presence p = new Presence();
p.setType(Presence.Type.subscribed);
p.setTo(from);
p.setFrom(to);
sendPacket(p);
} else if (packet.getType() == Presence.Type.unsubscribe) {
// For the time being, we are going to lie to the end user that the unsubscription has worked.
Presence p = new Presence();
p.setType(Presence.Type.unsubscribed);
p.setTo(from);
p.setFrom(to);
sendPacket(p);
} else if (packet.getType() == Presence.Type.subscribed) {
// let the legacy domain know that the contact was accepted.
session.acceptAddContact(packet.getTo());
} else {
// Anything else we will ignore for now.
}
} catch (NotFoundException e) {
// Well we just don't care then.
Log.debug("User not found while processing " + "presence stanza: " + packet.toXML(), e);
}
}
} catch (Exception e) {
Log.debug("Exception while processing packet: ", e);
}
return reply;
}
Aggregations