aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/xmpp/stanzas/MessagePacket.java
blob: e32811afe39bd04f22b6b7f0cb0f4ae769428bf1 (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
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
package eu.siacs.conversations.xmpp.stanzas;

import android.util.Pair;

import java.text.ParseException;

import eu.siacs.conversations.parser.AbstractParser;
import eu.siacs.conversations.xml.Element;

public class MessagePacket extends AbstractStanza {
	public static final int TYPE_CHAT = 0;
	public static final int TYPE_NORMAL = 2;
	public static final int TYPE_GROUPCHAT = 3;
	public static final int TYPE_ERROR = 4;
	public static final int TYPE_HEADLINE = 5;

	public MessagePacket() {
		super("message");
	}

	public String getBody() {
		return findChildContent("body");
	}

	public void setBody(String text) {
		this.children.remove(findChild("body"));
		Element body = new Element("body");
		body.setContent(text);
		this.children.add(0, body);
	}

	public void setType(int type) {
		switch (type) {
		case TYPE_CHAT:
			this.setAttribute("type", "chat");
			break;
		case TYPE_GROUPCHAT:
			this.setAttribute("type", "groupchat");
			break;
		case TYPE_NORMAL:
			break;
		case TYPE_ERROR:
			this.setAttribute("type","error");
			break;
		default:
			this.setAttribute("type", "chat");
			break;
		}
	}

	public int getType() {
		String type = getAttribute("type");
		if (type == null) {
			return TYPE_NORMAL;
		} else if (type.equals("normal")) {
			return TYPE_NORMAL;
		} else if (type.equals("chat")) {
			return TYPE_CHAT;
		} else if (type.equals("groupchat")) {
			return TYPE_GROUPCHAT;
		} else if (type.equals("error")) {
			return TYPE_ERROR;
		} else if (type.equals("headline")) {
			return TYPE_HEADLINE;
		} else {
			return TYPE_NORMAL;
		}
	}

	public Pair<MessagePacket,Long> getForwardedMessagePacket(String name, String namespace) {
		Element wrapper = findChild(name, namespace);
		if (wrapper == null) {
			return null;
		}
		Element forwarded = wrapper.findChild("forwarded", "urn:xmpp:forward:0");
		if (forwarded == null) {
			return null;
		}
		MessagePacket packet = create(forwarded.findChild("message"));
		if (packet == null) {
			return null;
		}
		Long timestamp = AbstractParser.getTimestamp(forwarded,null);
		return new Pair(packet,timestamp);
	}

	public static MessagePacket create(Element element) {
		if (element == null) {
			return null;
		}
		MessagePacket packet = new MessagePacket();
		packet.setAttributes(element.getAttributes());
		packet.setChildren(element.getChildren());
		return packet;
	}
}