Debian Codegraph @

xmpp-vala/src/module/stanza.vala

raw
1 namespace Xmpp {
2
3 public class Stanza : Object {
4
5 public const string ATTRIBUTE_FROM = "from";
6 public const string ATTRIBUTE_ID = "id";
7 public const string ATTRIBUTE_TO = "to";
8 public const string ATTRIBUTE_TYPE = "type";
9
10 public const string TYPE_ERROR = "error";
11
12 private Jid? my_jid;
13 private Jid? from_;
14 private Jid? to_;
15
16 public virtual Jid? from {
17 owned get {
18 string? from_attribute = stanza.get_attribute(ATTRIBUTE_FROM);
19 // "when a client receives a stanza that does not include a 'from' attribute, it MUST assume that the stanza
20 // is from the user's account on the server." (RFC6120 8.1.2.1)
21 if (from_attribute != null) {
22 try {
23 return from_ = new Jid(from_attribute);
24 } catch (InvalidJidError e) {
25 warning("Ignoring invalid from Jid: %s", e.message);
26 }
27 }
28 if (my_jid != null) {
29 return my_jid.bare_jid;
30 }
31 return null;
32 }
33 set { stanza.set_attribute(ATTRIBUTE_FROM, value.to_string()); }
34 }
35
36 public virtual string? id {
37 get { return stanza.get_attribute(ATTRIBUTE_ID); }
38 set { stanza.set_attribute(ATTRIBUTE_ID, value); }
39 }
40
41 public virtual Jid? to {
42 owned get {
43 string? to_attribute = stanza.get_attribute(ATTRIBUTE_TO);
44 // "if the stanza does not include a 'to' address then the client MUST treat it as if the 'to' address were
45 // included with a value of the client's full JID." (RFC6120 8.1.1.1)
46 try {
47 return to_attribute == null ? my_jid : to_ = new Jid(to_attribute);
48 } catch (InvalidJidError e) {
49 warning("Ignoring invalid to Jid: %s", e.message);
50 }
51 return my_jid;
52 }
53 set { stanza.set_attribute(ATTRIBUTE_TO, value.to_string()); }
54 }
55
56 public virtual string? type_ {
57 get { return stanza.get_attribute(ATTRIBUTE_TYPE); }
58 set { stanza.set_attribute(ATTRIBUTE_TYPE, value); }
59 }
60
61 public StanzaNode stanza;
62
63 public Stanza.incoming(StanzaNode stanza, Jid? my_jid) {
64 this.stanza = stanza;
65 this.my_jid = my_jid;
66 }
67
68 public Stanza.outgoing(StanzaNode stanza) {
69 this.stanza = stanza;
70 }
71
72 public bool is_error() {
73 return type_ == TYPE_ERROR;
74 }
75
76 public ErrorStanza? get_error() {
77 return ErrorStanza.from_stanza(this.stanza);
78 }
79 }
80
81 }