+ def get_uidnext(self):
+ """
+ Get the state (uidnext) for each inbox used in the configuration.
+ It gives the uid of the next message the will be added in the mbox.
+ We will only care about messages with a uid lower than this uidnext,
+ to avoid race conditions with a message arriving while we are in the
+ middle of rules processing.
+ """
+ self.logger.info('Getting inbox state...')
+ mboxes = [self.config["inbox"]] + [rule.inbox for rule in self.rules]
+ for m in mboxes:
+ if m is None:
+ continue
+ if self.uidnext.get(m, None) is not None:
+ continue
+ self.imap.select(m)
+ typ, dat = self.imap.status(m, "(UIDNEXT)")
+ if typ != 'OK':
+ raise ValueError("cannot get UIDNEXT: %s", typ)
+ match = re.match("[^ ]* \(UIDNEXT ([0-9]+)\)", dat[0])
+ if match is None:
+ raise ValueError("cannot match UIDNEXT: %s", typ)
+ self.uidnext[m] = int(match.groups()[0])
+ self.logger.info('Done: %r', self.uidnext)
+