MessagePacket.java

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