2019-11-29 00:51:41 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
2023-07-22 14:46:35 +00:00
|
|
|
"encoding/binary"
|
2019-12-01 13:13:45 +00:00
|
|
|
"fmt"
|
2019-11-29 00:51:41 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-07-22 14:46:35 +00:00
|
|
|
"hash/maphash"
|
2019-11-29 00:51:41 +00:00
|
|
|
"io"
|
2022-05-15 16:59:54 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-11-29 00:51:41 +00:00
|
|
|
"os"
|
2022-03-09 19:15:56 +00:00
|
|
|
osUser "os/user"
|
2019-12-01 13:13:45 +00:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2019-11-29 00:51:41 +00:00
|
|
|
"strconv"
|
2019-12-01 13:13:45 +00:00
|
|
|
"strings"
|
2019-11-29 00:51:41 +00:00
|
|
|
"time"
|
2023-10-29 12:47:35 +00:00
|
|
|
"unicode/utf8"
|
2019-11-29 00:51:41 +00:00
|
|
|
|
2020-01-05 13:03:10 +00:00
|
|
|
"dev.narayana.im/narayana/telegabber/telegram/cache"
|
2020-01-09 21:16:40 +00:00
|
|
|
"dev.narayana.im/narayana/telegabber/telegram/formatter"
|
2019-11-29 00:51:41 +00:00
|
|
|
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/soheilhy/args"
|
2022-01-17 20:45:40 +00:00
|
|
|
"github.com/zelenin/go-tdlib/client"
|
2019-11-29 00:51:41 +00:00
|
|
|
)
|
|
|
|
|
2023-06-01 20:37:38 +00:00
|
|
|
type VCardInfo struct {
|
2023-08-01 01:25:24 +00:00
|
|
|
Fn string
|
|
|
|
Photo *client.File
|
|
|
|
Nicknames []string
|
|
|
|
Given string
|
|
|
|
Family string
|
|
|
|
Tel string
|
|
|
|
Info string
|
2023-06-01 20:37:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
type messageStub struct {
|
|
|
|
MessageId int64
|
|
|
|
ChatId int64
|
|
|
|
Sender string
|
|
|
|
Date int32
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
var errOffline = errors.New("TDlib instance is offline")
|
2024-02-18 07:48:02 +00:00
|
|
|
var errOverLimit = errors.New("Over limit")
|
2019-11-29 00:51:41 +00:00
|
|
|
|
2019-12-01 13:13:45 +00:00
|
|
|
var spaceRegex = regexp.MustCompile(`\s+`)
|
2022-01-08 10:59:57 +00:00
|
|
|
var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n")
|
2019-12-01 13:13:45 +00:00
|
|
|
|
|
|
|
const newlineChar string = "\n"
|
2023-10-29 12:47:35 +00:00
|
|
|
const messageHeaderSeparator string = " | " // no hrunicode allowed here yet
|
2019-12-01 13:13:45 +00:00
|
|
|
|
2024-02-15 09:40:57 +00:00
|
|
|
// ChatType is an enum of chat types, roughly corresponding to TDLib's one but better
|
|
|
|
type ChatType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ChatTypeUnknown ChatType = iota
|
|
|
|
ChatTypePrivate
|
|
|
|
ChatTypeBasicGroup
|
|
|
|
ChatTypeSupergroup
|
|
|
|
ChatTypeSecret
|
|
|
|
ChatTypeChannel
|
|
|
|
)
|
|
|
|
|
2024-02-18 07:48:02 +00:00
|
|
|
// MembersList is an enum of member list filters
|
|
|
|
type MembersList int
|
|
|
|
|
|
|
|
const (
|
|
|
|
MembersListMembers MembersList = iota
|
|
|
|
MembersListRestricted
|
|
|
|
MembersListBanned
|
|
|
|
MembersListBannedAndAdministrators
|
|
|
|
)
|
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
// GetContactByUsername resolves username to user id retrieves user and chat information
|
|
|
|
func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) {
|
2019-12-15 02:26:07 +00:00
|
|
|
if !c.Online() {
|
2019-11-29 00:51:41 +00:00
|
|
|
return nil, nil, errOffline
|
|
|
|
}
|
|
|
|
|
2022-02-02 12:49:05 +00:00
|
|
|
var chat *client.Chat
|
|
|
|
var err error
|
|
|
|
var userID int64
|
|
|
|
if strings.HasPrefix(username, "@") {
|
|
|
|
chat, err = c.client.SearchPublicChat(&client.SearchPublicChatRequest{
|
|
|
|
Username: username,
|
|
|
|
})
|
2019-11-29 00:51:41 +00:00
|
|
|
|
2022-02-02 12:49:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userID = chat.Id
|
|
|
|
} else {
|
|
|
|
userID, err = strconv.ParseInt(username, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 12:49:05 +00:00
|
|
|
return c.GetContactByID(userID, chat)
|
2019-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetContactByID gets user and chat information from cache (or tries to retrieve it, if missing)
|
2019-12-04 18:37:46 +00:00
|
|
|
func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *client.User, error) {
|
2022-01-05 21:04:22 +00:00
|
|
|
if !c.Online() || id == 0 {
|
2019-11-29 00:51:41 +00:00
|
|
|
return nil, nil, errOffline
|
|
|
|
}
|
|
|
|
|
|
|
|
var user *client.User
|
|
|
|
var cacheChat *client.Chat
|
|
|
|
var ok bool
|
|
|
|
var err error
|
|
|
|
|
2021-12-04 18:10:54 +00:00
|
|
|
user, ok = c.cache.GetUser(id)
|
|
|
|
if !ok && id > 0 {
|
|
|
|
user, err = c.client.GetUser(&client.GetUserRequest{
|
2022-01-17 20:45:40 +00:00
|
|
|
UserId: id,
|
2021-12-04 18:10:54 +00:00
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
c.cache.SetUser(id, user)
|
2019-12-04 18:37:46 +00:00
|
|
|
}
|
2019-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 02:35:40 +00:00
|
|
|
cacheChat, ok = c.cache.GetChat(id)
|
2019-11-29 00:51:41 +00:00
|
|
|
if !ok {
|
|
|
|
if chat == nil {
|
|
|
|
cacheChat, err = c.client.GetChat(&client.GetChatRequest{
|
2022-01-17 20:45:40 +00:00
|
|
|
ChatId: id,
|
2019-11-29 00:51:41 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-07 19:25:37 +00:00
|
|
|
// error is irrelevant if the user was found successfully
|
2019-12-10 18:49:42 +00:00
|
|
|
if user != nil {
|
2019-12-07 19:25:37 +00:00
|
|
|
return nil, user, nil
|
|
|
|
}
|
2019-12-10 18:49:42 +00:00
|
|
|
|
|
|
|
return nil, nil, err
|
2019-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 02:35:40 +00:00
|
|
|
c.cache.SetChat(id, cacheChat)
|
2019-11-29 00:51:41 +00:00
|
|
|
} else {
|
2019-12-28 02:35:40 +00:00
|
|
|
c.cache.SetChat(id, chat)
|
2019-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if chat == nil {
|
|
|
|
chat = cacheChat
|
|
|
|
}
|
|
|
|
|
|
|
|
return chat, user, nil
|
|
|
|
}
|
|
|
|
|
2024-02-15 09:40:57 +00:00
|
|
|
// GetChatType obtains chat type from its information
|
|
|
|
func (c *Client) GetChatType(id int64) (ChatType, error) {
|
2023-03-18 21:43:11 +00:00
|
|
|
if !c.Online() || id == 0 {
|
2024-02-15 09:40:57 +00:00
|
|
|
return ChatTypeUnknown, errOffline
|
2023-03-18 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
chat, ok := c.cache.GetChat(id)
|
|
|
|
if !ok {
|
|
|
|
chat, err = c.client.GetChat(&client.GetChatRequest{
|
|
|
|
ChatId: id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-02-15 09:40:57 +00:00
|
|
|
return ChatTypeUnknown, err
|
2023-03-18 21:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.cache.SetChat(id, chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
chatType := chat.Type.ChatTypeType()
|
2024-02-15 09:40:57 +00:00
|
|
|
if chatType == client.TypeChatTypePrivate {
|
|
|
|
return ChatTypePrivate, nil
|
|
|
|
} else if chatType == client.TypeChatTypeBasicGroup {
|
|
|
|
return ChatTypeBasicGroup, nil
|
|
|
|
} else if chatType == client.TypeChatTypeSupergroup {
|
|
|
|
supergroup, _ := chat.Type.(*client.ChatTypeSupergroup)
|
|
|
|
if supergroup.IsChannel {
|
|
|
|
return ChatTypeChannel, nil
|
|
|
|
}
|
|
|
|
return ChatTypeSupergroup, nil
|
|
|
|
} else if chatType == client.TypeChatTypeSecret {
|
|
|
|
return ChatTypeSecret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ChatTypeUnknown, errors.New("Unknown chat type")
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPM checks if a chat is PM
|
|
|
|
func (c *Client) IsPM(id int64) (bool, error) {
|
|
|
|
typ, err := c.GetChatType(id)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if typ == ChatTypePrivate || typ == ChatTypeSecret {
|
2023-03-18 21:43:11 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:21:43 +00:00
|
|
|
func (c *Client) userStatusToText(status client.UserStatus, chatID int64) (string, string, string) {
|
|
|
|
var show, textStatus, presenceType string
|
2019-11-30 00:41:22 +00:00
|
|
|
|
|
|
|
switch status.UserStatusType() {
|
|
|
|
case client.TypeUserStatusOnline:
|
2022-01-31 14:31:05 +00:00
|
|
|
onlineStatus, _ := status.(*client.UserStatusOnline)
|
|
|
|
|
|
|
|
c.DelayedStatusesLock.Lock()
|
|
|
|
c.DelayedStatuses[chatID] = &DelayedStatus{
|
|
|
|
TimestampOnline: time.Now().Unix(),
|
|
|
|
TimestampExpired: int64(onlineStatus.Expires),
|
|
|
|
}
|
|
|
|
c.DelayedStatusesLock.Unlock()
|
|
|
|
|
2019-11-30 00:41:22 +00:00
|
|
|
textStatus = "Online"
|
|
|
|
case client.TypeUserStatusRecently:
|
|
|
|
show, textStatus = "dnd", "Last seen recently"
|
2022-01-31 14:31:05 +00:00
|
|
|
|
|
|
|
c.DelayedStatusesLock.Lock()
|
|
|
|
delete(c.DelayedStatuses, chatID)
|
|
|
|
c.DelayedStatusesLock.Unlock()
|
2019-11-30 00:41:22 +00:00
|
|
|
case client.TypeUserStatusLastWeek:
|
2022-02-07 16:21:43 +00:00
|
|
|
show, textStatus = "xa", "Last seen last week"
|
2019-11-30 00:41:22 +00:00
|
|
|
case client.TypeUserStatusLastMonth:
|
2022-02-07 16:21:43 +00:00
|
|
|
show, textStatus = "xa", "Last seen last month"
|
2019-11-30 00:41:22 +00:00
|
|
|
case client.TypeUserStatusEmpty:
|
2022-02-07 16:21:43 +00:00
|
|
|
presenceType, textStatus = "unavailable", "Last seen a long time ago"
|
2019-11-30 00:41:22 +00:00
|
|
|
case client.TypeUserStatusOffline:
|
2019-12-01 13:13:45 +00:00
|
|
|
offlineStatus, _ := status.(*client.UserStatusOffline)
|
2019-11-30 00:41:22 +00:00
|
|
|
// this will stop working in 2038 O\
|
2022-01-31 14:31:05 +00:00
|
|
|
wasOnline := int64(offlineStatus.WasOnline)
|
|
|
|
elapsed := time.Now().Unix() - wasOnline
|
2019-11-30 00:41:22 +00:00
|
|
|
if elapsed < 3600 {
|
|
|
|
show = "away"
|
|
|
|
} else {
|
|
|
|
show = "xa"
|
|
|
|
}
|
2022-01-31 14:31:05 +00:00
|
|
|
textStatus = c.LastSeenStatus(wasOnline)
|
|
|
|
|
|
|
|
c.DelayedStatusesLock.Lock()
|
|
|
|
delete(c.DelayedStatuses, chatID)
|
|
|
|
c.DelayedStatusesLock.Unlock()
|
2019-11-30 00:41:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:21:43 +00:00
|
|
|
return show, textStatus, presenceType
|
2019-11-30 00:41:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:31:05 +00:00
|
|
|
// LastSeenStatus formats a timestamp to a "Last seen at" string
|
|
|
|
func (c *Client) LastSeenStatus(timestamp int64) string {
|
|
|
|
return time.Unix(int64(timestamp), 0).
|
|
|
|
In(c.Session.TimezoneToLocation()).
|
|
|
|
Format("Last seen at 15:04 02/01/2006")
|
|
|
|
}
|
|
|
|
|
2020-01-05 13:03:10 +00:00
|
|
|
// ProcessStatusUpdate sets contact status
|
2022-02-07 16:21:43 +00:00
|
|
|
func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, oldArgs ...args.V) error {
|
2019-12-15 02:26:07 +00:00
|
|
|
if !c.Online() {
|
2019-11-29 00:51:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"chat_id": chatID,
|
|
|
|
}).Info("Status update for")
|
|
|
|
|
|
|
|
chat, user, err := c.GetContactByID(chatID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var photo string
|
|
|
|
if chat != nil && chat.Photo != nil {
|
2023-06-01 20:37:38 +00:00
|
|
|
file, path, err := c.ForceOpenFile(chat.Photo.Small, 1)
|
2019-11-29 00:51:41 +00:00
|
|
|
if err == nil {
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
hash := sha1.New()
|
|
|
|
_, err = io.Copy(hash, file)
|
|
|
|
if err == nil {
|
2019-12-01 13:12:55 +00:00
|
|
|
photo = fmt.Sprintf("%x", hash.Sum(nil))
|
2019-11-29 00:51:41 +00:00
|
|
|
} else {
|
|
|
|
log.Errorf("Error calculating hash: %v", path)
|
|
|
|
}
|
|
|
|
} else if path != "" {
|
|
|
|
log.Errorf("Photo does not exist: %v", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:21:43 +00:00
|
|
|
var presenceType string
|
|
|
|
if gateway.SPType.IsSet(oldArgs) {
|
|
|
|
presenceType = gateway.SPType.Get(oldArgs)
|
|
|
|
}
|
|
|
|
|
2022-02-05 10:21:56 +00:00
|
|
|
cachedStatus, ok := c.cache.GetStatus(chatID)
|
2019-11-30 00:41:22 +00:00
|
|
|
if status == "" {
|
2022-02-05 10:21:56 +00:00
|
|
|
if ok {
|
2023-08-07 00:04:49 +00:00
|
|
|
var typ string
|
|
|
|
show, status, typ = cachedStatus.Destruct()
|
|
|
|
if presenceType == "" {
|
|
|
|
presenceType = typ
|
|
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
2023-08-28 14:20:50 +00:00
|
|
|
"show": show,
|
|
|
|
"status": status,
|
2023-08-07 00:04:49 +00:00
|
|
|
"presenceType": presenceType,
|
|
|
|
}).Debug("Cached status")
|
2022-02-05 10:21:56 +00:00
|
|
|
} else if user != nil && user.Status != nil {
|
2022-02-07 16:21:43 +00:00
|
|
|
show, status, presenceType = c.userStatusToText(user.Status, chatID)
|
2023-08-07 00:04:49 +00:00
|
|
|
log.WithFields(log.Fields{
|
2023-08-28 14:20:50 +00:00
|
|
|
"show": show,
|
|
|
|
"status": status,
|
2023-08-07 00:04:49 +00:00
|
|
|
"presenceType": presenceType,
|
|
|
|
}).Debug("Status to text")
|
2019-11-30 00:41:22 +00:00
|
|
|
} else {
|
|
|
|
show, status = "chat", chat.Title
|
2019-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:04:49 +00:00
|
|
|
cacheShow := show
|
|
|
|
if presenceType == "unavailable" {
|
|
|
|
cacheShow = presenceType
|
|
|
|
}
|
|
|
|
c.cache.SetStatus(chatID, cacheShow, status)
|
2020-01-05 13:03:10 +00:00
|
|
|
|
2022-02-08 20:25:58 +00:00
|
|
|
newArgs := []args.V{
|
2019-11-29 00:51:41 +00:00
|
|
|
gateway.SPShow(show),
|
2019-11-30 00:41:22 +00:00
|
|
|
gateway.SPStatus(status),
|
2019-11-29 00:51:41 +00:00
|
|
|
gateway.SPPhoto(photo),
|
2022-02-07 16:21:43 +00:00
|
|
|
gateway.SPImmed(gateway.SPImmed.Get(oldArgs)),
|
|
|
|
}
|
2024-01-24 23:52:40 +00:00
|
|
|
newArgs = gateway.SPAppendFrom(newArgs, chatID)
|
2022-02-07 16:21:43 +00:00
|
|
|
if presenceType != "" {
|
|
|
|
newArgs = append(newArgs, gateway.SPType(presenceType))
|
|
|
|
}
|
|
|
|
|
2024-01-24 23:52:40 +00:00
|
|
|
return c.sendPresence(newArgs...)
|
2019-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 07:48:02 +00:00
|
|
|
// FormatContact retrieves a complete "full name (@usernames)" string for display
|
|
|
|
func (c *Client) FormatContact(chatID int64) string {
|
2019-12-01 13:13:45 +00:00
|
|
|
if chatID == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
chat, user, err := c.GetContactByID(chatID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "unknown contact: " + err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
var str string
|
|
|
|
if chat != nil {
|
2022-01-17 20:45:40 +00:00
|
|
|
str = fmt.Sprintf("%s (%v)", chat.Title, chat.Id)
|
2019-12-01 13:13:45 +00:00
|
|
|
} else if user != nil {
|
2023-08-01 01:25:24 +00:00
|
|
|
var usernames string
|
|
|
|
if user.Usernames != nil {
|
|
|
|
usernames = c.usernamesToString(user.Usernames.ActiveUsernames)
|
|
|
|
}
|
|
|
|
if usernames == "" {
|
|
|
|
usernames = strconv.FormatInt(user.Id, 10)
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 01:25:24 +00:00
|
|
|
str = fmt.Sprintf("%s %s (%v)", user.FirstName, user.LastName, usernames)
|
2019-12-01 13:13:45 +00:00
|
|
|
} else {
|
2019-12-04 19:29:57 +00:00
|
|
|
str = strconv.FormatInt(chatID, 10)
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
str = spaceRegex.ReplaceAllString(str, " ")
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2024-02-18 07:48:02 +00:00
|
|
|
func (c *Client) GetSenderId(sender client.MessageSender) (senderId int64) {
|
|
|
|
switch sender.MessageSenderType() {
|
|
|
|
case client.TypeMessageSenderUser:
|
|
|
|
senderUser, _ := sender.(*client.MessageSenderUser)
|
|
|
|
senderId = senderUser.UserId
|
|
|
|
case client.TypeMessageSenderChat:
|
|
|
|
senderChat, _ := sender.(*client.MessageSenderChat)
|
|
|
|
senderId = senderChat.ChatId
|
2023-03-05 08:00:53 +00:00
|
|
|
}
|
2024-02-18 07:48:02 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-05 08:00:53 +00:00
|
|
|
|
2024-02-18 07:48:02 +00:00
|
|
|
func (c *Client) getMessageSenderId(message *client.Message) (senderId int64) {
|
|
|
|
if message.SenderId != nil {
|
|
|
|
senderId = c.GetSenderId(message.SenderId)
|
|
|
|
}
|
2023-03-05 08:00:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) formatSender(message *client.Message) string {
|
2024-02-18 07:48:02 +00:00
|
|
|
return c.FormatContact(c.getMessageSenderId(message))
|
2023-03-05 08:00:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
func (c *Client) messageToStub(message *client.Message, preview bool, text string) *messageStub {
|
|
|
|
if text == "" {
|
|
|
|
text = c.messageContentToText(message.Content, message.ChatId, preview)
|
|
|
|
}
|
|
|
|
return &messageStub{
|
|
|
|
MessageId: message.Id,
|
|
|
|
ChatId: message.ChatId,
|
|
|
|
Sender: c.formatSender(message),
|
|
|
|
Date: message.Date,
|
|
|
|
Text: text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getMessageReply(message *client.Message, preview bool, noContent bool) (gatewayReply *gateway.Reply, tgReply *messageStub) {
|
2023-11-11 21:10:23 +00:00
|
|
|
if message.ReplyTo != nil && message.ReplyTo.MessageReplyToType() == client.TypeMessageReplyToMessage {
|
|
|
|
replyTo, _ := message.ReplyTo.(*client.MessageReplyToMessage)
|
2024-01-10 19:30:00 +00:00
|
|
|
var text string
|
2024-01-24 23:54:25 +00:00
|
|
|
if replyTo.Quote != nil && replyTo.Quote.Text != nil && !noContent {
|
2024-01-10 19:30:00 +00:00
|
|
|
text = formatter.Format(
|
2024-01-24 23:54:25 +00:00
|
|
|
replyTo.Quote.Text.Text,
|
|
|
|
replyTo.Quote.Text.Entities,
|
2024-01-10 19:30:00 +00:00
|
|
|
c.getFormatter(),
|
|
|
|
)
|
|
|
|
// make the whole quote fit one line
|
|
|
|
text = strings.ReplaceAll(text, "\n", " ")
|
2023-11-11 21:10:23 +00:00
|
|
|
}
|
2024-01-10 19:30:00 +00:00
|
|
|
if message.ChatId == replyTo.ChatId {
|
|
|
|
// obtain message from this chat
|
|
|
|
replyMsg, err := c.client.GetMessage(&client.GetMessageRequest{
|
|
|
|
ChatId: message.ChatId,
|
|
|
|
MessageId: replyTo.MessageId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("<error fetching message: %s>", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-11-11 21:10:23 +00:00
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
if !noContent {
|
|
|
|
tgReply = c.messageToStub(replyMsg, preview, text)
|
|
|
|
}
|
2023-03-05 08:00:53 +00:00
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
replyId, err := gateway.IdsDB.GetByTgIds(c.Session.Login, c.jid, message.ChatId, replyTo.MessageId)
|
|
|
|
if err != nil {
|
|
|
|
replyId = strconv.FormatInt(replyTo.MessageId, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
gatewayReply = &gateway.Reply{
|
2024-02-18 07:48:02 +00:00
|
|
|
Author: fmt.Sprintf("%v@%s", c.getMessageSenderId(replyMsg), gateway.Jid.Full()),
|
2024-01-10 19:30:00 +00:00
|
|
|
Id: replyId,
|
|
|
|
}
|
|
|
|
} else if !noContent {
|
|
|
|
// it's safe to assume there's no need to pass ChatId here
|
|
|
|
// as it's needed only for pin messages which are not allowed in replies
|
|
|
|
if text == "" && replyTo.Content != nil {
|
|
|
|
text = c.messageContentToText(replyTo.Content, 0, preview)
|
|
|
|
}
|
|
|
|
|
|
|
|
if text == "" {
|
|
|
|
log.Error("Empty reply from other/unknown chat")
|
|
|
|
log.Debugf("replyTo: %#v", replyTo)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tgReply = &messageStub{
|
2024-02-18 07:48:02 +00:00
|
|
|
Sender: c.formatOrigin(replyTo.Origin) + " @ " + c.FormatContact(replyTo.ChatId),
|
2024-01-10 19:30:00 +00:00
|
|
|
Date: replyTo.OriginSendDate,
|
|
|
|
Text: text,
|
|
|
|
}
|
2023-03-05 08:00:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:13:45 +00:00
|
|
|
func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, message *client.Message) string {
|
|
|
|
var err error
|
|
|
|
if message == nil {
|
|
|
|
message, err = c.client.GetMessage(&client.GetMessageRequest{
|
2022-01-17 20:45:40 +00:00
|
|
|
ChatId: chatID,
|
|
|
|
MessageId: messageID,
|
2019-12-01 13:13:45 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-30 04:56:36 +00:00
|
|
|
return fmt.Sprintf("<error fetching message: %s>", err.Error())
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if message == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
return c.formatMessageContent(preview, c.messageToStub(message, preview, ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) formatMessageContent(preview bool, message *messageStub) string {
|
2019-12-01 13:13:45 +00:00
|
|
|
var str strings.Builder
|
2022-02-05 18:54:35 +00:00
|
|
|
// add messageid and sender
|
2024-01-10 19:30:00 +00:00
|
|
|
if message.MessageId != 0 {
|
|
|
|
str.WriteString(fmt.Sprintf("%v | ", message.MessageId))
|
|
|
|
}
|
|
|
|
str.WriteString(fmt.Sprintf("%s | ", message.Sender))
|
2022-02-05 18:54:35 +00:00
|
|
|
// add date
|
2019-12-01 13:13:45 +00:00
|
|
|
if !preview {
|
2019-12-11 22:48:35 +00:00
|
|
|
str.WriteString(
|
|
|
|
time.Unix(int64(message.Date), 0).
|
|
|
|
In(c.Session.TimezoneToLocation()).
|
|
|
|
Format("02 Jan 2006 15:04:05 | "),
|
|
|
|
)
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 18:54:35 +00:00
|
|
|
// text message
|
2024-01-10 19:30:00 +00:00
|
|
|
text := message.Text
|
2019-12-01 13:13:45 +00:00
|
|
|
if text != "" {
|
|
|
|
if !preview {
|
|
|
|
str.WriteString(text)
|
|
|
|
} else {
|
|
|
|
newlinePos := strings.Index(text, newlineChar)
|
2022-02-05 18:54:35 +00:00
|
|
|
if newlinePos == -1 {
|
2019-12-01 13:13:45 +00:00
|
|
|
str.WriteString(text)
|
|
|
|
} else {
|
|
|
|
str.WriteString(text[0:newlinePos])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str.String()
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
func (c *Client) formatOrigin(origin client.MessageOrigin) string {
|
|
|
|
if origin == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
switch origin.MessageOriginType() {
|
2023-11-11 21:10:23 +00:00
|
|
|
case client.TypeMessageOriginUser:
|
2024-01-10 19:30:00 +00:00
|
|
|
originUser := origin.(*client.MessageOriginUser)
|
2024-02-18 07:48:02 +00:00
|
|
|
return c.FormatContact(originUser.SenderUserId)
|
2023-11-11 21:10:23 +00:00
|
|
|
case client.TypeMessageOriginChat:
|
2024-01-10 19:30:00 +00:00
|
|
|
originChat := origin.(*client.MessageOriginChat)
|
2022-01-10 09:29:50 +00:00
|
|
|
var signature string
|
|
|
|
if originChat.AuthorSignature != "" {
|
|
|
|
signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature)
|
|
|
|
}
|
2024-02-18 07:48:02 +00:00
|
|
|
return c.FormatContact(originChat.SenderChatId) + signature
|
2023-11-11 21:10:23 +00:00
|
|
|
case client.TypeMessageOriginHiddenUser:
|
2024-01-10 19:30:00 +00:00
|
|
|
originUser := origin.(*client.MessageOriginHiddenUser)
|
2022-01-10 09:29:50 +00:00
|
|
|
return originUser.SenderName
|
2023-11-11 21:10:23 +00:00
|
|
|
case client.TypeMessageOriginChannel:
|
2024-01-10 19:30:00 +00:00
|
|
|
channel := origin.(*client.MessageOriginChannel)
|
2022-01-10 09:29:50 +00:00
|
|
|
var signature string
|
|
|
|
if channel.AuthorSignature != "" {
|
|
|
|
signature = fmt.Sprintf(" (%s)", channel.AuthorSignature)
|
|
|
|
}
|
2024-02-18 07:48:02 +00:00
|
|
|
return c.FormatContact(channel.ChatId) + signature
|
2022-01-10 09:29:50 +00:00
|
|
|
}
|
2024-01-10 19:30:00 +00:00
|
|
|
return "Unknown origin type"
|
2022-01-10 09:29:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 01:35:13 +00:00
|
|
|
func (c *Client) formatFile(file *client.File, compact bool) (string, string) {
|
2023-06-01 20:37:38 +00:00
|
|
|
if file == nil {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
src, link := c.PermastoreFile(file, false)
|
|
|
|
|
|
|
|
if compact {
|
|
|
|
return link, link
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("%s (%v kbytes) | %s", filepath.Base(src), file.Size/1024, link), link
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PermastoreFile steals a file out of TDlib control into an independent shared directory
|
|
|
|
func (c *Client) PermastoreFile(file *client.File, clone bool) (string, string) {
|
2022-03-19 23:53:20 +00:00
|
|
|
log.Debugf("file: %#v", file)
|
2022-03-09 19:15:56 +00:00
|
|
|
if file == nil || file.Local == nil || file.Remote == nil {
|
2023-01-16 01:35:13 +00:00
|
|
|
return "", ""
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 01:42:12 +00:00
|
|
|
gateway.StorageLock.Lock()
|
|
|
|
defer gateway.StorageLock.Unlock()
|
|
|
|
|
2022-03-09 19:15:56 +00:00
|
|
|
var link string
|
|
|
|
var src string
|
|
|
|
|
|
|
|
if c.content.Path != "" && c.content.Link != "" {
|
|
|
|
src = file.Local.Path // source path
|
|
|
|
_, err := os.Stat(src)
|
|
|
|
if err != nil {
|
2022-03-19 23:53:20 +00:00
|
|
|
log.Errorf("Cannot access source file: %v", err)
|
2023-01-16 01:35:13 +00:00
|
|
|
return "", ""
|
2022-03-09 19:15:56 +00:00
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
|
2022-04-01 16:35:54 +00:00
|
|
|
size64 := uint64(file.Size)
|
2022-04-01 01:42:12 +00:00
|
|
|
c.prepareDiskSpace(size64)
|
|
|
|
|
2022-03-09 19:15:56 +00:00
|
|
|
basename := file.Remote.UniqueId + filepath.Ext(src)
|
|
|
|
dest := c.content.Path + "/" + basename // destination path
|
2022-04-01 16:35:54 +00:00
|
|
|
link = c.content.Link + "/" + basename // download link
|
2022-03-09 19:15:56 +00:00
|
|
|
|
2023-06-01 20:37:38 +00:00
|
|
|
if clone {
|
|
|
|
file, path, err := c.ForceOpenFile(file, 1)
|
|
|
|
if err == nil {
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
// mode
|
|
|
|
mode := os.FileMode(0644)
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
mode = fi.Mode().Perm()
|
|
|
|
}
|
|
|
|
|
|
|
|
// create destination
|
|
|
|
tempFile, err := os.OpenFile(dest, os.O_CREATE|os.O_EXCL|os.O_WRONLY, mode)
|
|
|
|
if err != nil {
|
|
|
|
pathErr := err.(*os.PathError)
|
|
|
|
if pathErr.Err.Error() == "file exists" {
|
|
|
|
log.Warn(err.Error())
|
|
|
|
return src, link
|
|
|
|
} else {
|
|
|
|
log.Errorf("File creation error: %v", err)
|
|
|
|
return "<ERROR>", ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer tempFile.Close()
|
|
|
|
// copy
|
|
|
|
_, err = io.Copy(tempFile, file)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("File copying error: %v", err)
|
|
|
|
return "<ERROR>", ""
|
|
|
|
}
|
|
|
|
} else if path != "" {
|
|
|
|
log.Errorf("Source file does not exist: %v", path)
|
|
|
|
return "<ERROR>", ""
|
2022-03-09 19:15:56 +00:00
|
|
|
} else {
|
2023-06-01 20:37:38 +00:00
|
|
|
log.Errorf("PHOTO: %#v", err.Error())
|
2023-01-16 01:35:13 +00:00
|
|
|
return "<ERROR>", ""
|
2022-03-09 19:15:56 +00:00
|
|
|
}
|
2023-06-01 20:37:38 +00:00
|
|
|
} else {
|
|
|
|
// move
|
|
|
|
err = os.Rename(src, dest)
|
|
|
|
if err != nil {
|
|
|
|
linkErr := err.(*os.LinkError)
|
|
|
|
if linkErr.Err.Error() == "file exists" {
|
|
|
|
log.Warn(err.Error())
|
|
|
|
} else {
|
|
|
|
log.Errorf("File moving error: %v", err)
|
|
|
|
return "<ERROR>", ""
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 19:15:56 +00:00
|
|
|
}
|
2022-04-01 01:42:12 +00:00
|
|
|
|
2022-03-09 19:15:56 +00:00
|
|
|
// chown
|
|
|
|
if c.content.User != "" {
|
|
|
|
user, err := osUser.Lookup(c.content.User)
|
|
|
|
if err == nil {
|
|
|
|
uid, err := strconv.ParseInt(user.Uid, 10, 0)
|
|
|
|
if err == nil {
|
|
|
|
err = os.Chown(dest, int(uid), -1)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Chown error: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Errorf("Broken uid: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Errorf("Wrong user name for chown: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 20:37:38 +00:00
|
|
|
// copy or move should have succeeded at this point
|
|
|
|
gateway.CachedStorageSize += size64
|
2022-03-14 21:57:03 +00:00
|
|
|
}
|
2023-06-01 20:37:38 +00:00
|
|
|
|
|
|
|
return src, link
|
2022-01-31 07:05:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 04:57:17 +00:00
|
|
|
func (c *Client) formatBantime(hours int64) int32 {
|
2022-01-17 19:58:16 +00:00
|
|
|
var until int32
|
2022-02-01 04:57:17 +00:00
|
|
|
if hours > 0 {
|
2022-01-17 19:58:16 +00:00
|
|
|
until = int32(time.Now().Unix() + hours*3600)
|
|
|
|
}
|
|
|
|
|
2022-02-01 04:57:17 +00:00
|
|
|
return until
|
2022-01-17 19:58:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 14:10:08 +00:00
|
|
|
func (c *Client) formatLocation(location *client.Location) string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"coordinates: %v,%v | https://www.google.com/maps/search/%v,%v/",
|
|
|
|
location.Latitude,
|
|
|
|
location.Longitude,
|
|
|
|
location.Latitude,
|
|
|
|
location.Longitude,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-05 18:54:35 +00:00
|
|
|
func (c *Client) messageToText(message *client.Message, preview bool) string {
|
2021-12-05 03:27:14 +00:00
|
|
|
if message.Content == nil {
|
2022-02-05 15:19:18 +00:00
|
|
|
log.Warnf("Unknown message: %#v", message)
|
|
|
|
return "<empty message>"
|
2021-12-05 03:27:14 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
return c.messageContentToText(message.Content, message.ChatId, preview)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) messageContentToText(content client.MessageContent, chatId int64, preview bool) string {
|
2023-11-16 00:38:45 +00:00
|
|
|
markupMode := c.getFormatter()
|
2024-01-10 19:30:00 +00:00
|
|
|
switch content.MessageContentType() {
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageSticker:
|
2024-01-10 19:30:00 +00:00
|
|
|
sticker, _ := content.(*client.MessageSticker)
|
2019-12-01 13:13:45 +00:00
|
|
|
return sticker.Sticker.Emoji
|
2022-02-05 15:19:18 +00:00
|
|
|
case client.TypeMessageAnimatedEmoji:
|
2024-01-10 19:30:00 +00:00
|
|
|
animatedEmoji, _ := content.(*client.MessageAnimatedEmoji)
|
2022-02-05 15:19:18 +00:00
|
|
|
return animatedEmoji.Emoji
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageBasicGroupChatCreate, client.TypeMessageSupergroupChatCreate:
|
|
|
|
return "has created chat"
|
|
|
|
case client.TypeMessageChatJoinByLink:
|
|
|
|
return "joined chat via invite link"
|
|
|
|
case client.TypeMessageChatAddMembers:
|
2024-01-10 19:30:00 +00:00
|
|
|
addMembers, _ := content.(*client.MessageChatAddMembers)
|
2019-12-01 13:13:45 +00:00
|
|
|
|
|
|
|
text := "invited "
|
2022-01-17 20:45:40 +00:00
|
|
|
if len(addMembers.MemberUserIds) > 0 {
|
2024-02-18 07:48:02 +00:00
|
|
|
text += c.FormatContact(addMembers.MemberUserIds[0])
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return text
|
|
|
|
case client.TypeMessageChatDeleteMember:
|
2024-01-10 19:30:00 +00:00
|
|
|
deleteMember, _ := content.(*client.MessageChatDeleteMember)
|
2024-02-18 07:48:02 +00:00
|
|
|
return "kicked " + c.FormatContact(deleteMember.UserId)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessagePinMessage:
|
2024-01-10 19:30:00 +00:00
|
|
|
pinMessage, _ := content.(*client.MessagePinMessage)
|
|
|
|
return "pinned message: " + c.formatMessage(chatId, pinMessage.MessageId, preview, nil)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageChatChangeTitle:
|
2024-01-10 19:30:00 +00:00
|
|
|
changeTitle, _ := content.(*client.MessageChatChangeTitle)
|
2019-12-01 13:13:45 +00:00
|
|
|
return "chat title set to: " + changeTitle.Title
|
|
|
|
case client.TypeMessageLocation:
|
2024-01-10 19:30:00 +00:00
|
|
|
location, _ := content.(*client.MessageLocation)
|
2022-02-05 14:10:08 +00:00
|
|
|
return c.formatLocation(location.Location)
|
|
|
|
case client.TypeMessageVenue:
|
2024-01-10 19:30:00 +00:00
|
|
|
venue, _ := content.(*client.MessageVenue)
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return venue.Venue.Title
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"*%s*\n%s\n%s",
|
|
|
|
venue.Venue.Title,
|
|
|
|
venue.Venue.Address,
|
|
|
|
c.formatLocation(venue.Venue.Location),
|
|
|
|
)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessagePhoto:
|
2024-01-10 19:30:00 +00:00
|
|
|
photo, _ := content.(*client.MessagePhoto)
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return photo.Caption.Text
|
|
|
|
} else {
|
|
|
|
return formatter.Format(
|
|
|
|
photo.Caption.Text,
|
2022-03-11 16:12:36 +00:00
|
|
|
photo.Caption.Entities,
|
2023-11-16 00:38:45 +00:00
|
|
|
markupMode,
|
2022-02-05 18:54:35 +00:00
|
|
|
)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageAudio:
|
2024-01-10 19:30:00 +00:00
|
|
|
audio, _ := content.(*client.MessageAudio)
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return audio.Caption.Text
|
|
|
|
} else {
|
|
|
|
return formatter.Format(
|
|
|
|
audio.Caption.Text,
|
2022-03-11 16:12:36 +00:00
|
|
|
audio.Caption.Entities,
|
2023-11-16 00:38:45 +00:00
|
|
|
markupMode,
|
2022-02-05 18:54:35 +00:00
|
|
|
)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageVideo:
|
2024-01-10 19:30:00 +00:00
|
|
|
video, _ := content.(*client.MessageVideo)
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return video.Caption.Text
|
|
|
|
} else {
|
|
|
|
return formatter.Format(
|
|
|
|
video.Caption.Text,
|
2022-03-11 16:12:36 +00:00
|
|
|
video.Caption.Entities,
|
2023-11-16 00:38:45 +00:00
|
|
|
markupMode,
|
2022-02-05 18:54:35 +00:00
|
|
|
)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageDocument:
|
2024-01-10 19:30:00 +00:00
|
|
|
document, _ := content.(*client.MessageDocument)
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return document.Caption.Text
|
|
|
|
} else {
|
|
|
|
return formatter.Format(
|
|
|
|
document.Caption.Text,
|
2022-03-11 16:12:36 +00:00
|
|
|
document.Caption.Entities,
|
2023-11-16 00:38:45 +00:00
|
|
|
markupMode,
|
2022-02-05 18:54:35 +00:00
|
|
|
)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageText:
|
2024-01-10 19:30:00 +00:00
|
|
|
text, _ := content.(*client.MessageText)
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return text.Text.Text
|
|
|
|
} else {
|
|
|
|
return formatter.Format(
|
|
|
|
text.Text.Text,
|
2022-03-11 16:12:36 +00:00
|
|
|
text.Text.Entities,
|
2023-11-16 00:38:45 +00:00
|
|
|
markupMode,
|
2022-02-05 18:54:35 +00:00
|
|
|
)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageVoiceNote:
|
2024-01-10 19:30:00 +00:00
|
|
|
voice, _ := content.(*client.MessageVoiceNote)
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return voice.Caption.Text
|
|
|
|
} else {
|
|
|
|
return formatter.Format(
|
|
|
|
voice.Caption.Text,
|
2022-03-11 16:12:36 +00:00
|
|
|
voice.Caption.Entities,
|
2023-11-16 00:38:45 +00:00
|
|
|
markupMode,
|
2022-02-05 18:54:35 +00:00
|
|
|
)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageVideoNote:
|
|
|
|
return ""
|
|
|
|
case client.TypeMessageAnimation:
|
2024-01-10 19:30:00 +00:00
|
|
|
animation, _ := content.(*client.MessageAnimation)
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return animation.Caption.Text
|
|
|
|
} else {
|
|
|
|
return formatter.Format(
|
|
|
|
animation.Caption.Text,
|
2022-03-11 16:12:36 +00:00
|
|
|
animation.Caption.Entities,
|
2023-11-16 00:38:45 +00:00
|
|
|
markupMode,
|
2022-02-05 18:54:35 +00:00
|
|
|
)
|
|
|
|
}
|
2022-02-05 14:49:21 +00:00
|
|
|
case client.TypeMessageContact:
|
2024-01-10 19:30:00 +00:00
|
|
|
contact, _ := content.(*client.MessageContact)
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return contact.Contact.FirstName + " " + contact.Contact.LastName
|
|
|
|
} else {
|
|
|
|
var jid string
|
|
|
|
if contact.Contact.UserId != 0 {
|
|
|
|
jid = fmt.Sprintf("%v@%s", contact.Contact.UserId, gateway.Jid.Bare())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"*%s %s*\n%s\n%s\n%s",
|
|
|
|
contact.Contact.FirstName,
|
|
|
|
contact.Contact.LastName,
|
|
|
|
contact.Contact.PhoneNumber,
|
|
|
|
contact.Contact.Vcard,
|
|
|
|
jid,
|
|
|
|
)
|
2022-02-05 14:49:21 +00:00
|
|
|
}
|
2022-02-05 15:46:24 +00:00
|
|
|
case client.TypeMessageDice:
|
2024-01-10 19:30:00 +00:00
|
|
|
dice, _ := content.(*client.MessageDice)
|
2022-02-05 15:46:24 +00:00
|
|
|
return fmt.Sprintf("%s 1d6: [%v]", dice.Emoji, dice.Value)
|
2022-02-05 16:33:53 +00:00
|
|
|
case client.TypeMessagePoll:
|
2024-01-10 19:30:00 +00:00
|
|
|
poll, _ := content.(*client.MessagePoll)
|
2022-02-05 16:33:53 +00:00
|
|
|
|
2022-02-05 18:54:35 +00:00
|
|
|
if preview {
|
|
|
|
return poll.Poll.Question
|
|
|
|
} else {
|
|
|
|
rows := []string{}
|
|
|
|
rows = append(rows, fmt.Sprintf("*%s*", poll.Poll.Question))
|
|
|
|
for _, option := range poll.Poll.Options {
|
|
|
|
var tick string
|
|
|
|
if option.IsChosen {
|
|
|
|
tick = "x"
|
|
|
|
} else {
|
|
|
|
tick = " "
|
|
|
|
}
|
|
|
|
rows = append(rows, fmt.Sprintf(
|
|
|
|
"[%s] %s | %v%% | %v vote",
|
|
|
|
tick,
|
|
|
|
option.Text,
|
|
|
|
option.VotePercentage,
|
|
|
|
option.VoterCount,
|
|
|
|
))
|
2022-02-05 16:33:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 18:54:35 +00:00
|
|
|
return strings.Join(rows, "\n")
|
|
|
|
}
|
2023-08-01 01:25:24 +00:00
|
|
|
case client.TypeMessageChatSetMessageAutoDeleteTime:
|
2024-01-10 19:30:00 +00:00
|
|
|
ttl, _ := content.(*client.MessageChatSetMessageAutoDeleteTime)
|
2024-02-18 07:48:02 +00:00
|
|
|
name := c.FormatContact(ttl.FromUserId)
|
2023-08-01 01:25:24 +00:00
|
|
|
if name == "" {
|
|
|
|
if ttl.MessageAutoDeleteTime == 0 {
|
|
|
|
return "The self-destruct timer was disabled"
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("The self-destruct timer was set to %v seconds", ttl.MessageAutoDeleteTime)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ttl.MessageAutoDeleteTime == 0 {
|
|
|
|
return fmt.Sprintf("%s disabled the self-destruct timer", name)
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("%s set the self-destruct timer to %v seconds", name, ttl.MessageAutoDeleteTime)
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
return fmt.Sprintf("unknown message (%s)", content.MessageContentType())
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 21:57:03 +00:00
|
|
|
func (c *Client) contentToFile(content client.MessageContent) (*client.File, *client.File) {
|
2021-12-05 03:27:14 +00:00
|
|
|
if content == nil {
|
2022-03-14 21:57:03 +00:00
|
|
|
return nil, nil
|
2021-12-05 03:27:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 13:13:45 +00:00
|
|
|
switch content.MessageContentType() {
|
|
|
|
case client.TypeMessageSticker:
|
|
|
|
sticker, _ := content.(*client.MessageSticker)
|
2022-03-14 21:57:03 +00:00
|
|
|
file := sticker.Sticker.Sticker
|
2023-08-01 02:00:58 +00:00
|
|
|
if sticker.Sticker.Format.StickerFormatType() == client.TypeStickerFormatTgs && sticker.Sticker.Thumbnail != nil && sticker.Sticker.Thumbnail.File != nil {
|
2022-03-14 21:57:03 +00:00
|
|
|
file = sticker.Sticker.Thumbnail.File
|
|
|
|
}
|
|
|
|
return file, nil
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageVoiceNote:
|
|
|
|
voice, _ := content.(*client.MessageVoiceNote)
|
2022-03-14 21:57:03 +00:00
|
|
|
return voice.VoiceNote.Voice, nil
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageVideoNote:
|
|
|
|
video, _ := content.(*client.MessageVideoNote)
|
2022-03-14 21:57:03 +00:00
|
|
|
var preview *client.File
|
|
|
|
if video.VideoNote.Thumbnail != nil {
|
|
|
|
preview = video.VideoNote.Thumbnail.File
|
|
|
|
}
|
|
|
|
return video.VideoNote.Video, preview
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageAnimation:
|
|
|
|
animation, _ := content.(*client.MessageAnimation)
|
2022-03-14 21:57:03 +00:00
|
|
|
var preview *client.File
|
|
|
|
if animation.Animation.Thumbnail != nil {
|
|
|
|
preview = animation.Animation.Thumbnail.File
|
|
|
|
}
|
|
|
|
return animation.Animation.Animation, preview
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessagePhoto:
|
|
|
|
photo, _ := content.(*client.MessagePhoto)
|
|
|
|
sizes := photo.Photo.Sizes
|
2022-02-04 15:56:34 +00:00
|
|
|
if len(sizes) >= 1 {
|
2019-12-02 16:25:55 +00:00
|
|
|
file := sizes[len(sizes)-1].Photo
|
2022-03-14 21:57:03 +00:00
|
|
|
return file, nil
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
2022-03-14 21:57:03 +00:00
|
|
|
return nil, nil
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageAudio:
|
|
|
|
audio, _ := content.(*client.MessageAudio)
|
2022-03-14 21:57:03 +00:00
|
|
|
var preview *client.File
|
|
|
|
if audio.Audio.AlbumCoverThumbnail != nil {
|
|
|
|
preview = audio.Audio.AlbumCoverThumbnail.File
|
|
|
|
}
|
|
|
|
return audio.Audio.Audio, preview
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageVideo:
|
|
|
|
video, _ := content.(*client.MessageVideo)
|
2022-03-14 21:57:03 +00:00
|
|
|
var preview *client.File
|
|
|
|
if video.Video.Thumbnail != nil {
|
|
|
|
preview = video.Video.Thumbnail.File
|
|
|
|
}
|
|
|
|
return video.Video.Video, preview
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageDocument:
|
|
|
|
document, _ := content.(*client.MessageDocument)
|
2022-03-14 21:57:03 +00:00
|
|
|
var preview *client.File
|
|
|
|
if document.Document.Thumbnail != nil {
|
|
|
|
preview = document.Document.Thumbnail.File
|
|
|
|
}
|
|
|
|
return document.Document.Document, preview
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 21:57:03 +00:00
|
|
|
return nil, nil
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 12:51:19 +00:00
|
|
|
func (c *Client) countCharsInLines(lines *[]string) (count int) {
|
|
|
|
for _, line := range *lines {
|
2023-10-29 12:47:35 +00:00
|
|
|
count += utf8.RuneCountInString(line)
|
2023-03-08 12:51:19 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-29 09:28:15 +00:00
|
|
|
func (c *Client) isCarbonsEnabled() bool {
|
|
|
|
return gateway.MessageOutgoingPermissionVersion > 0 && c.Session.Carbons
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string) (string, *gateway.Reply) {
|
2023-03-19 00:13:00 +00:00
|
|
|
isPM, err := c.IsPM(message.ChatId)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not determine if chat is PM: %v", err)
|
|
|
|
}
|
2023-08-02 17:53:34 +00:00
|
|
|
// with carbons, hide for all messages in PM and only for outgoing in group chats
|
2024-01-29 09:28:15 +00:00
|
|
|
hideSender := c.isCarbonsEnabled() && (message.IsOutgoing || isPM)
|
2023-03-19 00:13:00 +00:00
|
|
|
|
2019-12-01 13:13:45 +00:00
|
|
|
prefix := []string{}
|
|
|
|
// message direction
|
|
|
|
var directionChar string
|
2023-08-02 17:53:34 +00:00
|
|
|
if !hideSender {
|
2023-03-19 00:13:00 +00:00
|
|
|
if c.Session.AsciiArrows {
|
|
|
|
if message.IsOutgoing {
|
|
|
|
directionChar = "> "
|
|
|
|
} else {
|
|
|
|
directionChar = "< "
|
|
|
|
}
|
2022-02-14 00:05:59 +00:00
|
|
|
} else {
|
2023-03-19 00:13:00 +00:00
|
|
|
if message.IsOutgoing {
|
|
|
|
directionChar = "➡ "
|
|
|
|
} else {
|
|
|
|
directionChar = "⬅ "
|
|
|
|
}
|
2022-02-14 00:05:59 +00:00
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
2023-03-19 00:13:00 +00:00
|
|
|
if !isPM || !c.Session.HideIds {
|
|
|
|
prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10))
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
// show sender in group chats
|
2023-08-02 17:53:34 +00:00
|
|
|
if !hideSender {
|
2023-03-19 00:13:00 +00:00
|
|
|
sender := c.formatSender(message)
|
|
|
|
if sender != "" {
|
|
|
|
prefix = append(prefix, sender)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
2024-01-10 19:30:00 +00:00
|
|
|
|
2019-12-01 13:13:45 +00:00
|
|
|
// reply to
|
2024-01-10 19:30:00 +00:00
|
|
|
preview := true
|
|
|
|
reply, tgReply := c.getMessageReply(message, preview, false)
|
|
|
|
|
|
|
|
if tgReply != nil {
|
|
|
|
var replyStart, replyEnd int
|
|
|
|
|
2023-03-19 00:13:00 +00:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
replyStart = c.countCharsInLines(&prefix) + (len(prefix)-1)*len(messageHeaderSeparator)
|
|
|
|
}
|
2024-01-10 19:30:00 +00:00
|
|
|
|
|
|
|
replyLine := "reply: " + c.formatMessageContent(preview, tgReply)
|
2023-03-08 12:51:19 +00:00
|
|
|
prefix = append(prefix, replyLine)
|
2024-01-10 19:30:00 +00:00
|
|
|
|
2023-10-29 12:47:35 +00:00
|
|
|
replyEnd = replyStart + utf8.RuneCountInString(replyLine)
|
2023-03-19 00:13:00 +00:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
replyEnd += len(messageHeaderSeparator)
|
|
|
|
}
|
2024-01-10 19:30:00 +00:00
|
|
|
|
|
|
|
if reply != nil {
|
|
|
|
reply.Start = uint64(replyStart)
|
|
|
|
reply.End = uint64(replyEnd)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
2024-01-10 19:30:00 +00:00
|
|
|
|
2022-01-10 09:29:50 +00:00
|
|
|
if message.ForwardInfo != nil {
|
2024-01-10 19:30:00 +00:00
|
|
|
prefix = append(prefix, "fwd: "+c.formatOrigin(message.ForwardInfo.Origin))
|
2022-01-10 09:29:50 +00:00
|
|
|
}
|
2022-03-14 21:57:03 +00:00
|
|
|
// preview
|
|
|
|
if previewString != "" {
|
|
|
|
prefix = append(prefix, "preview: "+previewString)
|
|
|
|
}
|
2022-01-10 09:29:50 +00:00
|
|
|
// file
|
2019-12-01 13:13:45 +00:00
|
|
|
if fileString != "" {
|
|
|
|
prefix = append(prefix, "file: "+fileString)
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
return strings.Join(prefix, messageHeaderSeparator), reply
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 21:57:03 +00:00
|
|
|
func (c *Client) ensureDownloadFile(file *client.File) *client.File {
|
2022-04-01 01:42:12 +00:00
|
|
|
gateway.StorageLock.Lock()
|
|
|
|
defer gateway.StorageLock.Unlock()
|
|
|
|
|
2022-03-14 21:57:03 +00:00
|
|
|
if file != nil {
|
2022-04-01 01:42:12 +00:00
|
|
|
c.prepareDiskSpace(uint64(file.Size))
|
|
|
|
|
2022-03-14 21:57:03 +00:00
|
|
|
newFile, err := c.DownloadFile(file.Id, 1, true)
|
|
|
|
if err == nil {
|
|
|
|
return newFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
2022-03-12 17:25:53 +00:00
|
|
|
// ProcessIncomingMessage transfers a message to XMPP side and marks it as read on Telegram side
|
2023-08-02 20:41:18 +00:00
|
|
|
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
|
2024-01-29 09:28:15 +00:00
|
|
|
isCarbon := c.isCarbonsEnabled() && message.IsOutgoing
|
2023-08-02 20:41:18 +00:00
|
|
|
jids := c.getCarbonFullJids(isCarbon, "")
|
2023-07-09 03:52:30 +00:00
|
|
|
|
|
|
|
var text, oob, auxText string
|
2024-01-10 19:30:00 +00:00
|
|
|
var reply *gateway.Reply
|
|
|
|
var replyObtained bool
|
2023-03-05 08:00:53 +00:00
|
|
|
|
2022-03-12 17:25:53 +00:00
|
|
|
content := message.Content
|
|
|
|
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
|
|
|
|
chat, err := c.client.GetChat(&client.GetChatRequest{
|
|
|
|
ChatId: chatId,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
c.cache.SetChat(chatId, chat)
|
|
|
|
go c.ProcessStatusUpdate(chatId, "", "", gateway.SPImmed(true))
|
|
|
|
text = "<Chat photo has changed>"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
text = c.messageToText(message, false)
|
|
|
|
|
|
|
|
// OTR support (I do not know why would you need it, seriously)
|
2023-01-16 13:02:41 +00:00
|
|
|
if !(strings.HasPrefix(text, "?OTR") || (c.Session.RawMessages && !c.Session.OOBMode)) {
|
2022-03-14 21:57:03 +00:00
|
|
|
file, preview := c.contentToFile(content)
|
|
|
|
|
|
|
|
// download file and preview (if present)
|
|
|
|
file = c.ensureDownloadFile(file)
|
|
|
|
preview = c.ensureDownloadFile(preview)
|
|
|
|
|
2023-01-16 01:35:13 +00:00
|
|
|
previewName, _ := c.formatFile(preview, true)
|
|
|
|
fileName, link := c.formatFile(file, false)
|
2023-01-16 13:02:41 +00:00
|
|
|
|
2023-01-16 01:35:13 +00:00
|
|
|
oob = link
|
2023-01-16 13:02:41 +00:00
|
|
|
if c.Session.OOBMode && oob != "" {
|
2023-01-18 02:57:38 +00:00
|
|
|
typ := message.Content.MessageContentType()
|
|
|
|
if typ != client.TypeMessageSticker {
|
|
|
|
auxText = text
|
|
|
|
}
|
2023-01-16 13:02:41 +00:00
|
|
|
text = oob
|
|
|
|
} else if !c.Session.RawMessages {
|
2023-03-08 12:51:19 +00:00
|
|
|
var newText strings.Builder
|
|
|
|
|
2024-01-10 19:30:00 +00:00
|
|
|
prefix, prefixReply := c.messageToPrefix(message, previewName, fileName)
|
|
|
|
reply = prefixReply
|
|
|
|
replyObtained = true
|
2023-03-08 12:51:19 +00:00
|
|
|
newText.WriteString(prefix)
|
|
|
|
|
2023-01-16 13:02:41 +00:00
|
|
|
if text != "" {
|
|
|
|
// \n if it is groupchat and message is not empty
|
2023-03-19 00:13:00 +00:00
|
|
|
if prefix != "" {
|
|
|
|
if chatId < 0 {
|
|
|
|
newText.WriteString("\n")
|
|
|
|
} else if chatId > 0 {
|
|
|
|
newText.WriteString(" | ")
|
|
|
|
}
|
2023-01-16 13:02:41 +00:00
|
|
|
}
|
2022-03-12 17:25:53 +00:00
|
|
|
|
2023-03-08 12:51:19 +00:00
|
|
|
newText.WriteString(text)
|
2023-01-16 13:02:41 +00:00
|
|
|
}
|
2023-03-08 12:51:19 +00:00
|
|
|
text = newText.String()
|
2022-03-12 17:25:53 +00:00
|
|
|
}
|
2023-01-16 01:35:13 +00:00
|
|
|
}
|
2022-03-12 17:25:53 +00:00
|
|
|
}
|
2024-01-10 19:30:00 +00:00
|
|
|
if !replyObtained {
|
|
|
|
reply, _ = c.getMessageReply(message, false, true)
|
|
|
|
}
|
2022-03-12 17:25:53 +00:00
|
|
|
|
|
|
|
// mark message as read
|
2024-01-27 11:13:45 +00:00
|
|
|
if !c.Session.Receipts {
|
|
|
|
c.MarkAsRead(chatId, message.Id)
|
|
|
|
}
|
2023-03-18 21:43:11 +00:00
|
|
|
|
2022-03-12 17:25:53 +00:00
|
|
|
// forward message to XMPP
|
2023-03-04 02:41:45 +00:00
|
|
|
sId := strconv.FormatInt(message.Id, 10)
|
|
|
|
sChatId := strconv.FormatInt(chatId, 10)
|
2023-03-18 21:43:11 +00:00
|
|
|
|
|
|
|
for _, jid := range jids {
|
2024-01-29 09:28:15 +00:00
|
|
|
gateway.SendMessageWithOOB(jid, sChatId, text, sId, c.xmpp, reply, oob, "", isCarbon, c.Session.Receipts)
|
2023-03-18 21:43:11 +00:00
|
|
|
if auxText != "" {
|
2024-01-29 09:28:15 +00:00
|
|
|
gateway.SendMessage(jid, sChatId, auxText, sId, c.xmpp, reply, "", isCarbon, c.Session.Receipts)
|
2023-03-18 21:43:11 +00:00
|
|
|
}
|
2023-01-18 02:57:38 +00:00
|
|
|
}
|
2024-01-29 09:28:15 +00:00
|
|
|
c.UpdateLastChatMessageId(chatId, sId)
|
2022-03-12 17:25:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 11:13:45 +00:00
|
|
|
// MarkAsRead marks a message as read
|
|
|
|
func (c *Client) MarkAsRead(chatId, messageId int64) {
|
|
|
|
c.client.ViewMessages(&client.ViewMessagesRequest{
|
|
|
|
ChatId: chatId,
|
|
|
|
MessageIds: []int64{messageId},
|
|
|
|
ForceRead: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-04 00:25:00 +00:00
|
|
|
// PrepareMessageContent creates a simple text message
|
|
|
|
func (c *Client) PrepareOutgoingMessageContent(text string) client.InputMessageContent {
|
|
|
|
return c.prepareOutgoingMessageContent(text, nil)
|
|
|
|
}
|
|
|
|
|
2023-06-05 08:22:13 +00:00
|
|
|
// ProcessOutgoingMessage executes commands or sends messages to mapped chats, returns message id
|
|
|
|
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string, replyId int64, replaceId int64) int64 {
|
2022-01-08 10:59:57 +00:00
|
|
|
if !c.Online() {
|
|
|
|
// we're offline
|
2023-06-05 08:22:13 +00:00
|
|
|
return 0
|
2022-01-08 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 08:22:13 +00:00
|
|
|
if replaceId == 0 && (strings.HasPrefix(text, "/") || strings.HasPrefix(text, "!")) {
|
2022-01-08 10:59:57 +00:00
|
|
|
// try to execute commands
|
2024-02-18 09:36:23 +00:00
|
|
|
response, isCommand, _ := c.ProcessChatCommand(chatID, text)
|
2019-12-03 00:32:53 +00:00
|
|
|
if response != "" {
|
2023-06-04 00:25:00 +00:00
|
|
|
c.returnMessage(returnJid, chatID, response)
|
2019-12-03 00:32:53 +00:00
|
|
|
}
|
2019-12-03 16:48:41 +00:00
|
|
|
// do not send on success
|
2019-12-03 00:32:53 +00:00
|
|
|
if isCommand {
|
2023-06-05 08:22:13 +00:00
|
|
|
return 0
|
2019-12-03 00:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-03 16:48:41 +00:00
|
|
|
|
2022-01-08 10:59:57 +00:00
|
|
|
log.Warnf("Sending message to chat %v", chatID)
|
2019-12-03 16:48:41 +00:00
|
|
|
|
2022-01-08 10:59:57 +00:00
|
|
|
// quotations
|
|
|
|
var reply int64
|
2023-06-05 08:22:13 +00:00
|
|
|
if replaceId == 0 && replyId == 0 {
|
2023-03-14 21:16:02 +00:00
|
|
|
replySlice := replyRegex.FindStringSubmatch(text)
|
|
|
|
if len(replySlice) > 1 {
|
|
|
|
reply, _ = strconv.ParseInt(replySlice[1], 10, 64)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reply = replyId
|
2022-01-08 10:59:57 +00:00
|
|
|
}
|
2019-12-03 16:48:41 +00:00
|
|
|
|
2022-01-08 10:59:57 +00:00
|
|
|
// attach a file
|
2022-05-15 16:59:54 +00:00
|
|
|
var file *client.InputFileLocal
|
2023-06-04 00:25:00 +00:00
|
|
|
if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
|
2022-05-15 16:59:54 +00:00
|
|
|
response, err := http.Get(text)
|
|
|
|
if err != nil {
|
2023-06-04 00:25:00 +00:00
|
|
|
c.returnError(returnJid, chatID, "Failed to fetch the uploaded file", err)
|
2022-05-15 16:59:54 +00:00
|
|
|
}
|
|
|
|
if response != nil && response.Body != nil {
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != 200 {
|
2023-06-04 00:25:00 +00:00
|
|
|
c.returnMessage(returnJid, chatID, fmt.Sprintf("Received status code %v", response.StatusCode))
|
2022-05-15 16:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tempDir, err := ioutil.TempDir("", "telegabber-*")
|
|
|
|
if err != nil {
|
2023-06-04 00:25:00 +00:00
|
|
|
c.returnError(returnJid, chatID, "Failed to create a temporary directory", err)
|
2022-05-15 16:59:54 +00:00
|
|
|
}
|
|
|
|
tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(text)))
|
|
|
|
if err != nil {
|
2023-06-04 00:25:00 +00:00
|
|
|
c.returnError(returnJid, chatID, "Failed to create a temporary file", err)
|
2022-05-15 16:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(tempFile, response.Body)
|
|
|
|
if err != nil {
|
2023-06-04 00:25:00 +00:00
|
|
|
c.returnError(returnJid, chatID, "Failed to write a temporary file", err)
|
2022-05-15 16:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file = &client.InputFileLocal{
|
|
|
|
Path: tempFile.Name(),
|
|
|
|
}
|
2019-12-07 16:37:14 +00:00
|
|
|
}
|
2022-01-08 10:59:57 +00:00
|
|
|
}
|
2019-12-07 16:37:14 +00:00
|
|
|
|
2022-01-08 10:59:57 +00:00
|
|
|
// remove first line from text
|
2023-03-14 21:16:02 +00:00
|
|
|
if file != nil || (reply != 0 && replyId == 0) {
|
2022-01-08 10:59:57 +00:00
|
|
|
newlinePos := strings.Index(text, newlineChar)
|
|
|
|
if newlinePos != -1 {
|
|
|
|
text = text[newlinePos+1:]
|
2022-05-15 16:59:54 +00:00
|
|
|
} else {
|
|
|
|
text = ""
|
2019-12-07 16:37:14 +00:00
|
|
|
}
|
2022-01-08 10:59:57 +00:00
|
|
|
}
|
2019-12-07 16:37:14 +00:00
|
|
|
|
2023-06-04 00:25:00 +00:00
|
|
|
content := c.prepareOutgoingMessageContent(text, file)
|
|
|
|
|
2023-06-05 08:22:13 +00:00
|
|
|
if replaceId != 0 {
|
|
|
|
tgMessage, err := c.client.EditMessageText(&client.EditMessageTextRequest{
|
|
|
|
ChatId: chatID,
|
|
|
|
MessageId: replaceId,
|
|
|
|
InputMessageContent: content,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.returnError(returnJid, chatID, "Not edited", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return tgMessage.Id
|
|
|
|
}
|
|
|
|
|
2023-06-04 00:25:00 +00:00
|
|
|
tgMessage, err := c.client.SendMessage(&client.SendMessageRequest{
|
|
|
|
ChatId: chatID,
|
2023-11-11 21:10:23 +00:00
|
|
|
ReplyTo: &client.InputMessageReplyToMessage{MessageId: reply},
|
2023-06-04 00:25:00 +00:00
|
|
|
InputMessageContent: content,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-06-05 08:22:13 +00:00
|
|
|
c.returnError(returnJid, chatID, "Not sent", err)
|
|
|
|
return 0
|
2023-06-04 00:25:00 +00:00
|
|
|
}
|
2023-06-05 08:22:13 +00:00
|
|
|
return tgMessage.Id
|
2023-06-04 00:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) returnMessage(returnJid string, chatID int64, text string) {
|
|
|
|
gateway.SendTextMessage(returnJid, strconv.FormatInt(chatID, 10), text, c.xmpp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) returnError(returnJid string, chatID int64, msg string, err error) {
|
|
|
|
c.returnMessage(returnJid, chatID, fmt.Sprintf("%s: %s", msg, err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) prepareOutgoingMessageContent(text string, file *client.InputFileLocal) client.InputMessageContent {
|
2022-01-08 10:59:57 +00:00
|
|
|
formattedText := &client.FormattedText{
|
|
|
|
Text: text,
|
|
|
|
}
|
2019-12-07 16:37:14 +00:00
|
|
|
|
2023-06-04 00:25:00 +00:00
|
|
|
var content client.InputMessageContent
|
2022-01-08 10:59:57 +00:00
|
|
|
if file != nil {
|
|
|
|
// we can try to send a document
|
2023-06-04 00:25:00 +00:00
|
|
|
content = &client.InputMessageDocument{
|
2022-01-08 10:59:57 +00:00
|
|
|
Document: file,
|
|
|
|
Caption: formattedText,
|
2019-12-07 16:37:14 +00:00
|
|
|
}
|
2022-01-08 10:59:57 +00:00
|
|
|
} else {
|
|
|
|
// compile our message
|
2023-06-04 00:25:00 +00:00
|
|
|
content = &client.InputMessageText{
|
2022-01-08 10:59:57 +00:00
|
|
|
Text: formattedText,
|
2019-12-07 16:37:14 +00:00
|
|
|
}
|
2022-01-08 10:59:57 +00:00
|
|
|
}
|
2023-06-04 00:25:00 +00:00
|
|
|
return content
|
2019-11-29 00:51:41 +00:00
|
|
|
}
|
2020-01-05 13:03:10 +00:00
|
|
|
|
|
|
|
// StatusesRange proxies the following function from unexported cache
|
|
|
|
func (c *Client) StatusesRange() chan *cache.Status {
|
|
|
|
return c.cache.StatusesRange()
|
|
|
|
}
|
2022-01-03 03:54:13 +00:00
|
|
|
|
|
|
|
func (c *Client) addResource(resource string) {
|
|
|
|
if resource == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.locks.resourcesLock.Lock()
|
|
|
|
defer c.locks.resourcesLock.Unlock()
|
|
|
|
|
|
|
|
c.resources[resource] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) deleteResource(resource string) {
|
|
|
|
c.locks.resourcesLock.Lock()
|
|
|
|
defer c.locks.resourcesLock.Unlock()
|
|
|
|
|
|
|
|
if _, ok := c.resources[resource]; ok {
|
|
|
|
delete(c.resources, resource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 21:43:11 +00:00
|
|
|
func (c *Client) resourcesRange() chan string {
|
|
|
|
c.locks.resourcesLock.Lock()
|
|
|
|
|
|
|
|
resourceChan := make(chan string, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
c.locks.resourcesLock.Unlock()
|
|
|
|
close(resourceChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for resource := range c.resources {
|
|
|
|
resourceChan <- resource
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return resourceChan
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:04:22 +00:00
|
|
|
// resend statuses to (to another resource, for example)
|
|
|
|
func (c *Client) roster(resource string) {
|
2023-07-09 03:52:30 +00:00
|
|
|
c.locks.resourcesLock.Lock()
|
2022-01-03 03:54:13 +00:00
|
|
|
if _, ok := c.resources[resource]; ok {
|
2023-07-09 03:52:30 +00:00
|
|
|
c.locks.resourcesLock.Unlock()
|
2022-01-05 21:04:22 +00:00
|
|
|
return // we know it
|
2022-01-03 03:54:13 +00:00
|
|
|
}
|
2023-07-09 03:52:30 +00:00
|
|
|
c.locks.resourcesLock.Unlock()
|
2022-01-03 03:54:13 +00:00
|
|
|
|
2022-01-05 21:04:22 +00:00
|
|
|
log.Warnf("Sending roster for %v", resource)
|
2022-01-03 03:54:13 +00:00
|
|
|
|
|
|
|
for _, chat := range c.cache.ChatsKeys() {
|
|
|
|
c.ProcessStatusUpdate(chat, "", "")
|
|
|
|
}
|
|
|
|
|
2024-01-29 10:00:42 +00:00
|
|
|
c.sendPresence(gateway.SPStatus("Logged in as: " + c.Session.Login))
|
2022-01-05 21:04:22 +00:00
|
|
|
|
2022-01-03 03:54:13 +00:00
|
|
|
c.addResource(resource)
|
|
|
|
}
|
2022-01-06 12:13:57 +00:00
|
|
|
|
2022-02-03 20:25:45 +00:00
|
|
|
// get last messages from specified chat
|
2023-08-01 01:25:24 +00:00
|
|
|
func (c *Client) getLastMessages(id int64, query string, from int64, count int32) (*client.FoundChatMessages, error) {
|
2022-01-06 12:13:57 +00:00
|
|
|
return c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
|
2022-01-17 20:45:40 +00:00
|
|
|
ChatId: id,
|
|
|
|
Query: query,
|
|
|
|
SenderId: &client.MessageSenderUser{UserId: from},
|
|
|
|
Filter: &client.SearchMessagesFilterEmpty{},
|
|
|
|
Limit: count,
|
2022-01-06 12:13:57 +00:00
|
|
|
})
|
|
|
|
}
|
2022-01-27 02:09:19 +00:00
|
|
|
|
|
|
|
// DownloadFile actually obtains a file by id given by TDlib
|
|
|
|
func (c *Client) DownloadFile(id int32, priority int32, synchronous bool) (*client.File, error) {
|
|
|
|
return c.client.DownloadFile(&client.DownloadFileRequest{
|
|
|
|
FileId: id,
|
|
|
|
Priority: priority,
|
|
|
|
Synchronous: synchronous,
|
|
|
|
})
|
|
|
|
}
|
2022-02-06 13:25:46 +00:00
|
|
|
|
2023-06-01 20:37:38 +00:00
|
|
|
// ForceOpenFile reliably obtains a file if possible
|
|
|
|
func (c *Client) ForceOpenFile(tgFile *client.File, priority int32) (*os.File, string, error) {
|
|
|
|
if tgFile == nil {
|
|
|
|
return nil, "", errors.New("File not found")
|
2022-06-25 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 20:37:38 +00:00
|
|
|
path := tgFile.Local.Path
|
2022-06-25 20:58:15 +00:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err == nil {
|
|
|
|
return file, path, nil
|
2022-06-30 00:33:51 +00:00
|
|
|
} else
|
2022-06-25 20:58:15 +00:00
|
|
|
// obtain the photo right now if still not downloaded
|
2023-06-01 20:37:38 +00:00
|
|
|
if !tgFile.Local.IsDownloadingCompleted {
|
|
|
|
tdFile, tdErr := c.DownloadFile(tgFile.Id, priority, true)
|
2022-06-25 20:58:15 +00:00
|
|
|
if tdErr == nil {
|
|
|
|
path = tdFile.Local.Path
|
|
|
|
file, err = os.Open(path)
|
|
|
|
return file, path, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// give up
|
|
|
|
return nil, path, err
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:56:27 +00:00
|
|
|
// GetChatDescription obtains bio or description according to the chat type
|
|
|
|
func (c *Client) GetChatDescription(chat *client.Chat) string {
|
|
|
|
chatType := chat.Type.ChatTypeType()
|
|
|
|
if chatType == client.TypeChatTypePrivate {
|
|
|
|
privateType, _ := chat.Type.(*client.ChatTypePrivate)
|
|
|
|
fullInfo, err := c.client.GetUserFullInfo(&client.GetUserFullInfoRequest{
|
|
|
|
UserId: privateType.UserId,
|
|
|
|
})
|
|
|
|
if err == nil {
|
2023-08-01 01:25:24 +00:00
|
|
|
if fullInfo.Bio != nil && fullInfo.Bio.Text != "" {
|
|
|
|
return formatter.Format(
|
|
|
|
fullInfo.Bio.Text,
|
|
|
|
fullInfo.Bio.Entities,
|
|
|
|
c.getFormatter(),
|
|
|
|
)
|
|
|
|
} else if fullInfo.BotInfo != nil {
|
|
|
|
if fullInfo.BotInfo.ShortDescription != "" {
|
|
|
|
return fullInfo.BotInfo.ShortDescription
|
|
|
|
} else {
|
|
|
|
return fullInfo.BotInfo.Description
|
|
|
|
}
|
2022-06-29 13:56:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warnf("Coudln't retrieve private chat info: %v", err.Error())
|
|
|
|
}
|
|
|
|
} else if chatType == client.TypeChatTypeBasicGroup {
|
|
|
|
basicGroupType, _ := chat.Type.(*client.ChatTypeBasicGroup)
|
|
|
|
fullInfo, err := c.client.GetBasicGroupFullInfo(&client.GetBasicGroupFullInfoRequest{
|
|
|
|
BasicGroupId: basicGroupType.BasicGroupId,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return fullInfo.Description
|
|
|
|
} else {
|
|
|
|
log.Warnf("Coudln't retrieve basic group info: %v", err.Error())
|
|
|
|
}
|
|
|
|
} else if chatType == client.TypeChatTypeSupergroup {
|
|
|
|
supergroupType, _ := chat.Type.(*client.ChatTypeSupergroup)
|
|
|
|
fullInfo, err := c.client.GetSupergroupFullInfo(&client.GetSupergroupFullInfoRequest{
|
|
|
|
SupergroupId: supergroupType.SupergroupId,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return fullInfo.Description
|
|
|
|
} else {
|
|
|
|
log.Warnf("Coudln't retrieve supergroup info: %v", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-02-06 13:25:46 +00:00
|
|
|
// subscribe to a Telegram ID
|
|
|
|
func (c *Client) subscribeToID(id int64, chat *client.Chat) {
|
2024-01-24 23:52:40 +00:00
|
|
|
args := gateway.SimplePresence(id, "subscribe")
|
2022-02-06 13:25:46 +00:00
|
|
|
|
|
|
|
if chat == nil {
|
|
|
|
chat, _, _ = c.GetContactByID(id, nil)
|
|
|
|
}
|
|
|
|
if chat != nil {
|
|
|
|
args = append(args, gateway.SPNickname(chat.Title))
|
2022-06-30 21:22:44 +00:00
|
|
|
|
|
|
|
gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp)
|
2022-02-06 13:25:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 23:52:40 +00:00
|
|
|
c.sendPresence(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) sendPresence(args ...args.V) error {
|
|
|
|
return gateway.SendPresence(c.xmpp, c.jid, args...)
|
2022-02-06 13:25:46 +00:00
|
|
|
}
|
2022-04-01 01:42:12 +00:00
|
|
|
|
|
|
|
func (c *Client) prepareDiskSpace(size uint64) {
|
|
|
|
if gateway.StorageQuota > 0 && c.content.Path != "" {
|
|
|
|
var loweredQuota uint64
|
|
|
|
if gateway.StorageQuota >= size {
|
|
|
|
loweredQuota = gateway.StorageQuota - size
|
|
|
|
}
|
|
|
|
if gateway.CachedStorageSize >= loweredQuota {
|
|
|
|
log.Warn("Storage is rapidly clogged")
|
|
|
|
gateway.CleanOldFiles(c.content.Path, loweredQuota)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 20:37:38 +00:00
|
|
|
|
|
|
|
func (c *Client) GetVcardInfo(toID int64) (VCardInfo, error) {
|
|
|
|
var info VCardInfo
|
|
|
|
chat, user, err := c.GetContactByID(toID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if chat != nil {
|
|
|
|
info.Fn = chat.Title
|
|
|
|
|
|
|
|
if chat.Photo != nil {
|
|
|
|
info.Photo = chat.Photo.Small
|
|
|
|
}
|
|
|
|
info.Info = c.GetChatDescription(chat)
|
|
|
|
}
|
|
|
|
if user != nil {
|
2023-08-01 01:25:24 +00:00
|
|
|
if user.Usernames != nil {
|
|
|
|
info.Nicknames = make([]string, len(user.Usernames.ActiveUsernames))
|
|
|
|
copy(info.Nicknames, user.Usernames.ActiveUsernames)
|
|
|
|
}
|
2023-06-01 20:37:38 +00:00
|
|
|
info.Given = user.FirstName
|
|
|
|
info.Family = user.LastName
|
|
|
|
info.Tel = user.PhoneNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
2023-06-30 13:54:39 +00:00
|
|
|
|
|
|
|
func (c *Client) UpdateChatNicknames() {
|
|
|
|
for _, id := range c.cache.ChatsKeys() {
|
|
|
|
chat, ok := c.cache.GetChat(id)
|
|
|
|
if ok {
|
2023-08-07 00:04:49 +00:00
|
|
|
newArgs := []args.V{
|
|
|
|
gateway.SPNickname(chat.Title),
|
|
|
|
}
|
2024-01-24 23:52:40 +00:00
|
|
|
newArgs = gateway.SPAppendFrom(newArgs, id)
|
2023-08-07 00:04:49 +00:00
|
|
|
|
|
|
|
cachedStatus, ok := c.cache.GetStatus(id)
|
|
|
|
if ok {
|
|
|
|
show, status, typ := cachedStatus.Destruct()
|
|
|
|
newArgs = append(newArgs, gateway.SPShow(show), gateway.SPStatus(status))
|
|
|
|
if typ != "" {
|
|
|
|
newArgs = append(newArgs, gateway.SPType(typ))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 23:52:40 +00:00
|
|
|
c.sendPresence(newArgs...)
|
2023-08-07 00:04:49 +00:00
|
|
|
|
2023-06-30 13:54:39 +00:00
|
|
|
gateway.SetNickname(c.jid, strconv.FormatInt(id, 10), chat.Title, c.xmpp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-09 03:52:30 +00:00
|
|
|
|
2024-01-27 02:02:47 +00:00
|
|
|
// AddToEditOutbox temporarily store the resource from which a replace message with given ID was sent
|
|
|
|
func (c *Client) AddToEditOutbox(xmppId, resource string) {
|
|
|
|
c.locks.editOutboxLock.Lock()
|
|
|
|
defer c.locks.editOutboxLock.Unlock()
|
|
|
|
|
|
|
|
c.editOutbox[xmppId] = resource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) popFromEditOutbox(xmppId string) string {
|
|
|
|
c.locks.editOutboxLock.Lock()
|
|
|
|
defer c.locks.editOutboxLock.Unlock()
|
|
|
|
|
|
|
|
resource, ok := c.editOutbox[xmppId]
|
|
|
|
if ok {
|
|
|
|
delete(c.editOutbox, xmppId)
|
|
|
|
} else {
|
|
|
|
log.Warnf("No %v xmppId in edit outbox", xmppId)
|
|
|
|
}
|
|
|
|
return resource
|
|
|
|
}
|
|
|
|
|
2023-07-09 03:52:30 +00:00
|
|
|
// AddToOutbox remembers the resource from which a message with given ID was sent
|
|
|
|
func (c *Client) AddToOutbox(xmppId, resource string) {
|
|
|
|
c.locks.outboxLock.Lock()
|
|
|
|
defer c.locks.outboxLock.Unlock()
|
|
|
|
|
|
|
|
c.outbox[xmppId] = resource
|
|
|
|
}
|
|
|
|
|
2024-01-27 02:02:47 +00:00
|
|
|
func (c *Client) getFromOutbox(xmppId string) string {
|
2023-07-09 03:52:30 +00:00
|
|
|
c.locks.outboxLock.Lock()
|
|
|
|
defer c.locks.outboxLock.Unlock()
|
|
|
|
|
|
|
|
resource, ok := c.outbox[xmppId]
|
2024-01-27 02:02:47 +00:00
|
|
|
if !ok {
|
2023-07-09 03:52:30 +00:00
|
|
|
log.Warnf("No %v xmppId in outbox", xmppId)
|
|
|
|
}
|
|
|
|
return resource
|
|
|
|
}
|
2023-07-16 01:38:10 +00:00
|
|
|
|
|
|
|
func (c *Client) getCarbonFullJids(isOutgoing bool, ignoredResource string) []string {
|
|
|
|
var jids []string
|
|
|
|
if isOutgoing {
|
|
|
|
for resource := range c.resourcesRange() {
|
|
|
|
if ignoredResource == "" || resource != ignoredResource {
|
|
|
|
jids = append(jids, c.jid+"/"+resource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
jids = []string{c.jid}
|
|
|
|
}
|
|
|
|
return jids
|
|
|
|
}
|
2023-07-22 14:46:35 +00:00
|
|
|
|
|
|
|
func (c *Client) calculateMessageHash(messageId int64, content client.MessageContent) uint64 {
|
|
|
|
var h maphash.Hash
|
|
|
|
h.SetSeed(c.msgHashSeed)
|
|
|
|
|
|
|
|
buf8 := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(buf8, uint64(messageId))
|
|
|
|
h.Write(buf8)
|
|
|
|
|
|
|
|
if content != nil && content.MessageContentType() == client.TypeMessageText {
|
|
|
|
textContent, ok := content.(*client.MessageText)
|
|
|
|
if !ok {
|
|
|
|
uhOh()
|
|
|
|
}
|
|
|
|
|
|
|
|
if textContent.Text != nil {
|
|
|
|
h.WriteString(textContent.Text.Text)
|
|
|
|
for _, entity := range textContent.Text.Entities {
|
|
|
|
buf4 := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(buf4, uint32(entity.Offset))
|
|
|
|
h.Write(buf4)
|
|
|
|
binary.BigEndian.PutUint32(buf4, uint32(entity.Length))
|
|
|
|
h.Write(buf4)
|
|
|
|
h.WriteString(entity.Type.TextEntityTypeType())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.Sum64()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) updateLastMessageHash(chatId, messageId int64, content client.MessageContent) {
|
|
|
|
c.locks.lastMsgHashesLock.Lock()
|
|
|
|
defer c.locks.lastMsgHashesLock.Unlock()
|
|
|
|
|
|
|
|
c.lastMsgHashes[chatId] = c.calculateMessageHash(messageId, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) hasLastMessageHashChanged(chatId, messageId int64, content client.MessageContent) bool {
|
|
|
|
c.locks.lastMsgHashesLock.Lock()
|
|
|
|
defer c.locks.lastMsgHashesLock.Unlock()
|
|
|
|
|
|
|
|
oldHash, ok := c.lastMsgHashes[chatId]
|
|
|
|
newHash := c.calculateMessageHash(messageId, content)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
log.Warnf("Last message hash for chat %v does not exist", chatId)
|
|
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"old hash": oldHash,
|
|
|
|
"new hash": newHash,
|
|
|
|
}).Info("Message hashes")
|
|
|
|
|
|
|
|
return !ok || oldHash != newHash
|
|
|
|
}
|
2023-08-01 01:25:24 +00:00
|
|
|
|
2024-01-29 09:28:15 +00:00
|
|
|
func (c *Client) UpdateLastChatMessageId(chatId int64, messageId string) {
|
|
|
|
c.locks.lastMsgIdsLock.Lock()
|
|
|
|
defer c.locks.lastMsgIdsLock.Unlock()
|
|
|
|
|
|
|
|
c.lastMsgIds[chatId] = messageId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getLastChatMessageId(chatId int64) (string, bool) {
|
|
|
|
c.locks.lastMsgIdsLock.RLock()
|
|
|
|
defer c.locks.lastMsgIdsLock.RUnlock()
|
|
|
|
|
|
|
|
xmppId, ok := c.lastMsgIds[chatId]
|
|
|
|
return xmppId, ok
|
|
|
|
}
|
|
|
|
|
2023-11-16 00:38:45 +00:00
|
|
|
func (c *Client) getFormatter() formatter.MarkupModeType {
|
|
|
|
return formatter.MarkupModeXEP0393
|
2023-08-01 01:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) usernamesToString(usernames []string) string {
|
|
|
|
var atUsernames []string
|
|
|
|
for _, username := range usernames {
|
|
|
|
atUsernames = append(atUsernames, "@"+username)
|
|
|
|
}
|
|
|
|
return strings.Join(atUsernames, ", ")
|
|
|
|
}
|
2024-02-18 07:48:02 +00:00
|
|
|
|
|
|
|
// GetChatMembers retrieves a list of chat members. "Limited" mode works only if there are no more than 20 members at all
|
|
|
|
func (c *Client) GetChatMembers(chatID int64, limited bool, query string, membersList MembersList) ([]*client.ChatMember, error) {
|
|
|
|
var filters []client.ChatMembersFilter
|
|
|
|
switch membersList {
|
|
|
|
case MembersListMembers:
|
|
|
|
filters = []client.ChatMembersFilter{&client.ChatMembersFilterMembers{}}
|
|
|
|
case MembersListRestricted:
|
|
|
|
filters = []client.ChatMembersFilter{&client.ChatMembersFilterRestricted{}}
|
|
|
|
case MembersListBanned:
|
|
|
|
filters = []client.ChatMembersFilter{&client.ChatMembersFilterBanned{}}
|
|
|
|
case MembersListBannedAndAdministrators:
|
|
|
|
filters = []client.ChatMembersFilter{&client.ChatMembersFilterBanned{}, &client.ChatMembersFilterAdministrators{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
limit := int32(9999)
|
|
|
|
if limited {
|
|
|
|
limit = 20
|
|
|
|
|
|
|
|
chat, _, err := c.GetContactByID(chatID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if chat == nil {
|
|
|
|
return nil, errors.New("Chat not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
chatType := chat.Type.ChatTypeType()
|
|
|
|
if chatType == client.TypeChatTypeBasicGroup {
|
|
|
|
basicGroupType, _ := chat.Type.(*client.ChatTypeBasicGroup)
|
|
|
|
fullInfo, err := c.client.GetBasicGroupFullInfo(&client.GetBasicGroupFullInfoRequest{
|
|
|
|
BasicGroupId: basicGroupType.BasicGroupId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fullInfo.Members) > int(limit) {
|
|
|
|
return nil, errOverLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
return fullInfo.Members, nil
|
|
|
|
} else if chatType == client.TypeChatTypeSupergroup {
|
|
|
|
supergroupType, _ := chat.Type.(*client.ChatTypeSupergroup)
|
|
|
|
fullInfo, err := c.client.GetSupergroupFullInfo(&client.GetSupergroupFullInfoRequest{
|
|
|
|
SupergroupId: supergroupType.SupergroupId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fullInfo.MemberCount > limit {
|
|
|
|
return nil, errOverLimit
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Inapplicable chat type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var members []*client.ChatMember
|
|
|
|
for _, filter := range filters {
|
|
|
|
chatMembers, err := c.client.SearchChatMembers(&client.SearchChatMembersRequest{
|
|
|
|
ChatId: chatID,
|
|
|
|
Limit: limit,
|
|
|
|
Query: query,
|
|
|
|
Filter: filter,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
members = append(members, chatMembers.Members...)
|
|
|
|
}
|
|
|
|
return members, nil
|
|
|
|
}
|