1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
package de.thedevstack.conversationsplus.generator;
import de.thedevstack.conversationsplus.entities.Account;
import de.thedevstack.conversationsplus.entities.Contact;
import de.thedevstack.conversationsplus.entities.MucOptions;
import de.thedevstack.conversationsplus.entities.Presence;
import de.thedevstack.conversationsplus.xml.Element;
import de.thedevstack.conversationsplus.xmpp.disco.FeatureRegistry;
import de.thedevstack.conversationsplus.xmpp.jid.Jid;
import de.thedevstack.conversationsplus.xmpp.openpgp.OpenPgpXep;
import de.thedevstack.conversationsplus.xmpp.stanzas.PresencePacket;
public class PresenceGenerator extends AbstractGenerator {
private static PresencePacket generatePresencePacket(String type, Jid from, Jid to) {
PresencePacket packet = new PresencePacket();
if (null != type) {
packet.setAttribute("type", type);
}
if (null != to) {
packet.setTo(to);
}
if (null != from) {
packet.setFrom(from);
}
return packet;
}
private static PresencePacket subscription(String type, Contact contact) {
return generatePresencePacket(type, contact.getAccount().getJid().toBareJid(), contact.getJid());
}
public static PresencePacket requestPresenceUpdatesFrom(Contact contact) {
return subscription("subscribe", contact);
}
public static PresencePacket stopPresenceUpdatesFrom(Contact contact) {
return subscription("unsubscribe", contact);
}
public static PresencePacket stopPresenceUpdatesTo(Contact contact) {
return subscription("unsubscribed", contact);
}
public static PresencePacket sendPresenceUpdatesTo(Contact contact) {
return subscription("subscribed", contact);
}
public static PresencePacket selfPresence(Account account, Presence.Status status) {
PresencePacket packet = new PresencePacket(status);
packet.setFrom(account.getJid());
String sig = account.getPgpSignature();
if (sig != null) {
packet.addChild(OpenPgpXep.SIGNED_ELEMENT, OpenPgpXep.SIGNED_NAMESPACE).setContent(sig);
}
String capHash = FeatureRegistry.getCapHash();
if (capHash != null) {
Element cap = packet.addChild("c",
"http://jabber.org/protocol/caps");
cap.setAttribute("hash", "sha-1");
cap.setAttribute("node", "http://conversations.im");
cap.setAttribute("ver", capHash);
}
return packet;
}
private static PresencePacket generateOfflinePresencePacket(Jid from, Jid to) {
return generatePresencePacket("unavailable", from, to);
}
public static PresencePacket generateOfflinePresencePacketTo(Account account, Jid to) {
return generateOfflinePresencePacket(account.getJid(), to);
}
public static PresencePacket generateOfflinePresencePacket(Account account) {
return generateOfflinePresencePacket(account.getJid(), null);
}
public static PresencePacket generateMucJoin(Account account, Jid joinJid, MucOptions mucOptions, long lastMessageTransmitted) {
PresencePacket packet = generatePresencePacket(null, account.getJid(), joinJid);
Element x = packet.addChild("x", "http://jabber.org/protocol/muc");
if (mucOptions.getPassword() != null) {
x.addChild("password").setContent(mucOptions.getPassword());
}
Element historyElement = x.addChild("history");
if (mucOptions.mamSupport()) {
// Use MAM instead of the limited muc history to get history
historyElement.setAttribute("maxchars", "0");
} else {
// Fallback to muc history
historyElement.setAttribute("since", PresenceGenerator.getTimestamp(lastMessageTransmitted));
}
String sig = account.getPgpSignature();
if (sig != null) {
packet.addChild(OpenPgpXep.SIGNED_ELEMENT, OpenPgpXep.SIGNED_NAMESPACE).setContent(sig);
}
return packet;
}
public static PresencePacket generateMucRename(Account account, Jid joinJid) {
PresencePacket packet = generatePresencePacket(null, account.getJid(), joinJid);
String sig = account.getPgpSignature();
if (sig != null) {
packet.addChild("status").setContent("online");
packet.addChild(OpenPgpXep.SIGNED_ELEMENT, OpenPgpXep.SIGNED_NAMESPACE).setContent(sig);
}
return packet;
}
}
|