2014-06-06 16:26:40 +00: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 01:44:23 +00:00
|
|
|
import eu.siacs.conversations.generator.PresenceGenerator;
|
2014-06-06 16:26:40 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-07-12 00:36:37 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
|
2014-06-06 16:26:40 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
|
|
|
|
|
2014-07-12 00:36:37 +00:00
|
|
|
public class PresenceParser extends AbstractParser implements
|
|
|
|
OnPresencePacketReceived {
|
2014-06-06 16:26:40 +00:00
|
|
|
|
2014-07-12 01:44:23 +00:00
|
|
|
private PresenceGenerator mPresenceGenerator;
|
|
|
|
|
2014-06-06 16:26:40 +00:00
|
|
|
public PresenceParser(XmppConnectionService service) {
|
|
|
|
super(service);
|
2014-07-12 01:44:23 +00:00
|
|
|
mPresenceGenerator = service.getPresenceGenerator();
|
2014-06-06 16:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
mXmppConnectionService.updateUi(muc, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void parseContactPresence(PresencePacket packet, Account account) {
|
2014-07-12 00:36:37 +00:00
|
|
|
if (packet.getFrom() == null) {
|
2014-06-25 15:32:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
String[] fromParts = packet.getFrom().split("/");
|
2014-06-06 16:26:40 +00: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 16:27:50 +00:00
|
|
|
int sizeBefore = contact.getPresences().size();
|
2014-06-06 16:26:40 +00: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 16:27:50 +00:00
|
|
|
boolean online = sizeBefore < contact.getPresences().size();
|
2014-07-12 00:36:37 +00:00
|
|
|
updateLastseen(packet, account, true);
|
2014-06-06 16:26:40 +00:00
|
|
|
mXmppConnectionService.onContactStatusChanged
|
2014-07-12 00:36:37 +00:00
|
|
|
.onContactStatusChanged(contact, online);
|
2014-06-06 16:26:40 +00:00
|
|
|
}
|
|
|
|
} else if (type.equals("unavailable")) {
|
|
|
|
if (fromParts.length != 2) {
|
|
|
|
contact.clearPresences();
|
|
|
|
} else {
|
|
|
|
contact.removePresence(fromParts[1]);
|
|
|
|
}
|
|
|
|
mXmppConnectionService.onContactStatusChanged
|
2014-07-12 00:36:37 +00:00
|
|
|
.onContactStatusChanged(contact, false);
|
2014-06-06 16:26:40 +00:00
|
|
|
} else if (type.equals("subscribe")) {
|
|
|
|
if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
|
2014-07-12 01:44:23 +00:00
|
|
|
mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.stopPresenceUpdatesTo(contact));
|
2014-06-06 16:26:40 +00:00
|
|
|
if ((contact.getOption(Contact.Options.ASKING))
|
|
|
|
&& (!contact.getOption(Contact.Options.TO))) {
|
2014-07-12 01:44:23 +00:00
|
|
|
mXmppConnectionService.sendPresencePacket(account,mPresenceGenerator.requestPresenceUpdatesFrom(contact));
|
2014-06-06 16:26:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 00:36:37 +00: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 16:26:40 +00:00
|
|
|
}
|