muc_commands.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;
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 ModuleMucCommands : Module {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     4
    protected weak ModuleMuc muc;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     5
    public delegate void CommandHandler(ModuleMuc.Conference conf, ModuleMuc.Occupant user, string command, string? arguments);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     6
    public class Command : Object {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     7
        public weak Module mod;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     8
        public CommandHandler hnd;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     9
    }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    10
    protected HashMap<string,Command> commands;
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
    public string getconf(string jid, string key, string? def) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    13
        var res = cfg["muc "+jid, key];
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    14
        if (res==null) res = cfg["muc", key];
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    15
        if (res==null) res = def;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    16
        return res;
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
    public bool register(string command, Module mod, CommandHandler handler) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    19
        var cmd = new Command();
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    20
        cmd.mod = mod; /* this will cause "copying delegates is discouraged" warning. that's okay, this delegate's data needs no destroy notify */
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    21
        cmd.hnd = handler;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    22
        if (commands.has_key(command))
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    23
            log("muc_commands", LogLevelFlags.LEVEL_WARNING, "Command '%s' defined by '%s' was redefined by '%s'.",
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    24
                command, commands[command].mod.name(), mod.name());
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    25
        commands[command] = cmd;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    26
        return true;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    27
    }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    28
    public bool unregister(string command) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    29
        return commands.unset(command);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    30
    }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    31
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    32
    protected void hnd_modules(ModuleMuc.Conference conf, ModuleMuc.Occupant user, string command, string? arguments) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    33
        var sb = new StringBuilder();
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    34
        sb.append("Loaded modules:");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    35
        muc.name();
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    36
        foreach (var mod in conn.modules)
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    37
            sb.append(" "+mod.name());
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    38
        user.public_message(sb.str);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    39
    }
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
    protected void hnd_status(ModuleMuc.Conference conf, ModuleMuc.Occupant user, string command, string? arguments) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    42
        var sb = new StringBuilder();
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    43
        sb.append("Status:");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    44
        foreach (var room in muc.rooms.values) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    45
            sb.append(" ");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    46
            sb.append(room.jid.split("@",2)[0]);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    47
            sb.append("[");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    48
            sb.append(room.enabled ? "EN":"DIS");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    49
            sb.append(":");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    50
            switch (room.state) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    51
                case ModuleMuc.State.CONNECTED: sb.append("C"); break;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    52
                case ModuleMuc.State.DISCOVERING: sb.append("DS"); break;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    53
                case ModuleMuc.State.CONNECTING: sb.append(">C"); break;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    54
                case ModuleMuc.State.DISCONNECTING: sb.append(">D"); break;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    55
                case ModuleMuc.State.DISCONNECTED: sb.append("D"); break;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    56
            }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    57
            sb.append("]");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    58
        }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    59
        user.public_message(sb.str);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    60
    }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    61
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    62
    public ModuleMucCommands(Config cfg, Connection conn) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    63
        base(cfg, conn);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    64
        commands = new HashMap<string,Command>();
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    65
        foreach (var module in conn.modules) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    66
            if (module.name()=="muc")
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    67
                muc = (ModuleMuc)module;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    68
        }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    69
        if (muc==null)
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    70
            log("muc_log", LogLevelFlags.LEVEL_ERROR, "Module 'muc' is not loaded");
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    71
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    72
        muc.on_message.connect( (conf, user, message, body) => {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    73
            if ((user!=null)&&(body!=null)&&(message.get_child("delay")==null)) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    74
                if (getconf(conf.jid, "commands", "no")!="yes")
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    75
                    return;
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    76
                if (body.has_prefix(conf.nick)) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    77
                    var txt = body[conf.nick.length:body.length];
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    78
                    if (txt.has_prefix(": ")||txt.has_prefix(", ")) {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    79
                        var command_n_args = txt[2:txt.length].split(" ",2);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    80
                        var command = commands[command_n_args[0]];
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    81
                        if (command!=null)
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    82
                            command.hnd(conf, user, command_n_args[0], (command_n_args.length > 1) ? command_n_args[1] : null);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    83
                    }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    84
                }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    85
            }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    86
        });
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    87
        
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    88
        register("modules", this, hnd_modules);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    89
        register("status", this, hnd_status);
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    90
    }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    91
    public override string name() {
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    92
        return "muc_commands";
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    93
    }
d3e36b368fc5 iq, commands, makefile up
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    94
}