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 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("/")[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 }
62 }
63 } else {
64 Contact contact = account.getRoster().getContact(packet.getFrom());
65 if (type == null) {
66 String presence;
67 if (fromParts.length >= 2) {
68 presence = fromParts[1];
69 } else {
70 presence = "";
71 }
72 int sizeBefore = contact.getPresences().size();
73 contact.updatePresence(presence,
74 Presences.parseShow(packet.findChild("show")));
75 PgpEngine pgp = mXmppConnectionService.getPgpEngine();
76 if (pgp != null) {
77 Element x = packet.findChild("x", "jabber:x:signed");
78 if (x != null) {
79 Element status = packet.findChild("status");
80 String msg;
81 if (status != null) {
82 msg = status.getContent();
83 } else {
84 msg = "";
85 }
86 contact.setPgpKeyId(pgp.fetchKeyId(account, msg,
87 x.getContent()));
88 }
89 }
90 boolean online = sizeBefore < contact.getPresences().size();
91 updateLastseen(packet, account, true);
92 mXmppConnectionService.onContactStatusChanged
93 .onContactStatusChanged(contact, online);
94 } else if (type.equals("unavailable")) {
95 if (fromParts.length != 2) {
96 contact.clearPresences();
97 } else {
98 contact.removePresence(fromParts[1]);
99 }
100 mXmppConnectionService.onContactStatusChanged
101 .onContactStatusChanged(contact, false);
102 } else if (type.equals("subscribe")) {
103 if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
104 mXmppConnectionService.sendPresencePacket(account,
105 mPresenceGenerator.sendPresenceUpdatesTo(contact));
106 } else {
107 contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
108 }
109 }
110 Element nick = packet.findChild("nick", "http://jabber.org/protocol/nick");
111 if (nick != null) {
112 contact.setPresenceName(nick.getContent());
113 }
114 }
115 mXmppConnectionService.updateRosterUi();
116 }
117
118 @Override
119 public void onPresencePacketReceived(Account account, PresencePacket packet) {
120 if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
121 this.parseConferencePresence(packet, account);
122 } else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
123 this.parseConferencePresence(packet, account);
124 } else {
125 this.parseContactPresence(packet, account);
126 }
127 }
128
129}