summaryrefslogtreecommitdiff
path: root/sunshine/lqsoft/pygadu/models.py
blob: 6c70631953f75ba706d49e1ba881d1a352454f9a (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding: utf-8
__author__="lreqc"
__date__ ="$2009-07-14 07:33:27$"

from sunshine.lqsoft.pygadu.network_base import StructNotice
import xml.etree.ElementTree as ET
import hashlib
import zlib

class GaduProfile(object):

    def __init__(self, uin):
        self.uin = uin
        self.__status = None
        self.__hashelem = None
        self.__contacts = {}
        self.__groups = {}
        self.__connection = None
        self.contactsLoop = None
        self.exportLoop = None
        
    def __set_password(self, value):
        self.__hashelem = hashlib.new('sha1')
        self.__hashelem.update(value)

    def __get_hashelem(self):
        return self.__hashelem.copy()

    def __set_status(self, value):
        self.__status = value

    def __get_status(self):
        return self.__status

    password = property(__get_hashelem, __set_password)
    status = property(__get_status, __set_status)

    def _updateContact(self, notify):
        # notify is of class GGStruct_Status80
        if notify.uin != self.uin:
            if self.__contacts.has_key(notify.uin):
                contact = self.__contacts[notify.uin]
            else:
                contact_xml = ET.Element("Contact")
                ET.SubElement(contact_xml, "Guid").text = notify.uin
                ET.SubElement(contact_xml, "GGNumber").text = notify.uin
                ET.SubElement(contact_xml, "ShowName").text = "Unknown User"
                ET.SubElement(contact_xml, "Groups")
                c = GaduContact.from_xml(contact_xml)
                self.addContact( c )
                #contact = GaduContact.simple_make(self, notify.uin, "Unknown User")

            contact.status =  notify.status
            contact.description = notify.description
            self.onContactStatusChange(contact)

    def _creditials(self, result, *args, **kwargs):
        """Called by protocol, to get creditials, result will be passed to login
            procedure. It should be a 2-tuple with (uin, hash_elem)"""
        return self.onCreditialsNeeded()

    def _loginSuccess(self, conn, *args, **kwargs):
        self.__connection = conn
        self.onLoginSuccess()
        return self

    # high-level interface
    @property
    def connected(self):
        """Is the profile currently used in an active connection"""
        return self.__connection is not None

    def disconnect(self):
        self.__connection.loseConnection()

    def addContact(self, contact):
        if self.__contacts.has_key(contact.uin):
            raise ValueError("Contact with UIN %d already exists." % contact.uin)
        
        self.__contacts[contact.uin] = contact
        if self.connected:
            self.setNotifyState(contact.uin, contact.notify_flags)

    def removeContact(self, contact, notify=False):
        if self.__contacts.has_key(contact.uin):
            if self.connected:
                del self.__contacts[contact.uin]
                
                if notify == True:
                    self.__connection.delContact(contact)

    def notifyAboutContact(self, contact):
        """Notify GG server when new GG contact is added to the contacts list."""
        self.__connection.addNewContact(contact)

    def addGroup(self, group):
        if self.__groups.has_key(group.Id):
            raise ValueError("Group %d already exists." % group.Id)
        self.__groups[group.Id] = group

    # stuff that user can use
    def setNotifyState(self, uin, new_state):
        pass

    def sendTextMessage(self, text):
        pass

    def setMyState(self, new_state, new_description=''):
        if not self.connected:
            raise RuntimeError("You need to be connected, to import contact list from the server.")

        self.__connection.changeStatus(new_state, new_description)

    def sendTo(self, uin, html_message, plain_message):
        if not self.connected:
            raise RuntimeError("You need to be connected, to send messages.")
        self.__connection.sendHTMLMessage(uin, html_message + '\0', plain_message + '\0')

    def sendToConf(self, uin, html_message, plain_message, recipients):
        if not self.connected:
            raise RuntimeError("You need to be connected, to send messages.")
        self.__connection.sendConfMessage(uin, html_message + '\0', plain_message + '\0', recipients)

    def importContacts(self, callback):
        """Issue an import request. This is non-blocking and returns no data."""
        if not self.connected:
            raise RuntimeError("You need to be connected, to import contact list from the server.")

        def parse_xml(data):
            #print zlib.decompress(data)
            book = ET.fromstring(zlib.decompress(data))
            self._flushContacts()
            
            for elem in book.find('Groups').getchildren():
                self.addGroup( GaduContactGroup.from_xml(elem) )

            for elem in book.find('Contacts').getchildren():
                is_uin_ok = 1
                try:
                    check_uin = elem.find("GGNumber")
                    int(check_uin.text)
                    if check_uin.text == '':
                        is_uin_ok = 0
                except:
                    is_uin_ok = 0
                    
                if is_uin_ok == 1:
                    contact = GaduContact.from_xml(elem)
                    self.addContact( contact )
                    self.__connection.addNewContact(contact)
                else:
                    print 'Failed to import contact. Invalid uin: %s.' % check_uin.text

            callback()

        self.__connection.sendImportRequest(parse_xml)

    def exportContacts(self, xml):
        if not self.connected:
            raise RuntimeError("You need to be connected, to export contacts.")
        data = zlib.compress(xml)
        self.__connection.exportContactsList(data)

    def _flushContacts(self):
        self.__contacts = {}
        self.__groups = {}

    def disconnect(self):
        self.__connection.transport.loseConnection()

    # stuff that should be implemented by user
    def onCreditialsNeeded(self, *args, **kwargs):
        return (self.uin, self.__hashelem, self.__status)

    def onLoginSuccess(self):
        """Called when login is completed."""
        pass

    def onLoginFailure(self, reason):
        """Called after an unsuccessful login."""
        pass

    def onContactStatusChange(self, contact):
        """Called when a status of a contact has changed."""
        pass

    def onMessageReceived(self, message):
        """Called when a message had been received"""
        pass

    def onStatusNoticiesRecv(self):
        """Called when a contact list notify was sent"""
        pass

    def onXml(self, data):
        """Called when a XML action/event packet was sent"""
        pass

    def isContactExist(self, uin):
        return self.__contacts.has_key(uin)

    def get_contact(self, uin):
        if self.__contacts.has_key(uin):
            return self.__contacts[uin]
        else:
            return None

    @property
    def contacts(self):
        return self.__contacts.itervalues()

    @property
    def groups(self):
        return self.__groups.itervalues()

class Def(object):
    def __init__(self, type, default_value, required=False, exportable=True, init=lambda x: x):
        self.type = type
        self.default = default_value
        self.required = required
        self.exportable = exportable
        self._init = init

    def init(self, value):
        return self.type( self._init(value) )
        
def mkdef(*args):
    return Def(*args)


class FlatXMLObject(object):

    def __init__(self, **kwargs):
        for (k, v) in self.SCHEMA.iteritems():
            if v.required and not kwargs.has_key(k):
                raise ValueError("You must supply a %s field." % k)

            setattr(self, k, kwargs.get(k, v.default))
            if not isinstance(getattr(self, k), v.type):
                raise ValueError("Field %s has to be of class %s." % (k, v.type.__name__))

    @classmethod
    def from_xml(cls, element):
        dict = {}

        for (k, v) in cls.SCHEMA.iteritems():
            elem = element.find("./"+k)
            if not v.exportable:
                continue
                
            if v.required and elem is None:
                raise ValueError("Invalid element - need child element %s to unpack." % k)

            dict[k] = v.type(elem.text if elem is not None and elem.text else v.default)
            if k == 'Groups':
                dict[k] = v.type(ET.tostring(elem) if elem is not None else v.default)
        return cls(**dict)          

class GaduContactGroup(FlatXMLObject):
    SCHEMA = {
        "Id":           mkdef(str, '', True),
        "Name":         mkdef(str, '', True),
        "IsExpanded":   mkdef(bool, True),
        "IsRemovable":  mkdef(bool, True),
    }
    

class GaduContact(FlatXMLObject):
    """Single contact as seen in catalog (person we are watching) - conforming to GG8.0"""
    SCHEMA = {
        'Guid':             mkdef(str, '', True),
        'GGNumber':         mkdef(str, '', True),
        'ShowName':         mkdef(str, '', True),
        'MobilePhone':      mkdef(str, ''),
        'HomePhone':        mkdef(str, ''),
        'Email':            mkdef(str, 'someone@somewhere.moc'),
        'WWWAddress':       mkdef(str, ''),
        'FirstName':        mkdef(str, ''),
        'LastName':         mkdef(str, ''),
        'Gender':           mkdef(int, 0),
        'Birth':            mkdef(str, ''),
        'City':             mkdef(str, ''),
        'Province':         mkdef(str, ''),
        'Groups':           mkdef(str, ''),
        'CurrentAvatar':    mkdef(int, 0),
        # 'Avatars':          mkdef(list, []),
        'UserActivatedInMG':mkdef(bool, False),
        'FlagBuddy':        mkdef(bool, False),
        'FlagNormal':       mkdef(bool, False),
        'FlagFriend':       mkdef(bool, False),
        'FlagIgnored':      mkdef(bool, False),
        # state variables
        'description':      mkdef(str, '', False, False),
        'status':           mkdef(int, 0, False, False),
    }

    @classmethod
    def simple_make(cls, profile, uin, name):
        return cls(profile, Guid=str(uin), GGNumber=str(uin), ShowName=name)

    def __str__(self):
        return "[%s,%d: %s]" % (self.GGNumber, self.status, self.description)

    @property
    def uin(self):
        return int(self.GGNumber)

    @property
    def notify_flags(self):
        return int(self.FlagBuddy and StructNotice.TYPE.BUDDY) \
            & int(self.FlagFriend and StructNotice.TYPE.FRIEND) \
            & int(self.FlagIgnored and StructNotice.TYPE.IGNORE)

    def updateStatus(self, status, desc=None):
        self.status = status
        if desc: self.description = desc

    def updateName(self, name):
        self.ShowName = name

    def updateGroups(self, groups):
        self.Groups = groups

    def get_desc(self):
        #print 'Tak to get_desc, desctiption.text zwraca: %s a samo description: %s' % (self.description.text, self.description)
        if self.description.text:
            return self.description.text
        else:
            return ''

#     @classmethod
#     def from_request_string(cls, rqs):
#         dict = {}
#         for (fmt, value) in zip(cls.RQ_STRING_FORMAT, rqs.split(';')):
#             dict[fmt[0]] = fmt[1](value)
#         return cls(**dict)
# 
#     def request_string(self):
#         return ";".join( [ str(self.__getattribute__(fmt[0])) for fmt in self.RQ_STRING_FORMAT] )