Advertisement
Guest User

Untitled

a guest
Jul 20th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.33 KB | None | 0 0
  1. # logging
  2. from twisted.python import log
  3.  
  4. import sys
  5.  
  6. # MIME Multipart handling
  7. import email
  8. import email.mime.application
  9. import uuid
  10.  
  11. # IMAP Connection
  12. from twisted.mail import imap4
  13. from twisted.internet import protocol
  14.  
  15.  
  16. #SMTP Sending
  17. import os.path
  18. from OpenSSL.SSL import SSLv3_METHOD
  19. from twisted.internet import ssl
  20. from twisted.mail.smtp import ESMTPSenderFactory
  21. from twisted.internet.ssl import ClientContextFactory
  22. from twisted.internet.defer import Deferred
  23. from twisted.internet import reactor
  24.  
  25. #class AccountsManager(object):
  26.  
  27.  
  28. def connectToIMAPServer(imap_server, username, password):
  29. factory = IMAP4ClientFactory(username, password, login_insecure = True)
  30.  
  31. host, port = imap_server.split(":")
  32.  
  33. # connect to reactor
  34. if port == '993':
  35. reactor.connectSSL(host, int(port), factory, ssl.ClientContextFactory())
  36. else:
  37. if not port:
  38. port = 143
  39. reactor.connectTCP(host, int(port), factory)
  40.  
  41. d = factory.deferred
  42. d.addCallback(lambda r: factory.proto)
  43.  
  44. return d
  45.  
  46. class IMAP4Client(imap4.IMAP4Client):
  47. """
  48. A client with callbacks for greeting messages from an IMAP server.
  49. """
  50. greetDeferred = None
  51.  
  52. def serverGreeting(self, caps):
  53. self.serverCapabilities = caps
  54. if self.greetDeferred is not None:
  55. d, self.greetDeferred = self.greetDeferred, None
  56. d.callback(self)
  57.  
  58. class IMAP4ClientFactory(protocol.ClientFactory):
  59. usedUp = False
  60.  
  61. protocol = IMAP4Client
  62.  
  63.  
  64. def __init__(self, username, password, mailbox = "INBOX", login_insecure = False):
  65. self.ctx = ssl.ClientContextFactory()
  66.  
  67. self.username = username
  68. self.password = password
  69. self.mailbox = mailbox
  70. self.login_insecure = login_insecure
  71.  
  72. self.deferred = Deferred()
  73.  
  74. def buildProtocol(self, addr):
  75. """
  76. Initiate the protocol instance. Since we are building a simple IMAP
  77. client, we don't bother checking what capabilities the server has. We
  78. just add all the authenticators twisted.mail has. Note: Gmail no
  79. longer uses any of the methods below, it's been using XOAUTH since
  80. 2010.
  81. """
  82. assert not self.usedUp
  83. self.usedUp = True
  84.  
  85. p = self.protocol(self.ctx)
  86. p.factory = self
  87.  
  88. p.greetDeferred = self.deferred
  89.  
  90. p.registerAuthenticator(imap4.PLAINAuthenticator(self.username))
  91. p.registerAuthenticator(imap4.LOGINAuthenticator(self.username))
  92. p.registerAuthenticator(imap4.CramMD5ClientAuthenticator(self.username))
  93.  
  94. self.deferred.addCallback(self.GreetingCallback)
  95. self.deferred.addErrback(self.GreetingErrback)
  96.  
  97. self.proto = p
  98.  
  99. return p
  100.  
  101. def GreetingCallback(self, result):
  102. print "Secure Login"
  103. auth_d = self.proto.authenticate(self.password)
  104. auth_d.addCallback(self.AuthenticationCallback)
  105. auth_d.addErrback(self.AuthenticationErrback)
  106.  
  107. return auth_d # attach it to the main deferred
  108.  
  109. def GreetingErrback(self, error):
  110. log.err(error)
  111. self.CloseConnection()
  112. return error
  113.  
  114. def AuthenticationCallback(self, result):
  115. print "Selecting Mailbox"
  116. d = self.proto.examine(self.mailbox)
  117. return d
  118.  
  119. def AuthenticationErrback(self, failure):
  120. if self.login_insecure:
  121. failure.trap(imap4.NoSupportedAuthentication)
  122. return self.InsecureLogin()
  123. else:
  124. return error
  125.  
  126. def InsecureLogin(self):
  127. print "Insecure Login"
  128. d = self.proto.login(self.username, self.password)
  129. d.addCallback(self.AuthenticationCallback)
  130. return d
  131.  
  132. def CloseConnection(self):
  133. self.proto.transport.loseConnection()
  134.  
  135. def clientConnectionFailed(self, connector, reason):
  136. d, self.deferred = self.deferred, None
  137. d.errback(reason)
  138.  
  139.  
  140. class MailServer(object):
  141. "Manages a server"
  142.  
  143. size = 0
  144. used_space = 0
  145.  
  146.  
  147. def __init__(self, smtp_server, imap_server, username, password):
  148. self.smtp_server, self.smtp_port = smtp_server.split(":")
  149. self.imap_server, self.imap_port = imap_server.split(":")
  150. self.username = username
  151. self.password = password
  152.  
  153. self.imap_connection = IMAP4ClientFactory(username, password)
  154.  
  155.  
  156. def upload_data(self, data):
  157. """
  158. Uploads data to email server returns deferred that will return with the imap uid
  159. """
  160.  
  161. # Create a text/plain message
  162. id = str(uuid.uuid4()).upper()
  163.  
  164. msg = email.mime.Multipart.MIMEMultipart()
  165. msg['Subject'] = 'GMA ID: %s' % id
  166. msg['From'] = self.email_address
  167. msg['To'] = self.email_address
  168.  
  169. # The main body is just another attachment
  170. body = email.mime.Text.MIMEText("GMA ID: %s" % (self.uuid_id))
  171. msg.attach(body)
  172. att = email.mime.application.MIMEApplication(data,_subtype="raw")
  173. att.add_header('Content-Disposition','attachment',filename = os.path.basename(self.filename))
  174. msg.attach(att)
  175.  
  176. # Create a context factory which only allows SSLv3 and does not verify
  177. # the peer's certificate.
  178. contextFactory = ClientContextFactory()
  179. contextFactory.method = SSLv3_METHOD
  180.  
  181. d = Deferred()
  182.  
  183. mime_obj = StringIO(str(msg))
  184.  
  185. senderFactory = ESMTPSenderFactory(
  186. self.username,
  187. self.password,
  188. self.email_address,
  189. self.email_address,
  190. mime_obj,
  191. d,
  192. contextFactory=contextFactory)
  193.  
  194. d.addCallback(lambda r: self.email_sent(id, int(self.parts)) )
  195. d.addErrback(self.email_error)
  196.  
  197. reactor.connectTCP(self.smtp_server, self.smtp_port, senderFactory)
  198.  
  199. d.addCallback(self.upload_success, *args, **kw)
  200. d.addErrback(self.upload_error, 1)
  201.  
  202. return d
  203.  
  204.  
  205. def upload_success(self, result):
  206. print "upload was succesful!"
  207.  
  208. def upload_error(self, result):
  209. print "upload error"
  210.  
  211. def download_data(self, uid):
  212. """
  213. Downloads data from the email server returns a deferred that will return with the data
  214. """
  215. print "uid"
  216.  
  217.  
  218. if __name__ == "__main__":
  219. log.startLogging(sys.stdout)
  220. d = connectToIMAPServer("imap.gmail.com:993", "username", "password")
  221. def f(s):
  222. print s
  223.  
  224.  
  225. d.addCallback(lambda r: f("These are fired before the auth and examine callbacks, why?"))
  226. d.addCallback(lambda r: f("These are fired before the auth and examine callbacks, why?"))
  227. reactor.run()
  228.  
  229. from twisted.internet.endpoints import TCP4ClientEndpoint
  230. d = TCP4ClientEndpoint(reactor, host, int(port)).connect(factory)
  231.  
  232. d.addCallback(lambda r: factory.deferred)
  233.  
  234. d = factory.deferred
  235.  
  236. # logging
  237. from twisted.python import log
  238.  
  239.  
  240. import sys
  241.  
  242. # MIME Multipart handling
  243. import email
  244. import email.mime.application
  245. import uuid
  246.  
  247. # IMAP Connection
  248. from twisted.mail import imap4
  249. from twisted.internet import protocol
  250.  
  251.  
  252. #SMTP Sending
  253. import os.path
  254. from OpenSSL.SSL import SSLv3_METHOD
  255. from twisted.internet import ssl
  256. from twisted.mail.smtp import ESMTPSenderFactory
  257. from twisted.internet.ssl import ClientContextFactory
  258. from twisted.internet.defer import Deferred
  259. from twisted.internet import reactor
  260.  
  261. #class AccountsManager(object):
  262.  
  263.  
  264. def connectToIMAPServer(imap_server, username, password):
  265. factory = IMAP4ClientFactory(username, password, login_insecure = True)
  266.  
  267. host, port = imap_server.split(":")
  268.  
  269. # connect to reactor
  270. if port == '993':
  271. reactor.connectSSL(host, int(port), factory, ssl.ClientContextFactory())
  272. else:
  273. if not port:
  274. port = 143
  275. reactor.connectTCP(host, int(port), factory)
  276.  
  277. return factory.deferred
  278.  
  279. class IMAP4Client(imap4.IMAP4Client):
  280. """
  281. A client with callbacks for greeting messages from an IMAP server.
  282. """
  283. greetDeferred = None
  284.  
  285. def serverGreeting(self, caps):
  286. self.serverCapabilities = caps
  287. if self.greetDeferred is not None:
  288. d, self.greetDeferred = self.greetDeferred, None
  289. d.callback(self)
  290.  
  291. class IMAP4ClientFactory(protocol.ClientFactory):
  292. usedUp = False
  293.  
  294. protocol = IMAP4Client
  295.  
  296.  
  297. def __init__(self, username, password, mailbox = "INBOX", login_insecure = False):
  298. self.ctx = ssl.ClientContextFactory()
  299.  
  300. self.username = username
  301. self.password = password
  302. self.mailbox = mailbox
  303. self.login_insecure = login_insecure
  304.  
  305. # called when the protocol is all set up or there is an error setting it up
  306. self.deferred = Deferred()
  307.  
  308. def buildProtocol(self, addr):
  309. """
  310. Initiate the protocol instance. Since we are building a simple IMAP
  311. client, we don't bother checking what capabilities the server has. We
  312. just add all the authenticators twisted.mail has. Note: Gmail no
  313. longer uses any of the methods below, it's been using XOAUTH since
  314. 2010.
  315. """
  316. assert not self.usedUp
  317. self.usedUp = True
  318.  
  319. p = self.protocol(self.ctx)
  320. p.factory = self
  321.  
  322. # deferred for when the IMAP Greeting is done
  323. p.greetDeferred = Deferred()
  324.  
  325. p.registerAuthenticator(imap4.PLAINAuthenticator(self.username))
  326. p.registerAuthenticator(imap4.LOGINAuthenticator(self.username))
  327. p.registerAuthenticator(imap4.CramMD5ClientAuthenticator(self.username))
  328.  
  329. p.greetDeferred.addCallback(self.GreetingCallback)
  330. p.greetDeferred.addErrback(self.GreetingErrback)
  331.  
  332. self.proto = p
  333.  
  334. return p
  335.  
  336. def GreetingCallback(self, result):
  337. log.msg("Succesfully sent IMAP Greeting.")
  338. auth_d = self.proto.authenticate(self.password)
  339. auth_d.addCallback(self.AuthenticationCallback)
  340. auth_d.addErrback(self.AuthenticationErrback)
  341.  
  342. return auth_d
  343.  
  344. def GreetingErrback(self, error):
  345. log.msg("Problem sending greeting")
  346. log.err(error)
  347. self.CloseConnection()
  348. self.deferred.errback(error)
  349.  
  350. def AuthenticationCallback(self, result):
  351. log.msg("Authenticated")
  352. log.msg("Selecting Mailbox")
  353. d = self.proto.examine(self.mailbox)
  354. d.addCallback(self.MailboxSelectCallback)
  355. d.addErrback(self.MailboxSelectErrback)
  356. return d
  357.  
  358. def AuthenticationErrback(self, failure):
  359. log.msg("Unable to authenticate securly")
  360. if self.login_insecure:
  361. log.msg("Trying to login insecurly")
  362. failure.trap(imap4.NoSupportedAuthentication)
  363. return self.InsecureLogin()
  364. else:
  365. log.err(failure)
  366. self.deferred.errback(failure)
  367.  
  368. def InsecureLogin(self):
  369. log.msg("Logging in insecurly")
  370. d = self.proto.login(self.username, self.password)
  371. d.addCallback(self.AuthenticationCallback)
  372. return d
  373.  
  374. def MailboxSelectCallback(self, result):
  375. # connected and protocol set up
  376. log.msg("IMAP4 protocol setup")
  377. self.deferred.callback(self.proto)
  378.  
  379.  
  380. def MailboxSelectErrback(self, error):
  381. log.msg("Cannot select mailbox %s" % self.mailbox)
  382. log.err(error)
  383. self.deferred.errback(error)
  384.  
  385.  
  386. def CloseConnection(self):
  387. self.proto.transport.loseConnection()
  388.  
  389. def clientConnectionFailed(self, connector, reason):
  390. log.msg("Connecting was lost")
  391. log.err(reason)
  392. d, self.deferred = self.deferred, None
  393. d.errback(reason)
  394.  
  395.  
  396. class MailServer(object):
  397. "Manages a server"
  398.  
  399. size = 0
  400. used_space = 0
  401.  
  402.  
  403. def __init__(self, smtp_server, imap_server, username, password):
  404. self.smtp_server, self.smtp_port = smtp_server.split(":")
  405. self.imap_server, self.imap_port = imap_server.split(":")
  406. self.username = username
  407. self.password = password
  408.  
  409. self.imap_connection = IMAP4ClientFactory(username, password)
  410.  
  411.  
  412. def upload_data(self, data):
  413. """
  414. Uploads data to email server returns deferred that will return with the imap uid
  415. """
  416.  
  417. # Create a text/plain message
  418. id = str(uuid.uuid4()).upper()
  419.  
  420. msg = email.mime.Multipart.MIMEMultipart()
  421. msg['Subject'] = 'GMA ID: %s' % id
  422. msg['From'] = self.email_address
  423. msg['To'] = self.email_address
  424.  
  425. # The main body is just another attachment
  426. body = email.mime.Text.MIMEText("GMA ID: %s" % (self.uuid_id))
  427. msg.attach(body)
  428. att = email.mime.application.MIMEApplication(data,_subtype="raw")
  429. att.add_header('Content-Disposition','attachment',filename = os.path.basename(self.filename))
  430. msg.attach(att)
  431.  
  432. # Create a context factory which only allows SSLv3 and does not verify
  433. # the peer's certificate.
  434. contextFactory = ClientContextFactory()
  435. contextFactory.method = SSLv3_METHOD
  436.  
  437. d = Deferred()
  438.  
  439. mime_obj = StringIO(str(msg))
  440.  
  441. senderFactory = ESMTPSenderFactory(
  442. self.username,
  443. self.password,
  444. self.email_address,
  445. self.email_address,
  446. mime_obj,
  447. d,
  448. contextFactory=contextFactory)
  449.  
  450. d.addCallback(lambda r: self.email_sent(id, int(self.parts)) )
  451. d.addErrback(self.email_error)
  452.  
  453. reactor.connectTCP(self.smtp_server, self.smtp_port, senderFactory)
  454.  
  455. d.addCallback(self.upload_success, *args, **kw)
  456. d.addErrback(self.upload_error, 1)
  457.  
  458. return d
  459.  
  460.  
  461. def upload_success(self, result):
  462. print "upload was succesful!"
  463.  
  464. def upload_error(self, result):
  465. print "upload error"
  466.  
  467. def download_data(self, uid):
  468. """
  469. Downloads data from the email server returns a deferred that will return with the data
  470. """
  471. print "uid"
  472.  
  473.  
  474. if __name__ == "__main__":
  475. log.startLogging(sys.stdout)
  476. d = connectToIMAPServer("imap.gmail.com:993", "email", "password")
  477. def f(s):
  478. print s
  479.  
  480.  
  481. d.addCallback(lambda r: f("These are fired before the auth and examine callbacks, why?"))
  482. d.addCallback(lambda r: f("These are fired before the auth and examine callbacks, why?"))
  483. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement