1package eu.siacs.conversations.xmpp.stanzas;
2
3import android.util.Pair;
4
5import java.text.ParseException;
6
7import eu.siacs.conversations.parser.AbstractParser;
8import eu.siacs.conversations.xml.Element;
9
10public class MessagePacket extends AbstractStanza {
11 public static final int TYPE_CHAT = 0;
12 public static final int TYPE_NORMAL = 2;
13 public static final int TYPE_GROUPCHAT = 3;
14 public static final int TYPE_ERROR = 4;
15 public static final int TYPE_HEADLINE = 5;
16
17 public MessagePacket() {
18 super("message");
19 }
20
21 public String getBody() {
22 return findChildContent("body");
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 setAxolotlMessage(Element axolotlMessage) {
33 this.children.remove(findChild("body"));
34 this.children.add(0, axolotlMessage);
35 }
36
37 public void setType(int type) {
38 switch (type) {
39 case TYPE_CHAT:
40 this.setAttribute("type", "chat");
41 break;
42 case TYPE_GROUPCHAT:
43 this.setAttribute("type", "groupchat");
44 break;
45 case TYPE_NORMAL:
46 break;
47 case TYPE_ERROR:
48 this.setAttribute("type","error");
49 break;
50 default:
51 this.setAttribute("type", "chat");
52 break;
53 }
54 }
55
56 public int getType() {
57 String type = getAttribute("type");
58 if (type == null) {
59 return TYPE_NORMAL;
60 } else if (type.equals("normal")) {
61 return TYPE_NORMAL;
62 } else if (type.equals("chat")) {
63 return TYPE_CHAT;
64 } else if (type.equals("groupchat")) {
65 return TYPE_GROUPCHAT;
66 } else if (type.equals("error")) {
67 return TYPE_ERROR;
68 } else if (type.equals("headline")) {
69 return TYPE_HEADLINE;
70 } else {
71 return TYPE_NORMAL;
72 }
73 }
74
75 public Pair<MessagePacket,Long> getForwardedMessagePacket(String name, String namespace) {
76 Element wrapper = findChild(name, namespace);
77 if (wrapper == null) {
78 return null;
79 }
80 Element forwarded = wrapper.findChild("forwarded", "urn:xmpp:forward:0");
81 if (forwarded == null) {
82 return null;
83 }
84 MessagePacket packet = create(forwarded.findChild("message"));
85 if (packet == null) {
86 return null;
87 }
88 Long timestamp = AbstractParser.getTimestamp(forwarded,null);
89 return new Pair(packet,timestamp);
90 }
91
92 public static MessagePacket create(Element element) {
93 if (element == null) {
94 return null;
95 }
96 MessagePacket packet = new MessagePacket();
97 packet.setAttributes(element.getAttributes());
98 packet.setChildren(element.getChildren());
99 return packet;
100 }
101}