2019-11-29 00:51:41 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
2019-12-03 21:14:32 +00:00
|
|
|
"fmt"
|
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
|
|
|
|
2020-01-13 17:22:45 +00:00
|
|
|
"dev.narayana.im/narayana/telegabber/telegram/formatter"
|
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"
|
2022-01-17 20:45:40 +00:00
|
|
|
"github.com/zelenin/go-tdlib/client"
|
2019-11-29 00:51:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-30 05:01:56 +00:00
|
|
|
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)
|
2019-12-04 16:22:22 +00:00
|
|
|
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)
|
2019-12-04 16:22:22 +00:00
|
|
|
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)
|
2019-12-04 16:22:22 +00:00
|
|
|
log.Debugf("%#v", typedUpdate.Chat)
|
2022-02-06 13:25:46 +00:00
|
|
|
case client.TypeUpdateChatPosition:
|
|
|
|
typedUpdate, ok := update.(*client.UpdateChatPosition)
|
|
|
|
if !ok {
|
|
|
|
uhOh()
|
|
|
|
}
|
|
|
|
c.updateChatPosition(typedUpdate)
|
|
|
|
log.Debugf("%#v", typedUpdate)
|
|
|
|
case client.TypeUpdateChatLastMessage:
|
|
|
|
typedUpdate, ok := update.(*client.UpdateChatLastMessage)
|
|
|
|
if !ok {
|
|
|
|
uhOh()
|
|
|
|
}
|
|
|
|
c.updateChatLastMessage(typedUpdate)
|
|
|
|
log.Debugf("%#v", typedUpdate)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeUpdateNewMessage:
|
|
|
|
typedUpdate, ok := update.(*client.UpdateNewMessage)
|
|
|
|
if !ok {
|
|
|
|
uhOh()
|
|
|
|
}
|
|
|
|
c.updateNewMessage(typedUpdate)
|
2019-12-04 16:22:22 +00:00
|
|
|
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)
|
2019-12-04 16:22:22 +00:00
|
|
|
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-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) {
|
2022-01-17 20:45:40 +00:00
|
|
|
c.cache.SetUser(update.User.Id, update.User)
|
2022-02-07 16:21:43 +00:00
|
|
|
show, status, presenceType := c.userStatusToText(update.User.Status, update.User.Id)
|
|
|
|
go c.ProcessStatusUpdate(update.User.Id, status, show, gateway.SPType(presenceType))
|
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) {
|
2022-02-07 16:21:43 +00:00
|
|
|
show, status, presenceType := c.userStatusToText(update.Status, update.UserId)
|
|
|
|
go c.ProcessStatusUpdate(update.UserId, status, show, gateway.SPImmed(false), gateway.SPType(presenceType))
|
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) {
|
2019-12-30 05:01:56 +00:00
|
|
|
go func() {
|
|
|
|
if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil {
|
2022-02-03 21:01:38 +00:00
|
|
|
_, err := c.DownloadFile(update.Chat.Photo.Small.Id, 10, true)
|
2019-12-30 05:01:56 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to download the chat photo")
|
|
|
|
}
|
2019-11-30 00:41:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:45:40 +00:00
|
|
|
c.cache.SetChat(update.Chat.Id, update.Chat)
|
2019-11-30 00:41:22 +00:00
|
|
|
|
2022-02-06 13:25:46 +00:00
|
|
|
if update.Chat.Positions != nil && len(update.Chat.Positions) > 0 {
|
|
|
|
c.subscribeToID(update.Chat.Id, update.Chat)
|
2019-12-30 05:01:56 +00:00
|
|
|
}
|
2019-11-30 00:41:22 +00:00
|
|
|
|
2022-01-17 20:45:40 +00:00
|
|
|
if update.Chat.Id < 0 {
|
|
|
|
c.ProcessStatusUpdate(update.Chat.Id, update.Chat.Title, "chat")
|
2019-12-30 05:01:56 +00:00
|
|
|
}
|
|
|
|
}()
|
2019-11-29 11:48:27 +00:00
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
|
2022-02-06 13:25:46 +00:00
|
|
|
// chat position is updated
|
|
|
|
func (c *Client) updateChatPosition(update *client.UpdateChatPosition) {
|
|
|
|
if update.Position != nil && update.Position.Order != 0 {
|
|
|
|
go c.subscribeToID(update.ChatId, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chat last message is updated
|
|
|
|
func (c *Client) updateChatLastMessage(update *client.UpdateChatLastMessage) {
|
|
|
|
if update.Positions != nil && len(update.Positions) > 0 {
|
|
|
|
go c.subscribeToID(update.ChatId, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-12-30 05:01:56 +00:00
|
|
|
go func() {
|
2022-02-12 01:47:54 +00:00
|
|
|
chatId := update.Message.ChatId
|
|
|
|
|
2019-12-30 05:01:56 +00:00
|
|
|
// guarantee sequential message delivering per chat
|
2022-02-12 01:47:54 +00:00
|
|
|
lock := c.getChatMessageLock(chatId)
|
2019-12-30 05:01:56 +00:00
|
|
|
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
|
|
|
|
2019-12-30 05:01:56 +00:00
|
|
|
log.WithFields(log.Fields{
|
2022-02-12 01:47:54 +00:00
|
|
|
"chat_id": chatId,
|
2019-12-30 05:01:56 +00:00
|
|
|
}).Warn("New message from chat")
|
2019-12-01 13:13:45 +00:00
|
|
|
|
2022-02-12 01:47:54 +00:00
|
|
|
var text string
|
|
|
|
content := update.Message.Content
|
|
|
|
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
|
|
|
|
chat, err := c.client.GetChat(&client.GetChatRequest{
|
|
|
|
ChatId: chatId,
|
|
|
|
})
|
2022-01-31 07:05:42 +00:00
|
|
|
if err == nil {
|
2022-02-12 01:47:54 +00:00
|
|
|
c.cache.SetChat(chatId, chat)
|
|
|
|
go c.ProcessStatusUpdate(chatId, "", "", gateway.SPImmed(true))
|
|
|
|
text = "<Chat photo has changed>"
|
2022-01-31 07:05:42 +00:00
|
|
|
}
|
2022-02-12 01:47:54 +00:00
|
|
|
} else {
|
|
|
|
text = c.messageToText(update.Message, false)
|
2022-03-09 19:15:56 +00:00
|
|
|
file := c.contentToFile(content)
|
2019-12-30 05:01:56 +00:00
|
|
|
|
2022-03-09 19:15:56 +00:00
|
|
|
// download file (if one)
|
|
|
|
if file != nil {
|
2022-02-12 01:47:54 +00:00
|
|
|
newFile, err := c.DownloadFile(file.Id, 1, true)
|
|
|
|
if err == nil {
|
|
|
|
file = newFile
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
2022-02-12 01:47:54 +00:00
|
|
|
// OTR support (I do not know why would you need it, seriously)
|
|
|
|
if !(strings.HasPrefix(text, "?OTR") || c.Session.RawMessages) {
|
|
|
|
var prefix strings.Builder
|
2022-03-09 19:15:56 +00:00
|
|
|
prefix.WriteString(c.messageToPrefix(update.Message, c.formatFile(file)))
|
2022-02-12 01:47:54 +00:00
|
|
|
if text != "" {
|
|
|
|
// \n if it is groupchat and message is not empty
|
|
|
|
if chatId < 0 {
|
|
|
|
prefix.WriteString("\n")
|
|
|
|
} else if chatId > 0 {
|
|
|
|
prefix.WriteString(" | ")
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix.WriteString(text)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
|
2022-02-12 01:47:54 +00:00
|
|
|
text = prefix.String()
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 05:01:56 +00:00
|
|
|
// mark message as read
|
|
|
|
c.client.ViewMessages(&client.ViewMessagesRequest{
|
2022-02-12 01:47:54 +00:00
|
|
|
ChatId: chatId,
|
2022-01-17 20:45:40 +00:00
|
|
|
MessageIds: []int64{update.Message.Id},
|
2019-12-30 05:01:56 +00:00
|
|
|
ForceRead: true,
|
|
|
|
})
|
|
|
|
// forward message to XMPP
|
2022-02-12 01:47:54 +00:00
|
|
|
gateway.SendMessage(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp)
|
2019-12-30 05:01:56 +00:00
|
|
|
}()
|
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) {
|
2021-12-18 16:04:24 +00:00
|
|
|
markupFunction := formatter.EntityToXEP0393
|
2019-12-03 21:14:32 +00:00
|
|
|
if update.NewContent.MessageContentType() == client.TypeMessageText {
|
|
|
|
textContent := update.NewContent.(*client.MessageText)
|
2022-03-10 12:30:02 +00:00
|
|
|
var editChar string
|
|
|
|
if c.Session.AsciiArrows {
|
|
|
|
editChar = "e "
|
|
|
|
} else {
|
|
|
|
editChar = "✎ "
|
|
|
|
}
|
|
|
|
text := editChar + fmt.Sprintf("%v | %s", update.MessageId, formatter.Format(
|
2020-01-13 17:22:45 +00:00
|
|
|
textContent.Text.Text,
|
2022-03-11 16:12:36 +00:00
|
|
|
textContent.Text.Entities,
|
2020-01-13 17:22:45 +00:00
|
|
|
markupFunction,
|
|
|
|
))
|
2022-01-17 20:45:40 +00:00
|
|
|
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
|
2019-12-03 21:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-01-17 20:45:40 +00:00
|
|
|
text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIds), ",")
|
|
|
|
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
|
2019-12-03 21:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-03 22:17:36 +00:00
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|