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-12-20 17:40:10 +00:00
|
|
|
bh "github.com/timshannon/bolthold"
|
2021-07-21 18:52:45 +00:00
|
|
|
"net/http"
|
2021-01-31 21:48:53 +00:00
|
|
|
"regexp"
|
|
|
|
|
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
|
2021-10-05 23:05:27 +00:00
|
|
|
// Ephemeral sends a disappearing message to a user in chat
|
2020-10-24 13:54:18 +00:00
|
|
|
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
|
2021-10-05 23:05:27 +00:00
|
|
|
// Event is unknown/generic
|
2019-02-05 15:54:13 +00:00
|
|
|
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
|
2021-02-14 21:57:22 +00:00
|
|
|
// Startup is triggered after the connector has run the Serve function
|
|
|
|
Startup
|
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
|
2021-02-02 02:25:19 +00:00
|
|
|
type CallbackMap map[string]map[Kind][]HandlerSpec
|
2021-01-31 21:48:53 +00:00
|
|
|
|
2021-02-01 15:45:41 +00:00
|
|
|
type HandlerSpec struct {
|
2021-02-07 18:29:02 +00:00
|
|
|
Kind Kind
|
|
|
|
IsCmd bool
|
|
|
|
Regex *regexp.Regexp
|
|
|
|
HelpText string
|
|
|
|
Handler ResponseHandler
|
2021-02-01 15:45:41 +00:00
|
|
|
}
|
|
|
|
type HandlerTable []HandlerSpec
|
|
|
|
|
2021-01-31 21:48:53 +00:00
|
|
|
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
|
2021-12-20 17:40:10 +00:00
|
|
|
//DB() *sqlx.DB
|
|
|
|
Store() *bh.Store
|
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
|
|
|
|
2021-02-04 01:35:56 +00:00
|
|
|
// bot receives from a Connector.
|
2020-06-17 18:24:34 +00:00
|
|
|
// 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)
|
|
|
|
|
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)
|
|
|
|
|
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
|
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
|
2021-07-28 15:32:59 +00:00
|
|
|
RegisterWebName(http.Handler, string, string)
|
|
|
|
|
|
|
|
// RegisterWeb records a web endpoint for the API
|
|
|
|
RegisterWeb(http.Handler, string)
|
2021-07-21 18:52:45 +00:00
|
|
|
|
|
|
|
// Start the HTTP service
|
|
|
|
ListenAndServe()
|
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
|
2021-04-27 16:36:34 +00:00
|
|
|
|
|
|
|
// Check if a particular plugin is blacklisted
|
|
|
|
OnBlacklist(string, string) bool
|
2021-07-21 13:54:29 +00:00
|
|
|
|
|
|
|
// Check valid password
|
|
|
|
CheckPassword(secret, password string) bool
|
2021-11-16 01:34:09 +00:00
|
|
|
|
|
|
|
// PubToASub publishes a message to any subscribers
|
|
|
|
PubToASub(subject string, payload interface{})
|
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
|
|
|
|
2021-11-20 20:28:53 +00:00
|
|
|
// URLFormat utility
|
2020-12-02 14:53:57 +00:00
|
|
|
URLFormat(title, url string) string
|
|
|
|
|
2021-11-20 20:28:53 +00:00
|
|
|
// Emojy translates emojy to/from services
|
2020-12-02 14:53:57 +00:00
|
|
|
Emojy(string) string
|
2021-04-28 20:48:02 +00:00
|
|
|
|
|
|
|
// GetChannelName returns the human-friendly name for an ID (if possible)
|
|
|
|
GetChannelName(id string) string
|
|
|
|
|
2021-11-20 20:28:53 +00:00
|
|
|
// GetChannelID returns the channel ID for a human-friendly name (if possible)
|
2021-04-28 20:48:02 +00:00
|
|
|
GetChannelID(id string) string
|
2021-07-28 15:32:59 +00:00
|
|
|
|
2021-11-20 20:28:53 +00:00
|
|
|
// GetRouter gets any web handlers the connector exposes
|
2021-07-28 15:32:59 +00:00
|
|
|
GetRouter() (http.Handler, string)
|
2021-11-20 20:28:53 +00:00
|
|
|
|
|
|
|
// GetRoleNames returns list of names and IDs of roles
|
|
|
|
GetRoles() ([]Role, error)
|
|
|
|
|
|
|
|
// SetRole toggles a role on/off for a user by ID
|
|
|
|
SetRole(userID, roleID string) error
|
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{}
|
2021-11-20 20:28:53 +00:00
|
|
|
|
|
|
|
type Role struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|