Beispiel #1
0
 /// <summary>
 /// Load all global users of bot
 /// </summary>
 private static void GlobalLoad()
 {
     string[] dba = File.ReadAllLines(Variables.ConfigurationDirectory +
                    Path.DirectorySeparatorChar + "admins");
     lock (globalUsers)
     {
         globalUsers.Clear();
         foreach (string x in dba)
         {
             if (x.Contains(Configuration.System.Separator))
             {
                 string[] info = x.Split(Char.Parse(Configuration.System.Separator));
                 string level = info[1];
                 string name = Core.decode2(info[0]);
                 SystemUser user = new SystemUser(level, name);
                 if (info.Length > 3)
                 {
                     user.UserName = info[3];
                     user.Password = info[2];
                 }
                 globalUsers.Add(user);
                 Syslog.DebugLog("Registered global user (" + level + "): " + name, 2);
             }
         }
     }
 }
Beispiel #2
0
 /// <summary>
 /// Delete user
 /// </summary>
 /// <param name="origin"></param>
 /// <param name="user">Regex</param>
 /// <returns></returns>
 public void DeleteUser(SystemUser origin, string user)
 {
     foreach (SystemUser u in Users)
     {
         if (u.Name == user)
         {
             if (GetLevelOfRole(u.Role) > GetLevelOfRole(origin.Role))
             {
                 // users with role that has lower level than role of user who is to be removed aren't allowed to do that
                 // eg. trusted can't delete admin from channel
                 IRC.DeliverMessage(messages.Localize("RoleMismatch", this._Channel.Language), this._Channel);
                 return;
             }
             if (u.Name == origin.Name)
             {
                 // users aren't permitted to delete themselve
                 IRC.DeliverMessage(messages.Localize("Trust2", this._Channel.Language), this._Channel);
                 return;
             }
             Users.Remove(u);
             Save();
             IRC.DeliverMessage(messages.Localize("Trust3", this._Channel.Language), this._Channel);
             return;
         }
     }
     IRC.DeliverMessage(messages.Localize("Trust4", this._Channel.Language), this._Channel);
     return;
 }
Beispiel #3
0
 /// <summary>
 /// Return user object from a name
 /// 
 /// Search all global and local records for matching regex, if multiple matches are existing the one with
 /// highest privileges is returned.
 /// </summary>
 /// <param name="user">
 /// Identification string against which the user regexes are tested, this is usually a string
 /// in format of nick!ident@hostname (typical for IRC protocol)
 /// </param>
 /// <returns>
 /// This function always return an instance of SystemUser even if no such a user exists, in that case user
 /// with "null" role is returned, which has no privileges by default
 /// </returns>
 public SystemUser GetUser(string user)
 {
     SystemUser lv = GetGlobalUser(user);
     if (lv == null)
     {
         lv = new SystemUser("null", "");
     }
     int current = GetLevelOfRole(lv.Role);
     lock (Users)
     {
         foreach (SystemUser b in Users)
         {
             Core.RegexCheck id = new Core.RegexCheck(b.Name, user);
             if (id.IsMatch() == 1)
             {
                 int level = GetLevelOfRole(b.Role);
                 if (level > current)
                 {
                     current = level;
                     lv = b;
                 }
             }
         }
     }
     return lv;
 }
Beispiel #4
0
 /// <summary>
 /// Return user object from a name
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 public SystemUser GetUser(string user)
 {
     SystemUser lv = new SystemUser("null", "");
     int current = 0;
     lock (GlobalUsers)
     {
         foreach (SystemUser b in GlobalUsers)
         {
             Core.RegexCheck id = new Core.RegexCheck(b.Name, user);
             if (id.IsMatch() == 1)
             {
                 if (GetLevel(b.Role) > current)
                 {
                     current = GetLevel(b.Role);
                     lv = b;
                 }
             }
         }
     }
     lock (Users)
     {
         foreach (SystemUser b in Users)
         {
             Core.RegexCheck id = new Core.RegexCheck(b.Name, user);
             if (id.IsMatch() == 1)
             {
                 if (GetLevel(b.Role) > current)
                 {
                     current = GetLevel(b.Role);
                     lv = b;
                 }
             }
         }
     }
     return lv;
 }
Beispiel #5
0
 public static bool IsGloballyApproved(SystemUser user, string permission)
 {
     return HasPrivilege(permission, user.Role);
 }
Beispiel #6
0
        /// <summary>
        /// Display admin command
        /// </summary>
        /// <param name="chan">Channel</param>
        /// <param name="user">User name</param>
        /// <param name="host">Host</param>
        /// <param name="message">Message</param>
        public static void ParseAdmin(config.channel chan, string user, string host, string message)
        {
            User invoker = new User(user, host, "");

            if (message == config.CommandPrefix + "reload")
            {
                if (chan.Users.IsApproved(invoker, "admin"))
                {
                    chan.LoadConfig();
                    lock (Module.module)
                    {
                        foreach (Module xx in Module.module)
                        {
                            try
                            {
                                if (xx.working)
                                {
                                    xx.Hook_ReloadConfig(chan);
                                }
                            }
                            catch (Exception fail)
                            {
                                Program.Log("Crash on Hook_Reload in " + xx.Name);
                                handleException(fail);
                            }
                        }
                    }
                    irc._SlowQueue.DeliverMessage(messages.get("Config", chan.Language), chan);
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan);
                }
                return;
            }
            if (message == config.CommandPrefix + "refresh")
            {
                if (chan.Users.IsApproved(invoker, "flushcache"))
                {
                    irc.RestartIRCMessageDelivery();
                    chan.instance.irc.Message(messages.get("MessageQueueWasReloaded", chan.Language), chan.Name);
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan, IRC.priority.low);
                }
                return;
            }

            if (message == config.CommandPrefix + "info")
            {
                irc._SlowQueue.DeliverMessage(config.WebpageURL + config.DumpDir + "/" + System.Web.HttpUtility.UrlEncode(chan.Name) + ".htm", chan);
                return;
            }

            if (message.StartsWith(config.CommandPrefix + "part "))
            {
                string channel = message.Substring(6);
                if (channel != "")
                {
                    config.channel Channel = core.getChannel(channel);
                    if (Channel == null)
                    {
                        irc._SlowQueue.DeliverMessage(messages.get("UnknownChan", chan.Language), chan, IRC.priority.low);
                        return;
                    }
                    core.partChannel(Channel, invoker.Nick, invoker.Host, config.CommandPrefix + "part", chan.Name);
                    return;
                }
                irc._SlowQueue.DeliverMessage(messages.get("Responses-PartFail", chan.Language), chan, IRC.priority.low);
                return;
            }

            if (message.StartsWith(config.CommandPrefix + "drop "))
            {
                string channel = message.Substring(6);
                if (channel != "")
                {
                    config.channel Channel = core.getChannel(channel);
                    if (Channel == null)
                    {
                        irc._SlowQueue.DeliverMessage(messages.get("UnknownChan", chan.Language), chan, IRC.priority.low);
                        return;
                    }
                    core.partChannel(Channel, invoker.Nick, invoker.Host, config.CommandPrefix + "drop", chan.Name);
                    return;
                }
                irc._SlowQueue.DeliverMessage(messages.get("Responses-PartFail", chan.Language), chan, IRC.priority.low);
                return;
            }

            if (message.StartsWith(config.CommandPrefix + "language"))
            {
                if (chan.Users.IsApproved(invoker, "admin"))
                {
                    string parameter = "";
                    if (message.Contains(" "))
                    {
                        parameter = message.Substring(message.IndexOf(" ") + 1).ToLower();
                    }
                    if (parameter != "")
                    {
                        if (messages.exist(parameter))
                        {
                            chan.Language = parameter;
                            irc._SlowQueue.DeliverMessage(messages.get("Language", chan.Language), chan);
                            chan.SaveConfig();
                            return;
                        }
                        if (!chan.suppress_warnings)
                        {
                            irc._SlowQueue.DeliverMessage(messages.get("InvalidCode", chan.Language), chan);
                        }
                        return;
                    }
                    irc._SlowQueue.DeliverMessage(messages.get("LanguageInfo", chan.Language), chan);
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan, IRC.priority.low);
                }
                return;
            }

            if (message.StartsWith(config.CommandPrefix + "help"))
            {
                string parameter = "";
                if (message.Contains(" "))
                {
                    parameter = message.Substring(message.IndexOf(" ") + 1);
                }
                if (parameter != "")
                {
                    ShowHelp(parameter, chan);
                    return;
                }
                irc._SlowQueue.DeliverMessage("I am running http://meta.wikimedia.org/wiki/WM-Bot version " + config.Version + " my source code is licensed under GPL and located at https://github.com/benapetr/wikimedia-bot I will be very happy if you fix my bugs or implement new features", chan);
                return;
            }

            if (message == config.CommandPrefix + "suppress-off")
            {
                if (chan.Users.IsApproved(invoker, "admin"))
                {
                    if (!chan.suppress)
                    {
                        irc._SlowQueue.DeliverMessage(messages.get("Silence1", chan.Language), chan);
                        return;
                    }
                    chan.suppress = false;
                    irc._SlowQueue.DeliverMessage(messages.get("Silence2", chan.Language), chan);
                    chan.SaveConfig();
                    config.Save();
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan, IRC.priority.low);
                }
                return;
            }

            if (message == config.CommandPrefix + "suppress-on")
            {
                if (chan.Users.IsApproved(invoker, "admin"))
                {
                    if (chan.suppress)
                    {
                        //Message("Channel had already quiet mode disabled", chan.name);
                        return;
                    }
                    chan.instance.irc.Message(messages.get("SilenceBegin", chan.Language), chan.Name);
                    chan.suppress = true;
                    chan.SaveConfig();
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan, IRC.priority.low);
                }
                return;
            }

            if (message == config.CommandPrefix + "whoami")
            {
                SystemUser current = chan.Users.getUser(user + "!@" + host);
                if (current.level == "null")
                {
                    irc._SlowQueue.DeliverMessage(messages.get("Unknown", chan.Language), chan);
                    return;
                }
                irc._SlowQueue.DeliverMessage(messages.get("usr1", chan.Language, new List <string> {
                    current.level, current.name
                }), chan);
                return;
            }

            if (message == config.CommandPrefix + "system-relog")
            {
                if (chan.Users.IsApproved(invoker, "root"))
                {
                    core.irc.Authenticate();
                    return;
                }
            }

            if (message.StartsWith(config.CommandPrefix + "instance "))
            {
                if (chan.Users.IsApproved(invoker, "root"))
                {
                    string channel;
                    string instance;
                    message = message.Substring(".instance ".Length);
                    if (!message.Contains(" "))
                    {
                        irc._SlowQueue.DeliverMessage("This command need 2 parameters", chan);
                        return;
                    }
                    channel  = message.Substring(message.IndexOf(" ") + 1);
                    instance = message.Substring(0, message.IndexOf(" "));
                    config.channel ch = core.getChannel(channel);
                    if (ch == null)
                    {
                        irc._SlowQueue.DeliverMessage("This channel I never heard of :'(", chan);
                        return;
                    }

                    Instance _instance = null;

                    lock (core.Instances)
                    {
                        if (!core.Instances.ContainsKey(instance))
                        {
                            irc._SlowQueue.DeliverMessage("This instance I never heard of :'(", chan);
                            return;
                        }
                        _instance = core.Instances[instance];
                    }

                    if (_instance == ch.instance)
                    {
                        irc._SlowQueue.DeliverMessage("This channel is already in this instance", chan);
                        return;
                    }

                    ch.instance.irc.SendData("PART " + ch.Name + " :Switching instance");
                    ch.instance = _instance;
                    ch.instance.irc.SendData("JOIN " + ch.Name);
                    ch.DefaultInstance = ch.instance.Nick;
                    ch.SaveConfig();

                    chan.instance.irc._SlowQueue.DeliverMessage("Changed default instance of " + channel + " to " + instance, chan);
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan, IRC.priority.low);
                }
            }

            if (message == config.CommandPrefix + "traffic-off")
            {
                if (chan.Users.IsApproved(invoker, "root"))
                {
                    config.Logging = false;
                    irc._SlowQueue.DeliverMessage("Logging stopped", chan);
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan, IRC.priority.low);
                }
            }

            if (message == config.CommandPrefix + "traffic-on")
            {
                if (chan.Users.IsApproved(invoker, "root"))
                {
                    config.Logging = true;
                    irc._SlowQueue.DeliverMessage("Logging traf", chan.Name);
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan, IRC.priority.low);
                }
            }

            if (message == config.CommandPrefix + "restart")
            {
                if (chan.Users.IsApproved(invoker, "root"))
                {
                    irc.Message("System is shutting down, requested by " + invoker.Nick + " from " + chan.Name, config.DebugChan);
                    Program.Log("System is shutting down, requested by " + invoker.Nick + " from " + chan.Name);
                    Kill();
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan.Name, IRC.priority.low);
                }
            }

            if (message == config.CommandPrefix + "channellist")
            {
                irc._SlowQueue.DeliverMessage(messages.get("Responses-List", chan.Language, new List <string> {
                    config.channels.Count.ToString()
                }), chan);
                return;
            }

            if (message.StartsWith(config.CommandPrefix + "configure "))
            {
                if (chan.Users.IsApproved(invoker, "admin"))
                {
                    string text = message.Substring("@configure ".Length);
                    if (string.IsNullOrEmpty(text))
                    {
                        return;
                    }
                    if (text.Contains("=") && !text.EndsWith("="))
                    {
                        string name  = text.Substring(0, text.IndexOf("="));
                        string value = text.Substring(text.IndexOf("=") + 1);
                        bool   _temp_a;
                        switch (name)
                        {
                        case "ignore-unknown":
                            if (bool.TryParse(value, out _temp_a))
                            {
                                chan.IgnoreUnknown = _temp_a;
                                irc._SlowQueue.DeliverMessage(messages.get("configuresave", chan.Language, new List <string> {
                                    value, name
                                }), chan);
                                chan.SaveConfig();
                                return;
                            }
                            irc._SlowQueue.DeliverMessage(messages.get("configure-va", chan.Language, new List <string> {
                                name, value
                            }), chan);
                            return;

                        case "respond-wait":
                            int _temp_b;
                            if (int.TryParse(value, out _temp_b))
                            {
                                if (_temp_b > 1 && _temp_b < 364000)
                                {
                                    chan.respond_wait = _temp_b;
                                    irc._SlowQueue.DeliverMessage(messages.get("configuresave", chan.Language, new List <string> {
                                        value, name
                                    }), chan);
                                    chan.SaveConfig();
                                    return;
                                }
                            }
                            irc._SlowQueue.DeliverMessage(messages.get("configure-va", chan.Language, new List <string> {
                                name, value
                            }), chan);
                            return;

                        case "respond-message":
                            if (bool.TryParse(value, out _temp_a))
                            {
                                chan.respond_message = _temp_a;
                                irc._SlowQueue.DeliverMessage(messages.get("configuresave", chan.Language, new List <string> {
                                    value, name
                                }), chan);
                                chan.SaveConfig();
                                return;
                            }
                            irc._SlowQueue.DeliverMessage(messages.get("configure-va", chan.Language, new List <string> {
                                name, value
                            }), chan);
                            return;

                        case "suppress-warnings":
                            if (bool.TryParse(value, out _temp_a))
                            {
                                chan.suppress_warnings = _temp_a;
                                irc._SlowQueue.DeliverMessage(messages.get("configuresave", chan.Language, new List <string> {
                                    value, name
                                }), chan);
                                chan.SaveConfig();
                                return;
                            }
                            irc._SlowQueue.DeliverMessage(messages.get("configure-va", chan.Language, new List <string> {
                                name, value
                            }), chan);
                            return;
                        }
                        bool exist = false;
                        lock (Module.module)
                        {
                            foreach (Module curr in Module.module)
                            {
                                try
                                {
                                    if (curr.working)
                                    {
                                        if (curr.Hook_SetConfig(chan, invoker, name, value))
                                        {
                                            exist = true;
                                        }
                                    }
                                }
                                catch (Exception fail)
                                {
                                    Program.Log("Error on Hook_SetConfig module " + curr.Name);
                                    core.handleException(fail);
                                }
                            }
                        }
                        if (!chan.suppress_warnings && !exist)
                        {
                            irc._SlowQueue.DeliverMessage(messages.get("configure-wrong", chan.Language), chan);
                        }
                        return;
                    }
                    if (!text.Contains(" "))
                    {
                        switch (text)
                        {
                        case "ignore-unknown":
                            irc._SlowQueue.DeliverMessage(messages.get("Responses-Conf", chan.Language, new List <string> {
                                text, chan.IgnoreUnknown.ToString()
                            }), chan);
                            return;

                        case "respond-message":
                            irc._SlowQueue.DeliverMessage(messages.get("Responses-Conf", chan.Language, new List <string> {
                                text, chan.respond_message.ToString()
                            }), chan);
                            return;

                        case "suppress-warnings":
                            irc._SlowQueue.DeliverMessage(messages.get("Responses-Conf", chan.Language, new List <string> {
                                text, chan.suppress_warnings.ToString()
                            }), chan);
                            return;
                        }
                        bool exist = false;
                        lock (Module.module)
                        {
                            foreach (Module curr in Module.module)
                            {
                                try
                                {
                                    if (curr.working)
                                    {
                                        if (curr.Hook_GetConfig(chan, invoker, text))
                                        {
                                            exist = true;
                                        }
                                    }
                                }
                                catch (Exception fail)
                                {
                                    Program.Log("Error on Hook_GetConfig module " + curr.Name);
                                    core.handleException(fail);
                                }
                            }
                        }
                        if (exist)
                        {
                            return;
                        }
                    }
                    if (!chan.suppress_warnings)
                    {
                        irc._SlowQueue.DeliverMessage(messages.get("configure-wrong", chan.Language), chan);
                    }
                    return;
                }
                if (!chan.suppress_warnings)
                {
                    irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", chan.Language), chan, IRC.priority.low);
                }
                return;
            }

            if (message.StartsWith(config.CommandPrefix + "system-lm "))
            {
                if (chan.Users.IsApproved(invoker, "root"))
                {
                    string module = message.Substring("@system-lm ".Length);
                    if (module.EndsWith(".bin"))
                    {
                        Module _m = getModule(module);
                        if (_m != null)
                        {
                            irc._SlowQueue.DeliverMessage("This module was already loaded and you can't load one module twice, module will be reloaded now", chan, IRC.priority.high);
                            _m.Exit();
                        }
                        if (module.EndsWith(".bin"))
                        {
                            module = "modules" + Path.DirectorySeparatorChar + module;
                            if (File.Exists(module))
                            {
                                if (LoadMod(module))
                                {
                                    irc._SlowQueue.DeliverMessage("Loaded module " + module, chan, IRC.priority.high);
                                    return;
                                }
                                irc._SlowQueue.DeliverMessage("Unable to load module " + module, chan, IRC.priority.high);
                                return;
                            }
                            irc._SlowQueue.DeliverMessage("File not found " + module, chan, IRC.priority.high);
                            return;
                        }

                        irc._SlowQueue.DeliverMessage("Loaded module " + module, chan, IRC.priority.high);
                        return;
                    }
                    irc._SlowQueue.DeliverMessage("This module is not currently loaded in core", chan, IRC.priority.high);
                    return;
                }
            }

            if (message == config.CommandPrefix + "verbosity--")
            {
                if (chan.Users.IsApproved(invoker, "root"))
                {
                    if (config.SelectedVerbosity > 0)
                    {
                        config.SelectedVerbosity--;
                    }
                    irc._SlowQueue.DeliverMessage("Verbosity: " + config.SelectedVerbosity.ToString(), chan, IRC.priority.high);
                }
            }

            if (message == config.CommandPrefix + "verbosity++")
            {
                if (chan.Users.IsApproved(invoker, "root"))
                {
                    config.SelectedVerbosity++;
                    irc._SlowQueue.DeliverMessage("Verbosity: " + config.SelectedVerbosity.ToString(), chan, IRC.priority.high);
                }
            }

            if (message.StartsWith(config.CommandPrefix + "system-rm "))
            {
                if (chan.Users.IsApproved(invoker, "root"))
                {
                    string module = message.Substring("@system-lm ".Length);
                    Module _m     = getModule(module);
                    if (_m == null)
                    {
                        irc._SlowQueue.DeliverMessage("This module is not currently loaded in core", chan, IRC.priority.high);
                        return;
                    }
                    _m.Exit();
                    irc._SlowQueue.DeliverMessage("Unloaded module " + module, chan, IRC.priority.high);
                }
            }

            if (message.StartsWith(config.CommandPrefix + "join "))
            {
                if (chan.Users.IsApproved(invoker, "reconnect"))
                {
                    config.channel channel = core.getChannel(message.Substring("@join ".Length));
                    irc.Join(channel);
                }
            }

            if (message == config.CommandPrefix + "commands")
            {
                irc._SlowQueue.DeliverMessage("Commands: there is too many commands to display on one line, see http://meta.wikimedia.org/wiki/wm-bot for a list of commands and help", chan);
            }
        }
Beispiel #7
0
 /// <summary>
 /// Delete user
 /// </summary>
 /// <param name="origin"></param>
 /// <param name="user">Regex</param>
 /// <returns></returns>
 public bool DeleteUser(SystemUser origin, string user)
 {
     foreach (SystemUser u in Users)
     {
         if (u.Name == user)
         {
             if (GetLevel(u.Role) > GetLevel(origin.Role))
             {
                 Core.irc.Queue.DeliverMessage(messages.Localize("Trust1", this._Channel.Language), this._Channel);
                 return true;
             }
             if (u.Name == origin.Name)
             {
                 Core.irc.Queue.DeliverMessage(messages.Localize("Trust2", this._Channel.Language), this._Channel);
                 return true;
             }
             Users.Remove(u);
             Save();
             Core.irc.Queue.DeliverMessage(messages.Localize("Trust3", this._Channel.Language), this._Channel);
             return true;
         }
     }
     Core.irc.Queue.DeliverMessage(messages.Localize("Trust4", this._Channel.Language), this._Channel);
     return true;
 }
Beispiel #8
0
            public void ThreadExec(object data)
            {
                try
                {
                    this.connection = (TcpClient)data;
                    Syslog.DebugLog("Incoming connection from: " + connection.Client.RemoteEndPoint);
                    this.connection.NoDelay = true;
                    this.networkStream      = connection.GetStream();
                    this.streamReader       = new StreamReader(networkStream);
                    this.streamWriter       = new StreamWriter(networkStream);
                    // login
                    Write("Enter username:"******"Enter password:"******"Invalid user or password, bye");
                        return;
                    }
                    if (!Security.IsGloballyApproved(user, "terminal"))
                    {
                        Disconnect("No permissions, bye");
                        return;
                    }
                    Write("Successfuly logged in to wm-bot, I have " + SessionList.Count.ToString() + " users logged in");
                    while (connection.Connected && !streamReader.EndOfStream && Core.IsRunning)
                    {
                        string        text       = streamReader.ReadLine();
                        string        command    = text;
                        List <string> list       = new List <string>();
                        string        parameters = "";
                        if (command.Contains(" "))
                        {
                            parameters = command.Substring(command.IndexOf(" ") + 1);
                            command    = command.Substring(0, command.IndexOf(" "));
                            if (parameters.Contains(" "))
                            {
                                list.AddRange(parameters.Split(' '));
                            }
                        }

                        switch (command.ToLower())
                        {
                        case "exit":
                        case "quit":
                            Disconnect("Good bye");
                            return;

                        case "info":
                            string result = "Uptime: " + Core.getUptime() + " Version: " + Configuration.System.Version
                                            + "\n\nBuffer information:\nUnwritten lines (file storage): " + StorageWriter.Count + "\n";
                            // we flush it early so that we can see where it gets stuck in case of a dead lock
                            Write(result);
                            result = "";
                            if (Core.DB != null)
                            {
                                result += "Unwritten rows (MySQL): " + Core.DB.CacheSize() + "\n";
                            }
                            result += "\nThreads:\n";
                            foreach (Thread thread in Core.ThreadManager.ThreadList)
                            {
                                result += "Thread: " + FormatToSpecSize(thread.Name, 20) + " status: " +
                                          FormatToSpecSize(thread.ThreadState.ToString(), 20) +
                                          " id: " + FormatToSpecSize(thread.ManagedThreadId.ToString(), 8) + "\n";
                            }
                            Write(result);
                            Write("Instances:");
                            result = "";
                            Syslog.DebugLog("Retrieving information for user " + username + " in system");
                            foreach (Instance instance in Instance.Instances.Values)
                            {
                                Syslog.DebugLog("Retrieving information for user " + username + " of instance " + instance.Nick, 2);
                                result += instance.Nick + " channels: " + instance.ChannelCount +
                                          " connected: " + instance.IsConnected + " working: " +
                                          instance.IsWorking + " queue: " + instance.QueueSize() + "\n";
                            }
                            Write(result);
                            break;

                        case "help":
                            Write("Commands:\n"
                                  + "exit - shutdown connection\n"
                                  + "verbosity++\nverbosity--\n"
                                  + "info - print information about system\n"
                                  + "halt - shutdown bot\n"
                                  + "reauth [instance] - resend password to nickserv\n"
                                  + "traffic-on - turn on traffic logs\n"
                                  + "traffic-off - turn off traffic logs\n"
                                  + "send [instance] [text] - deliver a RAW text to IRC server on a given bot\n"
                                  + "kill [instance] - disconnect selected instance\n"
                                  + "conn [instance] - connect instance\n");
                            break;

                        case "halt":
                            Disconnect("Shutting down");
                            Core.Kill();
                            return;

                        case "reauth":
                            if (Instance.Instances.ContainsKey(parameters))
                            {
                                Instance.Instances[parameters].Protocol.Authenticate(false);
                                Write("Sent a password to nickserv on " + parameters);
                                break;
                            }
                            Write("Unknown instance: " + parameters);
                            break;

                        case "traffic-on":
                            Configuration.Network.Logging = true;
                            Write("Dumping traffic into datafile");
                            break;

                        case "verbosity++":
                            Configuration.System.SelectedVerbosity++;
                            break;

                        case "verbosity--":
                            Configuration.System.SelectedVerbosity--;
                            break;

                        case "traffic-off":
                            Configuration.Network.Logging = false;
                            Write("Disabled traffic");
                            break;

                        case "kill":
                            if (Instance.Instances.ContainsKey(parameters))
                            {
                                Instance.Instances[parameters].ShutDown();
                                Write("Killed: " + parameters);
                                break;
                            }
                            Write("Unknown instance: " + parameters);
                            break;

                        case "conn":
                            if (Instance.Instances.ContainsKey(parameters))
                            {
                                if (Instance.Instances[parameters].IsConnected)
                                {
                                    Write("Refusing to connect instance which is already connected: " + parameters);
                                    break;
                                }
                                Instance.Instances[parameters].Init();
                                Write("Initializing: " + parameters);
                                int curr = 0;
                                while (curr < 10 && !Instance.Instances[parameters].IsWorking)
                                {
                                    curr++;
                                    Thread.Sleep(1000);
                                }
                                if (!Instance.Instances[parameters].IsWorking)
                                {
                                    Write("Failed to initialize instance (timeout)");
                                    break;
                                }
                                Write("Joining channels");
                                Instance.Instances[parameters].ChannelsJoined = false;
                                Instance.Instances[parameters].Join();
                                curr = 0;
                                while (curr < Instance.Instances[parameters].ChannelCount && !Instance.Instances[parameters].ChannelsJoined)
                                {
                                    curr++;
                                    Thread.Sleep(6000);
                                }
                                if (!Instance.Instances[parameters].ChannelsJoined)
                                {
                                    Write("Failed to rejoin all channels in time");
                                    break;
                                }
                                Write("Instance is online: " + parameters);
                                break;
                            }
                            Write("Unknown instance: " + parameters);
                            break;

                        case "send":
                            if (!parameters.Contains(" "))
                            {
                                Write("This command requires 2 parameters");
                                break;
                            }
                            string to = parameters.Substring(0, parameters.IndexOf(" "));
                            if (Instance.Instances.ContainsKey(to))
                            {
                                if (!Instance.Instances[to].IsConnected)
                                {
                                    Write("Refusing to send data using instance which is not connected: " + to);
                                    break;
                                }
                                Instance.Instances[to].Network.Transfer(parameters.Substring(parameters.IndexOf(" ") + 1));
                                break;
                            }
                            Write("I have no such instance dude");
                            break;

                        default:
                            Write("Unknown command, try help");
                            break;
                        }
                    }
                }
                catch (Exception fail)
                {
                    Core.HandleException(fail);
                }
                Disconnect();
            }
Beispiel #9
0
        /// <summary>
        /// Return a level of role that this user posses
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int GetLevel(libirc.UserInfo user)
        {
            SystemUser current = GetUser(user.Nick + "!@" + user.Host);

            return(GetLevelOfRole(current.Role));
        }
Beispiel #10
0
        /// <summary>
        /// Check if user is approved to do operation requested
        /// </summary>
        /// <param name="User">Username</param>
        /// <param name="Host">Hostname</param>
        /// <param name="privilege">Approved for specified object / request</param>
        /// <returns></returns>
        public bool IsApproved(string User, string Host, string privilege)
        {
            SystemUser current = GetUser(User + "!@" + Host);

            return(HasPrivilege(privilege, current.Role));
        }
Beispiel #11
0
        public bool IsKnown(string User, string Host)
        {
            SystemUser current = GetUser(User + "!@" + Host);

            return(current.Role != "null");
        }
Beispiel #12
0
 public static bool IsGloballyApproved(SystemUser user, string permission)
 {
     return(HasPrivilege(permission, user.Role));
 }