Skip to content

Commit

Permalink
🏇 Fully refactored first version of this package
Browse files Browse the repository at this point in the history
  • Loading branch information
quenbyako committed Nov 28, 2020
1 parent 91bc54e commit 6582c6e
Show file tree
Hide file tree
Showing 74 changed files with 21,849 additions and 1,244 deletions.
5 changes: 5 additions & 0 deletions common_methods.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright (c) 2020 KHS Films
//
// This file is a part of mtproto package.
// See https://github.com/xelaj/mtproto/blob/master/LICENSE for details

package mtproto

// методы (ну или функции, you name it), которые взяты отсюда https://core.telegram.org/schema/mtproto
Expand Down
9 changes: 9 additions & 0 deletions encoding/tl/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,12 @@ func (i *Int256) UnmarshalTL(d *Decoder) error {
i.Int = big.NewInt(0).SetBytes(val)
return nil
}

// PseudoBool is a support struct which is required to get native
type PseudoBool struct {
value bool
}

func (*PseudoBool) CRC() uint32 {
panic("it's pseudo bool type")
}
5 changes: 2 additions & 3 deletions encoding/tl/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func DecodeUnknownObject(data []byte) (Object, error) {
}

return obj, nil

}

func (d *Decoder) decodeObject(o Object, ignoreCRC bool) {
Expand Down Expand Up @@ -115,6 +114,7 @@ func (d *Decoder) decodeObject(o Object, ignoreCRC bool) {
d.decodeValue(field)
if d.err != nil {
d.err = errors.Wrapf(d.err, "decode field '%s'", vtyp.Field(i).Name)
break
}
}
}
Expand All @@ -123,13 +123,12 @@ func (d *Decoder) decodeValue(value reflect.Value) {
if d.err != nil {
return
}

if m, ok := value.Interface().(Unmarshaler); ok {
err := m.UnmarshalTL(d)
if err != nil {
d.err = err
return
}
return
}

val := d.decodeValueGeneral(value)
Expand Down
4 changes: 2 additions & 2 deletions encoding/tl/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"testing"

"github.com/k0kubun/pp"
"github.com/xelaj/go-dry"

"github.com/stretchr/testify/assert"
"github.com/xelaj/go-dry"

"github.com/xelaj/mtproto/encoding/tl"
)

Expand Down
2 changes: 1 addition & 1 deletion encoding/tl/encoder_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func BenchmarkEncoder(b *testing.B) {
Dark: true,
Format: "abc",
Theme: &telegram.InputThemeObj{
Id: 123,
ID: 123,
AccessHash: 321,
},
})
Expand Down
2 changes: 1 addition & 1 deletion encoding/tl/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/xelaj/mtproto/encoding/tl"
)

Expand Down Expand Up @@ -95,4 +96,3 @@ func TestEncode(t *testing.T) {
})
}
}

1 change: 1 addition & 0 deletions encoding/tl/equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/xelaj/mtproto/encoding/tl"
)

Expand Down
1 change: 0 additions & 1 deletion encoding/tl/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,3 @@ type ResPQ struct {
func (*ResPQ) CRC() uint32 {
return 0x05162463
}

1 change: 1 addition & 0 deletions encoding/tl/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/xelaj/go-dry"

"github.com/xelaj/mtproto/encoding/tl"
)

Expand Down
3 changes: 2 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/pkg/errors"
"github.com/xelaj/go-dry"

"github.com/xelaj/mtproto/internal/mtproto/objects"
)

Expand Down Expand Up @@ -81,7 +82,7 @@ func TryExpandError(errStr string) (nativeErrorName string, additionalData inter
nativeErrorName = choosedPrefixSuffix.prefix + "X" + choosedPrefixSuffix.suffix
trimmedData := strings.TrimSuffix(strings.TrimPrefix(errStr, choosedPrefixSuffix.prefix), choosedPrefixSuffix.suffix)

switch v := choosedPrefixSuffix.kind; v {
switch v := choosedPrefixSuffix.kind; v { //nolint:exhaustive others will panic
case reflect.Int:
var err error
additionalData, err = strconv.Atoi(trimmedData)
Expand Down
7 changes: 5 additions & 2 deletions errors_internal.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright (c) 2020 KHS Films
//
// This file is a part of mtproto package.
// See https://github.com/xelaj/mtproto/blob/master/LICENSE for details

package mtproto

// internal errors for internal purposes
Expand All @@ -11,5 +16,3 @@ func (*errorSessionConfigsChanged) Error() string {
func (*errorSessionConfigsChanged) CRC() uint32 {
panic("makes no sense")
}


22 changes: 10 additions & 12 deletions examples/auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ func main() {
// edit these params for you!
client, err := telegram.NewClient(telegram.ClientConfig{
// where to store session configuration. must be set
SessionFile: "/home/me/.local/var/lib/mtproto/session1.json",
SessionFile: "/home/r0ck3t/.local/var/lib/mtproto/session1.json",
// host address of mtproto server. Actually, it can'be mtproxy, not only official
ServerHost: "149.154.167.50:443",
// public keys file is patrh to file with public keys, which you must get from https://my.telelgram.org
PublicKeysFile: "/home/me/.local/var/lib/mtproto/tg_public_keys.pem",
PublicKeysFile: "/home/r0ck3t/.local/var/lib/mtproto/tg_public_keys.pem",
AppID: 94575, // app id, could be find at https://my.telegram.org
AppHash: "a3406de8d171bb422bb6ddf3bbd800e2", // app hash, could be find at https://my.telegram.org
})
dry.PanicIfErr(err)

setCode, err := client.AuthSendCode(&telegram.AuthSendCodeParams{
setCode, err := client.AuthSendCode(
phoneNumber, 94575, "a3406de8d171bb422bb6ddf3bbd800e2", &telegram.CodeSettings{},
})
)
dry.PanicIfErr(err)
pp.Println(setCode)

fmt.Print("Auth code:")
code, _ := bufio.NewReader(os.Stdin).ReadString('\n')
code = strings.ReplaceAll(code, "\n", "")

auth, err := client.AuthSignIn(&telegram.AuthSignInParams{
PhoneNumber: phoneNumber,
PhoneCodeHash: setCode.PhoneCodeHash,
PhoneCode: code,
})
auth, err := client.AuthSignIn(
phoneNumber,
setCode.PhoneCodeHash,
code,
)
if err == nil {
pp.Println(auth)

Expand Down Expand Up @@ -80,9 +80,7 @@ func main() {
inputCheck, err := telegram.GetInputCheckPassword(password, accountPassword)
dry.PanicIfErr(err)

auth, err = client.AuthCheckPassword(&telegram.AuthCheckPasswordParams{
Password: inputCheck,
})
auth, err = client.AuthCheckPassword(inputCheck)
dry.PanicIfErr(err)

pp.Println(auth)
Expand Down
25 changes: 10 additions & 15 deletions examples/get_channel_users/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,43 @@ func main() {
peer, err := client.GetChatInfoByHashLink(hash)
dry.PanicIfErr(err)

total, err := client.GetPossibleAllParticipantsOfGroup(telegram.InputChannel(&telegram.InputChannelObj{
ChannelId: peer.(*telegram.Channel).Id,
total, err := client.GetPossibleAllParticipantsOfGroup(&telegram.InputChannelObj{
ChannelID: peer.(*telegram.Channel).ID,
AccessHash: peer.(*telegram.Channel).AccessHash,
}))
})

dry.PanicIfErr(err)
pp.Println(total, len(total))

println("this is partial users in CHANNEL. In supergroup you can use more easy way to find, see below")

resolved, err := client.ContactsResolveUsername(&telegram.ContactsResolveUsernameParams{"gogolang"})
resolved, err := client.ContactsResolveUsername("gogolang")
dry.PanicIfErr(err)

channel := resolved.Chats[0].(*telegram.Channel)
inCh := telegram.InputChannel(&telegram.InputChannelObj{
ChannelId: channel.Id,
ChannelID: channel.ID,
AccessHash: channel.AccessHash,
})

res := make(map[int]struct{})
totalCount := 100 // at least 100
offset := 0
for offset < totalCount {
resp, err := client.ChannelsGetParticipants(&telegram.ChannelsGetParticipantsParams{
Channel: inCh,
Filter: telegram.ChannelParticipantsFilter(&telegram.ChannelParticipantsRecent{}),
Limit: 100,
Offset: int32(offset),
})
resp, err := client.ChannelsGetParticipants(inCh, telegram.ChannelParticipantsFilter(&telegram.ChannelParticipantsRecent{}), 100, int32(offset), 0)
dry.PanicIfErr(err)
data := resp.(*telegram.ChannelsChannelParticipantsObj)
totalCount = int(data.Count)
for _, participant := range data.Participants {
switch user := participant.(type) {
case *telegram.ChannelParticipantSelf:
res[int(user.UserId)] = struct{}{}
res[int(user.UserID)] = struct{}{}
case *telegram.ChannelParticipantObj:
res[int(user.UserId)] = struct{}{}
res[int(user.UserID)] = struct{}{}
case *telegram.ChannelParticipantAdmin:
res[int(user.UserId)] = struct{}{}
res[int(user.UserID)] = struct{}{}
case *telegram.ChannelParticipantCreator:
res[int(user.UserId)] = struct{}{}
res[int(user.UserID)] = struct{}{}
default:
pp.Println(user)
panic("что?")
Expand Down
2 changes: 1 addition & 1 deletion examples/get_chats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func main() {
})
dry.PanicIfErr(err)

pp.Println(client.MessagesGetAllChats(&telegram.MessagesGetAllChatsParams{ExceptIds:[]int32{}}))
pp.Println(client.MessagesGetAllChats([]int32{}))
}
17 changes: 5 additions & 12 deletions examples/get_saved_contacts/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"reflect"

"github.com/k0kubun/pp"

"github.com/xelaj/go-dry"
Expand All @@ -29,21 +27,16 @@ func main() {
})
dry.PanicIfErr(err)

res, err := client.MakeRequestAsSlice( // this is crutch of decoder, cause SOME methods
// responses vector. BUT, vector is slice, not native object.
// You need to use MakeRequestAsSlice only when you are invoking
// method in another. If you using client.ContactsGetSavedParams,
// you just need to call it, generated implementation is already
// doing stuff for you.
res, err := client.MakeRequest(
&telegram.InvokeWithTakeoutParams{
TakeoutID: resp.Id,
Query: &telegram.ContactsGetSavedParams{}},
reflect.TypeOf(&telegram.SavedContact{}),
TakeoutID: resp.ID,
Query: &telegram.ContactsGetSavedParams{},
},
)

pp.Sprintln(res, err)

contacts, err := client.ContactsGetContacts(&telegram.ContactsGetContactsParams{})
contacts, err := client.ContactsGetContacts(0)
dry.PanicIfErr(err)
c := contacts.(*telegram.ContactsContactsObj)
pp.Println(int(c.SavedCount), len(c.Users), len(c.Contacts))
Expand Down
2 changes: 1 addition & 1 deletion examples/get_self/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func main() {
})
dry.PanicIfErr(err)

pp.Println(client.ContactsResolveUsername(&telegram.ContactsResolveUsernameParams{Username: "username"}))
pp.Println(client.ContactsResolveUsername("username"))
}
6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ module github.com/xelaj/mtproto
go 1.13

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/structtag v1.2.0
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator v9.31.0+incompatible
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
github.com/tj/assert v0.0.3
github.com/vikyd/zero v0.0.0-20190921142904-0f738d0bc858
github.com/xelaj/errs v0.0.0-20200831133608-d1c11863e019
github.com/xelaj/go-dry v0.0.0-20201104165138-61a25872c05a
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
16 changes: 0 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
Expand All @@ -25,13 +17,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
github.com/vikyd/zero v0.0.0-20190921142904-0f738d0bc858 h1:b6CjC51KJa0bGZfvkxfjSgEKLhK016I++4bxhhh+lVE=
github.com/vikyd/zero v0.0.0-20190921142904-0f738d0bc858/go.mod h1:AuUZRM/kTNOOSu3nAzKoDmUERB8S8JlwTkL1snMDzEs=
github.com/xelaj/errs v0.0.0-20200831133608-d1c11863e019 h1:3BHWVEmYnGFozw/ApnuOgqE0tvPmYIsuO58YogGjb0s=
github.com/xelaj/errs v0.0.0-20200831133608-d1c11863e019/go.mod h1:ZRqZExT3DbVJwy4R3q2HEXCyMwGs/lUO52dA0uorbrg=
github.com/xelaj/go-dry v0.0.0-20201104165138-61a25872c05a h1:xb1eHQRUPQyg8Du+uvVKHBG4A1eauDTYDexTZ5iaIZc=
Expand All @@ -47,13 +36,8 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201029080932-201ba4db2418 h1:HlFl4V6pEMziuLXyRkm5BIYq1y1GAbb02pRlWvI54OM=
golang.org/x/sys v0.0.0-20201029080932-201ba4db2418/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
Expand Down
5 changes: 5 additions & 0 deletions handshake.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Copyright (c) 2020 KHS Films
//
// This file is a part of mtproto package.
// See https://github.com/xelaj/mtproto/blob/master/LICENSE for details

package mtproto

import (
Expand Down
Loading

0 comments on commit 6582c6e

Please sign in to comment.