bot

package
v0.0.0-...-27f5e65 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2019 License: MIT Imports: 33 Imported by: 7

Documentation

Overview

Package bot implements the top-level package that any non-extension will use to start a bot instance.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckConfig

func CheckConfig(c *config.Config) bool

CheckConfig checks a bots config for validity.

func NewAPIServer

func NewAPIServer(b *Bot) *apiServer

NewAPIServer creates an api server

func NewCoreCmds

func NewCoreCmds(b *Bot) (*coreCmds, error)

NewCoreCmds initializes the core commands and registers them with the bot.

func RegisterExtension

func RegisterExtension(name string, ext Extension)

RegisterExtension with the bot. This should be called on init(), and the extension should rely upon import side effects in order to have that init() called. Panics if a name is registered twice.

func Run

func Run(cb func(b *Bot)) error

Run makes a very typical bot. It will call the cb function passed in before starting to allow registration of extensions etc. Returns error if the bot could not be created. Does NOT return until dead. The following are featured behaviors: Reads configuration file from ./config.toml Watches for Keyboard Input OR SIGTERM OR SIGKILL and shuts down normally. Pauses after death to allow all goroutines to come to a graceful shutdown.

Types

type Bot

type Bot struct {

	// Logging
	log15.Logger
	// contains filtered or unexported fields
}

Bot is a main type that joins together all the packages into a functioning irc bot. It should be able to carry out most major functions that a bot would need through it's exported functions.

func New

func New(conf *config.Config) (*Bot, error)

New simplifies the call to createBotFull by using default caps and conn provider functions.

func (*Bot) Cleanup

func (b *Bot) Cleanup() error

Cleanup should be called pre application exit.

func (*Bot) Close

func (b *Bot) Close() error

Close closes the store database.

func (*Bot) DumpConfig

func (b *Bot) DumpConfig() error

DumpConfig dumps the config to a file. It attempts to use the previously read config file name if loaded from a file... If not it will use a default file name.

func (*Bot) NetworkWriter

func (b *Bot) NetworkWriter(networkID string) (w irc.Writer)

NetworkWriter retrieves a network's writer. Will be nil if the network does not exist.

func (*Bot) ReadConfig

func (b *Bot) ReadConfig(fn configCallback)

ReadConfig opens the config for reading, for the duration of the callback the config is synchronized.

func (*Bot) Register

func (b *Bot) Register(network, channel, event string,
	handler dispatch.Handler) uint64

Register event handlers to the specified network and channel. Leave either blank to create a filter based on that field alone. Returns an identifier that can be used to unregister the event.

func (*Bot) RegisterCmd

func (b *Bot) RegisterCmd(network, channel string, command *cmd.Command) (uint64, error)

RegisterCmd registers a command with the bot filtered based on the network and channel. Leave either field blank to create a filter based on that field alone. See cmds.Cmds.Register for in-depth documentation.

func (*Bot) RegisterGlobal

func (b *Bot) RegisterGlobal(event string, handler dispatch.Handler) uint64

RegisterGlobal event handler to the bot. Returns an identifier that can be used to unregister the event.

func (*Bot) RegisterGlobalCmd

func (b *Bot) RegisterGlobalCmd(command *cmd.Command) (uint64, error)

RegisterGlobalCmd registers a command with the bot. See cmds.Cmds.Register for in-depth documentation.

func (*Bot) Rehash

func (b *Bot) Rehash() error

Rehash loads the config from a file. It attempts to use the previously read config file name if loaded from a file... If not it will use a default file name. It then calls Bot.ReplaceConfig.

func (*Bot) ReplaceConfig

func (b *Bot) ReplaceConfig(newConfig *config.Config) bool

ReplaceConfig replaces the current configuration for the bot. Running servers not present in the new config will be shut down immediately, while new servers will be connected to and started. Updates updateable attributes from the new configuration for each server. Returns false if the config had an error.

func (*Bot) Start

func (b *Bot) Start() <-chan error

Start runs the bot. A channel is returned, every time a server is killed permanently it reports the error on this channel. When the channel is closed, there are no more servers left to run and the program can safely exit.

func (*Bot) StartNetwork

func (b *Bot) StartNetwork(networkID string) (started bool)

StartNetwork starts a network by name. Start() should have been called prior to this.

func (*Bot) State

func (b *Bot) State(networkID string) *data.State

State returns the state db for that network id. If the server doesn't exist or state is disabled, returns nil.

func (*Bot) Stop

func (b *Bot) Stop()

Stop shuts down all connections and exits.

func (*Bot) StopNetwork

func (b *Bot) StopNetwork(networkID string) (stopped bool)

StopNetwork stops a network by name.

func (*Bot) Store

func (b *Bot) Store() *data.Store

Store returns the store for the bot. Returns nil if store is disabled.

func (*Bot) Unregister

func (b *Bot) Unregister(id uint64) bool

Unregister an event handler from the bot.

func (*Bot) UnregisterCmd

func (b *Bot) UnregisterCmd(id uint64) bool

UnregisterCmd from the bot. All parameters can be blank except for cmd. Leaving ext blank wipes out other extension's commands with the same name.

func (*Bot) WriteConfig

func (b *Bot) WriteConfig(fn configCallback)

WriteConfig opens the config for writing, for the duration of the callback the config is synchronized.

type ConnProvider

type ConnProvider func(string) (net.Conn, error)

ConnProvider transforms a "server:port" string into a net.Conn

type Extension

type Extension interface {
	// Init attaches event handlers and commands
	Init(*Bot) error
	// Deinit detaches event handlers and commands
	Deinit(*Bot) error
}

Extension provides methods to initialize and de-initialize itself. The bot type is passed in as the interface with which to register event handlers, commands, and access the databases. The de-init should destroy the extension until it is called upon again.

Event handlers can execute in parallel and so there must be provisions to protect mutable state within the implementation.

type LoggerProvider

type LoggerProvider func() log15.Handler

LoggerProvider returns a log15.Handler suitable for logging.

type Server

type Server struct {
	log15.Logger
	// contains filtered or unexported fields
}

Server is all the details around a specific server connection. Also contains the connection and configuration for the specific server.

func (*Server) Close

func (s *Server) Close() (err error)

Close shuts down the connection and returns.

func (*Server) GetStatus

func (s *Server) GetStatus() Status

GetStatus safely gets the status of the server.

func (*Server) Write

func (s *Server) Write(buf []byte) (int, error)

Write writes to the server's IrcClient.

type Status

type Status byte

Status is the status of a network connection.

const (
	STATUS_STOPPED Status = iota
	STATUS_CONNECTING
	STATUS_STARTED
	STATUS_RECONNECTING
)

Server Statuses

type StoreProvider

type StoreProvider func(string) (*data.Store, error)

StoreProvider transforms an optional path into a store.

Jump to

Keyboard shortcuts

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