mirror of
https://github.com/moparisthebest/Conversations
synced 2024-12-26 09:19:07 -05:00
moved iq parser to seperate class as well
This commit is contained in:
parent
99fe44a737
commit
c512d98b74
89
src/eu/siacs/conversations/parser/IqParser.java
Normal file
89
src/eu/siacs/conversations/parser/IqParser.java
Normal file
@ -0,0 +1,89 @@
|
||||
package eu.siacs.conversations.parser;
|
||||
|
||||
import eu.siacs.conversations.entities.Account;
|
||||
import eu.siacs.conversations.entities.Contact;
|
||||
import eu.siacs.conversations.services.XmppConnectionService;
|
||||
import eu.siacs.conversations.xml.Element;
|
||||
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
||||
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
||||
|
||||
public class IqParser extends AbstractParser implements OnIqPacketReceived {
|
||||
|
||||
public IqParser(XmppConnectionService service) {
|
||||
super(service);
|
||||
}
|
||||
|
||||
public void rosterItems(Account account, Element query) {
|
||||
String version = query.getAttribute("ver");
|
||||
if (version != null) {
|
||||
account.getRoster().setVersion(version);
|
||||
}
|
||||
for (Element item : query.getChildren()) {
|
||||
if (item.getName().equals("item")) {
|
||||
String jid = item.getAttribute("jid");
|
||||
String name = item.getAttribute("name");
|
||||
String subscription = item.getAttribute("subscription");
|
||||
Contact contact = account.getRoster().getContact(jid);
|
||||
if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
|
||||
contact.setServerName(name);
|
||||
}
|
||||
if (subscription.equals("remove")) {
|
||||
contact.resetOption(Contact.Options.IN_ROSTER);
|
||||
contact.resetOption(Contact.Options.DIRTY_DELETE);
|
||||
contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
|
||||
} else {
|
||||
contact.setOption(Contact.Options.IN_ROSTER);
|
||||
contact.resetOption(Contact.Options.DIRTY_PUSH);
|
||||
contact.parseSubscriptionFromElement(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onIqPacketReceived(Account account, IqPacket packet) {
|
||||
if (packet.hasChild("query", "jabber:iq:roster")) {
|
||||
String from = packet.getFrom();
|
||||
if ((from == null) || (from.equals(account.getJid()))) {
|
||||
Element query = packet.findChild("query");
|
||||
this.rosterItems(account, query);
|
||||
}
|
||||
} else if (packet
|
||||
.hasChild("open", "http://jabber.org/protocol/ibb")
|
||||
|| packet
|
||||
.hasChild("data", "http://jabber.org/protocol/ibb")) {
|
||||
mXmppConnectionService.getJingleConnectionManager().deliverIbbPacket(account, packet);
|
||||
} else if (packet.hasChild("query",
|
||||
"http://jabber.org/protocol/disco#info")) {
|
||||
IqPacket iqResponse = packet
|
||||
.generateRespone(IqPacket.TYPE_RESULT);
|
||||
Element query = iqResponse.addChild("query",
|
||||
"http://jabber.org/protocol/disco#info");
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:jingle:1");
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:jingle:apps:file-transfer:3");
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:jingle:transports:s5b:1");
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:jingle:transports:ibb:1");
|
||||
if (mXmppConnectionService.confirmMessages()) {
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:receipts");
|
||||
}
|
||||
account.getXmppConnection().sendIqPacket(iqResponse, null);
|
||||
} else {
|
||||
if ((packet.getType() == IqPacket.TYPE_GET)
|
||||
|| (packet.getType() == IqPacket.TYPE_SET)) {
|
||||
IqPacket response = packet
|
||||
.generateRespone(IqPacket.TYPE_ERROR);
|
||||
Element error = response.addChild("error");
|
||||
error.setAttribute("type", "cancel");
|
||||
error.addChild("feature-not-implemented",
|
||||
"urn:ietf:params:xml:ns:xmpp-stanzas");
|
||||
account.getXmppConnection().sendIqPacket(response, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -13,12 +13,9 @@ import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
|
||||
|
||||
public class PresenceParser extends AbstractParser implements
|
||||
OnPresencePacketReceived {
|
||||
|
||||
private PresenceGenerator mPresenceGenerator;
|
||||
|
||||
public PresenceParser(XmppConnectionService service) {
|
||||
super(service);
|
||||
mPresenceGenerator = service.getPresenceGenerator();
|
||||
}
|
||||
|
||||
public void parseConferencePresence(PresencePacket packet, Account account) {
|
||||
@ -43,6 +40,7 @@ public class PresenceParser extends AbstractParser implements
|
||||
}
|
||||
|
||||
public void parseContactPresence(PresencePacket packet, Account account) {
|
||||
PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator();
|
||||
if (packet.getFrom() == null) {
|
||||
return;
|
||||
}
|
||||
@ -95,11 +93,7 @@ public class PresenceParser extends AbstractParser implements
|
||||
.onContactStatusChanged(contact, false);
|
||||
} else if (type.equals("subscribe")) {
|
||||
if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
|
||||
mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.stopPresenceUpdatesTo(contact));
|
||||
if ((contact.getOption(Contact.Options.ASKING))
|
||||
&& (!contact.getOption(Contact.Options.TO))) {
|
||||
mXmppConnectionService.sendPresencePacket(account,mPresenceGenerator.requestPresenceUpdatesFrom(contact));
|
||||
}
|
||||
mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
|
||||
} else {
|
||||
contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
|
||||
import eu.siacs.conversations.entities.Presences;
|
||||
import eu.siacs.conversations.generator.MessageGenerator;
|
||||
import eu.siacs.conversations.generator.PresenceGenerator;
|
||||
import eu.siacs.conversations.parser.IqParser;
|
||||
import eu.siacs.conversations.parser.MessageParser;
|
||||
import eu.siacs.conversations.parser.PresenceParser;
|
||||
import eu.siacs.conversations.persistance.DatabaseBackend;
|
||||
@ -92,6 +93,7 @@ public class XmppConnectionService extends Service {
|
||||
|
||||
private MessageParser mMessageParser = new MessageParser(this);
|
||||
private PresenceParser mPresenceParser = new PresenceParser(this);
|
||||
private IqParser mIqParser = new IqParser(this);
|
||||
private MessageGenerator mMessageGenerator = new MessageGenerator();
|
||||
private PresenceGenerator mPresenceGenerator = new PresenceGenerator();
|
||||
|
||||
@ -179,58 +181,6 @@ public class XmppConnectionService extends Service {
|
||||
}
|
||||
};
|
||||
|
||||
private OnIqPacketReceived unknownIqListener = new OnIqPacketReceived() {
|
||||
|
||||
@Override
|
||||
public void onIqPacketReceived(Account account, IqPacket packet) {
|
||||
if (packet.hasChild("query", "jabber:iq:roster")) {
|
||||
String from = packet.getFrom();
|
||||
if ((from == null) || (from.equals(account.getJid()))) {
|
||||
Element query = packet.findChild("query");
|
||||
processRosterItems(account, query);
|
||||
} else {
|
||||
Log.d(LOGTAG, "unauthorized roster push from: " + from);
|
||||
}
|
||||
} else if (packet
|
||||
.hasChild("open", "http://jabber.org/protocol/ibb")
|
||||
|| packet
|
||||
.hasChild("data", "http://jabber.org/protocol/ibb")) {
|
||||
XmppConnectionService.this.mJingleConnectionManager
|
||||
.deliverIbbPacket(account, packet);
|
||||
} else if (packet.hasChild("query",
|
||||
"http://jabber.org/protocol/disco#info")) {
|
||||
IqPacket iqResponse = packet
|
||||
.generateRespone(IqPacket.TYPE_RESULT);
|
||||
Element query = iqResponse.addChild("query",
|
||||
"http://jabber.org/protocol/disco#info");
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:jingle:1");
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:jingle:apps:file-transfer:3");
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:jingle:transports:s5b:1");
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:jingle:transports:ibb:1");
|
||||
if (confirmMessages()) {
|
||||
query.addChild("feature").setAttribute("var",
|
||||
"urn:xmpp:receipts");
|
||||
}
|
||||
account.getXmppConnection().sendIqPacket(iqResponse, null);
|
||||
} else {
|
||||
if ((packet.getType() == IqPacket.TYPE_GET)
|
||||
|| (packet.getType() == IqPacket.TYPE_SET)) {
|
||||
IqPacket response = packet
|
||||
.generateRespone(IqPacket.TYPE_ERROR);
|
||||
Element error = response.addChild("error");
|
||||
error.setAttribute("type", "cancel");
|
||||
error.addChild("feature-not-implemented",
|
||||
"urn:ietf:params:xml:ns:xmpp-stanzas");
|
||||
account.getXmppConnection().sendIqPacket(response, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private OnJinglePacketReceived jingleListener = new OnJinglePacketReceived() {
|
||||
|
||||
@Override
|
||||
@ -306,33 +256,6 @@ public class XmppConnectionService extends Service {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void processRosterItems(Account account, Element elements) {
|
||||
String version = elements.getAttribute("ver");
|
||||
if (version != null) {
|
||||
account.getRoster().setVersion(version);
|
||||
}
|
||||
for (Element item : elements.getChildren()) {
|
||||
if (item.getName().equals("item")) {
|
||||
String jid = item.getAttribute("jid");
|
||||
String name = item.getAttribute("name");
|
||||
String subscription = item.getAttribute("subscription");
|
||||
Contact contact = account.getRoster().getContact(jid);
|
||||
if (!contact.getOption(Contact.Options.DIRTY_PUSH)) {
|
||||
contact.setServerName(name);
|
||||
}
|
||||
if (subscription.equals("remove")) {
|
||||
contact.resetOption(Contact.Options.IN_ROSTER);
|
||||
contact.resetOption(Contact.Options.DIRTY_DELETE);
|
||||
contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
|
||||
} else {
|
||||
contact.setOption(Contact.Options.IN_ROSTER);
|
||||
contact.resetOption(Contact.Options.DIRTY_PUSH);
|
||||
contact.parseSubscriptionFromElement(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class XmppConnectionBinder extends Binder {
|
||||
public XmppConnectionService getService() {
|
||||
return XmppConnectionService.this;
|
||||
@ -522,7 +445,7 @@ public class XmppConnectionService extends Service {
|
||||
connection.setOnStatusChangedListener(this.statusListener);
|
||||
connection.setOnPresencePacketReceivedListener(this.mPresenceParser);
|
||||
connection
|
||||
.setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener);
|
||||
.setOnUnregisteredIqPacketReceivedListener(this.mIqParser);
|
||||
connection.setOnJinglePacketReceivedListener(this.jingleListener);
|
||||
connection
|
||||
.setOnTLSExceptionReceivedListener(new OnTLSExceptionReceived() {
|
||||
@ -734,10 +657,10 @@ public class XmppConnectionService extends Service {
|
||||
@Override
|
||||
public void onIqPacketReceived(final Account account,
|
||||
IqPacket packet) {
|
||||
Element roster = packet.findChild("query");
|
||||
if (roster != null) {
|
||||
Element query = packet.findChild("query");
|
||||
if (query != null) {
|
||||
account.getRoster().markAllAsNotInRoster();
|
||||
processRosterItems(account, roster);
|
||||
mIqParser.rosterItems(account, query);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -1167,17 +1090,18 @@ public class XmppConnectionService extends Service {
|
||||
contact.setOption(Contact.Options.DIRTY_PUSH);
|
||||
Account account = contact.getAccount();
|
||||
if (account.getStatus() == Account.STATUS_ONLINE) {
|
||||
boolean ask = contact.getOption(Contact.Options.ASKING);
|
||||
boolean sendUpdates = contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
|
||||
&& contact.getOption(Contact.Options.PREEMPTIVE_GRANT);
|
||||
IqPacket iq = new IqPacket(IqPacket.TYPE_SET);
|
||||
iq.query("jabber:iq:roster").addChild(contact.asElement());
|
||||
account.getXmppConnection().sendIqPacket(iq, null);
|
||||
if (contact.getOption(Contact.Options.ASKING)) {
|
||||
sendPresencePacket(account, mPresenceGenerator.requestPresenceUpdatesFrom(contact));
|
||||
}
|
||||
if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)
|
||||
&& contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
|
||||
Log.d("xmppService", "contact had pending subscription");
|
||||
if (sendUpdates) {
|
||||
sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
|
||||
}
|
||||
if (ask) {
|
||||
sendPresencePacket(account, mPresenceGenerator.requestPresenceUpdatesFrom(contact));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1381,4 +1305,8 @@ public class XmppConnectionService extends Service {
|
||||
public PresenceGenerator getPresenceGenerator() {
|
||||
return this.mPresenceGenerator;
|
||||
}
|
||||
|
||||
public JingleConnectionManager getJingleConnectionManager() {
|
||||
return this.mJingleConnectionManager;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user