a Z^?c @s>dZddlZddlZddlZddlZddlZz ddlZWneyNdZYn0dZddl m Z ddlm Z gdZ dZGd d d eZGd d d eZGd ddeZGdddeZGdddeZGdddeZdZdZhdZgdZdddZdZedgdZedgd Zd!d"Z d#d$ZdZd%d&Z d[d'd(Z!d\d)d*Z"erZd+d,Z#Gd-d.d.Z$erGd/d0d0e$Z%e &d0e'd1kr:ddl(Z(e(j)d2d3Z*e*j+d4d5d6d7d8e*j+d9d:d;dd?e,d@eefdAe*j+dBdCdDe,dEdAe*j+dFdGdHddIdJe*-Z.e.j/Z/e.jsBe/d?kr2eZ/e$e.j0e/dKZ1ne/d?krPeZ/e%e.j0e/dKZ1e12Z3dLe3vrxe14e15e.j5\Z6Z7Z8Z9Z:e;dMe:dNe7dOe8dPe9dQdRZdSZ8e1?e8e9\Z6Z@e@D]Z\ZAZBe eBdTCdUdSdZDe eBdVZEe,eBdZFe;dWGeAe>> 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. NFT) decode_header)_GLOBAL_DEFAULT_TIMEOUT)NNTP NNTPErrorNNTPReplyErrorNNTPTemporaryErrorNNTPPermanentErrorNNTPProtocolError NNTPDataErrorric@seZdZdZddZdS)rz%Base class for all nntplib exceptionscGs>tj|g|Rz|d|_Wnty8d|_Yn0dS)NrzNo response given) Exception__init__responseZ IndexError)selfargsr/usr/lib64/python3.9/nntplib.pyr bs  zNNTPError.__init__N)__name__ __module__ __qualname____doc__r rrrrr`rc@eZdZdZdS)rzUnexpected [123]xx replyNrrrrrrrrrirrc@r)rz 4xx errorsNrrrrrrmrrc@r)rz 5xx errorsNrrrrrrqrrc@r)rz"Response does not begin with [1-5]Nrrrrrrurrc@r)r zError in response dataNrrrrrr yrr iwi3> Z224Z282Z101Z222211215Z225Z230Z231Z220Z221Z100)subjectfromdatez message-idZ references:bytes:linesrr)byteslines GroupInfo)grouplastfirstZflag ArticleInfo)Znumber message_idr cCsJg}t|D]2\}}t|tr4|||p,dq ||q d|S)zvTakes a unicode string representing a munged header value and decodes it as a (possibly non-ASCII) readable value.Zascii)_email_decode_header isinstancerappenddecodejoin)Z header_strZpartsZvZencrrrrs   rcCsg}|D]`}|ddkr:|ddd\}}}d|}n|d\}}}|}t||}||qt}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)Z partitionlower_OVERVIEW_FMT_ALTERNATIVESZgetr+_DEFAULT_OVERVIEW_FMTlenr )r fmtlinenameZ_ZsuffixZdefaultsrrr_parse_overview_fmts    r7cCstt}g}|D]}i}|d^}}t|}t|D]\} } | t|krLq6|| } | d} | |kr| s| d} | r| dt| | krtd| r| t| dnd} | ||| <q6|||fq|S)zZParse the response to an OVER or XOVER command according to the overview format `fmt`.z r.z: Nz?OVER/XOVER response doesn't include names of additional headers) r3r2splitintZ enumerate startswithr0r r+)r r4Zdata_process_funcZ n_defaultsZoverviewr5ZfieldsZarticle_numbertokensZiZtokenZ field_nameZ is_metadataZhrrr_parse_overviews&   r<cCs|dur |dd}|dd}t|dd}t|dd}t|dd}t|dd}t|dd}t|dd}|dkr|d7}n|d kr|d 7}t||||||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). NiiiFidil)r9datetime)date_strtime_strZhoursZminutesZsecondsyearZmonthZdayrrr_parse_datetimes   rEcCsPt|tjsd}n d|}|j}|r<|d}d||}n d||}||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. Z000000z({0.hour:02d}{0.minute:02d}{0.second:02d}r@z{0:02d}{1.month:02d}{1.day:02d}z{0:04d}{1.month:02d}{1.day:02d})r*rAformatrD)ZdtZlegacyrCZyrBrrr_unparse_datetimes   rGcCs|durt}|j||dS)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)Zserver_hostname)sslZ_create_stdlib_contextZ wrap_socket)sockcontextZhostnamerrr _encrypt_onsrKc@seZdZdZdZeddddefddZddZd d Z d d Z d dZ ddZ ddZ ddZeZddZddZdiddZddZdjddZd d!Zdkd"d#Zdld$d%Zd&d'Zd(d)Zd*d+Zdd,d-d.Zdd,d/d0Zdmdd,d1d2Zd3d4Zd5d6Zd7d8Z d9d:Z!dd,d;d<Z"d=d>Z#d?d@Z$dndAdBZ%dCdDZ&dEdFZ'dodGdHZ(dpdd,dIdJZ)dqdd,dKdLZ*drdd,dMdNZ+dOdPZ,dd,dQdRZ-dd,dSdTZ.dd,dUdVZ/dWdXZ0dYdZZ1d[d\Z2d]d^Z3d_d`Z4dadbZ5dsdcddZ6dedfZ7e8rdtdgdhZ9dS)urzutf-8ZsurrogateescapeNFcCs~||_||_|||_d|_z2|jd|_|||s@|rN||||Wn(|jrh|j|jYn0dS)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_socketrIfilemakefile _base_initloginclose)r rMrNuserpassword readermodeusenetrctimeoutrrrr 8s    z NNTP.__init__cCs^d|_||_d|_|d|_|rNd|jvrN||jsNd|_|d|_d|_dS)z~Partial initialization for the NNTP protocol. This instance method is extracted for supporting the test code. rNFREADER) debugging_getrespwelcome_capsgetcapabilitiesreadermode_afterauth_setreadermodetls_on authenticated)r rWrrrrR[s  zNNTP._base_initcCs|SNrr rrr __enter__}szNNTP.__enter__c s^fdd}|rZz6z Wnttfy6Yn0W|rZn|rX0dS)Ncs tdS)NrP)ZhasattrrrerrZzNNTP.__exit__..)quitOSErrorEOFError_close)r rZ is_connectedrrer__exit__s   z NNTP.__exit__cCs<|dur|stdtd||j|jt|j|jf|S)Nz0Non-blocking socket (timeout=0) is not supportedznntplib.connect) ValueErrorsysauditrMrNsocketZcreate_connection)r rYrrrrOs zNNTP._create_socketcCs|jrtdt|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*)r[printreprr]rerrr getwelcomeszNNTP.getwelcomec Cs|jdur~d|_d|_z|\}}WnttfyBi|_Yn<0||_d|vrfttt|d|_d|vr~d |d|_|jS)zGet the server capabilities, as read by __init__(). If the CAPABILITIES command is not supported, an empty dict is returned.Nr/ZVERSIONZIMPLEMENTATION ) r^ nntp_versionZnntp_implementation capabilitiesrrZmaxZmapr9r-)r respcapsrrrr_s  zNNTP.getcapabilitiescCs ||_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[)r Zlevelrrrset_debuglevelszNNTP.set_debuglevelcCsHtd|||t}|jdkr.tdt||j||jdS)zfInternal: send one line to the server, appending CRLF. The `line` must be a bytes-like object.znntplib.putliner/z*put*N) rnro_CRLFr[rqrrrPwriteflushr r5rrr_putlines  z NNTP._putlinecCs2|jrtdt|||j|j}||dS)zkInternal: send one command to the server (through _putline()). The `line` must be a unicode string.z*cmd*N)r[rqrrZencodeencodingerrorsr~r}rrr_putcmdsz NNTP._putcmdTcCs|jtd}t|tkr$td|jdkr+sz'NNTP._longcmdstring..r)r r5rPrwlistrrer_longcmdstring$s   zNNTP._longcmdstringcCs`z|jWStyYn0z|d\}}WntyLtdd}Yn 0t|}||_|S)zqInternal: get the overview format. Queries the server if not already done, else returns the cached value.zLIST OVERVIEW.FMTN)Z_cachedoverviewfmtZAttributeErrorrrr2r7)r rwr r4rrr_getoverviewfmt.s  zNNTP._getoverviewfmtcCsdd|DS)NcSsg|]}t|qSr)r"r8rrrrrArgz#NNTP._grouplist..r)r r rrr _grouplist?zNNTP._grouplistcCs8i}|d\}}|D]}|^}}|||<q||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'] }) Z CAPABILITIES)rr8)r rxrwr r5r6r;rrrrvCs   zNNTP.capabilities)rPcCsbt|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}'r=zNEWGROUPS {0} {1}) r*rAr TypeErrorrF __class__rrGrurr)r rrPrBrCcmdrwr rrr newgroupsQs zNNTP.newgroupscCsRt|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 rr=zNEWNEWS {0} {1} {2}) r*rArrrFrrrGrur)r r#rrPrBrCrrrrnewnewsasz NNTP.newnewscCs4|durd|}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 ZLIST)rr)r group_patternrPcommandrwr rrrrqs  z NNTP.listc Cstd}|d|\}}|ds8|d|\}}i}|D]:}||}|r@|dd\} } |sr| S| || <q@|r||fSdSdS)Nz^(?P[^ ]+)[ ]+(.*)$zLIST NEWSGROUPS rzXGTITLE r/r=r()recompilerr:ZsearchZstripr#) r rZ return_allZline_patrwr ZgroupsZraw_linematchr6Zdescrrr_getdescriptionss   zNNTP._getdescriptionscC ||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)r r#rrr descriptions zNNTP.descriptioncCr)z'Get descriptions for a range of groups.Tr)r rrrr descriptionsrzNNTP.descriptionscCs|d|}|ds t||}d}}}t|}|dkr|d}|dkr|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 rrr/r=rr>)rr:rr8r3r0r9)r r6rwwordscountr%r$Znrrrr#s    z NNTP.groupcC |d|S)aProcess a HELP command. Argument: - file: Filename string or file object to store the result in Returns: - resp: server response if successful - list: list of strings returned by the server in response to the HELP command ZHELP)r)r rPrrrhelpsz NNTP.helpcCs8|dst||}t|d}|d}|||fS)z_Internal: parse the response line of a STAT, NEXT, LAST, ARTICLE, HEAD or BODY command.Z22r/r=)r:rr8r9)r rwrart_numr'rrr _statparses   zNNTP._statparsecCs||}||S)z/Internal: process a STAT, NEXT or LAST command.)rr)r r5rwrrr_statcmds z NNTP._statcmdcCs"|r|d|S|dSdS)a(Process a STAT command. Argument: - message_spec: article number or message id (if not specified, the current article is selected) Returns: - resp: server response if successful - art_num: the article number - message_id: the message id zSTAT {0}ZSTATN)rrF)r message_specrrrstats z NNTP.statcC |dS)z;Process a NEXT command. No arguments. Return as for STAT.ZNEXTrrerrrnextrz NNTP.nextcCr)z;Process a LAST command. No arguments. Return as for STAT.ZLASTrrerrrr$rz NNTP.lastcCs0|||\}}||\}}}|t|||fS)z2Internal: process a HEAD, BODY or ARTICLE command.)rrr&)r r5rPrwr rr'rrr_artcmdsz NNTP._artcmdcC$|durd|}nd}|||S)a0Process a HEAD command. Argument: - message_spec: article number or message id - file: filename string or file object to store the headers in Returns: - resp: server response if successful - ArticleInfo: (article number, message id, list of header lines) NzHEAD {0}ZHEADrFrr rrPrrrrhead z NNTP.headcCr)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}ZBODYrrrrrbodyrz NNTP.bodycCr)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}ZARTICLErrrrrarticlerz NNTP.articlecCr)zYProcess a SLAVE command. Returns: - resp: server response if successful ZSLAVE)rrerrrslave!sz NNTP.slavecsDtd|d|||\}}fdd|fdd|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}cs|}|r|ddS|S)Nr/r=)rr#)r5Zm)patrr remove_number2s z NNTP.xhdr..remove_numbercsg|] }|qSrrr)rrrr5rgzNNTP.xhdr..)rrrrF)r ZhdrrrPrwr r)rrrxhdr's  z NNTP.xhdrcCs.|d|||\}}|}|t||fS)aFProcess an XOVER command (optional server extension) Arguments: - start: start of range - end: end of range - file: Filename string or file object to store the result in Returns: - resp: server response if successful - list: list of dicts containing the response fields z XOVER {0}-{1})rrFrr<)r startendrPrwr r4rrrxover7s z NNTP.xoverc Csxd|jvrdnd}t|ttfr>|\}}|d||p6d7}n|durR|d|}|||\}}|}|t||fS)aProcess an OVER command. If the command isn't supported, fall back to XOVER. Arguments: - message_spec: - either a message id, indicating the article to fetch information about - or a (start, end) tuple, indicating a range of article numbers; if end is None, information up to the newest message will be retrieved - or None, indicating the current article number must be used - file: Filename string or file object to store the result in Returns: - resp: server response if successful - list: list of dicts containing the response fields NOTE: the "message id" form isn't supported by XOVER ZOVERZXOVERz {0}-{1}r(Nrt)r^r*ZtuplerrFrrr<) r rrPrrrrwr r4rrroverEs z NNTP.overcCsb|d}|dst||}t|dkr8t||d}t|dkrTt||t|dfS)zProcess the DATE command. Returns: - resp: server response if successful - date: datetime object ZDATEZ111r=r/iN)rr:rr8r3r rE)r rwZelemrrrrr`s    z NNTP.datecCs||}|dst|t|ttfr2|}|D]:}|tsR| dt}|drdd|}|j |q6|j d|j | S)NZ3r!rs. )rr:rr*rZ bytearrayZ splitlinesZendswithrzZrstriprPr{r|r\)r rZfrwr5rrr_postqs      z NNTP._postcCr)zProcess a POST command. Arguments: - data: bytes object, iterable or file containing the article Returns: - resp: server response if successfulZPOST)r)r datarrrpostsz NNTP.postcCs|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})rrF)r r'rrrrihavesz NNTP.ihavecCs4z"|jr|j|`W|jn |j0dSrd)rPrTrIrerrrrks  z NNTP._closecCs&z|d}W|n |0|S)zdProcess a QUIT command and close the socket. Returns: - resp: server response if successfulZQUIT)rrk)r rwrrrrhs z NNTP.quitcCs|jrtd|s|stdz<|rX|sXddl}|}||j}|rX|d}|d}WntylYn0|svdS|d|}|dr|st|n |d|}|dst |d|_ | |j rd |j vr| d|_ | dS) NzAlready logged in.z7At least one of `user` and `usenetrc` must be specifiedrr=zauthinfo user Z381zauthinfo pass Z281rZ)rcrmnetrcZauthenticatorsrMrirr:rrr^r_r`ra)r rUrVrXrZ credentialsZauthrwrrrrSs>      z NNTP.loginc Cs`z|d|_WnJty"Yn:tyZ}z"|jdrDd|_nWYd}~n d}~00dS)Nz mode readerZ480T)rr]rrr r:r`)r Zerrrras  zNNTP._setreadermodecCs||jrtd|jrtd|d}|drp|jt|j||j |_|j d|_d|_d|_ | nt ddS) 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.STARTTLSZ382rLTNzTLS failed to start.)rbrmrcrr:rPrTrKrIrMrQr^r_r)r rJrwrrrstarttlss    z NNTP.starttls)Trdrdrdrdrdrdrdrdrd)NNTrd):rrrrr NNTP_PORTrr rRrfrlrOrsr_ryZdebugr~rrr\rrrrrrrvrrrrrrr#rrrrrr$rrrrrrrrrrrrrkrhrSra _have_sslrrrrrr(sp  #"     .         ) rcs8eZdZedddddeffdd ZfddZZS)NNTP_SSLNFc s"||_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 ) r rMrNrUrVrrWrXrYrrrr szNNTP_SSL.__init__cs@t|}zt||j|j}Wn|Yn0|SdSrd)rrOrKrrMrT)r rYrIrrrrOs zNNTP_SSL._create_socket)rrr NNTP_SSL_PORTrr rOZ __classcell__rrrrrs  rZ__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))rZtyperz-nz --nb-articlesi z2number of articles to fetch (default: %(default)s)z-Sz--sslZ store_truezuse NNTP over SSL)Zactionrr)rMrNrZGroupZhaszarticles, rangeZtocCs$t||kr |d|dd}|S)Nr>z...)r3)sZlimrrrcut3s rr/rzsA           S