Stanza.java

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