aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/parser/IqParser.java
blob: acbeee4dd5722fc0a377e55beef014aafb8f22b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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);
			}
		}
	}

}