PresenceParser.java

  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.services.XmppConnectionService;
  9import eu.siacs.conversations.xml.Element;
 10import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
 11
 12public class PresenceParser extends AbstractParser {
 13
 14	public PresenceParser(XmppConnectionService service) {
 15		super(service);
 16	}
 17
 18	public void parseConferencePresence(PresencePacket packet, Account account) {
 19		PgpEngine mPgpEngine = mXmppConnectionService.getPgpEngine();
 20		if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
 21			Conversation muc = mXmppConnectionService.findMuc(packet
 22					.getAttribute("from").split("/")[0], account);
 23			if (muc != null) {
 24				muc.getMucOptions().processPacket(packet, mPgpEngine);
 25			}
 26		} else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
 27			Conversation muc = mXmppConnectionService.findMuc(packet
 28					.getAttribute("from").split("/")[0], account);
 29			if (muc != null) {
 30				int error = muc.getMucOptions().getError();
 31				muc.getMucOptions().processPacket(packet, mPgpEngine);
 32				if (muc.getMucOptions().getError() != error) {
 33					mXmppConnectionService.updateUi(muc, false);
 34				}
 35			}
 36		}
 37	}
 38
 39	public void parseContactPresence(PresencePacket packet, Account account) {
 40		if (packet.getFrom()==null) {
 41			return;
 42		}
 43		String[] fromParts = packet.getFrom().split("/");
 44		String type = packet.getAttribute("type");
 45		if (fromParts[0].equals(account.getJid())) {
 46			if (fromParts.length == 2) {
 47				if (type == null) {
 48					account.updatePresence(fromParts[1],
 49							Presences.parseShow(packet.findChild("show")));
 50				} else if (type.equals("unavailable")) {
 51					account.removePresence(fromParts[1]);
 52				}
 53			}
 54
 55		} else {
 56			Contact contact = account.getRoster().getContact(packet.getFrom());
 57			if (type == null) {
 58				if (fromParts.length == 2) {
 59					int sizeBefore = contact.getPresences().size();
 60					contact.updatePresence(fromParts[1],
 61							Presences.parseShow(packet.findChild("show")));
 62					PgpEngine pgp = mXmppConnectionService.getPgpEngine();
 63					if (pgp != null) {
 64						Element x = packet.findChild("x", "jabber:x:signed");
 65						if (x != null) {
 66							Element status = packet.findChild("status");
 67							String msg;
 68							if (status != null) {
 69								msg = status.getContent();
 70							} else {
 71								msg = "";
 72							}
 73							contact.setPgpKeyId(pgp.fetchKeyId(account, msg,
 74									x.getContent()));
 75						}
 76					}
 77					boolean online = sizeBefore < contact.getPresences().size();
 78					updateLastseen(packet, account,true);
 79					mXmppConnectionService.onContactStatusChanged
 80							.onContactStatusChanged(contact,online);
 81				}
 82			} else if (type.equals("unavailable")) {
 83				if (fromParts.length != 2) {
 84					contact.clearPresences();
 85				} else {
 86					contact.removePresence(fromParts[1]);
 87				}
 88				mXmppConnectionService.onContactStatusChanged
 89						.onContactStatusChanged(contact,false);
 90			} else if (type.equals("subscribe")) {
 91				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
 92					mXmppConnectionService.sendPresenceUpdatesTo(contact);
 93					if ((contact.getOption(Contact.Options.ASKING))
 94							&& (!contact.getOption(Contact.Options.TO))) {
 95						mXmppConnectionService
 96								.requestPresenceUpdatesFrom(contact);
 97					}
 98				} else {
 99					contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
100				}
101			}
102		}
103	}
104
105}