forked from moorage/mtproto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.go
169 lines (145 loc) · 3.5 KB
/
functions.go
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
package mtproto
import (
"fmt"
"github.com/pkg/errors"
)
func (m *MTProto) Auth_SendCode(phonenumber string) (string, error) {
var authSentCode TL_auth_sentCode
flag := true
for flag {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{TL_auth_sendCode{
Flags: 1,
Current_number: TL_boolTrue{},
Phone_number: phonenumber,
Api_id: int32(m.appId),
Api_hash: m.appHash,
}, resp}
x := <-resp
switch x.(type) {
case TL_auth_sentCode:
authSentCode = x.(TL_auth_sentCode)
flag = false
case TL_rpc_error:
x := x.(TL_rpc_error)
if x.error_code != 303 {
return "", fmt.Errorf("RPC error: %v", x)
}
var newDc int32
n, _ := fmt.Sscanf(x.error_message, "PHONE_MIGRATE_%d", &newDc)
if n != 1 {
n, _ := fmt.Sscanf(x.error_message, "NETWORK_MIGRATE_%d", &newDc)
if n != 1 {
return "", fmt.Errorf("RPC error_string: %s", x.error_message)
}
}
newDcAddr, ok := m.dclist[newDc]
if !ok {
return "", fmt.Errorf("Wrong DC index: %d", newDc)
}
err := m.Reconnect(newDcAddr)
// fmt.Println("Reconnected")
if err != nil {
return "", err
}
default:
return "", fmt.Errorf("Got: %T", x)
}
}
if authSentCode.Flags&1 == 0 {
return "", errors.New("Cannot sign up yet")
}
return authSentCode.Phone_code_hash, nil
}
func (m *MTProto) Auth_SignIn(phonenumber string, hash, code string) (TL_auth_authorization, error) {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_auth_signIn{phonenumber, hash, code},
resp,
}
x := <-resp
auth, ok := x.(TL_auth_authorization)
if !ok {
return TL_auth_authorization{}, fmt.Errorf("RPC: %#v", x)
}
userSelf := auth.User.(TL_user)
fmt.Printf("Signed in: id %d name <%s %s>\n", userSelf.Id, userSelf.First_name, userSelf.Last_name)
return auth, nil
}
func (m *MTProto) Auth_CheckPhone(phonenumber string) bool {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_auth_checkPhone{
"989121228718",
},
resp,
}
x := <-resp
if v, ok := x.(TL_auth_checkedPhone); ok {
if toBool(v) {
return true
}
}
return false
}
func (m* MTProto) Auth_Export(dcId int32) (int32, []byte, error) {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_auth_exportAuthorization{
Dc_id: dcId,
},
resp,
}
x := <-resp
switch x.(type) {
case TL_rpc_error:
x := x.(TL_rpc_error)
return -1, nil, fmt.Errorf("Auth_Export: RPC error: %v", x)
case TL_auth_exportedAuthorization:
res := x.(TL_auth_exportedAuthorization)
return res.Id, res.Bytes, nil
default:
return -1, nil, fmt.Errorf("Auth_Export. Got: %T", x)
}
}
func (m* MTProto) Auth_Import(userId int32, bytes []byte) error {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_auth_importAuthorization{
Id: userId,
Bytes: bytes,
},
resp,
}
x := <-resp
switch x.(type) {
case TL_rpc_error:
x := x.(TL_rpc_error)
return fmt.Errorf("Auth_Import: RPC error: %v", x)
case TL_auth_authorization:
// res := x.(TL_auth_authorization)
return nil
default:
return fmt.Errorf("Auth_Import. Got: %T", x)
}
}
func (m *MTProto) Users_GetFullSelf() (User, error) {
return m.users_getFullUsers(TL_inputUserSelf{})
}
func (m *MTProto) users_getFullUsers(id TL) (User, error) {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_users_getFullUser{
Id: id,
},
resp,
}
x := <-resp
user, ok := x.(TL_userFull)
if !ok {
// log.Println(fmt.Sprintf("RPC: %#v", x))
return User{}, fmt.Errorf("RPC: %#v", x)
}
newUser := NewUser(user.User)
return *newUser, nil
}