cmd

package
v1.15.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2016 License: BSD-1-Clause Imports: 7 Imported by: 0

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

View Source
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

View Source
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

type AuthFunc func(string) bool

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) Add

func (c *Command) Add(name string, required bool, pattern *regexp.Regexp) *Command

Add adds a new command parameter.

func (*Command) RequiredParamCount

func (c *Command) RequiredParamCount() int

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.

func (List) Find

func (cl List) Find(name string) *Command

Find finds the command for the given name. Returns nil if it was not found.

func (List) Index

func (cl List) Index(name string) int

Index returns the index of the command for the given name. Returns -1 if it was not found.

func (List) Len

func (cl List) Len() int

func (List) Less

func (cl List) Less(i, j int) bool

func (List) Swap

func (cl List) Swap(i, j int)

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.

func (*Param) Bool

func (p *Param) Bool() bool

Bool returns the boolean value represented by the parameter. True is represented by the values: "1", "t", "true", "y", "yes", "on" Any other value returns false.

func (*Param) Float

func (p *Param) Float() float64

func (*Param) Int

func (p *Param) Int() int64

func (*Param) String

func (p *Param) String() string

func (*Param) Uint

func (p *Param) Uint() uint64

type ParamList

type ParamList []Param

ParamList defines a list of command parameters.

func (ParamList) Bool

func (p ParamList) Bool(n int) bool

func (ParamList) Float

func (p ParamList) Float(n int) float64

func (ParamList) Int

func (p ParamList) Int(n int) int64

func (ParamList) Join

func (p ParamList) Join() string

Join returns all parameter values, concatenated into a single string. Each entry is separated by a blank space.

func (ParamList) Len

func (p ParamList) Len() int

func (ParamList) String

func (p ParamList) String(n int) string

func (ParamList) Uint

func (p ParamList) Uint(n int) uint64

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set defines a set of bound commands.

func New

func New(prefix string, authenticate AuthFunc) *Set

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.

func (*Set) Bind

func (s *Set) Bind(name string, restricted bool, handler Handler) *Command

Bind binds the given command.

func (*Set) Dispatch

func (s *Set) Dispatch(w irc.ResponseWriter, r *irc.Request) bool

Dispatch accepts the given message and issues command calls if applicable. Returns false if no command call was issued.

func (*Set) Unbind

func (s *Set) Unbind(name string)

Unbind removes the given command.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL