2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.xmpp.stanzas;
|
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.parser.AbstractParser;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2019-09-12 08:12:47 +00:00
|
|
|
import eu.siacs.conversations.xml.LocalizedContent;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2015-08-13 16:25:10 +00:00
|
|
|
public class MessagePacket extends AbstractAcknowledgeableStanza {
|
2014-10-22 16:38:44 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2019-09-12 08:12:47 +00:00
|
|
|
public LocalizedContent getBody() {
|
|
|
|
return findInternationalizedChildContentInDefaultNamespace("body");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setBody(String text) {
|
|
|
|
this.children.remove(findChild("body"));
|
|
|
|
Element body = new Element("body");
|
|
|
|
body.setContent(text);
|
2015-02-15 19:18:41 +00:00
|
|
|
this.children.add(0, body);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 14:58:24 +00:00
|
|
|
public void setAxolotlMessage(Element axolotlMessage) {
|
|
|
|
this.children.remove(findChild("body"));
|
|
|
|
this.children.add(0, axolotlMessage);
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
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;
|
2015-03-21 15:07:17 +00:00
|
|
|
case TYPE_ERROR:
|
|
|
|
this.setAttribute("type","error");
|
|
|
|
break;
|
2014-10-22 16:38:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 12:42:21 +00:00
|
|
|
|
2015-05-15 03:14:15 +00:00
|
|
|
public Pair<MessagePacket,Long> getForwardedMessagePacket(String name, String namespace) {
|
2015-05-14 12:42:21 +00:00
|
|
|
Element wrapper = findChild(name, namespace);
|
|
|
|
if (wrapper == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
Element forwarded = wrapper.findChild("forwarded", "urn:xmpp:forward:0");
|
2015-05-14 12:42:21 +00:00
|
|
|
if (forwarded == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
MessagePacket packet = create(forwarded.findChild("message"));
|
|
|
|
if (packet == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-06-04 14:16:14 +00:00
|
|
|
Long timestamp = AbstractParser.parseTimestamp(forwarded, null);
|
2015-05-15 03:14:15 +00:00
|
|
|
return new Pair(packet,timestamp);
|
2015-05-14 12:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|