1
from twisted.words.protocols.jabber import xmlstream, client, jid
2
from twisted.words.xish import domish
3
from twisted.internet import reactor
5
COLOR_GREEN = "\033[92m"
8
print COLOR_GREEN + msg + COLOR_END
11
def __init__(self, jid, password, labels, cb_new, cb_count):
12
self.host = "talk.google.com"
15
self.password = password
17
self.cb_count = cb_count
21
self.labels_iter = iter(self.labels)
23
# True: use mailbox to fill labels with unread count
24
# False: Got new mail: query since tid and display notification
26
self.factory = client.XMPPClientFactory(jid, password)
27
self.factory.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, self.connectedCB)
28
self.factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.authenticationCB)
29
reactor.connectTCP(self.host, self.port, self.factory)
31
def authenticationCB(self, xmlstream):
32
xmlstream.addOnetimeObserver("/iq", self.usersettingIQ)
34
# We set the usersetting mail-notification
35
iq = domish.Element((None, "iq"), attribs={"type": "set", "id": "user-setting-3"})
36
usersetting = iq.addElement(("google:setting", "usersetting"))
37
mailnotifications = usersetting.addElement((None, "mailnotifications"))
38
mailnotifications.attributes['value'] = "true"
41
def usersettingIQ(self, iq):
45
self.xmlstream.removeObserver("/iq", self.gotNewMail)
46
self.xmlstream.addOnetimeObserver("/iq", self.gotLabel)
48
iq = domish.Element((None, "iq"), attribs={"type": "get", "id": "mail-request-1"})
49
query = iq.addElement(("google:mail:notify", "query"))
50
self.xmlstream.send(iq)
54
label = self.labels_iter.next()
56
self.xmlstream.addOnetimeObserver("/iq", self.gotLabel, label=label)
58
iq = domish.Element((None, "iq"), attribs={"type": "get", "id": "mail-request-1"})
59
query = iq.addElement(("google:mail:notify", "query"))
60
query.attributes['q'] = "label:%s AND is:unread" % label
61
self.xmlstream.send(iq)
63
self.labels_iter = iter(self.labels)
64
self.xmlstream.addObserver("/iq", self.gotNewMail)
65
self.cb_count(self.count)
67
def gotLabel(self, iq, label="inbox"):
68
if iq.firstChildElement() and iq.firstChildElement().name == "mailbox":
69
mailbox = iq.firstChildElement()
70
self.count[label] = unicode(mailbox.attributes['total-matched'])
73
if mailbox.firstChildElement():
74
self.last_tid = mailbox.firstChildElement().attributes['tid']
78
DEBUG("ERROR: received unexpected iq after querying for INBOX")
80
def gotNewMail(self, iq):
81
if iq.firstChildElement() and iq.firstChildElement().name == "new-mail":
82
self.xmlstream.removeObserver("/iq", self.gotNewMail)
85
iq = domish.Element((None, "iq"), attribs={"type": "result", "id": iq.attributes['id']})
86
self.xmlstream.send(iq)
89
self.xmlstream.addOnetimeObserver("/iq", self.gotNewMailQueryResult)
91
iq = domish.Element((None, "iq"), attribs={"type": "get", "id": "mail-request-1"})
92
query = iq.addElement(("google:mail:notify", "query"))
93
query.attributes['newer-than-tid'] = self.last_tid
94
self.xmlstream.send(iq)
96
DEBUG("ERROR: This was no new mail iq")
98
def gotNewMailQueryResult(self, iq):
99
if iq.firstChildElement() and iq.firstChildElement().name == "mailbox":
100
mailbox = iq.children[0]
101
threads = mailbox.children
104
self.newest_tid = unicode(newest.attributes['tid'])
108
for thread in threads:
110
for child in thread.children:
111
if child.name == "senders":
112
for sender in child.children:
113
if "address" in sender.attributes:
114
mail['sender_address'] = unicode(sender.attributes['address'])
115
if "name" in sender.attributes:
116
mail['sender_name'] = unicode(sender.attributes['name'])
117
elif child.name == "labels":
118
mail['labels'] = unicode(child).split("|")
119
elif child.name == "subject":
120
mail['subject'] = unicode(child)
121
elif child.name == "snippet":
122
mail['snippet'] = unicode(child)
129
def rawDataIn(self, buf):
130
print u"< %s" % unicode(buf, "utf-8")
132
def rawDataOut(self, buf):
133
print u"> %s" % unicode(buf, "utf-8")
135
def connectedCB(self, xmlstream):
136
self.xmlstream = xmlstream
138
xmlstream.rawDataInFn = self.rawDataIn
139
xmlstream.rawDataOutFn = self.rawDataOut
b'\\ No newline at end of file'