Ejemplo n.º 1
0
    def apply_labels_to(self, imap_ids, labels):
        """
           apply one labels to x emails
        """
        # go to All Mail folder
        LOG.debug("Applying labels %s" % (labels))
        
        the_timer = gmvault_utils.Timer()
        the_timer.start()

        #utf7 the labels as they should be
        labels = [ utf7_encode(label) for label in labels ]

        labels_str = self._build_labels_str(labels) # create labels str
    
        if labels_str:  
            #has labels so update email  
            the_timer.start()
            #LOG.debug("Before to store labels %s" % (labels_str))
            id_list = ",".join(map(str, imap_ids))
            #+X-GM-LABELS.SILENT to have not returned data
            ret_code, data = self.server._imap.uid('STORE', id_list, '+X-GM-LABELS.SILENT', labels_str) #pylint: disable=W0212

            #ret_code, data = self.server._imap.uid('COPY', id_list, labels[0])
            LOG.debug("After storing labels %s. Operation time = %s s.\nret = %s\ndata=%s" \
                      % (labels_str, the_timer.elapsed_ms(),ret_code, data))

            # check if it is ok otherwise exception
            if ret_code != 'OK':
                # Try again to code the error message (do not use .SILENT)
                ret_code, data = self.server._imap.uid('STORE', id_list, '+X-GM-LABELS', labels_str) #pylint: disable=W0212
                if ret_code != 'OK':
                    raise PushEmailError("Cannot add Labels %s to emails with uids %d. Error:%s" % (labels_str, imap_ids, data))
            else:
                LOG.debug("Stored Labels %s for gm_ids %s" % (labels_str, imap_ids))
    def push_data(self, a_folder, a_body, a_flags, a_internal_time):
        """
           Push the data
        """
        # protection against myself
        if self.login == '*****@*****.**':
            raise Exception("Cannot push to this account")

        the_timer = gmvault_utils.Timer()
        the_timer.start()
        LOG.debug("Before to Append email contents")
        #import sys  #to print the msg in stdout
        #import codecs
        #sys.stdout = codecs.getwriter('utf-8')(sys.__stdout__)
        #msg = "a_folder = %s, a_flags = %s" % (a_folder.encode('utf-8'), a_flags)
        #msg = "a_folder = %s" % (a_folder.encode('utf-8'))
        #msg = msg.encode('utf-8')
        #print(msg)
        res = None
        try:
            #a_body = self._clean_email_body(a_body)
            res = self.server.append(a_folder, a_body, a_flags,
                                     a_internal_time)
        except imaplib.IMAP4.abort, err:
            # handle issue when there are invalid characters (This is do to the presence of null characters)
            if str(err).find("APPEND => Invalid character in literal") >= 0:
                LOG.critical(
                    "Invalid character detected. Try to clean the email and reconnect."
                )
                a_body = self._clean_email_body(a_body)
                self.reconnect()
                res = self.server.append(a_folder, a_body, a_flags,
                                         a_internal_time)
Ejemplo n.º 3
0
    def push_email(self, a_body, a_flags, a_internal_time, a_labels):
        """
           Push a complete email body 
        """
        #protection against myself
        if self.login == '*****@*****.**':
            raise Exception("Cannot push to this account")
    
        the_t = gmvault_utils.Timer()
        the_t.start()
        LOG.debug("Before to Append email contents")
        #res = self.server.append(self.current_folder, a_body, a_flags, a_internal_time)
        res = self.server.append(u'[Google Mail]/All Mail', a_body, a_flags, a_internal_time)
    
        LOG.debug("Appended data with flags %s and internal time %s. Operation time = %s.\nres = %s\n" \
                  % (a_flags, a_internal_time, the_t.elapsed_ms(), res))
        
        # check res otherwise Exception
        if '(Success)' not in res:
            raise PushEmailError("GIMAPFetcher cannot restore email in %s account." %(self.login))
        
        match = GIMAPFetcher.APPENDUID_RE.match(res)
        if match:
            result_uid = int(match.group(1))
            LOG.debug("result_uid = %s" %(result_uid))
        else:
            # do not quarantine it because it seems to be done by Google Mail to forbid data uploading.
            raise PushEmailError("No email id returned by IMAP APPEND command. Quarantine this email.", quarantined = True)
        
        labels_str = self._build_labels_str(a_labels)
        
        if labels_str:  
            #has labels so update email  
            the_t.start()
            LOG.debug("Before to store labels %s" % (labels_str))
            self.server.select_folder(u'[Google Mail]/All Mail', readonly = self.readonly_folder) # go to current folder
            LOG.debug("Changing folders. elapsed %s s\n" % (the_t.elapsed_ms()))
            the_t.start()
            ret_code, data = self.server._imap.uid('STORE', result_uid, '+X-GM-LABELS', labels_str) #pylint: disable=W0212
            #ret_code = self.server._store('+X-GM-LABELS', [result_uid],labels_str)
            LOG.debug("After storing labels %s. Operation time = %s s.\nret = %s\ndata=%s" \
                      % (labels_str, the_t.elapsed_ms(),ret_code, data))
            
            LOG.debug("Stored Labels %s in gm_id %s" % (labels_str, result_uid))

            self.server.select_folder(u'[Google Mail]/Drafts', readonly = self.readonly_folder) # go to current folder
        
            # check if it is ok otherwise exception
            if ret_code != 'OK':
                raise PushEmailError("Cannot add Labels %s to email with uid %d. Error:%s" % (labels_str, result_uid, data))
        
        return result_uid
    def apply_labels_to(self, imap_ids, labels):
        """
           apply one labels to x emails
        """
        # go to All Mail folder
        LOG.debug("Applying labels %s" % (labels))

        the_timer = gmvault_utils.Timer()
        the_timer.start()

        #utf7 the labels as they should be
        labels = [utf7_encode(label) for label in labels]

        labels_str = self._build_labels_str(labels)  # create labels str

        if labels_str:
            #has labels so update email
            the_timer.start()
            LOG.debug("Before to store labels %s" % (labels_str))
            id_list = ",".join(map(str, imap_ids))
            #+X-GM-LABELS.SILENT to have not returned data
            try:
                ret_code, data = self.server._imap.uid('STORE', id_list,
                                                       '+X-GM-LABELS.SILENT',
                                                       labels_str)  #pylint: disable=W0212
            except imaplib.IMAP4.error, original_err:
                LOG.info("Error in apply_labels_to. See exception traceback")
                LOG.debug(gmvault_utils.get_exception_traceback())
                # try to add labels to each individual ids
                faulty_ids = []
                for the_id in imap_ids:
                    try:
                        ret_code, data = self.server._imap.uid(
                            'STORE', the_id, '+X-GM-LABELS.SILENT', labels_str)  #pylint: disable=W0212
                    except imaplib.IMAP4.error, store_err:
                        LOG.debug(
                            "Error when trying to apply labels %s to emails with imap_id %s. Error:%s"
                            % (labels_str, the_id, store_err))
                        faulty_ids.append(the_id)

                #raise an error to ignore faulty emails
                raise LabelError(
                    "Cannot add Labels %s to emails with uids %s. Error:%s" %
                    (labels_str, faulty_ids, original_err),
                    ignore=True)
Ejemplo n.º 5
0
    def deprecated_push_email(self, a_body, a_flags, a_internal_time, a_labels):
        """
           Push a complete email body 
        """
        #protection against myself
        raise Exception("Cannot push to this account")
    
        the_t = gmvault_utils.Timer()
        the_t.start()
        LOG.debug("Before to Append email contents")


        try:
           res = self.server.append(u'[Google Mail]/All Mail', a_body, a_flags, a_internal_time)
        except imaplib.IMAP4.abort, err:
           # handle issue when there are invalid characters (This is do to the presence of null characters)
           if str(err).find("APPEND => Invalid character in literal") >= 0:
              a_body = self._clean_email_body(a_body)
              res    = self.server.append(u'[Google Mail]/All Mail', a_body, a_flags, a_internal_time)
Ejemplo n.º 6
0
    def push_data(self, a_folder, a_body, a_flags, a_internal_time):
        """
           Push the data
        """
        # protection against myself
        if self.login == '*****@*****.**':
            raise Exception("Cannot push to this account")

        the_timer = gmvault_utils.Timer()
        the_timer.start()
        LOG.debug("Before to Append email contents")
        #import sys  #to print the msg in stdout
        #import codecs
        #sys.stdout = codecs.getwriter('utf-8')(sys.__stdout__)
        #msg = "a_folder = %s, a_flags = %s" % (a_folder.encode('utf-8'), a_flags)
        #msg = "a_folder = %s" % (a_folder.encode('utf-8'))
        #msg = msg.encode('utf-8')
        #print(msg)
        res = self.server.append(a_folder, a_body, a_flags, a_internal_time)

        LOG.debug("Appended data with flags %s and internal time %s. Operation time = %s.\nres = %s\n" \
                  % (a_flags, a_internal_time, the_timer.elapsed_ms(), res))

        # check res otherwise Exception
        if '(Success)' not in res:
            raise PushEmailError(
                "GIMAPFetcher cannot restore email in %s account." %
                (self.login))

        match = GIMAPFetcher.APPENDUID_RE.match(res)
        if match:
            result_uid = int(match.group(1))
            LOG.debug("result_uid = %s" % (result_uid))
        else:
            # do not quarantine it because it seems to be done by Google Mail to forbid data uploading.
            raise PushEmailError(
                "No email id returned by IMAP APPEND command. Quarantine this email.",
                quarantined=True)

        return result_uid
    def export_ids(self, kind, ids, default_folder, use_labels):
        """ export organised by ids """
        exported_labels = "default labels"
        if self.labels:
            exported_labels = "labels " + self.printable_label_list(self.labels)
        LOG.critical("Start %s export for %s." % (kind, exported_labels))

        timer = gmvault_utils.Timer()
        timer.start()
        done = 0

        for a_id in ids:
            meta, msg = self.storer.unbury_email(a_id)

            folders = [default_folder]
            if use_labels:
                add_labels = meta[gmvault_db.GmailStorer.LABELS_K]
                if not add_labels:
                    add_labels = [GMVaultExporter.ARCHIVED_FOLDER]
                folders.extend(add_labels)
            folders = [re.sub(r'^\\', '', f) for f in folders]
            folders = [f for f in folders if self.want_label(f)]

            LOG.debug("Processing id %s in labels %s." % \
                (a_id, self.printable_label_list(folders)))
            for folder in folders:
                self.mailbox.add(msg, folder, meta[gmvault_db.GmailStorer.FLAGS_K])

            done += 1
            left = len(ids) - done
            if done % self.PROGRESS_INTERVAL == 0 and left > 0:
                elapsed = timer.elapsed()
                LOG.critical("== Processed %d %s in %s, %d left (time estimate %s). ==\n" % \
                    (done, kind, timer.seconds_to_human_time(elapsed), \
                     left, timer.estimate_time_left(done, elapsed, left)))

        LOG.critical("Export completed in %s." % (timer.elapsed_human_time(),))