Make /s replace the whole message; fix replies and whitespace corruption

This commit is contained in:
Bohdan Horbeshko 2022-01-08 05:59:57 -05:00
parent ee6653c0c6
commit 9f04ed51bd
3 changed files with 84 additions and 78 deletions

View file

@ -3,10 +3,10 @@ package telegram
import (
"fmt"
"github.com/pkg/errors"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
@ -114,6 +114,24 @@ func parseCommand(cmdline string) (string, []string) {
return bodyFields[0][1:], bodyFields[1:]
}
func rawCmdArguments(cmdline string, start uint8) (string) {
var state uint
// /cmd ababa galamaga
// 01 2 3 45
startState := uint(3 + 2 * start)
for i, r := range cmdline {
isOdd := state % 2 == 1
isSpace := unicode.IsSpace(r)
if (!isOdd && !isSpace) || (isOdd && isSpace) {
state += 1
}
if state == startState {
return cmdline[i:]
}
}
return ""
}
func (c *Client) unsubscribe(chatID int64) {
gateway.SendPresence(
c.xmpp,
@ -263,7 +281,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
}
_, err := c.client.SetBio(&client.SetBioRequest{
Bio: strings.Join(args, " "),
Bio: rawCmdArguments(cmdline, 0),
})
if err != nil {
return errors.Wrap(err, "Couldn't set bio").Error()
@ -371,13 +389,9 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
if c.me == nil {
return "@me is not initialized", true
}
if len(args) < 2 {
if len(args) < 1 {
return "Not enough arguments", true
}
regex, err := regexp.Compile(args[0])
if err != nil {
return err.Error(), true
}
messages, err := c.getLastMessages(chatID, "", c.me.ID, 1)
if err != nil {
@ -392,13 +406,17 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return "Last message is empty", true
}
messageText, ok := message.Content.(*client.MessageText)
if !ok {
return "Last message is not a text!", true
}
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "")
text := regex.ReplaceAllString(messageText.Text.Text, strings.Join(args[1:], " "))
c.ProcessOutgoingMessage(chatID, text, message.ID, "")
if content != nil {
c.client.EditMessageText(&client.EditMessageTextRequest{
ChatID: chatID,
MessageID: message.ID,
InputMessageContent: content,
})
} else {
return "Message processing error", true
}
// add @contact
case "add":
if len(args) < 1 {
@ -441,7 +459,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
_, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
Title: args[0],
Description: strings.Join(args[1:], " "),
Description: rawCmdArguments(cmdline, 1),
})
if err != nil {
return err.Error(), true
@ -454,7 +472,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
_, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
Title: args[0],
Description: strings.Join(args[1:], " "),
Description: rawCmdArguments(cmdline, 1),
IsChannel: true,
})
if err != nil {

View file

@ -25,7 +25,7 @@ import (
var errOffline = errors.New("TDlib instance is offline")
var spaceRegex = regexp.MustCompile(`\s+`)
var replyRegex = regexp.MustCompile("> ?([0-9]{10,})")
var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n")
const newlineChar string = "\n"
@ -483,42 +483,26 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
}
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int64, returnJid string) {
if messageID == 0 && strings.HasPrefix(text, "/") {
// try to execute a command
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
response, isCommand := c.ProcessChatCommand(chatID, text)
if response != "" {
gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp)
}
// do not send on success
if isCommand {
return
return nil
}
}
if !c.Online() {
// we're offline
return
}
log.Warnf("Sending message to chat %v", chatID)
log.Warnf("Send message to chat %v", chatID)
if messageID != 0 {
formattedText := &client.FormattedText{
Text: text,
}
// compile our message
message := &client.InputMessageText{
Text: formattedText,
}
c.client.EditMessageText(&client.EditMessageTextRequest{
ChatID: chatID,
MessageID: messageID,
InputMessageContent: message,
})
} else {
// quotations
var reply int64
replySlice := replyRegex.FindStringSubmatch(text)
@ -528,7 +512,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
// attach a file
var file *client.InputFileRemote
if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
file = &client.InputFileRemote{
ID: text,
}
@ -560,6 +544,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
}
}
if chatID != 0 {
_, err := c.client.SendMessage(&client.SendMessageRequest{
ChatID: chatID,
ReplyToMessageID: reply,
@ -569,10 +554,13 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
gateway.SendMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Message not sent: %s", err.Error()),
fmt.Sprintf("Not sent: %s", err.Error()),
c.xmpp,
)
}
return nil
} else {
return message
}
}

View file

@ -77,7 +77,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
if len(toParts) > 1 {
toIDInt, err := strconv.ParseInt(toID, 10, 64)
if err == nil {
session.ProcessOutgoingMessage(toIDInt, msg.Body, 0, msg.From)
session.ProcessOutgoingMessage(toIDInt, msg.Body, msg.From)
return
}
log.WithFields(log.Fields{