mailbot: keep mbox open

This commit is contained in:
Egg 2024-10-09 16:29:10 +02:00
parent f4edfba532
commit a0dc703708
2 changed files with 17 additions and 8 deletions

View file

@ -15,18 +15,22 @@ logger = logging.getLogger("main")
class Mailbot(Bot):
mails = None
# returns a list of Report objects
def crawl(self, user):
reports = []
# todo: adjust to actual mailbox
try:
mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user'])
except FileNotFoundError:
logger.error("No mbox file found.")
return reports
for msg in mails:
if get_date_from_header(msg['Date']) > user.get_seen_mail():
if user.get_city().lower() in msg['To'].lower():
if self.mails is None:
try:
self.mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user'])
except FileNotFoundError:
logger.error("No mbox file found.")
self.mails = None
return reports
for msg in self.mails:
if user.get_city().lower() in msg['To'].lower():
if get_date_from_header(msg['Date']) > user.get_seen_mail():
reports.append(make_report(msg, user))
return reports

View file

@ -33,8 +33,12 @@ if __name__ == '__main__':
try:
while True:
sleep(15) # rest 15 seconds between each crawl run
timer_all = time()
user_count = 0
for user in db.active_users:
sleep(1)
user_count += 1
for bot in bots:
timer_crawl = time()
reports = bot.crawl(user)
@ -46,6 +50,7 @@ if __name__ == '__main__':
for bot2 in bots:
bot2.post(user, status)
logger.info("Resent: %d %s %s" % (user.uid, status.author, status.text))
logger.debug("All %d users crawled and msgs resent in %f seconds" % (user_count, time()-timer_all))
except Exception:
logger.error("Shutdown.", exc_info=True)
shutdown()