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.generator.PresenceGenerator;
9import eu.siacs.conversations.services.XmppConnectionService;
10import eu.siacs.conversations.xml.Element;
11import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
12import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
13
14public class PresenceParser extends AbstractParser implements
15 OnPresencePacketReceived {
16
17 public PresenceParser(XmppConnectionService service) {
18 super(service);
19 }
20
21 public void parseConferencePresence(PresencePacket packet, Account account) {
22 PgpEngine mPgpEngine = mXmppConnectionService.getPgpEngine();
23 if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
24 Conversation muc = mXmppConnectionService.find(account, packet
25 .getAttribute("from").split("/")[0]);
26 if (muc != null) {
27 muc.getMucOptions().processPacket(packet, mPgpEngine);
28 }
29 } else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
30 Conversation muc = mXmppConnectionService.find(account, packet
31 .getAttribute("from").split("/")[0]);
32 if (muc != null) {
33 muc.getMucOptions().processPacket(packet, mPgpEngine);
34 }
35 }
36 mXmppConnectionService.updateConversationUi();
37 }
38
39 public void parseContactPresence(PresencePacket packet, Account account) {
40 PresenceGenerator mPresenceGenerator = mXmppConnectionService
41 .getPresenceGenerator();
42 if (packet.getFrom() == null) {
43 return;
44 }
45 String[] fromParts = packet.getFrom().split("/");
46 String type = packet.getAttribute("type");
47 if (fromParts[0].equals(account.getJid())) {
48 if (fromParts.length == 2) {
49 if (type == null) {
50 account.updatePresence(fromParts[1],
51 Presences.parseShow(packet.findChild("show")));
52 } else if (type.equals("unavailable")) {
53 account.removePresence(fromParts[1]);
54 }
55 }
56
57 } else {
58 Contact contact = account.getRoster().getContact(packet.getFrom());
59 if (type == null) {
60 String presence;
61 if (fromParts.length >= 2) {
62 presence = fromParts[1];
63 } else {
64 presence = "";
65 }
66 int sizeBefore = contact.getPresences().size();
67 contact.updatePresence(presence,
68 Presences.parseShow(packet.findChild("show")));
69 PgpEngine pgp = mXmppConnectionService.getPgpEngine();
70 if (pgp != null) {
71 Element x = packet.findChild("x", "jabber:x:signed");
72 if (x != null) {
73 Element status = packet.findChild("status");
74 String msg;
75 if (status != null) {
76 msg = status.getContent();
77 } else {
78 msg = "";
79 }
80 contact.setPgpKeyId(pgp.fetchKeyId(account, msg,
81 x.getContent()));
82 }
83 }
84 boolean online = sizeBefore < contact.getPresences().size();
85 updateLastseen(packet, account, true);
86 mXmppConnectionService.onContactStatusChanged
87 .onContactStatusChanged(contact, online);
88 } else if (type.equals("unavailable")) {
89 if (fromParts.length != 2) {
90 contact.clearPresences();
91 } else {
92 contact.removePresence(fromParts[1]);
93 }
94 mXmppConnectionService.onContactStatusChanged
95 .onContactStatusChanged(contact, false);
96 } else if (type.equals("subscribe")) {
97 if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
98 mXmppConnectionService.sendPresencePacket(account,
99 mPresenceGenerator.sendPresenceUpdatesTo(contact));
100 } else {
101 contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
102 }
103 }
104 }
105 mXmppConnectionService.updateRosterUi();
106 }
107
108 @Override
109 public void onPresencePacketReceived(Account account, PresencePacket packet) {
110 if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
111 this.parseConferencePresence(packet, account);
112 } else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
113 this.parseConferencePresence(packet, account);
114 } else {
115 this.parseContactPresence(packet, account);
116 }
117 }
118
119}