commands

package
v0.19.0-mod-2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2024 License: MPL-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Deprecated: this should be used as a placeholder that needs to be fixed
	HelpSectionUnclassified = HelpSection{"Unclassified", -1}

	HelpSectionGeneral = HelpSection{"General", 0}
	HelpSectionAuth    = HelpSection{"Authentication", 10}
	HelpSectionChats   = HelpSection{"Starting and managing chats", 20}
	HelpSectionAdmin   = HelpSection{"Administration", 50}
)
View Source
var CommandCancel = &FullHandler{
	Func: func(ce *Event) {
		state := SwapCommandState(ce.User, nil)
		if state != nil {
			action := state.Action
			if action == "" {
				action = "Unknown action"
			}
			if state.Cancel != nil {
				state.Cancel()
			}
			ce.Reply("%s cancelled.", action)
		} else {
			ce.Reply("No ongoing command.")
		}
	},
	Name: "cancel",
	Help: HelpMeta{
		Section:     HelpSectionGeneral,
		Description: "Cancel an ongoing action.",
	},
}
View Source
var CommandDeletePortal = &FullHandler{
	Func: func(ce *Event) {
		err := ce.Portal.Delete(ce.Ctx)
		if err != nil {
			ce.Reply("Failed to delete portal: %v", err)
		}
		err = ce.Bot.DeleteRoom(ce.Ctx, ce.Portal.MXID, false)
		if err != nil {
			ce.Reply("Failed to clean up room: %v", err)
		}
		ce.MessageStatus.DisableMSS = true
	},
	Name: "delete-portal",
	Help: HelpMeta{
		Section:     HelpSectionAdmin,
		Description: "Delete the current portal room",
	},
	RequiresAdmin:  true,
	RequiresPortal: true,
}
View Source
var CommandHelp = &FullHandler{
	Func: func(ce *Event) {
		ce.Reply(FormatHelp(ce))
	},
	Name: "help",
	Help: HelpMeta{
		Section:     HelpSectionGeneral,
		Description: "Show this help message.",
	},
}
View Source
var CommandListLogins = &FullHandler{
	Func: fnListLogins,
	Name: "list-logins",
	Help: HelpMeta{
		Section:     HelpSectionAuth,
		Description: "List your logins",
	},
	RequiresLoginPermission: true,
}
View Source
var CommandLogin = &FullHandler{
	Func: fnLogin,
	Name: "login",
	Help: HelpMeta{
		Section:     HelpSectionAuth,
		Description: "Log into the bridge",
		Args:        "[_flow ID_]",
	},
	RequiresLoginPermission: true,
}
View Source
var CommandLogout = &FullHandler{
	Func: fnLogout,
	Name: "logout",
	Help: HelpMeta{
		Section:     HelpSectionAuth,
		Description: "Log out of the bridge",
		Args:        "<_login ID_>",
	},
}
View Source
var CommandRegisterPush = &FullHandler{
	Func: func(ce *Event) {
		if len(ce.Args) < 3 {
			ce.Reply("Usage: `$cmdprefix debug-register-push <login ID> <push type> <push token>`\n\nYour logins:\n\n%s", ce.User.GetFormattedUserLogins())
			return
		}
		pushType := bridgev2.PushTypeFromString(ce.Args[1])
		if pushType == bridgev2.PushTypeUnknown {
			ce.Reply("Unknown push type `%s`. Allowed types: `web`, `apns`, `fcm`", ce.Args[1])
			return
		}
		login := ce.Bridge.GetCachedUserLoginByID(networkid.UserLoginID(ce.Args[0]))
		if login == nil || login.UserMXID != ce.User.MXID {
			ce.Reply("Login `%s` not found", ce.Args[0])
			return
		}
		pushable, ok := login.Client.(bridgev2.PushableNetworkAPI)
		if !ok {
			ce.Reply("This network connector does not support push registration")
			return
		}
		pushToken := strings.Join(ce.Args[2:], " ")
		if pushToken == "null" {
			pushToken = ""
		}
		err := pushable.RegisterPushNotifications(ce.Ctx, pushType, pushToken)
		if err != nil {
			ce.Reply("Failed to register pusher: %v", err)
			return
		}
		if pushToken == "" {
			ce.Reply("Pusher de-registered successfully")
		} else {
			ce.Reply("Pusher registered successfully")
		}
	},
	Name: "debug-register-push",
	Help: HelpMeta{
		Section:     HelpSectionAdmin,
		Description: "Register a pusher",
		Args:        "<_login ID_> <_push type_> <_push token_>",
	},
	RequiresAdmin: true,
	RequiresLogin: true,
}
View Source
var CommandResolveIdentifier = &FullHandler{
	Func: fnResolveIdentifier,
	Name: "resolve-identifier",
	Help: HelpMeta{
		Section:     HelpSectionChats,
		Description: "Check if a given identifier is on the remote network",
		Args:        "[_login ID_] <_identifier_>",
	},
	RequiresLogin: true,
}
View Source
var CommandSetPreferredLogin = &FullHandler{
	Func:    fnSetPreferredLogin,
	Name:    "set-preferred-login",
	Aliases: []string{"prefer"},
	Help: HelpMeta{
		Section:     HelpSectionAuth,
		Description: "Set the preferred login ID for sending messages to this portal (only relevant when logged into multiple accounts via the bridge)",
		Args:        "<_login ID_>",
	},
	RequiresPortal:          true,
	RequiresLoginPermission: true,
}
View Source
var CommandSetRelay = &FullHandler{
	Func: fnSetRelay,
	Name: "set-relay",
	Help: HelpMeta{
		Section:     HelpSectionAuth,
		Description: "Use your account to relay messages sent by users who haven't logged in",
		Args:        "[_login ID_]",
	},
	RequiresPortal: true,
}
View Source
var CommandStartChat = &FullHandler{
	Func: fnResolveIdentifier,
	Name: "start-chat",
	Help: HelpMeta{
		Section:     HelpSectionChats,
		Description: "Start a direct chat with the given user",
		Args:        "[_login ID_] <_identifier_>",
	},
	RequiresLogin: true,
}
View Source
var CommandUnsetRelay = &FullHandler{
	Func: fnUnsetRelay,
	Name: "unset-relay",
	Help: HelpMeta{
		Section:     HelpSectionAuth,
		Description: "Stop relaying messages sent by users who haven't logged in",
	},
	RequiresPortal: true,
}

Functions

func FormatHelp

func FormatHelp(ce *Event) string

func NewProcessor

func NewProcessor(bridge *bridgev2.Bridge) bridgev2.CommandProcessor

NewProcessor creates a Processor

func StoreCommandState

func StoreCommandState(user *bridgev2.User, cs *CommandState)

Types

type AliasedCommandHandler

type AliasedCommandHandler interface {
	CommandHandler
	GetAliases() []string
}

type CommandHandler

type CommandHandler interface {
	MinimalCommandHandler
	GetName() string
}

type CommandState

type CommandState struct {
	Next   MinimalCommandHandler
	Action string
	Meta   any
	Cancel func()
}

func LoadCommandState

func LoadCommandState(user *bridgev2.User) *CommandState

func SwapCommandState

func SwapCommandState(user *bridgev2.User, cs *CommandState) *CommandState

type Event

type Event struct {
	Bot       bridgev2.MatrixAPI
	Bridge    *bridgev2.Bridge
	Portal    *bridgev2.Portal
	Processor *Processor
	Handler   MinimalCommandHandler
	RoomID    id.RoomID
	EventID   id.EventID
	User      *bridgev2.User
	Command   string
	Args      []string
	RawArgs   string
	ReplyTo   id.EventID
	Ctx       context.Context
	Log       *zerolog.Logger

	MessageStatus *bridgev2.MessageStatus
}

Event stores all data which might be used to handle commands

func (*Event) MarkRead

func (ce *Event) MarkRead()

MarkRead marks the command event as read.

func (*Event) React

func (ce *Event) React(key string)

React sends a reaction to the command.

func (*Event) Redact

func (ce *Event) Redact(req ...mautrix.ReqRedact)

Redact redacts the command.

func (*Event) Reply

func (ce *Event) Reply(msg string, args ...any)

Reply sends a reply to command as notice, with optional string formatting and automatic $cmdprefix replacement.

func (*Event) ReplyAdvanced

func (ce *Event) ReplyAdvanced(msg string, allowMarkdown, allowHTML bool)

ReplyAdvanced sends a reply to command as notice. It allows using HTML and disabling markdown, but doesn't have built-in string formatting.

type FullHandler

type FullHandler struct {
	Func func(*Event)

	Name    string
	Aliases []string
	Help    HelpMeta

	RequiresAdmin           bool
	RequiresPortal          bool
	RequiresLogin           bool
	RequiresEventLevel      event.Type
	RequiresLoginPermission bool
}

func (*FullHandler) GetAliases

func (fh *FullHandler) GetAliases() []string

func (*FullHandler) GetHelp

func (fh *FullHandler) GetHelp() HelpMeta

func (*FullHandler) GetName

func (fh *FullHandler) GetName() string

func (*FullHandler) Run

func (fh *FullHandler) Run(ce *Event)

func (*FullHandler) ShowInHelp

func (fh *FullHandler) ShowInHelp(ce *Event) bool

type HelpMeta

type HelpMeta struct {
	Command     string
	Section     HelpSection
	Description string
	Args        string
}

func (*HelpMeta) String

func (hm *HelpMeta) String() string

type HelpSection

type HelpSection struct {
	Name  string
	Order int
}

type HelpfulHandler

type HelpfulHandler interface {
	CommandHandler
	GetHelp() HelpMeta
	ShowInHelp(*Event) bool
}

type MinimalCommandHandler

type MinimalCommandHandler interface {
	Run(*Event)
}

type MinimalCommandHandlerFunc

type MinimalCommandHandlerFunc func(*Event)

func (MinimalCommandHandlerFunc) Run

func (mhf MinimalCommandHandlerFunc) Run(ce *Event)

type Processor

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

func (*Processor) AddHandler

func (proc *Processor) AddHandler(handler CommandHandler)

func (*Processor) AddHandlers

func (proc *Processor) AddHandlers(handlers ...CommandHandler)

func (*Processor) Handle

func (proc *Processor) Handle(ctx context.Context, roomID id.RoomID, eventID id.EventID, user *bridgev2.User, message string, replyTo id.EventID)

Handle handles messages to the bridge

Jump to

Keyboard shortcuts

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