aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/generator/IqGenerator.java
blob: 1a38347b1600bc92a69e8b4332a8725b4cadf7c5 (plain)
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
package eu.siacs.conversations.generator;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.pep.Avatar;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;

public class IqGenerator extends AbstractGenerator {

	

	public IqPacket discoResponse(IqPacket request) {
		IqPacket packet = new IqPacket(IqPacket.TYPE_RESULT);
		packet.setId(request.getId());
		packet.setTo(request.getFrom());
		Element query = packet.addChild("query","http://jabber.org/protocol/disco#info");
		query.setAttribute("node", request.query().getAttribute("node"));
		Element identity = query.addChild("identity");
		identity.setAttribute("category","client");
		identity.setAttribute("type", this.IDENTITY_TYPE);
		identity.setAttribute("name", IDENTITY_NAME);
		List<String> features = Arrays.asList(FEATURES);
		Collections.sort(features);
		for(String feature : features) {
			query.addChild("feature").setAttribute("var",feature);
		}
		return packet;
	}
	
	protected IqPacket publish(String node, Element item) {
		IqPacket packet = new IqPacket(IqPacket.TYPE_SET);
		Element pubsub = packet.addChild("pubsub", "http://jabber.org/protocol/pubsub");
		Element publish = pubsub.addChild("publish");
		publish.setAttribute("node", node);
		publish.addChild(item);
		return packet;
	}
	
	public IqPacket publishAvatar(Avatar avatar) {
		Element item = new Element("item");
		item.setAttribute("id", avatar.sha1sum);
		Element data = item.addChild("data","urn:xmpp:avatar:data");
		data.setContent(avatar.image);
		return publish("urn:xmpp:avatar:data", item);
	}
	
	public IqPacket publishAvatarMetadata(Avatar avatar) {
		Element item = new Element("item");
		item.setAttribute("id", avatar.sha1sum);
		Element metadata = item.addChild("metadata","urn:xmpp:avatar:metadata");
		Element info = metadata.addChild("info");
		info.setAttribute("bytes",avatar.size);
		info.setAttribute("id",avatar.sha1sum);
		info.setAttribute("height",avatar.height);
		info.setAttribute("width",avatar.height);
		info.setAttribute("type", avatar.type);
		return publish("urn:xmpp:avatar:metadata",item);
	}
}