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