1package im.conversations.android.xmpp.model.stanza;
2
3import eu.siacs.conversations.entities.Account;
4import eu.siacs.conversations.xmpp.Jid;
5import im.conversations.android.xmpp.model.StreamElement;
6import im.conversations.android.xmpp.model.error.Error;
7
8public abstract class Stanza extends StreamElement {
9
10 protected Stanza(final Class<? extends Stanza> clazz) {
11 super(clazz);
12 }
13
14 public Jid getTo() {
15 return this.getAttributeAsJid("to");
16 }
17
18 public Jid getFrom() {
19 return this.getAttributeAsJid("from");
20 }
21
22 public String getId() {
23 return this.getAttribute("id");
24 }
25
26 public void setId(final String id) {
27 this.setAttribute("id", id);
28 }
29
30 public void setFrom(final Jid from) {
31 this.setAttribute("from", from);
32 }
33
34 public void setTo(final Jid to) {
35 this.setAttribute("to", to);
36 }
37
38 public Error getError() {
39 return this.getExtension(Error.class);
40 }
41
42 public boolean isInvalid() {
43 final var to = getTo();
44 final var from = getFrom();
45 return to instanceof Jid.Invalid || from instanceof Jid.Invalid;
46 }
47
48 public boolean fromServer(final Account account) {
49 final Jid from = getFrom();
50 return from == null
51 || from.equals(account.getDomain())
52 || from.equals(account.getJid().asBareJid())
53 || from.equals(account.getJid());
54 }
55
56 public boolean toServer(final Account account) {
57 final Jid to = getTo();
58 return to == null
59 || to.equals(account.getDomain())
60 || to.equals(account.getJid().asBareJid())
61 || to.equals(account.getJid());
62 }
63
64 public boolean fromAccount(final Account account) {
65 final Jid from = getFrom();
66 return from != null && from.asBareJid().equals(account.getJid().asBareJid());
67 }
68}