\d dZddlZddlZddlZddlZddlZddlZ ddlZdZn #e $rdZYnwxYwddl m Z ddlm Z gdZejed d ZGd d eZGd deZGddeZGddeZGddeZGddeZdZdZhdZgdZdddZdZejdgd Z ejd!gd"Z!d#Z d$Z"dWd%Z#dWd&Z$dXd'Z%erd(Z&Gd)d*Z'er#Gd+d,e'Z(e)d,ed-kr,ddl*Z*e*j+d./Z,e,-d0d1d2d34e,-d5d6d7d84e,-d9d:d;e.d?e,-d@dAdBe.dC?e,-dDdEdFddGHe,/Z0e0j1Z1e0jse1d;kreZ1e'e0j2e1IZ3ne1d;kreZ1e(e0j2e1IZ3e34Z5dJe5vre36e37e0j7\Z8Z9Z:Z;Z<e=dKee?e.e;e0j@z dPzZ:e3Ae:e;\Z8ZBeBD]\ZCZDe eDdQEdRdPdZFe eDdSZGe.eDdZHe=dTIeCe>eFdUe>eGdVeHe3JdSdS)YaAn NNTP client class based on: - RFC 977: Network News Transfer Protocol - RFC 2980: Common NNTP Extensions - RFC 3977: Network News Transfer Protocol (version 2) Example: >>> from nntplib import NNTP >>> s = NNTP('news') >>> resp, count, first, last, name = s.group('comp.lang.python') >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last) Group comp.lang.python has 51 articles, range 5770 to 5821 >>> resp, subs = s.xhdr('subject', '{0}-{1}'.format(first, last)) >>> resp = s.quit() >>> Here 'resp' is the server response line. Error responses are turned into exceptions. To post an article from a file: >>> f = open(filename, 'rb') # file containing article, including header >>> resp = s.post(f) >>> For descriptions of all methods, read the comments in the code below. Note that all arguments and return values representing article numbers are strings, not numbers, since they are rarely used for calculations. NTF) decode_header)_GLOBAL_DEFAULT_TIMEOUT)NNTP NNTPErrorNNTPReplyErrorNNTPTemporaryErrorNNTPPermanentErrorNNTPProtocolError NNTPDataErrorr) )removeiceZdZdZdZdS)rz%Base class for all nntplib exceptionscvtj|g|R |d|_dS#t$r d|_YdSwxYw)NrzNo response given) Exception__init__response IndexError)selfargss ..\python\lib\nntplib.pyrzNNTPError.__init__esW4'$'''' 0 GDMMM 0 0 0/DMMMM 0s #88N)__name__ __module__ __qualname____doc__rrrrcs)//00000rrceZdZdZdS)rzUnexpected [123]xx replyNrrrrrrrrrls""DrrceZdZdZdS)rz 4xx errorsNrrrrrrpDrrceZdZdZdS)r z 5xx errorsNrrrrr r tr!rr ceZdZdZdS)r z"Response does not begin with [1-5]Nrrrrr r xs,,Drr ceZdZdZdS)r zError in response dataNrrrrr r |s  Drr wi3> 100101211215220221222224225230231282)subjectfromdatez message-id references:bytes:linesr6r7)byteslines GroupInfo)grouplastfirstflag ArticleInfo)number message_idr9cg}t|D]Z\}}t|tr+|||pdE||[d|S)zvTakes a unicode string representing a munged header value and decodes it as a (possibly non-ASCII) readable value.ascii)_email_decode_header isinstancer8appenddecodejoin) header_strpartsvencs rrrs| E&z223 a    LL#.11 2 2 2 2 LLOOOO 775>>rcg}|D]}|ddkr'|ddd\}}}d|z}n|d\}}}|}t||}||t }t |t |krtd|dt ||krtd|S)zParse a list of string representing the response to LIST OVERVIEW.FMT and return a list of header/metadata names. Raises NNTPDataError if the response is not compliant (cf. RFC 3977, section 8.4).r:Nz$LIST OVERVIEW.FMT response too shortz*LIST OVERVIEW.FMT redefines default fields) partitionlower_OVERVIEW_FMT_ALTERNATIVESgetrH_DEFAULT_OVERVIEW_FMTlenr )r9fmtlinename_suffixdefaultss r_parse_overview_fmtr^s C   7c>>"122h0055OD!V:DD#nnS11OD!Vzz||)--dD99 4$H 3xx#h--BCCC >CMM>h&&HIII Jrc(tt}g}|D]}i}|d^}}t|}t |D]\} } | t|kr|| } | d} | |kr`| s^| dz} | r<| dt| | krtd| r| t| dnd} | ||| <|||f|S)zZParse the response to an OVER or XOVER command according to the overview format `fmt`. rPz: Nz?OVER/XOVER response doesn't include names of additional headers) rWrVsplitint enumerate startswithrSr rH)r9rXdata_process_func n_defaultsoverviewrYfieldsarticle_numbertokensitoken field_name is_metadatahs r_parse_overviewrpsD*++JH22"&**T"2"2^,,!&)) # #HAuCHH}}QJ$//44KJ{%GU7CFF7^1133q88')FGGG*/9c!ffggT"F3q6NN01111 Orc||dd}|dd}t|dd}t|dd}t|dd}t|dd}t|dd}t|dd}|dkr|dz }n |d kr|d z }tj||||||S) zParse a pair of (date, time) strings, and return a datetime object. If only the date is given, it is assumed to be date and time concatenated together (e.g. response to the DATE command). NiFidil)rbdatetime)date_strtime_strhoursminutessecondsyearmonthdays r_parse_datetimers BCC=CRC= !  E(1Q3-  G(122,G x}  D B E hrssm  C byy       T5#ugw G GGrct|tjsd}nd|}|j}|r|dz}d||}nd||}||fS)aPFormat a date or datetime object as a pair of (date, time) strings in the format required by the NEWNEWS and NEWGROUPS commands. If a date object is passed, the time is assumed to be midnight (00h00). The returned representation depends on the legacy flag: * if legacy is False (the default): date has the YYYYMMDD format and time the HHMMSS format * if legacy is True: date has the YYMMDD format and time the HHMMSS format. RFC 3977 compliant servers should understand both formats; therefore, legacy is only needed when talking to old servers. 000000z({0.hour:02d}{0.minute:02d}{0.second:02d}rwz{0:02d}{1.month:02d}{1.day:02d}z{0:04d}{1.month:02d}{1.day:02d})rGrxformatr~)dtlegacyrzyrys r_unparse_datetimers b(+ , ,I=DDRHH A C G4;;ArBB4;;ArBB X rcZ|tj}|||S)zWrap a socket in SSL/TLS. Arguments: - sock: Socket to wrap - context: SSL context to use for the encrypted connection Returns: - sock: New, encrypted socket. N)server_hostname)ssl_create_stdlib_context wrap_socket)sockcontexthostnames r _encrypt_onrs0 ?022G""4"BBBrceZdZdZdZeddddefdZdZdZ dZ d Z d Z d Z d ZeZd ZdZd8dZdZd9dZdZd9dZd9dZdZdZdZdddZdddZd9dddZdZdZdZ d Z!ddd!Z"d"Z#d#Z$d9d$Z%d%Z&d&Z'd9d'Z(d9ddd(Z)d9ddd)Z*d9ddd*Z+d+Z,ddd,Z-ddd-Z.ddd.Z/d/Z0d0Z1d1Z2d2Z3d3Z4d4Z5d:d5Z6d6Z7e8rd9d7Z9dSdS);rzutf-8surrogateescapeNFc||_||_|||_d|_ |jd|_|||s|r||||dSdS#|jr|j|jxYw)a,Initialize an instance. Arguments: - host: hostname to connect to - port: port to connect to (default the standard NNTP port) - user: username to authenticate with - password: password to use with username - readermode: if true, send 'mode reader' command after connecting. - usenetrc: allow loading username and password from ~/.netrc file if not specified explicitly - timeout: timeout (in seconds) used for socket connections readermode is sometimes necessary if you are connecting to an NNTP server on the local machine and intend to call reader-specific commands, such as `group'. If you get unexpected NNTPPermanentErrors, you might need to set readermode. Nrwb) hostport_create_socketrfilemakefile _base_initloginclose)rrruserpassword readermodeusenetrctimeouts rrz NNTP.__init__;s(  ''00    **511DI OOJ ' ' ' 5x 5 4844444 5 5 y " !!! IOO    s ABzNNTP.__exit__..swtV44r)quitOSErrorEOFError_close)rr is_connecteds` r__exit__z NNTP.__exit__s4444 <>> " " X&     <>>"KKMMMMM""<<>>"KKMMMM"  " "s!'A ;A ;A !Bc||stdtjd||j|jt j|j|jf|S)Nz0Non-blocking socket (timeout=0) is not supportedznntplib.connect) ValueErrorsysauditrrsocketcreate_connection)rrs rrzNNTP._create_socketsS  w OPP P #T49di@@@'DI(>HHHrcb|jr"tdt|j|jS)zGet the welcome message from the server (this is read and squirreled away by __init__()). If the response code is 200, posting is allowed; if it 201, posting is not allowed.z *welcome*)rprintreprrrs r getwelcomezNNTP.getwelcomes- >A5d4<.@.@AAA|rc`|jd|_d|_ |\}}||_d|vr-t t t |d|_d|vr d|d|_n#ttf$r i|_YnwxYw|jS)zGet the server capabilities, as read by __init__(). If the CAPABILITIES command is not supported, an empty dict is returned.NrQVERSIONIMPLEMENTATION ) r nntp_versionnntp_implementation capabilitiesmaxmaprbrJr r)rrespcapss rrzNNTP.getcapabilitiess :  !D '+D $ P!..00 d " $$),CT)_,E,E(F(FD%#t++/2xx=M8N/O/OD,'(:;     zsB B&%B&c||_dS)zSet the debugging level. Argument 'level' means: 0: no debugging output (default) 1: print commands and responses but not body text etc. 2: also print raw lines read and sent before stripping CR/LFN)r)rlevels rset_debuglevelzNNTP.set_debuglevels rctjd|||tz}|jdkrt dt ||j||jdS)zfInternal: send one line to the server, appending CRLF. The `line` must be a bytes-like object.znntplib.putlinerQz*put*N) rr_CRLFrrrrwriteflushrrYs r_putlinez NNTP._putlinesp #T4000e| >A  uWd4jj999  rc|jrtdt|||j|j}||dS)zkInternal: send one command to the server (through _putline()). The `line` must be a unicode string.z*cmd*N)rrrencodeencodingerrorsrrs r_putcmdz NNTP._putcmdsP >55$t**555{{4=$+66 drTcp|jtdz}t|tkrt d|jdkrt dt||st|r9|ddtkr |dd}n|ddtvr |dd}|S)zInternal: return one line from the server, stripping _CRLF. Raise EOFError if the connection is closed. Returns a bytes object.rQz line too longz*get*ruN) rreadline_MAXLINErWr rrrrr)r strip_crlfrYs r_getlinez NNTP._getlinesy!!(A+.. t99x  00 0 >A   '4:: & & &#8^  !BCCyE!!CRCybcce##CRCy rcD|}|jrtdt|||j|j}|dd}|dkrt||dkrt||dvrt||S)zInternal: get a response from the server. Raise various errors if the response indicates an error. Returns a unicode string.z*resp*NrQ45123) rrrrrIrrrr r )rrcs rrz NNTP._getresps}} >654::666{{4=$+66 !H 88$T** * 88$T** * E>>#D)) ) rcd} t|ttfrt|dx}}|}|ddt vrt |g}|]dtzdf} |d}||vrn5| dr |dd}| |OnSd} |}||krn5| dr |dd}| |P|r| n#|r| wwxYw||fS) aPInternal: get a response plus following text from the server. Raise various errors if the response indicates an error. Returns a (response, lines) tuple where `response` is a unicode string and `lines` is a list of bytes objects. If `file` is a file-like object, it must be open in binary mode. Nwbr .s. rQFs..) rGstrr8openr _LONGRESPrrrrdrrHr)rr openedFilerr9 terminatorsrY terminators r _getlongrespzNNTP._getlongresps  #$e -- 5$(t$4$44 T==??DBQBxy(($T***E#e|V4 %==//D{**u--(#ABBxJJt$$$ % " '==??Dz))u--(#ABBxLL&&& ' #  """ #  """" #U{s DD11E cT|||S)zWInternal: send a command and get the response. Same return value as _getresp().)rrrs r _shortcmdzNNTP._shortcmds$ T}}rcV||||S)zoInternal: send a command and get the response plus following text. Same return value as _getlongresp().rr)rrYrs r_longcmdz NNTP._longcmd!s* T  &&&rc~||\}}|fd|DfS)zInternal: send a command and get the response plus following text. Same as _longcmd() and _getlongresp(), except that the returned `lines` are unicode strings rather than bytes objects. cPg|]"}|jj#Sr)rIrr).0rYrs r z'NNTP._longcmdstring...s;(((kk$-==(((rr)rrYrrlists` r_longcmdstringzNNTP._longcmdstring'sa T&&t,, d(((("&(((( (rc |jS#t$rYnwxYw |d\}}t|}n#t$rt dd}YnwxYw||_|S)zqInternal: get the overview format. Queries the server if not already done, else returns the cached value.zLIST OVERVIEW.FMTN)_cachedoverviewfmtAttributeErrorrr^r rV)rrr9rXs r_getoverviewfmtzNNTP._getoverviewfmt1s * *    D  ---.ABBKD% &e,,CC " + + +'*CCC + #& s AAAcd|DS)NcDg|]}t|Sr)r;ra)rrYs rrz#NNTP._grouplist..Ds%;;;T 4::<<(;;;rr)rr9s r _grouplistzNNTP._grouplistBs;;U;;;;rc~i}|d\}}|D]}|^}}|||<||fS)a!Process a CAPABILITIES command. Not supported by all servers. Return: - resp: server response if successful - caps: a dictionary mapping capability names to lists of tokens (for example {'VERSION': ['2'], 'OVER': [], LIST: ['ACTIVE', 'HEADERS'] }) CAPABILITIES)rra)rrrr9rYrZrjs rrzNNTP.capabilitiesFsU)).99 e  D JJLLMD6DJJTzr)rcjt|tjtjfs,td|jjt||jdk\}}d||}| ||\}}|| |fS)zProcess a NEWGROUPS command. Arguments: - date: a date or datetime object Return: - resp: server response if successful - list: list of newsgroup names Athe date parameter must be a date or datetime object, not '{:40}'rrzNEWGROUPS {0} {1}) rGrxr4 TypeErrorr __class__rrrrr)rr4rryrzcmdrr9s r newgroupszNNTP.newgroupsTs$ >?? ?$fT^%<==?? ?/tT5F5JKK(!((8<<))#t44 eT__U++++rc8t|tjtjfs,td|jjt||jdk\}}d|||}| ||S)zProcess a NEWNEWS command. Arguments: - group: group name or '*' - date: a date or datetime object Return: - resp: server response if successful - list: list of message ids r rrzNEWNEWS {0} {1} {2}) rGrxr4r rr rrrr)rr<r4rryrzr s rnewnewsz NNTP.newnewsds$ >?? ?$fT^%<==?? ?/tT5F5JKK(#**5(HEE""3---rcv|d|z}nd}|||\}}|||fS)a@Process a LIST or LIST ACTIVE command. Arguments: - group_pattern: a pattern indicating which groups to query - file: Filename string or file object to store the result in Returns: - resp: server response if successful - list: list of (group, last, first, flag) (strings) Nz LIST ACTIVE LIST)rr)r group_patternrcommandrr9s rrz NNTP.listtsK  $$}4GGG))'488 eT__U++++rcxtjd}|d|z\}}|ds|d|z\}}i}|D]O}||}|r$|dd\} } |s| cS| || <P|r||fSdS)Nz^(?P[^ ]+)[ ]+(.*)$zLIST NEWSGROUPS r)zXGTITLE rQrrrE)recompilerrdsearchstripr<) rr return_allline_patrr9groupsraw_linematchrZdescs r_getdescriptionszNNTP._getdescriptionss:>??))*<}*LMM eu%% J--j=.HIIKD% $ $HOOHNN$4$455E $"[[A.. d! KKK#t  < 2rc.||dS)aGet a description for a single group. If more than one group matches ('group' is a pattern), return the first. If no group matches, return an empty string. This elides the response code from the server, since it can only be '215' or '285' (for xgtitle) anyway. If the response code is needed, use the 'descriptions' method. NOTE: This neither checks for a wildcard in 'group' nor does it check whether the group actually exists.Fr)rr<s r descriptionzNNTP.descriptions$$UE222rc.||dS)z'Get descriptions for a range of groups.Tr!)rrs r descriptionszNNTP.descriptionss$$]D999rc|d|z}|dst||}dx}x}}t |}|dkrD|d}|dkr6|d}|dkr(|d}|dkr|d}|t |t |t ||fS)aProcess a GROUP command. Argument: - group: the group name Returns: - resp: server response if successful - count: number of articles - first: first article number - last: last article number - name: the group name zGROUP r(rrQrrr rs)rrdrrarWrSrb)rrZrwordscountr>r=ns rr<z NNTP.groups~~ho..u%% ' && &     JJ q55!HE1uuaq55 8D1uu$Qx~~//SZZUSYY<  )==!2!2rr4rr s rheadz NNTP.head8  ###L11CCC||C&&&rcb|d|}nd}|||S)a+Process a BODY command. Argument: - message_spec: article number or message id - file: filename string or file object to store the body in Returns: - resp: server response if successful - ArticleInfo: (article number, message id, list of body lines) NzBODY {0}BODYrArBs rbodyz NNTP.bodyrDrcb|d|}nd}|||S)a5Process an ARTICLE command. Argument: - message_spec: article number or message id - file: filename string or file object to store the article in Returns: - resp: server response if successful - ArticleInfo: (article number, message id, list of article lines) Nz ARTICLE {0}ARTICLErArBs rarticlez NNTP.articles8  #&&|44CCC||C&&&rc,|dS)zYProcess a SLAVE command. Returns: - resp: server response if successful SLAVE)rrs rslavez NNTP.slave$s~~g&&&rctjd|d|||\}}fd|fd|DfS)aiProcess an XHDR command (optional server extension). Arguments: - hdr: the header type (e.g. 'subject') - str: an article nr, a message id, or a range nr1-nr2 - file: Filename string or file object to store the result in Returns: - resp: server response if successful - list: list of (nr, value) strings z^([0-9]+) ?(.*) ?z XHDR {0} {1}cb|}|r|ddn|S)NrQrr)rr<)rYmpats r remove_numberz NNTP.xhdr..remove_number5s- $A$%/1771a===4 /rc&g|] }|Srr)rrYrRs rrzNNTP.xhdr..8s#<<>%% %Aw t99??%% %_T40000rcB||}|dst|t|tt fr|}|D]m}|ts| dtz}|drd|z}|j |n|j d|j | S)N3r:rs. )rrdrrGr8 bytearray splitlinesendswithrrstriprrrr)rrfrrYs r_postz NNTP._postts~~g&&s## ' && & a%+ , ,  A  " "D=='' 4{{7++e3t$$ #d{ IOOD ! ! ! ! !!! }}rc.|d|S)zProcess a POST command. Arguments: - data: bytes object, iterable or file containing the article Returns: - resp: server response if successfulPOST)rk)rdatas rpostz NNTP.posts zz&$'''rcT|d||S)a Process an IHAVE command. Arguments: - message_id: message-id of the article - data: file containing the article Returns: - resp: server response if successful Note that if the server refuses the article an exception is raised.z IHAVE {0})rkr)rrBrns rihavez NNTP.ihaves&zz+,,Z88$???rc |jr|j|`|jdS#|jwxYwr)rrrrs rrz NNTP._closesX y  !!!I IOO     DIOO    s "?Ac |d}|n#|wxYw|S)zdProcess a QUIT command and close the socket. Returns: - resp: server response if successfulQUIT)rr)rrs rrz NNTP.quits> >>&))D KKMMMMDKKMMMM s ,Ac|jrtd|s|std |rF|sDddl}|}||j}|r|d}|d}n#t $rYnwxYw|sdS|d|z}|drM|st||d|z}|dst|d|_ | |j r:d |j vr3| d|_ | dSdSdS) NzAlready logged in.z7At least one of `user` and `usenetrc` must be specifiedrrrzauthinfo user 381zauthinfo pass 281r)rrnetrcauthenticatorsrrrrdrr rrrr)rrrrrx credentialsauthrs rrz NNTP.logins   3122 2 KH KIKK K  ' ' #kkmm "11$)<<'7D#AwH    D   F~~.566 ??5 ! ! 3 3$T***~~&6&ABBu--3,T222    $ #)C)C    ! ! !DJ  " " " " "  # #)C)CsAA44 BBc |d|_dS#t$rYdSt$r.}|jdrd|_nYd}~dSd}~wwxYw)Nz mode reader480T)rrr rrrdr)res rrzNNTP._setreadermodes >>-88DLLL!    DD!   z$$U++ ,0))*))))) s A" A"#AA"c|jrtd|jrtd|d}|dr||jt|j||j |_|j d|_d|_d|_ | dStd) zzProcess a STARTTLS command. Arguments: - context: SSL context to use for the encrypted connection zTLS is already enabled.z+TLS cannot be started after authentication.STARTTLS382rTNzTLS failed to start.)rrrrrdrrrrrrrrr)rrrs rstarttlsz NNTP.starttlss { < !:;;;! P !NOOO>>*--Du%% 8 !!!' 7DIFF  I..u55 " " $$&&&&& 6777r)Tr)NNT):rrrrr NNTP_PORTrrrrrrrrrdebugrrrrrrrrrrrr rrrr"r$r<r+r/r1r5r9r=r>rCrGrJrMrUrYr^r4rkrorqrrrr _have_sslrrrrrr+sH F"+$ 50!!!!F # # #D " " "III , E" ,,,,\ '''' (((("<<<   '+,,,,, ,0..... ,t , , , , ,. 3 3 3:::===4 11111)))%%% ) ) ) )%%%%%%==== 'd ' ' ' ' ' 'd ' ' ' ' ' ' ' ' ' ' '''' &*===== )- 1 1 1 1 1*.111116111"*(((@@@'#'#'#'#R   8 8 8 8 8 8 888rrc8eZdZedddddeffd ZfdZxZS)NNTP_SSLNFc d||_t|||||||dS)zThis works identically to NNTP.__init__, except for the change in default port and the `ssl_context` argument for SSL connections. N) ssl_contextsuperr) rrrrrrrrrr s rrzNNTP_SSL.__init__sB +D  GG  T4x%w 0 0 0 0 0rct|} t||j|j}|S#|xYwr)rrrrrr)rrrr s rrzNNTP_SSL._create_socketsU77))'22D "4)949EE    s AA)rrr NNTP_SSL_PORTrrr __classcell__)r s@rrrsh&3$#e3 0 0 0 0 0 0         rr__main__zJ nntplib built-in demo - display the latest articles in a newsgroup)r"z-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+)rrrGrouphaszarticles, rangetocLt||kr|d|dz dz}|S)Nrsz...)rW)slims rcutr6s, q66C<<(37( e#ArrQr3r=rZrrr nb_articlesrY overviewsartnumr^raauthorr2r9rrrrrrseB  JJJIIIII ?>>>>>******    Xg.... 00000 000     Y                                I         OOO    "K ";#E#E#E G G %k$]%F%F%FHH   2:HHHH,4 C C C CK 8K 8K 8K 8K 8K 8K 8K 8\ 4. NN: zOOO $X $2NOOOF i1LRTTT j/JLLL hFOiiQ^Q^Q^_aaa orQSSS glE0222     D 9D 82 2::D Ddk - - - 2:: D H$+D 1 1 1    DT %&WWTZ%8%8"D%d E'4'8%tLLL CD D,,q0 1 1EggeT**OD)! tF|,,223::1=-Y00DN## %,,cc&"ooss7B'7'7@@    FFHHHHHas #--