def extract_mime_attachements \ ( msg, directory , def_ext = None , ignore = _is_false , main_type = _is_false , name_pat = _is_false , part_indices = None , sub_type = _is_false ) : if def_ext and not def_ext.startswith (".") : def_ext = "." + def_ext pis = part_indices if part_indices else range (len (msg.parts)) for i in pis : p = msg.parts [i] specified = (not ignore (p.content_type)) and \ ( main_type (p.main_type) or name_pat (p.filename) or sub_type (p.sub_type) ) if specified : name = p.filename or ("%s_%s" % (msg.name, i)) fn = Filename (directory, name) if def_ext : fn = Filename (def_ext, fn) print (" Saving", name) PMA.save (fn, p.body) elif p.main_type in ("multipart", "message") : extract_mime_attachements \ ( p, directory , def_ext, ignore, main_type, name_pat, part_indices, sub_type )
def message_from_file(filename, parser=None): if parser is None: parser = Lib.Parser() with open(filename, "rb") as fp: email = parser.parse(fp) email._pma_parsed_body = True return PMA.Message(email, sos.path.split(filename)[-1])
def download_pop (self, server, mdir) : md_name = PMA._Mailbox_.md_name result = 0 for msg_no, msg_size in self.pop_list (server) : email = self._new_email ("\n".join (server.retr (msg_no) [1])) name = md_name () tname = sos.path.join (mdir.path, "tmp", name) nname = sos.path.join (mdir.path, "new", name) PMA.save (tname, email.as_string ()) sos.link (tname, nname) sos.unlink (tname) server.dele (msg_no) result += 1 if self.finish : break return result
def __init__ (self, path, name = None, prefix = None, root = None) : if name is None : name = sos.path.split (path) [-1] if prefix is None : qname = name else : qname = self.name_sep.join ((prefix, name)) self.name = name self.qname = qname self.path = path self.root = root or self self._box_dict = {} self.change_list = [] self._messages = None self._msg_dict = {} self._ccount = TFL.Observed_Value (0, mailbox = self) self._chg_list_idx = {} self.unsynced = TFL.Observed_Value (0, mailbox = self) if qname not in self._Table : self._Table [qname] = self else : raise KeyError \ ( "Duplicate mailbox name %s <-> %s" % (self.path, self._Table [qname].path) ) self.status = status = PMA.Box_Status (self) if self.supports_status : self.status_fn = fn = sos.path.join (path, ".status") status.load (fn)
def _formatted (self, format, msg = None) : replacer = self.formatted_replacers_sb \ if self.short_body else self.formatted_replacers mapping = PMA.Msg_Scope (msg, self.locals, self.defaults) result = pyk.decoded (format, PMA.default_encoding) % mapping result = replacer (result) return result
def __init__(self, name, matcher, mailbox, prefix=None, **ckw): self.mailboxes = (mailbox, ) self.__super.__init__ \ ( name = name , prefix = mailbox.qname , root = mailbox.root ) if not isinstance(matcher, PMA._Matcher_): matcher = PMA.Matcher(matcher, **ckw) self._matcher = matcher
def __init__(self, email, name=None, mailbox=None, number=None): self.__super.__init__(email, name) self.mailbox = mailbox self.number = number self.status = None if mailbox: self.pending = _Pending_Action_(self) if mailbox.supports_status: mdn = mailbox.md_name(self) self.status = PMA.Msg_Status.new(mdn) if self.status is None: self.status = PMA.Msg_Status()
def messages_from_args (args, base_dirs) : parser = Lib.Parser () for arg in args : matches = tuple \ ( TFL.CAO.Rel_Path.resolved_paths (base_dirs, arg, single_match = False, skip_missing = True) ) if not matches and sos.path.exists (arg) : matches = [arg] if len (matches) == 1 : yield PMA.message_from_file (matches [0], parser) elif matches : print \ ( "Multiple matches for %r: choose one of\n %s" % (arg, "\n ".join (matches)) ) else : tail = ("in\n %s" % "\n ".join (base_dirs)) \ if base_dirs else "" print ("No match found for %r%s" % (arg, tail))
def messages_from_args(args, base_dirs): parser = Lib.Parser() for arg in args: matches = tuple \ ( TFL.CAO.Rel_Path.resolved_paths (base_dirs, arg, single_match = False, skip_missing = True) ) if not matches and sos.path.exists(arg): matches = [arg] if len(matches) == 1: yield PMA.message_from_file(matches[0], parser) elif matches: print \ ( "Multiple matches for %r: choose one of\n %s" % (arg, "\n ".join (matches)) ) else: tail = ("in\n %s" % "\n ".join (base_dirs)) \ if base_dirs else "" print("No match found for %r%s" % (arg, tail))
def _main (cmd) : msg_base_dirs = cmd.msg_base_dirs or PMA.msg_base_dirs directory = cmd.directory if directory and not directory.endswith (sos.sep) : directory = directory + sos.sep c_type = cmd.content_type def_ext = cmd.extension_default ignore = cmd.ignore or None name_pat = cmd.name_pat or None parts = cmd.Parts s_type = cmd.sub_type if not cmd.regexp : if c_type : c_type = re.escape (c_type) if s_type : s_type = re.escape (s_type) c_type = Re_Filter (c_type) if c_type else _is_false s_type = Re_Filter (s_type) if s_type else _is_false ignore = Re_Filter (ignore) if ignore else _is_false name_pat = Re_Filter (name_pat) if name_pat else _is_false for msg in PMA.messages_from_args (cmd.argv, msg_base_dirs) : print (msg.path) extract_mime_attachements \ (msg, directory, def_ext, ignore, c_type, name_pat, parts, s_type)
print (a2ps) a2ps () else : pyk.fprint (pyk.decoded (txt, encoding)) # end def _main _Command = TFL.CAO.Cmd \ ( handler = _main , args = ( "message:S?Message to print" , ) , opts = ( "-body_only:B?Restrict output to body of message" , "-msg_base_dirs:Q:?Base directories for searching `message`" , "-Print:B?Print the message(s)" , "-printer_name:S=lp?Name of printer to print to" , "-verbose:B" , TFL.CAO.Opt.Output_Encoding () ) , description = "Format mail messages for viewing and printing" ) if __name__ != "__main__" : PMA._Export ("*") else : import _PMA.Message PMA.load_user_config () _PMA.Message._Command () ### __END__ PMA.Message
caps = Lib.mailcap.getcaps () self.caps = caps self.mct = {} # end def __init__ def mime_type (self, name) : mct = self.mct result = mct.get (name) if name not in mct : if name in self.caps : result = Mailcap_Type (name, self.caps [name]) else : main_type, sub_type = name.split ("/") if sub_type != "*" : result = self.mime_type ("/".join ((main_type, "*"))) mct [name] = result return result # end def mime_type def __getitem__ (self, key) : return self.mime_type (key) # end def __getitem__ # end class _Mailcap_ Mailcap = _Mailcap_ () if __name__ != "__main__" : PMA._Export ("*", "Mailcap") ### __END__ PMA.Mailcap
from email.generator import * from email.header import * from email.message import * from email.mime.audio import MIMEAudio from email.mime.base import MIMEBase from email.mime.image import MIMEImage from email.mime.message import MIMEMessage from email.mime.multipart import MIMEMultipart from email.mime.nonmultipart import MIMENonMultipart from email.mime.text import MIMEText from email.parser import * from email.utils import * from email.utils import mktime_tz import mailbox import mailcap try : mailbox.UnixMailbox except AttributeError : mailbox.UnixMailbox = mailbox.mbox try : mailbox.MHMailbox except AttributeError : mailbox.MHMailbox = mailbox.MH if __name__ != "__main__" : PMA._Export_Module () ### __END__ PMA.Lib
def _new_message (self, m) : return PMA.Message (email = m, name = m._pma_path, mailbox = self)
# -*- coding: utf-8 -*- # Copyright (C) 2004 Mag. Christian Tanzer. All rights reserved # Glasauergasse 32, A--1130 Wien, Austria. [email protected] # **************************************************************************** # # This module is licensed under the terms of the BSD 3-Clause License # <http://www.c-tanzer.at/license/bsd_3c.html>. # **************************************************************************** # #++ # Name # PMA.SCM.__init__ # # Purpose # PMA change tracker` # # Revision Dates # 15-Jan-2006 (MG) Creation # ««revision-date»»··· #-- from _TFL.Package_Namespace import Package_Namespace from _PMA import PMA SCM = Package_Namespace () PMA._Export ("SCM") del Package_Namespace ### __END__ PMA.SCM.__init__
s = self._new_subbox (sos.path.join (self.path, b.name)) s.add_messages (* b.messages) if transitive : for sb in pyk.itervalues (b._box_dict) : s.add_subbox (sb, transitive) self.change_list.append (PMA.SCM.Add_Subbox (s)) self.change_count += 1 return s # end def add_subbox def import_from_mailbox (self, mailbox, transitive = False) : self.add_messages (* mailbox.messages) if transitive : for b in pyk.itervalues (mailbox._box_dict) : self.add_subbox (b, transitive) # end def import_from_mailbox @classmethod def md_name (cls, message = None) : if message is None : return super (Mailbox, cls).md_name () else : return message.name # end def md_name # end class Mailbox if __name__ != "__main__" : PMA._Export ("*", "_Mailbox_", "_Mailbox_in_Dir_", "_Mailbox_in_Dir_S_") ### __END__ PMA.Mailbox
self.__super.__init__ \ ( name = name , prefix = prefix ) # end def __init__ def _eligible (self, messages) : return messages # end def _eligible # end class V_Mailbox """ from _PMA import PMA import _PMA.Mailbox import _PMA.V_Mailbox import _PMA.Matcher mbi = PMA.Maildir ("/home/glueck/PMA/D/inbox") mb1 = PMA.Mailbox ("/home/glueck/PMA/TTTech/planung") mb2 = PMA.Mailbox ("/home/glueck/PMA/TTTech/BIKA") mbs = PMA.MH_Mailbox ("/home/glueck/work/MH/Installscript") vmb = PMA.V_Mailbox ("f1", (mb1, mb2)) vmb.messages m = mbs.messages [58] """ if __name__ != "__main__" : PMA._Export ("V_Mailbox", "_V_Mailbox_") ### __END__ PMA.V_Mailbox
pyk.pickle.dump (cls._Table, f, pyk.pickle_protocol) # end def save # end class _Status_C_ class _Status_I_ (_Status_) : def load (self, filename) : try : with open (filename, "rb") as f : try : attrs = pyk.pickle.load (f) except EOFError : pass else : self._set_attr (** attrs) except IOError : pass # end def load def save (self, filename) : with open (filename, "wb") as f : pyk.pickle.dump (self._attr, f, pyk.pickle_protocol) # end def save # end class _Status_I_ if __name__ != "__main__" : PMA._Export ("_Status_", "_Status_C_", "_Status_I_") ### __END__ PMA._Status_
def load_status(self, path): self.status = stat = PMA.Off_Status(self) self.status_fn = stfn = sos.path.join(path, ".status") self.msg_status_fn = msfn = sos.path.join(path, ".msg.status") stat.load(stfn) PMA.Msg_Status.load(msfn)
# -*- coding: utf-8 -*- # Copyright (C) 2004 Mag. Christian Tanzer. All rights reserved # Glasauergasse 32, A--1130 Wien, Austria. [email protected] # **************************************************************************** # # This module is licensed under the terms of the BSD 3-Clause License # <http://www.c-tanzer.at/license/bsd_3c.html>. # **************************************************************************** # #++ # Name # PMA.SCM.__init__ # # Purpose # PMA change tracker` # # Revision Dates # 15-Jan-2006 (MG) Creation # ««revision-date»»··· #-- from _TFL.Package_Namespace import Package_Namespace from _PMA import PMA SCM = Package_Namespace() PMA._Export("SCM") del Package_Namespace ### __END__ PMA.SCM.__init__
def __init__ (self, matcher) : self.matcher = matcher # end def __init__ def match (self, msg) : return not self.matcher.match (msg) # end def match def __repr__ (self) : return "not (%s)" % (self.matcher, ) # end def __repr__ # end class Not_Matcher class Or_Matcher (And_Matcher) : """Or-combination of Matcher instances""" def match (self, msg) : for m in self.matchers : if m.match (msg) : return True return False # end def match # end class Or_Matcher if __name__ != "__main__" : PMA._Export ("*", "_Matcher_") ### __END__ PMA.Matcher
@_trainer def untrain_spam (self, msg) : """Un-Train classifier with spam `msg`""" self.hammie.untrain_spam (msg) # end def untrain_spam def _close (self) : if self.hammie is not None and self.mode != "r" : self._save () self.hammie = None # end def _close def _open (self, mode = "c") : if self.hammie is None or self.mode != mode : self.mode = mode self.hammie = sb.hammie.open (self.dbname, self.usedb, mode) # end def _open def _save (self) : self.hammie.store () # end def _save # end class _SB_ SB = _SB_ () if __name__ != "__main__" : PMA._Export ("SB") ### __END__ PMA.SB
POP3_class = poplib.POP3 def __init__ ( self, path, host, user , passwd = None , prefix = None , root = None , port = 110 , poll_interval = 60 ### in seconds ) : self.__super.__init__ (path, prefix, root) self.poller = Pop3_Poller \ (self, host, user, passwd, port, poll_interval, auto_start = True) # end def __init__ def passwd_cb (self) : raise NotImplemented ("Must pass passwd to Pop3_Maildir") # end def passwd_cb # end class Pop3_Maildir class Pop3_Maildir_SSL (Pop3_Maildir) : """Model a Pop3_Maildir using a SSL conection to the POP server.""" POP3_class = poplib.POP3_SSL # end class Pop3_Maildir_SSL if __name__ != "__main__" : PMA._Export ("*") ### __END__ PMA.Pop3_Maildir
def _storage_boxes(self, path, da): return \ [ PMA.Mailbox (sa) for sa in subdirs (path) if sa != da if not sos.path.split (sa) [-1].startswith (".") ]
, "-domain:S?Domain of sender" , "-editor:S?Command used to start editor" , "-forward:S?Message to forward" , "-HTML:B?Convert ReST message to HTML" , "-mail_host:S?Name of SMTP server to use" , "-mail_local_hostname:S?Name of host sending the email" , "-mail_port:I=25?Number of port of SMTP server to use" , "-mail_user:S?User name for login into SMTP server" , "-mail_word:S?Password for login into SMTP server" , "-msg_base_dirs:Q:?Base directories for searching `message`" , "-reply:S?Message to reply to" , "-Reply_all:S?Message to reply to" , "-Short_body:U" "?Don't start an editor, use supplied value as body instead" , "-subject:U?Subject of email" , "-tls:B?Use SMTP in TLS (Transport Layer Security) mode." , "-To:U?Email of receiver" , "-user:U?Name of sender" ) , description = "Send mail message (newly composed or reply to existing email)" ) if __name__ != "__main__" : PMA._Export ("*") else : import _PMA.Composer PMA.load_user_config () _PMA.Composer._Command () ### __END__ PMA.Composer
def __init__ (self, mail_host = None, ** kw) : self.smtp = self.SMTP (mail_host = mail_host, ** kw) # end def __init__ def __call__ (self, email, envelope = None) : self.smtp.send_message (email, envelope) # end def __call__ def send (self, from_addr, to_addrs, msg, mail_opts = None, rcpt_opts = None) : self.smtp.send (from_addr, to_addrs, msg, mail_opts, rcpt_opts) # end def send # end class Sender class Sender_Logger (Sender) : SMTP = TFL.SMTP_Logger # end class Sender_Logger class Sender_Tester (Sender) : SMTP = TFL.SMTP_Tester # end class Sender_Tester if __name__ != "__main__" : PMA._Export ("*") ### __END__ PMA.Sender
def message_from_arg (cmd, arg) : try : return first (PMA.messages_from_args ([arg], cmd.msg_base_dirs)) except LookupError : raise SystemExit (1)
, prefix = prefix ) # end def __init__ def _eligible(self, messages): return messages # end def _eligible # end class V_Mailbox """ from _PMA import PMA import _PMA.Mailbox import _PMA.V_Mailbox import _PMA.Matcher mbi = PMA.Maildir ("/home/glueck/PMA/D/inbox") mb1 = PMA.Mailbox ("/home/glueck/PMA/TTTech/planung") mb2 = PMA.Mailbox ("/home/glueck/PMA/TTTech/BIKA") mbs = PMA.MH_Mailbox ("/home/glueck/work/MH/Installscript") vmb = PMA.V_Mailbox ("f1", (mb1, mb2)) vmb.messages m = mbs.messages [58] """ if __name__ != "__main__": PMA._Export("V_Mailbox", "_V_Mailbox_") ### __END__ PMA.V_Mailbox