telegabber/telegram/handlers.go

280 lines
7.2 KiB
Go
Raw Normal View History

2019-11-29 00:51:41 +00:00
package telegram
import (
2019-12-03 22:17:36 +00:00
"crypto/sha256"
2019-12-03 21:14:32 +00:00
"fmt"
2019-12-03 22:17:36 +00:00
"os"
"path/filepath"
2019-11-30 00:41:22 +00:00
"strconv"
2019-12-01 13:13:45 +00:00
"strings"
2019-12-03 21:14:32 +00:00
"sync"
2019-11-30 00:41:22 +00:00
2019-11-29 11:48:27 +00:00
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
2019-11-29 00:51:41 +00:00
log "github.com/sirupsen/logrus"
"github.com/zelenin/go-tdlib/client"
)
func uhOh() {
log.Fatal("Update type mismatch")
}
2019-12-03 21:14:32 +00:00
func int64SliceToStringSlice(ints []int64) []string {
strings := make([]string, len(ints))
wg := sync.WaitGroup{}
for i, xi := range ints {
wg.Add(1)
go func(i int, xi int64) {
strings[i] = strconv.FormatInt(xi, 10)
wg.Done()
}(i, xi)
}
wg.Wait()
return strings
}
func (c *Client) getChatMessageLock(chatID int64) *sync.Mutex {
lock, ok := c.locks.chatMessageLocks[chatID]
if !ok {
lock = &sync.Mutex{}
c.locks.chatMessageLocks[chatID] = lock
}
return lock
}
2019-11-29 00:51:41 +00:00
func (c *Client) updateHandler() {
listener := c.client.GetListener()
defer listener.Close()
for update := range listener.Updates {
if update.GetClass() == client.ClassUpdate {
switch update.GetType() {
case client.TypeUpdateUser:
typedUpdate, ok := update.(*client.UpdateUser)
if !ok {
uhOh()
}
c.updateUser(typedUpdate)
log.Debugf("%#v", typedUpdate.User)
2019-11-29 11:48:27 +00:00
case client.TypeUpdateUserStatus:
typedUpdate, ok := update.(*client.UpdateUserStatus)
if !ok {
uhOh()
}
c.updateUserStatus(typedUpdate)
log.Debugf("%#v", typedUpdate.Status)
2019-11-30 00:41:22 +00:00
case client.TypeUpdateNewChat:
typedUpdate, ok := update.(*client.UpdateNewChat)
if !ok {
uhOh()
}
c.updateNewChat(typedUpdate)
log.Debugf("%#v", typedUpdate.Chat)
2019-12-01 13:13:45 +00:00
case client.TypeUpdateNewMessage:
typedUpdate, ok := update.(*client.UpdateNewMessage)
if !ok {
uhOh()
}
c.updateNewMessage(typedUpdate)
log.Debugf("%#v", typedUpdate.Message)
2019-12-03 21:14:32 +00:00
case client.TypeUpdateMessageContent:
typedUpdate, ok := update.(*client.UpdateMessageContent)
if !ok {
uhOh()
}
c.updateMessageContent(typedUpdate)
log.Debugf("%#v", typedUpdate.NewContent)
2019-12-03 21:14:32 +00:00
case client.TypeUpdateDeleteMessages:
typedUpdate, ok := update.(*client.UpdateDeleteMessages)
if !ok {
uhOh()
}
c.updateDeleteMessages(typedUpdate)
2019-12-03 22:17:36 +00:00
case client.TypeUpdateFile:
typedUpdate, ok := update.(*client.UpdateFile)
if !ok {
uhOh()
}
c.updateFile(typedUpdate)
log.Debugf("%#v", typedUpdate.File)
2019-12-17 01:56:11 +00:00
case client.TypeUpdateAuthorizationState:
typedUpdate, ok := update.(*client.UpdateAuthorizationState)
if !ok {
uhOh()
}
c.updateAuthorizationState(typedUpdate)
2019-11-29 00:51:41 +00:00
default:
// log only handled types
continue
}
log.Debugf("%#v", update)
}
}
}
2019-12-03 22:17:36 +00:00
// new user discovered
2019-11-29 00:51:41 +00:00
func (c *Client) updateUser(update *client.UpdateUser) {
2019-12-28 02:35:40 +00:00
c.cache.SetUser(update.User.Id, update.User)
2019-12-11 22:48:35 +00:00
show, status := c.userStatusToText(update.User.Status)
2019-12-21 02:16:43 +00:00
go c.processStatusUpdate(int64(update.User.Id), status, show)
2019-11-29 00:51:41 +00:00
}
2019-11-29 11:48:27 +00:00
2019-12-03 22:17:36 +00:00
// user status changed
2019-11-29 11:48:27 +00:00
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
2019-12-11 22:48:35 +00:00
show, status := c.userStatusToText(update.Status)
2019-12-21 02:16:43 +00:00
go c.processStatusUpdate(int64(update.UserId), status, show, gateway.SPImmed(false))
2019-11-30 00:41:22 +00:00
}
2019-12-03 22:17:36 +00:00
// new chat discovered
2019-11-30 00:41:22 +00:00
func (c *Client) updateNewChat(update *client.UpdateNewChat) {
go func() {
if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil {
_, err := c.client.DownloadFile(&client.DownloadFileRequest{
FileId: update.Chat.Photo.Small.Id,
Priority: 32,
Synchronous: true,
})
if err != nil {
log.Error("Failed to download the chat photo")
}
2019-11-30 00:41:22 +00:00
}
c.cache.SetChat(update.Chat.Id, update.Chat)
2019-11-30 00:41:22 +00:00
var isChannel = false
if update.Chat.Type.ChatTypeType() == client.TypeChatTypeSupergroup {
typeSupergroup, ok := update.Chat.Type.(*client.ChatTypeSupergroup)
if !ok {
uhOh()
}
isChannel = typeSupergroup.IsChannel
2019-11-30 00:41:22 +00:00
}
if !(isChannel && update.Chat.LastReadInboxMessageId == 0) {
gateway.SendPresence(
c.xmpp,
c.jid,
gateway.SPFrom(strconv.FormatInt(update.Chat.Id, 10)),
gateway.SPType("subscribe"),
gateway.SPNickname(update.Chat.Title),
)
}
2019-11-30 00:41:22 +00:00
if update.Chat.Id < 0 {
c.processStatusUpdate(update.Chat.Id, update.Chat.Title, "chat")
}
}()
2019-11-29 11:48:27 +00:00
}
2019-12-01 13:13:45 +00:00
2019-12-03 22:17:36 +00:00
// message received
2019-12-01 13:13:45 +00:00
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
go func() {
// guarantee sequential message delivering per chat
lock := c.getChatMessageLock(update.Message.ChatId)
lock.Lock()
defer lock.Unlock()
// ignore self outgoing messages
if update.Message.IsOutgoing &&
update.Message.SendingState != nil &&
update.Message.SendingState.MessageSendingStateType() == client.TypeMessageSendingStatePending {
return
}
2019-12-01 13:13:45 +00:00
log.WithFields(log.Fields{
"chat_id": update.Message.ChatId,
}).Warn("New message from chat")
2019-12-01 13:13:45 +00:00
text := c.messageToText(update.Message)
file, filename := c.contentToFilename(update.Message.Content)
2019-12-01 13:13:45 +00:00
// download file(s)
if file != nil && !file.Local.IsDownloadingCompleted {
c.client.DownloadFile(&client.DownloadFileRequest{
FileId: file.Id,
Priority: 32,
Synchronous: true,
})
}
// OTR support (I do not know why would you need it, seriously)
if !strings.HasPrefix(text, "?OTR") {
var prefix strings.Builder
prefix.WriteString(c.messageToPrefix(update.Message, c.formatContent(file, filename)))
if text != "" {
// \n if it is groupchat and message is not empty
if update.Message.ChatId < 0 {
prefix.WriteString("\n")
} else if update.Message.ChatId > 0 {
prefix.WriteString(" | ")
}
prefix.WriteString(text)
2019-12-01 13:13:45 +00:00
}
text = prefix.String()
2019-12-01 13:13:45 +00:00
}
// mark message as read
c.client.ViewMessages(&client.ViewMessagesRequest{
ChatId: update.Message.ChatId,
MessageIds: []int64{update.Message.Id},
ForceRead: true,
})
// forward message to XMPP
gateway.SendMessage(c.jid, strconv.FormatInt(update.Message.ChatId, 10), text, c.xmpp)
}()
2019-12-01 13:13:45 +00:00
}
2019-12-03 21:14:32 +00:00
2019-12-03 22:17:36 +00:00
// message content updated
2019-12-03 21:14:32 +00:00
func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
if update.NewContent.MessageContentType() == client.TypeMessageText {
textContent := update.NewContent.(*client.MessageText)
text := fmt.Sprintf("✎ %v | %s", update.MessageId, textContent.Text.Text)
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
}
}
2019-12-03 22:17:36 +00:00
// message(s) deleted
2019-12-03 21:14:32 +00:00
func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) {
if update.IsPermanent {
text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIds), ",")
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
}
}
2019-12-03 22:17:36 +00:00
// file downloaded
func (c *Client) updateFile(update *client.UpdateFile) {
// not really
if !update.File.Local.IsDownloadingCompleted {
return
}
err := os.Symlink(
update.File.Local.Path,
fmt.Sprintf(
"%s/%s%s",
c.content.Path,
fmt.Sprintf("%x", sha256.Sum256([]byte(update.File.Remote.Id))),
filepath.Ext(update.File.Local.Path),
),
)
if err != nil {
log.Errorf("Error creating symlink: %v", err)
}
}
2019-12-17 01:56:11 +00:00
func (c *Client) updateAuthorizationState(update *client.UpdateAuthorizationState) {
switch update.AuthorizationState.AuthorizationStateType() {
case client.TypeAuthorizationStateClosing:
log.Warn("Closing the updates listener")
case client.TypeAuthorizationStateClosed:
log.Warn("Closed the updates listener")
c.forceClose()
}
}