def download_part(msgid, part_info, msg_info, filename, command=None, client=None, delete_file=False, max_time=60): part_id=('BODY[%s]'%part_info.section).encode('ascii') try: cmd=CommandRunner(command, filename, msg_info, delete_file, max_time) except ValueError as e: log.exception("Cannot download message: %s",e) return part=client.fetch(msgid, [part_id]) part=part[msgid][part_id] part=decode_part(part, part_info.encoding) if lower_safe(part_info.type) == 'text': charset=lower_safe(part_info.params.get('charset') ) if charset: part=reencode_charset(part, charset) try: cmd.run(part) except CommandRunner.Error as e: pass if command: log.debug('Command stdout:\n%s\nCommand stderr:\n%s\n', cmd.stdout, cmd.stderr)
def walk_structure(body, level='', count=1, multipart=False): def next_level(): return level+('.%d' if level else '%d')%count res=[] first=body[0] if isinstance(first, tuple): parts=[] i=0 while i < len (body): if not isinstance(body[i], tuple): break parts.append(body[i]) i+=1 nbody = (parts,) + body[i:] res.extend(walk_structure(nbody, level , count, multipart )) if isinstance(first, (list)): if multipart: res.append(extract_mime_info(level, body)) for part in first: res.extend(walk_structure(part, next_level() , 1, multipart )) count+=1 elif isinstance(first, six.string_types+(six.binary_type,)): # last condition is WA for servers that do not parse attached message/rfc822 - like gmail some cases if lower_safe(first) == 'message' and lower_safe(body[1]) == 'rfc822' and body[8] \ and isinstance(body[8], tuple) \ and not (isinstance(body[8][0], (six.text_type,six.binary_type)) and lower_safe(body[8][0]) == 'attachment'): if multipart: res.append(extract_mime_info(level, body)) res.extend(walk_structure(body[8],next_level() if isinstance(body[8][0], (six.text_type,six.binary_type)) \ else level,1, multipart)) elif first: res.append(extract_mime_info(level or str(count), body)) return res
def _inner(self,node, children): if not isinstance(children[0], (six.string_types)+(six.binary_type,)+(TagList,)): raise EvalError('Applicable only to strings: %s'% node.text, node.start) a=children[0] if not isinstance(a, TagList): a=lower_safe(a) b=lower_safe(children[-1]) return fn(self, node, [a,b])
def conv_disp(d): if not d: return {} res={} res['disposition']=lower_safe(d[0]) res.update(conv_dict(d[1])) return res
def conv_disp(d): if not d: return {} res = {} res['disposition'] = lower_safe(d[0]) res.update(conv_dict(d[1])) return res
def conv_dict(d): if not d: return {} if not isinstance(d, tuple): raise ValueError('Expected tuple as value') res={} for i in range(0,len(d)-1, 2): res[lower_safe(d[i])]=decode(d[i+1]) return res
def conv_dict(d): if not d: return {} if not isinstance(d, tuple): raise ValueError('Expected tuple as value') res = {} for i in range(0, len(d) - 1, 2): res[lower_safe(d[i])] = decode(d[i + 1]) return res
def extract_mime_info(level, body): def conv_dict(d): if not d: return {} if not isinstance(d, tuple): raise ValueError('Expected tuple as value') res = {} for i in range(0, len(d) - 1, 2): res[lower_safe(d[i])] = decode(d[i + 1]) return res def conv_disp(d): if not d: return {} res = {} res['disposition'] = lower_safe(d[0]) res.update(conv_dict(d[1])) return res def get(t, i): if i >= len(t): return None return t[i] body = [ lower_safe(p) if isinstance(p, six.binary_type) else p for p in body ] log.debug('Body info %s', body) if isinstance(body[0], list): info = MultiBodyInfo(level, 'multipart', body[1], conv_dict(body[2]), conv_disp(body[3]), get(body, 4), get(body, 5)) elif body[0] == 'text': info = TextBodyInfo( level, body[0], body[1], conv_dict(body[2]), body[3], body[4], body[5], int(body[6]), body[7], body[8], conv_disp(body[9]), get(body, 10), get(body, 11), ) elif body[0] == 'message' and body[1] == 'rfc822': info = BodyInfo(level, body[0], body[1], conv_dict(body[2]), body[3], body[4], body[5], int(body[6]), body[7], None, get(body, 9), get(body, 10)) else: info = BodyInfo(level, body[0], body[1], conv_dict(body[2]), body[3], body[4], body[5], int(body[6]), body[7], conv_disp(body[8]), get(body, 9), get(body, 10)) return info
def walk_structure(body, level='', count=1, multipart=False): def next_level(): return level + ('.%d' if level else '%d') % count res = [] first = body[0] if isinstance(first, tuple): parts = [] i = 0 while i < len(body): if not isinstance(body[i], tuple): break parts.append(body[i]) i += 1 nbody = (parts, ) + body[i:] res.extend(walk_structure(nbody, level, count, multipart)) if isinstance(first, (list)): if multipart: res.append(extract_mime_info(level, body)) for part in first: res.extend(walk_structure(part, next_level(), 1, multipart)) count += 1 elif isinstance(first, six.string_types + (six.binary_type, )): # last condition is WA for servers that do not parse attached message/rfc822 - like gmail some cases if lower_safe(first) == 'message' and lower_safe(body[1]) == 'rfc822' and body[8] \ and isinstance(body[8], tuple) \ and not (isinstance(body[8][0], (six.text_type,six.binary_type)) and lower_safe(body[8][0]) == 'attachment'): if multipart: res.append(extract_mime_info(level, body)) res.extend(walk_structure(body[8],next_level() if isinstance(body[8][0], (six.text_type,six.binary_type)) \ else level,1, multipart)) elif first: res.append(extract_mime_info(level or str(count), body)) return res
def extract_mime_info(level, body): def conv_dict(d): if not d: return {} if not isinstance(d, tuple): raise ValueError('Expected tuple as value') res={} for i in range(0,len(d)-1, 2): res[lower_safe(d[i])]=decode(d[i+1]) return res def conv_disp(d): if not d: return {} res={} res['disposition']=lower_safe(d[0]) res.update(conv_dict(d[1])) return res def get(t,i): if i >= len(t): return None return t[i] body = [lower_safe(p) if isinstance(p, six.binary_type) else p for p in body] log.debug('Body info %s', body) if isinstance(body[0], list): info= MultiBodyInfo(level, 'multipart', body[1], conv_dict(body[2]), conv_disp(body[3]), get(body,4), get(body,5)) elif body[0]=='text': info=TextBodyInfo(level, body[0], body[1], conv_dict(body[2]), body[3], body[4], body[5], int(body[6]), body[7], body[8], conv_disp(body[9]), get(body,10), get(body,11), ) elif body[0]=='message' and body[1]=='rfc822': info=BodyInfo(level, body[0], body[1], conv_dict(body[2]), body[3], body[4], body[5], int(body[6]), body[7], None, get(body,9), get(body,10) ) else: info=BodyInfo(level, body[0], body[1], conv_dict(body[2]), body[3], body[4], body[5], int(body[6]), body[7], conv_disp(body[8]), get(body,9), get(body,10) ) return info
def inner(self, tag): tag=lower_safe(tag) return fn(self,tag)
def __init__(self, tags): super(TagList, self).__init__(map(lambda x: lower_safe(x), tags))