Qf  ddlZddlZddlZddlZddlZddlZ ddlZdZddl m Z ddlm Z gdZ ejedd ZGd d eZGd d eZGddeZGddeZGddeZGddeZdZdZhdZgdZdddZdZej<dgdZej<d gd!Z d"Z d#Z!dVd$Z"dVd%Z#dWd&Z$erd'Z%Gd(d)Z&erGd*d+e&Z'e jQd+ed,k(rddl)Z)e)jTd-.Z+e+jYd/d0d1d23e+jYd4d5d6d73e+jYd8d9d:e-d;ede+jYd?d@dAe-dB>e+jYdCdDdEddFGe+j]Z/e/j`Z0e/j se0d:k(reZ0e&e/jbe0HZ2ne0d:k(reZ0e'e/jbe0HZ2e2jgZ4dIe4vre2jke2jme/jl\Z7Z8Z9Z:Z;ee-e:e/j~z dOzZ9e2je9e:\Z7ZAeAD]a\ZBZCe eCdPjdQdOdZEe eCdRZFe-eCdZGe 100101211215220221222224225230231282)subjectfromdatez message-id references:bytes:linesr5r6)byteslines GroupInfo)grouplastfirstflag ArticleInfo)number message_idr8c g}t|D]K\}}t|tr%|j|j |xsd;|j|Mdj |S)Nascii)_email_decode_header isinstancer7appenddecodejoin) header_strpartsvencs rrrs^@ E&z23 a  LL#.1 2 LLO 3 775>rc g}|D]t}|ddk(r|ddjd\}}}d|z}n|jd\}}}|j}tj||}|j |vt }t |t |kr td|dt ||k7r td|S)Nr:z$LIST OVERVIEW.FMT response too shortz*LIST OVERVIEW.FMT redefines default fields) partitionlower_OVERVIEW_FMT_ALTERNATIVESgetrG_DEFAULT_OVERVIEW_FMTlenr )r8fmtlinename_suffixdefaultss r_parse_overview_fmtr]s$ C 7c>"12h005OD!V:D#nnS1OD!Vzz|)--dD9 4%H 3x#h-BCC >CMh&HII Jrc tt}g}|D]}i}|jd^}}t|}t |D]|\} } | t|k\r|| } | j d} | |k\rE| sC| dz} | r*| dt| j | k7r td| r| t| dnd} | ||| <~|j||f|S)N rOz: z?OVER/XOVER response doesn't include names of additional headers) rVrUsplitint enumerate startswithrRr rG)r8rWdata_process_func n_defaultsoverviewrXfieldsarticle_numbertokensitoken field_name is_metadatahs r_parse_overviewros*+JH"&**T"2^,!&)HAuCH}QJ$//4KJ{%U7CF^113q8')FGG*/c!fgT"F3q6N!*" 01+, Orc" | |dd}|dd}t|dd}t|dd}t|dd}t|dd}t|dd}t|dd}|dkr|dz }n |dkr|d z }tj||||||S) NiFidil)radatetime)date_strtime_strhoursminutessecondsyearmonthdays r_parse_datetimersBC=CR= ! E(1Q- G(12,G x} D B E hrsm C by        T5#ugw GGrc t|tjsd}ndj|}|j}|r|dz}dj||}||fSdj||}||fS)N000000z({0.hour:02d}{0.minute:02d}{0.second:02d}rvz{0:02d}{1.month:02d}{1.day:02d}z{0:04d}{1.month:02d}{1.day:02d})rFrwformatr})dtlegacyryyrxs r_unparse_datetimers  b(++ ,=DDRH A G4;;ArB X 5;;ArB X rcV |tj}|j||S)N)server_hostname)ssl_create_stdlib_context wrap_socket)sockcontexthostnames r _encrypt_onrs2  ?002G""4"BBrceZdZdZdZeddddefdZdZdZ dZ d Z d Z d Z d ZeZd ZdZd7dZdZd8dZdZd8dZd8dZdZdZdZdddZdddZd8dddZdZdZdZ dZ!ddd Z"d!Z#d"Z$d8d#Z%d$Z&d%Z'd8d&Z(d8ddd'Z)d8ddd(Z*d8ddd)Z+d*Z,ddd+Z-ddd,Z.ddd-Z/d.Z0d/Z1d0Z2d1Z3d2Z4d3Z5d9d4Z6d5Z7e8rd8d6Z9yy):rzutf-8surrogateescapeNFc| ||_||_|j||_d|_ |jj d|_|j ||s|r|j|||yy#|jr|jj|jjxYw)Nrwb) hostport_create_socketrfilemakefile _base_initloginclose)rrruserpassword readermodeusenetrctimeouts rrz NNTP.__init__;s "  ''0    **51DI OOJ 'x 484  yy ! IIOO  s AA88AB;c d|_|j|_d|_|j d|_|rAd|jvr3|j |j sd|_|j d|_d|_y)NrFREADER) debugging_getrespwelcome_capsgetcapabilitiesreadermode_afterauth_setreadermodetls_on authenticated)rrs rrzNNTP._base_init^s }}   %*! ($**4    !,,! $$&  #rc|SNrrs r __enter__zNNTP.__enter__s rcfd}|r* j|rjyyy#ttf$rY+wxYw#|rjwwxYw)NctdS)Nr)hasattrrsrzNNTP.__exit__..s wtV4r)quitOSErrorEOFError_close)rr is_connecteds` r__exit__z NNTP.__exit__sd4 > "  >KKM" X&   >KKM"s!9A A A  AA(c| |s tdtjd||j|jt j |j|jf|S)Nz0Non-blocking socket (timeout=0) is not supportedznntplib.connect) ValueErrorsysauditrrsocketcreate_connection)rrs rrzNNTP._create_socketsQ  wOP P #T499dii@''DII(>HHrcr |jrtdt|j|jS)Nz *welcome*)rprintreprrrs r getwelcomezNNTP.getwelcomes, . >>5d4<<.@A||rcp |jwd|_d|_ |j\}}||_d|vr!t t t |d|_d|vrdj|d|_|jS|jS#ttf$ri|_Y|jSwxYw)NrPVERSIONIMPLEMENTATION ) r nntp_versionnntp_implementation capabilitiesmaxmaprarIr r)rrespcapss rrzNNTP.getcapabilitiess  ::  !D '+D $ P!..0 d " $),CT)_,E(FD%#t+/2xx=M8N/OD,zztzz'(:;  zz sBB54B5c ||_yr)r)rlevels rset_debuglevelzNNTP.set_debuglevels H rc tjd|||tz}|jdkDrt dt ||j j||j jy)Nznntplib.putlinerPz*put*) rr_CRLFrrrrwriteflushrrXs r_putlinez NNTP._putlinesX 3 #T40e| >>A uWd4j9  rc |jrtdt||j|j|j }|j |y)Nz*cmd*)rrrencodeencodingerrorsrrs r_putcmdz NNTP._putcmds? 0 >>5$t*5{{4==$++6 drc( |jjtdz}t|tkDr t d|j dkDrt dt||st|r#|ddtk(r|dd}|S|ddtvr|dd}|S)NrPz line too longz*get*rt) rreadline_MAXLINErVr rrrrr)r strip_crlfrXs r_getlinez NNTP._getlines #yy!!(A+. t9x 0 0 >>A  '4: &8^ BCyE!CRy bce#CRy rc |j}|jrtdt||j |j |j }|dd}|dk(r t||dk(r t||dvr t||S)Nz*resp*rP45123) rrrrrHrrrr r )rrcs rrz NNTP._getresps %}} >>54:6{{4==$++6 !H 8$T* * 8$T* * E>#D) ) rc d} t|ttfrt|dx}}|j }|ddt vr t |g}|Jdtzdf} |jd}||vrni|jdr|dd}|j|>d} |j}||k(rn(|jdr|dd}|j|>|r|j||fS#|r|jwwxYw)Nwbr .s. rPFs..) rFstrr7openr _LONGRESPrrrrcrrGr)rr openedFilerr8 terminatorsrX terminators r _getlongrespzNNTP._getlongresps.   #$e -$(t$44 T==?DBQxy($T**E#e|V4 ==/D{*u-#ABxJJt$ " ==?Dz)u-#ABxLL&   "U{  "s CC44D cF |j||jSr)rrrs r _shortcmdzNNTP._shortcmds , T}}rcH |j||j|Sr)rr)rrXrs r_longcmdz NNTP._longcmd!s$ 0 T  &&rc |j||j|\}}||Dcgc](}|j|j|j*c}fScc}wr)rrrHrr)rrXrrlists r_longcmdstringzNNTP._longcmdstring'si  T&&t, d"&("&$kk$--="&(( ((s-Ac |jS#t$rYnwxYw |jd\}}t|}n#t$r t dd}YnwxYw||_|S)NzLIST OVERVIEW.FMT)_cachedoverviewfmtAttributeErrorrr]r rU)rrr8rWs r_getoverviewfmtzNNTP._getoverviewfmt1sz 8 ** *    ---.ABKD% &e,C " +'*C + #& s  ?AAcT|Dcgc]}t|jc}Scc}wr)r:r`)rr8rXs r _grouplistzNNTP._grouplistBs%5:;UT 4::<(U;;;s%ct i}|jd\}}|D]}|j^}}|||<||fS)N CAPABILITIES)rr`)rrrr8rXrYris rrzNNTP.capabilitiesFsP )).9 eD JJLMD6DJTzr)rc` t|tjtjfs.tdj |j j t||jdk\}}dj ||}|j||\}}||j|fS)NAthe date parameter must be a date or datetime object, not '{:40}'rqzNEWGROUPS {0} {1}) rFrwr3 TypeErrorr __class__rrrrr)rr3rrxrycmdrr8s r newgroupszNNTP.newgroupsTs $ >?$fT^^%<%<=? ?/tT5F5F5JK(!((8<))#t4 eT__U+++rc6 t|tjtjfs.tdj |j j t||jdk\}}dj |||}|j||S)NrrqzNEWNEWS {0} {1} {2}) rFrwr3rrrrrrr)rr;r3rrxryrs rnewnewsz NNTP.newnewsds $ >?$fT^^%<%<=? ?/tT5F5F5JK(#**5(HE""3--rch |d|z}nd}|j||\}}||j|fS)Nz LIST ACTIVE LIST)rr)r group_patternrcommandrr8s rrz NNTP.listtsH   $$}4GG))'48 eT__U+++rcNtjd}|jd|z\}}|jds|jd|z\}}i}|D]D}|j |j }|s%|j dd\} } |s| cS| || <F|r||fSy)Nz^(?P[^ ]+)[ ]+(.*)$zLIST NEWSGROUPS r(zXGTITLE rPrqrD)recompilerrcsearchstripr;) rr  return_allline_patrr8groupsraw_linematchrYdescs r_getdescriptionszNNTP._getdescriptionss::>?))*<}*LM eu%--j=.HIKD%HOOHNN$45E"[[A. d!K#t  < rc( |j|dS)NFr)rr;s r descriptionzNNTP.descriptions 7$$UE22rc( |j|dS)NTr)rr s r descriptionszNNTP.descriptionss5$$]D99rcV |jd|z}|jds t||j}dx}x}}t |}|dkDr1|d}|dkDr'|d}|dkDr|d}|dkDr|dj }|t |t |t ||fS)NzGROUP r'rrPrqr rr)rrcrr`rVrRra)rrYrwordscountr=r<ns rr;z NNTP.groups ~~ho.u% & &     J q5!HE1uaq5 8D1u$Qx~~/SZUSY<rcT |dj|}nd}|j||S)Nz ARTICLE {0}ARTICLEr;r<s rarticlez NNTP.articles5   #&&|4CC||C&&rc& |jdS)NSLAVE)rrs rslavez NNTP.slave$s ~~g&&rc tjd|jdj|||\}}fd}||Dcgc] }|| c}fScc}w)Nz^([0-9]+) ?(.*) ?z XHDR {0} {1}cRj|}|r|jddS|S)NrPrq)rr;)rXmpats r remove_numberz NNTP.xhdr..remove_number5s' $A$%1771a= /4 /r)rrrr) rhdrrrrr8rLrXrKs @rxhdrz NNTP.xhdr*se jj-.)).*?*?S*I4P e 0e% %Aw t9?% %_T4000rc|j|}|jds t|t|tt fr|j }|D]`}|jts|jdtz}|jdrd|z}|jj|b|jjd|jj|jS)N3r9rs. )rrcrrFr7 bytearray splitlinesendswithrrstriprrrr)rr frrXs r_postz NNTP._postts~~g&s# & & a%+ , A D=='{{7+e3t$d{ IIOOD !  ! }}rc( |jd|S)NPOST)rd)rdatas rpostz NNTP.posts 1zz&$''rcF |jdj||S)Nz IHAVE {0})rdr)rrArgs rihavez NNTP.ihaves& O zz+,,Z8$??rc |jr|jj|`|jjy#|jjwxYwr)rrrrs rrz NNTP._closes> yy !I IIOO DIIOO s (AA!cv |jd}|j|S#|jwxYw)NQUIT)rr)rrs rrz NNTP.quits5 1 >>&)D KKM  KKMs&8cl|jr td|s |s td |r=|s;ddl}|j}|j|j}|r |d}|d}|sy|j d|z}|jdr=|s t||j d|z}|jds t|d|_ |j|jr7d |jvr(|jd|_ |jyyy#t $rYwxYw) NzAlready logged in.z7At least one of `user` and `usenetrc` must be specifiedrrqzauthinfo user 381zauthinfo pass 281r)rrnetrcauthenticatorsrrrrcrr rrrr)rrrrrq credentialsauthrs rrz NNTP.logins5   12 2HIK K #kkm "11$))<7D#AwH ~~.56 ??5 !$T**~~&6&ABu-,T22    $ $)C    !DJ  " *D $%   s?D'' D32D3c |jd|_y#t$rYyt$r.}|jj drd|_nYd}~yd}~wwxYw)Nz mode reader480T)rrr rrrcr)res rrzNNTP._setreadermodesT >>-8DL!  ! zz$$U+,0)* s AA$AAc |jr td|jr td|jd}|j dr|j j t|j||j|_|jjd|_d|_d|_ |jytd)NzTLS is already enabled.z+TLS cannot be started after authentication.STARTTLS382rTzTLS failed to start.)rrrrrcrrrrrrrrr)rrrs rstarttlsz NNTP.starttlss  {{ !:;;!! !NOO>>*-Du% !' 7DIIF  II..u5 " " $$& 677r)Tr)NNT):rrrrr NNTP_PORTrrrrrrrrrdebugrrrrrrrrrrrrr rrrrr;r%r)r+r/r3r<r8r=rArDrGrNrRrWr3rdrhrjrrrr _have_sslr{rrrrr+scH F"+$ 50!F #D "I , E" ,\ ' ("< '+, ,0. ,t ,. 3:=4 1)% )%%= 'd ' 'd ' ' '' &*= )- 1*.161"*(@'#R  8rrc8eZdZedddddeffd ZfdZxZS)NNTP_SSLNFc @ ||_t | |||||||yr) ssl_contextsuperr) rrrrrrrrrrs rrzNNTP_SSL.__init__s.  +D  G T4x%w 0rct||} t||j|j}|S#|j xYwr)rrrrrr)rrrrs rrzNNTP_SSL._create_socketsI7)'2D "4)9)9499E    s !5A)rrr NNTP_SSL_PORTrrr __classcell__)rs@rrrs$&3$#e3 0  rr__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))defaultr%z-sz--serverz news.gmane.ioz+NNTP server hostname (default: %(default)s)z-pz--portrzNNTP port number (default: z / ))rtyper%z-nz --nb-articles z2number of articles to fetch (default: %(default)s)z-Sz--ssl store_truezuse NNTP over SSL)actionrr%)rrryGrouphaszarticles, rangetoc8t||kDr |d|dz dz}|S)Nrrz...)rV)slims rcutr6s& q6C<(37 e#ArrPr2rsB  I>*  Xg. 0 0 Y        I     O    "K " ";#E G %k$$]%FH 2:H,4  CK 8K 8\ 4. NN: z $X $ $2NOF i1LRT j/JL hFOQ^_a orQS glE02    D 99D 88 2:D dkk - 2: D $++D 1   DT %&WWTZZ%8"D%d '4'8%tL D D,,,q0 1EggeT*OD)! tF|,223:1=Y0DN# %,,c&"os7B'7@  "FFHaUIsK66LL