update TODO, partially update error.c, rename module_get to modules_get, update config.lua
authorTero Marttila <terom@fixme.fi>
Sun, 12 Apr 2009 18:35:16 +0300
changeset 134 978041c1c04d
parent 133 e2d0c0c23b39
child 135 9159bd51525f
update TODO, partially update error.c, rename module_get to modules_get, update config.lua
TODO
config.lua
src/error.c
src/lua_objs.c
src/module.c
src/module.h
src/modules/logwatch.c
src/nexus.c
--- a/TODO	Sun Apr 12 17:42:34 2009 +0300
+++ b/TODO	Sun Apr 12 18:35:16 2009 +0300
@@ -15,10 +15,20 @@
 irc_net:
  * reconnect, maybe cycling servers?
 
+irc_chan:
+ * strdup irc_chan_info strings, currently lua_irc just leaks them
+
 config:
  * user-defined types
  * return values
 
+lua_console:
+ * some kind of remote console
+
 irc_log:
+ * log messages sent by ourself
  * recode to valid UTF8
 
+logwatch:
+ * figure out how to handle overflow
+
--- a/config.lua	Sun Apr 12 17:42:34 2009 +0300
+++ b/config.lua	Sun Apr 12 18:35:16 2009 +0300
@@ -1,4 +1,87 @@
-local conf = {
+
+--
+-- Helper functions
+--
+
+-- match all lines and output them as-is
+local function logwatch_filter_all () 
+    return { pat=nil, fmt=nil }
+end
+
+-- match using a regex pattern, but output the full line
+local function logwatch_filter_raw (pat)
+    return { pat=pat, fmt=nil }
+end
+
+-- match using a regexp pattern, and output a formatted line
+local function logwatch_filter (pat, fmt)
+    return { pat=pat, fmt=fmt }
+end
+
+-- match auth.log sudo entries
+local function logwatch_filter_sudo ()
+    return logwatch_filter(
+        "(?P<hostname>\\S+)\\s+sudo:\\s*(?P<username>\\S+) : TTY=(?P<tty>\\S+) ; PWD=(?P<pwd>.+?) ; USER=(?P<target_user>\\S+) ; COMMAND=(?P<command>.*)",
+        "{username}:{tty} - {target_user}@{hostname}:{pwd} - {command:r}"
+    )
+end
+
+--
+-- Procedural config
+--
+local function apply_config (conf)
+    -- apply general
+    log_level(conf.log_level)
+
+    -- apply conf.name
+    client:set_defaults(conf.name.nickname, conf.name.username, conf.name.realname)
+
+    -- apply conf.networks
+    for network, settings in pairs(conf.networks) do
+        -- establish the irc_net
+        net = client:connect(network, settings.hostname)
+
+        -- join each channel
+        for i, channel in ipairs(settings.channels) do
+            net:join(channel)
+        end
+    end
+
+    -- apply conf.modules_path
+    if conf.modules_path then
+        modules:path(conf.modules_path)
+    end
+
+    -- apply conf.modules
+    for name, settings in pairs(conf.modules) do
+        -- load the module
+        module = modules:load(name, settings.path)
+
+        -- apply confs
+        for key, value in pairs(settings.conf) do
+            module:conf(key, value)
+        end
+    end
+
+    -- conf.mod_logwatch
+    if conf.mod_logwatch then
+        module = modules:load("logwatch")
+
+        for fifo_path, settings in pairs(conf.mod_logwatch) do
+            module:conf("source_fifo", fifo_path)
+            source_name = fifo_path
+            
+            for filter_name, filter in pairs(settings.filters) do
+                module:conf("filter", filter_name, source_name, filter.pat, filter.fmt, settings.channel)
+            end
+        end
+    end
+end
+
+--
+-- Declarative configuration settings
+--
+apply_config{
     log_level   = "DEBUG",
 
     name = {
@@ -27,38 +110,17 @@
             }
         }
     },
+
+    mod_logwatch = {
+        ["test.fifo"] = {
+            channel     = "PVLNet/#test",
+            
+            filters     = {
+                ["test.foo"]    = logwatch_filter_raw("foo"),
+                ["test.sudo"]   = logwatch_filter_sudo(),
+            }
+        },
+    },
 }
 
--- apply general
-log_level(conf.log_level)
-
--- apply conf.name
-client:set_defaults(conf.name.nickname, conf.name.username, conf.name.realname)
-
--- apply conf.networks
-for network, settings in pairs(conf.networks) do
-    -- establish the irc_net
-    net = client:connect(network, settings.hostname)
 
-    -- join each channel
-    for i, channel in ipairs(settings.channels) do
-        net:join(channel)
-    end
-end
-
--- apply conf.modules_path
-if conf.modules_path then
-    modules:path(conf.modules_path)
-end
-
--- apply conf.modules
-for name, settings in pairs(conf.modules) do
-    -- load the module
-    module = modules:load(name, settings.path)
-
-    -- apply confs
-    for key, value in pairs(settings.conf) do
-        module:conf(key, value)
-    end
-end
-
--- a/src/error.c	Sun Apr 12 17:42:34 2009 +0300
+++ b/src/error.c	Sun Apr 12 18:35:16 2009 +0300
@@ -32,6 +32,8 @@
     {   ERR_WRITE_EOF,                      "write: EOF",                               ERR_EXTRA_NONE      },
     {   ERR_FCNTL,                          "fcntl",                                    ERR_EXTRA_ERRNO     },
     {   ERR_CLOSE,                          "close",                                    ERR_EXTRA_ERRNO     },
+    {   ERR_GETSOCKOPT,                     "getsockopt",                               ERR_EXTRA_ERRNO     },
+    {   ERR_OPEN,                           "open",                                     ERR_EXTRA_ERRNO     },
     {   _ERR_INVALID,                       NULL,                                       0                   }
 
 }, _sock_gnutls_error_desc[] = {
--- a/src/lua_objs.c	Sun Apr 12 17:42:34 2009 +0300
+++ b/src/lua_objs.c	Sun Apr 12 18:35:16 2009 +0300
@@ -342,7 +342,7 @@
     const char *name = luaL_checkstring(L, 2);
 
     // look it up, as a new reference
-    if ((module = module_get(lua_modules->modules, name)) == NULL)
+    if ((module = modules_get(lua_modules->modules, name)) == NULL)
         return luaL_error(L, "module_get: %s: no such module", name);
 
     // wrap
--- a/src/module.c	Sun Apr 12 17:42:34 2009 +0300
+++ b/src/module.c	Sun Apr 12 18:35:16 2009 +0300
@@ -41,7 +41,7 @@
 /**
  * module_get without the refcount stuff
  */
-static struct module* module_lookup (struct modules *modules, const char *name)
+static struct module* modules_lookup (struct modules *modules, const char *name)
 {
     struct module *module = NULL;
 
@@ -57,12 +57,12 @@
     return NULL;
 }
 
-struct module* module_get (struct modules *modules, const char *name)
+struct module* modules_get (struct modules *modules, const char *name)
 {
     struct module *module;
     
     // get it
-    if (!(module = module_lookup(modules, name)))
+    if (!(module = modules_lookup(modules, name)))
         return NULL;
 
     // up the refcount
@@ -193,7 +193,7 @@
         return SET_ERROR(err, ERR_MODULE_NAME);
     
     // already open with the same name?
-    if (module_lookup(modules, _info->name))
+    if (modules_lookup(modules, _info->name))
         return SET_ERROR(err, ERR_MODULE_DUP);
 
     // alloc
--- a/src/module.h	Sun Apr 12 17:42:34 2009 +0300
+++ b/src/module.h	Sun Apr 12 18:35:16 2009 +0300
@@ -211,7 +211,7 @@
  * @param name the module name to get
  * @return the module struct, or NULL if not found
  */
-struct module* module_get (struct modules *modules, const char *name);
+struct module* modules_get (struct modules *modules, const char *name);
 
 /**
  * Unload all modules, this just calls module_unload for each module, logging errors as warnings.
--- a/src/modules/logwatch.c	Sun Apr 12 17:42:34 2009 +0300
+++ b/src/modules/logwatch.c	Sun Apr 12 18:35:16 2009 +0300
@@ -119,6 +119,9 @@
     // lookup the channel
     if ((chan = logwatch_get_chan(ctx, irc_chan)) == NULL)
         RETURN_SET_ERROR_STR(err, ERR_MODULE_CONF, "logwatch_get_chan failed");
+    
+    log_info("add filter: name=%s, source=%s, pattern=%s, format=%s, chan=%s",
+        name, source_name, pattern, format, irc_chan_name(irc_chan));
 
     // invoke
     if ((filter = logwatch_filter(ctx, name, source, pattern, format, chan, err)) == NULL)
--- a/src/nexus.c	Sun Apr 12 17:42:34 2009 +0300
+++ b/src/nexus.c	Sun Apr 12 18:35:16 2009 +0300
@@ -258,7 +258,7 @@
     conf_value = opt;
 
     // lookup the module
-    if ((module = module_get(nexus->modules, module_name)) == NULL)
+    if ((module = modules_get(nexus->modules, module_name)) == NULL)
         RETURN_SET_ERROR_STR(err, ERR_CMD_OPT, "unknown module for --config");
     
     // do the config