[Spambayes-checkins] spambayes imapfilter.py,1.5,1.6
Tim Stone
timstone4 at users.sourceforge.net
Thu Apr 10 20:11:24 EDT 2003
Update of /cvsroot/spambayes/spambayes
In directory sc8-pr-cvs1:/tmp/cvs-serv20052
Modified Files:
imapfilter.py
Log Message:
Made some changes to accomodate the new message class. Not tested
yet, but checked in on the chance that Tony wants to see it sooner than
I can get it tested.
Index: imapfilter.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/imapfilter.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** imapfilter.py 9 Apr 2003 06:16:15 -0000 1.5
--- imapfilter.py 11 Apr 2003 02:11:21 -0000 1.6
***************
*** 29,39 ****
# a list is retrieved via imap.list()
- # IMAPFolder objects get created all over the place, and don't persist
- # at all. It would probably be good to change this, especially if
- # the filter doesn't run just once
-
- # All the imap responses should be checked - [0] should be "OK"
- # otherwise an error will occur and who knows what will happen
-
try:
True, False
--- 29,32 ----
***************
*** 55,65 ****
imap = None
! class IMAPMessage(message.HeaderMessage):
! def __init__(self, folder_id, folder_name, message_id):
message.Message.__init__(self)
self.setId(message_id)
! self.folder_id = folder_id
! self.folder_name = folder_name
! self.previous_folder = None
def extractTime(self):
--- 48,62 ----
imap = None
! class IMAPMessage(message.SBHeaderMessage):
! # response checking is necessary throughout this class
! def __init__(self, folder, message_id):
message.Message.__init__(self)
self.setId(message_id)
! self.folder = folder
!
! def _check(self, response, command):
! if response[0] != "OK":
! print "Invalid response to %s:\n%s" % (command, response)
! sys.exit(-1)
def extractTime(self):
***************
*** 75,85 ****
# this more than once)
if self.previous_folder is not None:
! self.previous_folder = self.folder_name
! self.folder_name = dest
def Save(self):
# 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.get_payload())
# we need to update the uid, as it will have changed
--- 72,82 ----
# this more than once)
if self.previous_folder is not None:
! self.previous_folder = self.folder
! self.folder = dest
def Save(self):
# 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.get_payload())
# we need to update the uid, as it will have changed
***************
*** 91,100 ****
self.id = response[1][0]
if self.previous_folder is not None:
! response = imap.select(self.previous_folder, False)
self.previous_folder = None
# this line is raising an error, but WHY?
#response = imap.uid("STORE", old_id, "+FLAGS.SILENT", "(\\Deleted)")
class IMAPFolder(object):
def __init__(self, folder_name, readOnly=True):
self.name = folder_name
--- 88,99 ----
self.id = response[1][0]
if self.previous_folder is not None:
! response = imap.select(self.previous_folder.name, False)
self.previous_folder = None
# this line is raising an error, but WHY?
#response = imap.uid("STORE", old_id, "+FLAGS.SILENT", "(\\Deleted)")
+
class IMAPFolder(object):
+ # response checking is necessary throughout this class
def __init__(self, folder_name, readOnly=True):
self.name = folder_name
***************
*** 114,118 ****
if response[0] != "OK":
self.rfc822_command = "(RFC822)"
!
def __iter__(self):
'''IMAPFolder is iterable'''
--- 113,126 ----
if response[0] != "OK":
self.rfc822_command = "(RFC822)"
!
! def Select(self):
! imap.select(self.name, False)
! self._check(folder, 'select')
!
! def _check(self, response, command):
! if response[0] != "OK":
! print "Invalid response to %s:\n%s" % (command, response)
! sys.exit(-1)
!
def __iter__(self):
'''IMAPFolder is iterable'''
***************
*** 145,152 ****
# we return an instance of *our* message class, not the
# raw rfc822 message
! msg = IMAPMessage(self.uid, self.name, key)
msg.setPayload(messageText)
return msg
!
def Train(self, classifier, isSpam):
'''Train folder as spam/ham'''
--- 153,160 ----
# we return an instance of *our* message class, not the
# raw rfc822 message
! msg = IMAPMessage(self, key)
msg.setPayload(messageText)
return msg
!
def Train(self, classifier, isSpam):
'''Train folder as spam/ham'''
***************
*** 164,184 ****
msg.RememberTrained(isSpam)
! def FilterMessage(self, msg):
if msg.GetClassification() == options.header_ham_string:
# we leave ham alone
pass
elif msg.GetClassification() == options.header_spam_string:
! msg.MoveTo(options.imap_spam_folder)
else:
! msg.MoveTo(options.imap_unsure_folder)
!
! def Filter(self, classifier):
! for msg in self:
! (prob, clues) = classifier.spamprob(msg.asTokens(), evidence=True)
! # add headers and remember classification
! msg.addSBHeaders(prob, clues)
! self.FilterMessage(msg)
! msg.Save()
class IMAPFilter(object):
--- 172,190 ----
msg.RememberTrained(isSpam)
! def Filter(self, classifier, spamfolder, unsurefolder):
! for msg in self:
! (prob, clues) = classifier.spamprob(msg.asTokens(), evidence=True)
! # add headers and remember classification
! msg.addSBHeaders(prob, clues)
!
if msg.GetClassification() == options.header_ham_string:
# we leave ham alone
pass
elif msg.GetClassification() == options.header_spam_string:
! msg.MoveTo(spamfolder)
else:
! msg.MoveTo(unsurefolder)
+ msg.Save()
class IMAPFilter(object):
***************
*** 187,203 ****
imap = imaplib.IMAP4(options.imap_server, options.imap_port)
! if options.verbose:
! print "Loading database...",
filename = options.pop3proxy_persistent_storage_file
filename = os.path.expanduser(filename)
if options.pop3proxy_persistent_use_database:
self.classifier = storage.DBDictClassifier(filename)
else:
self.classifier = storage.PickledClassifier(filename)
if options.verbose:
print "Done."
def Login(self):
- '''Log in to the IMAP server'''
lgn = imap.login(options.imap_username, options.imap_password)
--- 193,214 ----
imap = imaplib.IMAP4(options.imap_server, options.imap_port)
! self.spam_folder = IMAPFolder(options.imap_spam_folder)
! self.unsure_folder = IMAPFolder(options.imap_unsure_folder)
!
filename = options.pop3proxy_persistent_storage_file
filename = os.path.expanduser(filename)
+
+ if options.verbose:
+ print "Loading database %s..." % (filename),
+
if options.pop3proxy_persistent_use_database:
self.classifier = storage.DBDictClassifier(filename)
else:
self.classifier = storage.PickledClassifier(filename)
+
if options.verbose:
print "Done."
def Login(self):
lgn = imap.login(options.imap_username, options.imap_password)
***************
*** 205,208 ****
--- 216,220 ----
if options.verbose:
t = time.time()
+
if options.imap_ham_train_folders != "":
ham_training_folders = options.imap_ham_train_folders.split()
***************
*** 210,213 ****
--- 222,226 ----
folder = IMAPFolder(fol)
folder.Train(self.classifier, False)
+
if options.imap_spam_train_folders != "":
spam_training_folders = options.imap_spam_train_folders.split(' ' )
***************
*** 215,219 ****
--- 228,234 ----
folder = IMAPFolder(fol)
folder.Train(self.classifier, True)
+
self.classifier.store()
+
if options.verbose:
print "Training took", time.time() - t, "seconds."
***************
*** 222,237 ****
if options.verbose:
t = time.time()
for filter_folder in options.imap_filter_folders.split():
folder = IMAPFolder(filter_folder, False)
! folder.Filter(self.classifier)
if options.verbose:
print "Filtering took", time.time() - t, "seconds."
def Logout(self):
! '''Log out of the IMAP server'''
if options.imap_expunge:
imap.expunge()
imap.logout()
if __name__ == '__main__':
options.verbose = True
--- 237,255 ----
if options.verbose:
t = time.time()
+
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.verbose:
print "Filtering took", time.time() - t, "seconds."
def Logout(self):
! # sign off
if options.imap_expunge:
imap.expunge()
imap.logout()
+
if __name__ == '__main__':
options.verbose = True
***************
*** 239,243 ****
# imap_filter.imap.debug = 10
imap_filter.Login()
! #imap_filter.Train()
imap_filter.Filter()
imap_filter.Logout()
--- 257,261 ----
# imap_filter.imap.debug = 10
imap_filter.Login()
! imap_filter.Train()
imap_filter.Filter()
imap_filter.Logout()
More information about the Spambayes-checkins
mailing list