Add very basic arbitrary message/state event sending commands

This commit is contained in:
Tulir Asokan 2018-05-27 15:30:52 +03:00
parent 094a566189
commit e604346211
2 changed files with 75 additions and 12 deletions

View File

@ -36,10 +36,11 @@ type Command struct {
gomuksPointerContainer
Handler *CommandProcessor
Room *RoomView
Command string
Args []string
OrigText string
Room *RoomView
Command string
OrigCommand string
Args []string
OrigText string
}
func (cmd *Command) Reply(message string, args ...interface{}) {
@ -74,7 +75,9 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor {
Gomuks: parent.gmx,
},
aliases: map[string]*Alias{
"part": {"leave"},
"part": {"leave"},
"send": {"sendevent"},
"state": {"setstate"},
},
commands: map[string]CommandHandler{
"unknown-command": cmdUnknownCommand,
@ -86,6 +89,8 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor {
"join": cmdJoin,
"uitoggle": cmdUIToggle,
"logout": cmdLogout,
"sendevent": cmdSendEvent,
"setstate": cmdSetState,
},
}
}
@ -95,17 +100,16 @@ func (ch *CommandProcessor) ParseCommand(roomView *RoomView, text string) *Comma
return nil
}
text = text[1:]
args := strings.SplitN(text, " ", 2)
command := strings.ToLower(args[0])
args = args[1:]
split := strings.SplitN(text, " ", -1)
return &Command{
gomuksPointerContainer: ch.gomuksPointerContainer,
Handler: ch,
Room: roomView,
Command: command,
Args: args,
OrigText: text,
Room: roomView,
Command: strings.ToLower(split[0]),
OrigCommand: split[0],
Args: split[1:],
OrigText: text,
}
}

View File

@ -19,6 +19,7 @@ package ui
import (
"maunium.net/go/gomuks/debug"
"strings"
"encoding/json"
)
func cmdMe(cmd *Command) {
@ -70,6 +71,64 @@ func cmdJoin(cmd *Command) {
}
}
func cmdSendEvent(cmd *Command) {
debug.Print(cmd.Command, cmd.Args, len(cmd.Args))
if len(cmd.Args) < 3 {
cmd.Reply("Usage: /send <room id> <event type> <content>")
return
}
roomID := cmd.Args[0]
eventType := cmd.Args[1]
rawContent := strings.Join(cmd.Args[2:], "")
debug.Print(roomID, eventType, rawContent)
var content interface{}
err := json.Unmarshal([]byte(rawContent), &content)
debug.Print(err)
if err != nil {
cmd.Reply("Failed to parse content: %v", err)
return
}
debug.Print("Sending event to", roomID, eventType, content)
resp, err := cmd.Matrix.Client().SendMessageEvent(roomID, eventType, content)
debug.Print(resp, err)
if err != nil {
cmd.Reply("Error from server: %v", err)
} else {
cmd.Reply("Event sent, ID: %s", resp.EventID)
}
}
func cmdSetState(cmd *Command) {
if len(cmd.Args) < 4 {
cmd.Reply("Usage: /setstate <room id> <event type> <state key/`-`> <content>")
return
}
roomID := cmd.Args[0]
eventType := cmd.Args[1]
stateKey := cmd.Args[2]
if stateKey == "-" {
stateKey = ""
}
rawContent := strings.Join(cmd.Args[3:], "")
var content interface{}
err := json.Unmarshal([]byte(rawContent), &content)
if err != nil {
cmd.Reply("Failed to parse content: %v", err)
return
}
debug.Print("Sending state event to", roomID, eventType, stateKey, content)
resp, err := cmd.Matrix.Client().SendStateEvent(roomID, eventType, stateKey, content)
if err != nil {
cmd.Reply("Error from server: %v", err)
} else {
cmd.Reply("State event sent, ID: %s", resp.EventID)
}
}
func cmdUIToggle(cmd *Command) {
if len(cmd.Args) == 0 {
cmd.Reply("Usage: /uitoggle <rooms/users/baremessages>")