def _start_push(self): self.server = imap_server.IMAP_server( self.account_config["username"], self.account_config["password"], self.account_config["server"], self.account_config["port"], eval(self.account_config["folders"]), self.sqlite, self.store, self.max_age, self.account_config["headers_only"] == "True", self.account_config["ssl"] == "True", None, ) self.server.login() self.server.sync() self.idler = Idler(self.server.imap4, self.server) self.idler.start()
class Worker: def __init__(self, manager, account_name, config, presync, postsync): self.manager = manager self.config = config self.account_name = account_name self.account_type = self.account_name.split("-")[0] self.lock = threading.Lock() self.presync = presync self.postsync = postsync self.server = None self.terminate = False self.sqlite = Sqlite( os.path.join(self.config.dbroot, self.account_name + ".db"), Queue(), Queue(), self.account_type ) self.store = Store( self.sqlite, os.path.join(self.config.folderroot, self.account_name), self.manager.store_callback(self.account_name), self.manager.bulk_store_callback(self.account_name), self.manager.downloaded_callback(self.account_name), ) if self.account_type == "POP3": self.account_config = self.config.pop3[self.account_name] self.server = pop3_server.POP3_server( self.account_config["username"], self.account_config["password"], self.account_config["server"], self.account_config["port"], self.sqlite, self.store, self.account_config["ssl"], int(self.account_config["max_count"]), int(self.account_config["max_size"]), self.account_config["leave_on_server"], ) elif self.account_type == "IMAP": self.account_config = self.config.imap[self.account_name] self.max_age = ( datetime.date.today() - datetime.timedelta(days=int(self.account_config["max_age"])) ).strftime("%d-%b-%Y") def download_body(self, folder, uid): threading.Thread(target=self._download_body, args=(folder, uid)).start() def _download_body(self, folder, uid): if self.server: self.server.download_body(folder, uid) else: server = imap_server.IMAP_server( self.account_config["username"], self.account_config["password"], self.account_config["server"], self.account_config["port"], eval(self.account_config["folders"]), self.sqlite, self.store, self.max_age, self.account_config["headers_only"] == "True", bool(self.account_config["ssl"]), None, ) server.login() server.download_body(folder, uid) def request_sync(self, thread=True): if self.lock.locked(): logger.info("Requested sync when one already in progress.") else: if not thread: logger.debug("Starting sync in same thread") self._sync() else: logger.debug("Starting sync in seperate thread") threading.Thread(target=self._sync).start() def terminate_sync(self): try: self.idler.stop() except: pass finally: self.server.logout() if self.lock.locked(): self.server.terminate = True else: logger.debug("Requested termination when no sync running.") def _start_push(self): self.server = imap_server.IMAP_server( self.account_config["username"], self.account_config["password"], self.account_config["server"], self.account_config["port"], eval(self.account_config["folders"]), self.sqlite, self.store, self.max_age, self.account_config["headers_only"] == "True", self.account_config["ssl"] == "True", None, ) self.server.login() self.server.sync() self.idler = Idler(self.server.imap4, self.server) self.idler.start() def _sync(self): with self.lock: self.presync(self) if self.server: self.server.sync() else: logger.debug("Worker %s acquired lock, starting sync" % self.account_name) self.server = imap_server.IMAP_server( self.account_config["username"], self.account_config["password"], self.account_config["server"], self.account_config["port"], eval(self.account_config["folders"]), self.sqlite, self.store, self.max_age, self.account_config["headers_only"] == "True", bool(self.account_config["ssl"]), None, ) self.server.login() self.server.sync() self.server.logout() self.server = None self.postsync(self) logger.debug("Worker %s released lock, sync finished" % self.account_name)