2009-06-03 22:56:51 +00:00
|
|
|
"""
|
2010-07-12 02:01:51 +00:00
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2009-06-03 22:56:51 +00:00
|
|
|
"""
|
2010-07-12 02:01:51 +00:00
|
|
|
|
2009-06-03 22:56:51 +00:00
|
|
|
import logging
|
2010-07-12 02:01:51 +00:00
|
|
|
from . import base
|
|
|
|
from .. xmlstream.handler.callback import Callback
|
|
|
|
from .. xmlstream.matcher.xpath import MatchXPath
|
2010-07-19 17:58:53 +00:00
|
|
|
from .. xmlstream.stanzabase import registerStanzaPlugin, ElementBase, ET, JID
|
2010-07-12 02:01:51 +00:00
|
|
|
from .. stanza.iq import Iq
|
|
|
|
|
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2010-07-12 02:01:51 +00:00
|
|
|
class GmailQuery(ElementBase):
|
|
|
|
namespace = 'google:mail:notify'
|
|
|
|
name = 'query'
|
|
|
|
plugin_attrib = 'gmail'
|
|
|
|
interfaces = set(('newer-than-time', 'newer-than-tid', 'q', 'search'))
|
|
|
|
|
|
|
|
def getSearch(self):
|
|
|
|
return self['q']
|
|
|
|
|
|
|
|
def setSearch(self, search):
|
|
|
|
self['q'] = search
|
|
|
|
|
|
|
|
def delSearch(self):
|
|
|
|
del self['q']
|
|
|
|
|
|
|
|
|
|
|
|
class MailBox(ElementBase):
|
|
|
|
namespace = 'google:mail:notify'
|
|
|
|
name = 'mailbox'
|
|
|
|
plugin_attrib = 'mailbox'
|
2010-11-06 05:28:59 +00:00
|
|
|
interfaces = set(('result-time', 'total-matched', 'total-estimate',
|
2010-07-12 02:01:51 +00:00
|
|
|
'url', 'threads', 'matched', 'estimate'))
|
|
|
|
|
|
|
|
def getThreads(self):
|
|
|
|
threads = []
|
2010-11-06 05:28:59 +00:00
|
|
|
for threadXML in self.xml.findall('{%s}%s' % (MailThread.namespace,
|
2010-07-12 02:01:51 +00:00
|
|
|
MailThread.name)):
|
|
|
|
threads.append(MailThread(xml=threadXML, parent=None))
|
|
|
|
return threads
|
|
|
|
|
|
|
|
def getMatched(self):
|
|
|
|
return self['total-matched']
|
|
|
|
|
|
|
|
def getEstimate(self):
|
|
|
|
return self['total-estimate'] == '1'
|
|
|
|
|
|
|
|
|
|
|
|
class MailThread(ElementBase):
|
|
|
|
namespace = 'google:mail:notify'
|
|
|
|
name = 'mail-thread-info'
|
|
|
|
plugin_attrib = 'thread'
|
2010-11-06 05:28:59 +00:00
|
|
|
interfaces = set(('tid', 'participation', 'messages', 'date',
|
2010-07-12 02:01:51 +00:00
|
|
|
'senders', 'url', 'labels', 'subject', 'snippet'))
|
|
|
|
sub_interfaces = set(('labels', 'subject', 'snippet'))
|
2010-11-06 05:28:59 +00:00
|
|
|
|
2010-07-12 02:01:51 +00:00
|
|
|
def getSenders(self):
|
|
|
|
senders = []
|
|
|
|
sendersXML = self.xml.find('{%s}senders' % self.namespace)
|
|
|
|
if sendersXML is not None:
|
|
|
|
for senderXML in sendersXML.findall('{%s}sender' % self.namespace):
|
|
|
|
senders.append(MailSender(xml=senderXML, parent=None))
|
|
|
|
return senders
|
|
|
|
|
|
|
|
|
|
|
|
class MailSender(ElementBase):
|
|
|
|
namespace = 'google:mail:notify'
|
|
|
|
name = 'sender'
|
|
|
|
plugin_attrib = 'sender'
|
|
|
|
interfaces = set(('address', 'name', 'originator', 'unread'))
|
|
|
|
|
|
|
|
def getOriginator(self):
|
|
|
|
return self.xml.attrib.get('originator', '0') == '1'
|
|
|
|
|
|
|
|
def getUnread(self):
|
|
|
|
return self.xml.attrib.get('unread', '0') == '1'
|
|
|
|
|
|
|
|
|
|
|
|
class NewMail(ElementBase):
|
|
|
|
namespace = 'google:mail:notify'
|
|
|
|
name = 'new-mail'
|
|
|
|
plugin_attrib = 'new-mail'
|
|
|
|
|
2009-06-03 22:56:51 +00:00
|
|
|
|
|
|
|
class gmail_notify(base.base_plugin):
|
2010-07-12 02:01:51 +00:00
|
|
|
"""
|
|
|
|
Google Talk: Gmail Notifications
|
|
|
|
"""
|
2010-11-06 05:28:59 +00:00
|
|
|
|
2010-07-12 02:01:51 +00:00
|
|
|
def plugin_init(self):
|
|
|
|
self.description = 'Google Talk: Gmail Notifications'
|
|
|
|
|
|
|
|
self.xmpp.registerHandler(
|
|
|
|
Callback('Gmail Result',
|
2010-11-06 05:28:59 +00:00
|
|
|
MatchXPath('{%s}iq/{%s}%s' % (self.xmpp.default_ns,
|
2010-07-12 02:01:51 +00:00
|
|
|
MailBox.namespace,
|
|
|
|
MailBox.name)),
|
|
|
|
self.handle_gmail))
|
|
|
|
|
|
|
|
self.xmpp.registerHandler(
|
|
|
|
Callback('Gmail New Mail',
|
|
|
|
MatchXPath('{%s}iq/{%s}%s' % (self.xmpp.default_ns,
|
|
|
|
NewMail.namespace,
|
|
|
|
NewMail.name)),
|
|
|
|
self.handle_new_mail))
|
2010-11-06 05:28:59 +00:00
|
|
|
|
2010-07-19 17:58:53 +00:00
|
|
|
registerStanzaPlugin(Iq, GmailQuery)
|
|
|
|
registerStanzaPlugin(Iq, MailBox)
|
|
|
|
registerStanzaPlugin(Iq, NewMail)
|
2010-07-12 02:01:51 +00:00
|
|
|
|
|
|
|
self.last_result_time = None
|
|
|
|
|
|
|
|
def handle_gmail(self, iq):
|
|
|
|
mailbox = iq['mailbox']
|
|
|
|
approx = ' approximately' if mailbox['estimated'] else ''
|
2011-11-19 20:07:57 +00:00
|
|
|
log.info('Gmail: Received%s %s emails', approx, mailbox['total-matched'])
|
2010-07-12 02:01:51 +00:00
|
|
|
self.last_result_time = mailbox['result-time']
|
|
|
|
self.xmpp.event('gmail_messages', iq)
|
|
|
|
|
|
|
|
def handle_new_mail(self, iq):
|
2010-11-06 05:28:59 +00:00
|
|
|
log.info("Gmail: New emails received!")
|
2010-07-12 02:01:51 +00:00
|
|
|
self.xmpp.event('gmail_notify')
|
|
|
|
self.checkEmail()
|
|
|
|
|
|
|
|
def getEmail(self, query=None):
|
|
|
|
return self.search(query)
|
|
|
|
|
|
|
|
def checkEmail(self):
|
|
|
|
return self.search(newer=self.last_result_time)
|
|
|
|
|
|
|
|
def search(self, query=None, newer=None):
|
|
|
|
if query is None:
|
2010-11-06 05:28:59 +00:00
|
|
|
log.info("Gmail: Checking for new emails")
|
2010-07-12 02:01:51 +00:00
|
|
|
else:
|
2011-11-19 20:07:57 +00:00
|
|
|
log.info('Gmail: Searching for emails matching: "%s"', query)
|
2010-07-12 02:01:51 +00:00
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['type'] = 'get'
|
2010-12-16 20:38:00 +00:00
|
|
|
iq['to'] = self.xmpp.boundjid.bare
|
2010-07-12 02:01:51 +00:00
|
|
|
iq['gmail']['q'] = query
|
|
|
|
iq['gmail']['newer-than-time'] = newer
|
|
|
|
return iq.send()
|