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(Contact contact) {
29 PresencePacket packet = subscription("subscribe", contact);
30 String displayName = contact.getAccount().getDisplayName();
31 if (!TextUtils.isEmpty(displayName)) {
32 packet.addChild("nick", Namespace.NICK).setContent(displayName);
33 }
34 return packet;
35 }
36
37 public PresencePacket stopPresenceUpdatesFrom(Contact contact) {
38 return subscription("unsubscribe", contact);
39 }
40
41 public PresencePacket stopPresenceUpdatesTo(Contact contact) {
42 return subscription("unsubscribed", contact);
43 }
44
45 public PresencePacket sendPresenceUpdatesTo(Contact contact) {
46 return subscription("subscribed", contact);
47 }
48
49 public PresencePacket selfPresence(Account account, Presence.Status status) {
50 return selfPresence(account, status, true);
51 }
52
53 public PresencePacket selfPresence(final Account account, final Presence.Status status, final boolean personal) {
54 final PresencePacket packet = new PresencePacket();
55 if (personal) {
56 final String sig = account.getPgpSignature();
57 final String message = account.getPresenceStatusMessage();
58 if (status.toShowString() != null) {
59 packet.addChild("show").setContent(status.toShowString());
60 }
61 if (!TextUtils.isEmpty(message)) {
62 packet.addChild(new Element("status").setContent(message));
63 }
64 if (sig != null && mXmppConnectionService.getPgpEngine() != null) {
65 packet.addChild("x", "jabber:x:signed").setContent(sig);
66 }
67 }
68 final String capHash = getCapHash(account);
69 if (capHash != null) {
70 Element cap = packet.addChild("c",
71 "http://jabber.org/protocol/caps");
72 cap.setAttribute("hash", "sha-1");
73 cap.setAttribute("node", "http://conversations.im");
74 cap.setAttribute("ver", capHash);
75 }
76 return packet;
77 }
78
79 public PresencePacket leave(final MucOptions mucOptions) {
80 PresencePacket presencePacket = new PresencePacket();
81 presencePacket.setTo(mucOptions.getSelf().getFullJid());
82 presencePacket.setFrom(mucOptions.getAccount().getJid());
83 presencePacket.setAttribute("type", "unavailable");
84 return presencePacket;
85 }
86
87 public PresencePacket sendOfflinePresence(Account account) {
88 PresencePacket packet = new PresencePacket();
89 packet.setFrom(account.getJid());
90 packet.setAttribute("type", "unavailable");
91 return packet;
92 }
93}