Commit ed9be2d5 authored by Daniel Henninger's avatar Daniel Henninger Committed by dhenninger

[JM-761] iq:version and iq:gateway support added. registration support improved.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk/src/plugins/gateway@4604 b35dd754-fafc-0310-a699-88a17e54d16e
parent 9276ff20
...@@ -28,6 +28,8 @@ import org.jivesoftware.wildfire.user.UserAlreadyExistsException; ...@@ -28,6 +28,8 @@ import org.jivesoftware.wildfire.user.UserAlreadyExistsException;
import org.xmpp.component.Component; import org.xmpp.component.Component;
import org.xmpp.component.ComponentException; import org.xmpp.component.ComponentException;
import org.xmpp.component.ComponentManager; import org.xmpp.component.ComponentManager;
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.Message; import org.xmpp.packet.Message;
...@@ -113,7 +115,9 @@ public abstract class BaseTransport implements Component { ...@@ -113,7 +115,9 @@ public abstract class BaseTransport implements Component {
private final String DISCO_INFO = "http://jabber.org/protocol/disco#info"; private final String DISCO_INFO = "http://jabber.org/protocol/disco#info";
private final String DISCO_ITEMS = "http://jabber.org/protocol/disco#items"; private final String DISCO_ITEMS = "http://jabber.org/protocol/disco#items";
private final String IQ_GATEWAY = "jabber:iq:gateway";
private final String IQ_REGISTER = "jabber:iq:register"; private final String IQ_REGISTER = "jabber:iq:register";
private final String IQ_VERSION = "jabber:iq:version";
/** /**
* Handles all incoming XMPP stanzas, passing them to individual * Handles all incoming XMPP stanzas, passing them to individual
...@@ -324,10 +328,18 @@ public abstract class BaseTransport implements Component { ...@@ -324,10 +328,18 @@ public abstract class BaseTransport implements Component {
Log.debug("Matched Disco Items"); Log.debug("Matched Disco Items");
reply.addAll(handleDiscoItems(packet)); reply.addAll(handleDiscoItems(packet));
} }
else if (xmlns.equals(IQ_GATEWAY)) {
Log.debug("Matched IQ Gateway");
reply.addAll(handleIQGateway(packet));
}
else if (xmlns.equals(IQ_REGISTER)) { else if (xmlns.equals(IQ_REGISTER)) {
Log.debug("Matched IQ Register"); Log.debug("Matched IQ Register");
reply.addAll(handleIQRegister(packet)); reply.addAll(handleIQRegister(packet));
} }
else if (xmlns.equals(IQ_VERSION)) {
Log.debug("Matched IQ Version");
reply.addAll(handleIQVersion(packet));
}
else { else {
Log.debug("Matched nothing"); Log.debug("Matched nothing");
} }
...@@ -339,7 +351,7 @@ public abstract class BaseTransport implements Component { ...@@ -339,7 +351,7 @@ public abstract class BaseTransport implements Component {
* Handle service discovery info request. * Handle service discovery info request.
* *
* @param packet An IQ packet in the disco info namespace. * @param packet An IQ packet in the disco info namespace.
* @return An IQ packet to be returned to the user. * @return A list of IQ packets to be returned to the user.
*/ */
private List<Packet> handleDiscoInfo(IQ packet) { private List<Packet> handleDiscoInfo(IQ packet) {
List<Packet> reply = new ArrayList<Packet>(); List<Packet> reply = new ArrayList<Packet>();
...@@ -352,8 +364,12 @@ public abstract class BaseTransport implements Component { ...@@ -352,8 +364,12 @@ public abstract class BaseTransport implements Component {
.addAttribute("category", "gateway") .addAttribute("category", "gateway")
.addAttribute("type", this.transportType.toString()) .addAttribute("type", this.transportType.toString())
.addAttribute("name", this.description); .addAttribute("name", this.description);
response.addElement("feature")
.addAttribute("var", IQ_GATEWAY);
response.addElement("feature") response.addElement("feature")
.addAttribute("var", IQ_REGISTER); .addAttribute("var", IQ_REGISTER);
response.addElement("feature")
.addAttribute("var", IQ_VERSION);
result.setChildElement(response); result.setChildElement(response);
reply.add(result); reply.add(result);
} }
...@@ -365,7 +381,7 @@ public abstract class BaseTransport implements Component { ...@@ -365,7 +381,7 @@ public abstract class BaseTransport implements Component {
* Handle service discovery items request. * Handle service discovery items request.
* *
* @param packet An IQ packet in the disco items namespace. * @param packet An IQ packet in the disco items namespace.
* @return An IQ packet to be returned to the user. * @return A list of IQ packets to be returned to the user.
*/ */
private List<Packet> handleDiscoItems(IQ packet) { private List<Packet> handleDiscoItems(IQ packet) {
List<Packet> reply = new ArrayList<Packet>(); List<Packet> reply = new ArrayList<Packet>();
...@@ -374,11 +390,53 @@ public abstract class BaseTransport implements Component { ...@@ -374,11 +390,53 @@ public abstract class BaseTransport implements Component {
return reply; return reply;
} }
/**
* Handle gateway translation service request.
*
* @param packet An IQ packet in the iq gateway namespace.
* @return A list of IQ packets to be returned to the user.
*/
private List<Packet> handleIQGateway(IQ packet) {
List<Packet> reply = new ArrayList<Packet>();
if (packet.getType() == IQ.Type.get) {
IQ result = IQ.createResultIQ(packet);
Element query = DocumentHelper.createElement(QName.get("query", IQ_GATEWAY));
query.addElement("desc").addText("Please enter the person's "+this.getName()+" username.");
query.addElement("prompt");
result.setChildElement(query);
reply.add(result);
}
else if (packet.getType() == IQ.Type.set) {
IQ result = IQ.createResultIQ(packet);
String prompt = null;
Element promptEl = packet.getChildElement().element("prompt");
if (promptEl != null) {
prompt = promptEl.getTextTrim();
}
if (prompt == null) {
result.setError(Condition.bad_request);
}
else {
JID jid = this.convertIDToJID(prompt);
Element query = DocumentHelper.createElement(QName.get("query", IQ_GATEWAY));
// This is what Psi expects
query.addElement("prompt").addText(jid.toString());
// This is JEP complient
query.addElement("jid").addText(jid.toString());
result.setChildElement(query);
}
reply.add(result);
}
return reply;
}
/** /**
* Handle registration request. * Handle registration request.
* *
* @param packet An IQ packet in the iq registration namespace. * @param packet An IQ packet in the iq registration namespace.
* @return An IQ packet to be returned to the user. * @return A list of IQ packets to be returned to the user.
*/ */
private List<Packet> handleIQRegister(IQ packet) { private List<Packet> handleIQRegister(IQ packet) {
List<Packet> reply = new ArrayList<Packet>(); List<Packet> reply = new ArrayList<Packet>();
...@@ -427,6 +485,23 @@ public abstract class BaseTransport implements Component { ...@@ -427,6 +485,23 @@ public abstract class BaseTransport implements Component {
String username = null; String username = null;
String password = null; String password = null;
try {
DataForm form = new DataForm(packet.getChildElement().element("x"));
List<FormField> fields = form.getFields();
for (FormField field : fields) {
String var = field.getVariable();
if (var.equals("username")) {
username = field.getValues().get(0);
}
else if (var.equals("password")) {
password = field.getValues().get(0);
}
}
}
catch (Exception e) {
// No with data form apparantly
}
if (packet.getType() == IQ.Type.set) { if (packet.getType() == IQ.Type.set) {
Element userEl = packet.getChildElement().element("username"); Element userEl = packet.getChildElement().element("username");
Element passEl = packet.getChildElement().element("password"); Element passEl = packet.getChildElement().element("password");
...@@ -477,13 +552,33 @@ public abstract class BaseTransport implements Component { ...@@ -477,13 +552,33 @@ public abstract class BaseTransport implements Component {
eresult.setError(Condition.bad_request); eresult.setError(Condition.bad_request);
reply.add(eresult); reply.add(eresult);
} }
// Lets ask them what their presence is, maybe log
// them in immediately.
Presence p = new Presence(Presence.Type.probe);
p.setTo(packet.getFrom());
p.setFrom(packet.getTo());
reply.add(p);
} }
} }
else if (packet.getType() == IQ.Type.get) { else if (packet.getType() == IQ.Type.get) {
Element response = DocumentHelper.createElement(QName.get("query", IQ_REGISTER)); Element response = DocumentHelper.createElement(QName.get("query", IQ_REGISTER));
IQ result = IQ.createResultIQ(packet); IQ result = IQ.createResultIQ(packet);
response.addElement("instruction").addText("Please enter your " + this.getName() + " username and password."); DataForm form = new DataForm(DataForm.Type.form);
form.addInstruction("Please enter your " + this.getName() + " username and password.");
FormField usernameField = form.addField();
usernameField.setLabel("Username");
usernameField.setVariable("username");
usernameField.setType(FormField.Type.text_single);
FormField passwordField = form.addField();
passwordField.setLabel("Password");
passwordField.setVariable("password");
passwordField.setType(FormField.Type.text_private);
response.addElement("instructions").addText("Please enter your " + this.getName() + " username and password.");
response.addElement("username"); response.addElement("username");
response.addElement("password"); response.addElement("password");
...@@ -496,6 +591,28 @@ public abstract class BaseTransport implements Component { ...@@ -496,6 +591,28 @@ public abstract class BaseTransport implements Component {
return reply; return reply;
} }
/**
* Handle version request.
*
* @param packet An IQ packet in the iq version namespace.
* @return A list of IQ packets to be returned to the user.
*/
private List<Packet> handleIQVersion(IQ packet) {
List<Packet> reply = new ArrayList<Packet>();
if (packet.getType() == IQ.Type.get) {
IQ result = IQ.createResultIQ(packet);
Element query = DocumentHelper.createElement(QName.get("query", IQ_VERSION));
query.addElement("name").addText("Wildfire " + this.getDescription());
query.addElement("version").addText(this.getVersionString());
query.addElement("os").addText(System.getProperty("os.name"));
result.setChildElement(query);
reply.add(result);
}
return reply;
}
/** /**
* Converts a legacy username to a JID. * Converts a legacy username to a JID.
* *
...@@ -503,7 +620,17 @@ public abstract class BaseTransport implements Component { ...@@ -503,7 +620,17 @@ public abstract class BaseTransport implements Component {
* @return The legacy username as a JID. * @return The legacy username as a JID.
*/ */
public JID convertIDToJID(String username) { public JID convertIDToJID(String username) {
return new JID(username, this.jid.getDomain(), null); return new JID(username.replace('@', '%'), this.jid.getDomain(), null);
}
/**
* Converts a JID to a legacy username.
*
* @param jid JID to be converted to a legacy username.
* @return THe legacy username as a String.
*/
public String convertJIDToID(JID jid) {
return jid.getNode().replace('%', '@');
} }
/** /**
...@@ -550,6 +677,13 @@ public abstract class BaseTransport implements Component { ...@@ -550,6 +677,13 @@ public abstract class BaseTransport implements Component {
return componentManager; return componentManager;
} }
/**
* Returns the version string of the gateway.
*/
public String getVersionString() {
return "0.0.1";
}
/** /**
* Either updates or adds a JID to a user's roster. * Either updates or adds a JID to a user's roster.
* *
......
...@@ -57,6 +57,8 @@ public class MSNSession extends TransportSession { ...@@ -57,6 +57,8 @@ public class MSNSession extends TransportSession {
p.setFrom(getTransport().getJID()); p.setFrom(getTransport().getJID());
getTransport().sendPacket(p); getTransport().sendPacket(p);
msnManager.setStatus(ContactStatus.ONLINE); msnManager.setStatus(ContactStatus.ONLINE);
msnManager.setPrivacyMode(true);
msnManager.setReverseListBehaviour(true);
} }
catch (MSNException e) { catch (MSNException e) {
Log.error("MSN exception thrown while logging in: " + e.toString()); Log.error("MSN exception thrown while logging in: " + e.toString());
...@@ -112,7 +114,7 @@ public class MSNSession extends TransportSession { ...@@ -112,7 +114,7 @@ public class MSNSession extends TransportSession {
*/ */
public void sendMessage(JID jid, String message) { public void sendMessage(JID jid, String message) {
try { try {
msnManager.sendMessage(jid.getNode(), message); msnManager.sendMessage(getTransport().convertJIDToID(jid), message);
} }
catch (MSNException e) { catch (MSNException e) {
Log.error("MSN exception while sending message: " + e.toString()); Log.error("MSN exception while sending message: " + e.toString());
......
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