o
    f#                     @   s   d Z ddlZddlZddlZddgZdd ZedjZG dd	 d	e	Z
d
d Zdd Zdd Zdd Zdd Zdd Zddg fddZd#ddZg fddZdd Zdd Zd d! Zed"krde  dS dS )$z%Mailcap file handling.  See RFC 1524.    Ngetcaps	findmatchc                 C   s   d| v r
d| d fS dS )Nlinenor   )   r    )entryr   r   /usr/lib/python3.10/mailcap.pylineno_sort_key
   s   r	   z[^\xa1-\U0010FFFF\w@+=:,./-]c                   @   s   e Zd ZdZdS )UnsafeMailcapInputz)Warning raised when refusing unsafe inputN)__name__
__module____qualname____doc__r   r   r   r   r
      s    r
   c               	   C   s   i } d}t  D ]F}zt|d}W n	 ty   Y qw | t||\}}W d   n1 s.w   Y  | D ]\}}|| vrD|| |< q7| | | | |< q7q| S )a  Return a dictionary containing the mailcap database.

    The dictionary maps a MIME type (in all lowercase, e.g. 'text/plain')
    to a list of dictionaries corresponding to mailcap entries.  The list
    collects all the entries for that MIME type from all available mailcap
    files.  Each dictionary contains key-value pairs for that MIME type,
    where the viewing command is stored with the key "view".

    r   rN)listmailcapfilesopenOSError_readmailcapfileitems)capsr   mailcapfpmorecapskeyvaluer   r   r   r      s"   


c                  C   sR   dt jv rt jd } | t j}|S dt jv rt jd }nd}|d dddg}|S )z7Return a list of all mailcap files found on the system.MAILCAPSHOME.z	/.mailcapz/etc/mailcapz/usr/etc/mailcapz/usr/local/etc/mailcap)osenvironsplitpathsep)pathstrmailcapshomer   r   r   r   3   s   

	
r   c                 C   s    t dtd t| d\}}|S )z?Read a mailcap file and return a dictionary keyed by MIME type.z2readmailcapfile is deprecated, use getcaps instead   N)warningswarnDeprecationWarningr   )r   r   _r   r   r   readmailcapfileE   s
   r*   c           	      C   s  i }	 |   }|s||fS |d dks| dkrq|}|dd dkr<|   }|s,d}|dd | }|dd dks$t|\}}|rF|sGq|durS||d	< |d7 }|d
}tt|D ]
}||  ||< q^d
| }||v r||| | n|g||< q)a  Read a mailcap file and return a dictionary keyed by MIME type.

    Each MIME type is mapped to an entry consisting of a list of
    dictionaries; the list will contain more than one such dictionary
    if a given MIME type appears more than once in the mailcap file.
    Each dictionary contains key-value pairs for that MIME type, where
    the viewing command is stored with the key "view".
    r   r   # Nz\

r   /)	readlinestrip	parseliner    rangelenjoinlowerappend)	r   r   r   linenextliner   fieldstypesjr   r   r   r   M   s6   	

r   c           
      C   s   g }dt | }}||k r"t| ||\}}|| |d }||k st |dk r*dS |d |d |dd }}}d|i}|D ]+}|d}|dk rP|}d}	n|d|  }||d d  }	||v rgq@|	||< q@||fS )	zParse one entry in a mailcap file and return a dictionary.

    The viewing command is stored as the value with the key "view",
    and the rest of the fields produce key-value pairs in the dict.
    r   r   r%   NNNview=r,   )r4   
parsefieldr7   findr1   )
r8   r:   infieldr   r>   restfkeyfvaluer   r   r   r2   v   s,   
 

r2   c                 C   sT   |}||k r | | }|dkrn|dkr|d }n|d }||k s| ||   |fS )z/Separate one key-value pair in a mailcap entry.;\r%   r   )r1   )r8   rB   rC   startcr   r   r   r@      s   
r@   r>   z	/dev/nullc           
      C   s   t |rd|f }t|t dS t| ||}|D ]0}d|v r6t|d ||}|du r,q|r6t|dkr6qt|| |||}	|	durI|	|f  S qdS )a  Find a match for a mailcap entry.

    Return a tuple containing the command line, and the mailcap entry
    used; (None, None) if no match is found.  This may invoke the
    'test' command of several matching entries before deciding which
    entry to use.

    zHRefusing to use mailcap with filename %r. Use a safe temporary filename.r=   testNr   )_find_unsafer&   r'   r
   lookupsubstr   system)
r   MIMEtyper   filenameplistmsgentrieserL   commandr   r   r   r      s"   	
c                    sl   g }|| v r|| |  }| d}|d d }|| v r!|| |  } d ur. fdd|D }t|td}|S )Nr/   r   z/*c                    s   g | ]} |v r|qS r   r   ).0rV   r   r   r   
<listcomp>   s    zlookup.<locals>.<listcomp>rY   )r    sortedr	   )r   rQ   r   rU   	MIMEtypesr   rY   r   rN      s   
rN   c                 C   sd  d}dt | }}||k r| | }|d }|dkr.|dkr)| ||d  }|d }|| }n~| | }|d }|dkr?|| }nm|dkrH|| }nd|dkrbt|r]d|f }t|t d S || }nJ|d	kr|}	||k r| | d
kr|d }||k r| | d
ksr| |	| }
|d }t|
|}t|rd||
f }t|t d S || }n|d | }||k s|S )Nr,   r   r   %rI   stz9Refusing to substitute MIME type %r into a shell command.{}z=Refusing to substitute parameter %r (%s) into a shell command)r4   rM   r&   r'   r
   	findparam)rD   rQ   rR   rS   resrB   rC   rK   rT   rJ   nameparamr   r   r   rO      sF   






#rO   c                 C   sF   |   d } t| }|D ]}|d |   | kr ||d    S qdS )Nr?   r,   )r6   r4   )rd   rS   rC   pr   r   r   rb      s   rb   c            	      C   s   dd l } t }| jdd  st| d S tdt| jdD ]G}| j||d  }t|dk r5td  d S |d }|d }t||d|\}}|sNtdt qtd| t	
|}t	|}|rdtd| qd S )	Nr   r   r%   z"usage: mailcap [MIMEtype file] ...r>   zNo viewer found forz
Executing:zExit status:)sysr   argvshowr3   r4   printr   typer   rP   waitstatus_to_exitcode)	rg   r   rB   argsrQ   filerW   rV   stsr   r   r   rL     s,   



rL   c                 C   s   t d t D ]}t d|  qt   | st } t d t   t| }|D ]$}t | | | }|D ]}t|}|D ]}t d| ||  q9t   q1q%d S )NzMailcap files:	zMailcap entries:z  %-15s)rj   r   r   r[   )r   fnckeysrk   rU   rV   keyskr   r   r   ri     s"   
ri   __main__)N)r   r   r&   re__all__r	   compilesearchrM   Warningr
   r   r   r*   r   r2   r@   r   rN   rO   rb   rL   ri   r   r   r   r   r   <module>   s.    )
)
