Commit 169a17ed authored by Daniel Henninger's avatar Daniel Henninger Committed by dhenninger

[GATE-74] Improvements to XMPP transport functionality.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@8095 b35dd754-fafc-0310-a699-88a17e54d16e
parent f59ed6ab
...@@ -11,10 +11,8 @@ ...@@ -11,10 +11,8 @@
package org.jivesoftware.openfire.gateway.protocols.xmpp; package org.jivesoftware.openfire.gateway.protocols.xmpp;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.util.Log;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import org.dom4j.Element;
import org.dom4j.DocumentHelper;
import java.util.Collection; import java.util.Collection;
/** /**
...@@ -45,9 +43,10 @@ public class XMPPListener implements MessageListener, RosterListener, Connection ...@@ -45,9 +43,10 @@ public class XMPPListener implements MessageListener, RosterListener, Connection
* @param message Message received. * @param message Message received.
*/ */
public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) { public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) {
Log.debug("XMPP got message: "+message.toXML());
xmppSession.getTransport().sendMessage( xmppSession.getTransport().sendMessage(
xmppSession.getJIDWithHighestPriority(), xmppSession.getJIDWithHighestPriority(),
xmppSession.getTransport().convertIDToJID(message.getFrom()), xmppSession.getTransport().convertIDToJID(xmppSession.getBareJID(message.getFrom())),
message.getBody() message.getBody()
); );
} }
...@@ -65,10 +64,27 @@ public class XMPPListener implements MessageListener, RosterListener, Connection ...@@ -65,10 +64,27 @@ public class XMPPListener implements MessageListener, RosterListener, Connection
} }
public void presenceChanged(org.jivesoftware.smack.packet.Presence presence) { public void presenceChanged(org.jivesoftware.smack.packet.Presence presence) {
Element presElem = DocumentHelper.createElement(presence.toXML()); Presence p = new Presence();
Presence p = new Presence(presElem); if (presence.getType().equals(org.jivesoftware.smack.packet.Presence.Type.available)) {
// Nothing to do
}
else if (presence.getType().equals(org.jivesoftware.smack.packet.Presence.Type.unavailable)) {
p.setType(Presence.Type.unavailable);
}
else if (presence.getType().equals(org.jivesoftware.smack.packet.Presence.Type.subscribe)) {
p.setType(Presence.Type.subscribe);
}
else if (presence.getType().equals(org.jivesoftware.smack.packet.Presence.Type.subscribed)) {
p.setType(Presence.Type.subscribed);
}
else if (presence.getType().equals(org.jivesoftware.smack.packet.Presence.Type.unsubscribe)) {
p.setType(Presence.Type.unsubscribe);
}
else if (presence.getType().equals(org.jivesoftware.smack.packet.Presence.Type.unsubscribed)) {
p.setType(Presence.Type.unsubscribed);
}
p.setTo(xmppSession.getJID()); p.setTo(xmppSession.getJID());
p.setFrom(xmppSession.getTransport().convertIDToJID(presence.getFrom())); p.setFrom(xmppSession.getTransport().convertIDToJID(xmppSession.getBareJID(presence.getFrom())));
xmppSession.getTransport().sendPacket(p); xmppSession.getTransport().sendPacket(p);
} }
......
...@@ -17,10 +17,11 @@ import org.jivesoftware.util.JiveGlobals; ...@@ -17,10 +17,11 @@ import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.Chat;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.Presence; import org.xmpp.packet.Presence;
import java.util.Date;
/** /**
* Represents an XMPP session. * Represents an XMPP session.
* *
...@@ -87,6 +88,14 @@ public class XMPPSession extends TransportSession { ...@@ -87,6 +88,14 @@ public class XMPPSession extends TransportSession {
conn.login(acctjid.getNode(), getRegistration().getPassword(), "IMGateway"); conn.login(acctjid.getNode(), getRegistration().getPassword(), "IMGateway");
conn.getRoster().addRosterListener(listener); conn.getRoster().addRosterListener(listener);
conn.getChatManager().addChatListener(listener); conn.getChatManager().addChatListener(listener);
setLoginStatus(TransportLoginStatus.LOGGED_IN);
Presence p = new Presence();
p.setTo(getJIDWithHighestPriority());
p.setFrom(getTransport().getJID());
getTransport().sendPacket(p);
getRegistration().setLastLogin(new Date());
} }
catch (XMPPException e) { catch (XMPPException e) {
Log.error(getTransport().getType()+" user is not able to log in: "+getRegistration().getUsername(), e); Log.error(getTransport().getType()+" user is not able to log in: "+getRegistration().getUsername(), e);
...@@ -128,13 +137,12 @@ public class XMPPSession extends TransportSession { ...@@ -128,13 +137,12 @@ public class XMPPSession extends TransportSession {
} }
public void sendMessage(JID jid, String message) { public void sendMessage(JID jid, String message) {
Chat chat = conn.getChatManager().createChat(getTransport().convertJIDToID(jid), listener); org.jivesoftware.smack.packet.Message m = new org.jivesoftware.smack.packet.Message();
try { m.setFrom(getRegistration().getUsername());
chat.sendMessage(message); m.setTo(getTransport().convertJIDToID(jid));
} m.setBody(message);
catch (XMPPException e) { Log.debug("XMPP Sending Message: "+m.toXML());
// TODO: handle exception properly conn.sendPacket(m);
}
} }
public void sendServerMessage(String message) { public void sendServerMessage(String message) {
...@@ -150,4 +158,8 @@ public class XMPPSession extends TransportSession { ...@@ -150,4 +158,8 @@ public class XMPPSession extends TransportSession {
public void resendContactStatuses(JID jid) { public void resendContactStatuses(JID jid) {
} }
public String getBareJID(String jid) {
return jid.substring(0, jid.indexOf("/"));
}
} }
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