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