Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def failure_to_status(exc):
what = brat.SERVER
duration = brat.TEMPORARY
if isinstance(exc, socket.error) and exc.errno==errno.ECONNREFUSED:
why = brat.UNREACHABLE
elif isinstance(exc, IMAP4AuthException):
what = brat.ACCOUNT
why = exc.why
# It would be nice to treat authentication failures as permanent, but
# it isn't clear how to differentiate a "bad password" response (which
# is permanent) from a 'too many concurrent connections' type response
# which is temporary - so we assume all are temporary.
elif isinstance(exc, socket.timeout):
why = brat.TIMEOUT
elif isinstance(exc, imapclient.IMAPClient.Error):
logger.warn('unexpected IMAP error: %s', exc)
why = brat.UNKNOWN
else:
logger.exception('unexpected exception')
why = brat.UNKNOWN
return {'what': what,
'state': brat.BAD,
'why': why,
'duration': duration,
'message': str(exc)}
else:
resultS = 'OK'
host = self.config.get('EMAIL','EMAIL_IMAP_HOST')
if host != '':
try:
server = imapclient.IMAPClient(host, use_uid=False, ssl=True)
server.login(self.config.get('EMAIL','EMAIL_USERNAME'), self.config.get('EMAIL','EMAIL_PASSWORD'))
server.select_folder('INBOX')
server.logout()
resultI = "OK"
except socket.gaierror:
resultI = 'Connection Failed'
except socket.timeout:
resultI = 'Connection Timeout'
except imapclient.IMAPClient.Error as e:
resultI = e.args[0].decode('utf-8')
except:
resultI = str(sys.exc_info())
self.callback('IMAP: '+resultI,'SMTP: '+resultS)
return None
resultS = 'Failed'
else:
resultS = 'OK'
if self.config['EMAIL']['EMAIL_IMAP_HOST']!= None:
try:
server = imapclient.IMAPClient(self.config['EMAIL']['EMAIL_IMAP_HOST'], use_uid=False, ssl=True)
server.login(self.config['EMAIL']['EMAIL_USERNAME'], self.config['EMAIL']['EMAIL_PASSWORD'])
server.select_folder('INBOX')
server.logout()
resultI = "OK"
except socket.gaierror:
resultI = 'Connection Failed'
except socket.timeout:
resultI = 'Connection Timeout'
except imapclient.IMAPClient.Error as e:
resultI = e.args[0].decode('utf-8')
self.callback('IMAP: '+resultI,'SMTP: '+resultS)
return None
def run(self):
eventQueue.put(('status','email',"Connecting to server..."))
try:
server = setupIMAP()
except socket.gaierror:
eventQueue.put(('log',"Error: Could not connect to mail IMAP server, will not listen for remote commands"))
eventQueue.put(("Status",'email',"Error: IMAP Connection Failed"))
return
except imapclient.IMAPClient.Error as err:
eventQueue.put(('log',"Error: Could not connect to mail IMAP server, will not listen for remote commands"))
eventQueue.put(("Status",'email',err.args[0].decode()))
return
self.server = server
self.server.idle()
eventQueue.put(('status','email',"Active"))
connectionFails = 0
if self.running == False:
eventQueue.put(("Status",'email','Not Running'))
while self.running == True:
#refresh the connection every 14 mins so it doesnt timeout
def run(self):
eventQueue.put(("Status",'email','Connecting to server...'))
try:
server = setupIMAP()
except socket.gaierror:
eventQueue.put(("Status",'email',"Error: Connect Failed"))
return
except imapclient.IMAPClient.Error as err:
eventQueue.put(("Status",'email',err.args[0].decode()))
return
self.server = server
server.idle()
connectionFails = 0
#dont seem to get an exception when connection attempt interrupted by user
#this checks if 'stop' was pressed during the connecting phase
if self.running == False:
eventQueue.put(("Status",'email','Not Running'))
return
eventQueue.put(("Status",'email','Active'))
while self.running == True:
resultS = 'Failed'
else:
resultS = 'OK'
if self.config['EMAIL']['EMAIL_IMAP_HOST']!= None:
try:
server = imapclient.IMAPClient(self.config['EMAIL']['EMAIL_IMAP_HOST'], use_uid=False, ssl=True)
server.login(self.config['EMAIL']['EMAIL_USERNAME'], self.config['EMAIL']['EMAIL_PASSWORD'])
server.select_folder('INBOX')
server.logout()
resultI = "OK"
except socket.gaierror:
resultI = 'Connection Error'
except socket.timeout:
resultI = 'Connection Timeout'
except imapclient.IMAPClient.Error as e:
resultI = e.args[0].decode('utf-8')
self.callback('IMAP: '+resultI,'SMTP: '+resultS)
return None
# gmail's allmail folder always has the '\\AllMail' flag set
# regardless of the user language in gmail's settings
if self._label:
all_mail = self._label
else:
for flags, delimiter, folder_name in self._server.xlist_folders():
if u'\\AllMail' in flags:
all_mail = folder_name
break
# stats logging
print "LOG: selecting message folder '%s'" % all_mail
try:
self._server.select_folder(all_mail, readonly=True)
except IMAPClient.Error:
raise Exception('Cannot select the folder {}, please verify its name'.format(all_mail))
from threading import Thread, current_thread
import imapclient
from collections import namedtuple
import six
from six.moves.queue import Queue # @UnresolvedImport
import socket
from imap_detach.download import download
from copy import copy
from imap_detach.utils import IMAP_client_factory
log=logging.getLogger('pool')
# DownloadItem=namedtuple('DownloadItem', ('msgid', 'part_infos', 'msg_info', 'filename', 'command', 'delete', 'max_time',
# 'message_action', 'message_action_args'))
SOFT_ERRORS=(imapclient.IMAPClient.Error, socket.error)
class UIDExpired(Exception):
Exception
class Downloader(Thread):
def __init__(self, queue,host, port, ssl, user, password):
super(Downloader,self).__init__(name='Downloader thread')
self._client=IMAP_client_factory(host,port, use_ssl=ssl)
self._client.login(user, password)
self.daemon=True
self._queue=queue
self.running=True
self._selected_folder=None
def stop(self):
self.running=False