(self, origpastuids)
return sa_proc
Run spamassassin in the folder for spam.
Run spamassassin in the folder for spam.
Run spamassassin in the folder for spam .
def process_inbox(self, origpastuids):
        """Run spamassassin in the folder for spam."""
        sa_proc = Sa_Process()

        spamlist = []
        spamdeletelist = []

        # select inbox
        self.imap.select(self.imapsets.inbox, 1)

        # get the uids of all mails with a size less then the maxsize
        _, uids = self.imap.uid("SEARCH", None, "SMALLER", str(self.maxsize))

        uids, sa_proc.newpastuids = SpamAssassin.get_formated_uids(
            uids, origpastuids, self.partialrun)

        self.logger.debug(__('Got {} mails to check'.format(len(uids))))

        if self.dryrun:
            processednum = 0
            fakespammax = 1
            processmax = 5

        # Main loop that iterates over each new uid we haven't seen before
        for uid in uids:
            # Retrieve the entire message
            mail = imaputils.get_message(self.imap, uid, sa_proc.uids,
                                         logger=self.logger)

            # Unwrap spamassassin reports
            unwrapped = sa_unwrap.unwrap(mail)
            if unwrapped is not None and unwrapped:  # len(unwrapped) > 0
                mail = unwrapped[0]

            # Feed it to SpamAssassin in test mode
            if self.dryrun:
                if processednum > processmax:
                    break
                if processednum < fakespammax:
                    self.logger.info("Faking spam mail")
                    score = "10/10"
                    code = 1
                else:
                    self.logger.info("Faking ham mail")
                    score = "0/10"
                    code = 0
                processednum = processednum + 1
                spamassassin_result = None  # since dryrun doesn't run
                                            # test_mail()
            else:
                score, code, spamassassin_result = test_mail(mail, cmd=self.cmd_test)
                if score == "-9999":
                    self.logger.exception(__(
                        '{} error for mail {}'.format(self.cmd_test, uid)))
                    self.logger.debug(repr(mail))
                    uids.remove(uid)
                    continue

            if score == "0/0\n":
                raise isbg.ISBGError(isbg.__exitcodes__['spamc'],
                                     "spamc -> spamd error - aborting")

            self.logger.debug(__(
                "Score for uid {}: {}".format(uid, score.strip())))

            if code != 0:
                # Message is spam, delete it or move it to spaminbox
                # (optionally with report)
                if not self._process_spam(uid, score, mail, spamdeletelist, code, spamassassin_result):
                    continue
                spamlist.append(uid)

        sa_proc.nummsg = len(uids)
        sa_proc.spamdeleted = len(spamdeletelist)
        sa_proc.numspam = len(spamlist) + sa_proc.spamdeleted

        # If we found any spams, now go and mark the original messages
        if sa_proc.numspam or sa_proc.spamdeleted:
            if self.dryrun:
                self.logger.info('Skipping labelling/expunging of mails ' +
                                 ' because of --dryrun')
            else:
                self.imap.select(self.imapsets.inbox)
                # Only set message flags if there are any
                if self.spamflags:  # len(self.smpamflgs) > 0
                    for uid in spamlist:
                        self.imap.uid("STORE", uid, self.spamflagscmd,
                                      imaputils.imapflags(self.spamflags))
                        sa_proc.newpastuids.append(uid)
                # If its gmail, and --delete was passed, we actually copy!
                if self.delete and self.gmail:
                    for uid in spamlist:
                        self.imap.uid("COPY", uid, "[Gmail]/Trash")
                # Set deleted flag for spam with high score
                for uid in spamdeletelist:
                    if self.gmail is True:
                        self.imap.uid("COPY", uid, "[Gmail]/Trash")
                    else:
                        self.imap.uid("STORE", uid, self.spamflagscmd,
                                      "(\\Deleted)")
                if self.expunge:
                    self.imap.expunge()

        return sa_proc
def process_inbox ( self , origpastuids ) : sa_proc = Sa_Process ( ) spamlist = [ ] spamdeletelist = [ ] # select inbox self . imap . select ( self . imapsets . inbox , 1 ) # get the uids of all mails with a size less then the maxsize _ , uids = self . imap . uid ( "SEARCH" , None , "SMALLER" , str ( self . maxsize ) ) uids , sa_proc . newpastuids = SpamAssassin . get_formated_uids ( uids , origpastuids , self . partialrun ) self . logger . debug ( __ ( 'Got {} mails to check' . format ( len ( uids ) ) ) ) if self . dryrun : processednum = 0 fakespammax = 1 processmax = 5 # Main loop that iterates over each new uid we haven't seen before for uid in uids : # Retrieve the entire message mail = imaputils . get_message ( self . imap , uid , sa_proc . uids , logger = self . logger ) # Unwrap spamassassin reports unwrapped = sa_unwrap . unwrap ( mail ) if unwrapped is not None and unwrapped : # len(unwrapped) > 0 mail = unwrapped [ 0 ] # Feed it to SpamAssassin in test mode if self . dryrun : if processednum > processmax : break if processednum < fakespammax : self . logger . info ( "Faking spam mail" ) score = "10/10" code = 1 else : self . logger . info ( "Faking ham mail" ) score = "0/10" code = 0 processednum = processednum + 1 spamassassin_result = None # since dryrun doesn't run # test_mail() else : score , code , spamassassin_result = test_mail ( mail , cmd = self . cmd_test ) if score == "-9999" : self . logger . exception ( __ ( '{} error for mail {}' . format ( self . cmd_test , uid ) ) ) self . logger . debug ( repr ( mail ) ) uids . remove ( uid ) continue if score == "0/0\n" : raise isbg . ISBGError ( isbg . __exitcodes__ [ 'spamc' ] , "spamc -> spamd error - aborting" ) self . logger . debug ( __ ( "Score for uid {}: {}" . format ( uid , score . strip ( ) ) ) ) if code != 0 : # Message is spam, delete it or move it to spaminbox # (optionally with report) if not self . _process_spam ( uid , score , mail , spamdeletelist , code , spamassassin_result ) : continue spamlist . append ( uid ) sa_proc . nummsg = len ( uids ) sa_proc . spamdeleted = len ( spamdeletelist ) sa_proc . numspam = len ( spamlist ) + sa_proc . spamdeleted # If we found any spams, now go and mark the original messages if sa_proc . numspam or sa_proc . spamdeleted : if self . dryrun : self . logger . info ( 'Skipping labelling/expunging of mails ' + ' because of --dryrun' ) else : self . imap . select ( self . imapsets . inbox ) # Only set message flags if there are any if self . spamflags : # len(self.smpamflgs) > 0 for uid in spamlist : self . imap . uid ( "STORE" , uid , self . spamflagscmd , imaputils . imapflags ( self . spamflags ) ) sa_proc . newpastuids . append ( uid ) # If its gmail, and --delete was passed, we actually copy! if self . delete and self . gmail : for uid in spamlist : self . imap . uid ( "COPY" , uid , "[Gmail]/Trash" ) # Set deleted flag for spam with high score for uid in spamdeletelist : if self . gmail is True : self . imap . uid ( "COPY" , uid , "[Gmail]/Trash" ) else : self . imap . uid ( "STORE" , uid , self . spamflagscmd , "(\\Deleted)" ) if self . expunge : self . imap . expunge ( ) return sa_proc