mirror of https://github.com/craigerl/aprsd.git
fix check_email_thread to do proper threading, take delay as arg
This commit is contained in:
parent
f175f7e6e3
commit
e171e16854
121
aprsd/main.py
121
aprsd/main.py
|
@ -232,75 +232,74 @@ def resend_email(count, fromcall):
|
||||||
# end resend_email()
|
# end resend_email()
|
||||||
|
|
||||||
|
|
||||||
def check_email_thread():
|
def check_email_thread(check_email_delay):
|
||||||
# print "Email thread disabled."
|
|
||||||
# return
|
|
||||||
|
|
||||||
LOG.debug("Starting Email thread")
|
while True:
|
||||||
# how do we skip first run?
|
# threading.Timer(55, check_email_thread).start()
|
||||||
threading.Timer(55, check_email_thread).start()
|
|
||||||
|
time.sleep(check_email_delay)
|
||||||
|
|
||||||
|
shortcuts = CONFIG['shortcuts']
|
||||||
|
# swap key/value
|
||||||
|
shortcuts_inverted = dict([[v, k] for k, v in shortcuts.items()])
|
||||||
|
|
||||||
|
date = datetime.datetime.now()
|
||||||
|
month = date.strftime("%B")[:3] # Nov, Mar, Apr
|
||||||
|
day = date.day
|
||||||
|
year = date.year
|
||||||
|
today = "%s-%s-%s" % (day, month, year)
|
||||||
|
|
||||||
shortcuts = CONFIG['shortcuts']
|
LOG.debug("Connect to IMAP host '%s' with user '%s'" %
|
||||||
# swap key/value
|
(CONFIG['imap']['host'],
|
||||||
shortcuts_inverted = dict([[v, k] for k, v in shortcuts.items()])
|
CONFIG['imap']['login']))
|
||||||
|
|
||||||
date = datetime.datetime.now()
|
try:
|
||||||
month = date.strftime("%B")[:3] # Nov, Mar, Apr
|
server = IMAPClient(CONFIG['imap']['host'], use_uid=True, timeout=5)
|
||||||
day = date.day
|
server.login(CONFIG['imap']['login'], CONFIG['imap']['password'])
|
||||||
year = date.year
|
except Exception:
|
||||||
today = "%s-%s-%s" % (day, month, year)
|
LOG.exception("Failed to login with IMAP server")
|
||||||
|
#return
|
||||||
|
continue
|
||||||
|
|
||||||
LOG.debug("Connect to IMAP host '%s' with user '%s'" %
|
server.select_folder('INBOX')
|
||||||
(CONFIG['imap']['host'],
|
|
||||||
CONFIG['imap']['login']))
|
|
||||||
|
|
||||||
try:
|
messages = server.search(['SINCE', today])
|
||||||
server = IMAPClient(CONFIG['imap']['host'], use_uid=True, timeout=5)
|
LOG.debug("%d messages received today" % len(messages))
|
||||||
server.login(CONFIG['imap']['login'], CONFIG['imap']['password'])
|
|
||||||
except Exception:
|
|
||||||
LOG.exception("Failed to login with IMAP server")
|
|
||||||
return
|
|
||||||
|
|
||||||
# if 'gmail' in CONFIG['imap']['host'].lower():
|
for msgid, data in server.fetch(messages, ['ENVELOPE']).items():
|
||||||
# server.select_folder('INBOX')
|
envelope = data[b'ENVELOPE']
|
||||||
server.select_folder('INBOX')
|
LOG.debug('ID:%d "%s" (%s)' %
|
||||||
|
(msgid, envelope.subject.decode(), envelope.date))
|
||||||
|
f = re.search('([[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"
|
||||||
|
|
||||||
|
if "APRS" not in server.get_flags(msgid)[msgid]:
|
||||||
|
# if msg not flagged as sent via aprs
|
||||||
|
server.fetch([msgid], ['RFC822'])
|
||||||
|
(body, from_addr) = parse_email(msgid, data, server)
|
||||||
|
# unset seen flag, will stay bold in email client
|
||||||
|
server.remove_flags(msgid, [SEEN])
|
||||||
|
|
||||||
messages = server.search(['SINCE', today])
|
if from_addr in shortcuts_inverted:
|
||||||
LOG.debug("%d messages received today" % len(messages))
|
# reverse lookup of a shortcut
|
||||||
|
from_addr = shortcuts_inverted[from_addr]
|
||||||
|
|
||||||
for msgid, data in server.fetch(messages, ['ENVELOPE']).items():
|
reply = "-" + from_addr + " " + body
|
||||||
envelope = data[b'ENVELOPE']
|
# print "Sending message via aprs: " + reply
|
||||||
LOG.debug('ID:%d "%s" (%s)' %
|
# radio
|
||||||
(msgid, envelope.subject.decode(), envelope.date))
|
send_message(CONFIG['ham']['callsign'], reply)
|
||||||
f = re.search('([[A-a][0-9]_-]+@[[A-a][0-9]_-\.]+)',
|
# flag message as sent via aprs
|
||||||
str(envelope.from_[0]))
|
server.add_flags(msgid, ['APRS'])
|
||||||
if f is not None:
|
# unset seen flag, will stay bold in email client
|
||||||
from_addr = f.group(1)
|
server.remove_flags(msgid, [SEEN])
|
||||||
else:
|
|
||||||
from_addr = "noaddr"
|
|
||||||
|
|
||||||
if "APRS" not in server.get_flags(msgid)[msgid]:
|
server.logout()
|
||||||
# if msg not flagged as sent via aprs
|
|
||||||
server.fetch([msgid], ['RFC822'])
|
|
||||||
(body, from_addr) = parse_email(msgid, data, server)
|
|
||||||
# unset seen flag, will stay bold in email client
|
|
||||||
server.remove_flags(msgid, [SEEN])
|
|
||||||
|
|
||||||
if from_addr in shortcuts_inverted:
|
# end check_email()
|
||||||
# reverse lookup of a shortcut
|
|
||||||
from_addr = shortcuts_inverted[from_addr]
|
|
||||||
|
|
||||||
reply = "-" + from_addr + " " + body
|
|
||||||
# print "Sending message via aprs: " + reply
|
|
||||||
# radio
|
|
||||||
send_message(CONFIG['ham']['callsign'], reply)
|
|
||||||
# flag message as sent via aprs
|
|
||||||
server.add_flags(msgid, ['APRS'])
|
|
||||||
# unset seen flag, will stay bold in email client
|
|
||||||
server.remove_flags(msgid, [SEEN])
|
|
||||||
|
|
||||||
server.logout()
|
|
||||||
# end check_email()
|
|
||||||
|
|
||||||
|
|
||||||
def send_ack_thread(tocall, ack, retry_count):
|
def send_ack_thread(tocall, ack, retry_count):
|
||||||
|
@ -498,7 +497,9 @@ def main(args=args):
|
||||||
|
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
check_email_thread() # start email reader thread
|
check_email_delay = 60 # initial email check interval
|
||||||
|
checkemailthread = threading.Thread(target=check_email_thread, args=(check_email_delay, )) # args must be tuple
|
||||||
|
checkemailthread.start()
|
||||||
|
|
||||||
LOG.info("Start main loop")
|
LOG.info("Start main loop")
|
||||||
while True:
|
while True:
|
||||||
|
|
Loading…
Reference in New Issue