<s> """<STR_LIT>""" <EOL> from adlibre . fetchmail . app_settings import ENCRYPTION_EXISTS , DEFAULT_POP3_SSL_PORT , ENCRYPTION_ABSENT , DEFAULT_POP3_PORT <EOL> from adlibre . fetchmail . app_settings import DEFAULT_FILTER_SENDER_NAME , DEFAULT_FILTER_SUBJECT_NAME , DEFAULT_FILTER_FILENAME_NAME <EOL> from adlibre . fetchmail . processor import process_single_letter <EOL> import poplib <EOL> import rfc822 , string , StringIO <EOL> def connect_pop3 ( email_obj ) : <EOL> """<STR_LIT>""" <EOL> if email_obj . encryption == ENCRYPTION_EXISTS : <EOL> if email_obj . email_port_default : <EOL> pop3_server = poplib . POP3_SSL ( email_obj . server_name , DEFAULT_POP3_SSL_PORT ) <EOL> else : <EOL> pop3_server = poplib . POP3_SSL ( email_obj . server_name , email_obj . email_port_number ) <EOL> elif email_obj . encryption == ENCRYPTION_ABSENT : <EOL> if email_obj . email_port_default : <EOL> pop3_server = poplib . POP3 ( email_obj . server_name , DEFAULT_POP3_PORT ) <EOL> else : <EOL> pop3_server = poplib . POP3 ( email_obj . server_name , email_obj . email_port_number ) <EOL> pop3_server . user ( email_obj . login ) <EOL> pop3_server . pass_ ( email_obj . password ) <EOL> pop3_server . connection_type_info = email_obj . protocol <EOL> return pop3_server <EOL> def select_letters_pop3 ( server , filters , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> msg_ids_list = [ ] <EOL> resp , items , octets = server . list ( ) <EOL> messagesInfo = server . list ( ) [ <NUM_LIT:1> ] <EOL> print messagesInfo <EOL> for item in items : <EOL> msg_id , size = item . split ( ) <EOL> response , lines , octets = server . top ( msg_id , <NUM_LIT:3> ) <EOL> text = string . join ( lines , "<STR_LIT:\n>" ) <EOL> file = StringIO . StringIO ( text ) <EOL> message = rfc822 . Message ( file ) <EOL> message_pass , filename_filter_applied = filter_message_pop3 ( message , filters , quiet = quiet ) <EOL> if message_pass : <EOL> msg_ids_list . append ( msg_id ) <EOL> if not quiet : <EOL> print "<STR_LIT>" <EOL> print msg_ids_list <EOL> if filename_filter_applied : <EOL> print '<STR_LIT>' + str ( filename_filter_applied ) <EOL> return msg_ids_list , filename_filter_applied <EOL> def filter_message_pop3 ( message , filters , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> has_sender_filter = False <EOL> has_subject_filter = False <EOL> message_pass = False <EOL> filename_filter_applied = False <EOL> for filter_ in filters : <EOL> if filter_ . type == DEFAULT_FILTER_SENDER_NAME : <EOL> has_sender_filter = True <EOL> if filter_ . type == DEFAULT_FILTER_SUBJECT_NAME : <EOL> has_subject_filter = True <EOL> if filter_ . type == DEFAULT_FILTER_FILENAME_NAME : <EOL> filename_filter_applied = filter_ . value <EOL> checked_sender = check_sender ( message , filters ) <EOL> if not has_subject_filter : <EOL> message_pass = checked_sender <EOL> else : <EOL> message_pass = check_subject ( message , filters ) <EOL> return message_pass , filename_filter_applied <EOL> def check_sender ( message , filters ) : <EOL> """<STR_LIT>""" <EOL> message_pass = False <EOL> for filter_ in filters : <EOL> if filter_ . type == DEFAULT_FILTER_SENDER_NAME : <EOL> for key , value in message . items ( ) : <EOL> if key == '<STR_LIT>' : <EOL> if value . rfind ( filter_ . value ) != - <NUM_LIT:1> : <EOL> message_pass = True <EOL> return message_pass <EOL> def check_subject ( message , filters ) : <EOL> """<STR_LIT>""" <EOL> message_pass = False <EOL> for filter_ in filters : <EOL> if filter_ . type == DEFAULT_FILTER_SUBJECT_NAME : <EOL> for key , value in message . items ( ) : <EOL> if key == '<STR_LIT>' : <EOL> if value . rfind ( filter_ . value ) != - <NUM_LIT:1> : <EOL> message_pass = True <EOL> return message_pass <EOL> def download_letters_pop3 ( letters_list , server , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> messages = [ ] <EOL> for item in letters_list : <EOL> response , lines , octets = server . retr ( item ) <EOL> text = string . join ( lines , "<STR_LIT:\n>" ) <EOL> messages . append ( text ) <EOL> if not quiet : <EOL> print '<STR_LIT>' <EOL> return messages <EOL> def process_letters_pop3 ( messages , filename_filter_applied , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> for message in messages : <EOL> try : <EOL> process_single_letter ( message , filename_filter_applied , quiet = quiet ) <EOL> if not quiet : <EOL> print '<STR_LIT>' <EOL> except : <EOL> if not quiet : <EOL> print '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def delete_pop3_messages ( messages , server , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' <EOL> for message in messages : <EOL> print message <EOL> server . dele ( message ) <EOL> if not quiet : <EOL> print "<STR_LIT>" + str ( messages ) </s>
