summaryrefslogtreecommitdiff
path: root/sunshine/lqsoft/pygadu/network_base.py
blob: 392d42f71e646247553d15288e99f66f3b6b32a9 (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
# -*- coding: utf-8
__author__="lreqc"
__date__ ="$2009-07-14 01:07:32$"

from sunshine.lqsoft.pygadu.packets import Resolver, inpacket, outpacket

from sunshine.lqsoft.cstruct.common import CStruct
from sunshine.lqsoft.cstruct.fields import complex, numeric

from sunshine.lqsoft.utils import Enum

PACKET_HEADER_LENGTH = 8

class GaduPacketHeader(CStruct):
    """Struktura opisująca nagłówek pakietu w GG"""
    msg_type        = numeric.UIntField(0)
    msg_length      = numeric.UIntField(1)

    def __str__(self):
        return '[GGHDR: type=%d, length %d]' % (self.msg_type, self.msg_length)

class GaduPacket(CStruct):
    """Wspólna nadklasa dla wszystkich wiadomości w GG"""
    def as_packet(self):
        data = self.pack()
        hdr = GaduPacketHeader(msg_type=self.packet_id, msg_length=len(data))
        return hdr.pack() + data

    def __str__(self):
        return self.__class__.__name__

#
# INCOMING PACKETS
#
class WelcomePacket(GaduPacket):
    seed = numeric.IntField(0)    
WelcomePacket = inpacket(0x01)(WelcomePacket)

class MessageAckPacket(GaduPacket): #SendMsgAck
    MSG_STATUS = Enum({
        'BLOCKED': 0x0001, 'DELIVERED': 0x0002,
        'QUEUED': 0x0003, 'MBOXFULL': 0x0004,
        'NOT_DELIVERED': 0x0006
    })

    msg_status  = numeric.IntField(0)
    recipient   = numeric.IntField(1)
    seq         = numeric.IntField(2)
MessageAckPacket = inpacket(0x05)(MessageAckPacket)

class LoginFailedPacket(GaduPacket):
    pass
LoginFailedPacket = inpacket(0x09)(LoginFailedPacket)

class DisconnectPacket(GaduPacket):
    pass
DisconnectPacket = inpacket(0x0b)(DisconnectPacket)

class NeedEmailPacket(GaduPacket):
    pass
NeedEmailPacket = inpacket(0x14)(NeedEmailPacket)

class UnavailbleAckPacket(GaduPacket):
    pass
UnavailbleAckPacket = inpacket(0x0d)(UnavailbleAckPacket)

class PongPacket(GaduPacket):
    pass
PongPacket = inpacket(0x07)(PongPacket)

#
# OUTGOING PACKETS
#
class StructNotice(CStruct): # Notify
    TYPE = Enum({
        'BUDDY':    0x01,
        'FRIEND':   0x02,
        'IGNORE':  0x04
    })
    
    uin             = numeric.UIntField(0)
    type            = numeric.UByteField(1, default=0x03)

    def __str__(self):
        return "%d[%d]" (self.uin, self.type)

class NoticeFirstPacket(GaduPacket): #NotifyFirst
    contacts        = complex.ArrayField(0, complex.StructField(0, struct=StructNotice), length=-1)
NoticeFirstPacket = outpacket(0x0f)(NoticeFirstPacket)

class NoticeLastPacket(GaduPacket): #NotifyLast
    contacts        = complex.ArrayField(0, complex.StructField(0, struct=StructNotice), length=-1)
NoticeLastPacket = outpacket(0x10)(NoticeLastPacket)

class NoNoticesPacket(GaduPacket):
    pass
NoNoticesPacket = outpacket(0x12)(NoNoticesPacket)

class AddNoticePacket(GaduPacket):
    contact        = complex.StructField(0, struct=StructNotice)
AddNoticePacket = outpacket(0x0d)(AddNoticePacket)

class RemoveNoticePacket(GaduPacket):
    contact        = complex.StructField(0, struct=StructNotice)
RemoveNoticePacket = outpacket(0x0e)(RemoveNoticePacket)

class PingPacket(GaduPacket):
    pass
PingPacket = outpacket(0x08)(PingPacket)