예제 #1
0
파일: html.py 프로젝트: tgulacsi/apostle
def filter_iter(msg):
    html_part, cidmap = None, {}
    state = 0
    for part in mail_walk(msg):
        future = False
        typ = part.get_content_type()
        fn = part.get_filename(None) if typ.startswith('image/') else None
        LOG.debug("filter_iter(%s [%s])", typ, fn)
        if 0 == state:
            if 'text/html' == typ:
                LOG.debug('part=(%s) %r mp?%s cte=%s', type(part),
                        part.get_content_type(), part.is_multipart(),
                        part['Content-Transfer-Encoding'])

                html_part = decode_payload(part, debork=True)
                state += 1
                future = True
        elif 1 == state:
            if typ.startswith('image/'):
                if ('Content-ID' in part and 'Content-Disposition' in part
                        and part['Content-Disposition'].startswith('inline;')):
                    cidmap[part['Content-ID'].strip('<>')] = part
                    # LOG.debug('part(content-id)=%r', part.items())
                    future = True
                else:
                    state = 0
            elif 'text/html' == typ:
                yield (True, (html_part, cidmap))
                html_part = decode_payload(part, debork=True)
                cidmap = {}
                future = True
            else:
                state = 0
        # LOG.debug('future? %s', future)
        if not future:
            yield (False, part)
    if html_part:
        yield (True, (html_part, cidmap))