aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/stanza.vala
blob: 2bf3e29bd74b506f4a7f433732fa81ab6580fa8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Xmpp.Core;

namespace Xmpp {

    public class Stanza : Object {

        public const string ATTRIBUTE_FROM = "from";
        public const string ATTRIBUTE_ID = "id";
        public const string ATTRIBUTE_TO = "to";
        public const string ATTRIBUTE_TYPE = "type";

        public const string TYPE_ERROR = "error";

        private string? my_jid;

        public virtual string? from {
            owned get {
                string? from_attribute = stanza.get_attribute(ATTRIBUTE_FROM);
                // "when a client receives a stanza that does not include a 'from' attribute, it MUST assume that the stanza
                // is from the user's account on the server." (RFC6120 8.1.2.1)
                if (from_attribute != null) return from_attribute;
                if (my_jid != null) {
                    string my_bare_jid = get_bare_jid(my_jid); // has to be left-side value
                    return my_bare_jid;
                }
                return null;
            }
            set { stanza.set_attribute(ATTRIBUTE_FROM, value); }
        }

        public virtual string? id {
            get { return stanza.get_attribute(ATTRIBUTE_ID); }
            set { stanza.set_attribute(ATTRIBUTE_ID, value); }
        }

        public virtual string? to {
            owned get {
                string? to_attribute = stanza.get_attribute(ATTRIBUTE_TO);
                // "if the stanza does not include a 'to' address then the client MUST treat it as if the 'to' address were
                // included with a value of the client's full JID." (RFC6120 8.1.1.1)
                return to_attribute == null ? my_jid : to_attribute;
            }
            set { stanza.set_attribute(ATTRIBUTE_TO, value); }
        }

        public virtual string? type_ {
            get { return stanza.get_attribute(ATTRIBUTE_TYPE); }
            set { stanza.set_attribute(ATTRIBUTE_TYPE, value); }
        }

        public StanzaNode stanza;

        public Stanza.incoming(StanzaNode stanza, string? my_jid) {
            this.stanza = stanza;
            this.my_jid = my_jid;
        }

        public Stanza.outgoing(StanzaNode stanza) {
            this.stanza = stanza;
        }

        public bool is_error() {
            return type_ == TYPE_ERROR;
        }

        public ErrorStanza? get_error() {
            return new ErrorStanza.from_stanza(this.stanza);
        }
    }
}