Exemplo n.º 1
0
def process_parts(body,
                  msg_info,
                  eval_parser,
                  filter,
                  test=False):  # @ReservedAssignment
    def msg(part_info):
        msg = u'File "{name}" of type {mime} and size {size} in email "{subject}" from {from}'.format(
            **msg_info)
        if six.PY2:
            msg = msg.encode('ascii', 'replace')
        return msg

    log.debug('Body Structure: %s', body)
    part_infos = []
    for part_info in walk_structure(body):
        if part_info.type == 'multipart':  #or (part_info.type=='message' and part_info.sub_type =='rfc822'):
            log.error('We should not get multiparts here - %s',
                      part_info.sub_type)
        else:
            log.debug('Message part %s', part_info)
            msg_info.update_part_info(part_info)
            eval_parser.context = msg_info
            if eval_parser.parse(filter):
                log.debug('Will process this part')
                if test:
                    p(msg(part_info))
                else:
                    log.info(msg(part_info))
                    part_infos.append(part_info)
            else:
                log.debug('Will skip this part')
    return part_infos
Exemplo n.º 2
0
def process_parts(body, msg_info, eval_parser, filter, test=False):  # @ReservedAssignment
    def msg(part_info):
        msg = u'File "{name}" of type {mime} and size {size} in email "{subject}" from {from}'.format(**msg_info)
        if six.PY2:
            msg=msg.encode('ascii', 'replace')
        return msg
    log.debug('Body Structure: %s', body)
    part_infos=[]
    for part_info in walk_structure(body):
        if part_info.type == 'multipart': #or (part_info.type=='message' and part_info.sub_type =='rfc822'):
            log.error('We should not get multiparts here - %s', part_info.sub_type)
        else:
            log.debug('Message part %s', part_info)
            msg_info.update_part_info(part_info)
            eval_parser.context=msg_info
            if eval_parser.parse(filter):
                log.debug('Will process this part')
                if test:
                    p(msg(part_info))
                else:
                    log.info(msg(part_info))
                    part_infos.append(part_info)
            else:
                log.debug('Will skip this part')
    return part_infos

    
    

    
Exemplo n.º 3
0
 def test_single_single(self):
     has_section=False
     for info in walk_structure(BODY4, multipart=True):
         p(info)
         if info.section =='2.1':
             has_section = True
     self.assertTrue(has_section)
Exemplo n.º 4
0
 def test_single_single(self):
     has_section = False
     for info in walk_structure(BODY4, multipart=True):
         p(info)
         if info.section == '2.1':
             has_section = True
     self.assertTrue(has_section)
Exemplo n.º 5
0
 def test_gmail(self):
     sec_2 = False
     for info in walk_structure(BODY3, multipart=True):
         p(info)
         if info.section =='2':
             self.assertEqual(info.type, 'message')
             self.assertEqual(info.sub_type, 'rfc822')
             sec_2=True
     self.assertTrue(sec_2)
Exemplo n.º 6
0
 def test_gmail(self):
     sec_2 = False
     for info in walk_structure(BODY3, multipart=True):
         p(info)
         if info.section == '2':
             self.assertEqual(info.type, 'message')
             self.assertEqual(info.sub_type, 'rfc822')
             sec_2 = True
     self.assertTrue(sec_2)
Exemplo n.º 7
0
def main():
    start=time.time()
    parser=argparse.ArgumentParser(epilog=extra_help(), formatter_class=RawTextHelpFormatter)
    define_arguments(parser)
    opts=parser.parse_args()
    
    init_logging(opts, sys.stderr)
    client_main(opts)
        
    if opts.timeit:
        p('Total Time: %f s' % (time.time()-start))
Exemplo n.º 8
0
 def test_msg(self):
     sec_2=False
     sec_23=False
     for info in walk_structure(BODY2, multipart=True):
         p(info)
         if info.section =='2':
             self.assertTrue(info.type == 'multipart' or info.type == 'message')
             self.assertTrue(info.sub_type == 'mixed' or info.sub_type == 'rfc822' )
             sec_2=True
         if info.section =='2.3':
             self.assertEqual(info.type, 'image')
             self.assertEqual(info.sub_type, 'png')
             sec_23=True
     self.assertTrue(sec_2)
     self.assertTrue(sec_23)
Exemplo n.º 9
0
 def test(self):
     sec_2=False
     sec_122=False
     for info in walk_structure(BODY):
         p(info)
         if info.section =='2':
             self.assertEqual(info.type, 'text')
             self.assertEqual(info.sub_type, 'csv')
             sec_2=True
         if info.section =='1.2.2':
             self.assertEqual(info.type, 'image')
             self.assertEqual(info.sub_type, 'gif')
             sec_122=True
     self.assertTrue(sec_2)
     self.assertTrue(sec_122)
Exemplo n.º 10
0
 def test(self):
     sec_2 = False
     sec_122 = False
     for info in walk_structure(BODY):
         p(info)
         if info.section == '2':
             self.assertEqual(info.type, 'text')
             self.assertEqual(info.sub_type, 'csv')
             sec_2 = True
         if info.section == '1.2.2':
             self.assertEqual(info.type, 'image')
             self.assertEqual(info.sub_type, 'gif')
             sec_122 = True
     self.assertTrue(sec_2)
     self.assertTrue(sec_122)
Exemplo n.º 11
0
 def test_msg(self):
     sec_2 = False
     sec_23 = False
     for info in walk_structure(BODY2, multipart=True):
         p(info)
         if info.section == '2':
             self.assertTrue(info.type == 'multipart'
                             or info.type == 'message')
             self.assertTrue(info.sub_type == 'mixed'
                             or info.sub_type == 'rfc822')
             sec_2 = True
         if info.section == '2.3':
             self.assertEqual(info.type, 'image')
             self.assertEqual(info.sub_type, 'png')
             sec_23 = True
     self.assertTrue(sec_2)
     self.assertTrue(sec_23)
Exemplo n.º 12
0
def main(opts):
    host, port= split_host(opts.host, ssl=not opts.no_ssl)
    ssl= not opts.no_ssl
    if opts.insecure_ssl:
        ssl='insecure'
    log.debug('SSL status is %s', ssl)
    # test filter parsing
    eval_parser=SimpleEvaluator(DUMMY_INFO)
    filter=opts.filter  # @ReservedAssignment
    
    try:
        imap_filter=IMAPFilterGenerator(opts.unsafe_imap_search).parse(filter, serialize='string') if not opts.no_imap_search else None
        _ = eval_parser.parse(filter)
    except ParserSyntaxError as e:
        msg = "Invalid syntax of filter: %s" %extract_err_msg(e)
        log.error(msg)
#        p(msg, file=sys.stderr)
        sys.exit(1)
    except ParserEvalError as e:
        msg = "Invalid semantics of filter: %s" % extract_err_msg(e)
        log.error(msg)
        #p(msg, file=sys.stderr)
        sys.exit(2)
    
    charset=None    
    if  imap_filter:
        try:
            imap_filter.encode('ascii')
        except UnicodeEncodeError:
            log.warn('Your search contains non-ascii characters, will try UTF-8, but it may not work on some servers')
            try:
                imap_filter=IMAPFilterGenerator(opts.unsafe_imap_search).parse(filter, serialize='list')
                [part.encode('utf-8') for part in imap_filter]
                charset='UTF-8'
            except UnicodeEncodeError as e:
                log.error('Invalid characters in filter: %e',e)
                sys.exit(3)   
        
        
    log.debug('IMAP filter: %s', imap_filter) 

    pool=None
    try:   
        c=IMAP_client_factory(host,port,use_ssl=ssl)
        if not c.has_capability('imap4rev1'):
            log.error('This server is not compatible with IMAP4rev1!')
            sys.exit(4)
        try:
            c.login(opts.user, opts.password)
            if not opts.folder:
                folders=['INBOX']
            else:
                available=c.list_folders()
                folders= matched_folders(normalize_folders(available), opts.folder)
                if not folders:
                    log.warn('No folders patching %s', opts.folder)
             
            log.debug('Will process these folders: %s', folders)    
            if opts.move and  not c.folder_exists(opts.move):
                c.create_folder(opts.move)
           
            if opts.threads:
                pool=Pool(opts.threads, host, port, ssl, opts.user, opts.password)
            for folder in folders:
                log.info('Processing folder %s', folder)
                process_folder(c, pool, folder, imap_filter, charset, eval_parser, opts)  
            
            if pool:
                if opts.verbose:
                    p('Finished searching, now wait until all parts download in separate threads')
                def report():
                    while True:
                        log.debug('Pool status - size %d, unfinished %d', pool._queue.qsize(), pool._queue.unfinished_tasks)
                        time.sleep(5)
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    t=Thread(target=report, name='Debug reporting')
                    t.daemon=True
                    t.start()
                pool.wait_finish()   
                log.debug('Pool is finished')
        finally:
            if opts.delete or opts.move:
                try:
                    c.expunge() # last one shoyld be selected
                    for f in folders[:-1]:
                        c.select_folder(f)
                        c.expunge()
                    log.debug("Expunge folders")
                except Exception:
                    log.exception('Expunge Failed!')
                
                
            c.logout()
            
    except Exception:
        log.exception('Runtime Error')     
        sys.exit(4)
Exemplo n.º 13
0
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut 
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate 
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non 
proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""

words = re.split(r"\s*\.?,?\s*", text)


def rand_text():
    size = random.randint(len(words) / 2, len(words))
    t = random.sample(words, size)
    return " ".join(t)


count = int(sys.argv[1]) if len(sys.argv) > 1 else 1

s = smtplib.SMTP("localhost", port=20025)
s.login("test", "test")
for i in range(count):
    msg = MIMEText(rand_text())
    # you == the recipient's email address
    msg["Subject"] = "Test Email %d" % i
    msg["From"] = "*****@*****.**"
    msg["To"] = "*****@*****.**"
    s.sendmail("*****@*****.**", ["*****@*****.**"], msg.as_string())
    p("Sent email no. %d" % i)
    time.sleep(0.1)

s.quit()
Exemplo n.º 14
0
def main(opts):
    host, port = split_host(opts.host, ssl=not opts.no_ssl)
    ssl = not opts.no_ssl
    if opts.insecure_ssl:
        ssl = 'insecure'
    log.debug('SSL status is %s', ssl)
    # test filter parsing
    eval_parser = SimpleEvaluator(DUMMY_INFO)
    filter = opts.filter  # @ReservedAssignment

    try:
        imap_filter = IMAPFilterGenerator(opts.unsafe_imap_search).parse(
            filter, serialize='string') if not opts.no_imap_search else None
        _ = eval_parser.parse(filter)
    except ParserSyntaxError as e:
        msg = "Invalid syntax of filter: %s" % extract_err_msg(e)
        log.error(msg)
        #        p(msg, file=sys.stderr)
        sys.exit(1)
    except ParserEvalError as e:
        msg = "Invalid semantics of filter: %s" % extract_err_msg(e)
        log.error(msg)
        #p(msg, file=sys.stderr)
        sys.exit(2)

    charset = None
    if imap_filter:
        try:
            imap_filter.encode('ascii')
        except UnicodeEncodeError:
            log.warn(
                'Your search contains non-ascii characters, will try UTF-8, but it may not work on some servers'
            )
            try:
                imap_filter = IMAPFilterGenerator(
                    opts.unsafe_imap_search).parse(filter, serialize='list')
                [part.encode('utf-8') for part in imap_filter]
                charset = 'UTF-8'
            except UnicodeEncodeError as e:
                log.error('Invalid characters in filter: %e', e)
                sys.exit(3)

    log.debug('IMAP filter: %s', imap_filter)

    pool = None
    try:
        c = IMAP_client_factory(host, port, use_ssl=ssl)
        if not c.has_capability('imap4rev1'):
            log.error('This server is not compatible with IMAP4rev1!')
            sys.exit(4)
        try:
            c.login(opts.user, opts.password)
            if not opts.folder:
                folders = ['INBOX']
            else:
                available = c.list_folders()
                folders = matched_folders(normalize_folders(available),
                                          opts.folder)
                if not folders:
                    log.warn('No folders patching %s', opts.folder)

            log.debug('Will process these folders: %s', folders)
            if opts.move and not c.folder_exists(opts.move):
                c.create_folder(opts.move)

            if opts.threads:
                pool = Pool(opts.threads, host, port, ssl, opts.user,
                            opts.password)
            for folder in folders:
                log.info('Processing folder %s', folder)
                process_folder(c, pool, folder, imap_filter, charset,
                               eval_parser, opts)

            if pool:
                if opts.verbose:
                    p('Finished searching, now wait until all parts download in separate threads'
                      )

                def report():
                    while True:
                        log.debug('Pool status - size %d, unfinished %d',
                                  pool._queue.qsize(),
                                  pool._queue.unfinished_tasks)
                        time.sleep(5)

                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    t = Thread(target=report, name='Debug reporting')
                    t.daemon = True
                    t.start()
                pool.wait_finish()
                log.debug('Pool is finished')
        finally:
            if opts.delete or opts.move:
                try:
                    c.expunge()  # last one shoyld be selected
                    for f in folders[:-1]:
                        c.select_folder(f)
                        c.expunge()
                    log.debug("Expunge folders")
                except Exception:
                    log.exception('Expunge Failed!')

            c.logout()

    except Exception:
        log.exception('Runtime Error')
        sys.exit(4)
Exemplo n.º 15
0
text = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut 
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate 
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non 
proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""

words = re.split(r'\s*\.?,?\s*', text)


def rand_text():
    size = random.randint(len(words) / 2, len(words))
    t = random.sample(words, size)
    return ' '.join(t)


count = int(sys.argv[1]) if len(sys.argv) > 1 else 1

s = smtplib.SMTP('localhost', port=20025)
s.login('test', 'test')
for i in range(count):
    msg = MIMEText(rand_text())
    # you == the recipient's email address
    msg['Subject'] = 'Test Email %d' % i
    msg['From'] = '*****@*****.**'
    msg['To'] = '*****@*****.**'
    s.sendmail('*****@*****.**', ['*****@*****.**'], msg.as_string())
    p('Sent email no. %d' % i)
    time.sleep(0.1)

s.quit()