ž ¦ÿfR‹c@sdZdgZddlZddlZddlZddlZddlmZmZddl m Z ddl m Z ddl m Z ddl mZdd lmZejZd Zejd ƒZd d „Zdddd„Zdd„Zdd„ZGdd„dƒZdS(u8Basic message object for the email package object model.uMessageiN(uBytesIOuStringIO(uutils(uerrors(ucompat32(ucharset(udecode_bu; u[ \(\)<>@,;:\\"/\[\]\?=]cCsJt|ƒjdƒ\}}}|s4|jƒdfS|jƒ|jƒfS(Nu;(ustru partitionustripuNone(uparamuausepub((u2/opt/alt/python33/lib64/python3.3/email/message.pyu _splitparamsu _splitparamcCsþ|d k röt|ƒdkröt|tƒrf|d7}tj|d|d|dƒ}d||fSy|jdƒWn?tk r¸|d7}tj|ddƒ}d||fSYnX|sÎtj |ƒråd |tj |ƒfSd||fSn|Sd S( u~Convenience 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. If it contains non-ascii characters it will likewise be encoded according to RFC2231 rules, using the utf-8 charset and a null language. iu*iiu%s=%suasciiuutf-8uu%s="%s"N( uNoneulenu isinstanceutupleuutilsuencode_rfc2231uencodeuUnicodeEncodeErroru tspecialsusearchuquote(uparamuvalueuquote((u2/opt/alt/python33/lib64/python3.3/email/message.pyu _formatparam(s  !  u _formatparamcCsOdt|ƒ}g}x2|dd…dkrJ|dd…}|jdƒ}xR|dkr¢|jdd|ƒ|jdd|ƒdr¢|jd|dƒ}qQW|dkr¾t|ƒ}n|d|…}d|kr$|jdƒ}|d|…jƒjƒd||dd…jƒ}n|j|jƒƒ||d…}qW|S(Nu;iiu"u\"iu=(ustrufinducountulenuindexustripuloweruappend(usuplistuendufui((u2/opt/alt/python33/lib64/python3.3/email/message.pyu _parseparamJs ;  ;u _parseparamcCsBt|tƒr1|d|dtj|dƒfStj|ƒSdS(Niii(u isinstanceutupleuutilsuunquote(uvalue((u2/opt/alt/python33/lib64/python3.3/email/message.pyu _unquotevalue^s"u _unquotevaluecBsp|EeZdZdZedd„Zdd„Zd\ddd„Zd d „Z d d „Z d d„Z dd„Z d[d\dd„Zd[dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd[d+d,„Zd-d.„Zd/d0„Zd[d1d2„Zd3d4„Zd5d6„Z d7d8„Z!d9d:„Z"d;d<„Z#d=d>„Z$d?d@„Z%dAdB„Z&d[dCd]dDdE„Z(d[dCd]dFdG„Z)dHd]d[dIdJdK„Z*dCd]dLdM„Z+dHd]dNdO„Z,d[dPdQ„Z-d[dRdS„Z.dTdU„Z/d[dVdW„Z0d[dXdY„Z1ddZl2m3Z3d[S(^uMessageuÈBasic 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. cCsS||_g|_d|_d|_d|_d|_|_g|_d|_ dS(Nu text/plain( upolicyu_headersuNoneu _unixfromu_payloadu_charsetupreambleuepilogueudefectsu _default_type(uselfupolicy((u2/opt/alt/python33/lib64/python3.3/email/message.pyu__init__ys      uMessage.__init__cCs |jƒS(uwReturn the entire formatted message as a string. This includes the headers, body, and envelope header. (u as_string(uself((u2/opt/alt/python33/lib64/python3.3/email/message.pyu__str__…suMessage.__str__icCsNddlm}tƒ}||ddd|ƒ}|j|d|ƒ|jƒS(uMReturn 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. For more flexibility, use the flatten() method of a Generator instance. i(u Generatoru mangle_from_u maxheaderlenuunixfromF(uemail.generatoru GeneratoruStringIOuFalseuflattenugetvalue(uselfuunixfromu maxheaderlenu Generatorufpug((u2/opt/alt/python33/lib64/python3.3/email/message.pyu as_string‹s  uMessage.as_stringcCst|jtƒS(u6Return True if the message consists of multiple parts.(u isinstanceu_payloadulist(uself((u2/opt/alt/python33/lib64/python3.3/email/message.pyu is_multipartšsuMessage.is_multipartcCs ||_dS(N(u _unixfrom(uselfuunixfrom((u2/opt/alt/python33/lib64/python3.3/email/message.pyu set_unixfrom¡suMessage.set_unixfromcCs|jS(N(u _unixfrom(uself((u2/opt/alt/python33/lib64/python3.3/email/message.pyu get_unixfrom¤suMessage.get_unixfromcCs2|jdkr|g|_n|jj|ƒdS(uðAdd 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(u_payloaduNoneuappend(uselfupayload((u2/opt/alt/python33/lib64/python3.3/email/message.pyuattachªsuMessage.attachc CsT|jƒr7|rdS|dkr)|jS|j|Sn|dk rrt|jtƒ rrtdt|jƒƒ‚n|j}t|jddƒƒj ƒ}t|tƒrat j |ƒr|j ddƒ}|s^y"|j |jddƒdƒ}Wqtk r|j ddƒ}YqXq^qa|ray|j dƒ}Wq^tk rZ|j dƒ}Yq^Xqan|sk|S|d kr„t j|ƒS|d krÙtd j|jƒƒƒ\}}x!|D]}|jj||ƒq¸W|S|dkr=t|ƒ} tƒ} y$tj | | ddƒ| jƒSWq=tjk r9|SYq=Xnt|tƒrP|S|S(uZReturn 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. uExpected list, got %sucontent-transfer-encodinguuasciiusurrogateescapeucharsetureplaceuraw-unicode-escapeuquoted-printableubase64su x-uuencodeuuuencodeuuueux-uueuquietN(u x-uuencodeuuuencodeuuueux-uueT(u is_multipartuNoneu_payloadu isinstanceulistu TypeErrorutypeustrugetuloweruutilsu_has_surrogatesuencodeudecodeu get_paramu LookupErroru UnicodeErroru_qdecodeudecode_bujoinu splitlinesupolicyu handle_defectuBytesIOuuuuTrueugetvalueuError( uselfuiudecodeupayloaducteubpayloaduvalueudefectsudefectuin_fileuout_file((u2/opt/alt/python33/lib64/python3.3/email/message.pyu get_payload¶sV"   "     !     uMessage.get_payloadcCs«t|dƒr[|dkr(||_dSt|tƒsFt|ƒ}n|j|jƒ}nt|dƒr‚|jddƒ|_n ||_|dk r§|j|ƒndS(u˜Set the payload to the given value. Optional charset sets the message's default character set. See set_charset() for details. uencodeNudecodeuasciiusurrogateescape( uhasattruNoneu_payloadu isinstanceuCharsetuencodeuoutput_charsetudecodeu set_charset(uselfupayloaducharset((u2/opt/alt/python33/lib64/python3.3/email/message.pyu set_payloads    uMessage.set_payloadcCsŠ|dkr&|jdƒd|_dSt|tƒsDt|ƒ}n||_d|krl|jddƒnd|kr—|jddd|jƒƒn|jd|jƒƒ||jƒkr×|j|j ƒ|_ nd|kr†|j ƒ}y||ƒWq†t k r‚|j }|r\y|j dd ƒ}Wq\t k rX|j |jƒ}Yq\Xn|j|ƒ|_ |jd|ƒYq†XndS( uÛSet 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. ucharsetNu MIME-Versionu1.0u Content-Typeu text/plainuContent-Transfer-Encodinguasciiusurrogateescape(uNoneu del_paramu_charsetu isinstanceuCharsetu add_headeruget_output_charsetu set_paramu body_encodeu_payloaduget_body_encodingu TypeErroruencodeu UnicodeErroruoutput_charset(uselfucharsetucteupayload((u2/opt/alt/python33/lib64/python3.3/email/message.pyu set_charset$s8           uMessage.set_charsetcCs|jS(uKReturn the Charset instance associated with the message's payload. (u_charset(uself((u2/opt/alt/python33/lib64/python3.3/email/message.pyu get_charsetSsuMessage.get_charsetcCs t|jƒS(u9Return the total number of headers, including duplicates.(ulenu_headers(uself((u2/opt/alt/python33/lib64/python3.3/email/message.pyu__len__[suMessage.__len__cCs |j|ƒS(u-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. (uget(uselfuname((u2/opt/alt/python33/lib64/python3.3/email/message.pyu __getitem___s uMessage.__getitem__cCs°|jj|ƒ}|r|jƒ}d}x`|jD]R\}}|jƒ|kr4|d7}||kr†tdj||ƒƒ‚q†q4q4Wn|jj|jj||ƒƒdS(u¸Set 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. iiu/There may be at most {} {} headers in a messageN(upolicyuheader_max_countuloweru_headersu ValueErroruformatuappenduheader_store_parse(uselfunameuvalu max_countulnameufoundukuv((u2/opt/alt/python33/lib64/python3.3/email/message.pyu __setitem__js    uMessage.__setitem__cCsa|jƒ}g}x?|jD]4\}}|jƒ|kr|j||fƒqqW||_dS(uwDelete all occurrences of a header, if present. Does not raise an exception if the header is missing. N(uloweru_headersuappend(uselfunameu newheadersukuv((u2/opt/alt/python33/lib64/python3.3/email/message.pyu __delitem__|s  uMessage.__delitem__cCs |jƒdd„|jDƒkS(NcSs"g|]\}}|jƒ‘qS((ulower(u.0ukuv((u2/opt/alt/python33/lib64/python3.3/email/message.pyu ‰s u(Message.__contains__..(uloweru_headers(uselfuname((u2/opt/alt/python33/lib64/python3.3/email/message.pyu __contains__ˆsuMessage.__contains__ccs#x|jD]\}}|Vq WdS(N(u_headers(uselfufielduvalue((u2/opt/alt/python33/lib64/python3.3/email/message.pyu__iter__‹suMessage.__iter__cCsdd„|jDƒS(u.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. cSsg|]\}}|‘qS(((u.0ukuv((u2/opt/alt/python33/lib64/python3.3/email/message.pyu —s u Message.keys..(u_headers(uself((u2/opt/alt/python33/lib64/python3.3/email/message.pyukeyssu Message.keyscs‡fdd†ˆjDƒS(u)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. cs+g|]!\}}ˆjj||ƒ‘qS((upolicyuheader_fetch_parse(u.0ukuv(uself(u2/opt/alt/python33/lib64/python3.3/email/message.pyu ¡s u"Message.values..(u_headers(uself((uselfu2/opt/alt/python33/lib64/python3.3/email/message.pyuvalues™suMessage.valuescs‡fdd†ˆjDƒS(u'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. cs1g|]'\}}|ˆjj||ƒf‘qS((upolicyuheader_fetch_parse(u.0ukuv(uself(u2/opt/alt/python33/lib64/python3.3/email/message.pyu ¬s u!Message.items..(u_headers(uself((uselfu2/opt/alt/python33/lib64/python3.3/email/message.pyuitems¤su Message.itemscCsO|jƒ}x<|jD]1\}}|jƒ|kr|jj||ƒSqW|S(u~Get a header value. Like __getitem__() but return failobj instead of None when the field is missing. (uloweru_headersupolicyuheader_fetch_parse(uselfunameufailobjukuv((u2/opt/alt/python33/lib64/python3.3/email/message.pyuget¯s  u Message.getcCs|jj||fƒdS(u‡Store name and value in the model without modification. This is an "internal" API, intended only for use by a parser. N(u_headersuappend(uselfunameuvalue((u2/opt/alt/python33/lib64/python3.3/email/message.pyuset_rawÀsuMessage.set_rawcCst|jjƒƒS(uŽReturn the (name, value) header pairs without modification. This is an "internal" API, intended only for use by a generator. (uiteru_headersucopy(uself((u2/opt/alt/python33/lib64/python3.3/email/message.pyu raw_itemsÇsuMessage.raw_itemscCskg}|jƒ}xH|jD]=\}}|jƒ|kr|j|jj||ƒƒqqW|sg|S|S(uQReturn 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). (uloweru_headersuappendupolicyuheader_fetch_parse(uselfunameufailobjuvaluesukuv((u2/opt/alt/python33/lib64/python3.3/email/message.pyuget_allÒs  #uMessage.get_allcKs£g}xd|jƒD]V\}}|dkrG|j|jddƒƒq|jt|jddƒ|ƒƒqW|dk rŒ|jd|ƒntj|ƒ||Wtj|ƒ}|S(Nu=iu( uobjectugetu _parseparamusplitustripu ValueErroruappenduutilsu decode_params( uselfufailobjuheaderumissinguvalueuparamsupunameuval((u2/opt/alt/python33/lib64/python3.3/email/message.pyu_get_params_preserveQs       uMessage._get_params_preserveu content-typecCsJtƒ}|j||ƒ}||kr+|S|rBdd„|DƒS|SdS(umReturn the message's Content-Type parameters, as a list. The elements of the returned list are 2-tuples of key/value pairs, as split on the `=' sign. The left hand side of the `=' is the key, while the right hand side is the value. If there is no `=' sign in the parameter the value is the empty string. The value is as described in the get_param() method. Optional failobj is the object to return if there is no Content-Type header. Optional header is the header to search instead of Content-Type. If unquote is True, the value is unquoted. cSs(g|]\}}|t|ƒf‘qS((u _unquotevalue(u.0ukuv((u2/opt/alt/python33/lib64/python3.3/email/message.pyu xs u&Message.get_params..N(uobjectu_get_params_preserve(uselfufailobjuheaderuunquoteumissinguparams((u2/opt/alt/python33/lib64/python3.3/email/message.pyu get_paramsfs  uMessage.get_paramscCsf||kr|SxO|j||ƒD];\}}|jƒ|jƒkr#|rWt|ƒS|Sq#q#W|S(u‚Return the parameter value if found in the Content-Type header. Optional failobj is the object to return if there is no Content-Type header, or the Content-Type header has no such parameter. Optional header is the header to search instead of Content-Type. Parameter keys are always compared case insensitively. The return value can either be a string, or a 3-tuple if the parameter was RFC 2231 encoded. When it's a 3-tuple, the elements of the value are of the form (CHARSET, LANGUAGE, VALUE). Note that both CHARSET and LANGUAGE can be None, in which case you should consider VALUE to be encoded in the us-ascii charset. You can usually ignore LANGUAGE. The parameter value (either the returned string, or the VALUE item in the 3-tuple) is always unquoted, unless unquote is set to False. If your application doesn't care whether the parameter was RFC 2231 encoded, it can turn the return value into a string as follows: rawparam = msg.get_param('foo') param = email.utils.collapse_rfc2231_value(rawparam) (u_get_params_preserveuloweru _unquotevalue(uselfuparamufailobjuheaderuunquoteukuv((u2/opt/alt/python33/lib64/python3.3/email/message.pyu get_param|s   uMessage.get_paramu Content-Typeuc Cswt|tƒ r(|r(|||f}n||krO|jƒdkrOd}n|j|ƒ}|j|d|ƒs²|sŽt|||ƒ}qJtj|t|||ƒgƒ}n˜d}x|jd|d|ƒD]u\}} d} |jƒ|jƒkrt|||ƒ} nt|| |ƒ} |s1| }qÑtj|| gƒ}qÑW||j|ƒkrs||=|||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. Nuuheaderuunquote(u get_paramsuloweru _formatparamu SEMISPACEujoinuget(uselfuparamuheaderurequoteu new_ctypeupuv((u2/opt/alt/python33/lib64/python3.3/email/message.pyu del_paramÎs % uMessage.del_paramcCsÂ|jdƒdkst‚n|jƒdkrD|d=d|dtƒ}|jd|ƒ}||kr+|Stj|ƒjƒS(u¸Return the boundary associated with the payload if present. The boundary is extracted from the Content-Type header's `boundary' parameter, and it is unquoted. uboundary(uobjectu get_paramuutilsucollapse_rfc2231_valueurstrip(uselfufailobjumissinguboundary((u2/opt/alt/python33/lib64/python3.3/email/message.pyu get_boundarys   uMessage.get_boundarycCs‘tƒ}|j|dƒ}||kr9tjdƒ‚ng}d}xY|D]Q\}}|jƒdkrŠ|jdd|fƒd }qL|j||fƒqLW|sÁ|jdd|fƒng}xº|jD]¯\} } | jƒdkrmg} xG|D]?\} } | dkr$| j| ƒqü| jd| | fƒqüWt j | ƒ} |j|j j | | ƒƒqÑ|j| | fƒqÑW||_dS( u»Set 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. u content-typeuNo Content-Type header founduboundaryu"%s"uu%s=%sNFT( uobjectu_get_params_preserveuerrorsuHeaderParseErroruFalseuloweruappenduTrueu_headersu SEMISPACEujoinupolicyuheader_store_parse(uselfuboundaryumissinguparamsu newparamsufoundpupkupvu newheadersuhuvupartsukuval((u2/opt/alt/python33/lib64/python3.3/email/message.pyu set_boundary!s2    uMessage.set_boundarycCsÌtƒ}|jd|ƒ}||kr+|St|tƒr˜|dpGd}y&|djdƒ}t||ƒ}Wq˜ttfk r”|d}Yq˜Xny|jdƒWntk rÁ|SYnX|jƒS(uõReturn 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. ucharsetiuus-asciiiuraw-unicode-escape( uobjectu get_paramu isinstanceutupleuencodeustru LookupErroru UnicodeErrorulower(uselfufailobjumissingucharsetupcharsetuas_bytes((u2/opt/alt/python33/lib64/python3.3/email/message.pyuget_content_charsetOs     uMessage.get_content_charsetcs‡fdd†|jƒDƒS(uÕReturn 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. csg|]}|jˆƒ‘qS((uget_content_charset(u.0upart(ufailobj(u2/opt/alt/python33/lib64/python3.3/email/message.pyu }s u(Message.get_charsets..(uwalk(uselfufailobj((ufailobju2/opt/alt/python33/lib64/python3.3/email/message.pyu get_charsetsmsuMessage.get_charsets(uwalkNFT(4u__name__u __module__u __qualname__u__doc__ucompat32u__init__u__str__uFalseu as_stringu is_multipartu set_unixfromu get_unixfromuattachuNoneu get_payloadu set_payloadu set_charsetu get_charsetu__len__u __getitem__u __setitem__u __delitem__u __contains__u__iter__ukeysuvaluesuitemsugetuset_rawu raw_itemsuget_allu add_headerureplace_headeruget_content_typeuget_content_maintypeuget_content_subtypeuget_default_typeuset_default_typeu_get_params_preserveuTrueu get_paramsu get_paramu set_paramu del_paramuset_typeu get_filenameu get_boundaryu set_boundaryuget_content_charsetu get_charsetsuemail.iteratorsuwalk(u __locals__((u2/opt/alt/python33/lib64/python3.3/email/message.pyuMessagejs\     Z /            !/  .T(u__doc__u__all__ureuuuubase64ubinasciiuiouBytesIOuStringIOuemailuutilsuerrorsuemail._policybaseucompat32ucharsetu_charsetuemail._encoded_wordsudecode_buCharsetu SEMISPACEucompileu tspecialsu _splitparamuNoneuTrueu _formatparamu _parseparamu _unquotevalueuMessage(((u2/opt/alt/python33/lib64/python3.3/email/message.pyus&       "