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("/", 2)[0]);
26 if (muc != null) {
27 boolean before = muc.getMucOptions().online();
28 muc.getMucOptions().processPacket(packet, mPgpEngine);
29 if (before != muc.getMucOptions().online()) {
30 mXmppConnectionService.updateConversationUi();
31 }
32 }
33 } else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
34 Conversation muc = mXmppConnectionService.find(account, packet
35 .getAttribute("from").split("/", 2)[0]);
36 if (muc != null) {
37 boolean before = muc.getMucOptions().online();
38 muc.getMucOptions().processPacket(packet, mPgpEngine);
39 if (before != muc.getMucOptions().online()) {
40 mXmppConnectionService.updateConversationUi();
41 }
42 }
43 }
44 }
45
46 public void parseContactPresence(PresencePacket packet, Account account) {
47 PresenceGenerator mPresenceGenerator = mXmppConnectionService
48 .getPresenceGenerator();
49 if (packet.getFrom() == null) {
50 return;
51 }
52 String[] fromParts = packet.getFrom().split("/", 2);
53 String type = packet.getAttribute("type");
54 if (fromParts[0].equals(account.getJid())) {
55 if (fromParts.length == 2) {
56 if (type == null) {
57 account.updatePresence(fromParts[1],
58 Presences.parseShow(packet.findChild("show")));
59 } else if (type.equals("unavailable")) {
60 account.removePresence(fromParts[1]);
61 mXmppConnectionService.getNotificationService()
62 .deactivateGracePeriod();
63 }
64 }
65 } else {
66 Contact contact = account.getRoster().getContact(packet.getFrom());
67 if (type == null) {
68 String presence;
69 if (fromParts.length >= 2) {
70 presence = fromParts[1];
71 } else {
72 presence = "";
73 }
74 int sizeBefore = contact.getPresences().size();
75 contact.updatePresence(presence,
76 Presences.parseShow(packet.findChild("show")));
77 PgpEngine pgp = mXmppConnectionService.getPgpEngine();
78 if (pgp != null) {
79 Element x = packet.findChild("x", "jabber:x:signed");
80 if (x != null) {
81 Element status = packet.findChild("status");
82 String msg;
83 if (status != null) {
84 msg = status.getContent();
85 } else {
86 msg = "";
87 }
88 contact.setPgpKeyId(pgp.fetchKeyId(account, msg,
89 x.getContent()));
90 }
91 }
92 boolean online = sizeBefore < contact.getPresences().size();
93 updateLastseen(packet, account, true);
94 mXmppConnectionService.onContactStatusChanged
95 .onContactStatusChanged(contact, online);
96 } else if (type.equals("unavailable")) {
97 if (fromParts.length != 2) {
98 contact.clearPresences();
99 } else {
100 contact.removePresence(fromParts[1]);
101 }
102 mXmppConnectionService.onContactStatusChanged
103 .onContactStatusChanged(contact, false);
104 } else if (type.equals("subscribe")) {
105 if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
106 mXmppConnectionService.sendPresencePacket(account,
107 mPresenceGenerator.sendPresenceUpdatesTo(contact));
108 } else {
109 contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
110 }
111 }
112 Element nick = packet.findChild("nick",
113 "http://jabber.org/protocol/nick");
114 if (nick != null) {
115 contact.setPresenceName(nick.getContent());
116 }
117 }
118 mXmppConnectionService.updateRosterUi();
119 }
120
121 @Override
122 public void onPresencePacketReceived(Account account, PresencePacket packet) {
123 if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
124 this.parseConferencePresence(packet, account);
125 } else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
126 this.parseConferencePresence(packet, account);
127 } else {
128 this.parseContactPresence(packet, account);
129 }
130 }
131
132}