U e5d @sddlZddlZddlZddlZddlZddlZz ddlZWnek rTdZYnXdZddl m Z ddlm Z dddd d d d d gZ dZGdddeZGdddeZGdd d eZGdd d eZGdd d eZGdd d eZdZdZdddddddddd d!d"h Zd#d$d%d&d'd(d)gZd(d)d*Zd+Zed,d-d.d/d0gZed1d2d3d4gZd5d Z d6d7Zdld8d9Z dmd:d;Z!dndd?Z#Gd@dAdAZ$GdBdde$Z%erGdCdDdDe$Z&e 'dDe(dEkrddl)Z)e)j*dFdGZ+e+j,dHdIdJdKdLe+j,dMdNdOdPdLe+j,dQdRdSe-dTeefdUe+j,dVdWdXe-dYdUe+j,dZd[d\dd]d^e+.Z/e/j0Z0e/jse0dSkrxeZ0e%e/j1e0d_Z2ne0dSkreZ0e&e/j1e0d_Z2e23Z4d`e4kre25e26e/j6\Z7Z8Z9Z:Z;ee-e:e/j?dgZ9e2@e9e:\Z7ZAeAD]Z\ZBZCe eCd$DdhdgdZEe eCd#ZFe-eCd)ZGetj|f|z|d|_Wntk r8d|_YnXdS)NrzNo response given) Exception__init__response IndexError)selfargsr/usr/lib64/python3.8/nntplib.pyr cs zNNTPError.__init__N)__name__ __module__ __qualname__r rrrrrasc@s eZdZdS)rNrrrrrrrrjsc@s eZdZdS)rNrrrrrrnsc@s eZdZdS)rNrrrrrrrsc@s eZdZdS)r Nrrrrrr vsc@s eZdZdS)r Nrrrrrr zswi3Z100Z101211215Z220Z221Z222Z224Z225Z230Z231Z282subjectfromdatez message-idZ referencesz:bytesz:lines)byteslines GroupInfogrouplastfirstflag ArticleInfoZnumber message_idrcCsJg}t|D]2\}}t|tr4|||p,dq ||q d|S)Nascii)_email_decode_header isinstancerappenddecodejoin)Z header_strpartsvencrrrrs   cCsg}|D]`}|ddkr:|ddd\}}}d|}n|d\}}}|}t||}||qt}t|t|krtd|dt||krtd|S)Nr:z$LIST OVERVIEW.FMT response too shortz*LIST OVERVIEW.FMT redefines default fields) partitionlower_OVERVIEW_FMT_ALTERNATIVESgetr+_DEFAULT_OVERVIEW_FMTlenr )rfmtlinename_suffixdefaultsrrr_parse_overview_fmts    r?cCstt}g}|D]}i}|d^}}t|}t|D]\} } | t|krLq6|| } | d} | |kr| s| d} | r| dt| | krtd| r| t| dnd} | ||| <q6|||fq|S)N r1z: z?OVER/XOVER response doesn't include names of additional headers) r8r7splitint enumerate startswithr4r r+)rr9Zdata_process_funcZ n_defaultsZoverviewr:ZfieldsZarticle_numbertokensitokenZ field_nameZ is_metadatahrrr_parse_overviews&   rIcCs|dkr |dd}|dd}t|dd}t|dd}t|dd}t|dd}t|dd}t|dd}|dkr|d7}n|dkr|d 7}t||||||S) NiFidil)rBdatetime)date_strtime_strZhoursZminutesZsecondsyearZmonthZdayrrr_parse_datetimes   rTcCsPt|tjsd}n d|}|j}|r<|d}d||}n d||}||fS)NZ000000z({0.hour:02d}{0.minute:02d}{0.second:02d}rOz{0:02d}{1.month:02d}{1.day:02d}z{0:04d}{1.month:02d}{1.day:02d})r*rPformatrS)ZdtZlegacyrRyrQrrr_unparse_datetimes   rWcCs|dkrt}|j||dS)N)Zserver_hostname)sslZ_create_stdlib_contextZ wrap_socket)sockcontextZhostnamerrr _encrypt_onsr[c@seZdZdZdZdefddZddZdd Zd d Z d d Z ddZ e Z ddZ ddZdhddZddZdiddZddZdjddZdkdd Zd!d"Zd#d$Zd%d&Zdd'd(d)Zdd'd*d+Zdldd'd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zdd'd6d7Zd8d9Z d:d;Z!dmdd?Z#d@dAZ$dndBdCZ%dodd'dDdEZ&dpdd'dFdGZ'dqdd'dHdIZ(dJdKZ)dd'dLdMZ*dd'dNdOZ+dd'dPdQZ,dd'dRdSZ-dTdUZ.dVdWZ/dXdYZ0dZd[Z1d\d]Z2d^d_Z3d`daZ4drdbdcZ5dddeZ6e7rdsdfdgZ8dS)t _NNTPBasezutf-8surrogateescapeNcCsj||_||_d|_||_d|_|d|_|rZd|jkrZ||jsZd|_|d|_ d|_ dS)NrFREADER) hostfile debugging_getrespwelcome_capsgetcapabilitiesreadermode_afterauth_setreadermodetls_on authenticated)rr`r_ readermodetimeoutrrrr 9s  z_NNTPBase.__init__cCs|SNrrrrr __enter__hsz_NNTPBase.__enter__c sRfdd}|rNz*z Wnttfk r8YnXW5|rLXdS)Ncs tdS)Nr`)hasattrrrmrrlz$_NNTPBase.__exit__..)_closequitOSErrorEOFError)rrZ is_connectedrrmr__exit__ks   z_NNTPBase.__exit__cCs|jrtdt|j|jS)Nz *welcome*)raprintreprrcrmrrr getwelcomevsz_NNTPBase.getwelcomec Cs|jdkrd|_d|_z|\}}Wnttfk rDi|_Ynsz,_NNTPBase._longcmdstring..r)rr:r`rlistrrmr_longcmdstring s   z_NNTPBase._longcmdstringcCsdz|jWStk rYnXz|d\}}Wn tk rPtdd}Yn Xt|}||_|S)NzLIST OVERVIEW.FMT)Z_cachedoverviewfmtAttributeErrorrrr7r?)rrrr9rrr_getoverviewfmtsz_NNTPBase._getoverviewfmtcCsdd|DS)NcSsg|]}t|qSr)r rArrrrr&sz(_NNTPBase._grouplist..r)rrrrr _grouplist$sz_NNTPBase._grouplistcCs8i}|d\}}|D]}|^}}|||<q||fS)NZ CAPABILITIES)rrA)rrrrr:r;rErrrr|(s   z_NNTPBase.capabilities)r`cCsbt|tjtjfs$td|jjt||jdk\}}d||}| ||\}}|| |fS)NAthe date parameter must be a date or datetime object, not '{:40}'rJzNEWGROUPS {0} {1}) r*rPr TypeErrorrU __class__rrWr{rr)rrr`rQrRcmdrrrrr newgroups6s z_NNTPBase.newgroupscCsRt|tjtjfs$td|jjt||jdk\}}d|||}| ||S)NrrJzNEWNEWS {0} {1} {2}) r*rPrrrUrrrWr{r)rr!rr`rQrRrrrrnewnewsFsz_NNTPBase.newnewscCs4|dk rd|}nd}|||\}}|||fS)Nz LIST ACTIVE ZLIST)rr)r group_patternr`commandrrrrrrVs  z_NNTPBase.listc Cstd}|d|\}}|ds8|d|\}}i}|D]:}||}|r@|dd\} } |sr| S| || <q@|r||fSdSdS)Nz^(?P[^ ]+)[ ]+(.*)$zLIST NEWSGROUPS rXGTITLE r2rJr()recompilerrDsearchstripr!) rrZ return_allline_patrrgroupsraw_linematchr;Zdescrrr_getdescriptionses   z_NNTPBase._getdescriptionscCs ||dS)NFr)rr!rrr description|s z_NNTPBase.descriptioncCs ||dS)NTr)rrrrr descriptionssz_NNTPBase.descriptionscCs|d|}|ds t||}d}}}t|}|dkr|d}|dkr|d}|dkr|d}|dkr|d}|t|t|t||fS)NzGROUP rrr2rJrrK)rrDrrAr8r4rB)rr;rwordscountr#r"nrrrr!s    z_NNTPBase.groupcCs |d|S)NZHELP)r)rr`rrrhelpsz_NNTPBase.helpcCs8|dst||}t|d}|d}|||fS)NZ22r2rJ)rDrrArB)rrrart_numr&rrr _statparses   z_NNTPBase._statparsecCs||}||Srl)rr)rr:rrrr_statcmds z_NNTPBase._statcmdcCs"|r|d|S|dSdS)NzSTAT {0}ZSTAT)rrU)r message_specrrrstats z_NNTPBase.statcCs |dS)NZNEXTrrmrrrnextsz_NNTPBase.nextcCs |dS)NZLASTrrmrrrr"sz_NNTPBase.lastcCs0|||\}}||\}}}|t|||fSrl)rrr%)rr:r`rrrr&rrr_artcmdsz_NNTPBase._artcmdcCs$|dk rd|}nd}|||S)NzHEAD {0}ZHEADrUrrrr`rrrrheads z_NNTPBase.headcCs$|dk rd|}nd}|||S)NzBODY {0}ZBODYrrrrrbodys z_NNTPBase.bodycCs$|dk rd|}nd}|||S)Nz ARTICLE {0}ZARTICLErrrrrarticles z_NNTPBase.articlecCs |dS)NZSLAVE)rrmrrrslavesz_NNTPBase.slavecsDtd|d|||\}}fdd|fdd|DfS)Nz^([0-9]+) ?(.*) ?z XHDR {0} {1}cs|}|r|ddS|S)Nr2rJ)rr!)r:m)patrr remove_numbers z%_NNTPBase.xhdr..remove_numbercsg|] }|qSrrr)rrrrsz"_NNTPBase.xhdr..)rrrrU)rZhdrrr`rrr)rrrxhdr s  z_NNTPBase.xhdrcCs.|d|||\}}|}|t||fS)Nz XOVER {0}-{1})rrUrrI)rstartendr`rrr9rrrxovers z_NNTPBase.xoverc Csxd|jkrdnd}t|ttfr>|\}}|d||p6d7}n|dk rR|d|}|||\}}|}|t||fS)NZOVERZXOVERz {0}-{1}r(rz)rdr*tuplerrUrrrI) rrr`rrrrrr9rrrover*s z_NNTPBase.overc Csftdtdtd}|d||\}}g}|D](}||}|r4|| ddq4||fS)NzFThe XGTITLE extension is not actively used, use descriptions() insteadrJz^([^ ]+)[ ]+(.*)$rr2) warningswarnDeprecationWarningrrrrrr+r!) rr!r`rrZ raw_linesrrrrrrxgtitleEs z_NNTPBase.xgtitlecCsltdtd|d|}|ds0t|z|\}}Wntk r^t|dYn X||fSdS)Nz(The XPATH extension is not actively usedrJz XPATH {0}Z223) rrrrrUrDrrA ValueError)ridrZresp_numpathrrrxpathWs z_NNTPBase.xpathcCsb|d}|dst||}t|dkr8t||d}t|dkrTt||t|dfS)NZDATEZ111rJr2)rrDrrAr8r rT)rrelemrrrrrks    z_NNTPBase.datecCs||}|dst|t|ttfr2|}|D]:}|tsR| dt}|drdd|}|j |q6|j d|j | S)N3rrs. )rrDrr*r bytearray splitlinesendswithrrstripr`rrrb)rrfrr:rrr_post|s      z_NNTPBase._postcCs |d|S)NZPOST)r)rdatarrrpostsz_NNTPBase.postcCs|d||S)Nz IHAVE {0})rrU)rr&rrrrihavesz_NNTPBase.ihavecCs|j|`dSrl)r`rrmrrrrrs z_NNTPBase._closecCsz|d}W5|X|S)NZQUIT)rrr)rrrrrrss z_NNTPBase.quitcCs|jrtd|s|stdz<|rX|sXddl}|}||j}|rX|d}|d}Wntk rnYnX|sxdS|d|}|dr|st|n |d|}|dst |d|_ | |j rd |j kr| d|_ | dS) NzAlready logged in.z7At least one of `user` and `usenetrc` must be specifiedrrJzauthinfo user Z381zauthinfo pass Z281r^)rirnetrcZauthenticatorsr_rtrrDrrrdrerfrg)ruserpasswordusenetrcrZ credentialsZauthrrrrlogins>     z_NNTPBase.loginc Cs`z|d|_WnJtk r$Yn8tk rZ}z|jdrHd|_nW5d}~XYnXdS)Nz mode readerZ480T)rrcrrr rDrf)rerrrrgs z_NNTPBase._setreadermodecCs||jrtd|jrtd|d}|drp|jt|j||j |_|j d|_d|_d|_ | nt ddS)NzTLS is already enabled.z+TLS cannot be started after authentication.STARTTLSZ382rwbTzTLS failed to start.)rhrrirrDr`rr[rYr_makefilerdrer)rrZrrrrstarttlss    z_NNTPBase.starttls)T)N)N)N)N)N)N)N)N)N)NNT)N)9rrrrrrr rnrvryrerdebugrrrrbrrrrrrr|rrrrrrr!rrrrrr"rrrrrrrrrrrrrrrrrsrrg _have_sslrrrrrr\)sn  /     .         ) r\c@s*eZdZeddddefddZddZdS)rNFc Cs||_||_td|||t||f||_d}z8|jd}t ||||||sZ|rh| |||Wn$|r~| |j YnXdS)Nnntplib.connectr) r_portrrsocketcreate_connectionrYrr\r rr) rr_rrrrjrrkr`rrrr s$   z NNTP.__init__cCs zt|W5|jXdSrlrYrr\rrrmrrrrr$sz NNTP._close)rrr NNTP_PORTrr rrrrrrrs  %c@s,eZdZedddddefddZddZdS)NNTP_SSLNFc Cstd|||t||f||_d} zJt|j|||_|jd} tj|| |||d|s`|rn| |||Wn$| r| |j YnXdS)Nrr)rjrk) rrrrrYr[rr\r rr) rr_rrrZ ssl_contextrjrrkr`rrrr .s"   zNNTP_SSL.__init__cCs zt|W5|jXdSrlrrmrrrrrEszNNTP_SSL._close)rrr NNTP_SSL_PORTrr rrrrrrr,s r__main__zJ nntplib built-in demo - display the latest articles in a newsgroup)rz-gz--groupzgmane.comp.python.generalz3group to fetch messages from (default: %(default)s))defaultrz-sz--serverz news.gmane.ioz+NNTP server hostname (default: %(default)s)z-pz--portrz#NNTP port number (default: %s / %s))rtyperz-nz --nb-articles z2number of articles to fetch (default: %(default)s)z-Sz--ssl store_truezuse NNTP over SSL)actionrr)r_rrZGroupZhaszarticles, rangetocCs$t||kr |d|dd}|S)NrKz...)r8)sZlimrrrcutps r r2Bs           Y.