|
|
|
@ -44,6 +44,12 @@ var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n")
|
|
|
|
|
const newlineChar string = "\n"
|
|
|
|
|
const messageHeaderSeparator string = " | "
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
ChatTypeOther byte = iota
|
|
|
|
|
ChatTypePM
|
|
|
|
|
ChatTypeGroup
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
@ -121,10 +127,10 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli
|
|
|
|
|
return chat, user, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsPM checks if a chat is PM
|
|
|
|
|
func (c *Client) IsPM(id int64) (bool, error) {
|
|
|
|
|
// GetChatType checks if a chat is PM or group
|
|
|
|
|
func (c *Client) GetChatType(id int64) (byte, error) {
|
|
|
|
|
if !c.Online() || id == 0 {
|
|
|
|
|
return false, errOffline
|
|
|
|
|
return ChatTypeOther, errOffline
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
@ -135,7 +141,7 @@ func (c *Client) IsPM(id int64) (bool, error) {
|
|
|
|
|
ChatId: id,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
return ChatTypeOther, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.cache.SetChat(id, chat)
|
|
|
|
@ -143,9 +149,12 @@ func (c *Client) IsPM(id int64) (bool, error) {
|
|
|
|
|
|
|
|
|
|
chatType := chat.Type.ChatTypeType()
|
|
|
|
|
if chatType == client.TypeChatTypePrivate || chatType == client.TypeChatTypeSecret {
|
|
|
|
|
return true, nil
|
|
|
|
|
return ChatTypePM, nil
|
|
|
|
|
}
|
|
|
|
|
if c.IsGroup(chat) {
|
|
|
|
|
return ChatTypeGroup, nil
|
|
|
|
|
}
|
|
|
|
|
return false, nil
|
|
|
|
|
return ChatTypeOther, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) userStatusToText(status client.UserStatus, chatID int64) (string, string, string) {
|
|
|
|
@ -294,25 +303,52 @@ func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, o
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) SendMUCStatuses(chatID int64) {
|
|
|
|
|
// JoinMUC saves MUC join fact and sends initialization data
|
|
|
|
|
func (c *Client) JoinMUC(chatId int64, resource string) {
|
|
|
|
|
// save the nickname in this MUC, also as a marker of join
|
|
|
|
|
c.locks.mucResourcesLock.Lock()
|
|
|
|
|
oldMap, ok := c.mucResources[chatId]
|
|
|
|
|
if ok {
|
|
|
|
|
_, ok := oldMap[resource]
|
|
|
|
|
if ok {
|
|
|
|
|
// already joined, initializing anyway
|
|
|
|
|
} else {
|
|
|
|
|
oldMap[resource] = true
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
newMap := make(map[string]bool)
|
|
|
|
|
newMap[resource] = true
|
|
|
|
|
c.mucResources[chatId] = newMap
|
|
|
|
|
}
|
|
|
|
|
c.locks.mucResourcesLock.Unlock()
|
|
|
|
|
|
|
|
|
|
c.sendMUCStatuses(chatId)
|
|
|
|
|
|
|
|
|
|
messages, err := c.getNLastMessages(chatId, 20)
|
|
|
|
|
if err == nil {
|
|
|
|
|
c.sendMessagesReverse(chatId, messages, false, c.jid+"/"+resource)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) sendMUCStatuses(chatID int64) {
|
|
|
|
|
sChatId := strconv.FormatInt(chatID, 10)
|
|
|
|
|
myNickname := "me"
|
|
|
|
|
if c.me != nil {
|
|
|
|
|
myNickname := c.me.FirstName
|
|
|
|
|
if c.me.LastName != "" {
|
|
|
|
|
myNickname = myNickname + " " + c.me.LastName
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
myAffiliation := "member"
|
|
|
|
|
|
|
|
|
|
members, err := c.client.SearchChatMembers(&client.SearchChatMembersRequest{
|
|
|
|
|
ChatId: chatID,
|
|
|
|
|
Limit: 200,
|
|
|
|
|
Filter: &client.ChatMembersFilterMembers{},
|
|
|
|
|
})
|
|
|
|
|
if err == nil {
|
|
|
|
|
chatIDString := strconv.FormatInt(chatID, 10)
|
|
|
|
|
gatewayJidSuffix := "@" + gateway.Jid.Full()
|
|
|
|
|
|
|
|
|
|
myNickname := "me"
|
|
|
|
|
if c.me != nil {
|
|
|
|
|
myNickname := c.me.FirstName
|
|
|
|
|
if c.me.LastName != "" {
|
|
|
|
|
myNickname = myNickname + " " + c.me.LastName
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
myAffiliation := "member"
|
|
|
|
|
|
|
|
|
|
for _, member := range members.Members {
|
|
|
|
|
var senderId int64
|
|
|
|
|
switch member.MemberId.MessageSenderType() {
|
|
|
|
@ -324,7 +360,7 @@ func (c *Client) SendMUCStatuses(chatID int64) {
|
|
|
|
|
senderId = memberChat.ChatId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nickname := c.formatContact(senderId)
|
|
|
|
|
nickname := c.getMUCNickname(senderId)
|
|
|
|
|
affiliation := c.memberStatusToAffiliation(member.Status)
|
|
|
|
|
if c.me != nil && senderId == c.me.Id {
|
|
|
|
|
myNickname = nickname
|
|
|
|
@ -335,25 +371,29 @@ func (c *Client) SendMUCStatuses(chatID int64) {
|
|
|
|
|
gateway.SendPresence(
|
|
|
|
|
c.xmpp,
|
|
|
|
|
c.jid,
|
|
|
|
|
gateway.SPFrom(chatIDString),
|
|
|
|
|
gateway.SPFrom(sChatId),
|
|
|
|
|
gateway.SPResource(nickname),
|
|
|
|
|
gateway.SPImmed(true),
|
|
|
|
|
gateway.SPMUCAffiliation(affiliation),
|
|
|
|
|
gateway.SPMUCJid(strconv.FormatInt(senderId, 10) + gatewayJidSuffix),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// according to the spec, own member entry should be sent the last
|
|
|
|
|
gateway.SendPresence(
|
|
|
|
|
c.xmpp,
|
|
|
|
|
c.jid,
|
|
|
|
|
gateway.SPFrom(chatIDString),
|
|
|
|
|
gateway.SPResource(myNickname),
|
|
|
|
|
gateway.SPImmed(true),
|
|
|
|
|
gateway.SPMUCAffiliation(myAffiliation),
|
|
|
|
|
gateway.SPMUCStatusCodes([]uint16{100, 110, 210}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// according to the spec, own member entry should be sent the last
|
|
|
|
|
gateway.SendPresence(
|
|
|
|
|
c.xmpp,
|
|
|
|
|
c.jid,
|
|
|
|
|
gateway.SPFrom(sChatId),
|
|
|
|
|
gateway.SPResource(myNickname),
|
|
|
|
|
gateway.SPImmed(true),
|
|
|
|
|
gateway.SPMUCAffiliation(myAffiliation),
|
|
|
|
|
gateway.SPMUCStatusCodes([]uint16{100, 110, 210}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) getMUCNickname(chatID int64) string {
|
|
|
|
|
return c.formatContact(chatID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) formatContact(chatID int64) string {
|
|
|
|
@ -917,13 +957,13 @@ func (c *Client) countCharsInLines(lines *[]string) (count int) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, replyMsg *client.Message) (string, int, int) {
|
|
|
|
|
isPM, err := c.IsPM(message.ChatId)
|
|
|
|
|
chatType, err := c.GetChatType(message.ChatId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("Could not determine if chat is PM: %v", err)
|
|
|
|
|
log.Errorf("Could not determine chat type: %v", err)
|
|
|
|
|
}
|
|
|
|
|
isCarbonsEnabled := gateway.MessageOutgoingPermissionVersion > 0 && c.Session.Carbons
|
|
|
|
|
// with carbons, hide for all messages in PM and only for outgoing in group chats
|
|
|
|
|
hideSender := isCarbonsEnabled && (message.IsOutgoing || isPM)
|
|
|
|
|
hideSender := (isCarbonsEnabled && (message.IsOutgoing || chatType == ChatTypePM)) || (c.Session.MUC && chatType == ChatTypeGroup)
|
|
|
|
|
|
|
|
|
|
var replyStart, replyEnd int
|
|
|
|
|
prefix := []string{}
|
|
|
|
@ -944,7 +984,7 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !isPM || !c.Session.HideIds {
|
|
|
|
|
if chatType != ChatTypePM || !c.Session.HideIds {
|
|
|
|
|
prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10))
|
|
|
|
|
}
|
|
|
|
|
// show sender in group chats
|
|
|
|
@ -999,8 +1039,20 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File {
|
|
|
|
|
|
|
|
|
|
// ProcessIncomingMessage transfers a message to XMPP side and marks it as read on Telegram side
|
|
|
|
|
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
|
|
|
|
|
isCarbon := gateway.MessageOutgoingPermissionVersion > 0 && c.Session.Carbons && message.IsOutgoing
|
|
|
|
|
jids := c.getCarbonFullJids(isCarbon, "")
|
|
|
|
|
c.sendMessageToGateway(chatId, message, false, "", []string{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) sendMessageToGateway(chatId int64, message *client.Message, delay bool, groupChatFrom string, groupChatTos []string) {
|
|
|
|
|
var isCarbon bool
|
|
|
|
|
var jids []string
|
|
|
|
|
var isGroupchat bool
|
|
|
|
|
if len(groupChatTos) == 0 {
|
|
|
|
|
isCarbon = gateway.MessageOutgoingPermissionVersion > 0 && c.Session.Carbons && message.IsOutgoing
|
|
|
|
|
jids = c.getCarbonFullJids(isCarbon, "")
|
|
|
|
|
} else {
|
|
|
|
|
isGroupchat = true
|
|
|
|
|
jids = groupChatTos
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var text, oob, auxText string
|
|
|
|
|
|
|
|
|
@ -1073,12 +1125,22 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
|
|
|
|
|
|
|
|
|
|
// forward message to XMPP
|
|
|
|
|
sId := strconv.FormatInt(message.Id, 10)
|
|
|
|
|
sChatId := strconv.FormatInt(chatId, 10)
|
|
|
|
|
var from string
|
|
|
|
|
if groupChatFrom == "" {
|
|
|
|
|
from = strconv.FormatInt(chatId, 10)
|
|
|
|
|
} else {
|
|
|
|
|
from = groupChatFrom
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var timestamp int64
|
|
|
|
|
if delay {
|
|
|
|
|
timestamp = int64(message.Date)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, jid := range jids {
|
|
|
|
|
gateway.SendMessageWithOOB(jid, sChatId, text, sId, c.xmpp, reply, oob, isCarbon)
|
|
|
|
|
gateway.SendMessageWithOOB(jid, from, text, sId, c.xmpp, reply, timestamp, oob, isCarbon, isGroupchat)
|
|
|
|
|
if auxText != "" {
|
|
|
|
|
gateway.SendMessage(jid, sChatId, auxText, sId, c.xmpp, reply, isCarbon)
|
|
|
|
|
gateway.SendMessage(jid, from, auxText, sId, c.xmpp, reply, timestamp, isCarbon, isGroupchat)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -1294,6 +1356,36 @@ func (c *Client) getLastMessages(id int64, query string, from int64, count int32
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) getNLastMessages(chatID int64, limit int32) ([]*client.Message, error) {
|
|
|
|
|
var newMessages *client.Messages
|
|
|
|
|
var messages []*client.Message
|
|
|
|
|
var err error
|
|
|
|
|
var fromId int64
|
|
|
|
|
|
|
|
|
|
for _ = range make([]struct{}, limit) { // safety limit
|
|
|
|
|
if len(messages) > 0 {
|
|
|
|
|
fromId = messages[len(messages)-1].Id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newMessages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{
|
|
|
|
|
ChatId: chatID,
|
|
|
|
|
FromMessageId: fromId,
|
|
|
|
|
Limit: limit,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
messages = append(messages, newMessages.Messages...)
|
|
|
|
|
|
|
|
|
|
if len(newMessages.Messages) == 0 || len(messages) >= int(limit) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messages, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
@ -1652,3 +1744,39 @@ func (c *Client) memberStatusToAffiliation(memberStatus client.ChatMemberStatus)
|
|
|
|
|
}
|
|
|
|
|
return "member"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message, plain bool, toJid string) {
|
|
|
|
|
sChatId := strconv.FormatInt(chatID, 10)
|
|
|
|
|
var mucJid string
|
|
|
|
|
if toJid != "" {
|
|
|
|
|
mucJid = sChatId + "@" + gateway.Jid.Bare()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := len(messages) - 1; i >= 0; i-- {
|
|
|
|
|
message := messages[i]
|
|
|
|
|
|
|
|
|
|
if plain {
|
|
|
|
|
reply, _ := c.getMessageReply(message)
|
|
|
|
|
|
|
|
|
|
gateway.SendMessage(
|
|
|
|
|
c.jid,
|
|
|
|
|
sChatId,
|
|
|
|
|
c.formatMessage(0, 0, false, message),
|
|
|
|
|
strconv.FormatInt(message.Id, 10),
|
|
|
|
|
c.xmpp,
|
|
|
|
|
reply,
|
|
|
|
|
0,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
c.sendMessageToGateway(
|
|
|
|
|
chatID,
|
|
|
|
|
message,
|
|
|
|
|
true,
|
|
|
|
|
mucJid + "/" + c.getMUCNickname(c.getSenderId(message)),
|
|
|
|
|
[]string{toJid},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|