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