PresenceGenerator.java

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