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