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 if (to instanceof Jid.Invalid || from instanceof Jid.Invalid) {
46 return true;
47 }
48 return false;
49 }
50
51 public boolean fromServer(final Account account) {
52 final Jid from = getFrom();
53 return from == null
54 || from.equals(account.getDomain())
55 || from.equals(account.getJid().asBareJid())
56 || from.equals(account.getJid());
57 }
58
59 public boolean toServer(final Account account) {
60 final Jid to = getTo();
61 return to == null
62 || to.equals(account.getDomain())
63 || to.equals(account.getJid().asBareJid())
64 || to.equals(account.getJid());
65 }
66
67 public boolean fromAccount(final Account account) {
68 final Jid from = getFrom();
69 return from != null && from.asBareJid().equals(account.getJid().asBareJid());
70 }
71}