2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.parser;
|
|
|
|
|
2015-02-21 21:19:10 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.crypto.PgpEngine;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2014-11-20 21:44:31 +00:00
|
|
|
import eu.siacs.conversations.entities.MucOptions;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.entities.Presences;
|
|
|
|
import eu.siacs.conversations.generator.PresenceGenerator;
|
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
|
|
|
import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
|
2014-11-06 19:10:03 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
|
|
|
|
|
|
|
|
public class PresenceParser extends AbstractParser implements
|
|
|
|
OnPresencePacketReceived {
|
|
|
|
|
|
|
|
public PresenceParser(XmppConnectionService service) {
|
|
|
|
super(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void parseConferencePresence(PresencePacket packet, Account account) {
|
|
|
|
PgpEngine mPgpEngine = mXmppConnectionService.getPgpEngine();
|
2014-11-21 19:02:13 +00:00
|
|
|
final Conversation conversation = packet.getFrom() == null ? null : mXmppConnectionService.find(account, packet.getFrom().toBareJid());
|
|
|
|
if (conversation != null) {
|
|
|
|
final MucOptions mucOptions = conversation.getMucOptions();
|
|
|
|
boolean before = mucOptions.online();
|
|
|
|
int count = mucOptions.getUsers().size();
|
2015-02-22 12:16:07 +00:00
|
|
|
final ArrayList<MucOptions.User> tileUserBefore = new ArrayList<>(mucOptions.getUsers().subList(0,Math.min(mucOptions.getUsers().size(),5)));
|
2014-11-21 19:02:13 +00:00
|
|
|
mucOptions.processPacket(packet, mPgpEngine);
|
2015-02-22 12:16:07 +00:00
|
|
|
final ArrayList<MucOptions.User> tileUserAfter = new ArrayList<>(mucOptions.getUsers().subList(0,Math.min(mucOptions.getUsers().size(),5)));
|
2015-02-21 21:19:10 +00:00
|
|
|
if (!tileUserAfter.equals(tileUserBefore)) {
|
|
|
|
mXmppConnectionService.getAvatarService().clear(conversation);
|
|
|
|
}
|
2014-11-21 19:02:13 +00:00
|
|
|
if (before != mucOptions.online() || (mucOptions.online() && count != mucOptions.getUsers().size())) {
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
2014-11-23 14:19:44 +00:00
|
|
|
} else if (mucOptions.online()) {
|
|
|
|
mXmppConnectionService.updateMucRosterUi();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void parseContactPresence(PresencePacket packet, Account account) {
|
|
|
|
PresenceGenerator mPresenceGenerator = mXmppConnectionService
|
|
|
|
.getPresenceGenerator();
|
|
|
|
if (packet.getFrom() == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-21 19:02:13 +00:00
|
|
|
final Jid from = packet.getFrom();
|
2014-10-22 16:38:44 +00:00
|
|
|
String type = packet.getAttribute("type");
|
2014-12-20 16:23:03 +00:00
|
|
|
Contact contact = account.getRoster().getContact(packet.getFrom());
|
|
|
|
if (type == null) {
|
|
|
|
String presence;
|
2014-11-09 18:13:19 +00:00
|
|
|
if (!from.isBareJid()) {
|
2014-12-20 16:23:03 +00:00
|
|
|
presence = from.getResourcepart();
|
|
|
|
} else {
|
|
|
|
presence = "";
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2014-12-20 16:23:03 +00:00
|
|
|
int sizeBefore = contact.getPresences().size();
|
|
|
|
contact.updatePresence(presence,
|
|
|
|
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 = "";
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2014-12-20 16:23:03 +00:00
|
|
|
contact.setPgpKeyId(pgp.fetchKeyId(account, msg,
|
|
|
|
x.getContent()));
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-20 16:23:03 +00:00
|
|
|
boolean online = sizeBefore < contact.getPresences().size();
|
2015-01-03 12:01:38 +00:00
|
|
|
updateLastseen(packet, account, false);
|
|
|
|
mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, online);
|
2014-12-20 16:23:03 +00:00
|
|
|
} else if (type.equals("unavailable")) {
|
|
|
|
if (from.isBareJid()) {
|
|
|
|
contact.clearPresences();
|
|
|
|
} else {
|
|
|
|
contact.removePresence(from.getResourcepart());
|
|
|
|
}
|
|
|
|
mXmppConnectionService.onContactStatusChanged
|
|
|
|
.onContactStatusChanged(contact, false);
|
|
|
|
} else if (type.equals("subscribe")) {
|
|
|
|
if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
|
|
|
|
mXmppConnectionService.sendPresencePacket(account,
|
|
|
|
mPresenceGenerator.sendPresenceUpdatesTo(contact));
|
|
|
|
} else {
|
|
|
|
contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-20 16:23:03 +00:00
|
|
|
Element nick = packet.findChild("nick",
|
|
|
|
"http://jabber.org/protocol/nick");
|
|
|
|
if (nick != null) {
|
|
|
|
contact.setPresenceName(nick.getContent());
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
mXmppConnectionService.updateRosterUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|