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