gmail_notify.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """
  2. SleekXMPP: The Sleek XMPP Library
  3. Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
  4. This file is part of SleekXMPP.
  5. See the file LICENSE for copying permission.
  6. """
  7. import logging
  8. from . import base
  9. from .. xmlstream.handler.callback import Callback
  10. from .. xmlstream.matcher.xpath import MatchXPath
  11. from .. xmlstream.stanzabase import registerStanzaPlugin, ElementBase, ET, JID
  12. from .. stanza.iq import Iq
  13. log = logging.getLogger(__name__)
  14. class GmailQuery(ElementBase):
  15. namespace = 'google:mail:notify'
  16. name = 'query'
  17. plugin_attrib = 'gmail'
  18. interfaces = set(('newer-than-time', 'newer-than-tid', 'q', 'search'))
  19. def getSearch(self):
  20. return self['q']
  21. def setSearch(self, search):
  22. self['q'] = search
  23. def delSearch(self):
  24. del self['q']
  25. class MailBox(ElementBase):
  26. namespace = 'google:mail:notify'
  27. name = 'mailbox'
  28. plugin_attrib = 'mailbox'
  29. interfaces = set(('result-time', 'total-matched', 'total-estimate',
  30. 'url', 'threads', 'matched', 'estimate'))
  31. def getThreads(self):
  32. threads = []
  33. for threadXML in self.xml.findall('{%s}%s' % (MailThread.namespace,
  34. MailThread.name)):
  35. threads.append(MailThread(xml=threadXML, parent=None))
  36. return threads
  37. def getMatched(self):
  38. return self['total-matched']
  39. def getEstimate(self):
  40. return self['total-estimate'] == '1'
  41. class MailThread(ElementBase):
  42. namespace = 'google:mail:notify'
  43. name = 'mail-thread-info'
  44. plugin_attrib = 'thread'
  45. interfaces = set(('tid', 'participation', 'messages', 'date',
  46. 'senders', 'url', 'labels', 'subject', 'snippet'))
  47. sub_interfaces = set(('labels', 'subject', 'snippet'))
  48. def getSenders(self):
  49. senders = []
  50. sendersXML = self.xml.find('{%s}senders' % self.namespace)
  51. if sendersXML is not None:
  52. for senderXML in sendersXML.findall('{%s}sender' % self.namespace):
  53. senders.append(MailSender(xml=senderXML, parent=None))
  54. return senders
  55. class MailSender(ElementBase):
  56. namespace = 'google:mail:notify'
  57. name = 'sender'
  58. plugin_attrib = 'sender'
  59. interfaces = set(('address', 'name', 'originator', 'unread'))
  60. def getOriginator(self):
  61. return self.xml.attrib.get('originator', '0') == '1'
  62. def getUnread(self):
  63. return self.xml.attrib.get('unread', '0') == '1'
  64. class NewMail(ElementBase):
  65. namespace = 'google:mail:notify'
  66. name = 'new-mail'
  67. plugin_attrib = 'new-mail'
  68. class gmail_notify(base.base_plugin):
  69. """
  70. Google Talk: Gmail Notifications
  71. """
  72. def plugin_init(self):
  73. self.description = 'Google Talk: Gmail Notifications'
  74. self.xmpp.registerHandler(
  75. Callback('Gmail Result',
  76. MatchXPath('{%s}iq/{%s}%s' % (self.xmpp.default_ns,
  77. MailBox.namespace,
  78. MailBox.name)),
  79. self.handle_gmail))
  80. self.xmpp.registerHandler(
  81. Callback('Gmail New Mail',
  82. MatchXPath('{%s}iq/{%s}%s' % (self.xmpp.default_ns,
  83. NewMail.namespace,
  84. NewMail.name)),
  85. self.handle_new_mail))
  86. registerStanzaPlugin(Iq, GmailQuery)
  87. registerStanzaPlugin(Iq, MailBox)
  88. registerStanzaPlugin(Iq, NewMail)
  89. self.last_result_time = None
  90. def handle_gmail(self, iq):
  91. mailbox = iq['mailbox']
  92. approx = ' approximately' if mailbox['estimated'] else ''
  93. log.info('Gmail: Received%s %s emails', approx, mailbox['total-matched'])
  94. self.last_result_time = mailbox['result-time']
  95. self.xmpp.event('gmail_messages', iq)
  96. def handle_new_mail(self, iq):
  97. log.info("Gmail: New emails received!")
  98. self.xmpp.event('gmail_notify')
  99. self.checkEmail()
  100. def getEmail(self, query=None):
  101. return self.search(query)
  102. def checkEmail(self):
  103. return self.search(newer=self.last_result_time)
  104. def search(self, query=None, newer=None):
  105. if query is None:
  106. log.info("Gmail: Checking for new emails")
  107. else:
  108. log.info('Gmail: Searching for emails matching: "%s"', query)
  109. iq = self.xmpp.Iq()
  110. iq['type'] = 'get'
  111. iq['to'] = self.xmpp.boundjid.bare
  112. iq['gmail']['q'] = query
  113. iq['gmail']['newer-than-time'] = newer
  114. return iq.send()