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