simple_iq.vala
author Stiletto <blasux@blasux.ru>
Mon, 05 Nov 2012 23:54:44 +0400
changeset 12 d3e36b368fc5
permissions -rw-r--r--
iq, commands, makefile up
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     1
using Gee, Lm;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     2
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     3
class ModuleIq : Module {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     4
    const string NS_IQ_VERSION = "jabber:iq:version";
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     5
    public static delegate void Callback(string id, MessageNode request, MessageNode response);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     6
    protected HashMap<string, Callback> callbacks;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     7
    public ModuleIq(Config cfg, Connection conn) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     8
        base(cfg, conn);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     9
        var iq_handler = new Lm.MessageHandler((handler, connection, message) => {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    10
            var node = message.node;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    11
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    12
            var query = node.get_child("query");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    13
            if (query == null) return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    14
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    15
            var from = node.get_attribute("from");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    16
            var id = node.get_attribute("id");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    17
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    18
            var response = new Lm.Message(from, Lm.MessageType.IQ);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    19
            response.node.set_attribute("type","result");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    20
            if (id!=null)
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    21
                response.node.set_attribute("id",id);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    22
            var rquery = response.node.add_child("query",null);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    23
            
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    24
            switch (query.get_attribute("xmlns")) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    25
                case NS_IQ_VERSION:
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    26
                    rquery.set_attribute("xmlns", NS_IQ_VERSION);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    27
                    rquery.add_child("name", "iswydt");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    28
                    rquery.add_child("version", IswydtVersion);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    29
                    stderr.printf("RQ: %s\n",rquery.to_string());
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    30
                    stderr.printf("RS: %s\n",response.node.to_string());
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    31
                    this.conn.cn.send(response);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    32
                    return Lm.HandlerResult.REMOVE_MESSAGE;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    33
            }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    34
            return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    35
        }, null);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    36
        conn.cn.register_message_handler(iq_handler, Lm.MessageType.IQ, Lm.HandlerPriority.NORMAL);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    37
    }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    38
    public override string name() {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    39
        return "iq";
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    40
    }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    41
}