nmh-workers
[Top] [All Lists]

[Nmh-workers] slocal and regular expressions.

2010-10-05 15:30:28
Hi *

In an ever more concentrated battle to separate out spam, I've added
regexes to slocal. By this, I mean that in the second column of
~/.maildelivery, the match is not for a case-insensitive substring, but
for a case-insensitive regex.

I've fairly roughly hacked it into the FreeBSD port of NMH-1.3, and it
could stand some tidying.

Would folks be interested in this?

I'm guessing that for backwards compatibility's sake, some kind of
syntactical wrapping may be desirable, but I've not done this.

I also fixed a "utmp" vs "utmpx" thing.

M
-- 
Mark R V Murray
Cert APS(Open) Dip Phys(Open) BSc Open(Open) BSc(Hons)(Open)
Pi: 132511160
--- ./uip/rcvtty.c.orig 2008-04-11 15:12:55.000000000 +0100
+++ ./uip/rcvtty.c      2010-08-30 13:10:52.000000000 +0100
@@ -23,7 +23,7 @@
 #include <signal.h>
 #include <fcntl.h>
 
-#include <utmp.h>
+#include <utmpx.h>
 
 #ifndef HAVE_GETUTENT
 # ifndef UTMP_FILE
@@ -95,7 +95,7 @@
 #ifdef HAVE_GETUTENT
     struct utmp * utp;
 #else
-    struct utmp ut;
+    struct utmpx ut;
     register FILE *uf;
 #endif
 
@@ -193,8 +193,8 @@
     if ((uf = fopen (UTMP_FILE, "r")) == NULL)
        exit (RCV_MBX);
     while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1)
-       if (ut.ut_name[0] != 0
-               && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
+       if (ut.ut_user[0] != 0
+               && strncmp (user, ut.ut_user, sizeof(ut.ut_user)) == 0) {
            strncpy (tty, ut.ut_line, sizeof(ut.ut_line));
            alert (tty, md);
        }
--- ./uip/slocal.c.orig 2008-04-11 15:12:55.000000000 +0100
+++ ./uip/slocal.c      2010-08-30 13:13:51.000000000 +0100
@@ -36,6 +36,12 @@
 #include <sys/ioctl.h>
 #include <fcntl.h>
 
+#define USE_REGEX_PATTERN
+
+#ifdef USE_REGEX_PATTERN
+#include <regex.h>
+#endif
+
 #ifdef INITGROUPS_HEADER
 #include INITGROUPS_HEADER
 #else
@@ -56,7 +62,7 @@
 #include NDBM_HEADER
 #endif
 
-#include <utmp.h>
+#include <utmpx.h>
 
 #ifndef HAVE_GETUTENT
 # ifndef UTMP_FILE
@@ -458,6 +464,12 @@
 static int
 usr_delivery (int fd, char *delivery, int su)
 {
+#ifdef USE_REGEX_PATTERN
+    regex_t reg;
+    regmatch_t regmatch;
+    int rv, match;
+    char buf[1024], *mstr;
+#endif
     int i, accept, status=1, won, vecp, next;
     char *field, *pattern, *action, *result, *string;
     char buffer[BUFSIZ], tmpbuf[BUFSIZ];
@@ -590,8 +602,41 @@
                 * find header field in lookup table, and
                 * see if the pattern matches.
                 */
+#ifdef USE_REGEX_PATTERN
+               match = 0;
+
+               if ((p = lookup (hdrs, field)) && (p->p_value != NULL)) {
+
+                       if (debug)
+                               debug_printf("p_value = '%s' pattern = '%s'\n",
+                                   p->p_value, pattern);
+
+                       if ((rv = regcomp(&reg, pattern, 
REG_EXTENDED|REG_ICASE|REG_NOSUB)) != 0) {
+                               regerror(rv, &reg, buf, sizeof(buf));
+                               debug_printf("Cannot compile RE `%s' (%s)\n",
+                                   pattern, buf);
+                       }
+
+                       rv = regexec(&reg, p->p_value, 1, &regmatch, 0);
+                       if (rv == 0) {
+                               match = 1;
+                       } else if (rv != REG_NOMATCH) {
+                               regerror(rv, &reg, buf, sizeof(buf));
+                               debug_printf("RE evaluation error (%s)\n", buf);
+                       }
+
+                       regfree(&reg);
+
+               }
+
+               if (debug)
+                       debug_printf("match = %d\n", match);
+
+               if (match) {
+#else
                if ((p = lookup (hdrs, field)) && (p->p_value != NULL)
                        && matches (p->p_value, pattern)) {
+#endif
                    next = 1;
                } else {
                    next = 0;
@@ -979,7 +1024,7 @@
 static int
 logged_in (void)
 {
-    struct utmp ut;
+    struct utmpx ut;
     FILE *uf;
 
     if (utmped)
@@ -989,8 +1034,8 @@
        return NOTOK;
 
     while (fread ((char *) &ut, sizeof(ut), 1, uf) == 1) {
-       if (ut.ut_name[0] != 0
-           && strncmp (user, ut.ut_name, sizeof(ut.ut_name)) == 0) {
+       if (ut.ut_user[0] != 0
+           && strncmp (user, ut.ut_user, sizeof(ut.ut_user)) == 0) {
            if (debug)
                continue;
            fclose (uf);
_______________________________________________
Nmh-workers mailing list
Nmh-workers(_at_)nongnu(_dot_)org
http://lists.nongnu.org/mailman/listinfo/nmh-workers
<Prev in Thread] Current Thread [Next in Thread>