2019-11-29 00:51:41 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
2019-12-01 13:13:45 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
2019-11-29 00:51:41 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
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"
|
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"
|
2021-12-29 00:53:38 +00:00
|
|
|
"github.com/godcong/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+`)
|
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"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
|
|
|
|
Username: username,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-04 18:10:54 +00:00
|
|
|
return c.GetContactByID(chat.ID, 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{
|
|
|
|
UserID: id,
|
|
|
|
})
|
|
|
|
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{
|
2021-12-04 18:10:54 +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
|
|
|
|
}
|
|
|
|
|
2019-12-11 22:48:35 +00:00
|
|
|
func (c *Client) userStatusToText(status client.UserStatus) (string, string) {
|
2019-11-30 00:41:22 +00:00
|
|
|
var show, textStatus string
|
|
|
|
|
|
|
|
switch status.UserStatusType() {
|
|
|
|
case client.TypeUserStatusOnline:
|
|
|
|
textStatus = "Online"
|
|
|
|
case client.TypeUserStatusRecently:
|
|
|
|
show, textStatus = "dnd", "Last seen recently"
|
|
|
|
case client.TypeUserStatusLastWeek:
|
|
|
|
show, textStatus = "unavailable", "Last seen last week"
|
|
|
|
case client.TypeUserStatusLastMonth:
|
|
|
|
show, textStatus = "unavailable", "Last seen last month"
|
|
|
|
case client.TypeUserStatusEmpty:
|
|
|
|
show, textStatus = "unavailable", "Last seen a long time ago"
|
|
|
|
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\
|
|
|
|
elapsed := time.Now().Unix() - int64(offlineStatus.WasOnline)
|
|
|
|
if elapsed < 3600 {
|
|
|
|
show = "away"
|
|
|
|
} else {
|
|
|
|
show = "xa"
|
|
|
|
}
|
2019-12-11 22:48:35 +00:00
|
|
|
textStatus = time.Unix(int64(offlineStatus.WasOnline), 0).
|
|
|
|
In(c.Session.TimezoneToLocation()).
|
|
|
|
Format("Last seen at 15:04 02/01/2006")
|
2019-11-30 00:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return show, textStatus
|
|
|
|
}
|
|
|
|
|
2020-01-05 13:03:10 +00:00
|
|
|
// ProcessStatusUpdate sets contact status
|
|
|
|
func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, args ...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 {
|
|
|
|
path := chat.Photo.Small.Local.Path
|
|
|
|
file, err := os.Open(path)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 00:41:22 +00:00
|
|
|
if status == "" {
|
|
|
|
if user != nil {
|
2019-12-11 22:48:35 +00:00
|
|
|
show, status = c.userStatusToText(user.Status)
|
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)
|
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
gateway.SendPresence(
|
|
|
|
c.xmpp,
|
|
|
|
c.jid,
|
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.SPImmed(gateway.SPImmed.Get(args)),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-12-04 18:10:54 +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 == "" {
|
2021-12-04 18:10:54 +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{
|
2021-12-04 18:10:54 +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 ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var str strings.Builder
|
2021-12-04 18:10:54 +00:00
|
|
|
var senderId int64
|
|
|
|
switch message.Sender.MessageSenderType() {
|
|
|
|
case client.TypeMessageSenderUser:
|
|
|
|
senderUser, _ := message.Sender.(*client.MessageSenderUser)
|
|
|
|
senderId = senderUser.UserID
|
|
|
|
case client.TypeMessageSenderChat:
|
|
|
|
senderChat, _ := message.Sender.(*client.MessageSenderChat)
|
|
|
|
senderId = senderChat.ChatID
|
|
|
|
}
|
|
|
|
str.WriteString(fmt.Sprintf("%v | %s | ", message.ID, c.formatContact(senderId)))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
var text string
|
2021-12-05 03:27:14 +00:00
|
|
|
if message.Content != nil {
|
|
|
|
switch message.Content.MessageContentType() {
|
|
|
|
case client.TypeMessageText:
|
|
|
|
messageText, _ := message.Content.(*client.MessageText)
|
|
|
|
text = messageText.Text.Text
|
|
|
|
// TODO: handle other message types with labels (not supported in Zhabogram!)
|
|
|
|
}
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
if text != "" {
|
|
|
|
if !preview {
|
|
|
|
str.WriteString(text)
|
|
|
|
} else {
|
|
|
|
newlinePos := strings.Index(text, newlineChar)
|
|
|
|
if !preview || newlinePos == -1 {
|
|
|
|
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)
|
|
|
|
return c.formatContact(originUser.SenderUserID)
|
|
|
|
case client.TypeMessageForwardOriginChat:
|
|
|
|
originChat := fwd.Origin.(*client.MessageForwardOriginChat)
|
|
|
|
var signature string
|
|
|
|
if originChat.AuthorSignature != "" {
|
|
|
|
signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature)
|
|
|
|
}
|
|
|
|
return c.formatContact(originChat.SenderChatID)+signature
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return c.formatContact(channel.ChatID)+signature
|
|
|
|
}
|
|
|
|
return "Unknown forward type"
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:13:45 +00:00
|
|
|
func (c *Client) formatContent(file *client.File, filename string) string {
|
|
|
|
if file == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%s (%v kbytes) | %s/%s%s",
|
|
|
|
filename,
|
|
|
|
file.Size/1024,
|
|
|
|
c.content.Link,
|
2021-12-04 18:10:54 +00:00
|
|
|
fmt.Sprintf("%x", sha256.Sum256([]byte(file.Remote.ID))),
|
2019-12-01 13:13:45 +00:00
|
|
|
filepath.Ext(filename),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) messageToText(message *client.Message) string {
|
2021-12-05 03:27:14 +00:00
|
|
|
if message.Content == nil {
|
|
|
|
log.Warnf("Unknown message (big emoji?): %#v", message)
|
|
|
|
return "<BIG EMOJI>"
|
|
|
|
}
|
|
|
|
|
2021-12-18 16:04:24 +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
|
|
|
|
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 "
|
2021-12-04 18:10:54 +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)
|
2021-12-04 18:10:54 +00:00
|
|
|
return "kicked " + c.formatContact(deleteMember.UserID)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessagePinMessage:
|
|
|
|
pinMessage, _ := message.Content.(*client.MessagePinMessage)
|
2021-12-04 18:10:54 +00:00
|
|
|
return "pinned message: " + c.formatMessage(message.ChatID, pinMessage.MessageID, false, 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)
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"coordinates: %v,%v | https://www.google.com/maps/search/%v,%v/",
|
|
|
|
location.Location.Latitude,
|
|
|
|
location.Location.Longitude,
|
|
|
|
location.Location.Latitude,
|
|
|
|
location.Location.Longitude,
|
|
|
|
)
|
|
|
|
case client.TypeMessagePhoto:
|
|
|
|
photo, _ := message.Content.(*client.MessagePhoto)
|
2020-01-09 21:16:40 +00:00
|
|
|
return formatter.Format(
|
|
|
|
photo.Caption.Text,
|
|
|
|
formatter.SortEntities(photo.Caption.Entities),
|
|
|
|
markupFunction,
|
|
|
|
)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageAudio:
|
|
|
|
audio, _ := message.Content.(*client.MessageAudio)
|
2020-01-09 21:16:40 +00:00
|
|
|
return formatter.Format(
|
|
|
|
audio.Caption.Text,
|
|
|
|
formatter.SortEntities(audio.Caption.Entities),
|
|
|
|
markupFunction,
|
|
|
|
)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageVideo:
|
|
|
|
video, _ := message.Content.(*client.MessageVideo)
|
2020-01-09 21:16:40 +00:00
|
|
|
return formatter.Format(
|
|
|
|
video.Caption.Text,
|
|
|
|
formatter.SortEntities(video.Caption.Entities),
|
|
|
|
markupFunction,
|
|
|
|
)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageDocument:
|
|
|
|
document, _ := message.Content.(*client.MessageDocument)
|
2020-01-09 21:16:40 +00:00
|
|
|
return formatter.Format(
|
|
|
|
document.Caption.Text,
|
|
|
|
formatter.SortEntities(document.Caption.Entities),
|
|
|
|
markupFunction,
|
|
|
|
)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageText:
|
|
|
|
text, _ := message.Content.(*client.MessageText)
|
2020-01-09 21:16:40 +00:00
|
|
|
return formatter.Format(
|
|
|
|
text.Text.Text,
|
|
|
|
formatter.SortEntities(text.Text.Entities),
|
|
|
|
markupFunction,
|
|
|
|
)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageVoiceNote:
|
|
|
|
voice, _ := message.Content.(*client.MessageVoiceNote)
|
2020-01-09 21:16:40 +00:00
|
|
|
return formatter.Format(
|
|
|
|
voice.Caption.Text,
|
|
|
|
formatter.SortEntities(voice.Caption.Entities),
|
|
|
|
markupFunction,
|
|
|
|
)
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageVideoNote:
|
|
|
|
return ""
|
|
|
|
case client.TypeMessageAnimation:
|
|
|
|
animation, _ := message.Content.(*client.MessageAnimation)
|
2020-01-09 21:16:40 +00:00
|
|
|
return formatter.Format(
|
|
|
|
animation.Caption.Text,
|
|
|
|
formatter.SortEntities(animation.Caption.Entities),
|
|
|
|
markupFunction,
|
|
|
|
)
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("unknown message (%s)", message.Content.MessageContentType())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) contentToFilename(content client.MessageContent) (*client.File, string) {
|
2021-12-05 03:27:14 +00:00
|
|
|
if content == nil {
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:13:45 +00:00
|
|
|
switch content.MessageContentType() {
|
|
|
|
case client.TypeMessageSticker:
|
|
|
|
sticker, _ := content.(*client.MessageSticker)
|
|
|
|
return sticker.Sticker.Sticker, "sticker.webp"
|
|
|
|
case client.TypeMessageVoiceNote:
|
|
|
|
voice, _ := content.(*client.MessageVoiceNote)
|
|
|
|
return voice.VoiceNote.Voice, fmt.Sprintf("voice note (%v s.).oga", voice.VoiceNote.Duration)
|
|
|
|
case client.TypeMessageVideoNote:
|
|
|
|
video, _ := content.(*client.MessageVideoNote)
|
|
|
|
return video.VideoNote.Video, fmt.Sprintf("video note (%v s.).mp4", video.VideoNote.Duration)
|
|
|
|
case client.TypeMessageAnimation:
|
|
|
|
animation, _ := content.(*client.MessageAnimation)
|
|
|
|
return animation.Animation.Animation, "animation.mp4"
|
|
|
|
case client.TypeMessagePhoto:
|
|
|
|
photo, _ := content.(*client.MessagePhoto)
|
|
|
|
sizes := photo.Photo.Sizes
|
|
|
|
if len(sizes) > 1 {
|
2019-12-02 16:25:55 +00:00
|
|
|
file := sizes[len(sizes)-1].Photo
|
2021-12-04 18:10:54 +00:00
|
|
|
return file, strconv.FormatInt(file.ID, 10) + ".jpg"
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
2019-12-02 16:32:32 +00:00
|
|
|
return nil, ""
|
2019-12-01 13:13:45 +00:00
|
|
|
case client.TypeMessageAudio:
|
|
|
|
audio, _ := content.(*client.MessageAudio)
|
|
|
|
return audio.Audio.Audio, audio.Audio.FileName
|
|
|
|
case client.TypeMessageVideo:
|
|
|
|
video, _ := content.(*client.MessageVideo)
|
|
|
|
return video.Video.Video, video.Video.FileName
|
|
|
|
case client.TypeMessageDocument:
|
|
|
|
document, _ := content.(*client.MessageDocument)
|
|
|
|
return document.Document.Document, document.Document.FileName
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) messageToPrefix(message *client.Message, fileString string) string {
|
|
|
|
prefix := []string{}
|
|
|
|
// message direction
|
|
|
|
var directionChar string
|
|
|
|
if message.IsOutgoing {
|
2022-01-03 04:08:55 +00:00
|
|
|
directionChar = "⇾ "
|
2019-12-01 13:13:45 +00:00
|
|
|
} else {
|
2022-01-03 04:08:55 +00:00
|
|
|
directionChar = "⇽ "
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
2021-12-04 18:10:54 +00:00
|
|
|
prefix = append(prefix, directionChar+strconv.FormatInt(message.ID, 10))
|
2019-12-01 13:13:45 +00:00
|
|
|
// show sender in group chats
|
2021-12-04 18:10:54 +00:00
|
|
|
if message.ChatID < 0 && message.Sender != nil {
|
|
|
|
var senderId int64
|
|
|
|
switch message.Sender.MessageSenderType() {
|
|
|
|
case client.TypeMessageSenderUser:
|
|
|
|
senderUser, _ := message.Sender.(*client.MessageSenderUser)
|
|
|
|
senderId = senderUser.UserID
|
|
|
|
case client.TypeMessageSenderChat:
|
|
|
|
senderChat, _ := message.Sender.(*client.MessageSenderChat)
|
|
|
|
senderId = senderChat.ChatID
|
|
|
|
}
|
|
|
|
prefix = append(prefix, c.formatContact(senderId))
|
2019-12-01 13:13:45 +00:00
|
|
|
}
|
|
|
|
// reply to
|
2021-12-04 18:10:54 +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))
|
|
|
|
}
|
|
|
|
// file
|
2019-12-01 13:13:45 +00:00
|
|
|
if fileString != "" {
|
|
|
|
prefix = append(prefix, "file: "+fileString)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(prefix, " | ")
|
|
|
|
}
|
|
|
|
|
2019-12-03 00:32:53 +00:00
|
|
|
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
|
2022-01-08 10:59:57 +00:00
|
|
|
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, "/") {
|
|
|
|
// 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 {
|
2022-01-08 10:59:57 +00:00
|
|
|
return nil
|
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
|
|
|
|
replySlice := replyRegex.FindStringSubmatch(text)
|
|
|
|
if len(replySlice) > 1 {
|
|
|
|
reply, _ = strconv.ParseInt(replySlice[1], 10, 64)
|
|
|
|
}
|
2019-12-03 16:48:41 +00:00
|
|
|
|
2022-01-08 10:59:57 +00:00
|
|
|
// attach a file
|
|
|
|
var file *client.InputFileRemote
|
|
|
|
if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
|
|
|
|
file = &client.InputFileRemote{
|
|
|
|
ID: 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
|
|
|
|
2022-01-08 10:59:57 +00:00
|
|
|
// remove first line from text
|
|
|
|
if file != nil || reply != 0 {
|
|
|
|
newlinePos := strings.Index(text, newlineChar)
|
|
|
|
if newlinePos != -1 {
|
|
|
|
text = text[newlinePos+1:]
|
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
|
|
|
formattedText := &client.FormattedText{
|
|
|
|
Text: text,
|
|
|
|
}
|
2019-12-07 16:37:14 +00:00
|
|
|
|
2022-01-08 10:59:57 +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
|
|
|
}
|
2022-01-08 10:59:57 +00:00
|
|
|
} else {
|
|
|
|
// compile our message
|
|
|
|
message = &client.InputMessageText{
|
|
|
|
Text: formattedText,
|
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
|
|
|
if chatID != 0 {
|
2019-12-20 23:44:21 +00:00
|
|
|
_, err := c.client.SendMessage(&client.SendMessageRequest{
|
2021-12-04 18:10:54 +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),
|
2022-01-08 10:59:57 +00:00
|
|
|
fmt.Sprintf("Not sent: %s", err.Error()),
|
2019-12-20 23:44:21 +00:00
|
|
|
c.xmpp,
|
|
|
|
)
|
|
|
|
}
|
2022-01-08 10:59:57 +00:00
|
|
|
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
|
|
|
|
|
|
|
func (c *Client) getLastMessages(id int64, query string, from int64, count int32) (*client.Messages, error) {
|
|
|
|
return c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
|
|
|
|
ChatID: id,
|
|
|
|
Query: query,
|
|
|
|
Sender: &client.MessageSenderUser{UserID: from},
|
|
|
|
Filter: &client.SearchMessagesFilterEmpty{},
|
|
|
|
Limit: count,
|
|
|
|
})
|
|
|
|
}
|