mirror of
https://github.com/craigerl/aprsd.git
synced 2025-03-08 12:28:55 -05:00
EmailThread was exiting because of IMAP timeout, added exceptions for this
This commit is contained in:
parent
db2b537317
commit
47135c6086
119
aprsd/email.py
119
aprsd/email.py
@ -34,8 +34,8 @@ def _imap_connect():
|
|||||||
ssl=use_ssl,
|
ssl=use_ssl,
|
||||||
timeout=30,
|
timeout=30,
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
LOG.error("Failed to connect IMAP server")
|
LOG.error("Failed to connect IMAP server", e)
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -421,7 +421,7 @@ class APRSDEmailThread(threads.APRSDThread):
|
|||||||
try:
|
try:
|
||||||
server = _imap_connect()
|
server = _imap_connect()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception("Failed to get IMAP server Can't check email.", e)
|
LOG.exception("IMAP failed to connect.", e)
|
||||||
|
|
||||||
LOG.debug("Tried _imap_connect")
|
LOG.debug("Tried _imap_connect")
|
||||||
|
|
||||||
@ -429,64 +429,75 @@ class APRSDEmailThread(threads.APRSDThread):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
LOG.debug("Try Server.search since today.")
|
LOG.debug("Try Server.search since today.")
|
||||||
messages = server.search(["SINCE", today])
|
try:
|
||||||
|
messages = server.search(["SINCE", today])
|
||||||
|
except Exception as e:
|
||||||
|
LOG.exception("IMAP failed to search for messages since today.", e)
|
||||||
|
continue
|
||||||
LOG.debug("{} messages received today".format(len(messages)))
|
LOG.debug("{} messages received today".format(len(messages)))
|
||||||
|
|
||||||
LOG.debug("Try Server.fetch.")
|
LOG.debug("Try Server.fetch.")
|
||||||
for msgid, data in server.fetch(messages, ["ENVELOPE"]).items():
|
try:
|
||||||
envelope = data[b"ENVELOPE"]
|
for msgid, data in server.fetch(messages, ["ENVELOPE"]).items():
|
||||||
LOG.debug(
|
envelope = data[b"ENVELOPE"]
|
||||||
'ID:%d "%s" (%s)'
|
LOG.debug(
|
||||||
% (msgid, envelope.subject.decode(), envelope.date),
|
'ID:%d "%s" (%s)'
|
||||||
)
|
% (msgid, envelope.subject.decode(), envelope.date),
|
||||||
f = re.search(
|
|
||||||
r"'([[A-a][0-9]_-]+@[[A-a][0-9]_-\.]+)",
|
|
||||||
str(envelope.from_[0]),
|
|
||||||
)
|
|
||||||
if f is not None:
|
|
||||||
from_addr = f.group(1)
|
|
||||||
else:
|
|
||||||
from_addr = "noaddr"
|
|
||||||
|
|
||||||
# LOG.debug("Message flags/tags: " + str(server.get_flags(msgid)[msgid]))
|
|
||||||
# if "APRS" not in server.get_flags(msgid)[msgid]:
|
|
||||||
# in python3, imap tags are unicode. in py2 they're strings. so .decode them to handle both
|
|
||||||
taglist = [
|
|
||||||
x.decode(errors="ignore")
|
|
||||||
for x in server.get_flags(msgid)[msgid]
|
|
||||||
]
|
|
||||||
if "APRS" not in taglist:
|
|
||||||
# if msg not flagged as sent via aprs
|
|
||||||
LOG.debug("Try single fetch.")
|
|
||||||
server.fetch([msgid], ["RFC822"])
|
|
||||||
LOG.debug("Did single fetch.")
|
|
||||||
(body, from_addr) = parse_email(msgid, data, server)
|
|
||||||
# unset seen flag, will stay bold in email client
|
|
||||||
LOG.debug("Try remove flags.")
|
|
||||||
server.remove_flags(msgid, [imapclient.SEEN])
|
|
||||||
LOG.debug("Did remove flags.")
|
|
||||||
|
|
||||||
if from_addr in shortcuts_inverted:
|
|
||||||
# reverse lookup of a shortcut
|
|
||||||
from_addr = shortcuts_inverted[from_addr]
|
|
||||||
|
|
||||||
reply = "-" + from_addr + " " + body.decode(errors="ignore")
|
|
||||||
msg = messaging.TextMessage(
|
|
||||||
self.config["aprs"]["login"],
|
|
||||||
self.config["ham"]["callsign"],
|
|
||||||
reply,
|
|
||||||
)
|
)
|
||||||
self.msg_queues["tx"].put(msg)
|
f = re.search(
|
||||||
# flag message as sent via aprs
|
r"'([[A-a][0-9]_-]+@[[A-a][0-9]_-\.]+)",
|
||||||
server.add_flags(msgid, ["APRS"])
|
str(envelope.from_[0]),
|
||||||
# unset seen flag, will stay bold in email client
|
)
|
||||||
server.remove_flags(msgid, [imapclient.SEEN])
|
if f is not None:
|
||||||
# check email more often since we just received an email
|
from_addr = f.group(1)
|
||||||
check_email_delay = 60
|
else:
|
||||||
|
from_addr = "noaddr"
|
||||||
|
|
||||||
|
# LOG.debug("Message flags/tags: " + str(server.get_flags(msgid)[msgid]))
|
||||||
|
# if "APRS" not in server.get_flags(msgid)[msgid]:
|
||||||
|
# in python3, imap tags are unicode. in py2 they're strings. so .decode them to handle both
|
||||||
|
taglist = [
|
||||||
|
x.decode(errors="ignore")
|
||||||
|
for x in server.get_flags(msgid)[msgid]
|
||||||
|
]
|
||||||
|
if "APRS" not in taglist:
|
||||||
|
# if msg not flagged as sent via aprs
|
||||||
|
LOG.debug("Try single fetch.")
|
||||||
|
server.fetch([msgid], ["RFC822"])
|
||||||
|
LOG.debug("Did single fetch.")
|
||||||
|
(body, from_addr) = parse_email(msgid, data, server)
|
||||||
|
# unset seen flag, will stay bold in email client
|
||||||
|
LOG.debug("Try remove flags.")
|
||||||
|
server.remove_flags(msgid, [imapclient.SEEN])
|
||||||
|
LOG.debug("Did remove flags.")
|
||||||
|
|
||||||
|
if from_addr in shortcuts_inverted:
|
||||||
|
# reverse lookup of a shortcut
|
||||||
|
from_addr = shortcuts_inverted[from_addr]
|
||||||
|
|
||||||
|
reply = "-" + from_addr + " " + body.decode(errors="ignore")
|
||||||
|
msg = messaging.TextMessage(
|
||||||
|
self.config["aprs"]["login"],
|
||||||
|
self.config["ham"]["callsign"],
|
||||||
|
reply,
|
||||||
|
)
|
||||||
|
self.msg_queues["tx"].put(msg)
|
||||||
|
# flag message as sent via aprs
|
||||||
|
server.add_flags(msgid, ["APRS"])
|
||||||
|
# unset seen flag, will stay bold in email client
|
||||||
|
server.remove_flags(msgid, [imapclient.SEEN])
|
||||||
|
# check email more often since we just received an email
|
||||||
|
check_email_delay = 60
|
||||||
|
except Exception as e:
|
||||||
|
LOG.exception("IMAP failed to fetch/flag messages: ", e)
|
||||||
# reset clock
|
# reset clock
|
||||||
LOG.debug("Done looping over Server.fetch, logging out.")
|
LOG.debug("Done looping over Server.fetch, logging out.")
|
||||||
past = datetime.datetime.now()
|
past = datetime.datetime.now()
|
||||||
server.logout()
|
try:
|
||||||
|
server.logout()
|
||||||
|
except Exception as e:
|
||||||
|
LOG.exception("IMAP failed to logout: ", e)
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
# We haven't hit the email delay yet.
|
# We haven't hit the email delay yet.
|
||||||
# LOG.debug("Delta({}) < {}".format(now - past, check_email_delay))
|
# LOG.debug("Delta({}) < {}".format(now - past, check_email_delay))
|
||||||
|
Loading…
Reference in New Issue
Block a user