anotherim-desktop/xmpp-vala/src/module/stanza.vala

81 lines
2.5 KiB
Vala
Raw Normal View History

2017-03-02 14:37:32 +00:00
namespace Xmpp {
public class Stanza : Object {
2017-03-02 14:37:32 +00:00
public const string ATTRIBUTE_FROM = "from";
public const string ATTRIBUTE_ID = "id";
public const string ATTRIBUTE_TO = "to";
public const string ATTRIBUTE_TYPE = "type";
2017-03-02 14:37:32 +00:00
public const string TYPE_ERROR = "error";
2017-03-02 14:37:32 +00:00
private Jid? my_jid;
private Jid? from_;
private Jid? to_;
2017-03-02 14:37:32 +00:00
public virtual Jid? 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)
2019-12-22 03:10:53 +00:00
if (from_attribute != null) {
try {
return from_ = new Jid(from_attribute);
} catch (InvalidJidError e) {
warning("Ignoring invalid from Jid: %s", e.message);
}
}
if (my_jid != null) {
return my_jid.bare_jid;
2017-03-02 14:37:32 +00:00
}
return null;
2017-03-02 14:37:32 +00:00
}
set { stanza.set_attribute(ATTRIBUTE_FROM, value.to_string()); }
}
2017-03-02 14:37:32 +00:00
public virtual string? id {
get { return stanza.get_attribute(ATTRIBUTE_ID); }
set { stanza.set_attribute(ATTRIBUTE_ID, value); }
}
2017-03-02 14:37:32 +00:00
public virtual Jid? 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)
2019-12-22 03:10:53 +00:00
try {
return to_attribute == null ? my_jid : to_ = new Jid(to_attribute);
} catch (InvalidJidError e) {
warning("Ignoring invalid to Jid: %s", e.message);
}
return my_jid;
2017-03-02 14:37:32 +00:00
}
set { stanza.set_attribute(ATTRIBUTE_TO, value.to_string()); }
}
2017-03-02 14:37:32 +00:00
public virtual string? type_ {
get { return stanza.get_attribute(ATTRIBUTE_TYPE); }
set { stanza.set_attribute(ATTRIBUTE_TYPE, value); }
}
2017-03-02 14:37:32 +00:00
public StanzaNode stanza;
2017-03-02 14:37:32 +00:00
public Stanza.incoming(StanzaNode stanza, Jid? my_jid) {
this.stanza = stanza;
this.my_jid = my_jid;
}
2017-03-02 14:37:32 +00:00
public Stanza.outgoing(StanzaNode stanza) {
this.stanza = stanza;
}
2017-03-02 14:37:32 +00:00
public bool is_error() {
return type_ == TYPE_ERROR;
}
2017-03-02 14:37:32 +00:00
public ErrorStanza? get_error() {
return ErrorStanza.from_stanza(this.stanza);
2017-03-02 14:37:32 +00:00
}
}
2017-03-02 14:37:32 +00:00
}