PresenceGenerator.java

  1package eu.siacs.conversations.generator;
  2
  3import android.text.TextUtils;
  4import eu.siacs.conversations.entities.Account;
  5import eu.siacs.conversations.entities.Contact;
  6import eu.siacs.conversations.entities.MucOptions;
  7import eu.siacs.conversations.services.XmppConnectionService;
  8import eu.siacs.conversations.xml.Element;
  9import eu.siacs.conversations.xml.Namespace;
 10import im.conversations.android.xmpp.model.stanza.Presence;
 11
 12public class PresenceGenerator extends AbstractGenerator {
 13
 14    public PresenceGenerator(XmppConnectionService service) {
 15        super(service);
 16    }
 17
 18    private im.conversations.android.xmpp.model.stanza.Presence subscription(
 19            String type, Contact contact) {
 20        im.conversations.android.xmpp.model.stanza.Presence packet =
 21                new im.conversations.android.xmpp.model.stanza.Presence();
 22        packet.setAttribute("type", type);
 23        packet.setTo(contact.getJid());
 24        packet.setFrom(contact.getAccount().getJid().asBareJid());
 25        return packet;
 26    }
 27
 28    public im.conversations.android.xmpp.model.stanza.Presence requestPresenceUpdatesFrom(
 29            final Contact contact) {
 30        return requestPresenceUpdatesFrom(contact, null);
 31    }
 32
 33    public im.conversations.android.xmpp.model.stanza.Presence requestPresenceUpdatesFrom(
 34            final Contact contact, final String preAuth) {
 35        im.conversations.android.xmpp.model.stanza.Presence packet =
 36                subscription("subscribe", contact);
 37        String displayName = contact.getAccount().getDisplayName();
 38        if (!TextUtils.isEmpty(displayName)) {
 39            packet.addChild("nick", Namespace.NICK).setContent(displayName);
 40        }
 41        if (preAuth != null) {
 42            packet.addChild("preauth", Namespace.PARS).setAttribute("token", preAuth);
 43        }
 44        return packet;
 45    }
 46
 47    public im.conversations.android.xmpp.model.stanza.Presence stopPresenceUpdatesFrom(
 48            Contact contact) {
 49        return subscription("unsubscribe", contact);
 50    }
 51
 52    public im.conversations.android.xmpp.model.stanza.Presence stopPresenceUpdatesTo(
 53            Contact contact) {
 54        return subscription("unsubscribed", contact);
 55    }
 56
 57    public im.conversations.android.xmpp.model.stanza.Presence sendPresenceUpdatesTo(
 58            Contact contact) {
 59        return subscription("subscribed", contact);
 60    }
 61
 62    public im.conversations.android.xmpp.model.stanza.Presence selfPresence(
 63            Account account, Presence.Availability status) {
 64        return selfPresence(account, status, true);
 65    }
 66
 67    public im.conversations.android.xmpp.model.stanza.Presence selfPresence(
 68            final Account account, final Presence.Availability status, final boolean personal) {
 69        final im.conversations.android.xmpp.model.stanza.Presence packet =
 70                new im.conversations.android.xmpp.model.stanza.Presence();
 71        if (personal) {
 72            final String sig = account.getPgpSignature();
 73            final String message = account.getPresenceStatusMessage();
 74            packet.setAvailability(status);
 75            packet.setStatus(message);
 76            if (sig != null && mXmppConnectionService.getPgpEngine() != null) {
 77                packet.addChild("x", "jabber:x:signed").setContent(sig);
 78            }
 79        }
 80        final String capHash = getCapHash(account);
 81        if (capHash != null) {
 82            Element cap = packet.addChild("c", "http://jabber.org/protocol/caps");
 83            cap.setAttribute("hash", "sha-1");
 84            cap.setAttribute("node", "http://conversations.im");
 85            cap.setAttribute("ver", capHash);
 86        }
 87        return packet;
 88    }
 89
 90    public im.conversations.android.xmpp.model.stanza.Presence leave(final MucOptions mucOptions) {
 91        im.conversations.android.xmpp.model.stanza.Presence presence =
 92                new im.conversations.android.xmpp.model.stanza.Presence();
 93        presence.setTo(mucOptions.getSelf().getFullJid());
 94        presence.setFrom(mucOptions.getAccount().getJid());
 95        presence.setAttribute("type", "unavailable");
 96        return presence;
 97    }
 98
 99    public im.conversations.android.xmpp.model.stanza.Presence sendOfflinePresence(
100            Account account) {
101        im.conversations.android.xmpp.model.stanza.Presence packet =
102                new im.conversations.android.xmpp.model.stanza.Presence();
103        packet.setFrom(account.getJid());
104        packet.setAttribute("type", "unavailable");
105        return packet;
106    }
107}