Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def wait_data(self):
if self.response is None:
return False
last_fetch_index = 0
for index, line in enumerate(self.response.lines):
if isinstance(line, str) and self.FETCH_MESSAGE_DATA_RE.match(line):
last_fetch_index = index
return not matched_parenthesis(''.join(filter(lambda l: isinstance(l, str),
self.response.lines[last_fetch_index:])))
def matched_parenthesis(string):
return string.count('(') == string.count(')')
class IdleCommand(Command):
def __init__(self, tag, queue, *args, prefix=None, untagged_resp_name=None,
loop=asyncio.get_event_loop(), timeout=None):
super().__init__('IDLE', tag, *args, prefix=prefix, untagged_resp_name=untagged_resp_name,
loop=loop, timeout=timeout)
self.queue = queue
self.buffer = list()
def append_to_resp(self, line, result='Pending'):
if result != 'Pending':
super().append_to_resp(line, result)
else:
self.buffer.append(line)
def flush(self):
if self.buffer:
self.queue.put_nowait(copy(self.buffer))
@change_state
@asyncio.coroutine
def login(self, user, password):
response = yield from self.execute(
Command('LOGIN', self.new_tag(), user, '%s' % quoted(password), loop=self.loop))
if 'OK' == response.result:
self.state = AUTH
for line in response.lines:
if 'CAPABILITY' in line:
self.capabilities = self.capabilities.union(set(line.replace('CAPABILITY', '').strip().split()))
return response
@asyncio.coroutine
def capability(self):
response = yield from self.execute(Command('CAPABILITY', self.new_tag(), loop=self.loop))
capability_list = response.lines[0].split()
self.capabilities = set(capability_list)
version = capability_list[0].upper()
if version not in AllowedVersions:
raise Error('server not IMAP4 compliant')
else:
self.imap_version = version
@asyncio.coroutine
def expunge(self, *args, by_uid=False):
return (yield from self.execute(
Command('EXPUNGE', self.new_tag(), *args,
prefix='UID' if by_uid else '', loop=self.loop)))
@change_state
@asyncio.coroutine
def logout(self):
response = (yield from self.execute(Command('LOGOUT', self.new_tag(), loop=self.loop)))
if 'OK' == response.result:
self.state = LOGOUT
return response
if current_cmd is not None and current_cmd.wait_literal_data():
data = current_cmd.append_literal_data(data)
if current_cmd.wait_literal_data():
raise IncompleteRead(current_cmd)
line, separator, tail = data.partition(CRLF)
if not separator:
raise IncompleteRead(current_cmd, data)
cmd = line_handler(line.decode(), current_cmd)
begin_literal = literal_data_re.match(line)
if begin_literal:
size = int(begin_literal.group('size'))
if cmd is None:
cmd = Command('NIL', 'unused')
cmd.begin_literal_data(size)
self._handle_responses(tail, line_handler, current_cmd=cmd)
elif cmd is not None and cmd.wait_data():
self._handle_responses(tail, line_handler, current_cmd=cmd)
else:
self._handle_responses(tail, line_handler)
self._literal_data = None
def _set_timer(self):
if self._timeout is not None:
self._timer = self._loop.call_later(self._timeout, self._timeout_callback)
def _timeout_callback(self):
self._exception = CommandTimeout(self)
self.close(str(self._exception), 'KO')
def _reset_timer(self):
self._timer.cancel()
self._set_timer()
class FetchCommand(Command):
FETCH_MESSAGE_DATA_RE = re.compile(r'[0-9]+ FETCH \(')
def __init__(self, tag, *args, prefix=None, untagged_resp_name=None,
loop=asyncio.get_event_loop(), timeout=None):
super().__init__('FETCH', tag, *args, prefix=prefix, untagged_resp_name=untagged_resp_name,
loop=loop, timeout=timeout)
def wait_data(self):
if self.response is None:
return False
last_fetch_index = 0
for index, line in enumerate(self.response.lines):
if isinstance(line, str) and self.FETCH_MESSAGE_DATA_RE.match(line):
last_fetch_index = index
return not matched_parenthesis(''.join(filter(lambda l: isinstance(l, str),
self.response.lines[last_fetch_index:])))
@asyncio.coroutine
def move(self, uid_set, mailbox, by_uid=False):
if 'MOVE' not in self.capabilities:
raise Abort('server has not MOVE capability')
return (yield from self.execute(
Command('MOVE', self.new_tag(), uid_set, mailbox, prefix='UID' if by_uid else '', loop=self.loop)))
@asyncio.coroutine
def append(self, message_bytes, mailbox='INBOX', flags=None, date=None, timeout=None):
args = [mailbox]
if flags is not None:
if (flags[0], flags[-1]) != ('(', ')'):
args.append('(%s)' % flags)
else:
args.append(flags)
if date is not None:
args.append(time2internaldate(date))
args.append('{%s}' % len(message_bytes))
self.literal_data = message_bytes
return (yield from self.execute(Command('APPEND', self.new_tag(), *args, loop=self.loop, timeout=timeout)))