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