Add unfinished command handler system based on the improved bot support proposal

This commit is contained in:
Tulir Asokan 2018-06-19 01:25:47 +03:00
parent 946dac989a
commit 307a32e0c0
9 changed files with 355 additions and 16 deletions

View file

@ -28,7 +28,8 @@ type Event struct {
Client *Client
}
func roundtripContent(rawContent map[string]interface{}) (content maubot.Content) {
func roundtripContent(rawContent map[string]interface{}) (content *maubot.Content) {
content = &maubot.Content{}
if len(rawContent) == 0 {
content.Raw = rawContent
return
@ -55,7 +56,7 @@ func (client *Client) ParseEvent(mxEvent *gomatrix.Event) *Event {
Timestamp: mxEvent.Timestamp,
ID: mxEvent.ID,
RoomID: mxEvent.RoomID,
Content: roundtripContent(mxEvent.Content),
Content: *roundtripContent(mxEvent.Content),
Redacts: mxEvent.Redacts,
Unsigned: maubot.Unsigned{
PrevContent: roundtripContent(mxEvent.Unsigned.PrevContent),

View file

@ -45,10 +45,18 @@ func NewClient(db *database.MatrixClient) (*Client, error) {
client.Client.Syncer = client.syncer
client.AddEventHandler(maubot.StateMember, client.onJoin)
client.AddEventHandler(maubot.EventMessage, client.onMessage)
return client, nil
}
func (client *Client) Proxy(owner string) *ClientProxy {
return &ClientProxy{
hiddenClient: client,
owner: owner,
}
}
func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) {
client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult {
if evt.Sender == client.UserID {
@ -58,6 +66,18 @@ func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.Event
})
}
func (client *Client) AddCommandHandler(evt string, handler maubot.CommandHandler) {
// TODO add command handler
}
func (client *Client) SetCommandSpec(owner string, spec *maubot.CommandSpec) {
changed := client.DB.SetCommandSpec(owner, spec)
if changed {
log.Debugln("Command spec of", owner, "on", client.UserID, "updated.")
// TODO
}
}
func (client *Client) GetEvent(roomID, eventID string) *maubot.Event {
evt, err := client.Client.GetEvent(roomID, eventID)
if err != nil {
@ -67,6 +87,11 @@ func (client *Client) GetEvent(roomID, eventID string) *maubot.Event {
return client.ParseEvent(evt).Event
}
func (client *Client) onMessage(evt *maubot.Event) maubot.EventHandlerResult {
// TODO call command handlers
return maubot.Continue
}
func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult {
if client.DB.AutoJoinRooms && evt.StateKey == client.DB.UserID && evt.Content.Membership == "invite" {
client.JoinRoom(evt.RoomID)
@ -87,3 +112,14 @@ func (client *Client) Sync() {
}
}()
}
type hiddenClient = Client
type ClientProxy struct {
*hiddenClient
owner string
}
func (cp *ClientProxy) SetCommandSpec(spec *maubot.CommandSpec) {
cp.hiddenClient.SetCommandSpec(cp.owner, spec)
}