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.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				muc.getMucOptions().processPacket(packet, mPgpEngine);
 28			}
 29		} else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
 30			Conversation muc = mXmppConnectionService.find(account,packet
 31					.getAttribute("from").split("/")[0]);
 32			if (muc != null) {
 33				muc.getMucOptions().processPacket(packet, mPgpEngine);
 34			}
 35		}
 36		mXmppConnectionService.updateConversationUi();
 37	}
 38
 39	public void parseContactPresence(PresencePacket packet, Account account) {
 40		PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator();
 41		if (packet.getFrom() == null) {
 42			return;
 43		}
 44		String[] fromParts = packet.getFrom().split("/");
 45		String type = packet.getAttribute("type");
 46		if (fromParts[0].equals(account.getJid())) {
 47			if (fromParts.length == 2) {
 48				if (type == null) {
 49					account.updatePresence(fromParts[1],
 50							Presences.parseShow(packet.findChild("show")));
 51				} else if (type.equals("unavailable")) {
 52					account.removePresence(fromParts[1]);
 53				}
 54			}
 55
 56		} else {
 57			Contact contact = account.getRoster().getContact(packet.getFrom());
 58			if (type == null) {
 59				if (fromParts.length == 2) {
 60					int sizeBefore = contact.getPresences().size();
 61					contact.updatePresence(fromParts[1],
 62							Presences.parseShow(packet.findChild("show")));
 63					PgpEngine pgp = mXmppConnectionService.getPgpEngine();
 64					if (pgp != null) {
 65						Element x = packet.findChild("x", "jabber:x:signed");
 66						if (x != null) {
 67							Element status = packet.findChild("status");
 68							String msg;
 69							if (status != null) {
 70								msg = status.getContent();
 71							} else {
 72								msg = "";
 73							}
 74							contact.setPgpKeyId(pgp.fetchKeyId(account, msg,
 75									x.getContent()));
 76						}
 77					}
 78					boolean online = sizeBefore < contact.getPresences().size();
 79					updateLastseen(packet, account, true);
 80					mXmppConnectionService.onContactStatusChanged
 81							.onContactStatusChanged(contact, online);
 82				}
 83			} else if (type.equals("unavailable")) {
 84				if (fromParts.length != 2) {
 85					contact.clearPresences();
 86				} else {
 87					contact.removePresence(fromParts[1]);
 88				}
 89				mXmppConnectionService.onContactStatusChanged
 90						.onContactStatusChanged(contact, false);
 91			} else if (type.equals("subscribe")) {
 92				if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
 93					mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
 94				} else {
 95					contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
 96				}
 97			}
 98		}
 99		mXmppConnectionService.updateRosterUi();
100	}
101
102	@Override
103	public void onPresencePacketReceived(Account account, PresencePacket packet) {
104		if (packet.hasChild("x", "http://jabber.org/protocol/muc#user")) {
105			this.parseConferencePresence(packet, account);
106		} else if (packet.hasChild("x", "http://jabber.org/protocol/muc")) {
107			this.parseConferencePresence(packet, account);
108		} else {
109			this.parseContactPresence(packet, account);
110		}
111	}
112
113}