MessagePacket.java

 1package eu.siacs.conversations.xmpp.stanzas;
 2
 3import eu.siacs.conversations.xml.Element;
 4
 5public class MessagePacket extends AbstractStanza {
 6	public static final int TYPE_CHAT = 0;
 7	public static final int TYPE_NORMAL = 2;
 8	public static final int TYPE_GROUPCHAT = 3;
 9	public static final int TYPE_ERROR = 4;
10	public static final int TYPE_HEADLINE = 5;
11
12	public MessagePacket() {
13		super("message");
14	}
15
16	public String getBody() {
17		Element body = this.findChild("body");
18		if (body != null) {
19			return body.getContent();
20		} else {
21			return null;
22		}
23	}
24
25	public void setBody(String text) {
26		this.children.remove(findChild("body"));
27		Element body = new Element("body");
28		body.setContent(text);
29		this.children.add(0, body);
30	}
31
32	public void setType(int type) {
33		switch (type) {
34		case TYPE_CHAT:
35			this.setAttribute("type", "chat");
36			break;
37		case TYPE_GROUPCHAT:
38			this.setAttribute("type", "groupchat");
39			break;
40		case TYPE_NORMAL:
41			break;
42		case TYPE_ERROR:
43			this.setAttribute("type","error");
44			break;
45		default:
46			this.setAttribute("type", "chat");
47			break;
48		}
49	}
50
51	public int getType() {
52		String type = getAttribute("type");
53		if (type == null) {
54			return TYPE_NORMAL;
55		} else if (type.equals("normal")) {
56			return TYPE_NORMAL;
57		} else if (type.equals("chat")) {
58			return TYPE_CHAT;
59		} else if (type.equals("groupchat")) {
60			return TYPE_GROUPCHAT;
61		} else if (type.equals("error")) {
62			return TYPE_ERROR;
63		} else if (type.equals("headline")) {
64			return TYPE_HEADLINE;
65		} else {
66			return TYPE_NORMAL;
67		}
68	}
69
70	public MessagePacket getForwardedMessagePacket(String name, String namespace) {
71		Element wrapper = findChild(name, namespace);
72		if (wrapper == null) {
73			return null;
74		}
75		Element forwarded = wrapper.findChild("forwarded","urn:xmpp:forward:0");
76		if (forwarded == null) {
77			return null;
78		}
79		return MessagePacket.create(forwarded.findChild("message"));
80	}
81
82
83	public static MessagePacket create(Element element) {
84		if (element == null) {
85			return null;
86		}
87		MessagePacket packet = new MessagePacket();
88		packet.setAttributes(element.getAttributes());
89		packet.setChildren(element.getChildren());
90		return packet;
91	}
92}