Documentation ¶
Overview ¶
Package cmd allows the definition of command handlers to be called by users from either a channel or a private message. These are messages like the following:
!join #test
Beginning with a predefined character (! in this case), followed by the command name and an optional set of arguments. This package parses the command data, verifies it refers to an existing command and ensures that the parameter values have the correct formats.
For example:
join := cmd.Bind("join", true, onJoin) join.Add("channel", true, cmd.RegChannel) join.Add("password", false, cmd.RegAny) ... func onJoin(w irc.ResponseWriter, r *cmd.Request) { var c irc.Channel c.Name = r.String(0) if r.Len() > 1 { c.Password = r.String(1) } proto.Join(w, c) }
The name and description texts for the command and parameters, are there for user documentation. You can bind a `!help` command to `cmd.HelpHandler`, which will present the user either with an overview of all registered commands, or get detailed help on a specific command.
The `cmd.RegXXX` values passed into the parameter definitions are predefined regular expressions. You are free to pass in your own patterns. These are used to ensure a parameter value given by a user, matches your expectations. If this is not the case, the command handler is never executed and the user is presented with an appropriate error response.
By the time the registered command handler is actually called, you may be certain that the parameter value matches your definition.
The boolean value pass passed into each parameter definition determines if that specific parameter is optional or not. This provides for rudimentary varargs functionality.
In the example listed above, the user may call the `join` command in one of two ways:
!join #channel !join #channel somepassword
A parameter occupying multiple whitespace separated words, is to be supplied in double quotes:
!join #channel "some long password"
Index ¶
Constants ¶
const ( TextMissingParameters = "Ontbrekende parameters voor commando: %s" TextInvalidParameter = "Commando %s: ongeldige waarde voor parameter %q" TextAccessDenied = "Helaas, pindakaas. Het commando %q mag uitsluitend door beheerders uitgevoerd worden." )
Variables ¶
var ( RegAny = regexp.MustCompile(`^.*$`) RegInt = regexp.MustCompile(`^[+-]?\d+$`) RegUint = regexp.MustCompile(`^[+]?\d+$`) RegFloat = regexp.MustCompile(`^[+-]?\d+(\.\d+([eE][+-]?\d+)?)?$`) RegBool = regexp.MustCompile(`^(1|0|t(rue)?|f(alse)?|y(es)?|no?|on|off)$`) RegChannel = regexp.MustCompile(`^[#&+!][^ ,:]{1,50}$`) RegMode = regexp.MustCompile(`^[+-][obveI]$`) RegUrl = regexp.MustCompile(`^https?\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]+(\:[0-9]+)?(/\S*)?$`) )
Functions ¶
This section is empty.
Types ¶
type AuthFunc ¶
AuthFunc returns true if the given hostmask defines a whitelisted user. This function is used by the command dispatcher to ensure the user is allowed to execute a given, restricted command.
type Command ¶
type Command struct { Name string // Name by which the command is called. Handler Handler // Command handler. Params []Param // Command parameter list. Restricted bool // Command may only be run by authorized users. }
Command defines a single command which can be called by IRC users.
func (*Command) RequiredParamCount ¶
RequiredParamCount returns the amunt of required parameters for this command.
type Handler ¶
type Handler func(irc.ResponseWriter, *irc.Request, ParamList)
Handler defines a callbck function for a registered command.
type List ¶
type List []*Command
List defines a list of commands, sortable by name.
type Param ¶
type Param struct { Name string // Parameter name -- used in help listing. Description string // Parameter description -- used in help listing. Value string // Parameter value. Pattern *regexp.Regexp // Pattern defining the type of accepted value. Required bool // Parameter is required or not? }
Param defines a parameter for a command.
type ParamList ¶
type ParamList []Param
ParamList defines a list of command parameters.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set defines a set of bound commands.
func New ¶
New creates a new, empty set for the given prefix and auth handler. The auth handler is used to ensure a caller is allowed to run a restricted command. This can be nil, which will outright deny access to all commands which have the restricted flag set.