o w[e#@sdZddlZddlZddlZddgZddZedjZGdd d e Z d dZ d d Z d dZ ddZddZddZddgfddZd#ddZgfddZddZddZd d!Zed"krdedSdS)$z%Mailcap file handling. See RFC 1524.Ngetcaps findmatchcCsd|vr d|dfSdS)Nlinenor)r)entryrr/usr/lib/python3.10/mailcap.pylineno_sort_key s r z[^\xa1-\U0010FFFF\w@+=:,./-]c@seZdZdZdS)UnsafeMailcapInputz)Warning raised when refusing unsafe inputN)__name__ __module__ __qualname____doc__rrrrr sr c Csi}d}tD]F}zt|d}Wn tyYqw|t||\}}Wdn1s.wY|D]\}}||vrD|||<q7|||||<q7q|S)aReturn 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". rrN)listmailcapfilesopenOSError_readmailcapfileitems)capsrmailcapfpmorecapskeyvaluerrrrs"    cCsRdtjvrtjd}|tj}|Sdtjvrtjd}nd}|ddddg}|S)z7Return a list of all mailcap files found on the system.MAILCAPSHOME.z /.mailcapz /etc/mailcapz/usr/etc/mailcapz/usr/local/etc/mailcap)osenvironsplitpathsep)pathstrmailcapshomerrrr3s    rcCs tdtdt|d\}}|S)z?Read a mailcap file and return a dictionary keyed by MIME type.z2readmailcapfile is deprecated, use getcaps insteadN)warningswarnDeprecationWarningr)rr_rrrreadmailcapfileEs r*c Csi} |}|s ||fS|ddks|dkrq|}|dddkr<|}|s,d}|dd|}|dddks$t|\}}|rF|sGq|durS||d <|d7}|d }tt|D] }||||<q^d |}||vr||||n|g||<q) aRead 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". rr#Nz\  r/) readlinestrip parseliner rangelenjoinlowerappend) rrrlinenextlinerfieldstypesjrrrrMs6    rc Csg}dt|}}||kr"t|||\}}|||d}||ks t|dkr*dS|d|d|dd}}}d|i}|D]+}|d}|dkrP|}d} n|d|}||dd} ||vrgq@| ||<q@||fS) 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. rrr%NNNview=r,)r4 parsefieldr7findr1) r8r:infieldrr>restfkeyfvaluerrrr2vs,     r2cCsT|}||kr ||}|dkrn|dkr|d}n|d}||ks||||fS)z/Separate one key-value pair in a mailcap entry.;\r%r)r1)r8rBrCstartcrrrr@s r@r>z /dev/nullc Cst|rd|f}t|tdSt|||}|D]0}d|vr6t|d||}|dur,q|r6t|dkr6qt|||||} | durI| |fSqdS)aFind 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 lookupsubstrsystem) rMIMEtyperfilenameplistmsgentrieserLcommandrrrrs"    cslg}||vr |||}|d}|dd}||vr!|||}dur.fdd|D}t|td}|S)Nr/rz/*csg|]}|vr|qSrr).0rVrrr szlookup..rY)r sortedr )rrQrrU MIMEtypesrrYrrNs     rNc Csdd}dt|}}||kr||}|d}|dkr.|dkr)|||d}|d}||}n~||}|d}|dkr?||}nm|dkrH||}nd|dkrbt|r]d|f}t|tdS||}nJ|d kr|} ||kr||d kr|d}||kr||d ksr|| |} |d}t| |} t| rd | | f}t|tdS|| }n|d|}||ks |S) Nr,rr%rIstz9Refusing to substitute MIME type %r into a shell command.{}z=Refusing to substitute parameter %r (%s) into a shell command)r4rMr&r'r findparam) rDrQrRrSresrBrCrKrTrJnameparamrrrrOsF            #rOcCsF|d}t|}|D]}|d||kr ||dSq dS)Nr?r,)r6r4)rdrSrCprrrrbs rbc Csddl}t}|jddst|dStdt|jdD]G}|j||d}t|dkr5tddS|d}|d}t||d|\}}|sNtdtqtd|t |}t |}|rdtd|qdS) Nrrr%z"usage: mailcap [MIMEtype file] ...r>zNo viewer found forz Executing:z Exit status:) sysrargvshowr3r4printrtyperrPwaitstatus_to_exitcode) rgrrBargsrQfilerWrVstsrrrrLs,      rLcCstdtD]}td|qt|st}tdtt|}|D]$}t|||}|D]}t|}|D] }td|||q9tq1q%dS)NzMailcap files: zMailcap entries:z %-15s)rjrrr[)rfnckeysrkrUrVkeyskrrrris" ri__main__)N)rrr&re__all__r compilesearchrMWarningr rrr*rr2r@rrNrOrbrLrir rrrrs. )  )