implemented atime handling, rules maintain a timestamp when they where last triggered
authorChristian Thaeter <ct@pipapo.org>
Wed, 17 Oct 2007 16:29:20 +0000 (18:29 +0200)
committerChristian Thaeter <ct@pipapo.org>
Wed, 17 Oct 2007 16:29:20 +0000 (18:29 +0200)
src/rxpd.h
src/rxpd_connection_cmd.c
src/rxpd_file.c
src/rxpd_rule.c

index 844efae..ced01a0 100644 (file)
@@ -38,6 +38,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <event.h>
+#include <time.h>
 
 #define RXPD_COMMANDS                                           \
   RXPD_CMD(CHECK,       "data against regular expressions")     \
@@ -113,6 +114,7 @@ struct rxpd_rule
 {
   llist node;
   char* string;
+  time_t atime;
   regex_t rx;
 };
 
index 1e352a1..86262e6 100644 (file)
@@ -46,6 +46,9 @@ rxpd_connection_cmd_CHECK (int fd, short event, void* ptr)
                     {
                       if (regexec (&rule->rx, line, 0, NULL, 0) == 0)
                         {
+                          if (rule->atime != (time_t) -1)
+                            time (&rule->atime);
+
                           rxpd_buffer_printf (&self->out, "%s\n", rule->string);
                           break;
                         }
@@ -331,7 +334,12 @@ rxpd_connection_cmd_DUMP (int fd, short event, void* ptr)
       LLIST_FOREACH (&self->file->rules, n)
         {
           struct rxpd_rule* rule = (struct rxpd_rule*)n;
-          rxpd_buffer_printf (&self->out, "%s\n", rule->string);
+          if (rule->atime != (time_t)-1)
+            rxpd_buffer_printf (&self->out, "%ld:%s\n", rule->atime, rule->string);
+          else if (*rule->string != '#')
+            rxpd_buffer_printf (&self->out, ":%s\n", rule->string);
+          else
+            rxpd_buffer_printf (&self->out, "%s\n", rule->string);
         }
     }
 
index 0a78c72..75b551d 100644 (file)
@@ -127,7 +127,12 @@ rxpd_file_save (struct rxpd_file* self)
       LLIST_FOREACH (&self->rules, n)
         {
           struct rxpd_rule* node = (struct rxpd_rule*)n;
-          fprintf (f, "%s\n", node->string);
+          if (node->atime != (time_t)-1)
+            fprintf (f, "%ld:%s\n", node->atime, node->string);
+          else if (*node->string != '#')
+            fprintf (f, ":%s\n", node->string);
+          else
+            fprintf (f, "%s\n", node->string);
         }
 
       fclose (f);
index 2877742..f59ed0e 100644 (file)
@@ -31,33 +31,49 @@ rxpd_rule_new (const char* buf)
   if (*buf != '#')
     {
       int err;
-      char* rxstart = strchr (buf, ':');
+      char* namestart = strchr (buf, ':');
+      char* rxstart = namestart? strchr (namestart+1, ':') : NULL;
 
       if (!rxstart)
         self->string = rxpd_strdup ("#ERROR: Syntax error, line was neither a comment nor a rule");
       else
         {
+          if (namestart == buf)
+            /* No atime given */
+            self->atime = (time_t)-1;
+          else
+            {
+              /* atime given */
+              self->atime = atoi (buf);
+              if (!self->atime)
+                /* atime was zero or not set */
+                self->atime = time (NULL)-1;
+            }
+
           // TODO regflags from base
           err = regcomp (&self->rx, rxstart+1, REG_EXTENDED|REG_ICASE|REG_NOSUB);
 
           if (!err)
-            self->string = rxpd_strdup (buf);
+            self->string = rxpd_strdup (namestart+1);
           else
             {
               regfree (&self->rx);
               char ebuf[256];
               size_t len = regerror (err, NULL, ebuf, 256);
-              self->string = rxpd_malloc (len + strlen(buf) + 14);
+              self->string = rxpd_malloc (len + strlen(namestart+1) + 14);
               strcpy (self->string, "#ERROR: ");
               strcat (self->string, ebuf);
               strcat (self->string, " in '");
-              strcat (self->string, buf);
+              strcat (self->string, namestart+1);
               strcat (self->string, "'");
             }
         }
     }
   else
-    self->string = rxpd_strdup (buf);
+    {
+      self->atime = (time_t)-1;
+      self->string = rxpd_strdup (buf);
+    }
 
   return self;
 }