parent
7e036fd795
commit
aaf7233c89
@ -0,0 +1,118 @@
|
|||||||
|
package telegram
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"dev.narayana.im/narayana/telegabber/config"
|
||||||
|
|
||||||
|
"github.com/zelenin/go-tdlib/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
var logConstants = map[string]int32{
|
||||||
|
"fatal": 0,
|
||||||
|
"error": 1,
|
||||||
|
"warn": 2,
|
||||||
|
"info": 3,
|
||||||
|
"debug": 4,
|
||||||
|
"verbose": 5,
|
||||||
|
"all": 1023,
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringToLogConstant(c string) int32 {
|
||||||
|
level, ok := logConstants[c]
|
||||||
|
if !ok {
|
||||||
|
level = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return level
|
||||||
|
}
|
||||||
|
|
||||||
|
type TelegramClient struct {
|
||||||
|
client *client.Client
|
||||||
|
jid string
|
||||||
|
parameters *client.TdlibParameters
|
||||||
|
online bool
|
||||||
|
logVerbosity client.Option
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient instantiates a Telegram App
|
||||||
|
func NewClient(conf config.TelegramConfig, jid string) (TelegramClient, error) {
|
||||||
|
logVerbosity := client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
|
||||||
|
NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
|
||||||
|
})
|
||||||
|
|
||||||
|
apiId, err := strconv.Atoi(conf.Tdlib.Client.APIID)
|
||||||
|
if err != nil {
|
||||||
|
return TelegramClient{}, errors.Wrap(err, "Wrong api_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
parameters := client.TdlibParameters{
|
||||||
|
UseTestDc: false,
|
||||||
|
|
||||||
|
DatabaseDirectory: filepath.Join("./sessions/", jid),
|
||||||
|
FilesDirectory: filepath.Join("./sessions/", 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 TelegramClient{
|
||||||
|
parameters: ¶meters,
|
||||||
|
jid: jid,
|
||||||
|
logVerbosity: logVerbosity,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateHandler(tdlibClient *client.Client) {
|
||||||
|
listener := tdlibClient.GetListener()
|
||||||
|
defer listener.Close()
|
||||||
|
|
||||||
|
for update := range listener.Updates {
|
||||||
|
if update.GetClass() == client.ClassUpdate {
|
||||||
|
fmt.Printf("%#v", update)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TelegramClient) Connect() error {
|
||||||
|
if c.online {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizer := client.ClientAuthorizer()
|
||||||
|
authorizer.TdlibParameters <- c.parameters
|
||||||
|
|
||||||
|
tdlibClient, err := client.NewClient(authorizer, c.logVerbosity)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Coudn't initialize a Telegram client instance")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.client = tdlibClient
|
||||||
|
c.online = true
|
||||||
|
|
||||||
|
go updateHandler(c.client)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TelegramClient) Disconnect() {
|
||||||
|
if !c.online {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
@ -1,22 +1,109 @@
|
|||||||
package xmpp
|
package xmpp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"dev.narayana.im/narayana/telegabber/telegram"
|
||||||
"os"
|
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"gosrc.io/xmpp"
|
"gosrc.io/xmpp"
|
||||||
"gosrc.io/xmpp/stanza"
|
"gosrc.io/xmpp/stanza"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var sessions map[string]telegram.TelegramClient
|
||||||
|
|
||||||
|
func logPacketType(p stanza.Packet) {
|
||||||
|
log.Warn("Ignoring packet: %T\n", p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleIq processes an incoming XMPP iq
|
||||||
|
func HandleIq(s xmpp.Sender, p stanza.Packet) {
|
||||||
|
iq, ok := p.(stanza.IQ)
|
||||||
|
if !ok {
|
||||||
|
logPacketType(p)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Iq: %#v\n", iq)
|
||||||
|
}
|
||||||
|
|
||||||
// HandleMessage processes an incoming XMPP message
|
// HandleMessage processes an incoming XMPP message
|
||||||
func HandleMessage(s xmpp.Sender, p stanza.Packet) {
|
func HandleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||||
msg, ok := p.(stanza.Message)
|
msg, ok := p.(stanza.Message)
|
||||||
if !ok {
|
if !ok {
|
||||||
_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
|
logPacketType(p)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
|
log.Printf("Message: %#v\n", msg)
|
||||||
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body}
|
reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body}
|
||||||
_ = s.Send(reply)
|
_ = s.Send(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandlePresence processes an incoming XMPP presence
|
||||||
|
func HandlePresence(s xmpp.Sender, p stanza.Packet) {
|
||||||
|
prs, ok := p.(stanza.Presence)
|
||||||
|
if !ok {
|
||||||
|
logPacketType(p)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if prs.Type == "subscribe" {
|
||||||
|
handleSubscription(s, prs)
|
||||||
|
} else if prs.To == jid.Bare() {
|
||||||
|
handlePresence(s, prs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleSubscription(s xmpp.Sender, p stanza.Presence) {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"from": p.From,
|
||||||
|
"to": p.To,
|
||||||
|
}).Warn("Subscription request")
|
||||||
|
log.Debugf("%#v", p)
|
||||||
|
|
||||||
|
reply := stanza.Presence{Attrs: stanza.Attrs{
|
||||||
|
From: p.To,
|
||||||
|
To: p.From,
|
||||||
|
Id: p.Id,
|
||||||
|
Type: "subscribed",
|
||||||
|
}}
|
||||||
|
|
||||||
|
_ = s.Send(reply)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlePresence(s xmpp.Sender, p stanza.Presence) {
|
||||||
|
presenceType := p.Type
|
||||||
|
if presenceType == "" {
|
||||||
|
presenceType = "online"
|
||||||
|
}
|
||||||
|
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"type": presenceType,
|
||||||
|
"from": p.From,
|
||||||
|
"to": p.To,
|
||||||
|
}).Warn("Presence")
|
||||||
|
log.Debugf("%#v", p)
|
||||||
|
|
||||||
|
fromJid, err := xmpp.NewJid(p.From)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Invalid from JID!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
bareFromJid := fromJid.Bare()
|
||||||
|
session, ok := sessions[bareFromJid]
|
||||||
|
if !ok {
|
||||||
|
client, err := telegram.NewClient(tgConf, bareFromJid)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Invalid from JID!")
|
||||||
|
}
|
||||||
|
sessions[bareFromJid] = client
|
||||||
|
}
|
||||||
|
|
||||||
|
switch p.Type {
|
||||||
|
case "unsubscribed":
|
||||||
|
delete(sessions, bareFromJid)
|
||||||
|
case "unavailable", "error":
|
||||||
|
session.Disconnect()
|
||||||
|
case "":
|
||||||
|
session.Connect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in new issue