telegabber/telegram/utils.go

1058 lines
27 KiB
Go
Raw Normal View History

2019-11-29 00:51:41 +00:00
package telegram
import (
"crypto/sha1"
2019-12-01 13:13:45 +00:00
"fmt"
2019-11-29 00:51:41 +00:00
"github.com/pkg/errors"
"io"
"io/ioutil"
"net/http"
2019-11-29 00:51:41 +00:00
"os"
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"
2020-01-05 13:03:10 +00:00
"dev.narayana.im/narayana/telegabber/telegram/cache"
"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
)
var errOffline = errors.New("TDlib instance is offline")
2019-12-01 13:13:45 +00:00
var spaceRegex = regexp.MustCompile(`\s+`)
var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n")
2019-12-01 13:13:45 +00:00
const newlineChar string = "\n"
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) {
if !c.Online() {
2019-11-29 00:51:41 +00:00
return nil, nil, errOffline
}
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
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
}
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 {
// error is irrelevant if the user was found successfully
2019-12-10 18:49:42 +00:00
if user != nil {
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
}
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:
show, textStatus = "xa", "Last seen last week"
2019-11-30 00:41:22 +00:00
case client.TypeUserStatusLastMonth:
show, textStatus = "xa", "Last seen last month"
2019-11-30 00:41:22 +00:00
case client.TypeUserStatusEmpty:
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
}
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
func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, oldArgs ...args.V) error {
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 {
file, path, err := c.OpenPhotoFile(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)
}
}
var presenceType string
if gateway.SPType.IsSet(oldArgs) {
presenceType = gateway.SPType.Get(oldArgs)
}
cachedStatus, ok := c.cache.GetStatus(chatID)
2019-11-30 00:41:22 +00:00
if status == "" {
if ok {
show, status = cachedStatus.XMPP, cachedStatus.Description
} else if user != nil && user.Status != nil {
show, status, presenceType = c.userStatusToText(user.Status, chatID)
2019-11-30 00:41:22 +00:00
} else {
show, status = "chat", chat.Title
2019-11-29 00:51:41 +00:00
}
}
2020-01-05 13:03:10 +00:00
c.cache.SetStatus(chatID, show, status)
2022-02-08 20:25:58 +00:00
newArgs := []args.V{
2019-12-04 19:29:57 +00:00
gateway.SPFrom(strconv.FormatInt(chatID, 10)),
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),
gateway.SPResource(gateway.Jid.Resource),
gateway.SPImmed(gateway.SPImmed.Get(oldArgs)),
}
if presenceType != "" {
newArgs = append(newArgs, gateway.SPType(presenceType))
}
return gateway.SendPresence(
c.xmpp,
c.jid,
newArgs...,
2019-11-29 00:51:41 +00:00
)
}
2019-12-04 18:37:46 +00:00
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 {
username := user.Username
if username == "" {
2022-01-17 20:45:40 +00:00
username = strconv.FormatInt(user.Id, 10)
2019-12-01 13:13:45 +00:00
}
str = fmt.Sprintf("%s %s (%v)", user.FirstName, user.LastName, username)
} 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
}
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 {
return fmt.Sprintf("<error fetching message: %s>", err.Error())
2019-12-01 13:13:45 +00:00
}
}
if message == nil {
return ""
}
var str strings.Builder
2022-02-05 18:54:35 +00:00
// add messageid and sender
2021-12-04 18:10:54 +00:00
var senderId int64
if message.SenderId != nil {
switch message.SenderId.MessageSenderType() {
case client.TypeMessageSenderUser:
senderUser, _ := message.SenderId.(*client.MessageSenderUser)
senderId = senderUser.UserId
case client.TypeMessageSenderChat:
senderChat, _ := message.SenderId.(*client.MessageSenderChat)
senderId = senderChat.ChatId
}
2021-12-04 18:10:54 +00:00
}
2022-01-17 20:45:40 +00:00
str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(senderId)))
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
2019-12-01 13:13:45 +00:00
var text string
2021-12-05 03:27:14 +00:00
if message.Content != nil {
2022-02-05 18:54:35 +00:00
text = c.messageToText(message, preview)
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()
}
2022-01-10 09:29:50 +00:00
func (c *Client) formatForward(fwd *client.MessageForwardInfo) string {
switch fwd.Origin.MessageForwardOriginType() {
case client.TypeMessageForwardOriginUser:
originUser := fwd.Origin.(*client.MessageForwardOriginUser)
2022-01-17 20:45:40 +00:00
return c.formatContact(originUser.SenderUserId)
2022-01-10 09:29:50 +00:00
case client.TypeMessageForwardOriginChat:
originChat := fwd.Origin.(*client.MessageForwardOriginChat)
var signature string
if originChat.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature)
}
2022-02-08 20:25:58 +00:00
return c.formatContact(originChat.SenderChatId) + signature
2022-01-10 09:29:50 +00:00
case client.TypeMessageForwardOriginHiddenUser:
originUser := fwd.Origin.(*client.MessageForwardOriginHiddenUser)
return originUser.SenderName
case client.TypeMessageForwardOriginChannel:
channel := fwd.Origin.(*client.MessageForwardOriginChannel)
var signature string
if channel.AuthorSignature != "" {
signature = fmt.Sprintf(" (%s)", channel.AuthorSignature)
}
2022-02-08 20:25:58 +00:00
return c.formatContact(channel.ChatId) + signature
case client.TypeMessageForwardOriginMessageImport:
originImport := fwd.Origin.(*client.MessageForwardOriginMessageImport)
return originImport.SenderName
2022-01-10 09:29:50 +00:00
}
return "Unknown forward type"
}
2022-03-14 21:57:03 +00:00
func (c *Client) formatFile(file *client.File, compact bool) string {
log.Debugf("file: %#v", file)
if file == nil || file.Local == nil || file.Remote == nil {
2019-12-01 13:13:45 +00:00
return ""
}
gateway.StorageLock.Lock()
defer gateway.StorageLock.Unlock()
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 {
log.Errorf("Cannot access source file: %v", err)
return ""
}
2019-12-01 13:13:45 +00:00
2022-04-01 16:35:54 +00:00
size64 := uint64(file.Size)
c.prepareDiskSpace(size64)
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
// 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>"
}
}
gateway.CachedStorageSize += size64
// 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)
}
}
}
2022-03-14 21:57:03 +00:00
if compact {
return link
} else {
return fmt.Sprintf("%s (%v kbytes) | %s", filepath.Base(src), file.Size/1024, 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
}
markupFunction := formatter.EntityToXEP0393
2019-12-01 13:13:45 +00:00
switch message.Content.MessageContentType() {
case client.TypeMessageSticker:
sticker, _ := message.Content.(*client.MessageSticker)
return sticker.Sticker.Emoji
2022-02-05 15:19:18 +00:00
case client.TypeMessageAnimatedEmoji:
animatedEmoji, _ := message.Content.(*client.MessageAnimatedEmoji)
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:
addMembers, _ := message.Content.(*client.MessageChatAddMembers)
text := "invited "
2022-01-17 20:45:40 +00:00
if len(addMembers.MemberUserIds) > 0 {
text += c.formatContact(addMembers.MemberUserIds[0])
2019-12-01 13:13:45 +00:00
}
return text
case client.TypeMessageChatDeleteMember:
deleteMember, _ := message.Content.(*client.MessageChatDeleteMember)
2022-01-17 20:45:40 +00:00
return "kicked " + c.formatContact(deleteMember.UserId)
2019-12-01 13:13:45 +00:00
case client.TypeMessagePinMessage:
pinMessage, _ := message.Content.(*client.MessagePinMessage)
2022-02-05 18:54:35 +00:00
return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, preview, nil)
2019-12-01 13:13:45 +00:00
case client.TypeMessageChatChangeTitle:
changeTitle, _ := message.Content.(*client.MessageChatChangeTitle)
return "chat title set to: " + changeTitle.Title
case client.TypeMessageLocation:
location, _ := message.Content.(*client.MessageLocation)
2022-02-05 14:10:08 +00:00
return c.formatLocation(location.Location)
case client.TypeMessageVenue:
venue, _ := message.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:
photo, _ := message.Content.(*client.MessagePhoto)
2022-02-05 18:54:35 +00:00
if preview {
return photo.Caption.Text
} else {
return formatter.Format(
photo.Caption.Text,
photo.Caption.Entities,
2022-02-05 18:54:35 +00:00
markupFunction,
)
}
2019-12-01 13:13:45 +00:00
case client.TypeMessageAudio:
audio, _ := message.Content.(*client.MessageAudio)
2022-02-05 18:54:35 +00:00
if preview {
return audio.Caption.Text
} else {
return formatter.Format(
audio.Caption.Text,
audio.Caption.Entities,
2022-02-05 18:54:35 +00:00
markupFunction,
)
}
2019-12-01 13:13:45 +00:00
case client.TypeMessageVideo:
video, _ := message.Content.(*client.MessageVideo)
2022-02-05 18:54:35 +00:00
if preview {
return video.Caption.Text
} else {
return formatter.Format(
video.Caption.Text,
video.Caption.Entities,
2022-02-05 18:54:35 +00:00
markupFunction,
)
}
2019-12-01 13:13:45 +00:00
case client.TypeMessageDocument:
document, _ := message.Content.(*client.MessageDocument)
2022-02-05 18:54:35 +00:00
if preview {
return document.Caption.Text
} else {
return formatter.Format(
document.Caption.Text,
document.Caption.Entities,
2022-02-05 18:54:35 +00:00
markupFunction,
)
}
2019-12-01 13:13:45 +00:00
case client.TypeMessageText:
text, _ := message.Content.(*client.MessageText)
2022-02-05 18:54:35 +00:00
if preview {
return text.Text.Text
} else {
return formatter.Format(
text.Text.Text,
text.Text.Entities,
2022-02-05 18:54:35 +00:00
markupFunction,
)
}
2019-12-01 13:13:45 +00:00
case client.TypeMessageVoiceNote:
voice, _ := message.Content.(*client.MessageVoiceNote)
2022-02-05 18:54:35 +00:00
if preview {
return voice.Caption.Text
} else {
return formatter.Format(
voice.Caption.Text,
voice.Caption.Entities,
2022-02-05 18:54:35 +00:00
markupFunction,
)
}
2019-12-01 13:13:45 +00:00
case client.TypeMessageVideoNote:
return ""
case client.TypeMessageAnimation:
animation, _ := message.Content.(*client.MessageAnimation)
2022-02-05 18:54:35 +00:00
if preview {
return animation.Caption.Text
} else {
return formatter.Format(
animation.Caption.Text,
animation.Caption.Entities,
2022-02-05 18:54:35 +00:00
markupFunction,
)
}
2022-02-05 14:49:21 +00:00
case client.TypeMessageContact:
contact, _ := message.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:
dice, _ := message.Content.(*client.MessageDice)
return fmt.Sprintf("%s 1d6: [%v]", dice.Emoji, dice.Value)
2022-02-05 16:33:53 +00:00
case client.TypeMessagePoll:
poll, _ := message.Content.(*client.MessagePoll)
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")
}
2019-12-01 13:13:45 +00:00
}
return fmt.Sprintf("unknown message (%s)", message.Content.MessageContentType())
}
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
if sticker.Sticker.IsAnimated && sticker.Sticker.Thumbnail != nil && sticker.Sticker.Thumbnail.File != nil {
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 {
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
}
2022-03-14 21:57:03 +00:00
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string) string {
2019-12-01 13:13:45 +00:00
prefix := []string{}
// message direction
var directionChar string
if c.Session.AsciiArrows {
if message.IsOutgoing {
directionChar = "> "
} else {
directionChar = "< "
}
2019-12-01 13:13:45 +00:00
} else {
if message.IsOutgoing {
directionChar = "➡ "
} else {
directionChar = "⬅ "
}
2019-12-01 13:13:45 +00:00
}
2022-01-17 20:45:40 +00:00
prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10))
2019-12-01 13:13:45 +00:00
// show sender in group chats
2022-01-17 20:45:40 +00:00
if message.ChatId < 0 && message.SenderId != nil {
2021-12-04 18:10:54 +00:00
var senderId int64
2022-01-17 20:45:40 +00:00
switch message.SenderId.MessageSenderType() {
2021-12-04 18:10:54 +00:00
case client.TypeMessageSenderUser:
2022-01-17 20:45:40 +00:00
senderUser, _ := message.SenderId.(*client.MessageSenderUser)
senderId = senderUser.UserId
2021-12-04 18:10:54 +00:00
case client.TypeMessageSenderChat:
2022-01-17 20:45:40 +00:00
senderChat, _ := message.SenderId.(*client.MessageSenderChat)
senderId = senderChat.ChatId
2021-12-04 18:10:54 +00:00
}
prefix = append(prefix, c.formatContact(senderId))
2019-12-01 13:13:45 +00:00
}
// reply to
2022-01-17 20:45:40 +00:00
if message.ReplyToMessageId != 0 {
prefix = append(prefix, "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, nil))
2019-12-01 13:13:45 +00:00
}
2022-01-10 09:29:50 +00:00
if message.ForwardInfo != nil {
prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo))
}
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)
}
return strings.Join(prefix, " | ")
}
2022-03-14 21:57:03 +00:00
func (c *Client) ensureDownloadFile(file *client.File) *client.File {
gateway.StorageLock.Lock()
defer gateway.StorageLock.Unlock()
2022-03-14 21:57:03 +00:00
if file != nil {
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
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
var text string
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)
if !(strings.HasPrefix(text, "?OTR") || c.Session.RawMessages) {
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)
2022-03-12 17:25:53 +00:00
var prefix strings.Builder
2022-03-14 21:57:03 +00:00
prefix.WriteString(c.messageToPrefix(message, c.formatFile(preview, true), c.formatFile(file, false)))
2022-03-12 17:25:53 +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)
}
text = prefix.String()
}
}
// mark message as read
c.client.ViewMessages(&client.ViewMessagesRequest{
ChatId: chatId,
MessageIds: []int64{message.Id},
ForceRead: true,
})
// forward message to XMPP
gateway.SendMessage(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp)
}
2019-12-03 00:32:53 +00:00
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string) client.InputMessageContent {
if !c.Online() {
// we're offline
return nil
}
if returnJid != "" && (strings.HasPrefix(text, "/") || strings.HasPrefix(text, "!")) {
// try to execute commands
2019-12-05 23:21:39 +00:00
response, isCommand := c.ProcessChatCommand(chatID, text)
2019-12-03 00:32:53 +00:00
if response != "" {
2019-12-04 19:29:57 +00:00
gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp)
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 {
return nil
2019-12-03 00:32:53 +00:00
}
}
2019-12-03 16:48:41 +00:00
log.Warnf("Sending message to chat %v", chatID)
2019-12-03 16:48:41 +00:00
// quotations
var reply int64
replySlice := replyRegex.FindStringSubmatch(text)
if len(replySlice) > 1 {
reply, _ = strconv.ParseInt(replySlice[1], 10, 64)
}
2019-12-03 16:48:41 +00:00
// attach a file
var file *client.InputFileLocal
if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
response, err := http.Get(text)
if err != nil {
gateway.SendMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()),
c.xmpp,
)
return nil
}
if response != nil && response.Body != nil {
defer response.Body.Close()
if response.StatusCode != 200 {
gateway.SendMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Received status code %v", response.StatusCode),
c.xmpp,
)
return nil
}
tempDir, err := ioutil.TempDir("", "telegabber-*")
if err != nil {
gateway.SendMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to create a temporary directory: %s", err.Error()),
c.xmpp,
)
return nil
}
tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(text)))
if err != nil {
gateway.SendMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to create a temporary file: %s", err.Error()),
c.xmpp,
)
return nil
}
_, err = io.Copy(tempFile, response.Body)
if err != nil {
gateway.SendMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to write a temporary file: %s", err.Error()),
c.xmpp,
)
return nil
}
file = &client.InputFileLocal{
Path: tempFile.Name(),
}
2019-12-07 16:37:14 +00:00
}
}
2019-12-07 16:37:14 +00:00
// remove first line from text
if file != nil || reply != 0 {
newlinePos := strings.Index(text, newlineChar)
if newlinePos != -1 {
text = text[newlinePos+1:]
} else {
text = ""
2019-12-07 16:37:14 +00:00
}
}
2019-12-07 16:37:14 +00:00
formattedText := &client.FormattedText{
Text: text,
}
2019-12-07 16:37:14 +00:00
var message client.InputMessageContent
if file != nil {
// we can try to send a document
message = &client.InputMessageDocument{
Document: file,
Caption: formattedText,
2019-12-07 16:37:14 +00:00
}
} else {
// compile our message
message = &client.InputMessageText{
Text: formattedText,
2019-12-07 16:37:14 +00:00
}
}
2019-12-07 16:37:14 +00:00
if chatID != 0 {
2019-12-20 23:44:21 +00:00
_, err := c.client.SendMessage(&client.SendMessageRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
ReplyToMessageId: reply,
2019-12-03 16:48:41 +00:00
InputMessageContent: message,
})
2019-12-20 23:44:21 +00:00
if err != nil {
gateway.SendMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Not sent: %s", err.Error()),
2019-12-20 23:44:21 +00:00
c.xmpp,
)
}
return nil
} else {
return message
2019-12-03 16:48:41 +00:00
}
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)
}
}
2022-01-05 21:04:22 +00:00
// resend statuses to (to another resource, for example)
func (c *Client) roster(resource string) {
2022-01-03 03:54:13 +00:00
if _, ok := c.resources[resource]; ok {
2022-01-05 21:04:22 +00:00
return // we know it
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, "", "")
}
2022-01-05 21:04:22 +00:00
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login))
2022-01-03 03:54:13 +00:00
c.addResource(resource)
}
2022-01-06 12:13:57 +00:00
// get last messages from specified chat
2022-01-06 12:13:57 +00:00
func (c *Client) getLastMessages(id int64, query string, from int64, count int32) (*client.Messages, error) {
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
})
}
// 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
// OpenPhotoFile reliably obtains a photo if possible
func (c *Client) OpenPhotoFile(photoFile *client.File, priority int32) (*os.File, string, error) {
if photoFile == nil {
return nil, "", errors.New("Photo file not found")
}
path := photoFile.Local.Path
file, err := os.Open(path)
if err == nil {
return file, path, nil
// obtain the photo right now if still not downloaded
} else if !photoFile.Local.IsDownloadingCompleted {
tdFile, tdErr := c.DownloadFile(photoFile.Id, priority, true)
if tdErr == nil {
path = tdFile.Local.Path
file, err = os.Open(path)
return file, path, err
}
}
// give up
return nil, path, err
}
2022-02-06 13:25:46 +00:00
// subscribe to a Telegram ID
func (c *Client) subscribeToID(id int64, chat *client.Chat) {
var args []args.V
args = append(args, gateway.SPFrom(strconv.FormatInt(id, 10)))
args = append(args, gateway.SPType("subscribe"))
if chat == nil {
chat, _, _ = c.GetContactByID(id, nil)
}
if chat != nil {
args = append(args, gateway.SPNickname(chat.Title))
}
gateway.SendPresence(
c.xmpp,
c.jid,
args...,
)
}
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)
}
}
}