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
|
|
|
|
|
2016-03-30 14:00:20 +00:00
|
|
|
import (
|
2021-01-31 21:48:53 +00:00
|
|
|
"regexp"
|
|
|
|
|
2016-03-30 14:00:20 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-06-09 17:48:56 +00:00
|
|
|
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
2016-03-30 14:00:20 +00:00
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
2019-02-05 15:54:13 +00:00
|
|
|
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
|
2019-02-05 15:54:13 +00:00
|
|
|
// 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
|
2020-09-09 17:21:39 +00:00
|
|
|
// Delete removes a message by ID
|
|
|
|
Delete
|
2019-02-05 15:54:13 +00:00
|
|
|
)
|
|
|
|
|
2020-10-24 13:54:18 +00:00
|
|
|
type EphemeralID string
|
|
|
|
|
2021-01-09 21:55:55 +00:00
|
|
|
type UnfurlLinks bool
|
|
|
|
|
2019-03-10 03:40:03 +00:00
|
|
|
type ImageAttachment struct {
|
|
|
|
URL string
|
|
|
|
AltTxt string
|
2020-09-09 17:21:39 +00:00
|
|
|
Width int
|
|
|
|
Height int
|
2019-03-10 03:40:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 21:48:53 +00:00
|
|
|
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
|
2021-01-31 21:48:53 +00:00
|
|
|
type ResponseHandler func(Request) bool
|
|
|
|
type CallbackMap map[string]map[Kind]map[*regexp.Regexp][]ResponseHandler
|
|
|
|
|
|
|
|
type RegexValues map[string]string
|
2019-02-05 15:54:13 +00:00
|
|
|
|
2020-05-25 18:42:56 +00:00
|
|
|
// b interface serves to allow mocking of the actual bot
|
2016-03-30 14:00:20 +00:00
|
|
|
type Bot interface {
|
2019-02-05 16:20:43 +00:00
|
|
|
// Config allows access to the bot's configuration system
|
2016-03-30 14:00:20 +00:00
|
|
|
Config() *config.Config
|
2020-06-17 18:24:34 +00:00
|
|
|
|
2019-02-05 16:20:43 +00:00
|
|
|
// DB gives access to the current database
|
2016-03-30 14:00:20 +00:00
|
|
|
DB() *sqlx.DB
|
2020-06-17 18:24:34 +00:00
|
|
|
|
2019-02-05 16:20:43 +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)
|
2016-04-01 14:20:03 +00:00
|
|
|
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
|
|
|
|
2019-02-05 16:20:43 +00:00
|
|
|
// AddPlugin registers a new plugin handler
|
2019-02-05 21:10:36 +00:00
|
|
|
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-01-31 21:48:53 +00:00
|
|
|
// Register a plugin callback
|
|
|
|
// Kind will be matched to the event for the callback
|
|
|
|
RegisterRegex(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
|
2019-02-05 18:58:12 +00:00
|
|
|
Register(Plugin, Kind, Callback)
|
2019-02-05 15:54:13 +00:00
|
|
|
|
2020-06-17 18:24:34 +00:00
|
|
|
// Filter prepares a message to be sent
|
|
|
|
// This loads the message with things like $variables
|
2016-04-01 14:20:03 +00:00
|
|
|
Filter(msg.Message, string) string
|
2020-06-17 18:24:34 +00:00
|
|
|
|
|
|
|
// LastMessage returns the last message the bot has seen
|
2016-04-01 14:20:03 +00:00
|
|
|
LastMessage(string) (msg.Message, error)
|
2019-02-05 15:54:13 +00:00
|
|
|
|
2020-06-17 18:24:34 +00:00
|
|
|
// CheckAdmin returns a user's admin status
|
2016-04-01 14:20:03 +00:00
|
|
|
CheckAdmin(string) bool
|
2020-06-17 18:24:34 +00:00
|
|
|
|
|
|
|
// GetEmojiList returns known emoji
|
2017-07-24 19:09:27 +00:00
|
|
|
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
|
2019-06-09 04:15:06 +00:00
|
|
|
GetWebNavigation() []EndPoint
|
2020-06-17 18:24:34 +00:00
|
|
|
|
|
|
|
// GetPassword generates a unique password for modification commands on the public website
|
2019-06-13 14:04:06 +00:00
|
|
|
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
|
2020-06-09 17:48:56 +00:00
|
|
|
GetPluginNames() []string
|
2020-06-17 18:24:34 +00:00
|
|
|
|
|
|
|
// RefreshPluginBlacklist reloads the list of plugins disabled per room from the DB
|
2020-06-09 17:48:56 +00:00
|
|
|
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
|
2016-03-30 14:00:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:43 +00:00
|
|
|
// 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
|
2019-02-06 03:52:49 +00:00
|
|
|
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
|
2019-02-05 18:33:18 +00:00
|
|
|
Send(Kind, ...interface{}) (string, error)
|
2019-02-05 15:54:13 +00:00
|
|
|
|
2020-06-17 18:24:34 +00:00
|
|
|
// GetEmojiList returns a connector's known custom emoji
|
2017-07-24 19:09:27 +00:00
|
|
|
GetEmojiList() map[string]string
|
2020-06-17 18:24:34 +00:00
|
|
|
|
|
|
|
// Serve starts a connector's connection routine
|
2017-09-07 04:32:53 +00:00
|
|
|
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
|
2020-05-08 14:31:27 +00:00
|
|
|
Profile(string) (user.User, error)
|
2020-12-02 14:53:57 +00:00
|
|
|
|
|
|
|
// URL Format utility
|
|
|
|
URLFormat(title, url string) string
|
|
|
|
|
|
|
|
// Translate emojy to/from services
|
|
|
|
Emojy(string) string
|
2016-03-10 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 16:20:43 +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{}
|