aboutsummaryrefslogtreecommitdiff
path: root/xmpp
diff options
context:
space:
mode:
Diffstat (limited to 'xmpp')
-rw-r--r--xmpp/handlers.go91
1 files changed, 75 insertions, 16 deletions
diff --git a/xmpp/handlers.go b/xmpp/handlers.go
index a062f0c..c71fc19 100644
--- a/xmpp/handlers.go
+++ b/xmpp/handlers.go
@@ -7,6 +7,7 @@ import (
"fmt"
"github.com/pkg/errors"
"io"
+ "sort"
"strconv"
"strings"
@@ -701,31 +702,89 @@ func handleSetQueryCommand(s xmpp.Sender, iq *stanza.IQ, command *stanza.Command
log.Debugf("command: %#v", command)
+ bare, resource, ok := gateway.SplitJID(iq.From)
+ if !ok {
+ return
+ }
+
+ var cmdString string
if command.Action == "" || command.Action == stanza.CommandActionExecute {
_, ok := toToID(iq.To)
if !ok {
- bare, resource, ok := gateway.SplitJID(iq.From)
- if !ok {
- return
+ cmd, ok := telegram.GetCommand(telegram.CommandTypeTransport, command.Node)
+ if ok && cmd.RequiredArgs > 0 {
+ var fields []*stanza.Field
+ for i, arg := range cmd.Arguments {
+ fields = append(fields, &stanza.Field{
+ Var: strconv.FormatInt(int64(i), 10),
+ Label: arg,
+ })
+ }
+ answer.Payload = &stanza.Command{
+ SessionId: command.Node,
+ Node: command.Node,
+ Status: stanza.CommandStatusExecuting,
+ CommandElement: &stanza.Form{
+ Title: command.Node,
+ Instructions: []string{cmd.Description},
+ Fields: fields,
+ },
+ }
+ } else {
+ cmdString = "/" + command.Node
}
+ }
+ } else if command.Action == stanza.CommandActionComplete {
+ _, ok := toToID(iq.To)
+ if !ok {
+ form, ok := command.CommandElement.(*stanza.Form)
+ if ok {
+ // just for the case the client messed the order somehow
+ sort.Slice(form.Fields, func(i int, j int) bool {
+ iField := form.Fields[i]
+ jField := form.Fields[j]
+ if iField != nil && jField != nil {
+ ii, iErr := strconv.ParseInt(iField.Var, 10, 64)
+ ji, jErr := strconv.ParseInt(jField.Var, 10, 64)
+ return iErr == nil && jErr == nil && ii < ji
+ }
+ return false
+ })
+
+ var cmd strings.Builder
+ cmd.WriteString("/")
+ cmd.WriteString(command.Node)
+ for _, field := range form.Fields {
+ cmd.WriteString(" ")
+ if len(field.ValuesList) > 0 {
+ cmd.WriteString(field.ValuesList[0])
+ }
+ }
- session, ok := sessions[bare]
- if !ok {
- return
+ cmdString = cmd.String()
}
+ }
+ }
- response := session.ProcessTransportCommand("/" + command.Node, resource)
+ if cmdString != "" {
+ session, ok := sessions[bare]
+ if !ok {
+ return
+ }
- answer.Payload = &stanza.Command{
- Node: command.Node,
- Status: stanza.CommandStatusCompleted,
- CommandElement: &stanza.Note{
- Text: response,
- Type: stanza.CommandNoteTypeInfo,
- },
- }
- log.Debugf("command response: %#v", answer.Payload)
+ response := session.ProcessTransportCommand(cmdString, resource)
+
+ answer.Payload = &stanza.Command{
+ SessionId: command.Node,
+ Node: command.Node,
+ Status: stanza.CommandStatusCompleted,
+ CommandElement: &stanza.Note{
+ Text: response,
+ Type: stanza.CommandNoteTypeInfo,
+ },
}
+
+ log.Debugf("command response: %#v", answer.Payload)
}
}