1package eu.siacs.conversations.generator;
2
3import eu.siacs.conversations.entities.Account;
4import eu.siacs.conversations.entities.Contact;
5import eu.siacs.conversations.entities.Presences;
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, int presence) {
41 PresencePacket packet = new PresencePacket();
42 switch(presence) {
43 case Presences.AWAY:
44 packet.addChild("show").setContent("away");
45 break;
46 case Presences.XA:
47 packet.addChild("show").setContent("xa");
48 break;
49 case Presences.CHAT:
50 packet.addChild("show").setContent("chat");
51 break;
52 case Presences.DND:
53 packet.addChild("show").setContent("dnd");
54 break;
55 }
56 packet.setFrom(account.getJid());
57 String sig = account.getPgpSignature();
58 if (sig != null) {
59 packet.addChild("x", "jabber:x:signed").setContent(sig);
60 }
61 String capHash = getCapHash();
62 if (capHash != null) {
63 Element cap = packet.addChild("c",
64 "http://jabber.org/protocol/caps");
65 cap.setAttribute("hash", "sha-1");
66 cap.setAttribute("node", "http://conversations.im");
67 cap.setAttribute("ver", capHash);
68 }
69 return packet;
70 }
71
72 public PresencePacket sendOfflinePresence(Account account) {
73 PresencePacket packet = new PresencePacket();
74 packet.setFrom(account.getJid());
75 packet.setAttribute("type","unavailable");
76 return packet;
77 }
78}