ó
è¾bc           @   s)  d  Z  d d l Z d d l Z d d d d d d d	 d
 d d d g Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d e f d „  ƒ  YZ e Z e Z e	 Z e
 Z e Z d Z d d d d d d d d d g	 Z d  Z d f  d! „  ƒ  YZ e d" k r%d d l Z d# oUe j d$ Z e j d% ƒ d k rvd& Z n e Z e e d& e ƒZ e j d' ƒ \ Z Z Z  Z! Z" e GHd( Ge" Gd) Ge Gd* Ge  Gd+ Ge! GHe j# d, e  d- e! ƒ \ Z Z$ e GHx e$ D] Z% d. e% GHqþWe j& ƒ  Z e GHn  d S(/   sH  An 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ÿÿÿÿNt   NNTPt   NNTPReplyErrort   NNTPTemporaryErrort   NNTPPermanentErrort   NNTPProtocolErrort   NNTPDataErrort   error_replyt
   error_tempt
   error_permt   error_protot
   error_datai   t	   NNTPErrorc           B   s   e  Z d  Z d „  Z RS(   s%   Base class for all nntplib exceptionsc         G   sB   t  j |  | Œ y | d |  _ Wn t k
 r= d |  _ n Xd  S(   Ni    s   No response given(   t	   Exceptiont   __init__t   responset
   IndexError(   t   selft   args(    (    s   /usr/lib/python2.7/nntplib.pyR   2   s
    (   t   __name__t
   __module__t   __doc__R   (    (    (    s   /usr/lib/python2.7/nntplib.pyR   0   s   c           B   s   e  Z d  Z RS(   s   Unexpected [123]xx reply(   R   R   R   (    (    (    s   /usr/lib/python2.7/nntplib.pyR   9   s   c           B   s   e  Z d  Z RS(   s
   4xx errors(   R   R   R   (    (    (    s   /usr/lib/python2.7/nntplib.pyR   =   s   c           B   s   e  Z d  Z RS(   s
   5xx errors(   R   R   R   (    (    (    s   /usr/lib/python2.7/nntplib.pyR   A   s   c           B   s   e  Z d  Z RS(   s"   Response does not begin with [1-5](   R   R   R   (    (    (    s   /usr/lib/python2.7/nntplib.pyR   E   s   c           B   s   e  Z d  Z RS(   s   Error in response data(   R   R   R   (    (    (    s   /usr/lib/python2.7/nntplib.pyR   I   s   iw   t   100t   215t   220t   221t   222t   224t   230t   231t   282s   
c           B   sy  e  Z e d# d# d# e d  „ Z d „  Z d „  Z e Z d „  Z	 d „  Z
 d „  Z d „  Z d# d „ Z d „  Z d# d	 „ Z d# d
 „ Z d# d „ Z d# d „ Z d „  Z d „  Z d „  Z d# d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d# d „ Z d „  Z d# d „ Z d „  Z d „  Z  d# d „ Z! d# d „ Z" d# d „ Z# d „  Z$ d „  Z% d  „  Z& d! „  Z' d" „  Z( RS($   c         C   sà  | |  _  | |  _ t j | | f ƒ |  _ |  j j d ƒ |  _ d |  _ |  j ƒ  |  _	 d } | rÇ y |  j
 d ƒ |  _	 WqÇ t k
 rŒ qÇ t k
 rÃ } | r½ | j d  d k r½ d } qÄ ‚  qÇ Xn  yX | r| rd d l }	 |	 j ƒ  }
 |
 j | ƒ } | r| d } | d	 } qn  Wn t k
 r2n X| rÜ|  j
 d
 | ƒ } | d  d k r¦| sqt | ƒ ‚ q¦|  j
 d | ƒ } | d  d k r¦t | ƒ ‚ q¦n  | rÜy |  j
 d ƒ |  _	 WqÙt k
 rÕqÙXqÜn  d S(   sm  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.

        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.
        t   rbi    s   mode readeri   t   480i   iÿÿÿÿNi   s   authinfo user t   381s   authinfo pass t   281(   t   hostt   portt   sockett   create_connectiont   sockt   makefilet   filet	   debuggingt   getrespt   welcomet   shortcmdR   R   R   t   netrct   authenticatorst   IOErrorR   (   R   R"   R#   t   usert   passwordt
   readermodet   usenetrct   readermode_afterautht   eR-   t   credentialst   autht   resp(    (    s   /usr/lib/python2.7/nntplib.pyR   e   sP    				

c         C   s%   |  j  r d Gt |  j ƒ GHn  |  j S(   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)   t   reprR+   (   R   (    (    s   /usr/lib/python2.7/nntplib.pyt
   getwelcome²   s    	 c         C   s   | |  _  d S(   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)   (   R   t   level(    (    s   /usr/lib/python2.7/nntplib.pyt   set_debuglevel»   s    c         C   s?   | t  } |  j d k r+ d Gt | ƒ GHn  |  j j | ƒ d S(   s6   Internal: send one line to the server, appending CRLF.i   s   *put*N(   t   CRLFR)   R9   R&   t   sendall(   R   t   line(    (    s   /usr/lib/python2.7/nntplib.pyt   putlineÄ   s    
 c         C   s,   |  j  r d Gt | ƒ GHn  |  j | ƒ d S(   s=   Internal: send one command to the server (through putline()).s   *cmd*N(   R)   R9   R@   (   R   R?   (    (    s   /usr/lib/python2.7/nntplib.pyt   putcmdÊ   s    	 c         C   s¥   |  j  j t d ƒ } t | ƒ t k r7 t d ƒ ‚ n  |  j d k rX d Gt | ƒ GHn  | sg t ‚ n  | d t k r„ | d  } n | d t k r¡ | d  } n  | S(   sn   Internal: return one line from the server, stripping CRLF.
        Raise EOFError if the connection is closed.i   s   line too longs   *get*iþÿÿÿiÿÿÿÿ(	   R(   t   readlinet   _MAXLINEt   lenR   R)   R9   t   EOFErrorR=   (   R   R?   (    (    s   /usr/lib/python2.7/nntplib.pyt   getlineÏ   s     	  c         C   s†   |  j  ƒ  } |  j r' d Gt | ƒ GHn  | d  } | d k rL t | ƒ ‚ n  | d k rg t | ƒ ‚ n  | d k r‚ t | ƒ ‚ n  | S(   sj   Internal: get a response from the server.
        Raise various errors if the response indicates an error.s   *resp*i   t   4t   5t   123(   RF   R)   R9   R   R   R   (   R   R8   t   c(    (    s   /usr/lib/python2.7/nntplib.pyR*   Ü   s    	 
c         C   sè   d } zÁ t | t ƒ r. t | d ƒ } } n  |  j ƒ  } | d  t k rY t | ƒ ‚ n  g  } xd |  j ƒ  } | d k r~ Pn  | d  d k r› | d } n  | rµ | j | d ƒ qb | j	 | ƒ qb WWd | rÝ | j
 ƒ  n  X| | f S(	   s~   Internal: get a response plus following text from the server.
        Raise various errors if the response indicates an error.t   wi   t   .i   s   ..i   s   
N(   t   Nonet
   isinstancet   strt   openR*   t   LONGRESPR   RF   t   writet   appendt   close(   R   R(   t
   openedFileR8   t   listR?   (    (    s   /usr/lib/python2.7/nntplib.pyt   getlongrespê   s(    c         C   s   |  j  | ƒ |  j ƒ  S(   s.   Internal: send a command and get the response.(   RA   R*   (   R   R?   (    (    s   /usr/lib/python2.7/nntplib.pyR,   	  s    c         C   s   |  j  | ƒ |  j | ƒ S(   sB   Internal: send a command and get the response plus following text.(   RA   RW   (   R   R?   R(   (    (    s   /usr/lib/python2.7/nntplib.pyt   longcmd  s    c         C   s   |  j  d | 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   (   R   t   datet   timeR(   (    (    s   /usr/lib/python2.7/nntplib.pyt	   newgroups  s    c         C   s*   d | d | d | } |  j  | | ƒ S(   s  Process 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 idss   NEWNEWS RY   (   RX   (   R   t   groupRZ   R[   R(   t   cmd(    (    s   /usr/lib/python2.7/nntplib.pyt   newnews  s    	c         C   sY   |  j  d | ƒ \ } } x4 t t | ƒ ƒ D]  } t | | j ƒ  ƒ | | <q+ W| | f S(   s‹   Process a LIST command.  Return:
        - resp: server response if successful
        - list: list of (group, last, first, flag) (strings)t   LIST(   RX   t   rangeRD   t   tuplet   split(   R   R(   R8   RV   t   i(    (    s   /usr/lib/python2.7/nntplib.pyRV   )  s    c         C   s;   |  j  | ƒ \ } } t | ƒ d k r+ d S| d d Sd S(   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.i    t    i   N(   t   descriptionsRD   (   R   R]   R8   t   lines(    (    s   /usr/lib/python2.7/nntplib.pyt   description4  s    c         C   s¬   t  j d ƒ } |  j d | ƒ \ } } | d  d k rT |  j d | ƒ \ } } n  g  } xE | D]= } | j | j ƒ  ƒ } | ra | j | j d d ƒ ƒ qa qa W| | f S(   s'   Get descriptions for a range of groups.s   ^(?P<group>[^ 	]+)[ 	]+(.*)$s   LIST NEWSGROUPS i   R   s   XGTITLE i   i   (   t   ret   compileRX   t   searcht   stripRS   R]   (   R   t   group_patternt   line_patR8   t	   raw_linesRg   t   raw_linet   match(    (    s   /usr/lib/python2.7/nntplib.pyRf   G  s     c         C   sÕ   |  j  d | ƒ } | d  d k r2 t | ƒ ‚ n  | j ƒ  } d } } } t | ƒ } | d k rÂ | d } | d k rÂ | d } | d k r¿ | d } | d k r¼ | d j ƒ  } q¼ q¿ qÂ n  | | | | | f S(   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 names   GROUP i   t   211i    i   i   i   (   R,   R   Rc   RD   t   lower(   R   t   nameR8   t   wordst   countt   firstt   lastt   n(    (    s   /usr/lib/python2.7/nntplib.pyR]   X  s    



c         C   s   |  j  d | ƒ S(   so   Process a HELP command.  Returns:
        - resp: server response if successful
        - list: list of stringst   HELP(   RX   (   R   R(   (    (    s   /usr/lib/python2.7/nntplib.pyt   helpr  s    c         C   s‚   | d  d k r t  | ƒ ‚ n  | j ƒ  } d } d } t | ƒ } | d k ru | d } | d k ru | d } qu n  | | | f S(   s=   Internal: parse the response of a STAT, NEXT or LAST command.i   t   22i    Re   i   (   R   Rc   RD   (   R   R8   Ru   t   nrt   idRy   (    (    s   /usr/lib/python2.7/nntplib.pyt	   statparsey  s    
c         C   s   |  j  | ƒ } |  j | ƒ S(   s/   Internal: process a STAT, NEXT or LAST command.(   R,   R   (   R   R?   R8   (    (    s   /usr/lib/python2.7/nntplib.pyt   statcmd‡  s    c         C   s   |  j  d | ƒ 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 ids   STAT (   R€   (   R   R~   (    (    s   /usr/lib/python2.7/nntplib.pyt   statŒ  s    c         C   s   |  j  d ƒ S(   s;   Process a NEXT command.  No arguments.  Return as for STAT.t   NEXT(   R€   (   R   (    (    s   /usr/lib/python2.7/nntplib.pyt   next–  s    c         C   s   |  j  d ƒ S(   s;   Process a LAST command.  No arguments.  Return as for STAT.t   LAST(   R€   (   R   (    (    s   /usr/lib/python2.7/nntplib.pyRx   š  s    c         C   s@   |  j  | | ƒ \ } } |  j | ƒ \ } } } | | | | f S(   s2   Internal: process a HEAD, BODY or ARTICLE command.(   RX   R   (   R   R?   R(   R8   RV   R}   R~   (    (    s   /usr/lib/python2.7/nntplib.pyt   artcmdž  s    c         C   s   |  j  d | ƒ 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 headers   HEAD (   R…   (   R   R~   (    (    s   /usr/lib/python2.7/nntplib.pyt   head¤  s    	c         C   s   |  j  d | | ƒ S(   sk  Process 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 useds   BODY (   R…   (   R   R~   R(   (    (    s   /usr/lib/python2.7/nntplib.pyt   body¯  s    c         C   s   |  j  d | ƒ 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 articles   ARTICLE (   R…   (   R   R~   (    (    s   /usr/lib/python2.7/nntplib.pyt   article¼  s    	c         C   s   |  j  d ƒ S(   sP   Process a SLAVE command.  Returns:
        - resp: server response if successfult   SLAVE(   R,   (   R   (    (    s   /usr/lib/python2.7/nntplib.pyt   slaveÇ  s    c   
      C   s’   t  j d ƒ } |  j d | d | | ƒ \ } } xR t t | ƒ ƒ D]> } | | } | j | ƒ }	 |	 rF |	 j d d ƒ | | <qF qF W| | f S(   s  Process 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]+) ?(.*)
?s   XHDR RY   i   i   (   Ri   Rj   RX   Ra   RD   Rq   R]   (
   R   t   hdrRO   R(   t   patR8   Rg   Rd   R?   t   m(    (    s   /usr/lib/python2.7/nntplib.pyt   xhdrÍ  s    $
c   	      C   sÃ   |  j  d | d | | ƒ \ } } g  } xŒ | D]„ } | j d ƒ } yO | j | d | d | d | d | d | d	 j ƒ  | d
 | d f ƒ Wq1 t k
 r´ t | ƒ ‚ q1 Xq1 W| | f S(   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)s   XOVER t   -s   	i    i   i   i   i   i   i   i   (   RX   Rc   RS   R   R   (	   R   t   startt   endR(   R8   Rg   t   xover_linesR?   t   elem(    (    s   /usr/lib/python2.7/nntplib.pyt   xoverÞ  s     	$c   	      C   sƒ   t  j d ƒ } |  j d | | ƒ \ } } g  } xE | D]= } | j | j ƒ  ƒ } | r8 | j | j d d ƒ ƒ q8 q8 W| | f S(   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   ^([^ 	]+)[ 	]+(.*)$s   XGTITLE i   i   (   Ri   Rj   RX   Rk   Rl   RS   R]   (	   R   R]   R(   Rn   R8   Ro   Rg   Rp   Rq   (    (    s   /usr/lib/python2.7/nntplib.pyt   xgtitleø  s     c         C   sv   |  j  d | ƒ } | d  d k r2 t | ƒ ‚ n  y | j ƒ  \ } } Wn t k
 rg t | ƒ ‚ n X| | f Sd S(   sÈ   Process an XPATH command (optional server extension) Arguments:
        - id: Message id of article
        Returns:
        resp: server response if successful
        path: directory path to articles   XPATH i   t   223N(   R,   R   Rc   t
   ValueError(   R   R~   R8   t   resp_numt   path(    (    s   /usr/lib/python2.7/nntplib.pyt   xpath  s    c         C   sº   |  j  d ƒ } | d  d k r. t | ƒ ‚ n  | j ƒ  } t | ƒ d k r[ t | ƒ ‚ n  | d d d !} | d d } t | ƒ d k sž t | ƒ d k r­ t | ƒ ‚ n  | | | f S(	   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.t   DATEi   t   111i   i   i   iúÿÿÿi   (   R,   R   Rc   RD   R   (   R   R8   R“   RZ   R[   (    (    s   /usr/lib/python2.7/nntplib.pyRZ     s    $c         C   s©   |  j  d ƒ } | d d k r. t | ƒ ‚ n  xa | j ƒ  } | sG Pn  | d d k rd | d  } n  | d  d k r d | } n  |  j | ƒ q1 W|  j d ƒ |  j ƒ  S(   s‹   Process a POST command.  Arguments:
        - f: file containing the article
        Returns:
        - resp: server response if successfult   POSTi    t   3iÿÿÿÿs   
i   RL   (   R,   R   RB   R@   R*   (   R   t   fR8   R?   (    (    s   /usr/lib/python2.7/nntplib.pyt   post.  s    c         C   s­   |  j  d | ƒ } | d d k r2 t | ƒ ‚ n  xa | j ƒ  } | sK Pn  | d d k rh | d  } n  | d  d k r… d | } n  |  j | ƒ q5 W|  j d ƒ |  j ƒ  S(   s  Process 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.s   IHAVE i    Rž   iÿÿÿÿs   
i   RL   (   R,   R   RB   R@   R*   (   R   R~   RŸ   R8   R?   (    (    s   /usr/lib/python2.7/nntplib.pyt   ihaveD  s    c         C   s9   |  j  d ƒ } |  j j ƒ  |  j j ƒ  |  ` |  ` | S(   sd   Process a QUIT command and close the socket.  Returns:
        - resp: server response if successfult   QUIT(   R,   R(   RT   R&   (   R   R8   (    (    s   /usr/lib/python2.7/nntplib.pyt   quit\  s
    N()   R   R   t	   NNTP_PORTRM   t   TrueR   R:   R<   t   debugR@   RA   RF   R*   RW   R,   RX   R\   R_   RV   Rh   Rf   R]   R{   R   R€   R   Rƒ   Rx   R…   R†   R‡   Rˆ   RŠ   RŽ   R”   R•   Rš   RZ   R    R¡   R£   (    (    (    s   /usr/lib/python2.7/nntplib.pyR    d   sJ   	L								
						
									t   __main__t   newst
   NNTPSERVERRL   R2   s   comp.lang.pythont   Groupt   hass   articles, ranget   tot   subjectR   s   %7s %s('   R   Ri   R$   t   __all__RC   R   R   R   R   R   R   R   R   R   R   R	   R
   R¤   RQ   R=   R    R   t   ost   environt   newshostt   findt   modeRM   t   sR]   R8   Rv   Rw   Rx   Rt   RŽ   t   subst   itemR£   (    (    (    s   /usr/lib/python2.7/nntplib.pyt   <module>   sP   				!ÿ ÿ 
	! 