diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2024-02-15 12:40:57 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2024-02-15 12:40:57 +0300 |
commit | 9b5fee88262f22ad14acf621e992167966af278a (patch) | |
tree | c6ef439abf2e3826bea45e7bcdc1b15366c34f0f /xmpp | |
parent | dc6f99dc3ca0906bfd5f9bda9eab618445cfa878 (diff) |
Filter available commands by chat type
Diffstat (limited to 'xmpp')
-rw-r--r-- | xmpp/handlers.go | 25 |
1 files changed, 25 insertions, 0 deletions
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 { |