1package eu.siacs.conversations.generator;
2
3import eu.siacs.conversations.entities.Account;
4import eu.siacs.conversations.entities.Contact;
5import eu.siacs.conversations.entities.Presence;
6import eu.siacs.conversations.services.XmppConnectionService;
7import eu.siacs.conversations.xml.Element;
8import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
9
10public class PresenceGenerator extends AbstractGenerator {
11
12 public PresenceGenerator(XmppConnectionService service) {
13 super(service);
14 }
15
16 private PresencePacket subscription(String type, Contact contact) {
17 PresencePacket packet = new PresencePacket();
18 packet.setAttribute("type", type);
19 packet.setTo(contact.getJid());
20 packet.setFrom(contact.getAccount().getJid().toBareJid());
21 return packet;
22 }
23
24 public PresencePacket requestPresenceUpdatesFrom(Contact contact) {
25 return subscription("subscribe", contact);
26 }
27
28 public PresencePacket stopPresenceUpdatesFrom(Contact contact) {
29 return subscription("unsubscribe", contact);
30 }
31
32 public PresencePacket stopPresenceUpdatesTo(Contact contact) {
33 return subscription("unsubscribed", contact);
34 }
35
36 public PresencePacket sendPresenceUpdatesTo(Contact contact) {
37 return subscription("subscribed", contact);
38 }
39
40 public PresencePacket selfPresence(Account account, Presence.Status status) {
41 PresencePacket packet = new PresencePacket();
42 if(status.toShowString() != null) {
43 packet.addChild("show").setContent(status.toShowString());
44 }
45 packet.setFrom(account.getJid());
46 String sig = account.getPgpSignature();
47 if (sig != null && mXmppConnectionService.getPgpEngine() != null) {
48 packet.addChild("x", "jabber:x:signed").setContent(sig);
49 }
50 String capHash = getCapHash();
51 if (capHash != null) {
52 Element cap = packet.addChild("c",
53 "http://jabber.org/protocol/caps");
54 cap.setAttribute("hash", "sha-1");
55 cap.setAttribute("node", "http://conversations.im");
56 cap.setAttribute("ver", capHash);
57 }
58 return packet;
59 }
60
61 public PresencePacket sendOfflinePresence(Account account) {
62 PresencePacket packet = new PresencePacket();
63 packet.setFrom(account.getJid());
64 packet.setAttribute("type","unavailable");
65 return packet;
66 }
67}