diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-01-08 13:59:57 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2022-01-08 13:59:57 +0300 |
commit | 9f04ed51bd26923afec70f086bdeba934fec32ba (patch) | |
tree | d3160232bbe0b64002b325c992c21194867a9989 /telegram/commands.go | |
parent | ee6653c0c6c09a5a38d1c1ef2be5318edfbe2e14 (diff) |
Make /s replace the whole message; fix replies and whitespace corruption
Diffstat (limited to 'telegram/commands.go')
-rw-r--r-- | telegram/commands.go | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/telegram/commands.go b/telegram/commands.go index cf4ebc8..8baf068 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -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 { |