Support commands in mapped chats

This commit is contained in:
bodqhrohro 2019-12-03 02:32:53 +02:00
parent 40b3c6a768
commit 90f0490e16
3 changed files with 39 additions and 10 deletions

View file

@ -97,8 +97,15 @@ func helpString(ht helpType) string {
return str.String()
}
// ProcessTransportCommand executes commands sent directly to the component
func (c *Client) ProcessTransportCommand(cmd string, args []string) string {
func parseCommand(cmdline string) (string, []string) {
bodyFields := strings.Fields(cmdline)
return bodyFields[0][1:], bodyFields[1:]
}
// ProcessTransportCommand executes a command sent directly to the component
// and returns a response
func (c *Client) ProcessTransportCommand(cmdline string) string {
cmd, args := parseCommand(cmdline)
switch cmd {
case "login", "code", "password":
if cmd == "login" && c.Session.Login != "" {
@ -127,3 +134,15 @@ func (c *Client) ProcessTransportCommand(cmd string, args []string) string {
return ""
}
// ProcessChatCommand executes a command sent in a mapped chat
// and returns a response and the status of command support
func (c *Client) ProcessChatCommand(cmdline string) (string, bool) {
cmd, _ := parseCommand(cmdline)
switch cmd {
case "help":
return helpString(helpTypeChat), false
}
return "", true
}

View file

@ -405,6 +405,15 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
return strings.Join(prefix, " | ")
}
func (c *Client) ProcessOutgoingMessage(chatID int, text string, messageID int) {
// TODO
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
func (c *Client) ProcessOutgoingMessage(chatID int, text string, messageID int, returnJid string) {
if strings.HasPrefix(text, "/") {
response, isCommand := c.ProcessChatCommand(text)
if response != "" {
gateway.SendMessage(returnJid, strconv.Itoa(int(chatID)), response, c.xmpp)
}
if isCommand {
return
}
}
}

View file

@ -65,15 +65,16 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
toID := toParts[0]
if len(toParts) > 1 {
toIDInt, err := strconv.Atoi(toID)
if err != nil {
session.ProcessOutgoingMessage(toIDInt, msg.Body, 0)
if err == nil {
session.ProcessOutgoingMessage(toIDInt, msg.Body, 0, msg.From)
return
}
log.WithFields(log.Fields{
"toID": toID,
}).Error(errors.Wrap(err, "Invalid to JID!"))
} else if toID == gateway.Jid.Bare() {
bodyFields := strings.Fields(msg.Body)
cmd := bodyFields[0]
if strings.HasPrefix(cmd, "/") {
response := session.ProcessTransportCommand(cmd[1:], bodyFields[1:])
if strings.HasPrefix(msg.Body, "/") {
response := session.ProcessTransportCommand(msg.Body)
if response != "" {
gateway.SendMessage(msg.From, "", response, component)
}