def get_messages(self, logAgent, indice_log, is_retry, smtp): nmesg = len(self.lmsg) general.cursor_off() header = '' fromwho = '' if self.delivery == 'mta': deliver = self.deliver_smtp elif self.delivery == 'mbox': deliver = self.deliver_mbox elif self.delivery == 'maildir': deliver = self.deliver_maildir elif self.delivery == 'pipe': deliver = self.deliver_pipe elif self.delivery == 'global': if self.config.optsmtp: deliver = self.deliver_smtp elif self.config.optmbox: deliver = self.deliver_mbox elif self.config.optmaildir: deliver = self.deliver_maildir elif self.config.optsendmail: deliver = self.deliver_pipe num_trans = 0 retcode = 'pass' for msg in self.lmsg[:]: must_deliver = True num_trans += 1 (size, unit) = general.compute(msg['size']) if not self.config.optsyslog: aprint('-'* 21, general.GREEN) if self.config.optexpresion: (retcode, header, fromwho) = self.look_message(msg,logAgent,indice_log,nmesg - num_trans,smtp) if retcode == 'delete': #Deleted if self.config.optsmtp: smtp.smtpobj.noop() continue # end if self.config.optexpresion aprint(_("Downloading message %d (%.2f %s), Left: %d") % (msg['orignumber'], size, unit, nmesg - num_trans),general.VIOLET) (msgbody, fromm, tmpheader) = self.get_message(msg, smtp, header, nmesg) if fromm != '' and fromwho == '': fromwho = fromm if tmpheader != '': header = tmpheader if not self.config.optsyslog and not self.config.optquiet: sys.stderr.write(general.colorize(_("\rDownloaded: "),general.YELLOW)) sys.stderr.write(general.colorize("100%\n",general.WHITE)) # If self.config.optexpression we've already looked for the IMAP # DONTDELETETHISMESSAGE messages in the look_header method if not self.config.optexpresion: if msg['orignumber'] == 1: resultWUIMAP = re_wuimap.search(msgbody) resultWUIMAP2 = re_wuimap.search(header) if (resultWUIMAP is not None) or (resultWUIMAP2 is not None): aprint(_("Ignoring message %d: DONTDELETETHISMESSAGE type message") % (msg['orignumber']), general.RED) retcode = 'wuimap' if retcode == 'wuimap': must_deliver = False try: if retcode == 'postergate': #Message delayed animailBoxPath = self.config.homedir + '/.animail/animailDelayed' if not os.path.exists(animailBoxPath): os.mkdir(animailBoxPath, 0700) (foo,emailaddress) = rfc822.parseaddr(fromwho) tmpbox = open(animailBoxPath + '/' + emailaddress, 'a+') self.write_mbox(tmpbox, header, msgbody, fromm) else: if retcode != 'wuimap': must_deliver = True if (self.config.lpostfilters != []) and (retcode != 'wuimap'): msgstr = self.create_msgstr(header,msgbody) tmpfile = open(tempfile.mktemp(), 'w+') os.chmod(tmpfile.name, 600) tmpfile.write(msgstr) for pfilter in self.config.lpostfilters: output = '' aprint(_("Executing postfilter %s on message...") % pfilter.filtername, general.YELLOW) (pretcode, output) = pfilter.filter_mail(msgstr,tmpfile.name) if pretcode == 1: must_deliver = False if not self.config.optnotlog: logAgent.append_filtered(indice_log,_("%s (Postfiltered by'%s')")%(fromm,pfilter.filtername),msg['size']) if pfilter.savepostfilteredmails != 0: pfilter.log_filtered_mail(msgstr,fromm, output) tmpfile.close() os.unlink(tmpfile.name) if must_deliver: deliver(header, msgbody, fromm, smtp) if self.optdelete and retcode != 'wuimap': aprint(_("Deleting message"), general.YELLOW) self.delmesg(msg) except SMTPException, x: print print _("The local SMTP rejected this message so it won't be deleted from the mail server:"), str(x) smtp.quit() smtp.connect() if not self.config.optnotlog: logAgent.incident(indice_log, str(x)) except socket.error, x: print print _("Socket error sending to the local SMTP, it won't be deleted from the mail server:"), str(x) try: smtp.quit() smtp.connect() except AttributeError: pass if not self.config.optnotlog: logAgent.incident(indice_log, str(x)) continue
def get_messages(self, logAgent, indice_log, is_retry, smtp): nmesg = len(self.lmsg) general.cursor_off() header = '' fromwho = '' if self.delivery == 'mta': deliver = self.deliver_smtp elif self.delivery == 'mbox': deliver = self.deliver_mbox elif self.delivery == 'maildir': deliver = self.deliver_maildir elif self.delivery == 'pipe': deliver = self.deliver_pipe elif self.delivery == 'global': if self.config.optsmtp: deliver = self.deliver_smtp elif self.config.optmbox: deliver = self.deliver_mbox elif self.config.optmaildir: deliver = self.deliver_maildir elif self.config.optsendmail: deliver = self.deliver_pipe num_trans = 0 retcode = 'pass' for msg in self.lmsg[:]: must_deliver = True num_trans += 1 (size, unit) = general.compute(msg['size']) if not self.config.optsyslog: aprint('-'* 21, general.GREEN) if self.config.optexpresion: (retcode, header, fromwho) = self.look_message(msg,logAgent,indice_log,nmesg - num_trans,smtp) if retcode == 'delete': #Deleted if self.config.optsmtp: smtp.smtpobj.noop() continue # end if self.config.optexpresion aprint(_("Downloading message %d (%.2f %s), Left: %d (%s)") % (msg['orignumber'], size, unit, nmesg - num_trans, self.alias),general.VIOLET) (msgbody, fromm, tmpheader) = self.get_message(msg, smtp, header, nmesg) if fromm != '' and fromwho == '': fromwho = fromm if tmpheader != '': header = tmpheader if not self.config.optsyslog and not self.config.optquiet: sys.stderr.write(general.colorize(_("\rDownloaded: "),general.YELLOW)) sys.stderr.write(general.colorize("100%\n",general.WHITE)) # If self.config.optexpression we've already looked for the IMAP # DONTDELETETHISMESSAGE messages in the look_header method if not self.config.optexpresion: if msg['orignumber'] == 1: resultWUIMAP = re_wuimap.search(msgbody) resultWUIMAP2 = re_wuimap.search(header) if (resultWUIMAP is not None) or (resultWUIMAP2 is not None): aprint(_("Ignoring message %d: DONTDELETETHISMESSAGE type message") % (msg['orignumber']), general.RED) retcode = 'wuimap' if retcode == 'wuimap': must_deliver = False try: if retcode == 'postergate': #Message delayed animailBoxPath = self.config.homedir + '/.animail/animailDelayed' if not os.path.exists(animailBoxPath): os.mkdir(animailBoxPath, 0700) (foo,emailaddress) = rfc822.parseaddr(fromwho) tmpbox = open(animailBoxPath + '/' + emailaddress, 'a+') self.write_mbox(tmpbox, header, msgbody, fromm) else: if retcode != 'wuimap': must_deliver = True if (self.config.lpostfilters != []) and (retcode != 'wuimap'): #FIXME SESSION: En realidad sólo sería necesario pasarle al #postfiltro el msgstr; después este, en su método #filter_mail ya se encargaría de crear el archivo temporal, #y sólo de ser necesario (si el filtro tiene opciones y #si estas opciones contienen un %M). #el create_msgstr también podría ir en el postfiltro pero #depende de la subclase de mailserver (pop3 o imap4). Eso #habría que arreglarlo definitivamente para que en la clase #base no se depende del tipo, quizás añadiendo un método a #las clases derivadas header_and_body o algo así. msgstr = self.create_msgstr(header,msgbody) tmpfile = open(tempfile.mktemp(), 'w+') os.chmod(tmpfile.name, 0600) tmpfile.write(msgstr) # Number of filters that have matched this message: npositives = 0 # Have we copied this email to the spambox yet? alreadycopied = 0 for pfilter in self.config.lpostfilters: output = '' aprint(_("Executing postfilter %s on message...") % pfilter.filtername, general.YELLOW) (pretcode, output) = pfilter.filter_mail(msgstr,tmpfile.name) if pretcode == 1: # Matched! npositives += 1 if self.config.allpostfilters and ( npositives != len( self.config.lpostfilters) ): # allpostfilters needs all the postfilters to match a # message for having it filtered: continue must_deliver = False if not self.config.optnotlog: logAgent.append_filtered(indice_log,_("%s (Postfiltered by'%s')")%(fromm,pfilter.filtername),msg['size']) if pfilter.savepostfilteredmails != 0 and not alreadycopied: pfilter.log_filtered_mail(msgstr,fromm, output) alreadycopied = 1 if npositives >= self.config.minpostfilters: break tmpfile.close() os.unlink(tmpfile.name) if must_deliver: deliver(header, msgbody, fromm, smtp) if self.optdelete and retcode != 'wuimap': aprint(_("Deleting message"), general.YELLOW) self.delmesg(msg) except SMTPException, x: print print _("The local SMTP rejected this message so it won't be deleted from the mail server:"), str(x) smtp.quit() smtp.connect() if not self.config.optnotlog: logAgent.incident(indice_log, str(x)) except socket.error, x: print print _("Socket error sending to the local SMTP, it won't be deleted from the mail server:"), str(x) try: smtp.quit() smtp.connect() except AttributeError: pass if not self.config.optnotlog: logAgent.incident(indice_log, str(x)) continue
def show(self): str = '' tmpstr = colorize(_("\n####### Operation summary #######\n\n"), VIOLET) str = ''.join(tmpstr) #FIXME: This should be on local time format (including weekday names) date = time.strftime( ('%a %b %d %H:%M:%S %Y\n'), time.localtime(time.time())) str = ''.join( (str, colorize(date, GREEN)) ) for serv in self.lserv.values(): tmpstr = colorize(_("\n--- Server: "), CYAN) tmpstr2 = colorize("%s\n\n" % serv['name'], RED) str = ''.join( (str, tmpstr, tmpstr2) ) tmpstr = colorize(_("* Total messages: "), CYAN) tmpstr2 = colorize("%s\n" % serv['numtotal'], GREEN) str = ''.join( (str, tmpstr, tmpstr2) ) tmpstr = colorize(_("* Total volume on server: "), CYAN) tmpstr2 = colorize("%.2f %s\n" % (compute(serv['tamtotal'])), GREEN) str = ''.join( (str, tmpstr, tmpstr2) ) tmpstr = colorize(_("* Filtered messages: "), CYAN) tmpstr2 = colorize("%d\n" % serv['numfilt'], GREEN) str = ''.join( (str, tmpstr, tmpstr2) ) if serv['numfilt'] > 0: tmpstr = colorize(_("\tTotal filtered volume: "), CYAN) tmpstr2 = colorize("%.2f %s\n" % compute(serv['filtered_vol']), RED) str = ''.join( (str, tmpstr, tmpstr2) ) tmpstr = colorize(_("* Downloaded messages: "), CYAN) tmpstr2 = colorize("%d\n" % serv['numdesc'], GREEN) str = ''.join( (str, tmpstr, tmpstr2) ) if serv['numdesc'] > 0: tmpstr = colorize(_("\tDownloaded Volume: "), CYAN) tmpstr2 = colorize("%.2f %s\n" % compute(serv['downloaded_vol']), GREEN) str = ''.join( (str, tmpstr, tmpstr2) ) tmpstr = colorize(_("* Postergated messages: "), CYAN) tmpstr2 = colorize("%d\n" % serv['numpost'], GREEN) str = ''.join( (str, tmpstr, tmpstr2) ) if serv['numpost'] > 0: tmpstr = colorize(_("\tPostergated Volume: "), CYAN) tmpstr2 = colorize("%.2f %s\n" % compute(serv['post_vol']),GREEN) str = ''.join( (str,tmpstr,tmpstr2) ) if serv['numfilt'] > 0: tmpstr = colorize(_("\n* Patterns of filtered messages:\n"), CYAN) str = ''.join( (str, tmpstr) ) for pattern in serv['dfilt'].keys(): str = ''.join( (str, colorize("\t%d" % serv['dfilt'][pattern][NUMBER],RED)) ) str = ''.join( (str, colorize(" %s " % pattern, RED))) str = ''.join( (str, colorize("\t\t%.2f %s\n" % compute(serv['dfilt'][pattern][VOLUME]), RED)) ) if serv['numdesc'] > 0: tmpstr = colorize(_("* Origin of downloaded messages:\n"), CYAN) str = ''.join( (str, tmpstr) ) for origin in serv['ddesc'].keys(): str = ''.join( (str, colorize("\t%d" % serv['ddesc'][origin][NUMBER], GREEN))) str = ''.join( (str, colorize(" %s " % origin, GREEN))) str = ''.join( (str, colorize("%.2f %s\n" % compute(serv['ddesc'][origin][VOLUME]), GREEN))) if serv['numpost'] > 0: tmpstr = colorize(_("* Origin of postergated messages:\n"),CYAN) str = ''.join( (str, tmpstr) ) for origin in serv['dpost'].keys(): str = ''.join( (str,colorize("\t%d" % serv['dpost'][origin][NUMBER], GREEN))) str = ''.join( (str, colorize(" %s " % origin, GREEN))) str = ''.join( (str, colorize("%.2f %s\n" % compute(serv['dpost'][origin][VOLUME]),GREEN))) if serv['incidents'] != []: tmpstr = colorize(_("* Incidents:\n"), CYAN) str = ''.join( (str, tmpstr) ) for inc in serv['incidents']: str = ''.join( (str, colorize("\t-%s\n" % inc, RED)) ) print str
def get_messages(self, logAgent, indice_log, is_retry, smtp): nmesg = len(self.lmsg) general.cursor_off() header = '' fromwho = '' if self.delivery == 'mta': deliver = self.deliver_smtp elif self.delivery == 'mbox': deliver = self.deliver_mbox elif self.delivery == 'maildir': deliver = self.deliver_maildir elif self.delivery == 'pipe': deliver = self.deliver_pipe elif self.delivery == 'global': if self.config.optsmtp: deliver = self.deliver_smtp elif self.config.optmbox: deliver = self.deliver_mbox elif self.config.optmaildir: deliver = self.deliver_maildir elif self.config.optsendmail: deliver = self.deliver_pipe num_trans = 0 retcode = 'pass' for msg in self.lmsg[:]: must_deliver = 1 num_trans += 1 (size, unit) = general.compute(msg['size']) if not self.config.optsyslog: aprint('-'* 21, general.GREEN) if self.config.optexpresion: (retcode, header, fromwho) = self.look_message(msg,logAgent,indice_log,nmesg - num_trans,smtp) if retcode == 'delete': #Deleted if self.config.optsmtp: smtp.smtpobj.noop() continue # end if self.config.optexpresion aprint(_("Downloading message %d (%.2f %s), Left: %d") % (msg['number'], size, unit, nmesg - num_trans),general.VIOLET) (msgbody, fromm, tmpheader) = self.get_message(msg, smtp, header, nmesg) if fromm != '' and fromwho == '': fromwho = fromm if tmpheader != '': header = tmpheader if not self.config.optsyslog and not self.config.optquiet: sys.stderr.write(general.colorize(_("\rDownloaded: "),general.YELLOW)) sys.stderr.write(general.colorize("100%\n",general.WHITE)) # If self.config.optexpression we've already looked for the IMAP # DONTDELETETHISMESSAGE messages in the look_header method if not self.config.optexpresion: if msg['number'] == 1: resultWUIMAP = re_wuimap.search(msgbody) resultWUIMAP2 = re_wuimap.search(header) if (resultWUIMAP is not None) or (resultWUIMAP2 is not None): aprint(_("Ignoring message %d: DONTDELETETHISMESSAGE type message") % (msg['number']), general.RED) retcode = 'wuimap' if retcode == 'wuimap': must_deliver = 0 try: if retcode == 'postergate': #Message delayed animailBoxPath = self.config.homedir + '/.animail/animailDelayed' if not os.path.exists(animailBoxPath): os.mkdir(animailBoxPath, 0700) (foo,emailaddress) = rfc822.parseaddr(fromwho) tmpbox = open(animailBoxPath + '/' + emailaddress, 'a+') self.write_mbox(tmpbox, header, msgbody, fromm) else: if retcode != 'wuimap': must_deliver = 1 if (self.config.lpostfilters != []) and (retcode != 'wuimap'): msgstr = self.create_msgstr(header,msgbody) tmpfile = open(tempfile.mktemp(), 'w+') os.chmod(tmpfile.name, 600) tmpfile.write(msgstr) for pfilter in self.config.lpostfilters: output = '' aprint(_("Executing postfilter %s on message...") % pfilter.filtername, general.YELLOW) (pretcode, output) = pfilter.filter_mail(msgstr,tmpfile.name) if pretcode == 1: must_deliver = 0 if not self.config.optnotlog: logAgent.append_filtered(indice_log,_("%s (Postfiltered by'%s')")%(fromm,pfilter.filtername),msg['size']) if pfilter.savepostfilteredmails != 0: pfilter.log_filtered_mail(msgstr,fromm, output) tmpfile.close() os.unlink(tmpfile.name) if must_deliver: deliver(header, msgbody, fromm, smtp) if self.optdelete and retcode != 'wuimap': aprint(_("Deleting message"), general.YELLOW) self.delmesg(msg) except SMTPException, x: print print _("The local SMTP rejected this message so it won't be deleted from the mail server:"), str(x) smtp.quit() smtp.connect() if not self.config.optnotlog: logAgent.incident(indice_log, str(x)) except socket.error, x: print print _("Socket error sending to the local SMTP, it won't be deleted from the mail server:"), str(x) try: smtp.quit() smtp.connect() except AttributeError: pass if not self.config.optnotlog: logAgent.incident(indice_log, str(x)) continue