From 2a49d4a1aaf1d1fb644d3e4586989c696f657bcc Mon Sep 17 00:00:00 2001 From: Marvin W Date: Mon, 3 Apr 2017 15:09:30 +0200 Subject: Add --print-xmpp flag to log details --- xmpp-vala/src/core/stanza_node.vala | 65 ++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 12 deletions(-) (limited to 'xmpp-vala/src/core/stanza_node.vala') diff --git a/xmpp-vala/src/core/stanza_node.vala b/xmpp-vala/src/core/stanza_node.vala index aff1770d..d9398a39 100644 --- a/xmpp-vala/src/core/stanza_node.vala +++ b/xmpp-vala/src/core/stanza_node.vala @@ -3,6 +3,11 @@ using Gee; namespace Xmpp.Core { public abstract class StanzaEntry { + protected const string ANSI_COLOR_END = "\x1b[0m"; + protected const string ANSI_COLOR_GREEN = "\x1b[32m"; + protected const string ANSI_COLOR_YELLOW = "\x1b[33m"; + protected const string ANSI_COLOR_GRAY = "\x1b[37m"; + public string? ns_uri; public string name; public string? val; @@ -46,7 +51,8 @@ public class StanzaNode : StanzaEntry { public bool has_nodes = false; public bool pseudo = false; - public StanzaNode () {} + public StanzaNode() { + } public StanzaNode.build(string name, string ns_uri = "jabber:client", ArrayList? nodes = null, ArrayList? attrs = null) { this.ns_uri = ns_uri; @@ -81,7 +87,7 @@ public class StanzaNode : StanzaEntry { _ns_uri = this.ns_uri; } } - foreach(var attr in attributes) { + foreach (var attr in attributes) { if (attr.ns_uri == _ns_uri && attr.name == _name) return attr.val; } return null; @@ -93,6 +99,12 @@ public class StanzaNode : StanzaEntry { return int.parse(res); } + public uint get_attribute_uint(string name, uint def = 0, string? ns_uri = null) { + string? res = get_attribute(name, ns_uri); + if (res == null) return def; + return (uint) long.parse(res); + } + public bool get_attribute_bool(string name, bool def = false, string? ns_uri = null) { string? res = get_attribute(name, ns_uri); if (res == null) return def; @@ -111,7 +123,7 @@ public class StanzaNode : StanzaEntry { _ns_uri = this.ns_uri; } } - foreach(var attr in attributes) { + foreach (var attr in attributes) { if (attr.ns_uri == _ns_uri && attr.name == _name) return attr; } return null; @@ -119,7 +131,7 @@ public class StanzaNode : StanzaEntry { public ArrayList get_attributes_by_ns_uri(string ns_uri) { ArrayList ret = new ArrayList (); - foreach(var attr in attributes) { + foreach (var attr in attributes) { if (attr.ns_uri == ns_uri) ret.add(attr); } return ret; @@ -145,7 +157,7 @@ public class StanzaNode : StanzaEntry { StanzaNode? node = this; string? attribute_name = l.arg(); if (attribute_name == null) return null; - while(true) { + while (true) { string? s = l.arg(); if (s == null) break; node = node.get_subnode(attribute_name); @@ -167,7 +179,7 @@ public class StanzaNode : StanzaEntry { _ns_uri = this.ns_uri; } } - foreach(var node in sub_nodes) { + foreach (var node in sub_nodes) { if (node.ns_uri == _ns_uri && node.name == _name) return node; if (recurse) { var x = node.get_subnode(_name, _ns_uri, recurse); @@ -190,7 +202,7 @@ public class StanzaNode : StanzaEntry { _ns_uri = this.ns_uri; } } - foreach(var node in sub_nodes) { + foreach (var node in sub_nodes) { if (node.ns_uri == _ns_uri && node.name == _name) ret.add(node); if (recurse) { ret.add_all(node.get_subnodes(_name, _ns_uri, recurse)); @@ -206,7 +218,7 @@ public class StanzaNode : StanzaEntry { public StanzaNode? get_deep_subnode_(va_list l) { StanzaNode? node = this; - while(true) { + while (true) { string? s = l.arg(); if (s == null) break; node = node.get_subnode(s); @@ -226,7 +238,7 @@ public class StanzaNode : StanzaEntry { StanzaNode? node = this; string? subnode_name = l.arg(); if (subnode_name == null) return new ArrayList(); - while(true) { + while (true) { string? s = l.arg(); if (s == null) break; node = node.get_subnode(subnode_name); @@ -276,7 +288,7 @@ public class StanzaNode : StanzaEntry { **/ public void set_attribute(string name, string val, string? ns_uri = null) { if (ns_uri == null) ns_uri = this.ns_uri; - foreach(var attr in attributes) { + foreach (var attr in attributes) { if (attr.ns_uri == ns_uri && attr.name == name) { attr.val = val; return; @@ -314,10 +326,38 @@ public class StanzaNode : StanzaEntry { return sb.str; } - public string to_xml (NamespaceState? state = null) throws XmlError { + public string to_ansi_string(bool hide_ns = false, int i = 0) { + string indent = string.nfill (i * 2, ' '); + if (name == "#text") { + return @"$indent$val\n"; + } + var sb = new StringBuilder(); + sb.append(@"$indent$ANSI_COLOR_YELLOW<"); + if (!hide_ns) sb.append(@"$ANSI_COLOR_GRAY{$ns_uri}:$ANSI_COLOR_YELLOW"); + sb.append(@"$name$ANSI_COLOR_END"); + foreach (StanzaAttribute attr in attributes) { + sb.append_printf(" %s", attr.to_ansi_string(hide_ns)); + } + if (!has_nodes && sub_nodes.size == 0) { + sb.append(@" $ANSI_COLOR_YELLOW/>$ANSI_COLOR_END\n"); + } else { + sb.append(@"$ANSI_COLOR_YELLOW>$ANSI_COLOR_END\n"); + if (sub_nodes.size != 0) { + foreach (StanzaNode subnode in sub_nodes) { + sb.append(subnode.to_ansi_string(hide_ns, i + 1)); + } + sb.append(@"$indent$ANSI_COLOR_YELLOW$ANSI_COLOR_END\n"); + } + } + return sb.str; + } + + public string to_xml(NamespaceState? state = null) throws XmlError { NamespaceState my_state = state ?? new NamespaceState.for_stanza(); if (name == "#text") return @"$encoded_val"; - foreach(var xmlns in get_attributes_by_ns_uri (XMLNS_URI)) { + foreach (var xmlns in get_attributes_by_ns_uri (XMLNS_URI)) { if (xmlns.name == "xmlns") { my_state = new NamespaceState.with_current(my_state, xmlns.val); } else { @@ -352,4 +392,5 @@ public class StanzaNode : StanzaEntry { return sb.str; } } + } -- cgit v1.2.3-54-g00ecf