-
Notifications
You must be signed in to change notification settings - Fork 12
/
functions.go
165 lines (153 loc) · 4.14 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
package mtproto
import (
"crypto/sha256"
"fmt"
"log"
"strings"
"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_SignIn2FA(phonenumber string, hash, code string, password string) (TL_auth_authorization, error) {
var userSelf TL_user
var authAuthorization TL_auth_authorization
respAuthSignIn := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_auth_signIn{phonenumber, hash, code},
respAuthSignIn,
}
authSignIn := <-respAuthSignIn
authAuthorization, ok := authSignIn.(TL_auth_authorization)
if ok {
userSelf = authAuthorization.User.(TL_user)
} else {
err, ok := authSignIn.(TL_rpc_error)
if !ok || strings.Compare(err.error_message, "SESSION_PASSWORD_NEEDED") != 0 {
return TL_auth_authorization{}, fmt.Errorf("RPC: %#v", authSignIn)
}
respGetPassword := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_account_getPassword{},
respGetPassword,
}
getPassword := <-respGetPassword
accountPassword, ok := getPassword.(TL_account_password)
if !ok {
return TL_auth_authorization{}, fmt.Errorf("RPC: %#v", getPassword)
}
hash := sha256.New()
hash.Write(accountPassword.Current_salt)
hash.Write([]byte(password))
hash.Write(accountPassword.Current_salt)
respCheckPassword := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_auth_checkPassword{hash.Sum(nil)},
respCheckPassword,
}
checkPassword := <-respCheckPassword
authAuthorization, ok := checkPassword.(TL_auth_authorization)
if !ok {
return TL_auth_authorization{}, fmt.Errorf("RPC: %#v", checkPassword)
}
userSelf = authAuthorization.User.(TL_user)
}
fmt.Printf("Signed in: id %d name <%s %s>\n", userSelf.Id, userSelf.First_name, userSelf.Last_name)
return authAuthorization, nil
}
func (m *MTProto) Auth_CheckPhone(phonenumber string) bool {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
TL_auth_checkPhone{
phonenumber,
},
resp,
}
x := <-resp
if v, ok := x.(TL_auth_checkedPhone); ok {
if toBool(v) {
return true
}
}
return false
}
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
}