Patch: add regex support to ipurge to improve folder selection

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,


attached the patch to add regex pattern matching to folders for ipurge.



-- 
With best regards,

Carsten Hoeger
diff -urN cyrus-imapd-2.3.7.orig/imap/ipurge.c cyrus-imapd-2.3.7/imap/ipurge.c
--- cyrus-imapd-2.3.7.orig/imap/ipurge.c	2009-07-21 10:43:19.000000000 +0200
+++ cyrus-imapd-2.3.7/imap/ipurge.c	2009-07-21 10:43:43.000000000 +0200
@@ -68,6 +68,9 @@
 #include "xmalloc.h"
 #include "mboxlist.h"
 
+#include <sys/types.h>
+#include <regex.h>
+
 /* config.c stuff */
 const int config_need_data = CONFIG_NEED_PARTITION_DATA;
 
@@ -85,6 +88,8 @@
 int skipflagged = 0;
 int datemode = OFFSET_SENTDATE;
 int invertmatch = 0;
+char* regpattern = NULL;
+int notpattern = 0;
 
 /* for statistical purposes */
 typedef struct mbox_stats_s {
@@ -115,11 +120,17 @@
 
   if (geteuid() == 0) fatal("must run as the Cyrus user", EX_USAGE);
 
-  while ((option = getopt(argc, argv, "C:hxd:b:k:m:fsXi")) != EOF) {
+  while ((option = getopt(argc, argv, "C:hxd:b:k:m:fsXip:n")) != EOF) {
     switch (option) {
     case 'C': /* alt config file */
       alt_config = optarg;
       break;
+    case 'p':
+      regpattern = optarg;
+      break;
+    case 'n':
+      notpattern = 1;
+      break;
     case 'd': {
       if (optarg == 0) {
 	usage(argv[0]);
@@ -211,7 +222,7 @@
 
 int
 usage(char *name) {
-  printf("usage: %s [-f] [-s] [-C <alt_config>] [-x] [-X] [-i] {-d days | -b bytes|-k Kbytes|-m Mbytes}\n\t[mboxpattern1 ... [mboxpatternN]]\n", name);
+  printf("usage: %s [-f] [-s] [-C <alt_config>] [-x] [-X] [-i] [-p <pattern] [-n] {-d days | -b bytes|-k Kbytes|-m Mbytes}\n\t[mboxpattern1 ... [mboxpatternN]]\n", name);
   printf("\tthere are no defaults and at least one of -d, -b, -k, -m\n\tmust be specified\n");
   printf("\tif no mboxpattern is given %s works on all mailboxes\n", name);
   printf("\t -x specifies an exact match for days or size\n");
@@ -219,6 +230,8 @@
   printf("\t -s skip over messages that are flagged.\n");
   printf("\t -X use delivery time instead of date header for date matches.\n");
   printf("\t -i invert match logic: -x means not equal, date is for newer, size is for smaller.\n");
+  printf("\t -p <pattern> apply an additional regex pattern to mailbox.\n");
+  printf("\t -n negate pattern specified with -p (not matches).\n");
   exit(0);
 }
 
@@ -237,12 +250,31 @@
 
   memset(&stats, '\0', sizeof(mbox_stats_t));
 
-  if (verbose) {
-      char mboxname[MAX_MAILBOX_NAME+1];
+  char mboxname[MAX_MAILBOX_NAME+1];
+  /* Convert internal name to external */
+  (*purge_namespace.mboxname_toexternal)(&purge_namespace, name,
+					 "cyrus", mboxname);
+  if( regpattern != NULL ) {
+#define BLEN 512
+    char regerrbuf[BLEN];
+    regex_t preg;
+    int ret;
+    if( (ret=regcomp(&preg, regpattern, REG_EXTENDED)) ) {
+      syslog(LOG_ERR, "regcomp failed on mboxname <%s>: %s", name,regerror(ret, &preg, regerrbuf, BLEN));
+      return 1;
+    }
+    
+    ret = regexec(&preg, mboxname, 0, NULL, 0);
+    if( ret ) {
+      if( !notpattern )
+	return 0;
+    } else {
+      if( notpattern )
+	return 0;
+    }
+  }
 
-      /* Convert internal name to external */
-      (*purge_namespace.mboxname_toexternal)(&purge_namespace, name,
-					     "cyrus", mboxname);
+  if (verbose) {
       printf("Working on %s...\n", mboxname);
   }
 
diff -urN cyrus-imapd-2.3.7.orig/man/ipurge.8 cyrus-imapd-2.3.7/man/ipurge.8
--- cyrus-imapd-2.3.7.orig/man/ipurge.8	2009-07-21 10:43:19.000000000 +0200
+++ cyrus-imapd-2.3.7/man/ipurge.8	2009-07-21 10:57:16.000000000 +0200
@@ -75,6 +75,14 @@
 ]
 .br
        [
+.B \-p
+.I regex-pattern
+]
+[
+.B \-n
+]
+.br
+       [
 .IR mailbox-pattern ...
 ]
 .SH DESCRIPTION
@@ -128,6 +136,17 @@
 .TP
 .B \-s
 Skip over messages that have the \\Flagged flag set.
+.TP
+.B \-p
+Specify an additional
+.IR regex (7)
+pattern to limit the mailbox(es) where to apply the requested operation. All mailboxes matching
+the \fImailbox-pattern\fR as specified above must also match this addtional expression, if specified.
+.TP
+.B \-n
+Negate the expression as specified with -p. All mailboxes matching the
+\fImailbox-pattern\fR as specified above must \fBNOT\fR match this addtional expression.
+.TP
 .SH FILES
 .TP
 .B /etc/imapd.conf

Attachment: pgpP48ackGwpy.pgp
Description: PGP signature

----
Cyrus Home Page: http://cyrusimap.web.cmu.edu/
Cyrus Wiki/FAQ: http://cyrusimap.web.cmu.edu/twiki
List Archives/Info: http://asg.web.cmu.edu/cyrus/mailing-list.html

[Index of Archives]     [Cyrus SASL]     [Squirrel Mail]     [Asterisk PBX]     [Video For Linux]     [Photo]     [Yosemite News]     [gtk]     [KDE]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux