def do_spamassassin(self): """Do the spamassassin procesing. It creates a instance of :py:class:`~isbg.spamproc.SpamAssassin` every time that this method is called. The ``SpamAssasssin`` object would contact to the IMAP server to get the emails and to ``SpamAssassin`` command line to process them. """ sa = spamproc.SpamAssassin.create_from_isbg(self) # SpamAssassin training: Learn spam s_learned = spamproc.Sa_Learn() if self.imapsets.learnspambox: uidvalidity = self.imap.get_uidvalidity(self.imapsets.learnspambox) origpastuids = self.pastuid_read(uidvalidity, 'spam') s_learned = sa.learn(self.imapsets.learnspambox, 'spam', None, origpastuids) self.pastuid_write(uidvalidity, s_learned.newpastuids, s_learned.uids, 'spam') # SpamAssassin training: Learn ham h_learned = spamproc.Sa_Learn() if self.imapsets.learnhambox: uidvalidity = self.imap.get_uidvalidity(self.imapsets.learnhambox) origpastuids = self.pastuid_read(uidvalidity, 'ham') h_learned = sa.learn(self.imapsets.learnhambox, 'ham', self.movehamto, origpastuids) self.pastuid_write(uidvalidity, h_learned.newpastuids, h_learned.uids, 'ham') if not self.teachonly: # check spaminbox exists by examining it self.imap.select(self.imapsets.spaminbox, 1) uidvalidity = self.imap.get_uidvalidity(self.imapsets.inbox) origpastuids = self.pastuid_read(uidvalidity) proc = sa.process_inbox(origpastuids) self.pastuid_write(uidvalidity, proc.newpastuids, proc.uids) else: proc = None if self.nostats is False: if self.imapsets.learnspambox is not None: self.logger.info( __("{}/{} spams learned".format(s_learned.learned, s_learned.tolearn))) if self.imapsets.learnhambox: self.logger.info( __("{}/{} hams learned".format(h_learned.learned, h_learned.tolearn))) if proc is not None: self.logger.info( __("{} spams found in {} messages".format( proc.numspam, proc.nummsg))) self.logger.info( __("{}/{} was automatically deleted".format( proc.spamdeleted, proc.numspam))) return proc
def test_sa_learn(self): """Test for sa_learn.""" learn = spamproc.Sa_Learn() assert learn.tolearn == 0 assert learn.learned == 0 assert len(learn.uids) == 0 assert len(learn.newpastuids) == 0