home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_672 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.5 KB  |  305 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import socket
  6. __all__ = [
  7.     'POP3',
  8.     'error_proto']
  9.  
  10. class error_proto(Exception):
  11.     pass
  12.  
  13. POP3_PORT = 110
  14. POP3_SSL_PORT = 995
  15. CR = '\r'
  16. LF = '\n'
  17. CRLF = CR + LF
  18.  
  19. class POP3:
  20.     
  21.     def __init__(self, host, port = POP3_PORT, timeout = socket._GLOBAL_DEFAULT_TIMEOUT):
  22.         self.host = host
  23.         self.port = port
  24.         self.sock = socket.create_connection((host, port), timeout)
  25.         self.file = self.sock.makefile('rb')
  26.         self._debugging = 0
  27.         self.welcome = self._getresp()
  28.  
  29.     
  30.     def _putline(self, line):
  31.         if self._debugging > 1:
  32.             print '*put*', repr(line)
  33.         
  34.         self.sock.sendall('%s%s' % (line, CRLF))
  35.  
  36.     
  37.     def _putcmd(self, line):
  38.         if self._debugging:
  39.             print '*cmd*', repr(line)
  40.         
  41.         self._putline(line)
  42.  
  43.     
  44.     def _getline(self):
  45.         line = self.file.readline()
  46.         if self._debugging > 1:
  47.             print '*get*', repr(line)
  48.         
  49.         if not line:
  50.             raise error_proto('-ERR EOF')
  51.         line
  52.         octets = len(line)
  53.         if line[-2:] == CRLF:
  54.             return (line[:-2], octets)
  55.         if line[0] == CR:
  56.             return (line[1:-1], octets)
  57.         return (line[:-1], octets)
  58.  
  59.     
  60.     def _getresp(self):
  61.         (resp, o) = self._getline()
  62.         if self._debugging > 1:
  63.             print '*resp*', repr(resp)
  64.         
  65.         c = resp[:1]
  66.         if c != '+':
  67.             raise error_proto(resp)
  68.         c != '+'
  69.         return resp
  70.  
  71.     
  72.     def _getlongresp(self):
  73.         resp = self._getresp()
  74.         list = []
  75.         octets = 0
  76.         (line, o) = self._getline()
  77.         while line != '.':
  78.             if line[:2] == '..':
  79.                 o = o - 1
  80.                 line = line[1:]
  81.             
  82.             octets = octets + o
  83.             list.append(line)
  84.             (line, o) = self._getline()
  85.         return (resp, list, octets)
  86.  
  87.     
  88.     def _shortcmd(self, line):
  89.         self._putcmd(line)
  90.         return self._getresp()
  91.  
  92.     
  93.     def _longcmd(self, line):
  94.         self._putcmd(line)
  95.         return self._getlongresp()
  96.  
  97.     
  98.     def getwelcome(self):
  99.         return self.welcome
  100.  
  101.     
  102.     def set_debuglevel(self, level):
  103.         self._debugging = level
  104.  
  105.     
  106.     def user(self, user):
  107.         return self._shortcmd('USER %s' % user)
  108.  
  109.     
  110.     def pass_(self, pswd):
  111.         return self._shortcmd('PASS %s' % pswd)
  112.  
  113.     
  114.     def stat(self):
  115.         retval = self._shortcmd('STAT')
  116.         rets = retval.split()
  117.         if self._debugging:
  118.             print '*stat*', repr(rets)
  119.         
  120.         numMessages = int(rets[1])
  121.         sizeMessages = int(rets[2])
  122.         return (numMessages, sizeMessages)
  123.  
  124.     
  125.     def list(self, which = None):
  126.         if which is not None:
  127.             return self._shortcmd('LIST %s' % which)
  128.         return self._longcmd('LIST')
  129.  
  130.     
  131.     def retr(self, which):
  132.         return self._longcmd('RETR %s' % which)
  133.  
  134.     
  135.     def dele(self, which):
  136.         return self._shortcmd('DELE %s' % which)
  137.  
  138.     
  139.     def noop(self):
  140.         return self._shortcmd('NOOP')
  141.  
  142.     
  143.     def rset(self):
  144.         return self._shortcmd('RSET')
  145.  
  146.     
  147.     def quit(self):
  148.         
  149.         try:
  150.             resp = self._shortcmd('QUIT')
  151.         except error_proto:
  152.             val = None
  153.             resp = val
  154.  
  155.         self.file.close()
  156.         self.sock.close()
  157.         del self.file
  158.         del self.sock
  159.         return resp
  160.  
  161.     
  162.     def rpop(self, user):
  163.         return self._shortcmd('RPOP %s' % user)
  164.  
  165.     timestamp = re.compile('\\+OK.*(<[^>]+>)')
  166.     
  167.     def apop(self, user, secret):
  168.         m = self.timestamp.match(self.welcome)
  169.         if not m:
  170.             raise error_proto('-ERR APOP not supported by server')
  171.         m
  172.         import hashlib
  173.         digest = hashlib.md5(m.group(1) + secret).digest()
  174.         digest = ''.join(map((lambda x: '%02x' % ord(x)), digest))
  175.         return self._shortcmd('APOP %s %s' % (user, digest))
  176.  
  177.     
  178.     def top(self, which, howmuch):
  179.         return self._longcmd('TOP %s %s' % (which, howmuch))
  180.  
  181.     
  182.     def uidl(self, which = None):
  183.         if which is not None:
  184.             return self._shortcmd('UIDL %s' % which)
  185.         return self._longcmd('UIDL')
  186.  
  187.  
  188.  
  189. try:
  190.     import ssl
  191. except ImportError:
  192.     pass
  193.  
  194.  
  195. class POP3_SSL(POP3):
  196.     
  197.     def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None):
  198.         self.host = host
  199.         self.port = port
  200.         self.keyfile = keyfile
  201.         self.certfile = certfile
  202.         self.buffer = ''
  203.         msg = 'getaddrinfo returns an empty list'
  204.         self.sock = None
  205.         for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
  206.             (af, socktype, proto, canonname, sa) = res
  207.             
  208.             try:
  209.                 self.sock = socket.socket(af, socktype, proto)
  210.                 self.sock.connect(sa)
  211.             except socket.error:
  212.                 msg = None
  213.                 if self.sock:
  214.                     self.sock.close()
  215.                 
  216.                 self.sock = None
  217.                 continue
  218.  
  219.         
  220.         if not self.sock:
  221.             raise socket.error, msg
  222.         self.sock
  223.         self.file = self.sock.makefile('rb')
  224.         self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
  225.         self._debugging = 0
  226.         self.welcome = self._getresp()
  227.  
  228.     
  229.     def _fillBuffer(self):
  230.         localbuf = self.sslobj.read()
  231.         if len(localbuf) == 0:
  232.             raise error_proto('-ERR EOF')
  233.         len(localbuf) == 0
  234.         self.buffer += localbuf
  235.  
  236.     
  237.     def _getline(self):
  238.         line = ''
  239.         renewline = re.compile('.*?\\n')
  240.         match = renewline.match(self.buffer)
  241.         while not match:
  242.             self._fillBuffer()
  243.             match = renewline.match(self.buffer)
  244.         line = match.group(0)
  245.         self.buffer = renewline.sub('', self.buffer, 1)
  246.         if self._debugging > 1:
  247.             print '*get*', repr(line)
  248.         
  249.         octets = len(line)
  250.         if line[-2:] == CRLF:
  251.             return (line[:-2], octets)
  252.         if line[0] == CR:
  253.             return (line[1:-1], octets)
  254.         return (line[:-1], octets)
  255.  
  256.     
  257.     def _putline(self, line):
  258.         if self._debugging > 1:
  259.             print '*put*', repr(line)
  260.         
  261.         line += CRLF
  262.         bytes = len(line)
  263.         while bytes > 0:
  264.             sent = self.sslobj.write(line)
  265.             if sent == bytes:
  266.                 break
  267.             
  268.             line = line[sent:]
  269.             bytes = bytes - sent
  270.  
  271.     
  272.     def quit(self):
  273.         
  274.         try:
  275.             resp = self._shortcmd('QUIT')
  276.         except error_proto:
  277.             val = None
  278.             resp = val
  279.  
  280.         self.sock.close()
  281.         del self.sslobj
  282.         del self.sock
  283.         return resp
  284.  
  285.  
  286. __all__.append('POP3_SSL')
  287. if __name__ == '__main__':
  288.     import sys
  289.     a = POP3(sys.argv[1])
  290.     print a.getwelcome()
  291.     a.user(sys.argv[2])
  292.     a.pass_(sys.argv[3])
  293.     a.list()
  294.     (numMsgs, totalSize) = a.stat()
  295.     for i in range(1, numMsgs + 1):
  296.         (header, msg, octets) = a.retr(i)
  297.         print 'Message %d:' % i
  298.         for line in msg:
  299.             print '   ' + line
  300.         
  301.         print '-----------------------'
  302.     
  303.     a.quit()
  304.  
  305.