diff options
Diffstat (limited to 'xmpp/handlers.go')
-rw-r--r-- | xmpp/handlers.go | 174 |
1 files changed, 98 insertions, 76 deletions
diff --git a/xmpp/handlers.go b/xmpp/handlers.go index 6fb1af2..1885aae 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -490,23 +490,30 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoInfo) { disco := answer.DiscoInfo() _, ok := toToID(iq.To) - if ok { - disco.AddIdentity("", "account", "registered") - disco.AddFeatures(stanza.NSMsgChatMarkers) - disco.AddFeatures(stanza.NSMsgReceipts) - } else { - if di.Node == "" { + if di.Node == "" { + if ok { + disco.AddIdentity("", "account", "registered") + disco.AddFeatures(stanza.NSMsgChatMarkers) + disco.AddFeatures(stanza.NSMsgReceipts) + } else { disco.AddIdentity("Telegram Gateway", "gateway", "telegram") disco.AddFeatures("jabber:iq:register") - disco.AddFeatures(NSCommand) + } + disco.AddFeatures(NSCommand) + } else { + var cmdType telegram.CommandType + if ok { + cmdType = telegram.CommandTypeChat } else { - for name, command := range telegram.GetCommands(telegram.CommandTypeTransport) { - if di.Node == name { - answer.Payload = di - di.AddIdentity(telegram.CommandToHelpString(name, command), "automation", "command-node") - di.AddFeatures(NSCommand, "jabber:x:data") - break - } + cmdType = telegram.CommandTypeTransport + } + + for name, command := range telegram.GetCommands(cmdType) { + if di.Node == name { + answer.Payload = di + di.AddIdentity(telegram.CommandToHelpString(name, command), "automation", "command-node") + di.AddFeatures(NSCommand, "jabber:x:data") + break } } } @@ -539,16 +546,22 @@ func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoItems) { log.Debugf("discoItems: %#v", di) _, ok := toToID(iq.To) - if !ok { - commands := telegram.GetCommands(telegram.CommandTypeTransport) - if di.Node == NSCommand { - answer.Payload = di - for name, command := range commands { - di.AddItem(iq.To, name, telegram.CommandToHelpString(name, command)) - } + if di.Node == NSCommand { + answer.Payload = di + + var cmdType telegram.CommandType + if ok { + cmdType = telegram.CommandTypeChat } else { - answer.Payload = answer.DiscoItems() + cmdType = telegram.CommandTypeTransport } + + commands := telegram.GetCommands(cmdType) + for name, command := range commands { + di.AddItem(iq.To, name, telegram.CommandToHelpString(name, command)) + } + } else { + answer.Payload = answer.DiscoItems() } component, ok := s.(*xmpp.Component) @@ -706,66 +719,70 @@ func handleSetQueryCommand(s xmpp.Sender, iq *stanza.IQ, command *stanza.Command if !ok { return } - _, toOk := toToID(iq.To) + toId, toOk := toToID(iq.To) var cmdString string - if !toOk { - form, formOk := command.CommandElement.(*stanza.Form) - if formOk { - // 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]) - } + var cmdType telegram.CommandType + form, formOk := command.CommandElement.(*stanza.Form) + if toOk { + cmdType = telegram.CommandTypeChat + } else { + cmdType = telegram.CommandTypeTransport + } + if formOk { + // 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]) } + } - cmdString = cmd.String() - } else { - if command.Action == "" || command.Action == stanza.CommandActionExecute { - 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{ - Type: stanza.FormTypeForm, - Title: command.Node, - Instructions: []string{cmd.Description}, - Fields: fields, - }, - } - } else { - cmdString = "/" + command.Node + cmdString = cmd.String() + } else { + if command.Action == "" || command.Action == stanza.CommandActionExecute { + cmd, ok := telegram.GetCommand(cmdType, command.Node) + if ok && len(cmd.Arguments) > 0 { + var fields []*stanza.Field + for i, arg := range cmd.Arguments { + fields = append(fields, &stanza.Field{ + Var: strconv.FormatInt(int64(i), 10), + Label: arg, + }) } - } else if command.Action == stanza.CommandActionCancel { answer.Payload = &stanza.Command{ - SessionId: command.Node, - Node: command.Node, - Status: stanza.CommandStatusCancelled, + SessionId: command.Node, + Node: command.Node, + Status: stanza.CommandStatusExecuting, + CommandElement: &stanza.Form{ + Type: stanza.FormTypeForm, + Title: command.Node, + Instructions: []string{cmd.Description}, + Fields: fields, + }, } + } else { + cmdString = "/" + command.Node + } + } else if command.Action == stanza.CommandActionCancel { + answer.Payload = &stanza.Command{ + SessionId: command.Node, + Node: command.Node, + Status: stanza.CommandStatusCancelled, } } } @@ -776,7 +793,12 @@ func handleSetQueryCommand(s xmpp.Sender, iq *stanza.IQ, command *stanza.Command return } - response := session.ProcessTransportCommand(cmdString, resource) + var response string + if toOk { + response, _ = session.ProcessChatCommand(toId, cmdString) + } else { + response = session.ProcessTransportCommand(cmdString, resource) + } answer.Payload = &stanza.Command{ SessionId: command.Node, |