simple_iq.vala
changeset 12 d3e36b368fc5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simple_iq.vala	Mon Nov 05 23:54:44 2012 +0400
@@ -0,0 +1,41 @@
+using Gee, Lm;
+
+class ModuleIq : Module {
+    const string NS_IQ_VERSION = "jabber:iq:version";
+    public static delegate void Callback(string id, MessageNode request, MessageNode response);
+    protected HashMap<string, Callback> callbacks;
+    public ModuleIq(Config cfg, Connection conn) {
+        base(cfg, conn);
+        var iq_handler = new Lm.MessageHandler((handler, connection, message) => {
+            var node = message.node;
+
+            var query = node.get_child("query");
+            if (query == null) return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
+
+            var from = node.get_attribute("from");
+            var id = node.get_attribute("id");
+
+            var response = new Lm.Message(from, Lm.MessageType.IQ);
+            response.node.set_attribute("type","result");
+            if (id!=null)
+                response.node.set_attribute("id",id);
+            var rquery = response.node.add_child("query",null);
+            
+            switch (query.get_attribute("xmlns")) {
+                case NS_IQ_VERSION:
+                    rquery.set_attribute("xmlns", NS_IQ_VERSION);
+                    rquery.add_child("name", "iswydt");
+                    rquery.add_child("version", IswydtVersion);
+                    stderr.printf("RQ: %s\n",rquery.to_string());
+                    stderr.printf("RS: %s\n",response.node.to_string());
+                    this.conn.cn.send(response);
+                    return Lm.HandlerResult.REMOVE_MESSAGE;
+            }
+            return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
+        }, null);
+        conn.cn.register_message_handler(iq_handler, Lm.MessageType.IQ, Lm.HandlerPriority.NORMAL);
+    }
+    public override string name() {
+        return "iq";
+    }
+}