summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/gmail_notify.py
blob: a56ca699084e11d46c9098802976f06d7da84eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
    Slixmpp: The Slick XMPP Library
    Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
    This file is part of Slixmpp.

    See the file LICENSE for copying permission.
"""

import logging
from slixmpp.plugins import BasePlugin
from .. xmlstream.handler.callback import Callback
from .. xmlstream.matcher.xpath import MatchXPath
from .. xmlstream.stanzabase import register_stanza_plugin, ElementBase, ET, JID
from .. stanza.iq import Iq


log = logging.getLogger(__name__)


class GmailQuery(ElementBase):
    namespace = 'google:mail:notify'
    name = 'query'
    plugin_attrib = 'gmail'
    interfaces = {'newer-than-time', 'newer-than-tid', 'q', 'search'}

    def get_search(self):
        return self['q']

    def set_search(self, search):
        self['q'] = search

    def del_search(self):
        del self['q']


class MailBox(ElementBase):
    namespace = 'google:mail:notify'
    name = 'mailbox'
    plugin_attrib = 'mailbox'
    interfaces = {'result-time', 'total-matched', 'total-estimate',
                  'url', 'threads', 'matched', 'estimate'}

    def get_threads(self):
        threads = []
        for threadXML in self.xml.findall('{%s}%s' % (MailThread.namespace,
                                                      MailThread.name)):
            threads.append(MailThread(xml=threadXML, parent=None))
        return threads

    def get_matched(self):
        return self['total-matched']

    def get_estimate(self):
        return self['total-estimate'] == '1'


class MailThread(ElementBase):
    namespace = 'google:mail:notify'
    name = 'mail-thread-info'
    plugin_attrib = 'thread'
    interfaces = {'tid', 'participation', 'messages', 'date',
                  'senders', 'url', 'labels', 'subject', 'snippet'}
    sub_interfaces = {'labels', 'subject', 'snippet'}

    def get_senders(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 = {'address', 'name', 'originator', 'unread'}

    def get_originator(self):
        return self.xml.attrib.get('originator', '0') == '1'

    def get_unread(self):
        return self.xml.attrib.get('unread', '0') == '1'


class NewMail(ElementBase):
    namespace = 'google:mail:notify'
    name = 'new-mail'
    plugin_attrib = 'new-mail'


class gmail_notify(BasePlugin):
    """
    Google Talk: Gmail Notifications
    """

    def plugin_init(self):
        self.description = 'Google Talk: Gmail Notifications'

        self.xmpp.registerHandler(
            Callback('Gmail Result',
                     MatchXPath('{%s}iq/{%s}%s' % (self.xmpp.default_ns,
                                                   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))

        register_stanza_plugin(Iq, GmailQuery)
        register_stanza_plugin(Iq, MailBox)
        register_stanza_plugin(Iq, NewMail)

        self.last_result_time = None

    def handle_gmail(self, iq):
        mailbox = iq['mailbox']
        approx = ' approximately' if mailbox['estimated'] else ''
        log.info('Gmail: Received%s %s emails', approx, mailbox['total-matched'])
        self.last_result_time = mailbox['result-time']
        self.xmpp.event('gmail_messages', iq)

    def handle_new_mail(self, iq):
        log.info("Gmail: New emails received!")
        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:
            log.info("Gmail: Checking for new emails")
        else:
            log.info('Gmail: Searching for emails matching: "%s"', query)
        iq = self.xmpp.Iq()
        iq['type'] = 'get'
        iq['to'] = self.xmpp.boundjid.bare
        iq['gmail']['q'] = query
        iq['gmail']['newer-than-time'] = newer
        return iq.send()