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
|
# File 'lib/mtproto/type/rpc/auth/authorization.rb', line 14
def self.parse(response)
constructor = response[0, 4].unpack1('L<')
if constructor == Type::GzipPacked::CONSTRUCTOR
response = Type::GzipPacked.unpack(response)
constructor = response[0, 4].unpack1('L<')
end
if constructor == Type::RpcError::CONSTRUCTOR
error = Type::RpcError.deserialize(response)
raise MTProto::RpcError.new(error.error_code, error.error_message)
end
if constructor == CONSTRUCTOR_SIGN_UP_REQUIRED
return { authorization: nil, sign_up_required: true }
end
if constructor == CONSTRUCTOR
offset = 4
flags = response[offset, 4].unpack1('L<')
offset += 4
if (flags & (1 << 0)) != 0
offset += 4
end
if (flags & (1 << 1)) != 0
offset += 4
end
if (flags & (1 << 2)) != 0
first_byte = response[offset].unpack1('C')
if first_byte < 254
token_len = first_byte
offset += 1 + token_len
padding = (4 - ((1 + token_len) % 4)) % 4
offset += padding
else
offset += 1
token_len = response[offset, 3].unpack('CCC').inject(0) {|sum, b| (sum << 8) + b}
offset += 3 + token_len
padding = (4 - ((4 + token_len) % 4)) % 4
offset += padding
end
end
user_constructor = response[offset, 4].unpack1('L<')
offset += 4
user_flags = response[offset, 4].unpack1('L<')
offset += 4
user_flags2 = response[offset, 4].unpack1('L<')
offset += 4
user_id = response[offset, 8].unpack1('Q<')
offset += 8
access_hash = nil
if (user_flags & (1 << 0)) != 0
access_hash = response[offset, 8].unpack1('Q<')
offset += 8
end
return {
authorization: true,
flags: flags,
sign_up_required: false,
user_id: user_id,
access_hash: access_hash
}
end
raise UnexpectedConstructorError.new(constructor)
end
|