added less buggy version that uses polling

This commit is contained in:
kaylee 2024-02-02 23:30:41 +00:00
parent 757eba1c6c
commit d10bf5b7ff
1 changed files with 9 additions and 50 deletions

View File

@ -194,46 +194,7 @@ import time
import socket import socket
import re import re
def idleFunction(s,unseen):
s._startIdle = time.time()
if 'IDLE' not in s.capabilities:
raise s.error('Server does not support IDLE')
idle_tag = s._command('IDLE') # start idling
s._get_response()
while line := s._get_line():
if b'FETCH' in line:
l = str(line)
msg = re.findall(r'\d+',l)[0]
if config["debug"]: print(msg, unseen)
if "seen" in l.lower():
unseen.remove(msg)
else:
unseen.append(msg)
print(unseen)
writeUnreadFile(unseen)
else:
# we only want to idle for ten minutes maximum. we will use the keep-alive messages to keep track in a single thread.
if(time.time() - s.start > 600):
s.send(b'DONE' + imaplib.CRLF)
return s._command_complete('IDLE', idle_tag)
# thanks to trentbuck on github for this awful idea
def daemon(): def daemon():
import imaplib
imaplib.Commands['IDLE'] = ('AUTH', 'SELECTED')
class IMAP4_SSL_plus_IDLE(imaplib.IMAP4_SSL):
def idle(self, unseen):
idleFunction(self,unseen)
class IMAP4_plus_IDLE(imaplib.IMAP4):
def idle(self, unseen):
idleFunction(self,unseen)
IMAP4_SSL = IMAP4_SSL_plus_IDLE
IMAP4 = IMAP4_plus_IDLE
socket.setdefaulttimeout(120)
# first check if daemon is already running # first check if daemon is already running
@ -262,25 +223,23 @@ def daemon():
# main loop # main loop
import time import time
start_time = time.time(); import imaplib
IMAP4_func = IMAP4_SSL if config["ssl"] else IMAP4 IMAP4_func = imaplib.IMAP4_SSL if config["ssl"] else imaplib.IMAP4
while True: while True:
try: try:
with IMAP4_func(config["server"],config["port"]) as conn: with IMAP4_func(config["server"],config["port"]) as conn:
conn.login(user=config["user"],password=config["password"]) conn.login(user=config["user"],password=config["password"])
conn.select(mailbox='INBOX',readonly=True) conn.select(mailbox='INBOX',readonly=True)
typ,msgnums = conn.search(None, "UNSEEN"); typ,msgnums = conn.search(None, "UNSEEN");
unseen = [n.decode() for n in msgnums if n != b''][0].split(' ') unseen = [n.decode() for n in msgnums if n != b'']
if len(unseen)>0:
unseen=unseen[0].split(' ')
else:
unseen=[]
if config["debug"]: print(unseen) if config["debug"]: print(unseen)
writeUnreadFile(unseen) writeUnreadFile(unseen)
#try: conn.logout()
resp = conn.idle(unseen) time.sleep(30)
#except Exception as e:
if config["debug"]: print(e)
if time.time() - start_time <= 10:
print("uvgotmail: too many imap errors. exiting.")
sys.exit(1)
start_time = time.time();
except Exception as e: except Exception as e:
if config["debug"]: if config["debug"]: