Exemplo n.º 1
0
        public bool ReadConfiguration()
        {
            // Read configuration
            ConfigurationBase configuration = ConfigurationReader.Read(this._configurationFile);

            if (configuration == null)
            {
                Core.Output("Error", "Unable to load configuration file: " + this._configurationFile);
                return(false);
            }
            this._configuration = configuration.Core;
            Core.Output("Core", "Loaded configuration file: " + this._configurationFile);

            // Prepare bot managers
            foreach (ConfigurationBot configurationBot in configuration.Bots)
            {
                lock (this._bots)
                {
                    BotManager bot;
                    if (!this._bots.ContainsKey(configurationBot.GetID()))
                    {
                        bot               = new BotManager();
                        bot.Connected     = false;
                        bot.Enabled       = configurationBot.Enabled;
                        bot.Configuration = configurationBot;
                        this._bots.Add(bot.ID, bot);
                    }
                    else
                    {
                        bot               = this._bots[configurationBot.GetID()];
                        bot.Enabled       = configurationBot.Enabled;
                        bot.Configuration = configurationBot;
                    }
                    Core.Output("Core", "Registered " + configurationBot.ToString() + " as " + bot.ID);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // Error handling
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);

            // Parse arguments
            Dictionary <string, string> arguments = new Dictionary <string, string>();

            foreach (string arg in args)
            {
                if (!arg.Contains("=") || arg.IndexOf("=") + 1 == arg.Length)
                {
                    Console.WriteLine("Invalid argument: " + arg);
                    continue;
                }
                string argKey   = arg.Substring(0, arg.IndexOf("="));
                string argValue = arg.Substring(arg.IndexOf("=") + 1);
                arguments.Add(argKey, argValue);
            }

            // Verify arguments
            string channelName = "";

            if (arguments.ContainsKey("channel"))
            {
                channelName = arguments["channel"];
            }
            if (string.IsNullOrEmpty(channelName))
            {
                Console.WriteLine("No valid remoting channel specified");
                Environment.Exit(ExitCodes.INVALID_ARGUMENT);
                return;
            }
            string id = null;

            if (arguments.ContainsKey("id"))
            {
                id = arguments["id"];
            }
            if (id == null || id == string.Empty)
            {
                Console.WriteLine("No valid id specified");
                Environment.Exit(ExitCodes.INVALID_ARGUMENT);
                return;
            }
            string key = null;

            if (arguments.ContainsKey("key"))
            {
                key = arguments["key"];
            }
            if (key == null || key == string.Empty)
            {
                Console.WriteLine("No valid key specified");
                Environment.Exit(ExitCodes.INVALID_ARGUMENT);
                return;
            }
            int pid = -1;

            if (arguments.ContainsKey("pid"))
            {
                try { pid = Convert.ToInt32(arguments["pid"]); }
                catch { }
            }
            if (pid < 1)
            {
                Console.WriteLine("No valid core process id specified");
                Environment.Exit(ExitCodes.INVALID_ARGUMENT);
                return;
            }

            // Be sure we know when the core goes down
            Process coreProcess = Process.GetProcessById(pid);

            if (coreProcess == null)
            {
                Console.WriteLine("Unable to access VhaBot.Core process");
                Environment.Exit(ExitCodes.COMMUNICATION_LOST);
                return;
            }
            coreProcess.Exited += new EventHandler(OnCoreShutdown);

            // Fix working directory
            string path = Assembly.GetExecutingAssembly().Location;

            if (path.LastIndexOf(Path.DirectorySeparatorChar) > 0)
            {
                path = path.Substring(0, path.LastIndexOf(Path.DirectorySeparatorChar));
                Environment.CurrentDirectory = path;
            }

            // Connect to core
            Console.WriteLine("Connecting to VhaBot.Core on channel " + channelName);
            ClientCommunication communication;

            try
            {
                ServerCommunication server = (ServerCommunication)Activator.GetObject(typeof(ServerCommunication), "ipc://" + channelName + "/ServerCommunication");
                communication = server.AuthorizeClient(id, key);
                if (communication == null)
                {
                    Console.WriteLine("Unable to authorize client on VhaBot.Core");
                    Environment.Exit(ExitCodes.REMOTING_FAILED);
                    return;
                }
            }
            catch
            {
                Console.WriteLine("An error occurred while connecting to VhaBot.Core");
                Environment.Exit(ExitCodes.REMOTING_FAILED);
                return;
            }
            Console.WriteLine("Connected to VhaBot.Core (" + communication.CoreID + ")");
            ConfigurationBot  configurationBot  = communication.GetConfigurationBot();
            ConfigurationCore configurationCore = communication.GetConfigurationCore();

            if (configurationBot == null || configurationCore == null)
            {
                Console.WriteLine("An error occurred while fetching configuration from VhaBot.Core");
                Environment.Exit(ExitCodes.NO_CONFIGURATION);
                return;
            }
            Console.WriteLine("Received bot configuration for " + configurationBot.ToString());

            // Verify core PID
            if (pid != communication.CoreID)
            {
                Console.WriteLine("Core process id appeared to be invalid. " + pid + " was specified but the actual id is " + communication.CoreID);
                Environment.Exit(ExitCodes.INVALID_ARGUMENT);
                return;
            }

            // Initiate the bot
            Console.WriteLine("Starting BotShell");
            BotShell bot = new BotShell(configurationBot, configurationCore, new SendMessageHandler(communication.SendMessage));

            // Main loop, check for messages and relay
            int collectTimeout = 1;

            while (true)
            {
                coreProcess.Refresh();
                if (coreProcess.HasExited)
                {
                    Console.WriteLine("Core shutdown");
                    Environment.Exit(ExitCodes.SHUTDOWN);
                }
                try { communication.Ping(); }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to ping core: " + ex.Message);
                    Environment.Exit(ExitCodes.COMMUNICATION_LOST);
                    return;
                }
                while (communication.QueueSize > 0)
                {
                    MessageBase message = communication.Dequeue();
                    bot.RelayMessage(message);
                }
                // Cleanup duties every 60 seconds
                collectTimeout--;
                if (collectTimeout < 1)
                {
                    bot.Clean();
                    collectTimeout = 600;
                }
                System.Threading.Thread.Sleep(100);
            }
        }
Exemplo n.º 3
0
        private static void LoadConfigFiles(String server)
        {
            lock (m_syncLock)
            {
                var path = "";
                if (!UseCoreConfiguration)
                {
                    path = ConfigurationManager.AppSettings["Mtn.Library.ConfigFolder"];
                }
                else
                {
                    path = ConfigurationCore.GetSection("Mtn.Library")?["ConfigFolder"];
                }

                if (string.IsNullOrWhiteSpace(path))
                {
                    path = IsHosted
                        ? AppDomainAppPath
                        : Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                    if (path == null)
                    {
                        return;
                    }

                    path = Path.Combine(path, "config");
                    if (!Directory.Exists(path))
                    {
                        return;
                    }
                }
                if (server == null)
                {
                    return;
                }


                var file = string.Empty;
                if (!UseCoreConfiguration)
                {
                    file = ConfigurationManager.AppSettings["Mtn.Library.Config.Default"];
                }
                else
                {
                    file = ConfigurationCore.GetSection("Mtn.Library")?["Config.Default"];
                }

                if (file.IsNullOrWhiteSpaceMtn())
                {
                    if (!UseCoreConfiguration)
                    {
                        file = ConfigurationManager.AppSettings["Mtn.Library.Config." + server];
                    }
                    else
                    {
                        file = ConfigurationCore.GetSection("Mtn.Library")?["Config." + server];
                    }
                }

                if (file.IsNullOrWhiteSpaceMtn())
                {
                    var depthWildcard = "*.";
                    var depthServer   = server.SplitMtn(".").Count() > 3?
                                        String.Join(".", server.SplitMtn(".").Skip(1)):server;
                    if (!UseCoreConfiguration)
                    {
                        file = ConfigurationManager.AppSettings["Mtn.Library.Config." + depthWildcard + depthServer];
                    }
                    else
                    {
                        file = ConfigurationCore.GetSection("Mtn.Library")?["Config." + depthWildcard + depthServer];
                    }

                    if (file.IsNullOrWhiteSpaceMtn())
                    {
                        for (var i = 1; i < Core.Parameter.ConfigurationDepth; i++)
                        {
                            depthWildcard += "*.";
                            depthServer    = depthServer.SplitMtn(".").Count() > 2 ? String.Join(".", depthServer.SplitMtn(".").Skip(1)) : depthServer;
                            if (!UseCoreConfiguration)
                            {
                                file = ConfigurationManager.AppSettings["Mtn.Library.Config." + depthWildcard + depthServer];
                            }
                            else
                            {
                                file = ConfigurationCore.GetSection("Mtn.Library")?["Config." + depthWildcard + depthServer];
                            }

                            if (!file.IsNullOrWhiteSpaceMtn())
                            {
                                break;
                            }
                        }
                    }
                }

                if (file.IsNullOrWhiteSpaceMtn())
                {
                    if (!UseCoreConfiguration)
                    {
                        file = ConfigurationManager.AppSettings["Mtn.Library.Config.Fallback"];
                    }
                    else
                    {
                        file = ConfigurationCore.GetSection("Mtn.Library")?["Config.Fallback"];
                    }
                }


                if (file.IsNullOrWhiteSpaceMtn())
                {
                    return;
                }
                var filePath = Path.Combine(path, file);

                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException(
                              string.Format("File {0} error. Check if file exists and have permission to read", filePath));
                }

                if (!m_Configs.ContainsKey(server))
                {
                    m_Configs.Add(server, new SortedDictionary <string, string>());
                    m_CnnConfigs.Add(server, new SortedDictionary <string, ConnectionStringSettings>());
                }

                var fileExtension = file.SplitMtn(".").Last().ToLower();
                switch (fileExtension)
                {
                case "xml":
                case "config":
                    //LoadConfigFiles(server);
                    LoadXmlConfig(server, filePath);
                    break;

                case "json":
                case "js":
                case "javascript":
                    //LoadConfigFiles(server);
                    LoadJsonConfig(server, filePath);
                    break;

                //case "csv":
                //case "ini":
                default:
                    throw new Exception(
                              "Mtn do not read this extension, try [name].config.xml or [name].config.json ");
                }
            }
        }