[Spambayes-checkins] spambayes imapfilter.py,1.26,1.27

Tony Meyer anadelonbrin at users.sourceforge.net
Sat Apr 19 02:22:02 EDT 2003


Update of /cvsroot/spambayes/spambayes
In directory sc8-pr-cvs1:/tmp/cvs-serv4533

Modified Files:
	imapfilter.py 
Log Message:
Fixed the long-standing problem with establishing the new
id for a message after it is saved.
Stopped filtering messages marked as /Deleted.
Fixed so that the flags of the original message are copied
along with the text.
Found a way to get the date of a message from a
(well-behaved) imap server, so switched to using that as
a default, and then falling back on the date header, and
then as a last resort the current time.

Index: imapfilter.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/imapfilter.py,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** imapfilter.py	19 Apr 2003 01:45:20 -0000	1.26
--- imapfilter.py	19 Apr 2003 08:22:00 -0000	1.27
***************
*** 63,72 ****
        "OK", then the filter terminates.  Handling of these errors could be
        much nicer.
!     o The filter is currently designed to be periodically run (with cron,
!       for example).  It would probably be nicer if it was continually
!       running (like pop3proxy, for example) and periodically checked for
!       any new messages to process (with the RECENT command).  The period
!       could be an option.  This is partially done with the -l operand.
!     o Mail flagged with /Deleted shouldn't be filtered.
      o The flags should be copied along with the message (especially
        the /Seen flag, but all of them, really).
--- 63,67 ----
        "OK", then the filter terminates.  Handling of these errors could be
        much nicer.
!     o IMAP over SSL would be nice. (isbg has an example of how to do this)
      o The flags should be copied along with the message (especially
        the /Seen flag, but all of them, really).
***************
*** 197,205 ****
          # we can't actually update the message with IMAP
          # so what we do is create a new message and delete the old one
          response = imap.append(self.folder.name, None,
!                                self.extractTime(), self.as_string())
          self._check(response, 'append')
  
-         old_id = self.id
          if self.previous_folder is None:
              imap.SelectFolder(self.folder.name, False)
--- 192,216 ----
          # we can't actually update the message with IMAP
          # so what we do is create a new message and delete the old one
+         # we need to copy the flags as well
+         response = imap.uid("FETCH", self.id, "(FLAGS INTERNALDATE)")
+         self._check(response, 'fetch (flags internaldate)')
+         response_pattern = r"[\d]+ \(UID [\w]+ FLAGS (\([\\\w]+\)) "
+         response_pattern += r'INTERNALDATE ["]?([\w\-: ]+)["]?\)'
+         mo = re.match(response_pattern, response[1][0])
+         if mo is None:
+             msg_time = self.extractTime()
+             flags = None
+         else:
+             flags = mo.group(1)
+             msg_time = mo.group(2)
+ 
+         # See searching for new uid comments below
+         old_id = self.id
+         self["X-Spambayes-IMAP-OldID"] = old_id
+                     
          response = imap.append(self.folder.name, None,
!                                msg_time, self.as_string())
          self._check(response, 'append')
  
          if self.previous_folder is None:
              imap.SelectFolder(self.folder.name, False)
***************
*** 211,227 ****
  
          # We need to update the uid, as it will have changed
!         # XXX There will be problems here if the message *has not*
!         # XXX changed, as the message to be deleted will be found first
!         # XXX (if they are in the same folder)
!         imap.SelectFolder(self.folder.name, True)
!         #response = imap.uid("SEARCH", "TEXT", self.as_string())
!         #self._check(response, 'search')
!         #new_id = response[1][0]
!         new_id = ""
  
          #XXX This code to delete the old message id from the message
          #XXX info db and manipulate the message id, is a *serious* hack.
          #XXX There's gotta be a better way to do this.
- 
          message.msginfoDB._delState(self)
          self.id = new_id
--- 222,243 ----
  
          # We need to update the uid, as it will have changed
!         # Searching for the new message is full of problems.  Searching for
!         # the text sends far too much data through the connection, and
!         # doesn't work reliably anyway.  We instead search for a special
!         # header that we add for this explicit purpose.
!         imap.SelectFolder(self.folder.name, False)
!         response = imap.uid("SEARCH", "HEADER", "X-Spambayes-IMAP-OldID",
!                             old_id)
!         self._check(response, 'search')
!         new_id = response[1][0]
! 
!         # now that we know the new id, we need to correct the flags
!         if flags != None:
!             response = imap.uid("STORE", new_id, "+FLAGS.SILENT", flags)
!             self._check(response, "store flags")
  
          #XXX This code to delete the old message id from the message
          #XXX info db and manipulate the message id, is a *serious* hack.
          #XXX There's gotta be a better way to do this.
          message.msginfoDB._delState(self)
          self.id = new_id
***************
*** 250,253 ****
--- 266,277 ----
                  pass
  
+     def recent_keys(self):
+         '''Returns uids for all the messages in the folder that
+         are flagged as recent, but not flagged as deleted.'''
+         imap.SelectFolder(self.name, True)
+         response = imap.uid("SEARCH", "(RECENT UNDELETED)")
+         self._check(response, "SEARCH (RECENT UNDELETED)")
+         return response[1][0].split(' ')
+ 
      def keys(self):
          '''Returns uids for all the messages in the folder'''
***************
*** 257,267 ****
          if total_messages == '0':
              return []
!         response = imap.fetch("1:" + total_messages, "UID")
!         r = re.compile(r"[0-9]+ \(UID ([0-9]+)\)")
          uids = []
          for i in response[1]:
              mo = r.match(i)
              if mo is not None:
!                 uids.append(mo.group(1))
          return uids
  
--- 281,293 ----
          if total_messages == '0':
              return []
!         response = imap.fetch("1:" + total_messages, "(UID FLAGS)")
!         r = re.compile(r"[0-9]+ \(UID ([0-9]+) FLAGS \(([\\\w]*)\)\)")
          uids = []
          for i in response[1]:
              mo = r.match(i)
              if mo is not None:
!                 # We are not interested in messages marked as deleted
!                 if mo.group(2).lower() != "\\deleted":
!                     uids.append(mo.group(1))
          return uids
  
***************
*** 323,326 ****
--- 349,353 ----
  
                  msg.Save()
+ 
              
  class IMAPFilter(object):
***************
*** 328,332 ****
          self.spam_folder = IMAPFolder(options["imap", "spam_folder"])
          self.unsure_folder = IMAPFolder(options["imap", "unsure_folder"])
- 
          self.classifier = classifier
          
--- 355,358 ----
***************
*** 362,366 ****
          for filter_folder in options["imap", "filter_folders"].split():
              folder = IMAPFolder(filter_folder, False)
!             folder.Filter(self.classifier, self.spam_folder, self.unsure_folder)
   
          if options["globals", "verbose"]:
--- 388,393 ----
          for filter_folder in options["imap", "filter_folders"].split():
              folder = IMAPFolder(filter_folder, False)
!             folder.Filter(self.classifier, self.spam_folder,
!                           self.unsure_folder)
   
          if options["globals", "verbose"]:
***************
*** 455,458 ****
--- 482,486 ----
      # XXX using the same port by default.
      if launchUI:
+         imap.login(options["imap", "username"], pwd)
          httpServer = UserInterfaceServer(options["html_ui", "port"])
          httpServer.register(IMAPUserInterface(classifier, imap))





More information about the Spambayes-checkins mailing list