ó
Ú"wTc           @   s¤   d  Z  d d d g Z d d l Z d d l m Z m Z m Z m Z m Z d d l	 Td d l
 Z e j j   Z d Z d e f d	     YZ d e f d
     YZ d S(   s$   Provide an SSL-capable POP3 class.

t   POP3_ssl_portt	   sslsockett   POP3SSLi˙˙˙˙N(   t   POP3t   CRt   LFt   CRLFt   error_proto(   t   *iă  c           B   sA   e  Z d  Z d d d  Z d   Z d   Z d   Z d   Z RS(   s  The Python poplib.POP3() class mixes socket-like .sendall() and
    file-like .readline() for communications.  That would be okay, except that
    the new socket.ssl objects provide only read() and write(), so they
    don't act like a socket /or/ like a file.  Argh.

    This class takes a standard, connected socket.socket() object, sets it
    to blocking mode (required for socket.ssl() to work correctly, though
    apparently not documented), wraps .write() for .sendall() and implements
    .readline().

    The modified POP3 class below can then use this to provide POP3-over-SSL.

    Thanks to Frank Benkstein for the inspiration.
    c         C   sx   t  j   | |  _ |  j j d  | rM | rM t j |  j | |  |  _ n t j |  j  |  _ d |  _ d |  _ d  S(   Ni   t    i   (   t   logt   tracet   sockt   setblockingt   sockett   sslt   buft   bufsize(   t   selfR   t   keyfilet   certfile(    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyt   __init__$   s    
		c         C   s   t  j   |  j t |  j  } t  j d |  | d k rA d S|  j j |  } t |  } t  j d |  |  j | 7_ d S(   s8   Fill an internal buffer for .readline() to use.
        s   want %i bytes
i    Ns   got %i bytes
(   R
   R   R   t   lenR   R   t   read(   R   t   wantt   st   got(    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyt   _fillbuf/   s    
c         C   s   |  j  j   d  |  _ d  S(   N(   R   t   closet   NoneR   (   R   (    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyR   <   s    c         C   s   |  j  j |  d  S(   N(   R   t   write(   R   R   (    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyt   sendallA   s    c         C   s{  t  j   d } y5|  j s) |  j   n  t  j d  |  j r/t  j d |  j t |  j  f  xĘ t r+t  j d  |  j j d  } | d k rÔ t  j d |  | |  j | d  7} |  j | d |  _ Pn  t  j d	  | |  j 7} d |  _ |  j   |  j st  j d
  Pn  t  j d  qe Wn  t  j d |  | SWn/ t j t j	 f k
 rv} t
 d |   n Xd S(   sf   Simple hack to implement .readline() on a non-file object that
        only supports .read().
        R	   s   checking self.buf
s   self.buf = "%r", len %i
s   looking for EOL
s   
i˙˙˙˙s   EOL found at %d
i   s'   EOL not found, trying to fill self.buf
s   nothing read, exiting
s   end of loop
s   returning line "%r"
s/   socket/ssl error while reading from server (%s)N(   R
   R   R   R   R   t   Truet   findR   t   sslerrort   errort   getmailOperationError(   R   t   linet   it   o(    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyt   readlineF   s<    
					
	N(	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R(   (    (    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyR      s   			c           B   s2   e  Z d  Z e d d d  Z d   Z d   Z RS(   s2  Thin subclass to add SSL functionality to the built-in POP3 class.
    Note that Python's socket module does not do certificate verification
    for SSL connections.

    This gets rid of the .file attribute from os.makefile(rawsock) and relies on
    sslsocket() above to provide .readline() instead.
    c         C   sz  | r | p% | | k o# d  k n s7 t d   n  | |  _ | |  _ d } d  |  _ d  |  _ xâ t j |  j |  j d t j  D]ż } | \ } } }	 }
 } ye t j | | |	  |  _ |  j j	 |  | rđ | rđ t
 |  j | |  |  _ n t
 |  j  |  _ Wn; t j k
 r@} |  j r1|  j j   n  d  |  _ q n XPq W|  j s^t j |  n  d |  _ |  j   |  _ d  S(   Ns   certfile requires keyfiles!   getaddrinfo returns an empty listi    (   R   t   getmailConfigurationErrort   hostt   portt   rawsockR   R   t   getaddrinfot   SOCK_STREAMt   connectR   R#   R   t
   _debuggingt   _getrespt   welcome(   R   R-   R.   R   R   t   msgt   rest   aft   socktypet   protot	   canonnamet   sa(    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyR   s   s4    (								c         C   s   |  j  j   } |  j d k r+ d G| GHn  | s@ t d   n  t |  } | d t k rj | d  | f S| d t k r | d d !| f S| d  | f S(   Ni   s   *get*s   -ERR EOFiţ˙˙˙i    i˙˙˙˙(   R   R(   R3   R   R   R   R   (   R   R%   t   octets(    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyt   _getline   s    c         C   sO   y |  j  d  } Wn" t t j f k
 r7 } | } n X|  j j   |  ` | S(   sM   Signoff: commit changes on server, unlock mailbox, close connection.
        t   QUIT(   t	   _shortcmdR   R   R#   R   R   (   R   t   respt   val(    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyt   quitŁ   s    
N(   R)   R*   R+   R    R   R   R>   RC   (    (    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyR   k   s    	(   R+   t   __all__R   t   poplibR   R   R   R   R   t   getmailcore.exceptionst   getmailcore.loggingt   getmailcoret   loggingt   LoggerR
   R    t   objectR   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/getmailcore/_pop3ssl.pyt   <module>   s   	(
V