package telegram import ( "github.com/pkg/errors" "path/filepath" "strconv" "sync" "time" "dev.narayana.im/narayana/telegabber/config" "dev.narayana.im/narayana/telegabber/persistence" "dev.narayana.im/narayana/telegabber/telegram/cache" "github.com/zelenin/go-tdlib/client" "gosrc.io/xmpp" ) // DelayedStatus describes an online status expiring on timeout type DelayedStatus struct { TimestampOnline int64 TimestampExpired int64 } // MUCState holds MUC metadata type MUCState struct { Resources map[string]bool Occupants map[int64]*MUCOccupant } // MUCOccupant represents a MUC occupant type MUCOccupant struct { Nickname string Affiliation string Role string Status client.ChatMemberStatus } func NewMUCState() *MUCState { return &MUCState{ Resources: make(map[string]bool), Occupants: make(map[int64]*MUCOccupant), } } // HashedAvatar stores a SHA-1 hash and a Telegram file ID type HashedAvatar struct { Hash string File int32 } // IntPair holds two int64 values type IntPair struct { ChatId int64 MessageId int64 } // Client stores the metadata for lazily invoked TDlib instance type Client struct { client *client.Client authorizer *clientAuthorizer parameters *client.SetTdlibParametersRequest options []client.Option me *client.User xmpp *xmpp.Component jid string Session *persistence.Session resources map[string]bool content *config.TelegramContentConfig cache *cache.Cache online bool loginWizard *loginWizardMetadata lastAuthorizationStateType string outbox map[string]string editOutbox map[string]string pinOutbox map[IntPair]chan int64 DelayedStatuses map[int64]*DelayedStatus DelayedStatusesLock sync.Mutex lastMsgHashes map[int64]uint64 lastMsgIds map[int64]string mucCache map[int64]*MUCState LastBotCmdString string XmppClientFeatures map[string]*[]string XmppClientFeaturesLock sync.Mutex avatarHashes map[int64]*HashedAvatar avatarHashesLock sync.Mutex locks clientLocks SendMessageLock sync.Mutex } type clientLocks struct { authorizationReady sync.Mutex chatMessageLocks map[int64]*sync.Mutex resourcesLock sync.Mutex outboxLock sync.Mutex mucCacheLock sync.Mutex editOutboxLock sync.Mutex pinOutboxLock sync.Mutex lastMsgHashesLock sync.Mutex lastMsgIdsLock sync.RWMutex authorizerReadLock sync.Mutex authorizerWriteLock sync.Mutex loginWizardReadLock sync.Mutex loginWizardWriteLock sync.Mutex } type loginWizardMetadata struct { nextStage chan string chanBusy bool commandSent bool } // NewClient instantiates a Telegram App func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component, session *persistence.Session) (*Client, error) { var options []client.Option if conf.Tdlib.Client.CatchTimeout != 0 { options = append(options, client.WithCatchTimeout( time.Duration(conf.Tdlib.Client.CatchTimeout)*time.Second, )) } apiID, err := strconv.ParseInt(conf.Tdlib.Client.APIID, 10, 32) if err != nil { return &Client{}, errors.Wrap(err, "Wrong api_id") } datadir := conf.Tdlib.Datadir if datadir == "" { datadir = "./sessions/" // ye olde defaute } parameters := client.SetTdlibParametersRequest{ UseTestDc: false, DatabaseDirectory: filepath.Join(datadir, jid), FilesDirectory: filepath.Join(datadir, jid, "/files/"), UseFileDatabase: true, UseChatInfoDatabase: conf.Tdlib.Client.UseChatInfoDatabase, UseMessageDatabase: true, UseSecretChats: conf.Tdlib.Client.UseSecretChats, ApiId: int32(apiID), 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, } return &Client{ parameters: ¶meters, xmpp: component, jid: jid, Session: session, resources: make(map[string]bool), content: &conf.Content, cache: cache.NewCache(), outbox: make(map[string]string), editOutbox: make(map[string]string), pinOutbox: make(map[IntPair]chan int64), mucCache: make(map[int64]*MUCState), options: options, DelayedStatuses: make(map[int64]*DelayedStatus), lastMsgHashes: make(map[int64]uint64), lastMsgIds: make(map[int64]string), XmppClientFeatures: make(map[string]*[]string), avatarHashes: make(map[int64]*HashedAvatar), locks: clientLocks{ chatMessageLocks: make(map[int64]*sync.Mutex), }, }, nil }