﻿Services.factory("Commands", ["Connection", function (Connection)
{
    var handlers = [];

    function Command(line)
    {
        var parts = line.split(" ");

        command = new Object();
        command.name = parts[0].toUpperCase();
        command.args = parts.slice(1);

        return command;
    }

    function cmdHandler(check, process)
    {
        handler = new Object();
        handler.check = check;
        handler.process = process;

        return handler;
    }

    function registerHandler(cmdHandler)
    {
        handlers.push(cmdHandler);
    }

    // Channel management
    var kickHandler = new cmdHandler(function (cmd)
    {
        return cmd.name === "KICK" && cmd.args.length >= 1;
    }, function (cmd, target)
    {
        if (target.name)
        {
            var channel = target;
            var nickname = cmd.args[0];
            var reason = cmd.args.slice(1).join(" ");
            if (reason)
            {
                Connection.send("KICK " + channel.name + " " + nickname + " :" + reason);
            } else
            {
                Connection.send("KICK " + channel.name + " " + nickname);
            }
        }
    });
    registerHandler(kickHandler);

    var banHandler = new cmdHandler(function (cmd)
    {
        return cmd.name === "BAN" && cmd.args.length >= 1;
    }, function (cmd, target)
    {
        if (target.name)
        {
            var channel = target;
            var mask = cmd.args[0];

            Connection.send("MODE " + channel.name + " +b " + mask);
        }
    });
    registerHandler(banHandler);

    // Ranks

    // UI
    var clearHandler = new cmdHandler(function (cmd)
    {
        return cmd.name === "CLEAR";
    }, function (cmd, target)
    {
        target.clear();
    });
    registerHandler(clearHandler);

    return {
        parse: function (line, target)
        {
            var command = new Command(line.substring(1));

            for (var i = 0; i < handlers.length; i++)
            {
                var handler = handlers[i];

                if (handler.check(command))
                {
                    handler.process(command, target);
                }
            }
        }
    };
}]);
@@ -37,16 +37,24 @@
 ection%22,
+ %22User%22,
  functio
@@ -66,16 +66,22 @@
 nnection
+, User
 )%0A%7B%0A    
@@ -1830,24 +1830,505 @@
 rHandler);%0A%0A
+    // MISC%0A    var actionHandler = new cmdHandler(function (cmd)%0A    %7B%0A        return cmd.name === %22ME%22;%0A    %7D, function (cmd, target)%0A    %7B%0A        if (target.name)%0A        %7B%0A            var channel = target;%0A            var line = cmd.args.join(%22 %22);%0A%0A            Connection.send(%22PRIVMSG %22 + channel.name + %22 %5Cu0001ACTION %22 + line + %22%5Cu0001%22);%0A            target.addLine(%22%5Cu0001ACTION %22 + line + %22%5Cu0001%22, User.get(%22~%22));%0A        %7D%0A    %7D);%0A    registerHandler(actionHandler);%0A%0A
     return %7B

Add actionHandler