mò
­fIc           @   sp  d  Z  d k Z d k Z d d d g Z d e f d „  ƒ  YZ d Z d Z d Z d	 Z	 e e	 Z
 d f  d
 „  ƒ  YZ d e f d „  ƒ  YZ e d j o× d k Z e e i d ƒ Z e i ƒ  GHe i e i d ƒ e i e i d ƒ e i ƒ  e i ƒ  \ Z Z x[ e d e d ƒ D]F Z e i e ƒ \ Z Z Z d e GHx e D] Z d e GHqBWd GHqWe i ƒ  n d S(   s@   A POP3 client class.

Based on the J. Myers POP3 draft, Jan. 96
Nt   POP3t   error_protot   POP3_SSLc           B   s   t  Z RS(   N(   t   __name__t
   __module__(    (    (    t#   /mit/python/lib/python2.4/poplib.pyR      s    in   iã  s   s   
c           B   sõ   t  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e i d ƒ Z d „  Z d „  Z e d „ Z RS(   sÏ  This class supports both the minimal and optional command sets.
    Arguments can be strings or integers (where appropriate)
    (e.g.: retr(1) and retr('1') both work equally well.

    Minimal Command Set:
            USER name               user(name)
            PASS string             pass_(string)
            STAT                    stat()
            LIST [msg]              list(msg = None)
            RETR msg                retr(msg)
            DELE msg                dele(msg)
            NOOP                    noop()
            RSET                    rset()
            QUIT                    quit()

    Optional Commands (some servers support these):
            RPOP name               rpop(name)
            APOP name digest        apop(name, digest)
            TOP msg n               top(msg, n)
            UIDL [msg]              uidl(msg = None)

    Raises one exception: 'error_proto'.

    Instantiate with:
            POP3(hostname, port=110)

    NB:     the POP protocol locks the mailbox from user
            authorization until QUIT, so be sure to get in, suck
            the messages, and quit, each time you access the
            mailbox.

            POP is a line-based protocol, which means large mail
            messages consume lots of python cycles reading them
            line-by-line.

            If it's available on your mail server, use IMAP4
            instead, it doesn't suffer from the two problems
            above.
    c   
      C   s  | |  _  | |  _ d } d  |  _ x­ t i |  i  |  i d t i ƒ D]Š } | \ } } } } }	 y, t i | | | ƒ |  _ |  i i |	 ƒ Wn? t i j
 o0 } |  i o |  i i ƒ  n d  |  _ qC n XPqC W|  i p t i | ‚ n |  i i d ƒ |  _ d |  _ |  i ƒ  |  _ d  S(   Ns!   getaddrinfo returns an empty listi    t   rb(   t   hostt   selft   portt   msgt   Nonet   sockt   sockett   getaddrinfot   SOCK_STREAMt   rest   aft   socktypet   protot	   canonnamet   sat   connectt   errort   closet   makefilet   filet
   _debuggingt   _getrespt   welcome(
   R   R   R	   R   R   R   R   R   R
   R   (    (    R   t   __init__O   s*    			" 
	
	c         C   sA   |  i d j o d Gt | ƒ GHn |  i i d | t f ƒ d  S(   Ni   s   *put*s   %s%s(   R   R   t   reprt   lineR   t   sendallt   CRLF(   R   R    (    (    R   t   _putlinef   s     c         C   s.   |  i o d Gt | ƒ GHn |  i | ƒ d  S(   Ns   *cmd*(   R   R   R   R    R#   (   R   R    (    (    R   t   _putcmdm   s    
 c         C   s¬   |  i i ƒ  } |  i d j o d Gt | ƒ GHn | p t d ƒ ‚ n t | ƒ } | d t	 j o | d  | f Sn | d t
 j o | d d !| f Sn | d  | f S(   Ni   s   *get*s   -ERR EOFiþÿÿÿi    iÿÿÿÿ(   R   R   t   readlineR    R   R   R   t   lent   octetsR"   t   CR(   R   R'   R    (    (    R   t   _getlinev   s      c         C   s`   |  i ƒ  \ } } |  i d j o d Gt | ƒ GHn | d  } | d j o t | ƒ ‚ n | S(   Ni   s   *resp*t   +(   R   R)   t   respt   oR   R   t   cR   (   R   R-   R,   R+   (    (    R   R   ˆ   s     
c         C   sž   |  i ƒ  } g  } d } |  i ƒ  \ } } xd | d j oV | d  d j o | d } | d } n | | } | i | ƒ |  i ƒ  \ } } q- W| | | f S(   Ni    t   .i   s   ..i   (	   R   R   R+   t   listR'   R)   R    R,   t   append(   R   R/   R,   R'   R    R+   (    (    R   t   _getlongresp“   s     

c         C   s   |  i | ƒ |  i ƒ  S(   N(   R   R$   R    R   (   R   R    (    (    R   t	   _shortcmd£   s    c         C   s   |  i | ƒ |  i ƒ  S(   N(   R   R$   R    R1   (   R   R    (    (    R   t   _longcmdª   s    c         C   s   |  i S(   N(   R   R   (   R   (    (    R   t
   getwelcome±   s    c         C   s   | |  _ d  S(   N(   t   levelR   R   (   R   R5   (    (    R   t   set_debuglevelµ   s    c         C   s   |  i d | ƒ S(   sV   Send user name, return response

        (should indicate password required).
        s   USER %sN(   R   R2   t   user(   R   R7   (    (    R   R7   »   s     c         C   s   |  i d | ƒ S(   s    Send password, return response

        (response includes message count, mailbox size).

        NB: mailbox is locked by server from here to 'quit()'
        s   PASS %sN(   R   R2   t   pswd(   R   R8   (    (    R   t   pass_Ã   s     c         C   sb   |  i d ƒ } | i ƒ  } |  i o d Gt | ƒ GHn t | d ƒ } t | d ƒ } | | f S(   s]   Get mailbox status.

        Result is tuple of 2 ints (message count, mailbox size)
        t   STATs   *stat*i   i   N(
   R   R2   t   retvalt   splitt   retsR   R   t   intt   numMessagest   sizeMessages(   R   R@   R=   R;   R?   (    (    R   t   statÍ   s     
 c         C   s/   | d j	 o |  i d | ƒ Sn |  i d ƒ S(   s  Request listing, return result.

        Result without a message number argument is in form
        ['response', ['mesg_num octets', ...], octets].

        Result when a message number argument is given is a
        single response: the "scan listing" for that message.
        s   LIST %st   LISTN(   t   whichR   R   R2   R3   (   R   RC   (    (    R   R/   Ú   s     c         C   s   |  i d | ƒ S(   so   Retrieve whole message number 'which'.

        Result is in form ['response', ['line', ...], octets].
        s   RETR %sN(   R   R3   RC   (   R   RC   (    (    R   t   retrè   s     c         C   s   |  i d | ƒ S(   sF   Delete message number 'which'.

        Result is 'response'.
        s   DELE %sN(   R   R2   RC   (   R   RC   (    (    R   t   deleð   s     c         C   s   |  i d ƒ S(   sX   Does nothing.

        One supposes the response indicates the server is alive.
        t   NOOPN(   R   R2   (   R   (    (    R   t   noopø   s     c         C   s   |  i d ƒ S(   s   Not sure what this does.t   RSETN(   R   R2   (   R   (    (    R   t   rset   s     c         C   s[   y |  i d ƒ } Wn t j
 o } | } n X|  i i ƒ  |  i i ƒ  |  ` |  ` | S(   sD   Signoff: commit changes on server, unlock mailbox, close connection.t   QUITN(   R   R2   R+   R   t   valR   R   R   (   R   R+   RK   (    (    R   t   quit  s     c         C   s   |  i d | ƒ S(   s   Not sure what this does.s   RPOP %sN(   R   R2   R7   (   R   R7   (    (    R   t   rpop  s     s   \+OK.*(<[^>]+>)c         C   s‰   |  i i |  i ƒ } | p t d ƒ ‚ n d k } | i | i d ƒ | ƒ i
 ƒ  } d i t d „  | ƒ ƒ } |  i d | | f ƒ S(   s   Authorisation

        - only possible if server has supplied a timestamp in initial greeting.

        Args:
                user    - mailbox user;
                secret  - secret shared between client and server.

        NB: mailbox is locked by server from here to 'quit()'
        s!   -ERR APOP not supported by serverNi   t    c         C   s   d t  |  ƒ S(   Ns   %02x(   t   ordt   x(   RP   (    (    R   t   <lambda>,  s    s
   APOP %s %s(   R   t	   timestampt   matchR   t   mR   t   md5t   newt   groupt   secrett   digestt   joint   mapR2   R7   (   R   R7   RX   RT   RY   RU   (    (    R   t   apop  s    
 	"c         C   s   |  i d | | f ƒ S(   s­   Retrieve message header of message number 'which'
        and first 'howmuch' lines of message body.

        Result is in form ['response', ['line', ...], octets].
        s	   TOP %s %sN(   R   R3   RC   t   howmuch(   R   RC   R]   (    (    R   t   top0  s     c         C   s/   | d j	 o |  i d | ƒ Sn |  i d ƒ S(   sì   Return message digest (unique id) list.

        If 'which', result contains unique id for that message
        in the form 'response mesgnum uid', otherwise result is
        the list ['response', ['mesgnum uid', ...], octets]
        s   UIDL %st   UIDLN(   RC   R   R   R2   R3   (   R   RC   (    (    R   t   uidl9  s     (   R   R   t   __doc__t	   POP3_PORTR   R#   R$   R)   R   R1   R2   R3   R4   R6   R7   R9   RA   R   R/   RD   RE   RG   RI   RL   RM   t   ret   compileRR   R\   R^   R`   (    (    (    R   R    $   s2   ( 												
										c           B   sD   t  Z d  Z e e e d „ Z d „  Z d „  Z d „  Z d „  Z	 RS(   s¦  POP3 client class over SSL connection

    Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None)

           hostname - the hostname of the pop3 over ssl server
           port - port number
           keyfile - PEM formatted file that countains your private key
           certfile - PEM formatted certificate chain file

        See the methods of the parent class POP3 for more documentation.
    c         C   sX  | |  _  | |  _ | |  _ | |  _ d |  _ d }
 d  |  _ x­ t	 i
 |  i  |  i d t	 i ƒ D]Š } | \ } } } }	 } y, t	 i	 | | | ƒ |  _ |  i i | ƒ Wn? t	 i j
 o0 }
 |  i o |  i i ƒ  n d  |  _ q^ n XPq^ W|  i p t	 i |
 ‚ n |  i i d ƒ |  _ t	 i |  i |  i |  i ƒ |  _ d |  _ |  i ƒ  |  _ d  S(   NRN   s!   getaddrinfo returns an empty listi    R   (   R   R   R	   t   keyfilet   certfilet   bufferR
   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   sslt   sslobjR   R   R   (   R   R   R	   Re   Rf   R   R   R   R   R   R
   R   (    (    R   R   Q  s2    						" 
	
!	c         C   sE   |  i i ƒ  } t | ƒ d j o t d ƒ ‚ n |  i | 7_ d  S(   Ni    s   -ERR EOF(   R   Ri   t   readt   localbufR&   R   Rg   (   R   Rk   (    (    R   t   _fillBufferk  s    c         C   s  d } t i d ƒ } | i |  i ƒ } x( | p  |  i ƒ  | i |  i ƒ } q* W| i d ƒ } | i	 d |  i d ƒ |  _ |  i
 d j o d Gt | ƒ GHn t | ƒ } | d t j o | d  | f Sn | d t j o | d d !| f Sn | d  | f S(   NRN   s   .*?\ni    i   s   *get*iþÿÿÿiÿÿÿÿ(   R    Rc   Rd   t	   renewlineRS   R   Rg   Rl   RW   t   subR   R   R&   R'   R"   R(   (   R   Rm   R'   R    RS   (    (    R   R)   q  s"     
 c         C   sŠ   |  i d j o d Gt | ƒ GHn | t 7} t | ƒ } xJ | d j o< |  i i | ƒ } | | j o Pn | | } | | } q< Wd  S(   Ni   s   *put*i    (
   R   R   R   R    R"   R&   t   bytesRi   t   writet   sent(   R   R    Ro   Rq   (    (    R   R#   ƒ  s     
 
c         C   sN   y |  i d ƒ } Wn t j
 o } | } n X|  i i ƒ  |  ` |  ` | S(   sD   Signoff: commit changes on server, unlock mailbox, close connection.RJ   N(   R   R2   R+   R   RK   R   R   Ri   (   R   R+   RK   (    (    R   RL   Ž  s     (
   R   R   Ra   t   POP3_SSL_PORTR   R   Rl   R)   R#   RL   (    (    (    R   R   D  s    			t   __main__i   i   i   s   Message %d:s      s   -----------------------(    Ra   Rc   R   t   __all__t	   ExceptionR   Rb   Rr   R(   t   LFR"   R    R   R   t   syst   argvt   aR4   R7   R9   R/   RA   t   numMsgst	   totalSizet   ranget   iRD   t   headerR
   R'   R    RL   (   Rb   R~   R(   Rv   Rt   R{   Rc   Rz   R
   Rr   R'   Rw   R   R"   R    Ry   R   R   R}   R    (    (    R   t   ?   s8   
ÿ !U	
 	 	