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