Advertisement
Guest User

Untitled

a guest
May 11th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.40 KB | None | 0 0
  1.    1.
  2.        #!/usr/bin/env python
  3.    2.
  4.       #coding=utf-8
  5.    3.
  6.        
  7.    4.
  8.        
  9.    5.
  10.       """
  11.   6.
  12.      Client de IMAP4 que descarga contenido del INBOX de una cuenta en especifico
  13.   7.
  14.      para luego extraer el numero de telefono que debe venir en los correos enviados
  15.   8.
  16.      """
  17.    9.
  18.        
  19.   10.
  20.       import StringIO
  21.   11.
  22.       import sys
  23.   12.
  24.       from Config import retCredentials
  25.   13.
  26.        
  27.   14.
  28.       from twisted.internet.task import LoopingCall
  29.   15.
  30.       from twisted.internet import protocol
  31.   16.
  32.       from twisted.internet import defer
  33.   17.
  34.       from twisted.mail import imap4
  35.   18.
  36.       from twisted.python import util
  37.   19.
  38.       from twisted.python import log
  39.   20.
  40.       from twisted.internet import reactor
  41.   21.
  42.       debug = 1
  43.   22.
  44.        
  45.   23.
  46.       class IMAP4Client(imap4.IMAP4Client):
  47.   24.
  48.        
  49.   25.
  50.           def serverGreeting(self,caps):
  51.   26.
  52.               """
  53.  27.
  54.             Metodo llamado cuando el servidor contesta
  55.  28.
  56.             """
  57.   29.
  58.               if debug: print "On serverGreeting"
  59.   30.
  60.        
  61.   31.
  62.               self.serverCapabilities = caps
  63.   32.
  64.               if self.greetDeferred is not None:
  65.   33.
  66.                   d, self.greetDeferred = self.greetDeferred, None
  67.   34.
  68.                   d.addCallback(self.cbLogin)
  69.   35.
  70.                   d.callback(self)
  71.   36.
  72.        
  73.   37.
  74.        
  75.   38.
  76.           def cbLogin(self, proto):
  77.   39.
  78.               """
  79.  40.
  80.             Callback to IMAP login
  81.  41.
  82.             """
  83.   42.
  84.               if debug: print "On Login"
  85.   43.
  86.        
  87.   44.
  88.               login =  self.login(self.factory.username, self.factory.password)
  89.   45.
  90.               login.addCallback(self.startPolling)
  91.   46.
  92.        
  93.   47.
  94.        
  95.   48.
  96.           def startPolling(self, proto):
  97.   49.
  98.               """
  99.  50.
  100.             Callback to poll every x seconds
  101.  51.
  102.             """
  103.   52.
  104.               call = LoopingCall(self.selectMailbox,mailbox="INBOX")
  105.   53.
  106.               call.start(20, now=True)
  107.   54.
  108.              
  109.   55.
  110.              
  111.   56.
  112.           def selectMailbox(self, mailbox):
  113.   57.
  114.               """
  115.  58.
  116.             Select the mailbox to examin
  117.  59.
  118.             """
  119.   60.
  120.               if debug: print "On selectMailbox"
  121.   61.
  122.        
  123.   62.
  124.               mailbox = self.factory.mailbox
  125.   63.
  126.               return self.select(mailbox).addCallback(self.cbSelectSuccess)
  127.   64.
  128.              
  129.   65.
  130.        
  131.   66.
  132.           def cbSelectSuccess(self, selected):
  133.   67.
  134.               """
  135.  68.
  136.             Examine the INBOX mailbox for new mails
  137.  69.
  138.      
  139.  70.
  140.             """
  141.   71.
  142.               if debug: print "On cbSelectSuccess"
  143.   72.
  144.        
  145.   73.
  146.        
  147.   74.
  148.               self.messageCount = selected['EXISTS']
  149.   75.
  150.               print "Messages: ", self.messageCount
  151.   76.
  152.        
  153.   77.
  154.               unseen = selected['EXISTS'] - selected['RECENT']
  155.   78.
  156.        
  157.   79.
  158.               if selected['RECENT'] == 0:
  159.   80.
  160.                   print "No new messages"
  161.   81.
  162.                   return
  163.   82.
  164.        
  165.   83.
  166.               return self.fetchMessage("%s:*" % (unseen)
  167.   84.
  168.                       ).addCallback(self.cbProcMessages)
  169.   85.
  170.        
  171.   86.
  172.           def cbProcMessages(self,messages):
  173.   87.
  174.        
  175.   88.
  176.               print messages
  177.   89.
  178.        
  179.   90.
  180.        
  181.   91.
  182.       class IMAP4ClientFactory(protocol.ClientFactory):
  183.   92.
  184.        
  185.   93.
  186.           protocol = IMAP4Client
  187.   94.
  188.        
  189.   95.
  190.           def __init__(self, username, password,  onConn):
  191.   96.
  192.        
  193.   97.
  194.               self.username = username
  195.   98.
  196.               self.password = password
  197.   99.
  198.               self.mailbox = 'INBOX'
  199.  100.
  200.               self.onConn = onConn
  201.  101.
  202.        
  203.  102.
  204.           def buildProtocol(self,addr):
  205.  103.
  206.               if debug: print "On buildProtocol"
  207.  104.
  208.               p = self.protocol()
  209.  105.
  210.               p.factory = self
  211.  106.
  212.               p.greetDeferred = self.onConn
  213.  107.
  214.               auth = imap4.CramMD5ClientAuthenticator(self.username)
  215.  108.
  216.               p.registerAuthenticator(auth)
  217.  109.
  218.        
  219.  110.
  220.               return p
  221.  111.
  222.        
  223.  112.
  224.           def clientConectionFailed(self, connector, reason):
  225.  113.
  226.               d, self.onConn = self.onConn, None
  227.  114.
  228.               d.errback(reason)
  229.  115.
  230.        
  231.  116.
  232.        
  233.  117.
  234.        
  235.  118.
  236.       def ebConnection(reason):
  237.  119.
  238.           log.startLogging(sys.stdout)
  239.  120.
  240.           log.err(reason)
  241.  121.
  242.           reactor.stop()
  243.  122.
  244.        
  245.  123.
  246.        
  247.  124.
  248.        
  249.  125.
  250.       PORT = 143
  251.  126.
  252.       RESULT = "INBOX"
  253.  127.
  254.        
  255.  128.
  256.        
  257.  129.
  258.       def main():
  259.  130.
  260.           credentials = retCredentials()
  261.  131.
  262.           hostname = credentials['server']
  263.  132.
  264.           username = credentials['mailbox']
  265.  133.
  266.           password = util.getPassword('IMAP4 Password: ')
  267.  134.
  268.        
  269.  135.
  270.           onConn = defer.Deferred(
  271.  136.
  272.                   ).addErrback(ebConnection
  273.  137.
  274.                   )
  275.  138.
  276.        
  277.  139.
  278.           factory = IMAP4ClientFactory(username, password, onConn)
  279.  140.
  280.        
  281.  141.
  282.          
  283.  142.
  284.           reactor.connectTCP(hostname, PORT, factory)
  285.  143.
  286.           reactor.run()
  287.  144.
  288.        
  289.  145.
  290.       if  __name__ == "__main__":
  291.  146.
  292.           main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement