2014-06-06 18:26:40 +02:00
|
|
|
package eu.siacs.conversations.parser;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.crypto.PgpEngine;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.Presences;
|
2014-07-12 03:44:23 +02:00
|
|
|
import eu.siacs.conversations.generator.PresenceGenerator;
|
2014-06-06 18:26:40 +02:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-07-12 02:36:37 +02:00
|
|
|
import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
|
2014-06-06 18:26:40 +02:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
|
|
|
|
|
2014-07-12 02:36:37 +02:00
|
|
|
public class PresenceParser extends AbstractParser implements
|
|
|
|
OnPresencePacketReceived {
|
2014-07-12 03:44:23 +02:00
|
|
|
|
2014-06-06 18:26:40 +02:00
|
|
|
public PresenceParser(XmppConnectionService service) {
|
|
|
|
super(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void parseConferencePresence(PresencePacket packet, Account account) {
|
|
|
|
PgpEngine mPgpEngine = mXmppConnectionService.getPgpEngine();
|
|
|
|
if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
|
|
|
|
Conversation muc = mXmppConnectionService.findMuc(packet
|
|
|
|
.getAttribute("from").split("/")[0], account);
|
|
|
|
if (muc != null) {
|
|
|
|
muc.getMucOptions().processPacket(packet, mPgpEngine);
|
|
|
|
}
|
|
|
|
} else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
|
|
|
|
Conversation muc = mXmppConnectionService.findMuc(packet
|
|
|
|
.getAttribute("from").split("/")[0], account);
|
|
|
|
if (muc != null) {
|
|
|
|
int error = muc.getMucOptions().getError();
|
|
|
|
muc.getMucOptions().processPacket(packet, mPgpEngine);
|
|
|
|
if (muc.getMucOptions().getError() != error) {
|
2014-07-12 13:42:17 +02:00
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-06-06 18:26:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void parseContactPresence(PresencePacket packet, Account account) {
|
2014-07-12 12:28:28 +02:00
|
|
|
PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator();
|
2014-07-12 02:36:37 +02:00
|
|
|
if (packet.getFrom() == null) {
|
2014-06-25 17:32:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-06 18:26:40 +02:00
|
|
|
String type = packet.getAttribute("type");
|
|
|
|
if (fromParts[0].equals(account.getJid())) {
|
|
|
|
if (fromParts.length == 2) {
|
|
|
|
if (type == null) {
|
|
|
|
account.updatePresence(fromParts[1],
|
|
|
|
Presences.parseShow(packet.findChild("show")));
|
|
|
|
} else if (type.equals("unavailable")) {
|
|
|
|
account.removePresence(fromParts[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Contact contact = account.getRoster().getContact(packet.getFrom());
|
|
|
|
if (type == null) {
|
|
|
|
if (fromParts.length == 2) {
|
2014-06-12 18:27:50 +02:00
|
|
|
int sizeBefore = contact.getPresences().size();
|
2014-06-06 18:26:40 +02:00
|
|
|
contact.updatePresence(fromParts[1],
|
|
|
|
Presences.parseShow(packet.findChild("show")));
|
|
|
|
PgpEngine pgp = mXmppConnectionService.getPgpEngine();
|
|
|
|
if (pgp != null) {
|
|
|
|
Element x = packet.findChild("x", "jabber:x:signed");
|
|
|
|
if (x != null) {
|
|
|
|
Element status = packet.findChild("status");
|
|
|
|
String msg;
|
|
|
|
if (status != null) {
|
|
|
|
msg = status.getContent();
|
|
|
|
} else {
|
|
|
|
msg = "";
|
|
|
|
}
|
|
|
|
contact.setPgpKeyId(pgp.fetchKeyId(account, msg,
|
|
|
|
x.getContent()));
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 18:27:50 +02:00
|
|
|
boolean online = sizeBefore < contact.getPresences().size();
|
2014-07-12 02:36:37 +02:00
|
|
|
updateLastseen(packet, account, true);
|
2014-06-06 18:26:40 +02:00
|
|
|
mXmppConnectionService.onContactStatusChanged
|
2014-07-12 02:36:37 +02:00
|
|
|
.onContactStatusChanged(contact, online);
|
2014-06-06 18:26:40 +02:00
|
|
|
}
|
|
|
|
} else if (type.equals("unavailable")) {
|
|
|
|
if (fromParts.length != 2) {
|
|
|
|
contact.clearPresences();
|
|
|
|
} else {
|
|
|
|
contact.removePresence(fromParts[1]);
|
|
|
|
}
|
|
|
|
mXmppConnectionService.onContactStatusChanged
|
2014-07-12 02:36:37 +02:00
|
|
|
.onContactStatusChanged(contact, false);
|
2014-06-06 18:26:40 +02:00
|
|
|
} else if (type.equals("subscribe")) {
|
|
|
|
if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
|
2014-07-12 12:28:28 +02:00
|
|
|
mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
|
2014-06-06 18:26:40 +02:00
|
|
|
} else {
|
|
|
|
contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 15:35:31 +02:00
|
|
|
mXmppConnectionService.updateRosterUi();
|
2014-06-06 18:26:40 +02:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:36:37 +02:00
|
|
|
@Override
|
|
|
|
public void onPresencePacketReceived(Account account, PresencePacket packet) {
|
|
|
|
if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
|
|
|
|
this.parseConferencePresence(packet, account);
|
|
|
|
} else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
|
|
|
|
this.parseConferencePresence(packet, account);
|
|
|
|
} else {
|
|
|
|
this.parseContactPresence(packet, account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 18:26:40 +02:00
|
|
|
}
|