|
|
|
@ -261,6 +261,46 @@ func (c *Client) formatContact(chatID int64) string {
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) getSenderId(message *client.Message) (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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) formatSender(message *client.Message) string {
|
|
|
|
|
return c.formatContact(c.getSenderId(message))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) getMessageReply(message *client.Message) (reply *gateway.Reply, replyMsg *client.Message) {
|
|
|
|
|
if message.ReplyToMessageId != 0 {
|
|
|
|
|
var err error
|
|
|
|
|
replyMsg, err = c.client.GetMessage(&client.GetMessageRequest{
|
|
|
|
|
ChatId: message.ChatId,
|
|
|
|
|
MessageId: message.ReplyToMessageId,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("<error fetching message: %s>", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply = &gateway.Reply {
|
|
|
|
|
Author: fmt.Sprintf("%v@%s", c.getSenderId(replyMsg), gateway.Jid.Full()),
|
|
|
|
|
Id: strconv.FormatInt(message.ReplyToMessageId, 10),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, message *client.Message) string {
|
|
|
|
|
var err error
|
|
|
|
|
if message == nil {
|
|
|
|
@ -279,18 +319,7 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
|
|
|
|
|
|
|
|
|
|
var str strings.Builder
|
|
|
|
|
// add messageid and sender
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(senderId)))
|
|
|
|
|
str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatSender(message)))
|
|
|
|
|
// add date
|
|
|
|
|
if !preview {
|
|
|
|
|
str.WriteString(
|
|
|
|
@ -681,7 +710,7 @@ func (c *Client) contentToFile(content client.MessageContent) (*client.File, *cl
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string) string {
|
|
|
|
|
func (c *Client) messageToPrefix(message *client.Message, previewString string, fileString string, replyMsg *client.Message) string {
|
|
|
|
|
prefix := []string{}
|
|
|
|
|
// message direction
|
|
|
|
|
var directionChar string
|
|
|
|
@ -700,21 +729,12 @@ func (c *Client) messageToPrefix(message *client.Message, previewString string,
|
|
|
|
|
}
|
|
|
|
|
prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10))
|
|
|
|
|
// show sender in group chats
|
|
|
|
|
if message.ChatId < 0 && message.SenderId != nil {
|
|
|
|
|
var senderId int64
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
prefix = append(prefix, c.formatContact(senderId))
|
|
|
|
|
if message.ChatId < 0 {
|
|
|
|
|
prefix = append(prefix, c.formatSender(message))
|
|
|
|
|
}
|
|
|
|
|
// reply to
|
|
|
|
|
if message.ReplyToMessageId != 0 {
|
|
|
|
|
prefix = append(prefix, "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, nil))
|
|
|
|
|
prefix = append(prefix, "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, replyMsg))
|
|
|
|
|
}
|
|
|
|
|
if message.ForwardInfo != nil {
|
|
|
|
|
prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo))
|
|
|
|
@ -750,6 +770,9 @@ 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) {
|
|
|
|
|
var text, oob, auxText string
|
|
|
|
|
|
|
|
|
|
reply, replyMsg := c.getMessageReply(message)
|
|
|
|
|
|
|
|
|
|
content := message.Content
|
|
|
|
|
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
|
|
|
|
|
chat, err := c.client.GetChat(&client.GetChatRequest{
|
|
|
|
@ -783,7 +806,7 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
|
|
|
|
|
text = oob
|
|
|
|
|
} else if !c.Session.RawMessages {
|
|
|
|
|
var prefix strings.Builder
|
|
|
|
|
prefix.WriteString(c.messageToPrefix(message, previewName, fileName))
|
|
|
|
|
prefix.WriteString(c.messageToPrefix(message, previewName, fileName, replyMsg))
|
|
|
|
|
if text != "" {
|
|
|
|
|
// \n if it is groupchat and message is not empty
|
|
|
|
|
if chatId < 0 {
|
|
|
|
@ -808,9 +831,9 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
|
|
|
|
|
// forward message to XMPP
|
|
|
|
|
sId := strconv.FormatInt(message.Id, 10)
|
|
|
|
|
sChatId := strconv.FormatInt(chatId, 10)
|
|
|
|
|
gateway.SendMessageWithOOB(c.jid, sChatId, text, sId, c.xmpp, oob)
|
|
|
|
|
gateway.SendMessageWithOOB(c.jid, sChatId, text, sId, c.xmpp, reply, oob)
|
|
|
|
|
if auxText != "" {
|
|
|
|
|
gateway.SendMessage(c.jid, sChatId, auxText, sId, c.xmpp)
|
|
|
|
|
gateway.SendMessage(c.jid, sChatId, auxText, sId, c.xmpp, reply)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -825,7 +848,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
|
|
|
|
// try to execute commands
|
|
|
|
|
response, isCommand := c.ProcessChatCommand(chatID, text)
|
|
|
|
|
if response != "" {
|
|
|
|
|
gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, "", c.xmpp)
|
|
|
|
|
gateway.SendTextMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp)
|
|
|
|
|
}
|
|
|
|
|
// do not send on success
|
|
|
|
|
if isCommand {
|
|
|
|
@ -847,11 +870,10 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
|
|
|
|
if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
|
|
|
|
|
response, err := http.Get(text)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gateway.SendMessage(
|
|
|
|
|
gateway.SendTextMessage(
|
|
|
|
|
returnJid,
|
|
|
|
|
strconv.FormatInt(chatID, 10),
|
|
|
|
|
fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()),
|
|
|
|
|
"",
|
|
|
|
|
c.xmpp,
|
|
|
|
|
)
|
|
|
|
|
return nil
|
|
|
|
@ -860,11 +882,10 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
|
gateway.SendMessage(
|
|
|
|
|
gateway.SendTextMessage(
|
|
|
|
|
returnJid,
|
|
|
|
|
strconv.FormatInt(chatID, 10),
|
|
|
|
|
fmt.Sprintf("Received status code %v", response.StatusCode),
|
|
|
|
|
"",
|
|
|
|
|
c.xmpp,
|
|
|
|
|
)
|
|
|
|
|
return nil
|
|
|
|
@ -872,22 +893,20 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
|
|
|
|
|
|
|
|
|
tempDir, err := ioutil.TempDir("", "telegabber-*")
|
|
|
|
|
if err != nil {
|
|
|
|
|
gateway.SendMessage(
|
|
|
|
|
gateway.SendTextMessage(
|
|
|
|
|
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(
|
|
|
|
|
gateway.SendTextMessage(
|
|
|
|
|
returnJid,
|
|
|
|
|
strconv.FormatInt(chatID, 10),
|
|
|
|
|
fmt.Sprintf("Failed to create a temporary file: %s", err.Error()),
|
|
|
|
|
"",
|
|
|
|
|
c.xmpp,
|
|
|
|
|
)
|
|
|
|
|
return nil
|
|
|
|
@ -895,11 +914,10 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(tempFile, response.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gateway.SendMessage(
|
|
|
|
|
gateway.SendTextMessage(
|
|
|
|
|
returnJid,
|
|
|
|
|
strconv.FormatInt(chatID, 10),
|
|
|
|
|
fmt.Sprintf("Failed to write a temporary file: %s", err.Error()),
|
|
|
|
|
"",
|
|
|
|
|
c.xmpp,
|
|
|
|
|
)
|
|
|
|
|
return nil
|
|
|
|
@ -946,11 +964,10 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
|
|
|
|
InputMessageContent: message,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
gateway.SendMessage(
|
|
|
|
|
gateway.SendTextMessage(
|
|
|
|
|
returnJid,
|
|
|
|
|
strconv.FormatInt(chatID, 10),
|
|
|
|
|
fmt.Sprintf("Not sent: %s", err.Error()),
|
|
|
|
|
"",
|
|
|
|
|
c.xmpp,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|