PresenceGenerator.java

 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("status").setContent("online");
60			packet.addChild("x", "jabber:x:signed").setContent(sig);
61		}
62		String capHash = getCapHash();
63		if (capHash != null) {
64			Element cap = packet.addChild("c",
65					"http://jabber.org/protocol/caps");
66			cap.setAttribute("hash", "sha-1");
67			cap.setAttribute("node", "http://conversations.im");
68			cap.setAttribute("ver", capHash);
69		}
70		return packet;
71	}
72
73	public PresencePacket sendOfflinePresence(Account account) {
74		PresencePacket packet = new PresencePacket();
75		packet.setFrom(account.getJid());
76		packet.setAttribute("type","unavailable");
77		return packet;
78	}
79}