~jconti/gm-notify/messaging-menu

« back to all changes in this revision

Viewing changes to gtalk.py

  • Committer: Alexander Hungenberg
  • Date: 2010-02-22 18:59:25 UTC
  • Revision ID: defreng@defreng-desktop-20100222185925-ygmmftf29gwg7cke
ok - first commit with new backend... You wanna do some alpha testing?

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from twisted.words.protocols.jabber import xmlstream, client, jid
 
2
from twisted.words.xish import domish
 
3
from twisted.internet import reactor
 
4
 
 
5
COLOR_GREEN = "\033[92m"
 
6
COLOR_END = "\033[0m"
 
7
def DEBUG(msg):
 
8
    print COLOR_GREEN + msg + COLOR_END
 
9
 
 
10
class MailChecker():
 
11
    def __init__(self, jid, password, labels, cb_new, cb_count):
 
12
        self.host = "talk.google.com"
 
13
        self.port = 5222
 
14
        self.jid = jid
 
15
        self.password = password
 
16
        self.cb_new = cb_new
 
17
        self.cb_count = cb_count
 
18
        
 
19
        self.last_tid = "0"
 
20
        self.labels = labels
 
21
        self.labels_iter = iter(self.labels)
 
22
        self.count = {}
 
23
        # True: use mailbox to fill labels with unread count
 
24
        # False: Got new mail: query since tid and display notification
 
25
 
 
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)
 
30
 
 
31
    def authenticationCB(self, xmlstream):
 
32
        xmlstream.addOnetimeObserver("/iq", self.usersettingIQ)
 
33
        
 
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"
 
39
        xmlstream.send(iq)
 
40
    
 
41
    def usersettingIQ(self, iq):
 
42
        self.queryInbox()
 
43
    
 
44
    def queryInbox(self):
 
45
        self.xmlstream.removeObserver("/iq", self.gotNewMail)
 
46
        self.xmlstream.addOnetimeObserver("/iq", self.gotLabel)
 
47
        
 
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)
 
51
    
 
52
    def queryLabel(self):
 
53
        try:
 
54
            label = self.labels_iter.next()
 
55
 
 
56
            self.xmlstream.addOnetimeObserver("/iq", self.gotLabel, label=label)
 
57
            
 
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)
 
62
        except StopIteration:
 
63
            self.labels_iter = iter(self.labels)
 
64
            self.xmlstream.addObserver("/iq", self.gotNewMail)
 
65
            self.cb_count(self.count)
 
66
    
 
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'])
 
71
            
 
72
            if label == "inbox":
 
73
                if mailbox.firstChildElement():
 
74
                    self.last_tid = mailbox.firstChildElement().attributes['tid']
 
75
            
 
76
            self.queryLabel()
 
77
        else:
 
78
            DEBUG("ERROR: received unexpected iq after querying for INBOX")
 
79
    
 
80
    def gotNewMail(self, iq):
 
81
        if iq.firstChildElement() and iq.firstChildElement().name == "new-mail":
 
82
            self.xmlstream.removeObserver("/iq", self.gotNewMail)
 
83
            
 
84
            # Acknowledge iq
 
85
            iq = domish.Element((None, "iq"), attribs={"type": "result", "id": iq.attributes['id']})
 
86
            self.xmlstream.send(iq)
 
87
            
 
88
            # Get the new mail
 
89
            self.xmlstream.addOnetimeObserver("/iq", self.gotNewMailQueryResult)
 
90
            
 
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)
 
95
        else:
 
96
            DEBUG("ERROR: This was no new mail iq")
 
97
    
 
98
    def gotNewMailQueryResult(self, iq):
 
99
        if iq.firstChildElement() and iq.firstChildElement().name == "mailbox":
 
100
            mailbox = iq.children[0]
 
101
            threads = mailbox.children
 
102
            if threads:
 
103
                newest = threads[0]
 
104
                self.newest_tid = unicode(newest.attributes['tid'])
 
105
                
 
106
                mails = []
 
107
                
 
108
                for thread in threads:
 
109
                    mail = {}
 
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)
 
123
                    mails.append(mail)
 
124
                
 
125
                self.cb_new(mails)
 
126
        
 
127
        self.queryInbox()
 
128
    
 
129
    def rawDataIn(self, buf):
 
130
        print u"< %s" % unicode(buf, "utf-8")
 
131
    
 
132
    def rawDataOut(self, buf):
 
133
        print u"> %s" % unicode(buf, "utf-8")
 
134
    
 
135
    def connectedCB(self, xmlstream):
 
136
        self.xmlstream = xmlstream
 
137
        
 
138
        xmlstream.rawDataInFn = self.rawDataIn
 
139
        xmlstream.rawDataOutFn = self.rawDataOut
 
 
b'\\ No newline at end of file'