catbase/bot/interfaces.go

191 lines
5.0 KiB
Go
Raw Normal View History

2016-03-19 19:32:51 +00:00
// © 2016 the CatBase Authors under the WTFPL license. See AUTHORS for the list of authors.
2016-03-10 18:37:07 +00:00
package bot
import (
"regexp"
"github.com/jmoiron/sqlx"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
"github.com/velour/catbase/config"
)
const (
_ = iota
// Message any standard chat
Message
2020-10-24 13:54:18 +00:00
// Send a disappearing message to a user in chat
Ephemeral
// Reply something containing a message reference
Reply
// Action any /me action
Action
// Reaction Icon reaction if service supports it
Reaction
// Edit message ref'd new message to replace
Edit
// Not sure what event is
Event
// Help is used when the bot help system is triggered
Help
// SelfMessage triggers when the bot is sending a message
SelfMessage
// Delete removes a message by ID
Delete
)
2020-10-24 13:54:18 +00:00
type EphemeralID string
2021-01-09 21:55:55 +00:00
type UnfurlLinks bool
type ImageAttachment struct {
URL string
AltTxt string
Width int
Height int
}
type Request struct {
Conn Connector
Kind Kind
Msg msg.Message
Values RegexValues
Args []interface{}
}
2019-02-05 17:25:31 +00:00
type Kind int
2019-05-27 23:21:53 +00:00
type Callback func(Connector, Kind, msg.Message, ...interface{}) bool
type ResponseHandler func(Request) bool
type CallbackMap map[string]map[Kind]map[*regexp.Regexp][]ResponseHandler
2021-02-01 15:45:41 +00:00
type HandlerSpec struct {
Kind Kind
IsCmd bool
Regex *regexp.Regexp
Handler ResponseHandler
}
type HandlerTable []HandlerSpec
type RegexValues map[string]string
2020-05-25 18:42:56 +00:00
// b interface serves to allow mocking of the actual bot
type Bot interface {
// Config allows access to the bot's configuration system
Config() *config.Config
2020-06-17 18:24:34 +00:00
// DB gives access to the current database
DB() *sqlx.DB
2020-06-17 18:24:34 +00:00
// Who lists users in a particular channel
2020-06-17 18:24:34 +00:00
// The channel should be represented as an ID for slack (check the connector for details)
Who(string) []user.User
2020-06-17 18:24:34 +00:00
2019-05-27 23:21:53 +00:00
// WhoAmI gives a nick for the bot
WhoAmI() string
2020-06-17 18:24:34 +00:00
// AddPlugin registers a new plugin handler
AddPlugin(Plugin)
2020-06-17 18:24:34 +00:00
// Send transmits a message to a Connector.
// Kind is listed in the bot's enum, one of bot.Message/Reply/Action/etc
// Usually, the first vararg should be a channel ID, but refer to the Connector for info
2019-05-27 23:21:53 +00:00
Send(Connector, Kind, ...interface{}) (string, error)
2020-06-17 18:24:34 +00:00
// Bot receives from a Connector.
// The Kind arg should be one of bot.Message/Reply/Action/etc
2019-05-27 23:21:53 +00:00
Receive(Connector, Kind, msg.Message, ...interface{}) bool
2020-06-17 18:24:34 +00:00
2021-02-01 15:45:41 +00:00
// Register a set of plugin callbacks
// Kind will be matched to the event for the callback
RegisterTable(Plugin, HandlerTable)
// Register a plugin callback
// Kind will be matched to the event for the callback
RegisterRegex(Plugin, Kind, *regexp.Regexp, ResponseHandler)
2021-01-31 23:08:25 +00:00
// Register a plugin callback filtering non-commands out
// Kind will be matched to the event for the callback
RegisterRegexCmd(Plugin, Kind, *regexp.Regexp, ResponseHandler)
2020-06-17 18:24:34 +00:00
// Register a plugin callback
// Kind will be matched to the event for the callback
Register(Plugin, Kind, Callback)
2020-06-17 18:24:34 +00:00
// Filter prepares a message to be sent
// This loads the message with things like $variables
Filter(msg.Message, string) string
2020-06-17 18:24:34 +00:00
// LastMessage returns the last message the bot has seen
LastMessage(string) (msg.Message, error)
2020-06-17 18:24:34 +00:00
// CheckAdmin returns a user's admin status
CheckAdmin(string) bool
2020-06-17 18:24:34 +00:00
// GetEmojiList returns known emoji
GetEmojiList() map[string]string
2020-06-17 18:24:34 +00:00
// RegisterFilter creates a filter function for message processing
2017-09-29 04:58:21 +00:00
RegisterFilter(string, func(string) string)
2020-06-17 18:24:34 +00:00
// RegisterWeb records a web endpoint for the UI
2019-02-07 16:30:42 +00:00
RegisterWeb(string, string)
2020-06-17 18:24:34 +00:00
// DefaultConnector returns the base connector, which may not be the only connector
2019-05-27 23:21:53 +00:00
DefaultConnector() Connector
2020-06-17 18:24:34 +00:00
// GetWebNavigation returns the current known web endpoints
GetWebNavigation() []EndPoint
2020-06-17 18:24:34 +00:00
// GetPassword generates a unique password for modification commands on the public website
GetPassword() string
2020-06-17 18:24:34 +00:00
// SetQuiet toggles the bot's ability to send messages
2020-04-29 21:45:53 +00:00
SetQuiet(bool)
2020-06-17 18:24:34 +00:00
// GetPluginNames returns an ordered list of registered plugins
GetPluginNames() []string
2020-06-17 18:24:34 +00:00
// RefreshPluginBlacklist reloads the list of plugins disabled per room from the DB
RefreshPluginBlacklist() error
2020-10-09 16:00:10 +00:00
// RefreshPluginWhitelist reloads the list of plugins enabled from the DB
RefreshPluginWhitelist() error
// Get the contents of the white list
GetWhitelist() []string
}
// Connector represents a server connection to a chat service
2016-03-10 18:37:07 +00:00
type Connector interface {
2020-06-17 18:24:34 +00:00
// RegisterEvent creates a callback to watch Connector events
RegisterEvent(Callback)
2016-03-10 18:37:07 +00:00
2020-06-17 18:24:34 +00:00
// Send transmits a message on the connector
Send(Kind, ...interface{}) (string, error)
2020-06-17 18:24:34 +00:00
// GetEmojiList returns a connector's known custom emoji
GetEmojiList() map[string]string
2020-06-17 18:24:34 +00:00
// Serve starts a connector's connection routine
Serve() error
2016-04-21 15:19:38 +00:00
2020-06-17 18:24:34 +00:00
// Who returns a user list for a channel
2016-04-21 15:19:38 +00:00
Who(string) []string
2020-06-17 18:24:34 +00:00
// Profile returns a user's information given an ID
Profile(string) (user.User, error)
// URL Format utility
URLFormat(title, url string) string
// Translate emojy to/from services
Emojy(string) string
2016-03-10 18:37:07 +00:00
}
// Plugin interface used for compatibility with the Plugin interface
2019-02-07 16:30:42 +00:00
// Uhh it turned empty, but we're still using it to ID plugins
2020-06-17 18:24:34 +00:00
type Plugin interface{}