Explorar el Código

add options to filter with smtp reject message (info)
permit wildcard matching with filters for email, info

Emmanuel Bouthenot hace 17 años
padre
commit
48e49c0565
Se han modificado 1 ficheros con 42 adiciones y 14 borrados
  1. 42 14
      pmailq

+ 42 - 14
pmailq

@@ -14,19 +14,25 @@ _DESC = "%s postfix mail queue manager" % _NAME
 _VERSION = '0.1'
 _AUTHOR = 'Emmanuel Bouthenot <kolter@openics.org>'
 
+
+MAILQ = "postqueue -p"
+DELQ = "postsuper -d"
+
+
 from optparse import OptionParser, OptionGroup # needs python >= 2.3
-import sys, popen2
+import sys, popen2, fnmatch
 
 class mailqueue:
 
     def __init__(self):
         self.mailqueue = []
         self.filters = {
-            'email' : None,
+            'email'   : None,
+            'msg'     : None,
             'lowsize' : 0,
-            'upsize' : 0,
-            'active' : False,
-            'hold' : False
+            'upsize'  : 0,
+            'active'  : False,
+            'hold'    : False
             }
         self.parse()
 
@@ -37,7 +43,7 @@ class mailqueue:
 
     def parse(self):
         
-        proc = popen2.Popen3("postqueue -p", True)    
+        proc = popen2.Popen3(MAILQ, True)    
         
         p_ret = proc.wait()
         if p_ret != 0:
@@ -92,9 +98,21 @@ class mailqueue:
                 })
 
 
-    def check(self, size, active, hold, dest):
-        if self.filters['email'] != None and self.filters['email'].lower() not in dest:
-            return False
+    def check(self, size, active, hold, dest, infos):
+        if self.filters['email'] != None:
+            match = False
+            for e in dest:
+                if fnmatch.fnmatch(e.lower(), self.filters['email'].lower()):
+                    match = True
+            if not match:
+                return False
+        if self.filters['msg'] != None:
+            match = False
+            for i in infos:
+                if fnmatch.fnmatch(i.lower(), self.filters['msg'].lower()):
+                    match = True
+            if not match:
+                return False
         if self.filters['active'] and not active:
             return False
         if self.filters['hold'] and not hold:
@@ -116,32 +134,39 @@ class mailqueue:
             out += "  -hold: %s\n" % str(m['hold'])
             out += "  -to:\n"
             to = []
+            i = []
             for n in m['queue']:
+                i.append(n['info'])
                 to += n['dest']
                 out += "    + %s : [%s]\n" % (",".join(n['dest']), n['info'])
 
-            if self.check(m['size'], m['active'], m['hold'], to):
+            if self.check(m['size'], m['active'], m['hold'], to, i):
                 print out
 
         
     def showcsv(self):
         for m in self.mailqueue:
             e = []
+            i = []
             for n in m['queue']:
+                i.append(n['info'])
                 for o in n['dest']:
                     e.append(o)
-                if self.check(m['size'], m['active'], m['hold'], e):
+                
+                if self.check(m['size'], m['active'], m['hold'], e, i):
                     print "%s|%s|%s|%d|%d|%s" % (m['id'], m['date'], m['size'], int(m['active']), int(m['hold']),  ",".join(n['dest']))
 
 
     def delete(self):
         for m in self.mailqueue:
             e = []
+            i=[]
             for n in m['queue']:
+                i.append(n['info'])
                 for o in n['dest']:
                     e.append(o)
-                if self.check(m['size'], m['active'], m['hold'], e):
-                    proc = popen2.Popen3("postsuper -d %s" % m['id'], True)
+                if self.check(m['size'], m['active'], m['hold'], e, i):
+                    proc = popen2.Popen3("%s %s" % (DELQ, m['id']), True)
                     p_ret = proc.wait()
                     if p_ret != 0:
                         print "deleting %s [FAILED] (%s)" % (m['id'], "".join(proc.childerr.readlines()).strip())
@@ -154,8 +179,10 @@ def main():
     parser = OptionParser(usage=usage, description=desc, version=("%s %s" % (_NAME, _VERSION)))
 
     opts = OptionGroup(parser, "filters")
-    opts.add_option("-e", "--email", dest="email", type="string", metavar="EMAIL", help="select entries in queue sent to EMAIL")
+    opts.add_option("-e", "--email", dest="email", type="string", metavar="EMAIL", help="select entries in queue with email matching EMAIL")
     parser.set_defaults(email=None)
+    opts.add_option("-m", "--msg", dest="msg", type="string", metavar="MSG", help="select entries in queue with error message matching MSG")
+    parser.set_defaults(msg=None)
     opts.add_option("-l", "--size-lower", dest="lowsize", type="int", metavar="SIZE", help="select entries in queue with size smaller than SIZE")
     parser.set_defaults(lowsize=0)
     opts.add_option("-u", "--size-upper", dest="upsize", type="int", metavar="SIZE", help="select entries in queue with size upper than SIZE")
@@ -168,6 +195,7 @@ def main():
 
     m = mailqueue()
     m.add_filter("email", options.email)
+    m.add_filter("msg", options.msg)
     m.add_filter("lowsize", options.lowsize)
     m.add_filter("upsize", options.upsize)
     m.add_filter("active", options.active)