mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-08-05 04:07:07 +00:00
Generic online safety check for commands
This commit is contained in:
parent
e073ded9e4
commit
1d29aa4694
2 changed files with 94 additions and 81 deletions
|
|
@ -51,23 +51,23 @@ var permissionsMember = client.ChatPermissions{
|
|||
var permissionsReadonly = client.ChatPermissions{}
|
||||
|
||||
var transportCommands = map[string]command{
|
||||
"help": command{0, []string{}, "help", false, nil},
|
||||
"login": command{1, []string{"phone"}, "sign in", false, nil},
|
||||
"logout": command{0, []string{}, "sign out", true, nil},
|
||||
"cleanup": command{0, []string{}, "unsubscribe from all known chats", false, nil},
|
||||
"cancelauth": command{0, []string{}, "quit the signin wizard", false, nil},
|
||||
"code": command{1, []string{"xxxxx"}, "check one-time code", false, nil},
|
||||
"password": command{1, []string{"********"}, "check 2fa password", false, nil},
|
||||
"setusername": command{0, []string{"@username"}, "update @username", true, nil},
|
||||
"setname": command{1, []string{"first", "last"}, "update name", true, nil},
|
||||
"setbio": command{0, []string{"Lorem ipsum"}, "update about", true, nil},
|
||||
"setpassword": command{0, []string{"old", "new"}, "set or remove password", true, nil},
|
||||
"config": command{0, []string{"param", "value"}, "view or update configuration options", false, nil},
|
||||
"report": command{2, []string{"chat", "comment"}, "report a chat by id or @username", true, nil},
|
||||
"add": command{1, []string{"@username"}, "add @username to your chat list", true, nil},
|
||||
"join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", true, nil},
|
||||
"supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", true, nil},
|
||||
"channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", true, nil},
|
||||
"help": command{0, []string{}, "help", false, nil, false},
|
||||
"login": command{1, []string{"phone"}, "sign in", false, nil, false},
|
||||
"logout": command{0, []string{}, "sign out", true, nil, true},
|
||||
"cleanup": command{0, []string{}, "unsubscribe from all known chats", false, nil, false},
|
||||
"cancelauth": command{0, []string{}, "quit the signin wizard", false, nil, false},
|
||||
"code": command{1, []string{"xxxxx"}, "check one-time code", false, nil, false},
|
||||
"password": command{1, []string{"********"}, "check 2fa password", false, nil, false},
|
||||
"setusername": command{0, []string{"@username"}, "update @username", true, nil, true},
|
||||
"setname": command{1, []string{"first", "last"}, "update name", true, nil, false},
|
||||
"setbio": command{0, []string{"Lorem ipsum"}, "update about", true, nil, true},
|
||||
"setpassword": command{0, []string{"old", "new"}, "set or remove password", true, nil, true},
|
||||
"config": command{0, []string{"param", "value"}, "view or update configuration options", false, nil, false},
|
||||
"report": command{2, []string{"chat", "comment"}, "report a chat by id or @username", true, nil, true},
|
||||
"add": command{1, []string{"@username"}, "add @username to your chat list", true, nil, true},
|
||||
"join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", true, nil, true},
|
||||
"supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", true, nil, true},
|
||||
"channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", true, nil, true},
|
||||
}
|
||||
|
||||
var notForGroups = []ChatType{ChatTypeBasicGroup, ChatTypeSupergroup, ChatTypeChannel}
|
||||
|
|
@ -76,38 +76,38 @@ var notForPMAndBasic = []ChatType{ChatTypePrivate, ChatTypeSecret, ChatTypeBasic
|
|||
var onlyForSecret = []ChatType{ChatTypePrivate, ChatTypeBasicGroup, ChatTypeSupergroup, ChatTypeChannel}
|
||||
|
||||
var chatCommands = map[string]command{
|
||||
"help": command{0, []string{}, "help", false, nil},
|
||||
"d": command{0, []string{"n"}, "delete your last message(s)", true, nil},
|
||||
"s": command{1, []string{"edited message"}, "edit your last message", true, nil},
|
||||
"silent": command{1, []string{"message"}, "send a message without sound", true, nil},
|
||||
"schedule": command{2, []string{"{online | 2006-01-02T15:04:05 | 15:04:05}", "message"}, "schedules a message either to timestamp or to whenever the user goes online", true, nil},
|
||||
"raw": command{1, []string{"message"}, "send a raw message not interpeted as a transport command (e.g. a bot command)", true, nil},
|
||||
"forward": command{2, []string{"message_id", "target_chat"}, "forwards a message", true, nil},
|
||||
"vcard": command{0, []string{}, "print vCard as text", true, nil},
|
||||
"add": command{1, []string{"@username"}, "add @username to your chat list", true, nil},
|
||||
"join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", true, nil},
|
||||
"group": command{1, []string{"title"}, "create groupchat «title» with current user", true, ¬ForGroups},
|
||||
"supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", true, nil},
|
||||
"channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", true, nil},
|
||||
"secret": command{0, []string{}, "create secretchat with current user", true, ¬ForGroups},
|
||||
"search": command{0, []string{"string", "[limit]"}, "search <string> in current chat", true, nil},
|
||||
"history": command{0, []string{"limit"}, "get last [limit] messages from current chat", true, nil},
|
||||
"block": command{0, []string{}, "blacklist current user", true, ¬ForGroups},
|
||||
"unblock": command{0, []string{}, "unblacklist current user", true, ¬ForGroups},
|
||||
"invite": command{1, []string{"id or @username"}, "add user to current chat", true, ¬ForPM},
|
||||
"link": command{0, []string{}, "get invite link for current chat", true, ¬ForPM},
|
||||
"kick": command{1, []string{"id or @username"}, "remove user from current chat", true, ¬ForPM},
|
||||
"mute": command{0, []string{"id or @username", "hours"}, "mute the whole chat or a user in current chat", true, ¬ForPMAndBasic},
|
||||
"unmute": command{0, []string{"id or @username"}, "unmute the whole chat or a user in the current chat", true, ¬ForPMAndBasic},
|
||||
"ban": command{1, []string{"id or @username", "hours"}, "restrict @username from current chat for [hours] or forever", true, ¬ForPM},
|
||||
"unban": command{1, []string{"id or @username"}, "unbans @username in current chat (and devotes from admins)", true, ¬ForPM},
|
||||
"promote": command{1, []string{"id or @username", "title"}, "promote user to admin in current chat", true, ¬ForPM},
|
||||
"leave": command{0, []string{}, "leave current chat", true, ¬ForPM},
|
||||
"leave!": command{0, []string{}, "leave current chat (for owners)", true, ¬ForPM},
|
||||
"ttl": command{0, []string{"seconds"}, "set secret chat messages TTL before self-destroying", true, &onlyForSecret},
|
||||
"close": command{0, []string{}, "close current secret chat", true, &onlyForSecret},
|
||||
"delete": command{0, []string{}, "delete current chat from chat list", true, nil},
|
||||
"members": command{0, []string{"query"}, "search members [by optional query] in current chat (requires admin rights)", true, nil},
|
||||
"help": command{0, []string{}, "help", false, nil, false},
|
||||
"d": command{0, []string{"n"}, "delete your last message(s)", true, nil, true},
|
||||
"s": command{1, []string{"edited message"}, "edit your last message", true, nil, true},
|
||||
"silent": command{1, []string{"message"}, "send a message without sound", true, nil, true},
|
||||
"schedule": command{2, []string{"{online | 2006-01-02T15:04:05 | 15:04:05}", "message"}, "schedules a message either to timestamp or to whenever the user goes online", true, nil, true},
|
||||
"raw": command{1, []string{"message"}, "send a raw message not interpeted as a transport command (e.g. a bot command)", true, nil, true},
|
||||
"forward": command{2, []string{"message_id", "target_chat"}, "forwards a message", true, nil, true},
|
||||
"vcard": command{0, []string{}, "print vCard as text", true, nil, true},
|
||||
"add": command{1, []string{"@username"}, "add @username to your chat list", true, nil, true},
|
||||
"join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", true, nil, true},
|
||||
"group": command{1, []string{"title"}, "create groupchat «title» with current user", true, ¬ForGroups, true},
|
||||
"supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", true, nil, true},
|
||||
"channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", true, nil, true},
|
||||
"secret": command{0, []string{}, "create secretchat with current user", true, ¬ForGroups, true},
|
||||
"search": command{0, []string{"string", "[limit]"}, "search <string> in current chat", true, nil, true},
|
||||
"history": command{0, []string{"limit"}, "get last [limit] messages from current chat", true, nil, true},
|
||||
"block": command{0, []string{}, "blacklist current user", true, ¬ForGroups, true},
|
||||
"unblock": command{0, []string{}, "unblacklist current user", true, ¬ForGroups, true},
|
||||
"invite": command{1, []string{"id or @username"}, "add user to current chat", true, ¬ForPM, true},
|
||||
"link": command{0, []string{}, "get invite link for current chat", true, ¬ForPM, true},
|
||||
"kick": command{1, []string{"id or @username"}, "remove user from current chat", true, ¬ForPM, true},
|
||||
"mute": command{0, []string{"id or @username", "hours"}, "mute the whole chat or a user in current chat", true, ¬ForPMAndBasic, true},
|
||||
"unmute": command{0, []string{"id or @username"}, "unmute the whole chat or a user in the current chat", true, ¬ForPMAndBasic, true},
|
||||
"ban": command{1, []string{"id or @username", "hours"}, "restrict @username from current chat for [hours] or forever", true, ¬ForPM, true},
|
||||
"unban": command{1, []string{"id or @username"}, "unbans @username in current chat (and devotes from admins)", true, ¬ForPM, true},
|
||||
"promote": command{1, []string{"id or @username", "title"}, "promote user to admin in current chat", true, ¬ForPM, true},
|
||||
"leave": command{0, []string{}, "leave current chat", true, ¬ForPM, true},
|
||||
"leave!": command{0, []string{}, "leave current chat (for owners)", true, ¬ForPM, true},
|
||||
"ttl": command{0, []string{"seconds"}, "set secret chat messages TTL before self-destroying", true, &onlyForSecret, true},
|
||||
"close": command{0, []string{}, "close current secret chat", true, &onlyForSecret, true},
|
||||
"delete": command{0, []string{}, "delete current chat from chat list", true, nil, true},
|
||||
"members": command{0, []string{"query"}, "search members [by optional query] in current chat (requires admin rights)", true, nil, true},
|
||||
}
|
||||
|
||||
var transportConfigurationOptions = map[string]configurationOption{
|
||||
|
|
@ -129,6 +129,7 @@ type command struct {
|
|||
Description string
|
||||
LoginOnly bool
|
||||
NotFor *[]ChatType
|
||||
OnlineOnly bool
|
||||
}
|
||||
type configurationOption struct {
|
||||
arguments string
|
||||
|
|
@ -143,6 +144,15 @@ const (
|
|||
CommandTypeChat
|
||||
)
|
||||
|
||||
// OnlineFilter is a tri-state condition for commands selection
|
||||
type OnlineFilter int
|
||||
|
||||
const (
|
||||
OnlineFilterOnline OnlineFilter = iota
|
||||
OnlineFilterNotOnline
|
||||
OnlineFilterAny
|
||||
)
|
||||
|
||||
// GetCommands exposes the set of commands
|
||||
func GetCommands(typ CommandType) map[string]command {
|
||||
var commandMap map[string]command
|
||||
|
|
@ -165,14 +175,20 @@ func GetCommand(typ CommandType, cmd string) (command, bool) {
|
|||
}
|
||||
|
||||
// SortedCommandKeys sorts a slice with command keys
|
||||
func SortedCommandKeys(commandMap map[string]command) []string {
|
||||
func SortedCommandKeys(commandMap map[string]command, onlineFilter OnlineFilter) []string {
|
||||
keys := make([]string, len(commandMap))
|
||||
|
||||
i := 0
|
||||
for k := range commandMap {
|
||||
command := commandMap[k]
|
||||
if (onlineFilter == OnlineFilterOnline && !command.OnlineOnly) || (onlineFilter == OnlineFilterNotOnline && command.OnlineOnly) {
|
||||
continue
|
||||
}
|
||||
|
||||
keys[i] = k
|
||||
i++
|
||||
}
|
||||
keys = keys[:i]
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
|
|
@ -215,24 +231,32 @@ func IsCommandForChatType(cmd command, chatType ChatType) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (c *Client) helpString(typ CommandType, chatId int64) string {
|
||||
var str strings.Builder
|
||||
|
||||
commandMap := GetCommands(typ)
|
||||
chatType, chatTypeErr := c.GetChatType(chatId, true)
|
||||
|
||||
str.WriteString("Available commands:\n")
|
||||
for _, name := range SortedCommandKeys(commandMap) {
|
||||
func commandsToHelpString(str *strings.Builder, chatType ChatType, onlineFilter OnlineFilter, commandMap map[string]command) {
|
||||
for _, name := range SortedCommandKeys(commandMap, onlineFilter) {
|
||||
command := commandMap[name]
|
||||
if chatTypeErr == nil && !IsCommandForChatType(command, chatType) {
|
||||
if !IsCommandForChatType(command, chatType) {
|
||||
continue
|
||||
}
|
||||
str.WriteString(CommandToHelpString(name, command))
|
||||
str.WriteString("\n")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) helpString(typ CommandType, chatId int64) string {
|
||||
var str strings.Builder
|
||||
|
||||
commandMap := GetCommands(typ)
|
||||
chatType, _ := c.GetChatType(chatId, true)
|
||||
|
||||
str.WriteString("Available commands:\n")
|
||||
|
||||
if typ == CommandTypeTransport {
|
||||
str.WriteString("Configuration options\n")
|
||||
commandsToHelpString(&str, chatType, OnlineFilterNotOnline, commandMap)
|
||||
|
||||
str.WriteString("\nOnline-only commands:\n")
|
||||
commandsToHelpString(&str, chatType, OnlineFilterOnline, commandMap)
|
||||
|
||||
str.WriteString("\nConfiguration options\n")
|
||||
for _, name := range persistence.ConfigKeys {
|
||||
option := transportConfigurationOptions[name]
|
||||
str.WriteString(name)
|
||||
|
|
@ -242,6 +266,8 @@ func (c *Client) helpString(typ CommandType, chatId int64) string {
|
|||
str.WriteString(option.description)
|
||||
str.WriteString("\n")
|
||||
}
|
||||
} else if typ == CommandTypeChat {
|
||||
commandsToHelpString(&str, chatType, OnlineFilterAny, commandMap)
|
||||
}
|
||||
str.WriteString("\nYou may use ! instead of / if it conflicts with internal commands of a client")
|
||||
|
||||
|
|
@ -336,6 +362,9 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin
|
|||
if len(args) < command.RequiredArgs {
|
||||
return notEnoughArguments, false
|
||||
}
|
||||
if command.OnlineOnly && !c.Online() {
|
||||
return notOnline, false
|
||||
}
|
||||
|
||||
switch cmd {
|
||||
case "login", "code", "password":
|
||||
|
|
@ -376,10 +405,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin
|
|||
}
|
||||
// sign out
|
||||
case "logout":
|
||||
if !c.Online() {
|
||||
return notOnline, false
|
||||
}
|
||||
|
||||
_, err := c.client.LogOut()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Logout error").Error(), false
|
||||
|
|
@ -400,10 +425,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin
|
|||
return "Cancelled", true
|
||||
// set @username
|
||||
case "setusername":
|
||||
if !c.Online() {
|
||||
return notOnline, false
|
||||
}
|
||||
|
||||
var username string
|
||||
if len(args) > 0 {
|
||||
username = args[0]
|
||||
|
|
@ -448,10 +469,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin
|
|||
}
|
||||
// set About
|
||||
case "setbio":
|
||||
if !c.Online() {
|
||||
return notOnline, false
|
||||
}
|
||||
|
||||
_, err := c.client.SetBio(&client.SetBioRequest{
|
||||
Bio: rawCmdArguments(cmdline, 0),
|
||||
})
|
||||
|
|
@ -460,10 +477,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin
|
|||
}
|
||||
// set password
|
||||
case "setpassword":
|
||||
if !c.Online() {
|
||||
return notOnline, false
|
||||
}
|
||||
|
||||
var oldPassword string
|
||||
var newPassword string
|
||||
if len(args) > 0 {
|
||||
|
|
@ -550,10 +563,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin
|
|||
// ProcessChatCommand executes a command sent in a mapped chat
|
||||
// and returns a response, the status of command support and the execution success result
|
||||
func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, bool) {
|
||||
if !c.Online() {
|
||||
return notOnline, true, false
|
||||
}
|
||||
|
||||
cmd, args := parseCommand(cmdline)
|
||||
command, ok := chatCommands[cmd]
|
||||
if !ok {
|
||||
|
|
@ -562,6 +571,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool,
|
|||
if len(args) < command.RequiredArgs {
|
||||
return notEnoughArguments, true, false
|
||||
}
|
||||
if command.OnlineOnly && !c.Online() {
|
||||
return notOnline, true, false
|
||||
}
|
||||
|
||||
|
||||
chatType, chatTypeErr := c.GetChatType(chatID, true)
|
||||
if chatTypeErr == nil && !IsCommandForChatType(command, chatType) {
|
||||
|
|
|
|||
|
|
@ -740,7 +740,7 @@ func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoItems) {
|
|||
}
|
||||
|
||||
commands := telegram.GetCommands(cmdType)
|
||||
for _, name := range telegram.SortedCommandKeys(commands) {
|
||||
for _, name := range telegram.SortedCommandKeys(commands, telegram.OnlineFilterAny) {
|
||||
command := commands[name]
|
||||
if chatTypeErr == nil && !telegram.IsCommandForChatType(command, chatType) {
|
||||
continue
|
||||
|
|
|
|||
Loading…
Reference in a new issue