ó
čžbc           @   sŤ  d  Z  d d l Z d d l Z d d g Z d e f d     YZ d Z d Z d Z d	 Z	 e e	 Z
 d
 Z d f  d     YZ y d d l Z Wn e k
 r n$ Xd e f d     YZ e j d  e d k r§d d l Z e e j d  Z e j   GHe j e j d  e j e j d  e j   e j   \ Z Z x[ e d e d  D]F Z e j e  \ Z Z  Z! d e GHx e  D] Z" d e" GHq~Wd GHqPWe j#   n  d S(   s@   A POP3 client class.

Based on the J. Myers POP3 draft, Jan. 96
i˙˙˙˙Nt   POP3t   error_protoc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s   /usr/lib/python2.7/poplib.pyR      s    in   iă  s   s   
i   c           B   sű   e  Z d  Z e e j 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 d d  Z d   Z d   Z d   Z d   Z d   Z d   Z e j d  Z d   Z d   Z d 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^   | |  _  | |  _ t j | | f |  |  _ |  j j d  |  _ d |  _ |  j   |  _	 d  S(   Nt   rbi    (
   t   hostt   portt   sockett   create_connectiont   sockt   makefilet   filet
   _debuggingt   _getrespt   welcome(   t   selfR   R   t   timeout(    (    s   /usr/lib/python2.7/poplib.pyt   __init__U   s    			c         C   s?   |  j  d k r! d Gt |  GHn  |  j j d | t f  d  S(   Ni   s   *put*s   %s%s(   R   t   reprR	   t   sendallt   CRLF(   R   t   line(    (    s   /usr/lib/python2.7/poplib.pyt   _putline_   s     c         C   s,   |  j  r d Gt |  GHn  |  j |  d  S(   Ns   *cmd*(   R   R   R   (   R   R   (    (    s   /usr/lib/python2.7/poplib.pyt   _putcmdf   s    	 c         C   sĆ   |  j  j t d  } t |  t k r7 t d   n  |  j d k rX d Gt |  GHn  | sm t d   n  t |  } | d t k r | d  | f S| d t k r¸ | d d !| f S| d  | f S(   Ni   s   line too longs   *get*s   -ERR EOFiţ˙˙˙i    i˙˙˙˙(	   R   t   readlinet   _MAXLINEt   lenR   R   R   R   t   CR(   R   R   t   octets(    (    s   /usr/lib/python2.7/poplib.pyt   _getlineo   s      c         C   s\   |  j    \ } } |  j d k r3 d Gt |  GHn  | d  } | d k rX t |   n  | S(   Ni   s   *resp*t   +(   R   R   R   R   (   R   t   respt   ot   c(    (    s   /usr/lib/python2.7/poplib.pyR      s     
c         C   s   |  j    } g  } d } |  j   \ } } x` | d k r | d  d k r` | d } | d } n  | | } | j |  |  j   \ } } q- W| | | f S(   Ni    t   .i   s   ..i   (   R   R   t   append(   R   R   t   listR   R   R    (    (    s   /usr/lib/python2.7/poplib.pyt   _getlongresp   s     

c         C   s   |  j  |  |  j   S(   N(   R   R   (   R   R   (    (    s   /usr/lib/python2.7/poplib.pyt	   _shortcmd   s    c         C   s   |  j  |  |  j   S(   N(   R   R%   (   R   R   (    (    s   /usr/lib/python2.7/poplib.pyt   _longcmdĽ   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s   /usr/lib/python2.7/poplib.pyt
   getwelcomeŹ   s    c         C   s   | |  _  d  S(   N(   R   (   R   t   level(    (    s   /usr/lib/python2.7/poplib.pyt   set_debuglevel°   s    c         C   s   |  j  d |  S(   sV   Send user name, return response

        (should indicate password required).
        s   USER %s(   R&   (   R   t   user(    (    s   /usr/lib/python2.7/poplib.pyR+   ś   s    c         C   s   |  j  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 %s(   R&   (   R   t   pswd(    (    s   /usr/lib/python2.7/poplib.pyt   pass_ž   s    c         C   s`   |  j  d  } | j   } |  j r6 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   (   R&   t   splitR   R   t   int(   R   t   retvalt   retst   numMessagest   sizeMessages(    (    s   /usr/lib/python2.7/poplib.pyt   statČ   s    	 c         C   s*   | d k	 r |  j d |  S|  j 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   NoneR&   R'   (   R   t   which(    (    s   /usr/lib/python2.7/poplib.pyR$   Ő   s    	c         C   s   |  j  d |  S(   so   Retrieve whole message number 'which'.

        Result is in form ['response', ['line', ...], octets].
        s   RETR %s(   R'   (   R   R8   (    (    s   /usr/lib/python2.7/poplib.pyt   retră   s    c         C   s   |  j  d |  S(   sF   Delete message number 'which'.

        Result is 'response'.
        s   DELE %s(   R&   (   R   R8   (    (    s   /usr/lib/python2.7/poplib.pyt   deleë   s    c         C   s   |  j  d  S(   sX   Does nothing.

        One supposes the response indicates the server is alive.
        t   NOOP(   R&   (   R   (    (    s   /usr/lib/python2.7/poplib.pyt   noopó   s    c         C   s   |  j  d  S(   s(   Unmark all messages marked for deletion.t   RSET(   R&   (   R   (    (    s   /usr/lib/python2.7/poplib.pyt   rsetű   s    c         C   sY   y |  j  d  } Wn t k
 r. } | } n X|  j j   |  j j   |  ` |  ` | S(   sD   Signoff: commit changes on server, unlock mailbox, close connection.t   QUIT(   R&   R   R   t   closeR	   (   R   R   t   val(    (    s   /usr/lib/python2.7/poplib.pyt   quit   s    
c         C   s   |  j  d |  S(   s   Not sure what this does.s   RPOP %s(   R&   (   R   R+   (    (    s   /usr/lib/python2.7/poplib.pyt   rpop  s    s   \+OK.[^<]*(<.*>)c         C   s   |  j  j |  j  } | s* t d   n  d d l } | j | j d  |  j   } d j t	 d   |   } |  j
 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 serveri˙˙˙˙Ni   t    c         S   s   d t  |   S(   Ns   %02x(   t   ord(   t   x(    (    s   /usr/lib/python2.7/poplib.pyt   <lambda>'  RD   s
   APOP %s %s(   t	   timestampt   matchR   R   t   hashlibt   md5t   groupt   digestt   joint   mapR&   (   R   R+   t   secrett   mRJ   RM   (    (    s   /usr/lib/python2.7/poplib.pyt   apop  s    "c         C   s   |  j  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 %s(   R'   (   R   R8   t   howmuch(    (    s   /usr/lib/python2.7/poplib.pyt   top+  s    c         C   s*   | d k	 r |  j d |  S|  j 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(   R7   R&   R'   (   R   R8   (    (    s   /usr/lib/python2.7/poplib.pyt   uidl4  s    N(!   R   R   t   __doc__t	   POP3_PORTR   t   _GLOBAL_DEFAULT_TIMEOUTR   R   R   R   R   R%   R&   R'   R(   R*   R+   R-   R5   R7   R$   R9   R:   R<   R>   RB   RC   t   ret   compileRH   RR   RT   RV   (    (    (    s   /usr/lib/python2.7/poplib.pyR    *   s4   (													
										t   POP3_SSLc           B   sD   e  Z d  Z e d d 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 contains your private key
               certfile - PEM formatted certificate chain file

            See the methods of the parent class POP3 for more documentation.
        c         C   sR  | |  _  | |  _ | |  _ | |  _ d |  _ d } d  |  _ xŠ t j |  j  |  j d t j	  D] } | \ } } }	 }
 } y, t j | | |	  |  _ |  j j
 |  Wn; t j k
 râ } |  j rÓ |  j j   n  d  |  _ q^ n XPq^ W|  j s t j |  n  |  j j d  |  _ t j |  j |  j |  j  |  _ d |  _ |  j   |  _ d  S(   NRD   s!   getaddrinfo returns an empty listi    R   (   R   R   t   keyfilet   certfilet   bufferR7   R	   R   t   getaddrinfot   SOCK_STREAMt   connectt   errorR@   R
   R   t   sslt   wrap_sockett   sslobjR   R   R   (   R   R   R   R]   R^   t   msgt   rest   aft   socktypet   protot	   canonnamet   sa(    (    s   /usr/lib/python2.7/poplib.pyR   R  s0    						(			!	c         C   sC   |  j  j   } t |  d k r0 t d   n  |  j | 7_ d  S(   Ni    s   -ERR EOF(   Rf   t   readR   R   R_   (   R   t   localbuf(    (    s   /usr/lib/python2.7/poplib.pyt   _fillBufferl  s    c         C   s  d } t  j d  } | j |  j  } xJ | ss |  j   t |  j  t k r^ t d   n  | j |  j  } q* W| j d  } | j	 d |  j d  |  _ |  j
 d k rż d Gt |  GHn  t |  } | d t k ré | d  | f S| d t k r
| d d !| f S| d  | f S(	   NRD   s   .*?\ns   line too longi    i   s   *get*iţ˙˙˙i˙˙˙˙(   RZ   R[   RI   R_   Rp   R   R   R   RL   t   subR   R   R   R   (   R   R   t	   renewlineRI   R   (    (    s   /usr/lib/python2.7/poplib.pyR   r  s$    	
 c         C   s   |  j  d k r! d Gt |  GHn  | t 7} t |  } xF | d k r |  j j |  } | | k rh Pn  | | } | | } q: Wd  S(   Ni   s   *put*i    (   R   R   R   R   Rf   t   write(   R   R   t   bytest   sent(    (    s   /usr/lib/python2.7/poplib.pyR     s     

c         C   sL   y |  j  d  } Wn t k
 r. } | } n X|  j j   |  ` |  ` | S(   sD   Signoff: commit changes on server, unlock mailbox, close connection.R?   (   R&   R   R	   R@   Rf   (   R   R   RA   (    (    s   /usr/lib/python2.7/poplib.pyRB     s    
N(
   R   R   RW   t   POP3_SSL_PORTR7   R   Rp   R   R   RB   (    (    (    s   /usr/lib/python2.7/poplib.pyR\   E  s   			t   __main__i   i   i   s   Message %d:s      s   -----------------------($   RW   RZ   R   t   __all__t	   ExceptionR   RX   Rv   R   t   LFR   R   R    Rd   t   ImportErrorR\   R#   R   t   syst   argvt   aR(   R+   R-   R$   R5   t   numMsgst	   totalSizet   ranget   iR9   t   headerRg   R   R   RB   (    (    (    s   /usr/lib/python2.7/poplib.pyt   <module>   s@   
˙ V
		