1package eu.siacs.conversations.parser;
2
3import android.util.Log;
4import eu.siacs.conversations.crypto.PgpEngine;
5import eu.siacs.conversations.entities.Account;
6import eu.siacs.conversations.entities.Contact;
7import eu.siacs.conversations.entities.Conversation;
8import eu.siacs.conversations.entities.Presences;
9import eu.siacs.conversations.services.XmppConnectionService;
10import eu.siacs.conversations.xml.Element;
11import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
12
13public class PresenceParser extends AbstractParser {
14
15 public PresenceParser(XmppConnectionService service) {
16 super(service);
17 }
18
19 public void parseConferencePresence(PresencePacket packet, Account account) {
20 PgpEngine mPgpEngine = mXmppConnectionService.getPgpEngine();
21 if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
22 Conversation muc = mXmppConnectionService.findMuc(packet
23 .getAttribute("from").split("/")[0], account);
24 if (muc != null) {
25 muc.getMucOptions().processPacket(packet, mPgpEngine);
26 }
27 } else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
28 Conversation muc = mXmppConnectionService.findMuc(packet
29 .getAttribute("from").split("/")[0], account);
30 if (muc != null) {
31 int error = muc.getMucOptions().getError();
32 muc.getMucOptions().processPacket(packet, mPgpEngine);
33 if (muc.getMucOptions().getError() != error) {
34 mXmppConnectionService.updateUi(muc, false);
35 }
36 }
37 }
38 }
39
40 public void parseContactPresence(PresencePacket packet, Account account) {
41 String[] fromParts = packet.getAttribute("from").split("/");
42 String type = packet.getAttribute("type");
43 if (fromParts[0].equals(account.getJid())) {
44 if (fromParts.length == 2) {
45 if (type == null) {
46 account.updatePresence(fromParts[1],
47 Presences.parseShow(packet.findChild("show")));
48 } else if (type.equals("unavailable")) {
49 account.removePresence(fromParts[1]);
50 }
51 }
52
53 } else {
54 Contact contact = account.getRoster().getContact(packet.getFrom());
55 if (type == null) {
56 if (fromParts.length == 2) {
57 contact.updatePresence(fromParts[1],
58 Presences.parseShow(packet.findChild("show")));
59 PgpEngine pgp = mXmppConnectionService.getPgpEngine();
60 if (pgp != null) {
61 Element x = packet.findChild("x", "jabber:x:signed");
62 if (x != null) {
63 Element status = packet.findChild("status");
64 String msg;
65 if (status != null) {
66 msg = status.getContent();
67 } else {
68 msg = "";
69 }
70 contact.setPgpKeyId(pgp.fetchKeyId(account, msg,
71 x.getContent()));
72 }
73 }
74 updateLastseen(packet, account,true);
75 mXmppConnectionService.onContactStatusChanged
76 .onContactStatusChanged(contact,true);
77 }
78 } else if (type.equals("unavailable")) {
79 if (fromParts.length != 2) {
80 contact.clearPresences();
81 } else {
82 contact.removePresence(fromParts[1]);
83 }
84 mXmppConnectionService.onContactStatusChanged
85 .onContactStatusChanged(contact,false);
86 } else if (type.equals("subscribe")) {
87 if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
88 mXmppConnectionService.sendPresenceUpdatesTo(contact);
89 contact.setOption(Contact.Options.FROM);
90 contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
91 if ((contact.getOption(Contact.Options.ASKING))
92 && (!contact.getOption(Contact.Options.TO))) {
93 mXmppConnectionService
94 .requestPresenceUpdatesFrom(contact);
95 }
96 } else {
97 contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
98 }
99 }
100 }
101 }
102
103}