config

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: 7 Imported by: 4

Documentation

Overview

Package config creates a configuration using toml.

An example configuration looks like this:

# Anything defined here provides fallback defaults for all networks.
# except the immediately following fields which are global-only.
# In other words, all values you see in the network definition can be
# defined here and all servers will use those values unless they have their
# own defined.
storefile = "/path/to/store/file.db"
nocorecmds = false
loglevel = "debug"
logfile = "/path/to/file.log"
secret_key = "myunbelievablylongandsecrettoken"
ignores = ["hostsuffix"]

# Most of the configuration values below have healthy defaults which means
# you don't have to set any of them. servers, nick, username, realname is
# enough!
[networks.ircnet]
	servers = ["localhost:3333", "server.com:6667"]

	nick = "Nick"
	altnick = "Altnick"
	username = "Username"
	realname = "Realname"
	password = "Password"

	# TLS Options
	# If tls is on it will connect with tls.
	# If tls_cert, tls_key are set it will send it in an attempt to perform
	# mutual TLS with the irc server.
	# If tls_ca_cert is present it will be used as a CA when connecting
	tls         = true
	tls_ca_cert = "/path/to/ca.crt"
	tls_cert    = "/path/to/a.crt"
	tls_key     = "/path/to/a.key"
	tls_insecure_skip_verify = false

	# Bot Internal Database Options
	nostate = false
	nostore = false

	# Auto(Re)Join controls.
	noautojoin = false
	# How many seconds after connect or while banned to wait to rejoin.
	joindelay = 5

	# Flood control fine tuning knobs.
	floodlenpenalty = 120
	floodtimeout = 10.0
	floodstep = 2.0

	# Send a ping to the server every X seconds.
	keepalive = 60.0

	# Reconnection controls.
	noreconnect = false
	reconnecttimeout = 20

	# For fallback of channels below.
	prefix = "."

	[[networks.ircnet.channels]]
		name     = "#channel1"
		password = "pass1"
		prefix   = "!"
	[[networks.ircnet.channels]]
		name     = "&channel2"
		password = "pass2"
		prefix   = "@"

# Ext provides defaults for all exts, much as the global definitions provide
# defaults for all networks.
[ext]
	# Define listen to create a extension server for extensions to connect
	listen = "localhost:3333"
	# OR
	listen = "/path/to/unix.sock"

	# If tls key & cert are present the remote extensions will require
	# MUTUAL tls, meaning the client will have to present a certificate as
	# well, you can use tls_client_ca to control which ca cert is used
	# to verify the client (otherwise the system ca cert pool is used).
	# If tls_insecure_skip_verify is set, the client's certificate
	# will still be required but will not be verified
	tls_key      = "/path/to/a.key"
	tls_cert     = "/path/to/a.crt"
	tls_client_ca  = "/path/to/ca.crt"
	# A CRL (revocation list) can be passed in, client certificates
	# can be revoked by adding them to the crl
	tls_client_revs = "/path/to/ca.crl"
	tls_insecure_skip_verify = false

	# Define the execdir to start all executables in the path.
	execdir = "/path/to/executables"

	# Ext configuration is deeply nested so we can configure it globally
	# based on the network, or based on the channel on that network, or even
	# on all channels on that network.
	[ext.config] # Global config value
		key = "stringvalue"
	[[ext.config.channels]] # All networks for #channel
		name = "#channel"
		key  = "stringvalue"
	[ext.config.networks.ircnet] # All channels on ircnet network
		key = "stringvalue"
	[[ext.config.networks.ircnet.channels]] # Freenode's #channel
		name = "#channel1"
		key  = "stringvalue"

[exts.myext]
	# Define exec to specify a path to the executable to launch.
	# NOTE: Currently NOT USED
	exec = "/path/to/executable"

	# Defining this means that the bot will try to connect to this extension
	# rather than expecting it to connect to the listen server in the
	# global configuration. Server can also be unix:/path/to/sock
	#
	# NOTE: Currently NOT USED
	server       = "localhost:44"
	tls_cert     = "/path/to/a.crt"
	noverifycert = false

	[exts.myext.active]
		ircnet = ["#channel1", "#channel2"]

Once again note the fallback mechanisms between network and the "global scope" as well as the exts and ext. This can save you lots of repetitive typing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

type Channel struct {
	Name     string
	Password string
	Prefix   string
}

Channel is the configuration for a single channel.

type Config

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

Config holds all the information related to the bot including global settings default settings, and network specific settings.

func New

func New() *Config

New initializes a Config object.

func (*Config) Clear

func (c *Config) Clear()

Clear re-initializes all memory in the configuration.

func (*Config) Clone

func (c *Config) Clone() *Config

Clone deep copies a configuration object.

func (*Config) DisplayErrors

func (c *Config) DisplayErrors(logger log15.Logger)

DisplayErrors is a helper function to log the output of all config errors to the standard logger.

func (*Config) Errors

func (c *Config) Errors() []error

Errors returns the errors encountered during validation.

func (*Config) Ext

func (c *Config) Ext(name string) *ExtNormalCTX

Ext returns the extension context useable to get/set fields for the given extension name.

func (*Config) ExtGlobal

func (c *Config) ExtGlobal() *ExtGlobalCTX

ExtGlobal returns the global extension context useable to get/set fields for all extensions.

func (*Config) Exts

func (c *Config) Exts() []string

Exts returns a list of configured extensions.

func (*Config) Filename

func (c *Config) Filename() string

Filename returns fileName of the configuration, or the default.

func (*Config) FromFile

func (c *Config) FromFile(filename string) *Config

FromFile overwrites the current config with the contents of the file. It will use defaultConfigFileName if filename is the empty string.

func (*Config) FromReader

func (c *Config) FromReader(reader io.Reader) *Config

FromReader overwrites the current config with the contents of the reader.

func (*Config) FromString

func (c *Config) FromString(config string) *Config

FromString overwrites the current config with the contents of the string.

func (*Config) Ignores

func (c *Config) Ignores() ([]string, bool)

Ignores returns the global set ignores

func (*Config) LogFile

func (c *Config) LogFile() (string, bool)

LogFile gets the global logfile or defaultLogFile.

func (*Config) LogLevel

func (c *Config) LogLevel() (string, bool)

LogLevel gets the global loglevel or defaultLogLevel.

func (*Config) Network

func (c *Config) Network(name string) *NetCTX

Network returns the network context useable to get/set the fields for that. Leave name blank to return the global network context.

func (*Config) Networks

func (c *Config) Networks() []string

Networks returns a list of configured networks.

func (*Config) NewExt

func (c *Config) NewExt(name string) *ExtNormalCTX

NewExt creates a extension and returns the extension's context. If the extension exists, the context will be nil.

func (*Config) NewNetwork

func (c *Config) NewNetwork(name string) *NetCTX

NewNetwork creates a network and returns the network's context. If the network exists, the context will be nil.

func (*Config) NoCoreCmds

func (c *Config) NoCoreCmds() (bool, bool)

NoCoreCmds gets the value of the corecmds variable.

func (*Config) Replace

func (c *Config) Replace(newConfig *Config) *Config

Replace replaces the configuration with a new one.

func (*Config) SecretKey

func (c *Config) SecretKey() (string, bool)

SecretKey gets the value of the secretKey variable

func (*Config) SetIgnores

func (c *Config) SetIgnores(ignores []string) *Config

SetIgnores sets the ignores array

func (*Config) SetLogFile

func (c *Config) SetLogFile(val string) *Config

SetLogFile sets the global logfile or defaultLogFile.

func (*Config) SetLogLevel

func (c *Config) SetLogLevel(val string) *Config

SetLogLevel sets the global loglevel or defaultLogLevel.

func (*Config) SetNoCoreCmds

func (c *Config) SetNoCoreCmds(val bool) *Config

SetNoCoreCmds gets the value of the corecmds variable.

func (*Config) SetSecretKey

func (c *Config) SetSecretKey(key string) *Config

SetSecretKey value in the config.

func (*Config) SetStoreFile

func (c *Config) SetStoreFile(val string) *Config

SetStoreFile sets the global storefile or defaultStoreFile.

func (*Config) StoreFile

func (c *Config) StoreFile() (string, bool)

StoreFile gets the global storefile or defaultStoreFile.

func (*Config) ToFile

func (c *Config) ToFile(filename string) error

ToFile writes a config out to a writer. If the filename is empty it will write to the file that this config was loaded from, or it will write to the defaultConfigFileName.

func (*Config) ToWriter

func (c *Config) ToWriter(writer io.Writer) error

ToWriter writes a config out to a writer.

func (*Config) Validate

func (c *Config) Validate() bool

Validate checks to see if the configuration is valid. If errors are found in the config the Config.Errors() will return the validation errors. These can be used to display to the user. See DisplayErrors for a display helper.

type ExtCTX

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

ExtCTX is an extension context. It's getters and setters are available on both the ExtGlobalCTX and the ExtNormalCTX. When using getters from ExtCTX on an ExtNormalCTX they will fallback to the parent if there is none set.

func (*ExtCTX) Active

func (e *ExtCTX) Active(network string) ([]string, bool)

func (*ExtCTX) NoReconnect

func (e *ExtCTX) NoReconnect() (bool, bool)

func (*ExtCTX) ReconnectTimeout

func (e *ExtCTX) ReconnectTimeout() (uint, bool)

func (*ExtCTX) SetActive

func (e *ExtCTX) SetActive(network string, value []string) *ExtCTX

func (*ExtCTX) SetNoReconnect

func (e *ExtCTX) SetNoReconnect(val bool) *ExtCTX

func (*ExtCTX) SetReconnectTimeout

func (e *ExtCTX) SetReconnectTimeout(val uint) *ExtCTX

type ExtGlobalCTX

type ExtGlobalCTX struct {
	*ExtCTX
}

ExtGlobalCTX is the configuration context for the global extension config portion.

func (*ExtGlobalCTX) Config

func (e *ExtGlobalCTX) Config(network, channel string) map[string]string

Config returns a map of config values for the given network and channel. Global values are overidden by more specific ones, and all global values are shared.

[ext.config]
	key = "val"
	global = "val"
[ext.config.channels.#channel]
	key = "chan"
[ext.config.networks.ircnet]
	key = "net"
[ext.config.networks.ircnet.channels.#channel]
	key = "netchan"

Given this configuration these results are expected:

Config("", "") => key: "val", global: "val"
Config("net", "") => key: "chan", global: "val"
Config("", "chan") => key: "net", global: "val"
Config("net", "chan") => key: "netchan", global: "val"

func (*ExtGlobalCTX) ConfigVal

func (e *ExtGlobalCTX) ConfigVal(network, channel, key string) (string, bool)

ConfigVal returns a value from the configuration with proper fallbacking to the global extension config. Ok is false if the key was not found.

func (*ExtGlobalCTX) ExecDir

func (e *ExtGlobalCTX) ExecDir() (string, bool)

func (*ExtGlobalCTX) Listen

func (e *ExtGlobalCTX) Listen() (string, bool)

func (*ExtGlobalCTX) SetConfig

func (e *ExtGlobalCTX) SetConfig(network, channel, key,
	value string) *ExtGlobalCTX

SetConfig sets a key value pair for a given network and channel. If you leave either network or channel empty, then it's set at the global level for that portion.

[ext.config]
	# SetConfig("", "", "key", "val")
	key = "val"
[ext.config.channels.#channel]
	# SetConfig("", "#channel", "key", "val")
	key = "val"
[ext.config.networks.ircnet]
	# SetConfig("ircnet", "", "key", "val")
	key = "val"
[ext.config.networks.ircnet.channels.#channel]
	# SetConfig("ircnet", "#channel", "key", "val")
	key = "val"

func (*ExtGlobalCTX) SetExecDir

func (e *ExtGlobalCTX) SetExecDir(val string) *ExtGlobalCTX

func (*ExtGlobalCTX) SetListen

func (e *ExtGlobalCTX) SetListen(val string) *ExtGlobalCTX

func (*ExtGlobalCTX) SetTLSCert

func (e *ExtGlobalCTX) SetTLSCert(val string) *ExtGlobalCTX

func (*ExtGlobalCTX) SetTLSClientCA

func (e *ExtGlobalCTX) SetTLSClientCA(val string) *ExtGlobalCTX

func (*ExtGlobalCTX) SetTLSClientRevs

func (e *ExtGlobalCTX) SetTLSClientRevs(val string) *ExtGlobalCTX

func (*ExtGlobalCTX) SetTLSInsecureSkipVerify

func (e *ExtGlobalCTX) SetTLSInsecureSkipVerify(val bool) *ExtGlobalCTX

func (*ExtGlobalCTX) SetTLSKey

func (e *ExtGlobalCTX) SetTLSKey(val string) *ExtGlobalCTX

func (*ExtGlobalCTX) TLSCert

func (e *ExtGlobalCTX) TLSCert() (string, bool)

func (*ExtGlobalCTX) TLSClientCA

func (e *ExtGlobalCTX) TLSClientCA() (string, bool)

func (*ExtGlobalCTX) TLSClientRevs

func (e *ExtGlobalCTX) TLSClientRevs() (string, bool)

func (*ExtGlobalCTX) TLSInsecureSkipVerify

func (e *ExtGlobalCTX) TLSInsecureSkipVerify() (bool, bool)

func (*ExtGlobalCTX) TLSKey

func (e *ExtGlobalCTX) TLSKey() (string, bool)

type ExtNormalCTX

type ExtNormalCTX struct {
	*ExtCTX
}

ExtNormalCTX is the configuration context for normal extensions (not global). All methods that are embedded from ExtCTX will fallback to the global values if they are not specifically set on this context.

func (*ExtNormalCTX) Exec

func (e *ExtNormalCTX) Exec() (string, bool)

func (*ExtNormalCTX) Server

func (e *ExtNormalCTX) Server() (string, bool)

func (*ExtNormalCTX) SetExec

func (e *ExtNormalCTX) SetExec(val string) *ExtNormalCTX

func (*ExtNormalCTX) SetServer

func (e *ExtNormalCTX) SetServer(val string) *ExtNormalCTX

func (*ExtNormalCTX) SetTLSCert

func (e *ExtNormalCTX) SetTLSCert(val string) *ExtNormalCTX

func (*ExtNormalCTX) SetTLSInsecureSkipVerify

func (e *ExtNormalCTX) SetTLSInsecureSkipVerify(val bool) *ExtNormalCTX

func (*ExtNormalCTX) SetUnix

func (e *ExtNormalCTX) SetUnix(val string) *ExtNormalCTX

func (*ExtNormalCTX) TLSCert

func (e *ExtNormalCTX) TLSCert() (string, bool)

func (*ExtNormalCTX) TLSInsecureSkipVerify

func (e *ExtNormalCTX) TLSInsecureSkipVerify() (bool, bool)

func (*ExtNormalCTX) Unix

func (e *ExtNormalCTX) Unix() (string, bool)

type NetCTX

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

NetCTX is a context for network parts of the config, allowing querying and setting of network related values. If this context belongs to a specific network and not the global network configuration, all the values will fallback to the global configuration if not set.

func (*NetCTX) Altnick

func (n *NetCTX) Altnick() (string, bool)

func (*NetCTX) ChannelPrefix

func (n *NetCTX) ChannelPrefix(channel string) (rune, bool)

ChannelPrefix retrieves the prefix with the correct default chain up to network and global space.

func (*NetCTX) Channels

func (n *NetCTX) Channels() (map[string]Channel, bool)

func (*NetCTX) FloodLenPenalty

func (n *NetCTX) FloodLenPenalty() (uint, bool)

func (*NetCTX) FloodStep

func (n *NetCTX) FloodStep() (float64, bool)

func (*NetCTX) FloodTimeout

func (n *NetCTX) FloodTimeout() (float64, bool)

func (*NetCTX) JoinDelay

func (n *NetCTX) JoinDelay() (uint, bool)

func (*NetCTX) KeepAlive

func (n *NetCTX) KeepAlive() (float64, bool)

func (*NetCTX) Nick

func (n *NetCTX) Nick() (string, bool)

func (*NetCTX) NoAutoJoin

func (n *NetCTX) NoAutoJoin() (bool, bool)

func (*NetCTX) NoReconnect

func (n *NetCTX) NoReconnect() (bool, bool)

func (*NetCTX) NoState

func (n *NetCTX) NoState() (bool, bool)

func (*NetCTX) NoStore

func (n *NetCTX) NoStore() (bool, bool)

func (*NetCTX) Password

func (n *NetCTX) Password() (string, bool)

func (*NetCTX) Prefix

func (n *NetCTX) Prefix() (rune, bool)

func (*NetCTX) Realname

func (n *NetCTX) Realname() (string, bool)

func (*NetCTX) ReconnectTimeout

func (n *NetCTX) ReconnectTimeout() (uint, bool)

func (*NetCTX) Servers

func (n *NetCTX) Servers() ([]string, bool)

func (*NetCTX) SetAltnick

func (n *NetCTX) SetAltnick(val string) *NetCTX

func (*NetCTX) SetChannels

func (n *NetCTX) SetChannels(val []Channel) *NetCTX

func (*NetCTX) SetFloodLenPenalty

func (n *NetCTX) SetFloodLenPenalty(val uint) *NetCTX

func (*NetCTX) SetFloodStep

func (n *NetCTX) SetFloodStep(val float64) *NetCTX

func (*NetCTX) SetFloodTimeout

func (n *NetCTX) SetFloodTimeout(val float64) *NetCTX

func (*NetCTX) SetJoinDelay

func (n *NetCTX) SetJoinDelay(val uint) *NetCTX

func (*NetCTX) SetKeepAlive

func (n *NetCTX) SetKeepAlive(val float64) *NetCTX

func (*NetCTX) SetNick

func (n *NetCTX) SetNick(val string) *NetCTX

func (*NetCTX) SetNoAutoJoin

func (n *NetCTX) SetNoAutoJoin(val bool) *NetCTX

func (*NetCTX) SetNoReconnect

func (n *NetCTX) SetNoReconnect(val bool) *NetCTX

func (*NetCTX) SetNoState

func (n *NetCTX) SetNoState(val bool) *NetCTX

func (*NetCTX) SetNoStore

func (n *NetCTX) SetNoStore(val bool) *NetCTX

func (*NetCTX) SetPassword

func (n *NetCTX) SetPassword(val string) *NetCTX

func (*NetCTX) SetPrefix

func (n *NetCTX) SetPrefix(val rune) *NetCTX

func (*NetCTX) SetRealname

func (n *NetCTX) SetRealname(val string) *NetCTX

func (*NetCTX) SetReconnectTimeout

func (n *NetCTX) SetReconnectTimeout(val uint) *NetCTX

func (*NetCTX) SetServers

func (n *NetCTX) SetServers(val []string) *NetCTX

func (*NetCTX) SetTLS

func (n *NetCTX) SetTLS(val bool) *NetCTX

func (*NetCTX) SetTLSCACert

func (n *NetCTX) SetTLSCACert(val string) *NetCTX

func (*NetCTX) SetTLSCert

func (n *NetCTX) SetTLSCert(val string) *NetCTX

func (*NetCTX) SetTLSInsecureSkipVerify

func (n *NetCTX) SetTLSInsecureSkipVerify(val bool) *NetCTX

func (*NetCTX) SetTLSKey

func (n *NetCTX) SetTLSKey(val string) *NetCTX

func (*NetCTX) SetUsername

func (n *NetCTX) SetUsername(val string) *NetCTX

func (*NetCTX) TLS

func (n *NetCTX) TLS() (bool, bool)

func (*NetCTX) TLSCACert

func (n *NetCTX) TLSCACert() (string, bool)

func (*NetCTX) TLSCert

func (n *NetCTX) TLSCert() (string, bool)

func (*NetCTX) TLSInsecureSkipVerify

func (n *NetCTX) TLSInsecureSkipVerify() (bool, bool)

func (*NetCTX) TLSKey

func (n *NetCTX) TLSKey() (string, bool)

func (*NetCTX) Username

func (n *NetCTX) Username() (string, bool)

Jump to

Keyboard shortcuts

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