Declaratively specify optional and required command arguments

This commit is contained in:
Bohdan Horbeshko 2024-02-01 12:14:06 -05:00
parent eace19eef7
commit e3a5191905

View file

@ -48,56 +48,56 @@ var permissionsMember = client.ChatPermissions{
var permissionsReadonly = client.ChatPermissions{} var permissionsReadonly = client.ChatPermissions{}
var transportCommands = map[string]command{ var transportCommands = map[string]command{
"help": command{"", "help"}, "help": command{0, []string{}, "help"},
"login": command{"phone", "sign in"}, "login": command{1, []string{"phone"}, "sign in"},
"logout": command{"", "sign out"}, "logout": command{0, []string{}, "sign out"},
"cancelauth": command{"", "quit the signin wizard"}, "cancelauth": command{0, []string{}, "quit the signin wizard"},
"code": command{"", "check one-time code"}, "code": command{1, []string{"xxxxx"}, "check one-time code"},
"password": command{"", "check 2fa password"}, "password": command{1, []string{"********"}, "check 2fa password"},
"setusername": command{"", "update @username"}, "setusername": command{0, []string{"@username"}, "update @username"},
"setname": command{"first last", "update name"}, "setname": command{1, []string{"first", "last"}, "update name"},
"setbio": command{"", "update about"}, "setbio": command{0, []string{"Lorem ipsum"}, "update about"},
"setpassword": command{"[old] [new]", "set or remove password"}, "setpassword": command{0, []string{"old", "new"}, "set or remove password"},
"config": command{"[param] [value]", "view or update configuration options"}, "config": command{0, []string{"param", "value"}, "view or update configuration options"},
"report": command{"[chat] [comment]", "report a chat by id or @username"}, "report": command{2, []string{"chat", "comment"}, "report a chat by id or @username"},
"add": command{"@username", "add @username to your chat list"}, "add": command{1, []string{"@username"}, "add @username to your chat list"},
"join": command{"https://t.me/invite_link", "join to chat via invite link or @publicname"}, "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname"},
"supergroup": command{"title description", "create new supergroup «title» with «description»"}, "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»"},
"channel": command{"title description", "create new channel «title» with «description»"}, "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»"},
} }
var chatCommands = map[string]command{ var chatCommands = map[string]command{
"help": command{"", "help"}, "help": command{0, []string{}, "help"},
"d": command{"[n]", "delete your last message(s)"}, "d": command{0, []string{"n"}, "delete your last message(s)"},
"s": command{"edited message", "edit your last message"}, "s": command{1, []string{"edited message"}, "edit your last message"},
"silent": command{"message", "send a message without sound"}, "silent": command{1, []string{"message"}, "send a message without sound"},
"schedule": command{"{online | 2006-01-02T15:04:05 | 15:04:05} message", "schedules a message either to timestamp or to whenever the user goes online"}, "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"},
"forward": command{"message_id target_chat", "forwards a message"}, "forward": command{2, []string{"message_id", "target_chat"}, "forwards a message"},
"vcard": command{"", "print vCard as text"}, "vcard": command{0, []string{}, "print vCard as text"},
"add": command{"@username", "add @username to your chat list"}, "add": command{1, []string{"@username"}, "add @username to your chat list"},
"join": command{"https://t.me/invite_link", "join to chat via invite link or @publicname"}, "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname"},
"group": command{"title", "create groupchat «title» with current user"}, "group": command{1, []string{"title"}, "create groupchat «title» with current user"},
"supergroup": command{"title description", "create new supergroup «title» with «description»"}, "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»"},
"channel": command{"title description", "create new channel «title» with «description»"}, "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»"},
"secret": command{"", "create secretchat with current user"}, "secret": command{0, []string{}, "create secretchat with current user"},
"search": command{"string [limit]", "search <string> in current chat"}, "search": command{0, []string{"string", "[limit]"}, "search <string> in current chat"},
"history": command{"[limit]", "get last [limit] messages from current chat"}, "history": command{0, []string{"limit"}, "get last [limit] messages from current chat"},
"block": command{"", "blacklist current user"}, "block": command{0, []string{}, "blacklist current user"},
"unblock": command{"", "unblacklist current user"}, "unblock": command{0, []string{}, "unblacklist current user"},
"invite": command{"id or @username", "add user to current chat"}, "invite": command{1, []string{"id or @username"}, "add user to current chat"},
"link": command{"", "get invite link for current chat"}, "link": command{0, []string{}, "get invite link for current chat"},
"kick": command{"id or @username", "remove user to current chat"}, "kick": command{1, []string{"id or @username"}, "remove user to current chat"},
"mute": command{"id or @username [hours]", "mute user in current chat"}, "mute": command{1, []string{"id or @username", "hours"}, "mute user in current chat"},
"unmute": command{"id or @username", "unrestrict user from current chat"}, "unmute": command{1, []string{"id or @username"}, "unrestrict user from current chat"},
"ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"}, "ban": command{1, []string{"id or @username", "hours"}, "restrict @username from current chat for [hours] or forever"},
"unban": command{"id or @username", "unbans @username in current chat (and devotes from admins)"}, "unban": command{1, []string{"id or @username"}, "unbans @username in current chat (and devotes from admins)"},
"promote": command{"id or @username [title]", "promote user to admin in current chat"}, "promote": command{1, []string{"id or @username", "title"}, "promote user to admin in current chat"},
"leave": command{"", "leave current chat"}, "leave": command{0, []string{}, "leave current chat"},
"leave!": command{"", "leave current chat (for owners)"}, "leave!": command{0, []string{}, "leave current chat (for owners)"},
"ttl": command{"", "set secret chat messages TTL before self-destroying (in seconds)"}, "ttl": command{0, []string{"seconds"}, "set secret chat messages TTL before self-destroying"},
"close": command{"", "close current secret chat"}, "close": command{0, []string{}, "close current secret chat"},
"delete": command{"", "delete current chat from chat list"}, "delete": command{0, []string{}, "delete current chat from chat list"},
"members": command{"[query]", "search members [by optional query] in current chat (requires admin rights)"}, "members": command{0, []string{"query"}, "search members [by optional query] in current chat (requires admin rights)"},
} }
var transportConfigurationOptions = map[string]configurationOption{ var transportConfigurationOptions = map[string]configurationOption{
@ -107,10 +107,14 @@ var transportConfigurationOptions = map[string]configurationOption{
} }
type command struct { type command struct {
requiredArgs int
arguments []string
description string
}
type configurationOption struct {
arguments string arguments string
description string description string
} }
type configurationOption command
// CommandType disinguishes command sets by chat // CommandType disinguishes command sets by chat
type CommandType int type CommandType int
@ -140,9 +144,16 @@ func CommandToHelpString(name string, cmd command) string {
str.WriteString("/") str.WriteString("/")
str.WriteString(name) str.WriteString(name)
if cmd.arguments != "" { for i, arg := range cmd.arguments {
optional := i >= cmd.requiredArgs
str.WriteString(" ") str.WriteString(" ")
str.WriteString(cmd.arguments) if optional {
str.WriteString("[")
}
str.WriteString(arg)
if optional {
str.WriteString("]")
}
} }
str.WriteString(" — ") str.WriteString(" — ")
str.WriteString(cmd.description) str.WriteString(cmd.description)
@ -252,16 +263,20 @@ func (c *Client) usernameOrIDToID(username string) (int64, error) {
// and returns a response // and returns a response
func (c *Client) ProcessTransportCommand(cmdline string, resource string) string { func (c *Client) ProcessTransportCommand(cmdline string, resource string) string {
cmd, args := parseCommand(cmdline) cmd, args := parseCommand(cmdline)
command, ok := transportCommands[cmd]
if !ok {
return "Unknown command"
}
if len(args) < command.requiredArgs {
return notEnoughArguments
}
switch cmd { switch cmd {
case "login", "code", "password": case "login", "code", "password":
if cmd == "login" && c.Session.Login != "" { if cmd == "login" && c.Session.Login != "" {
return "Phone number already provided, use /cancelauth to start over" return "Phone number already provided, use /cancelauth to start over"
} }
if len(args) < 1 {
return notEnoughArguments
}
if cmd == "login" { if cmd == "login" {
err := c.TryLogin(resource, args[0]) err := c.TryLogin(resource, args[0])
if err != nil { if err != nil {
@ -336,11 +351,9 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
} }
// set My Name // set My Name
case "setname": case "setname":
var firstname string firstname := args[0]
var lastname string var lastname string
if len(args) > 0 {
firstname = args[0]
}
if firstname == "" { if firstname == "" {
return "The name should contain at least one character" return "The name should contain at least one character"
} }
@ -439,10 +452,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
return strings.Join(entries, "\n") return strings.Join(entries, "\n")
case "report": case "report":
if len(args) < 2 {
return "Not enough arguments"
}
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0])
if err != nil { if err != nil {
return err.Error() return err.Error()