cmd

package
v0.0.0-...-115c377 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2013 License: BSD-1-Clause Imports: 6 Imported by: 0

README

cmd

This package holds bot command parsing and execution code. It is used by registering a command and handler with this package, before initializing the connection. Once the connection is active, the command package must be invoked for every PRIVMSG request:

client.Bind(proto.CmdPrivMsg, onPrivMsg)

...

func onPrivMsg(c *proto.Client, m *proto.Message) {
	...
	cmd.Parse(commandPrefix, c, m)
	...
}

The incoming message will be processed and matched against any registered commands. A command's execution handler will be invoked if, and only if the supplied message data matches all parameters, and the user sendering the request has permission to execute the command.

If the user omits required arguments, or the supplied arguments do not match the format we expect them to have, the bot will automatically send an appropriate error response to the user and the command handler is not invoked.

Examples

Register a command without any parameters:

cmd.Register("help", func() *cmd.Command {
	c := new(cmd.Command)
	c.Name = "help"
	c.Execute = func(cmd *cmd.Command, c *proto.Client, m *proto.Message) {
		// Code to handle command execution goes here.
	}
	return c
})

This can be invoked through IRC by sending !help. Provided ! is registered as the current command prefix.

Register a command with two parameters:

cmd.Register("add", func() *cmd.Command {
	c := new(cmd.Command)
	c.Name = "add"
	c.Params = []cmd.Param{
		{Name: "a", Pattern: cmd.RegDecimal},
		{Name: "b", Pattern: cmd.RegDecimal},
	}
	c.Execute = func(cmd *Command, c *proto.Client, m *proto.Message) {
		c.PrivMsg(m.SenderName, "%f", cmd.Params[0].F64(0)+cmd.Params[1].F64(0))
	}
	return c
})

This can be invoked through IRC by sending !add 1.23 3.56. Provided ! is registered as the current command prefix.

Command Parameters

A command which has parameters, is given a reference to the parsed parameters when its handler is executed. The values for these parameters have already been validated against a specified regular expression pattern. Once the command handler is executed, we can therefore be reasonably certain that we received all the data needed to safely execute the command.

Accessing the parameters is done through the Command.Params slice. Each parameter has convenience methods allowing quick conversion to a given data type. For example:

	_ = cmd.Params[0].F64(0) + cmd.Params[1].F64(0)

In this code, the two required parameters are referenced as float64 values. There are convenience accessors for all the basic Go data types:

Param.Bool(bool) bool
Param.I8(int8) int8
Param.I16(int16) int16
Param.I32(int32) int32
Param.I64(int64) int64
Param.U8(uint8) uint8
Param.U16(uint16) uint16
Param.U32(uint32) uint32
Param.U64(uint64) uint64
Param.F32(float32) float32
Param.F64(float64) float64

The one parameter for these is the default value returned when the data type conversion fails. To access the original string value, simply use the Param.Value field directly.

Usage

go get github.com/jteeuwen/ircb/cmd

License

Unless otherwise stated, all of the work in this project is subject to a 1-clause BSD license. Its contents can be found in the enclosed LICENSE file.

Documentation

Overview

This package holds bot command parsing and execution code.

Index

Constants

This section is empty.

Variables

View Source
var (
	RegAny     = regexp.MustCompile(`^.+$`)
	RegAlpha   = regexp.MustCompile(`^\w+$`)
	RegHex     = regexp.MustCompile(`^0x[a-fA-F0-9]+$`)
	RegDecimal = regexp.MustCompile(`^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$`)
	RegOctal   = regexp.MustCompile(`^0[0-7]+$`)
	RegBinary  = regexp.MustCompile(`^0b[01]+$`)
)

Functions

func Parse

func Parse(prefix string, c *proto.Client, m *proto.Message) bool

Parse reads incoming message data and tries to parse it into a command structure and then execute it.

func Register

func Register(name string, cf CommandFunc)

Register registers the given command name and constructor. Modules should call this during initialization to register their commands with the bot.

Types

type Command

type Command struct {
	Name        string      // Command name.
	Description string      // Command description.
	Params      []Param     // Command parameters.
	Execute     ExecuteFunc // Execution handler for the command.
	Restricted  bool        // Command is restricted to admin users.
}

Command represents a single bot command.

func (*Command) RequiredParamCount

func (c *Command) RequiredParamCount() int

RequiredParamCount counts the number of required parameters.

type CommandFunc

type CommandFunc func() *Command

CommandFunc represents a command constructor.

type ExecuteFunc

type ExecuteFunc func(*Command, *proto.Client, *proto.Message)

ExecuteFunc represents a command execution handler. These are executed in a separate goroutine.

type Param

type Param struct {
	Name        string         // Parameter name.
	Description string         // Parameter description.
	Value       string         // Parameter value.
	Pattern     *regexp.Regexp // Parameter validation pattern.
	Optional    bool           // Is this parameter optional?
}

Param represents a single command parameter.

func (*Param) Bool

func (p *Param) Bool(defaultVal bool) bool

func (*Param) F32

func (p *Param) F32(defaultVal float32) float32

func (*Param) F64

func (p *Param) F64(defaultVal float64) float64

func (*Param) I16

func (p *Param) I16(defaultVal int16) int16

func (*Param) I32

func (p *Param) I32(defaultVal int32) int32

func (*Param) I64

func (p *Param) I64(defaultVal int64) int64

func (*Param) I8

func (p *Param) I8(defaultVal int8) int8

func (*Param) U16

func (p *Param) U16(defaultVal uint16) uint16

func (*Param) U32

func (p *Param) U32(defaultVal uint32) uint32

func (*Param) U64

func (p *Param) U64(defaultVal uint64) uint64

func (*Param) U8

func (p *Param) U8(defaultVal uint8) uint8

func (*Param) Valid

func (p *Param) Valid() bool

Valid returns true if the parameter value matches the param pattern.

Jump to

Keyboard shortcuts

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