2019-11-03 22:15:43 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2023-07-22 14:46:35 +00:00
|
|
|
"hash/maphash"
|
2019-11-03 22:15:43 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2019-11-29 00:51:41 +00:00
|
|
|
"sync"
|
2019-12-16 01:02:53 +00:00
|
|
|
"time"
|
2019-11-03 22:15:43 +00:00
|
|
|
|
|
|
|
"dev.narayana.im/narayana/telegabber/config"
|
2019-11-12 15:50:25 +00:00
|
|
|
"dev.narayana.im/narayana/telegabber/persistence"
|
2019-12-28 02:35:40 +00:00
|
|
|
"dev.narayana.im/narayana/telegabber/telegram/cache"
|
2019-11-03 22:15:43 +00:00
|
|
|
|
2022-01-17 20:45:40 +00:00
|
|
|
"github.com/zelenin/go-tdlib/client"
|
2019-11-24 17:10:29 +00:00
|
|
|
"gosrc.io/xmpp"
|
2019-11-03 22:15:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var logConstants = map[string]int32{
|
2019-11-05 00:09:07 +00:00
|
|
|
":fatal": 0,
|
|
|
|
":error": 1,
|
|
|
|
":warn": 2,
|
|
|
|
":info": 3,
|
|
|
|
":debug": 4,
|
|
|
|
":verbose": 5,
|
|
|
|
":all": 1023,
|
2019-11-03 22:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stringToLogConstant(c string) int32 {
|
|
|
|
level, ok := logConstants[c]
|
|
|
|
if !ok {
|
|
|
|
level = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return level
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:31:05 +00:00
|
|
|
// DelayedStatus describes an online status expiring on timeout
|
|
|
|
type DelayedStatus struct {
|
|
|
|
TimestampOnline int64
|
|
|
|
TimestampExpired int64
|
|
|
|
}
|
|
|
|
|
2023-10-03 22:56:37 +00:00
|
|
|
// MUCState holds MUC metadata
|
|
|
|
type MUCState struct {
|
|
|
|
Resources map[string]bool
|
|
|
|
Members map[int64]*MUCMember
|
|
|
|
}
|
|
|
|
|
|
|
|
// MUCMember represents a MUC member
|
|
|
|
type MUCMember struct {
|
|
|
|
Nickname string
|
|
|
|
Affiliation string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMUCState() *MUCState {
|
|
|
|
return &MUCState{
|
|
|
|
Resources: make(map[string]bool),
|
|
|
|
Members: make(map[int64]*MUCMember),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 00:25:15 +00:00
|
|
|
// Client stores the metadata for lazily invoked TDlib instance
|
|
|
|
type Client struct {
|
2019-12-16 01:02:53 +00:00
|
|
|
client *client.Client
|
|
|
|
authorizer *clientAuthorizer
|
2023-08-01 01:25:24 +00:00
|
|
|
parameters *client.SetTdlibParametersRequest
|
2019-12-16 01:02:53 +00:00
|
|
|
options []client.Option
|
|
|
|
me *client.User
|
2019-11-26 22:14:06 +00:00
|
|
|
|
2022-01-03 03:54:13 +00:00
|
|
|
xmpp *xmpp.Component
|
|
|
|
jid string
|
|
|
|
Session *persistence.Session
|
|
|
|
resources map[string]bool
|
2023-07-09 03:52:30 +00:00
|
|
|
outbox map[string]string
|
2022-01-03 03:54:13 +00:00
|
|
|
content *config.TelegramContentConfig
|
|
|
|
cache *cache.Cache
|
|
|
|
online bool
|
2019-11-26 22:14:06 +00:00
|
|
|
|
2022-01-31 14:31:05 +00:00
|
|
|
DelayedStatuses map[int64]*DelayedStatus
|
|
|
|
DelayedStatusesLock sync.Mutex
|
|
|
|
|
2023-07-22 14:46:35 +00:00
|
|
|
lastMsgHashes map[int64]uint64
|
|
|
|
msgHashSeed maphash.Seed
|
|
|
|
|
2023-10-03 22:56:37 +00:00
|
|
|
mucCache map[int64]*MUCState
|
2023-09-19 08:23:39 +00:00
|
|
|
|
2023-07-09 03:52:30 +00:00
|
|
|
locks clientLocks
|
|
|
|
SendMessageLock sync.Mutex
|
2019-11-03 22:15:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
type clientLocks struct {
|
2023-01-23 07:30:02 +00:00
|
|
|
authorizationReady sync.Mutex
|
2019-12-30 05:01:56 +00:00
|
|
|
chatMessageLocks map[int64]*sync.Mutex
|
2022-01-03 03:54:13 +00:00
|
|
|
resourcesLock sync.Mutex
|
2023-07-09 03:52:30 +00:00
|
|
|
outboxLock sync.Mutex
|
2023-10-03 22:56:37 +00:00
|
|
|
mucCacheLock sync.Mutex
|
2023-07-22 14:46:35 +00:00
|
|
|
lastMsgHashesLock sync.Mutex
|
2023-08-31 21:26:35 +00:00
|
|
|
|
|
|
|
authorizerReadLock sync.Mutex
|
|
|
|
authorizerWriteLock sync.Mutex
|
2019-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 22:15:43 +00:00
|
|
|
// NewClient instantiates a Telegram App
|
2019-11-24 17:10:29 +00:00
|
|
|
func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component, session *persistence.Session) (*Client, error) {
|
2019-12-16 01:02:53 +00:00
|
|
|
var options []client.Option
|
|
|
|
|
|
|
|
options = append(options, client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
|
2019-11-03 22:15:43 +00:00
|
|
|
NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
|
2019-12-16 01:02:53 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
if conf.Tdlib.Client.CatchTimeout != 0 {
|
|
|
|
options = append(options, client.WithCatchTimeout(
|
|
|
|
time.Duration(conf.Tdlib.Client.CatchTimeout)*time.Second,
|
|
|
|
))
|
|
|
|
}
|
2019-11-03 22:15:43 +00:00
|
|
|
|
2022-01-17 20:45:40 +00:00
|
|
|
apiID, err := strconv.ParseInt(conf.Tdlib.Client.APIID, 10, 32)
|
2019-11-03 22:15:43 +00:00
|
|
|
if err != nil {
|
2019-11-24 17:10:29 +00:00
|
|
|
return &Client{}, errors.Wrap(err, "Wrong api_id")
|
2019-11-03 22:15:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 06:57:46 +00:00
|
|
|
datadir := conf.Tdlib.Datadir
|
|
|
|
if datadir == "" {
|
|
|
|
datadir = "./sessions/" // ye olde defaute
|
|
|
|
}
|
|
|
|
|
2023-08-01 01:25:24 +00:00
|
|
|
parameters := client.SetTdlibParametersRequest{
|
2019-11-03 22:15:43 +00:00
|
|
|
UseTestDc: false,
|
|
|
|
|
2022-01-27 06:57:46 +00:00
|
|
|
DatabaseDirectory: filepath.Join(datadir, jid),
|
|
|
|
FilesDirectory: filepath.Join(datadir, jid, "/files/"),
|
2019-11-03 22:15:43 +00:00
|
|
|
|
|
|
|
UseFileDatabase: true,
|
|
|
|
UseChatInfoDatabase: conf.Tdlib.Client.UseChatInfoDatabase,
|
|
|
|
UseMessageDatabase: true,
|
|
|
|
UseSecretChats: conf.Tdlib.Client.UseSecretChats,
|
|
|
|
|
2022-01-17 20:45:40 +00:00
|
|
|
ApiId: int32(apiID),
|
2019-11-03 22:15:43 +00:00
|
|
|
ApiHash: conf.Tdlib.Client.APIHash,
|
|
|
|
|
|
|
|
SystemLanguageCode: "en",
|
|
|
|
DeviceModel: conf.Tdlib.Client.DeviceModel,
|
|
|
|
SystemVersion: "1.0.0",
|
|
|
|
ApplicationVersion: conf.Tdlib.Client.ApplicationVersion,
|
|
|
|
|
|
|
|
EnableStorageOptimizer: true,
|
|
|
|
IgnoreFileNames: false,
|
|
|
|
}
|
|
|
|
|
2019-11-24 17:10:29 +00:00
|
|
|
return &Client{
|
2022-02-08 20:25:58 +00:00
|
|
|
parameters: ¶meters,
|
|
|
|
xmpp: component,
|
|
|
|
jid: jid,
|
|
|
|
Session: session,
|
|
|
|
resources: make(map[string]bool),
|
2023-07-09 03:52:30 +00:00
|
|
|
outbox: make(map[string]string),
|
2023-10-03 22:56:37 +00:00
|
|
|
mucCache: make(map[int64]*MUCState),
|
2022-02-08 20:25:58 +00:00
|
|
|
content: &conf.Content,
|
|
|
|
cache: cache.NewCache(),
|
|
|
|
options: options,
|
2022-01-31 14:31:05 +00:00
|
|
|
DelayedStatuses: make(map[int64]*DelayedStatus),
|
2023-07-22 14:46:35 +00:00
|
|
|
lastMsgHashes: make(map[int64]uint64),
|
|
|
|
msgHashSeed: maphash.MakeSeed(),
|
2019-12-30 05:01:56 +00:00
|
|
|
locks: clientLocks{
|
|
|
|
chatMessageLocks: make(map[int64]*sync.Mutex),
|
|
|
|
},
|
2019-11-03 22:15:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|