Assign IDs from Telegram to XMPP messages

This commit is contained in:
Bohdan Horbeshko 2023-03-03 21:41:45 -05:00
parent b3d66993e5
commit 6e32c62f8d
6 changed files with 28 additions and 16 deletions

View file

@ -183,10 +183,13 @@ func (c *Client) unsubscribe(chatID int64) error {
func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) { func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
for i := len(messages) - 1; i >= 0; i-- { for i := len(messages) - 1; i >= 0; i-- {
message := messages[i]
gateway.SendMessage( gateway.SendMessage(
c.jid, c.jid,
strconv.FormatInt(chatID, 10), strconv.FormatInt(chatID, 10),
c.formatMessage(0, 0, false, messages[i]), c.formatMessage(0, 0, false, message),
strconv.FormatInt(message.Id, 10),
c.xmpp, c.xmpp,
) )
} }

View file

@ -219,20 +219,20 @@ func (c *Client) interactor() {
if c.Session.Login != "" { if c.Session.Login != "" {
c.authorizer.PhoneNumber <- c.Session.Login c.authorizer.PhoneNumber <- c.Session.Login
} else { } else {
gateway.SendMessage(c.jid, "", "Please, enter your Telegram login via /login 12345", c.xmpp) gateway.SendMessage(c.jid, "", "Please, enter your Telegram login via /login 12345", "", c.xmpp)
} }
// stage 1: wait for auth code // stage 1: wait for auth code
case client.TypeAuthorizationStateWaitCode: case client.TypeAuthorizationStateWaitCode:
log.Warn("Waiting for authorization code...") log.Warn("Waiting for authorization code...")
gateway.SendMessage(c.jid, "", "Please, enter authorization code via /code 12345", c.xmpp) gateway.SendMessage(c.jid, "", "Please, enter authorization code via /code 12345", "", c.xmpp)
// stage 1b: wait for registration // stage 1b: wait for registration
case client.TypeAuthorizationStateWaitRegistration: case client.TypeAuthorizationStateWaitRegistration:
log.Warn("Waiting for full name...") log.Warn("Waiting for full name...")
gateway.SendMessage(c.jid, "", "This number is not registered yet! Please, enter your name via /setname John Doe", c.xmpp) gateway.SendMessage(c.jid, "", "This number is not registered yet! Please, enter your name via /setname John Doe", "", c.xmpp)
// stage 2: wait for 2fa // stage 2: wait for 2fa
case client.TypeAuthorizationStateWaitPassword: case client.TypeAuthorizationStateWaitPassword:
log.Warn("Waiting for 2FA password...") log.Warn("Waiting for 2FA password...")
gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp) gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", "", c.xmpp)
} }
} }
} }

View file

@ -242,7 +242,7 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
textContent.Text.Entities, textContent.Text.Entities,
markupFunction, markupFunction,
)) ))
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, "e" + strconv.FormatInt(update.MessageId, 10), c.xmpp)
} }
} }
@ -256,7 +256,7 @@ func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) {
deleteChar = "✗ " deleteChar = "✗ "
} }
text := deleteChar + strings.Join(int64SliceToStringSlice(update.MessageIds), ",") text := deleteChar + strings.Join(int64SliceToStringSlice(update.MessageIds), ",")
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, "", c.xmpp)
} }
} }

View file

@ -806,9 +806,11 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
ForceRead: true, ForceRead: true,
}) })
// forward message to XMPP // forward message to XMPP
gateway.SendMessageWithOOB(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp, oob) sId := strconv.FormatInt(message.Id, 10)
sChatId := strconv.FormatInt(chatId, 10)
gateway.SendMessageWithOOB(c.jid, sChatId, text, sId, c.xmpp, oob)
if auxText != "" { if auxText != "" {
gateway.SendMessage(c.jid, strconv.FormatInt(chatId, 10), auxText, c.xmpp) gateway.SendMessage(c.jid, sChatId, auxText, sId, c.xmpp)
} }
} }
@ -823,7 +825,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
// try to execute commands // try to execute commands
response, isCommand := c.ProcessChatCommand(chatID, text) response, isCommand := c.ProcessChatCommand(chatID, text)
if response != "" { if response != "" {
gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp) gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, "", c.xmpp)
} }
// do not send on success // do not send on success
if isCommand { if isCommand {
@ -849,6 +851,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid, returnJid,
strconv.FormatInt(chatID, 10), strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()), fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()),
"",
c.xmpp, c.xmpp,
) )
return nil return nil
@ -861,6 +864,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid, returnJid,
strconv.FormatInt(chatID, 10), strconv.FormatInt(chatID, 10),
fmt.Sprintf("Received status code %v", response.StatusCode), fmt.Sprintf("Received status code %v", response.StatusCode),
"",
c.xmpp, c.xmpp,
) )
return nil return nil
@ -872,6 +876,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid, returnJid,
strconv.FormatInt(chatID, 10), strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to create a temporary directory: %s", err.Error()), fmt.Sprintf("Failed to create a temporary directory: %s", err.Error()),
"",
c.xmpp, c.xmpp,
) )
return nil return nil
@ -882,6 +887,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid, returnJid,
strconv.FormatInt(chatID, 10), strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to create a temporary file: %s", err.Error()), fmt.Sprintf("Failed to create a temporary file: %s", err.Error()),
"",
c.xmpp, c.xmpp,
) )
return nil return nil
@ -893,6 +899,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid, returnJid,
strconv.FormatInt(chatID, 10), strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to write a temporary file: %s", err.Error()), fmt.Sprintf("Failed to write a temporary file: %s", err.Error()),
"",
c.xmpp, c.xmpp,
) )
return nil return nil
@ -943,6 +950,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid, returnJid,
strconv.FormatInt(chatID, 10), strconv.FormatInt(chatID, 10),
fmt.Sprintf("Not sent: %s", err.Error()), fmt.Sprintf("Not sent: %s", err.Error()),
"",
c.xmpp, c.xmpp,
) )
} }

View file

@ -27,16 +27,16 @@ var Jid *stanza.Jid
var DirtySessions = false var DirtySessions = false
// SendMessage creates and sends a message stanza // SendMessage creates and sends a message stanza
func SendMessage(to string, from string, body string, component *xmpp.Component) { func SendMessage(to string, from string, body string, id string, component *xmpp.Component) {
sendMessageWrapper(to, from, body, component, "") sendMessageWrapper(to, from, body, id, component, "")
} }
// SendMessageWithOOB creates and sends a message stanza with OOB URL // SendMessageWithOOB creates and sends a message stanza with OOB URL
func SendMessageWithOOB(to string, from string, body string, component *xmpp.Component, oob string) { func SendMessageWithOOB(to string, from string, body string, id string, component *xmpp.Component, oob string) {
sendMessageWrapper(to, from, body, component, oob) sendMessageWrapper(to, from, body, id, component, oob)
} }
func sendMessageWrapper(to string, from string, body string, component *xmpp.Component, oob string) { func sendMessageWrapper(to string, from string, body string, id string, component *xmpp.Component, oob string) {
componentJid := Jid.Full() componentJid := Jid.Full()
var logFrom string var logFrom string
@ -59,6 +59,7 @@ func sendMessageWrapper(to string, from string, body string, component *xmpp.Com
From: messageFrom, From: messageFrom,
To: to, To: to,
Type: "chat", Type: "chat",
Id: id,
}, },
Body: body, Body: body,
} }

View file

@ -106,7 +106,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
if err == nil && toJid.Bare() == gatewayJid && (strings.HasPrefix(msg.Body, "/") || strings.HasPrefix(msg.Body, "!")) { if err == nil && toJid.Bare() == gatewayJid && (strings.HasPrefix(msg.Body, "/") || strings.HasPrefix(msg.Body, "!")) {
response := session.ProcessTransportCommand(msg.Body, resource) response := session.ProcessTransportCommand(msg.Body, resource)
if response != "" { if response != "" {
gateway.SendMessage(msg.From, "", response, component) gateway.SendMessage(msg.From, "", response, "", component)
} }
return return
} }