(self, folder, learn_type, move_to, origpastuids)
return sa_learning
Learn the spams (and if requested deleted or move them).

        Args:
            folder (str): The IMAP folder.
            leart_type (str): ```spam``` to learn spam, ```ham``` to learn
                nonspam.
            move_to (str): If not ```None```, the imap folder where the emails
                will be moved.
            origpastuids (list(int)): ``uids`` to not process.
        Returns:
            Sa_Learn:
                It contains the information about the result of the process.

            It will call ``spamc`` to learn the emails.

        Raises:
            isbg.ISBGError: if learn_type is unknown.


        TODO:
            Add suport to ``learn_type=forget``.
Learn the spams (and if requested deleted or move them).
Learn the spams ( and if requested deleted or move them ) .
def learn(self, folder, learn_type, move_to, origpastuids):
        """Learn the spams (and if requested deleted or move them).

        Args:
            folder (str): The IMAP folder.
            leart_type (str): ```spam``` to learn spam, ```ham``` to learn
                nonspam.
            move_to (str): If not ```None```, the imap folder where the emails
                will be moved.
            origpastuids (list(int)): ``uids`` to not process.
        Returns:
            Sa_Learn:
                It contains the information about the result of the process.

            It will call ``spamc`` to learn the emails.

        Raises:
            isbg.ISBGError: if learn_type is unknown.


        TODO:
            Add suport to ``learn_type=forget``.

        """
        sa_learning = Sa_Learn()

        # Sanity checks:
        if learn_type not in ['spam', 'ham']:
            raise isbg.ISBGError(-1, message="Unknown learn_type")
        if self.imap is None:
            raise isbg.ISBGError(-1, message="Imap is required")

        self.logger.debug(__(
            "Teach {} to SA from: {}".format(learn_type, folder)))

        self.imap.select(folder)
        if self.learnunflagged:
            _, uids = self.imap.uid("SEARCH", None, "UNFLAGGED")
        elif self.learnflagged:
            _, uids = self.imap.uid("SEARCH", None, "(FLAGGED)")
        else:
            _, uids = self.imap.uid("SEARCH", None, "ALL")

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

        sa_learning.tolearn = len(uids)

        for uid in uids:
            mail = imaputils.get_message(self.imap, uid, logger=self.logger)

            # Unwrap spamassassin reports
            unwrapped = sa_unwrap.unwrap(mail)
            if unwrapped is not None:
                self.logger.debug(__("{} Unwrapped: {}".format(
                    uid, utils.shorten(imaputils.mail_content(
                        unwrapped[0]), 140))))

            if unwrapped is not None and unwrapped:  # len(unwrapped)>0
                mail = unwrapped[0]

            if self.dryrun:
                self.logger.warning("Skipped learning due to dryrun!")
                continue
            else:
                code, code_orig = learn_mail(mail, learn_type)

            if code == -9999:  # error processing email, try next.
                self.logger.exception(__(
                    'spamc error for mail {}'.format(uid)))
                self.logger.debug(repr(imaputils.mail_content(mail)))
                continue

            if code in [69, 74]:
                raise isbg.ISBGError(
                    isbg.__exitcodes__['flags'],
                    "spamassassin is misconfigured (use --allow-tell)")

            if code == 5:  # learned.
                sa_learning.learned += 1
                self.logger.debug(__(
                    "Learned {} (spamc return code {})".format(uid,
                                                               code_orig)))

            elif code == 6:  # already learned.
                self.logger.debug(__(
                    "Already learned {} (spamc return code {})".format(
                        uid, code_orig)))

            elif code == 98:  # too big.
                self.logger.warning(__(
                    "{} is too big (spamc return code {})".format(
                        uid, code_orig)))

            else:
                raise isbg.ISBGError(-1, ("{}: Unknown return code {} from " +
                                          "spamc").format(uid, code_orig))

            sa_learning.uids.append(int(uid))

            if not self.dryrun:
                if self.learnthendestroy:
                    if self.gmail:
                        self.imap.uid("COPY", uid, "[Gmail]/Trash")
                    else:
                        self.imap.uid("STORE", uid, self.spamflagscmd,
                                      "(\\Deleted)")
                elif move_to is not None:
                    self.imap.uid("COPY", uid, move_to)
                elif self.learnthenflag:
                    self.imap.uid("STORE", uid, self.spamflagscmd,
                                  "(\\Flagged)")

        return sa_learning
def learn ( self , folder , learn_type , move_to , origpastuids ) : sa_learning = Sa_Learn ( ) # Sanity checks: if learn_type not in [ 'spam' , 'ham' ] : raise isbg . ISBGError ( - 1 , message = "Unknown learn_type" ) if self . imap is None : raise isbg . ISBGError ( - 1 , message = "Imap is required" ) self . logger . debug ( __ ( "Teach {} to SA from: {}" . format ( learn_type , folder ) ) ) self . imap . select ( folder ) if self . learnunflagged : _ , uids = self . imap . uid ( "SEARCH" , None , "UNFLAGGED" ) elif self . learnflagged : _ , uids = self . imap . uid ( "SEARCH" , None , "(FLAGGED)" ) else : _ , uids = self . imap . uid ( "SEARCH" , None , "ALL" ) uids , sa_learning . newpastuids = SpamAssassin . get_formated_uids ( uids , origpastuids , self . partialrun ) sa_learning . tolearn = len ( uids ) for uid in uids : mail = imaputils . get_message ( self . imap , uid , logger = self . logger ) # Unwrap spamassassin reports unwrapped = sa_unwrap . unwrap ( mail ) if unwrapped is not None : self . logger . debug ( __ ( "{} Unwrapped: {}" . format ( uid , utils . shorten ( imaputils . mail_content ( unwrapped [ 0 ] ) , 140 ) ) ) ) if unwrapped is not None and unwrapped : # len(unwrapped)>0 mail = unwrapped [ 0 ] if self . dryrun : self . logger . warning ( "Skipped learning due to dryrun!" ) continue else : code , code_orig = learn_mail ( mail , learn_type ) if code == - 9999 : # error processing email, try next. self . logger . exception ( __ ( 'spamc error for mail {}' . format ( uid ) ) ) self . logger . debug ( repr ( imaputils . mail_content ( mail ) ) ) continue if code in [ 69 , 74 ] : raise isbg . ISBGError ( isbg . __exitcodes__ [ 'flags' ] , "spamassassin is misconfigured (use --allow-tell)" ) if code == 5 : # learned. sa_learning . learned += 1 self . logger . debug ( __ ( "Learned {} (spamc return code {})" . format ( uid , code_orig ) ) ) elif code == 6 : # already learned. self . logger . debug ( __ ( "Already learned {} (spamc return code {})" . format ( uid , code_orig ) ) ) elif code == 98 : # too big. self . logger . warning ( __ ( "{} is too big (spamc return code {})" . format ( uid , code_orig ) ) ) else : raise isbg . ISBGError ( - 1 , ( "{}: Unknown return code {} from " + "spamc" ) . format ( uid , code_orig ) ) sa_learning . uids . append ( int ( uid ) ) if not self . dryrun : if self . learnthendestroy : if self . gmail : self . imap . uid ( "COPY" , uid , "[Gmail]/Trash" ) else : self . imap . uid ( "STORE" , uid , self . spamflagscmd , "(\\Deleted)" ) elif move_to is not None : self . imap . uid ( "COPY" , uid , move_to ) elif self . learnthenflag : self . imap . uid ( "STORE" , uid , self . spamflagscmd , "(\\Flagged)" ) return sa_learning