catbase/bot/interfaces.go

279 lines
7.0 KiB
Go
Raw Permalink 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 (
"github.com/gabriel-vasile/mimetype"
2024-02-27 19:29:54 +00:00
"github.com/velour/catbase/bot/web"
"net/http"
"regexp"
"strings"
"github.com/jmoiron/sqlx"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
"github.com/velour/catbase/config"
)
const (
_ = iota
// Any will be picked up by the matcher as generic
Any
// 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
// Reply something containing a message reference
Reply
// Action any /me action
Action
2023-08-02 20:52:11 +00:00
// Spoiler is for commented out messages
Spoiler
// 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
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
// Startup is triggered after the connector has run the Serve function
Startup
2023-01-04 16:16:49 +00:00
// Shutdown is triggered after an OS interrupt
Shutdown
)
2020-10-24 13:54:18 +00:00
type EphemeralID string
2021-01-09 21:55:55 +00:00
type UnfurlLinks bool
type EmbedAuthor struct {
ID string
Who string
IconURL string
}
type File struct {
Description string
Data []byte
mime *mimetype.MIME
}
type MessageReference struct {
MessageID string `json:"message_id"`
ChannelID string `json:"channel_id,omitempty"`
GuildID string `json:"guild_id,omitempty"`
}
func (f File) Mime() *mimetype.MIME {
if f.mime == nil {
f.mime = mimetype.Detect(f.Data)
}
return f.mime
}
func (f File) ContentType() string {
return f.Mime().String()
}
func (f File) FileName() string {
ext := f.Mime().Extension()
return strings.ReplaceAll(f.Description, " ", "-") + ext
}
type ImageAttachment struct {
URL string
AltTxt string
Width int
Height int
}
type Request struct {
Conn Connector
Kind Kind
Msg msg.Message
Values RegexValues
Args []any
}
2019-02-05 17:25:31 +00:00
type Kind int
type Callback func(Connector, Kind, msg.Message, ...any) bool
type ResponseHandler func(Request) bool
2021-02-02 02:25:19 +00:00
type CallbackMap map[string]map[Kind][]HandlerSpec
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
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
Send(Connector, Kind, ...any) (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
Receive(Connector, Kind, msg.Message, ...any) 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(bool) 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
// 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
2024-02-27 19:29:54 +00:00
// GetWeb returns the bot's webserver structure
GetWeb() *web.Web
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
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 any)
}
// 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, ...any) (string, error)
2020-06-17 18:24:34 +00:00
// GetEmojiList returns a connector's known custom emoji
GetEmojiList(bool) 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
2022-07-22 21:04:51 +00:00
// Shutdown cleans up after the connection
Shutdown()
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)
2021-11-20 20:28:53 +00:00
// URLFormat utility
URLFormat(title, url string) string
2021-11-20 20:28:53 +00:00
// Emojy translates emojy to/from services
Emojy(string) string
// UploadEmojy creates a new emojy on the server related to the file at path
UploadEmojy(emojy, path string) error
// DeleteEmojy removes the specified emojy from the server
DeleteEmojy(emojy string) error
// 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)
GetChannelID(id string) string
2021-11-20 20:28:53 +00:00
// GetRouter gets any web handlers the connector exposes
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
2022-08-02 17:26:49 +00:00
// Nick sets the username of the bot on the server
Nick(string) error
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
type Plugin any
2021-11-20 20:28:53 +00:00
type Role struct {
ID string `json:"id"`
Name string `json:"name"`
}