Refactored plugin architecture out of WebThing.cs
[WebThing.git] / Config.cs
index 9ad8095..49e7203 100644 (file)
--- a/Config.cs
+++ b/Config.cs
@@ -1,24 +1,41 @@
 using System;
 using System.IO;
+using System.Collections;
 using System.Collections.Generic;
 using System.Text.RegularExpressions;
 
 namespace bytex64.WebThing {
     public class Config {
-        public static string ConfigurationPrefix = "/etc";
+        public static List<string> ConfigPath;
 
         public static Dictionary<string,string> Options;
         public static Dictionary<string,Dictionary<string,string>> PluginOptions;
         public static string[] Arguments;
-        private static HashSet<string> CommandLineOptions;
+        public static List<string> Plugins;
 
-        public static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$");
-        public static Regex file_var_re = new Regex(@"^([\w.]+)\s*=\s*(.*)$");
+        private static HashSet<string> CommandLineOptions;
+        private static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$");
+        private static Regex file_var_re = new Regex(@"^([\w.]+)\s*=\s*(.*)$");
+        private static Regex file_command_re = new Regex(@"^([\w]+)\s*(.*)$");
 
         static Config() {
             Options = new Dictionary<string,string>();
             PluginOptions = new Dictionary<string,Dictionary<string,string>>();
             CommandLineOptions = new HashSet<string>();
+            Plugins = new List<string>();
+
+            // Set up ConfigPath
+            IDictionary Env = Environment.GetEnvironmentVariables();
+            ConfigPath = new List<string>();
+            ConfigPath.Add("/etc/WebThing");
+            if (Env.Contains("HOME")) {
+                ConfigPath.Add(Env["HOME"] + "/.config/WebThing");
+                ConfigPath.Add(Env["HOME"] + "/.WebThing");
+            }
+            string LocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
+            if (LocalAppData != "") {
+                ConfigPath.Add(LocalAppData + "/WebThing");
+            }
         }
 
         private static void ParseOption(string key, string val) {
@@ -30,7 +47,15 @@ namespace bytex64.WebThing {
                     PluginOptions[plugin] = new Dictionary<string,string>();
                 PluginOptions[plugin][name] = val;
             } else {                   // Global Option
-                Options[name] = val;
+                switch(key) {
+                case "Plugin":
+                case "plugin":
+                    Plugins.Add(val);
+                    break;
+                default:
+                    Options[key] = val;
+                    break;
+                }
             }
         }
 
@@ -103,8 +128,13 @@ namespace bytex64.WebThing {
         }
 
         public static void Load() {
-            LoadFile(ConfigurationPrefix + "/WebThing.conf");
-            LoadFile(Environment.GetEnvironmentVariable("HOME") + "/.config/WebThing/WebThing.conf");
+            foreach (string path in ConfigPath) {
+                if (!Directory.Exists(path)) continue;
+                string[] ConfigFiles = Directory.GetFiles(path, "*.conf");
+                foreach (string file in ConfigFiles) {
+                    LoadFile(file);
+                }
+            }
             if (Options.ContainsKey("config"))
                 LoadFile(Options["config"]);
         }
@@ -120,11 +150,30 @@ namespace bytex64.WebThing {
 
             string line;
             while ((line = f.ReadLine()) != null) {
-                Match m = file_var_re.Match(line.Trim());
+                Match m;
+
+                line = line.Trim();
+                m = file_var_re.Match(line);
                 if (m.Success) {
                     string key = m.Groups[1].Value;
                     if (!CommandLineOptions.Contains(key))
                         ParseOption(key, m.Groups[2].Value);
+                    continue;
+                }
+
+                m = file_command_re.Match(line);
+                if (m.Success) {
+                    string cmd = m.Groups[1].Value;
+                    switch(cmd) {
+                    case "Plugin":
+                    case "plugin":
+                        Plugins.Add(m.Groups[2].Value);
+                        break;
+                    default:
+                        Console.WriteLine("Unknown Command in {0}: {1}", filename, line);
+                        break;
+                    }
+                    continue;
                 }
             }