�`^c@s)dZddlZddlZddddddd d
ddd
gZdZdefd��YZdefd��YZdefd��YZdefd��YZ defd��YZ
defd��YZeZeZ
e Ze
ZeZdZdddddddddg Zd Zdfd!��YZed"kr%ddlZd#oUejd$Zejd%�dkrvd&ZneZeed&e�Zejd'�\ZZZ Z!Z"eGHd(Ge"Gd)GeGd*Ge Gd+Ge!GHej#d,e d-e!�\ZZ$eGHxe$D]Z%d.e%GHq�j&�ZeGHndS(/sHAn NNTP client class based on RFC 977: Network News Transfer Protocol.
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', 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, 'r') # 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.
i�NtNNTPtNNTPReplyErrortNNTPTemporaryErrortNNTPPermanentErrortNNTPProtocolErrort
NNTPDataErrorterror_replyt
error_tempt
error_permterror_protot
error_datait NNTPErrorcBseZdZd�ZRS(s%Base class for all nntplib exceptionscGsBtj||�y|d|_Wntk
r=d|_nXdS(NisNo response given(t Exceptiont__init__tresponset
IndexError(tselftargs((s/sys/lib/python2.7/nntplib.pyR
2s
(t__name__t
__module__t__doc__R
(((s/sys/lib/python2.7/nntplib.pyR0scBseZdZRS(sUnexpected [123]xx reply(RRR(((s/sys/lib/python2.7/nntplib.pyR9scBseZdZRS(s
4xx errors(RRR(((s/sys/lib/python2.7/nntplib.pyR=scBseZdZRS(s
5xx errors(RRR(((s/sys/lib/python2.7/nntplib.pyRAscBseZdZRS(s"Response does not begin with [1-5](RRR(((s/sys/lib/python2.7/nntplib.pyREscBseZdZRS(sError in response data(RRR(((s/sys/lib/python2.7/nntplib.pyRIsiwt100t215t220t221t222t224t230t231t282s
cBsyeZed#d#d#ed�Zd�Zd�ZeZd�Z d�Z
d�Zd�Zd#d�Z
d�Zd#d �Zd#d
�Zd#d�Zd#d�Zd
�Zd�Zd�Zd#d�Zd�Zd�Zd�Zd�Zd�Zd#d�Zd�Zd#d�Zd�Zd�Z d#d�Z!d#d�Z"d#d�Z#d�Z$d�Z%d �Z&d!�Z'd"�Z(RS($c
Cs�||_||_tj||f�|_|jjd�|_d|_|j�|_ d}|r�y|j
d�|_ Wq�tk
r�q�tk
r�}|r�|j
d dkr�d}q��q�XnyX|r|rddl} | j�}
|
j|�}|r|d}|d }qnWntk
r2nX|r�|j
d
|�}|d dkr�|sqt|��q�|j
d|�}|d d
kr�t|��q�n|r�y|j
d�|_ Wq�tk
r�q�Xq�ndS(smInitialize 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.
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.
trbismode readerit480ii�Nisauthinfo user t381sauthinfo pass t281(thosttporttsockettcreate_connectiontsocktmakefiletfilet debuggingtgetresptwelcometshortcmdRRRtnetrctauthenticatorstIOErrorR(
RR"R#tusertpasswordt
readermodetusenetrctreadermode_afterauthteR-tcredentialstauthtresp((s/sys/lib/python2.7/nntplib.pyR
esP
cCs%|jrdGt|j�GHn|jS(s�Get 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.s *welcome*(R)treprR+(R((s/sys/lib/python2.7/nntplib.pyt
getwelcome�s cCs
||_dS(s�Set 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)(Rtlevel((s/sys/lib/python2.7/nntplib.pytset_debuglevel�scCs?|t}|jdkr+dGt|�GHn|jj|�dS(s6Internal: send one line to the server, appending CRLF.is*put*N(tCRLFR)R9R&tsendall(Rtline((s/sys/lib/python2.7/nntplib.pytputline�s
cCs,|jrdGt|�GHn|j|�dS(s=Internal: send one command to the server (through putline()).s*cmd*N(R)R9R@(RR?((s/sys/lib/python2.7/nntplib.pytputcmd�s cCs�|jjtd�}t|�tkr7td��n|jdkrXdGt|�GHn|sgt�n|dtkr�|d }n|dtkr�|d }n|S(snInternal: return one line from the server, stripping CRLF.
Raise EOFError if the connection is closed.is
line too longs*get*i�i�( R(treadlinet_MAXLINEtlenRR)R9tEOFErrorR=(RR?((s/sys/lib/python2.7/nntplib.pytgetline�s
cCs�|j�}|jr'dGt|�GHn|d }|dkrLt|��n|dkrgt|��n|dkr�t|��n|S(sjInternal: get a response from the server.
Raise various errors if the response indicates an error.s*resp*it4t5t123(RFR)R9RRR(RR8tc((s/sys/lib/python2.7/nntplib.pyR*�s
cCs�d}z�t|t�r.t|d�}}n|j�}|d tkrYt|��ng}xc|j�}|dkr~Pn|d dkr�|d}n|r�|j|d�qb|j |�qbWd|r�|j
�nX||fS( s~Internal: get a response plus following text from the server.
Raise various errors if the response indicates an error.twit.is..is
N(tNonet
isinstancetstrtopenR*tLONGRESPRRFtwritetappendtclose(RR(t
openedFileR8tlistR?((s/sys/lib/python2.7/nntplib.pytgetlongresp�s(
cCs|j|�|j�S(s.Internal: send a command and get the response.(RAR*(RR?((s/sys/lib/python2.7/nntplib.pyR, s
cCs|j|�|j|�S(sBInternal: send a command and get the response plus following text.(RARW(RR?R(((s/sys/lib/python2.7/nntplib.pytlongcmds
cCs|jd|d||�S(s�Process a NEWGROUPS command. Arguments:
- date: string 'yymmdd' indicating the date
- time: string 'hhmmss' indicating the time
Return:
- resp: server response if successful
- list: list of newsgroup namess
NEWGROUPS t (RX(RtdatettimeR(((s/sys/lib/python2.7/nntplib.pyt newgroupsscCs*d|d|d|}|j||�S(sProcess a NEWNEWS command. Arguments:
- group: group name or '*'
- date: string 'yymmdd' indicating the date
- time: string 'hhmmss' indicating the time
Return:
- resp: server response if successful
- list: list of message idssNEWNEWS RY(RX(RtgroupRZR[R(tcmd((s/sys/lib/python2.7/nntplib.pytnewnewss cCsY|jd|�\}}x4tt|��D] }t||j��||<q+W||fS(s�Process a LIST command. Return:
- resp: server response if successful
- list: list of (group, last, first, flag) (strings)tLIST(RXtrangeRDttupletsplit(RR(R8RVti((s/sys/lib/python2.7/nntplib.pyRV)scCs;|j|�\}}t|�dkr+dS|ddSdS(s�Get 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.itiN(tdescriptionsRD(RR]R8tlines((s/sys/lib/python2.7/nntplib.pytdescription4s
cCs�tjd�}|jd|�\}}|d dkrT|jd|�\}}ng}xE|D]=}|j|j��}|ra|j|jdd��qaqaW||fS(s'Get descriptions for a range of groups.s^(?P<group>[^ ]+)[ ]+(.*)$sLIST NEWSGROUPS iRsXGTITLE ii(tretcompileRXtsearchtstripRSR](Rt
group_patterntline_patR8t raw_linesRgtraw_linetmatch((s/sys/lib/python2.7/nntplib.pyRfGs
cCs�|jd|�}|d dkr2t|��n|j�}d}}}t|�}|dkr�|d}|dkr�|d}|dkr�|d}|dkr�|dj�}q�q�q�n|||||fS(s*Process a GROUP command. Argument:
- group: the group name
Returns:
- resp: server response if successful
- count: number of articles (string)
- first: first article number (string)
- last: last article number (string)
- name: the group namesGROUP it211iiii(R,RRcRDtlower(RtnameR8twordstcounttfirsttlasttn((s/sys/lib/python2.7/nntplib.pyR]Xs
cCs|jd|�S(soProcess a HELP command. Returns:
- resp: server response if successful
- list: list of stringstHELP(RX(RR(((s/sys/lib/python2.7/nntplib.pythelprscCs�|d dkrt|��n|j�}d}d}t|�}|dkru|d}|dkru|d}qun|||fS(s=Internal: parse the response of a STAT, NEXT or LAST command.it22iRei(RRcRD(RR8RutnrtidRy((s/sys/lib/python2.7/nntplib.pyt statparseys
cCs|j|�}|j|�S(s/Internal: process a STAT, NEXT or LAST command.(R,R(RR?R8((s/sys/lib/python2.7/nntplib.pytstatcmd�scCs|jd|�S(s�Process a STAT command. Argument:
- id: article number or message id
Returns:
- resp: server response if successful
- nr: the article number
- id: the message idsSTAT (R�(RR~((s/sys/lib/python2.7/nntplib.pytstat�scCs
|jd�S(s;Process a NEXT command. No arguments. Return as for STAT.tNEXT(R�(R((s/sys/lib/python2.7/nntplib.pytnext�scCs
|jd�S(s;Process a LAST command. No arguments. Return as for STAT.tLAST(R�(R((s/sys/lib/python2.7/nntplib.pyRx�scCs@|j||�\}}|j|�\}}}||||fS(s2Internal: process a HEAD, BODY or ARTICLE command.(RXR(RR?R(R8RVR}R~((s/sys/lib/python2.7/nntplib.pytartcmd�scCs|jd|�S(s�Process a HEAD command. Argument:
- id: article number or message id
Returns:
- resp: server response if successful
- nr: article number
- id: message id
- list: the lines of the article's headersHEAD (R�(RR~((s/sys/lib/python2.7/nntplib.pythead�s cCs|jd||�S(skProcess a BODY command. Argument:
- id: article number or message id
- file: Filename string or file object to store the article in
Returns:
- resp: server response if successful
- nr: article number
- id: message id
- list: the lines of the article's body or an empty list
if file was usedsBODY (R�(RR~R(((s/sys/lib/python2.7/nntplib.pytbody�scCs|jd|�S(s�Process an ARTICLE command. Argument:
- id: article number or message id
Returns:
- resp: server response if successful
- nr: article number
- id: message id
- list: the lines of the articlesARTICLE (R�(RR~((s/sys/lib/python2.7/nntplib.pytarticle�s cCs
|jd�S(sPProcess a SLAVE command. Returns:
- resp: server response if successfultSLAVE(R,(R((s/sys/lib/python2.7/nntplib.pytslave�sc
Cs�tjd�}|jd|d||�\}}xRtt|��D]>}||}|j|�} | rF| jdd�||<qFqFW||fS(sProcess 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
Returns:
- resp: server response if successful
- list: list of (nr, value) stringss^([0-9]+) ?(.*)
?sXHDR RYii(RiRjRXRaRDRqR](
RthdrROR(tpatR8RgRdR?tm((s/sys/lib/python2.7/nntplib.pytxhdr�s$
c Cs�|jd|d||�\}}g}x�|D]�}|jd�}yO|j|d|d|d|d|d|d j�|d
|df�Wq1tk
r�t|��q1Xq1W||fS(s(Process an XOVER command (optional server extension) Arguments:
- start: start of range
- end: end of range
Returns:
- resp: server response if successful
- list: list of (art-nr, subject, poster, date,
id, references, size, lines)sXOVER t-s iiiiiiii(RXRcRSRR( RtstarttendR(R8Rgtxover_linesR?telem((s/sys/lib/python2.7/nntplib.pytxover�s $
c Cs�tjd�}|jd||�\}}g}xE|D]=}|j|j��}|r8|j|jdd��q8q8W||fS(s�Process an XGTITLE command (optional server extension) Arguments:
- group: group name wildcard (i.e. news.*)
Returns:
- resp: server response if successful
- list: list of (name,title) stringss^([^ ]+)[ ]+(.*)$sXGTITLE ii(RiRjRXRkRlRSR]( RR]R(RnR8RoRgRpRq((s/sys/lib/python2.7/nntplib.pytxgtitle�s
cCsv|jd|�}|d dkr2t|��ny|j�\}}Wntk
rgt|��nX||fSdS(s�Process an XPATH command (optional server extension) Arguments:
- id: Message id of article
Returns:
resp: server response if successful
path: directory path to articlesXPATH it223N(R,RRct
ValueError(RR~R8tresp_numtpath((s/sys/lib/python2.7/nntplib.pytxpaths
cCs�|jd�}|d dkr.t|��n|j�}t|�dkr[t|��n|ddd!}|dd}t|�dks�t|�dkr�t|��n|||fS( s�Process the DATE command. Arguments:
None
Returns:
resp: server response if successful
date: Date suitable for newnews/newgroups commands etc.
time: Time suitable for newnews/newgroups commands etc.tDATEit111iiii�i(R,RRcRDR(RR8R�RZR[((s/sys/lib/python2.7/nntplib.pyRZs$cCs�|jd�}|ddkr.t|��nx`|j�}|sGPn|ddkrd|d }n|d dkr�d|}n|j|�q1|jd�|j�S(s�Process a POST command. Arguments:
- f: file containing the article
Returns:
- resp: server response if successfultPOSTit3i�s
iRL(R,RRBR@R*(RtfR8R?((s/sys/lib/python2.7/nntplib.pytpost.s
cCs�|jd|�}|ddkr2t|��nx`|j�}|sKPn|ddkrh|d }n|d dkr�d|}n|j|�q5|jd�|j�S(sProcess an IHAVE command. Arguments:
- id: message-id of the article
- f: file containing the article
Returns:
- resp: server response if successful
Note that if the server refuses the article an exception is raised.sIHAVE iR�i�s
iRL(R,RRBR@R*(RR~R�R8R?((s/sys/lib/python2.7/nntplib.pytihaveDs
cCs9|jd�}|jj�|jj�|`|`|S(sdProcess a QUIT command and close the socket. Returns:
- resp: server response if successfultQUIT(R,R(RTR&(RR8((s/sys/lib/python2.7/nntplib.pytquit\s
N()RRt NNTP_PORTRMtTrueR
R:R<tdebugR@RARFR*RWR,RXR\R_RVRhRfR]R{RR�R�R�RxR�R�R�R�R�R�R�R�R�RZR�R�R�(((s/sys/lib/python2.7/nntplib.pyRdsJ L
t__main__tnewst
NNTPSERVERRLR2scomp.lang.pythontGroupthassarticles, rangettotsubjectR�s%7s %s('RRiR$t__all__RCRRRRRRRRRRR R
R�RQR=RRtostenvirontnewshosttfindtmodeRMtsR]R8RvRwRxRtR�tsubstitemR�(((s/sys/lib/python2.7/nntplib.pyt<module>sP !�
!
|