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, null);
57 }
58
59 public im.conversations.android.xmpp.model.stanza.Presence selfPresence(final Account account, final Presence.Status status, final boolean personal, final String nickname) {
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 if (nickname != null) {
75 Element nick = packet.addChild("nick", "http://jabber.org/protocol/nick");
76 nick.setContent(nickname);
77 }
78 final String capHash = getCapHash(account);
79 if (capHash != null) {
80 Element cap = packet.addChild("c",
81 "http://jabber.org/protocol/caps");
82 cap.setAttribute("hash", "sha-1");
83 cap.setAttribute("node", "https://cheogram.com");
84 cap.setAttribute("ver", capHash);
85 }
86 return packet;
87 }
88
89 public im.conversations.android.xmpp.model.stanza.Presence leave(final MucOptions mucOptions) {
90 im.conversations.android.xmpp.model.stanza.Presence presence = new im.conversations.android.xmpp.model.stanza.Presence();
91 presence.setTo(mucOptions.getSelf().getFullJid());
92 presence.setFrom(mucOptions.getAccount().getJid());
93 presence.setAttribute("type", "unavailable");
94 return presence;
95 }
96
97 public im.conversations.android.xmpp.model.stanza.Presence sendOfflinePresence(Account account) {
98 im.conversations.android.xmpp.model.stanza.Presence packet = new im.conversations.android.xmpp.model.stanza.Presence();
99 packet.setFrom(account.getJid());
100 packet.setAttribute("type", "unavailable");
101 return packet;
102 }
103}