added less buggy version that uses polling
This commit is contained in:
		
							parent
							
								
									757eba1c6c
								
							
						
					
					
						commit
						d10bf5b7ff
					
				
							
								
								
									
										59
									
								
								uvgotmail.py
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								uvgotmail.py
									
									
									
									
									
								
							| @ -194,46 +194,7 @@ import time | ||||
| import socket | ||||
| 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(): | ||||
|     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 | ||||
| @ -262,25 +223,23 @@ def daemon(): | ||||
| 
 | ||||
|     # main loop | ||||
|     import time | ||||
|     start_time = time.time(); | ||||
|     IMAP4_func = IMAP4_SSL if config["ssl"] else IMAP4 | ||||
|     import imaplib | ||||
|     IMAP4_func = imaplib.IMAP4_SSL if config["ssl"] else imaplib.IMAP4 | ||||
|     while True: | ||||
|         try: | ||||
|             with IMAP4_func(config["server"],config["port"]) as conn: | ||||
|                 conn.login(user=config["user"],password=config["password"]) | ||||
|                 conn.select(mailbox='INBOX',readonly=True) | ||||
|                 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) | ||||
|                 writeUnreadFile(unseen) | ||||
|                 #try: | ||||
|                 resp = conn.idle(unseen) | ||||
|                 #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(); | ||||
|                 conn.logout() | ||||
|             time.sleep(30) | ||||
| 
 | ||||
|         except Exception as e: | ||||
|             if config["debug"]: | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user