B
‡ya@ÈNã @ sÜ d Z ddgZddlZddlZddlZddlmZmZ ddlm Z ddlm
Z
ddlmZm
Z
dd lmZ dd
lmZ ejZdZe d¡Zd
d„ Zddd„Zdd„ Zdd„ ZG dd„ dƒZG dd„ deƒZG dd„ deƒZdS )z8Basic message object for the email package object model.ÚMessageÚEmailMessageé N)ÚBytesIOÚStringIO)Úutils)Úerrors)ÚPolicyÚcompat32)Úcharset)Údecode_bz; z[ \(\)<>@,;:\\"/\[\]\?=]c C s4 t | ƒ d¡\}}}|s$| ¡ d fS | ¡ | ¡ fS )Nú;)ÚstrÚ partitionÚstrip)ÚparamÚaÚsepÚb© r ú2/opt/alt/python37/lib64/python3.7/email/message.pyÚ_splitparam s r Tc C sÄ |dk r¼t |ƒdkr¼t|tƒrL| d7 } t |d |d |d ¡}d| |f S y| d¡ W n2 tk
rŒ | d7 } t |dd ¡}d| |f S X |sœt |¡r®d
| t |¡f S d| |f S n| S dS )a~ 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.
Nr Ú*é é z%s=%sÚasciizutf-8Ú z%s="%s")
ÚlenÚ
isinstanceÚtupler Zencode_rfc2231ÚencodeÚUnicodeEncodeErrorÚ tspecialsÚsearchÚquote)r Úvaluer# r r r Ú_formatparam' s
r% c C sò dt | ƒ } g }xÜ| d d… dkrì| dd … } | d¡}x<|dkrt| dd|¡| dd|¡ d rt| d|d ¡}q:W |dk r†t| ƒ}| d |… }d|krÐ| d¡}|d |… ¡ ¡ d ||d d … ¡ }| | ¡ ¡ | |d … } qW |S )Nr r r ú"z\"r ú=)r
ÚfindÚcountr Úindexr ÚlowerÚappend)ÚsZplistÚendÚfÚir r r Ú_parseparamI s
*
,r1 c C s4 t | tƒr&| d | d t | d ¡fS t | ¡S d S )Nr r r )r r r Úunquote)r$ r r r Ú
_unquotevalue] s
r3 c @ s¦ e Zd ZdZefdd„Zdd„ Zddd d
„Zdd„ Zded
d„Z dd„ Z
dd„ Zdd„ Zdd„ Z
dfdd„Zdgdd„Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zdhd1d2„Zd3d4„ Zd5d6„ Zdid7d8„Zd9d:„ Zd;d<„ Z d=d>„ Z!d?d@„ Z"dAdB„ Z#dCdD„ Z$dEdF„ Z%dGdH„ Z&djdKdL„Z'dkdMdN„Z(dldQdR„Z)dmdSdT„Z*dndUdV„Z+dodWdX„Z,dpdYdZ„Z-d[d\„ Z.dqd]d^„Z/drd_d`„Z0dadb„ Z1ddcl2m3Z3 dS )sr aÈ 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.
c C s: || _ g | _d | _d | _d | _d | _| _g | _d| _d S )Nz
text/plain) ÚpolicyÚ_headersÚ _unixfromÚ_payloadÚ_charsetZpreambleZepilogueÚdefectsÚ
_default_type)Úselfr4 r r r Ú__init__x s zMessage.__init__c C s | ¡ S )z9Return the entire formatted message as a string.
)Ú as_string)r; r r r Ú__str__„ s zMessage.__str__Fr Nc C sJ ddl m} |dkr| jn|}tƒ }||d||d}|j| |d | ¡ S )a¾ Return the entire formatted message as a string.
Optional 'unixfrom', when true, means include the Unix From_ envelope
header. For backward compatibility reasons, if maxheaderlen is
not specified it defaults to 0, so you must override it explicitly
if you want a different maxheaderlen. 'policy' is passed to the
Generator instance used to serialize the mesasge; if it is not
specified the policy associated with the message instance is used.
If the message object contains binary data that is not encoded
according to RFC standards, the non-compliant data will be replaced by
unicode "unknown character" code points.
r )Ú GeneratorNF)Úmangle_from_Úmaxheaderlenr4 )Úunixfrom)Úemail.generatorr? r4 r ÚflattenÚgetvalue)r; rB rA r4 r? ÚfpÚgr r r r= ‰ s zMessage.as_stringc C s | ¡ S )z?Return the entire formatted message as a bytes object.
)Úas_bytes)r; r r r Ú __bytes__¡ s zMessage.__bytes__c C sH ddl m} |dkr| jn|}tƒ }||d|d}|j| |d | ¡ S )aJ Return the entire formatted message as a bytes object.
Optional 'unixfrom', when true, means include the Unix From_ envelope
header. 'policy' is passed to the BytesGenerator instance used to
serialize the message; if not specified the policy associated with
the message instance is used.
r )ÚBytesGeneratorNF)r@ r4 )rB )rC rJ r4 r rD rE )r; rB r4 rJ rF rG r r r rH ¦ s zMessage.as_bytesc C s t | jtƒS )z6Return True if the message consists of multiple parts.)r r7 Úlist)r; r r r Úis_multipartµ s zMessage.is_multipartc C s
|| _ d S )N)r6 )r; rB r r r Úset_unixfrom¼ s zMessage.set_unixfromc C s | j S )N)r6 )r; r r r Úget_unixfrom¿ s zMessage.get_unixfromc C sF | j dkr|g| _ n.y| j |¡ W n tk
r@ tdƒ‚Y nX dS )zð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.
Nz=Attach is not valid on a message with a non-multipart payload)r7 r, ÚAttributeErrorÚ TypeError)r; Úpayloadr r r ÚattachÅ s
zMessage.attachc C s¸ | ¡ r(|rdS |dkr| jS | j| S |dk rNt| jtƒsNtdt| jƒ ƒ‚| j}t| dd¡ƒ ¡ }t|tƒrút
|¡rÈ| dd¡}|súy| |
dd¡d¡}W qú tk
rÄ | dd¡}Y qúX n2|rúy| d¡}W n tk
rø | d ¡}Y nX |s|S |d
krt |¡S |dkrZtd | ¡ ¡ƒ\}}x|D ]}| j | |¡ q>W |S |d
kr¤t|ƒ} tƒ }
ytj| |
dd |
¡ S tjk
r¢ |S X t|tƒr´|S |S )aZ Return 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.
NzExpected list, got %szcontent-transfer-encodingr r Úsurrogateescaper
Úreplacezraw-unicode-escapezquoted-printableÚbase64ó )z
x-uuencodeZuuencodeZuuezx-uueT)Úquiet)rL r7 r rK rP Útyper
Úgetr+ r Z_has_surrogatesr ÚdecodeÚ get_paramÚLookupErrorÚUnicodeErrorÚquopriZdecodestringr ÚjoinÚ
splitlinesr4 Z
handle_defectr ÚuurE ÚError)r; r0 rZ rQ ÚcteZbpayloadr$ r9 ZdefectZin_fileZout_filer r r Úget_payloadÕ sV "
zMessage.get_payloadc C sp t |dƒr:|dkr|| _dS t|tƒs.t|ƒ}| |j¡}t |dƒrT| dd¡| _n|| _|dk rl| |¡ dS )z˜Set the payload to the given value.
Optional charset sets the message's default character set. See
set_charset() for details.
r NrZ r rS )Úhasattrr7 r ÚCharsetr Úoutput_charsetrZ Úset_charset)r; rQ r
r r r Úset_payload/ s
zMessage.set_payloadc C s |dkr| d¡ d| _dS t|tƒs.t|ƒ}|| _d| krH| dd¡ d| krf| jdd| ¡ d n| d| ¡ ¡ || ¡ kr| | j¡| _d| kr| ¡ }y|| ƒ W nj t
k
r | j}|rüy| d d
¡}W n tk
rú | |j
¡}Y nX | |¡| _| d|¡ Y nX dS )aÛ 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.
Nr
zMIME-Versionz1.0zContent-Typez
text/plain)r
zContent-Transfer-Encodingr rS )Ú del_paramr8 r rf Ú
add_headerZget_output_charsetÚ set_paramZbody_encoder7 Zget_body_encodingrP r r] rg )r; r
rc rQ r r r rh C s8
zMessage.set_charsetc C s | j S )zKReturn the Charset instance associated with the message's payload.
)r8 )r; r r r Úget_charsetr s zMessage.get_charsetc C s
t | jƒS )z9Return the total number of headers, including duplicates.)r r5 )r; r r r Ú__len__z s zMessage.__len__c C s
| |¡S )a- 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.
)rY )r; Únamer r r Ú__getitem__~ s zMessage.__getitem__c C sv | j |¡}|r\| ¡ }d}x>| jD ]4\}}| ¡ |kr$|d7 }||kr$td ||¡ƒ‚q$W | j | j ||¡¡ dS )z¸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.
r r z/There may be at most {} {} headers in a messageN)r4 Zheader_max_countr+ r5 Ú
ValueErrorÚformatr, Úheader_store_parse)r; ro ÚvalZ max_countZlnameÚfoundÚkÚvr r r Ú__setitem__‰ s zMessage.__setitem__c C sD | ¡ }g }x,| jD ]"\}}| ¡ |kr| ||f¡ qW || _dS )zwDelete all occurrences of a header, if present.
Does not raise an exception if the header is missing.
N)r+ r5 r, )r; ro Ú
newheadersrv rw r r r Ú__delitem__› s zMessage.__delitem__c C s | ¡ dd„ | jD ƒkS )Nc S s g | ]\}}| ¡ ‘qS r )r+ )Ú.0rv rw r r r ú