From 9b5fee88262f22ad14acf621e992167966af278a Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Thu, 15 Feb 2024 04:40:57 -0500 Subject: Filter available commands by chat type --- xmpp/handlers.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'xmpp') diff --git a/xmpp/handlers.go b/xmpp/handlers.go index c50dd1c..3554394 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -475,6 +475,21 @@ func handleGetVcardIq(s xmpp.Sender, iq *stanza.IQ, typ byte) { _ = gateway.ResumableSend(component, &answer) } +func getTelegramChatType(from string, to string) (telegram.ChatType, error) { + toId, ok := toToID(to) + if ok { + bare, _, ok := gateway.SplitJID(from) + if ok { + session, ok := sessions[bare] + if ok { + return session.GetChatType(toId) + } + } + } + + return telegram.ChatTypeUnknown, errors.New("Unknown chat type") +} + func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoInfo) { answer, err := stanza.NewIQ(stanza.Attrs{ Type: stanza.IQTypeResult, @@ -501,6 +516,8 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoInfo) { } disco.AddFeatures(NSCommand) } else { + chatType, chatTypeErr := getTelegramChatType(iq.From, iq.To) + var cmdType telegram.CommandType if ok { cmdType = telegram.CommandTypeChat @@ -510,6 +527,9 @@ func handleGetDiscoInfo(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoInfo) { for name, command := range telegram.GetCommands(cmdType) { if di.Node == name { + if chatTypeErr == nil && !telegram.IsCommandForChatType(command, chatType) { + break + } answer.Payload = di di.AddIdentity(telegram.CommandToHelpString(name, command), "automation", "command-node") di.AddFeatures(NSCommand, "jabber:x:data") @@ -549,6 +569,8 @@ func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoItems) { if di.Node == NSCommand { answer.Payload = di + chatType, chatTypeErr := getTelegramChatType(iq.From, iq.To) + var cmdType telegram.CommandType if ok { cmdType = telegram.CommandTypeChat @@ -559,6 +581,9 @@ func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoItems) { commands := telegram.GetCommands(cmdType) for _, name := range telegram.SortedCommandKeys(commands) { command := commands[name] + if chatTypeErr == nil && !telegram.IsCommandForChatType(command, chatType) { + continue + } di.AddItem(iq.To, name, telegram.CommandToHelpString(name, command)) } } else { -- cgit v1.2.3