def get_mail_list(self): if not self.configuration['email_to'] or self.configuration['email_to'] == '': log.error('No receiver email address defined.') return [] if not os.path.isdir(self.storage_dir): log.error('Invalid whisper storage path specified.') return [] mails = [] current_mail = self._setup_mail() current_size = 0 for attachment_path in self._match_files(self.storage_dir): attachment = self.create_attachment(attachment_path, self._path_to_metric_filename(attachment_path)) if attachment: # Attachments are Base64 encoded and hence, are # roughly 137% the size of the actual attachment size, # so we need to use this final size. file_size = len(attachment.as_string()) if file_size > self.max_mail_size: self.debug('File size exceeds limit and will not be sent: %s' % file_size) if current_size + file_size > self.max_mail_size: mails.append(current_mail) current_mail = self._setup_mail() current_size = 0 current_mail.attach(attachment) current_size += file_size if current_size > 0: mails.append(current_mail) return mails
def generate_mails(self): if not self.configuration['email_to'] or self.configuration[ 'email_to'] == '': log.error('No receiver email address defined.') return [] if not os.path.isdir(self.storage_dir): log.error('Invalid whisper storage path specified.') return [] mails = [] current_mail = self._setup_mail() current_size = 0 for attachment_path in self._match_files(self.storage_dir): attachment = self.create_attachment( attachment_path, self._path_to_metric_filename(attachment_path)) if attachment: # Attachments are Base64 encoded and hence, are # roughly 137% the size of the actual attachment size, # so we need to use this final size. file_size = len(attachment.as_string()) if file_size > self.max_mail_size: self.debug( 'File size exceeds limit and will not be sent: %s' % file_size) if current_size + file_size > self.max_mail_size: mails.append(current_mail) current_mail = self._setup_mail() current_size = 0 current_mail.attach(attachment) current_size += file_size if current_size > 0: mails.append(current_mail) return mails
def main(): import argparse parser = argparse.ArgumentParser(description='Warden IMAPReceiver configuration file parser') parser.add_argument('--config', help="Path to the Warden IMAPReceiver configuration file.", dest='config') parser.add_argument('--server', help='Run continually in server mode (default is to run once)', action='store_true', default=False) args, unknown = parser.parse_known_args(sys.argv) imapreceiver_configuration_file = os.path.abspath(os.path.expanduser(args.config)) if not os.path.exists(imapreceiver_configuration_file): log.error('The IMAPReceiver config file specified ("%s") does not exist!' % imapreceiver_configuration_file) sys.exit(1) config = ConfigParser.SafeConfigParser() config.read(imapreceiver_configuration_file) imapreceiver = IMAPReceiver(config) if args.server: imapreceiver.start() else: imapreceiver.connect_to_mailbox() imapreceiver.check_mails()
def start(self): # Try to connect to mailbox until successful while True: try: self.connection = self.connect_to_mailbox() self.connected = True resp, data = self.connection.select('INBOX') if resp != "OK": raise "The INBOX mailbox does not exist." # Continually check for new mails (with sleep time specified in settings.py) while True: self.check_mails() time.sleep(self.config.get('daemon','recheck_delay')) except Exception as e: log.error(e) finally: if self.connected: self.connection.logout() time.sleep(self.config.get('daemon','recheck_delay'))