Debian Codegraph @

xmpp-vala/src/module/xep/0428_fallback_indication.vala

raw
1 using Gee;
2
3 namespace Xmpp.Xep.FallbackIndication {
4
5 public const string NS_URI = "urn:xmpp:fallback:0";
6
7 public class Fallback {
8 public string ns_uri { get; set; }
9 public Gee.List<FallbackLocation> locations;
10
11 public Fallback(string ns_uri, Gee.List<FallbackLocation> locations) {
12 this.ns_uri = ns_uri;
13 this.locations = locations;
14 }
15 }
16
17 public class FallbackLocation {
18 public bool is_whole { get { return from_char == -1 && to_char == -1; } }
19 public int from_char { get; set; }
20 public int to_char { get; set; }
21
22 public FallbackLocation.partial_body(int from_char, int to_char) {
23 this.from_char = from_char;
24 this.to_char = to_char;
25 }
26
27 public FallbackLocation.whole_body() {
28 this.from_char = -1;
29 this.to_char = -1;
30 }
31 }
32
33 public static void set_fallback(MessageStanza message, Fallback fallback) {
34 StanzaNode fallback_node = (new StanzaNode.build("fallback", NS_URI))
35 .add_self_xmlns()
36 .put_attribute("for", fallback.ns_uri);
37 foreach (FallbackLocation location in fallback.locations) {
38 var node = new StanzaNode.build("body", NS_URI).add_self_xmlns();
39 if (!location.is_whole) {
40 node.put_attribute("start", location.from_char.to_string())
41 .put_attribute("end", location.to_char.to_string());
42 }
43 fallback_node.put_node(node);
44 }
45 message.stanza.put_node(fallback_node);
46 }
47
48 public Gee.List<Fallback> get_fallbacks(MessageStanza message) {
49 var ret = new ArrayList<Fallback>();
50
51 Gee.List<StanzaNode> fallback_nodes = message.stanza.get_subnodes("fallback", NS_URI);
52 if (fallback_nodes.is_empty) return ret;
53
54 foreach (StanzaNode fallback_node in fallback_nodes) {
55 string? ns_uri = fallback_node.get_attribute("for");
56 if (ns_uri == null) continue;
57
58 var locations = new ArrayList<FallbackLocation>();
59 if (fallback_node.get_all_subnodes().is_empty) {
60 locations.add(new FallbackLocation.whole_body());
61 } else {
62 Gee.List<StanzaNode> body_nodes = fallback_node.get_subnodes("body", NS_URI);
63 foreach (StanzaNode body_node in body_nodes) {
64 int start_char = body_node.get_attribute_int("start", -1);
65 int end_char = body_node.get_attribute_int("end", -1);
66 if (start_char == -1 && end_char == -1) {
67 locations.add(new FallbackLocation.whole_body());
68 continue;
69 }
70 if (start_char == -1 || end_char == -1) continue;
71 locations.add(new FallbackLocation.partial_body(start_char, end_char));
72 }
73 }
74 if (locations.is_empty) continue;
75 ret.add(new Fallback(ns_uri, locations));
76 }
77
78 return ret;
79 }
80 }