^Rc@sdZdgZddlZddlZddlZddlZddlmZddlZ ddl m Z ddl m Z dZ ej dZd Zded Zd Zd Zddd YZdS(s8Basic message object for the email package object model.tMessageiN(tStringIO(tutils(terrorss; s[ \(\)<>@,;:\\"/\[\]\?=]cCsD|jd\}}}|s.|jdfS|j|jfS(Nt;(t partitiontstriptNone(tparamtatseptb((s#/usr/lib/python2.7/email/message.pyt _splitparamscCs|dk rt|dkrt|tr[|d7}tj|d|d|d}n|sptj|rd|tj|fSd||fSn|SdS(sConvenience function to format and return a key=value pair. This will quote the value if needed or if quote is true. If value is a three tuple (charset, language, value), it will be encoded according to RFC2231 rules. it*iis%s="%s"s%s=%sN( Rtlent isinstancettupleRtencode_rfc2231t tspecialstsearchtquote(RtvalueR((s#/usr/lib/python2.7/email/message.pyt _formatparam&s $cCsg}x|d dkr|d}|jd}xR|dkr|jdd||jdd|dr|jd|d}q5W|dkrt|}n|| }d|kr|jd}|| jjd||dj}n|j|j||}q W|S(NiRit"s\"it=(tfindtcountRtindexRtlowertappend(tstplisttendtfti((s#/usr/lib/python2.7/email/message.pyt _parseparam>s ;   /cCsBt|tr1|d|dtj|dfStj|SdS(Niii(RRRtunquote(R((s#/usr/lib/python2.7/email/message.pyt _unquotevaluePs"cBseZdZdZdZedZdZdZdZ dZ d.edZ d.d Z d Zd Zd Zd ZdZdZdZdZdZdZdZd.dZd.dZdZdZdZdZdZdZ dZ!dZ"d.de#d Z$d.de#d!Z%d"e#d.d#d$Z&de#d%Z'd"e#d&Z(d.d'Z)d.d(Z*d)Z+d.d*Z,d.d+Z-d,d-l.m/Z/RS(/sBasic message object. A message object is defined as something that has a bunch of RFC 2822 headers and a payload. It may optionally have an envelope header (a.k.a. Unix-From or From_ header). If the message is a container (i.e. a multipart or a message/rfc822), then the payload is a list of Message objects, otherwise it is a string. Message objects implement part of the `mapping' interface, which assumes there is exactly one occurrence of the header per message. Some headers do in fact appear multiple times (e.g. Received) and for those headers, you must use the explicit API to set or get all the headers. Not all of the mapping methods are implemented. cCsJg|_d|_d|_d|_d|_|_g|_d|_dS(Ns text/plain( t_headersRt _unixfromt_payloadt_charsettpreambletepiloguetdefectst _default_type(tself((s#/usr/lib/python2.7/email/message.pyt__init__ks     cCs|jdtS(swReturn the entire formatted message as a string. This includes the headers, body, and envelope header. tunixfrom(t as_stringtTrue(R.((s#/usr/lib/python2.7/email/message.pyt__str__vscCsBddlm}t}||}|j|d||jS(sReturn the entire formatted message as a string. Optional `unixfrom' when True, means include the Unix From_ envelope header. This is a convenience method and may not generate the message exactly as you intend because by default it mangles lines that begin with "From ". For more flexibility, use the flatten() method of a Generator instance. i(t GeneratorR0(temail.generatorR4Rtflattentgetvalue(R.R0R4tfptg((s#/usr/lib/python2.7/email/message.pyR1|s   cCst|jtS(s6Return True if the message consists of multiple parts.(RR(tlist(R.((s#/usr/lib/python2.7/email/message.pyt is_multipartscCs ||_dS(N(R'(R.R0((s#/usr/lib/python2.7/email/message.pyt set_unixfromscCs|jS(N(R'(R.((s#/usr/lib/python2.7/email/message.pyt get_unixfromscCs2|jdkr|g|_n|jj|dS(sAdd the given payload to the current payload. The current payload will always be a list of objects after this method is called. If you want to set the payload to a scalar object, use set_payload() instead. N(R(RR(R.tpayload((s#/usr/lib/python2.7/email/message.pytattachscCs9|d kr|j}n;t|jtsFtdt|jn |j|}|r5|jrid S|jddj}|dkrt j |S|dkryt j |SWq2t j k r|SXq5|d kr5t}y0tjt|d |d t|j}Wq2tj k r.|SXq5n|S(sZReturn a reference to the payload. The payload will either be a list object or a string. If you mutate the list object, you modify the message's payload in place. Optional i returns that index into the payload. Optional decode is a flag indicating whether the payload should be decoded or not, according to the Content-Transfer-Encoding header (default is False). When True and the message is not a multipart, the payload will be decoded if this header's value is `quoted-printable' or `base64'. If some other encoding is used, or the header is missing, or if the payload has bogus data (i.e. bogus base64 or uuencoded data), the payload is returned as-is. If the message is a multipart and the decode flag is True, then None is returned. sExpected list, got %sscontent-transfer-encodingtsquoted-printabletbase64s x-uuencodetuuencodetuuesx-uues tquietN(s x-uuencodeRBRCsx-uue(RR(RR:t TypeErrorttypeR;tgetRRt_qdecodet_bdecodetbinasciitErrorRtuutdecodeR2R7(R.R"RMR>tctetsfp((s#/usr/lib/python2.7/email/message.pyt get_payloads0           cCs)||_|dk r%|j|ndS(sSet the payload to the given value. Optional charset sets the message's default character set. See set_charset() for details. N(R(Rt set_charset(R.R>tcharset((s#/usr/lib/python2.7/email/message.pyt set_payloads  cCs|dkr&|jdd|_dSt|trJtjj|}nt|tjjsnt|n||_d|kr|j ddnd|kr|j ddd|j n|j d|j t|j t r|j j|j|_ nt||j kr4|j|j |_ nd|kr|j}y||Wqtk r|j|j |_ |j d|qXndS(sSet the charset of the payload to a given character set. charset can be a Charset instance, a string naming a character set, or None. If it is a string it will be converted to a Charset instance. If charset is None, the charset parameter will be removed from the Content-Type field. Anything else will generate a TypeError. The message will be assumed to be of type text/* encoded with charset.input_charset. It will be converted to charset.output_charset and encoded properly, if needed, when generating the plain text representation of the message. MIME headers (MIME-Version, Content-Type, Content-Transfer-Encoding) will be added as needed. RRNs MIME-Versions1.0s Content-Types text/plainsContent-Transfer-Encoding(Rt del_paramR)Rt basestringtemailRRtCharsetREt add_headertget_output_charsett set_paramR(tunicodetencodetoutput_charsettstrt body_encodetget_body_encoding(R.RRRN((s#/usr/lib/python2.7/email/message.pyRQs4         cCs|jS(sKReturn the Charset instance associated with the message's payload. (R)(R.((s#/usr/lib/python2.7/email/message.pyt get_charsetscCs t|jS(s9Return the total number of headers, including duplicates.(RR&(R.((s#/usr/lib/python2.7/email/message.pyt__len__scCs |j|S(s-Get a header value. Return None if the header is missing instead of raising an exception. Note that if the header appeared multiple times, exactly which occurrence gets returned is undefined. Use get_all() to get all the values matching a header field name. (RG(R.tname((s#/usr/lib/python2.7/email/message.pyt __getitem__s cCs|jj||fdS(sSet the value of a header. Note: this does not overwrite an existing header with the same field name. Use __delitem__() first to delete any existing headers. N(R&R(R.Rctval((s#/usr/lib/python2.7/email/message.pyt __setitem__(scCsa|j}g}x?|jD]4\}}|j|kr|j||fqqW||_dS(swDelete all occurrences of a header, if present. Does not raise an exception if the header is missing. N(RR&R(R.Rct newheaderstktv((s#/usr/lib/python2.7/email/message.pyt __delitem__0s  cCs2|jg|jD]\}}|j^qkS(N(RR&(R.RcRhRi((s#/usr/lib/python2.7/email/message.pyt __contains__<scCst}|j|||k S(s/Return true if the message contains the header.(tobjectRG(R.Rctmissing((s#/usr/lib/python2.7/email/message.pythas_key?s cCs g|jD]\}}|^q S(s.Return a list of all the message's header field names. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. (R&(R.RhRi((s#/usr/lib/python2.7/email/message.pytkeysDscCs g|jD]\}}|^q S(s)Return a list of all the message's header values. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. (R&(R.RhRi((s#/usr/lib/python2.7/email/message.pytvaluesNscCs|jS(s'Get all the message's header fields and values. These will be sorted in the order they appeared in the original message, or were added to the message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. (R&(R.((s#/usr/lib/python2.7/email/message.pytitemsXscCs@|j}x-|jD]"\}}|j|kr|SqW|S(s~Get a header value. Like __getitem__() but return failobj instead of None when the field is missing. (RR&(R.RctfailobjRhRi((s#/usr/lib/python2.7/email/message.pyRGbs  cCs\g}|j}x9|jD].\}}|j|kr|j|qqW|sX|S|S(sQReturn a list of all the values for the named field. These will be sorted in the order they appeared in the original message, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. If no such fields exist, failobj is returned (defaults to None). (RR&R(R.RcRrRpRhRi((s#/usr/lib/python2.7/email/message.pytget_allrs  cKsg}xd|jD]V\}}|dkrG|j|jddq|jt|jdd|qW|dk r|jd|n|jj|tj|fdS(sExtended header setting. name is the header field to add. keyword arguments can be used to set additional parameters for the header field, with underscores converted to dashes. Normally the parameter will be added as key="value" unless value is None, in which case only the key will be added. If a parameter value contains non-ASCII characters it must be specified as a three-tuple of (charset, language, value), in which case it will be encoded according to RFC2231 rules. Example: msg.add_header('content-disposition', 'attachment', filename='bud.gif') t_t-iN( RqRRtreplaceRtinsertR&t SEMISPACEtjoin(R.t_namet_valuet_paramstpartsRhRi((s#/usr/lib/python2.7/email/message.pyRXs & cCs}|j}xjttt|j|jD];\}\}}|j|kr.||f|j|Remove the given parameter completely from the Content-Type header. The header will be re-written in place without the parameter or its value. All values will be quoted as necessary unless requote is False. Optional header specifies an alternative to the Content-Type header. NR@RR$(RRRRxRyRG(R.RRRt new_ctypeRRi((s#/usr/lib/python2.7/email/message.pyRTks % cCs|jddkstn|jdkrD|d=d|dt}|jd|}||kr+|Stj|jS(sReturn the boundary associated with the payload if present. The boundary is extracted from the Content-Type header's `boundary' parameter, and it is unquoted. tboundary(RlRRRtrstrip(R.RrRmR((s#/usr/lib/python2.7/email/message.pyt get_boundarys   c Cst}|j|d}||kr9tjdng}t}xY|D]Q\}}|jdkr|jdd|ft}qL|j||fqLW|s|jdd|fng}x|jD]\} } | jdkr^g} xG|D]?\} } | dkr$| j| q| jd| | fqW|j| t j | fq|j| | fqW||_dS(sSet the boundary parameter in Content-Type to 'boundary'. This is subtly different than deleting the Content-Type header and adding a new one with a new boundary parameter via add_header(). The main difference is that using the set_boundary() method preserves the order of the Content-Type header in the original message. HeaderParseError is raised if the message has no Content-Type header. s content-typesNo Content-Type header foundRs"%s"R@s%s=%sN( RlRRtHeaderParseErrortFalseRRR2R&RxRy( R.RRmRt newparamstfoundptpktpvRgthRiR}Rh((s#/usr/lib/python2.7/email/message.pyt set_boundarys0    cCst}|jd|}||kr+|St|tr|dpGd}y t|d|jd}Wqttfk r|d}qXny4t|trt|d}n|jd}Wntk r|SX|j S(sReturn the charset parameter of the Content-Type header. The returned string is always coerced to lower case. If there is no Content-Type header, or if that header has no charset parameter, failobj is returned. RRisus-asciii( RlRRRR[R\t LookupErrort UnicodeErrorR^R(R.RrRmRRtpcharset((s#/usr/lib/python2.7/email/message.pytget_content_charsets"    cCs&g|jD]}|j|^q S(sReturn a list containing the charset(s) used in this message. The returned list of items describes the Content-Type headers' charset parameter for this message and all the subparts in its payload. Each item will either be a string (the value of the charset parameter in the Content-Type header of that part) or the value of the 'failobj' parameter (defaults to None), if the part does not have a main MIME type of "text", or the charset is not defined. The list will contain one string for each part of the message, plus one for the container message (i.e. self), so that a non-multipart message will still return a list of length 1. (twalkR(R.Rrtpart((s#/usr/lib/python2.7/email/message.pyt get_charsets si(RN(0t__name__t __module__t__doc__R/R3RR1R;R<R=R?RRPRSRQRaRbRdRfRjRkRnRoRpRqRGRsRXRRRRRRRR2RRRZRTRRRRRRtemail.iteratorsR(((s#/usr/lib/python2.7/email/message.pyR\sX      2 -            #/  -  ((Rt__all__treRLRJtwarningst cStringIORt email.charsetRVRRRxtcompileRR RR2RR#R%R(((s#/usr/lib/python2.7/email/message.pyts