gomuks/ui/commands.go

406 lines
11 KiB
Go
Raw Normal View History

2018-05-27 13:54:07 +02:00
// gomuks - A terminal Matrix client written in Go.
2019-01-17 13:13:25 +01:00
// Copyright (C) 2019 Tulir Asokan
2018-05-27 13:54:07 +02:00
//
// This program is free software: you can redistribute it and/or modify
2019-01-17 13:13:25 +01:00
// it under the terms of the GNU Affero General Public License as published by
2018-05-27 13:54:07 +02:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2019-01-17 13:13:25 +01:00
// GNU Affero General Public License for more details.
2018-05-27 13:54:07 +02:00
//
2019-01-17 13:13:25 +01:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-05-27 13:54:07 +02:00
package ui
import (
2018-06-01 23:44:21 +02:00
"encoding/json"
"fmt"
2019-06-15 18:10:18 +02:00
"io"
2019-04-07 17:21:38 +02:00
"os"
"runtime"
2019-06-15 16:04:08 +02:00
dbg "runtime/debug"
2019-04-07 17:21:38 +02:00
"runtime/pprof"
2019-06-15 18:10:18 +02:00
"runtime/trace"
2019-06-15 16:04:08 +02:00
"strconv"
"strings"
2019-06-15 16:04:08 +02:00
"time"
"unicode"
2018-07-02 09:00:42 +02:00
"github.com/lucasb-eyer/go-colorful"
2019-01-17 13:13:25 +01:00
2019-04-10 00:04:39 +02:00
"maunium.net/go/mautrix"
2019-06-14 20:01:24 +02:00
"maunium.net/go/gomuks/debug"
2018-05-27 13:54:07 +02:00
)
func cmdMe(cmd *Command) {
text := strings.Join(cmd.Args, " ")
2019-04-10 00:04:39 +02:00
go cmd.Room.SendMessage(mautrix.MsgEmote, text)
2018-05-27 13:54:07 +02:00
cmd.UI.Render()
}
// GradientTable from https://github.com/lucasb-eyer/go-colorful/blob/master/doc/gradientgen/gradientgen.go
type GradientTable []struct {
Col colorful.Color
Pos float64
}
func (gt GradientTable) GetInterpolatedColorFor(t float64) colorful.Color {
for i := 0; i < len(gt)-1; i++ {
c1 := gt[i]
c2 := gt[i+1]
if c1.Pos <= t && t <= c2.Pos {
t := (t - c1.Pos) / (c2.Pos - c1.Pos)
return c1.Col.BlendHcl(c2.Col, t).Clamped()
}
}
return gt[len(gt)-1].Col
}
var rainbow = GradientTable{
{colorful.LinearRgb(1, 0, 0), 0.0},
{colorful.LinearRgb(1, 0.5, 0), 0.1},
{colorful.LinearRgb(0.5, 0.5, 0), 0.2}, // Yellow is 0.5, 0.5 instead of 1, 1 to make it readable on light themes
{colorful.LinearRgb(0.5, 1, 0), 0.3},
{colorful.LinearRgb(0, 1, 0), 0.4},
{colorful.LinearRgb(0, 1, 0.5), 0.5},
{colorful.LinearRgb(0, 1, 1), 0.6},
{colorful.LinearRgb(0, 0.5, 1), 0.7},
{colorful.LinearRgb(0.5, 0, 1), 0.8},
{colorful.LinearRgb(1, 0, 1), 0.9},
{colorful.LinearRgb(1, 0, 0.5), 1},
}
2019-04-07 17:21:38 +02:00
func cmdHeapProfile(cmd *Command) {
runtime.GC()
2019-06-16 13:29:03 +02:00
dbg.FreeOSMemory()
2019-06-15 16:04:08 +02:00
memProfile, err := os.Create("gomuks.heap.prof")
2019-04-07 17:21:38 +02:00
if err != nil {
2019-06-15 16:04:08 +02:00
debug.Print("Failed to open gomuks.heap.prof:", err)
return
2019-04-07 17:21:38 +02:00
}
2019-06-15 16:04:08 +02:00
defer func() {
err := memProfile.Close()
if err != nil {
debug.Print("Failed to close gomuks.heap.prof:", err)
}
}()
2019-04-07 17:21:38 +02:00
if err := pprof.WriteHeapProfile(memProfile); err != nil {
2019-06-15 16:04:08 +02:00
debug.Print("Heap profile error:", err)
}
}
2019-06-15 18:10:18 +02:00
func runTimedProfile(cmd *Command, start func(writer io.Writer) error, stop func(), task, file string) {
2019-06-15 16:04:08 +02:00
if len(cmd.Args) == 0 {
2019-06-15 18:10:18 +02:00
cmd.Reply("Usage: /%s <seconds>", cmd.Command)
} else if dur, err := strconv.Atoi(cmd.Args[0]); err != nil || dur < 0 {
cmd.Reply("Usage: /%s <seconds>", cmd.Command)
} else if cpuProfile, err := os.Create(file); err != nil {
debug.Printf("Failed to open %s: %v", file, err)
} else if err = start(cpuProfile); err != nil {
2019-06-15 16:04:08 +02:00
_ = cpuProfile.Close()
2019-06-15 18:10:18 +02:00
debug.Print(task, "error:", err)
} else {
cmd.Reply("Started %s for %d seconds", task, dur)
go func() {
time.Sleep(time.Duration(dur) * time.Second)
stop()
cmd.Reply("%s finished.", task)
err := cpuProfile.Close()
if err != nil {
debug.Print("Failed to close gomuks.cpu.prof:", err)
}
}()
2019-06-15 16:04:08 +02:00
}
2019-06-15 18:10:18 +02:00
}
2019-06-15 16:04:08 +02:00
2019-06-15 18:10:18 +02:00
func cmdCPUProfile(cmd *Command) {
runTimedProfile(cmd, pprof.StartCPUProfile, pprof.StopCPUProfile, "CPU profiling", "gomuks.cpu.prof")
}
func cmdTrace(cmd *Command) {
runTimedProfile(cmd, trace.Start, trace.Stop, "Call tracing", "gomuks.trace")
2019-04-07 17:21:38 +02:00
}
// TODO this command definitely belongs in a plugin once we have a plugin system.
func cmdRainbow(cmd *Command) {
text := strings.Join(cmd.Args, " ")
var html strings.Builder
2019-06-15 00:11:51 +02:00
_, _ = fmt.Fprint(&html, "**🌈** ")
for i, char := range text {
if unicode.IsSpace(char) {
html.WriteRune(char)
continue
}
color := rainbow.GetInterpolatedColorFor(float64(i) / float64(len(text))).Hex()
2019-06-15 00:11:51 +02:00
_, _ = fmt.Fprintf(&html, "<font color=\"%s\">%c</font>", color, char)
}
2019-04-10 00:04:39 +02:00
go cmd.Room.SendMessage("m.text", html.String())
cmd.UI.Render()
}
2018-05-27 13:54:07 +02:00
func cmdQuit(cmd *Command) {
2019-06-15 00:11:51 +02:00
cmd.Gomuks.Stop(true)
2018-05-27 13:54:07 +02:00
}
func cmdClearCache(cmd *Command) {
cmd.Config.Clear()
2019-06-15 00:11:51 +02:00
cmd.Gomuks.Stop(false)
2018-05-27 13:54:07 +02:00
}
func cmdUnknownCommand(cmd *Command) {
cmd.Reply("Unknown command \"%s\". Try \"/help\" for help.", cmd.Command)
}
func cmdHelp(cmd *Command) {
2019-03-30 17:51:26 +01:00
cmd.Reply(`/help - Show the temporary help message.
/quit - Quit gomuks.
/clearcache - Clear cache and quit gomuks.
/logout - Log out of Matrix.
/me <message> - Send an emote message.
/rainbow <message> - Send a rainbow message (markdown not supported).
/create [room name] - Create a room.
2019-06-14 20:01:24 +02:00
/pm <user id> <...> - Create a private chat with the given user(s).
2019-03-30 17:51:26 +01:00
/join <room address> - Join a room.
/leave - Leave the current room.
/invite <user id> - Invite a user.
/kick <user id> [reason] - Kick a user.
/ban <user id> [reason] - Ban a user.
/unban <user id> - Unban a user.
2019-03-30 17:51:26 +01:00
/send <room id> <type> <json> - Send a custom event to the given room.
/msend <type> <json> - Send a custom event to the current room.
/setstate <room id> <type> <key/-> <json> - Send a custom event to the given room.
/msetstate <type> <key/-> <json> - Send a custom event to the current room.
/toggle <thing> - Temporary command to toggle various UI features.`)
2018-05-27 13:54:07 +02:00
}
func cmdLeave(cmd *Command) {
err := cmd.Matrix.LeaveRoom(cmd.Room.MxRoom().ID)
debug.Print("Leave room error:", err)
if err == nil {
cmd.MainView.RemoveRoom(cmd.Room.MxRoom())
}
}
func cmdInvite(cmd *Command) {
if len(cmd.Args) != 1 {
cmd.Reply("Usage: /invite <user id>")
return
}
2019-04-10 01:47:47 +02:00
_, err := cmd.Matrix.Client().InviteUser(cmd.Room.MxRoom().ID, &mautrix.ReqInviteUser{UserID: cmd.Args[0]})
if err != nil {
debug.Print("Error in invite call:", err)
2019-04-10 01:47:47 +02:00
cmd.Reply("Failed to invite user: %v", err)
}
}
func cmdBan(cmd *Command) {
if len(cmd.Args) < 1 {
2019-03-30 17:51:26 +01:00
cmd.Reply("Usage: /ban <user> [reason]")
return
}
reason := "you are the weakest link, goodbye!"
if len(cmd.Args) >= 2 {
2019-03-30 17:51:26 +01:00
reason = strings.Join(cmd.Args[1:], " ")
}
2019-04-10 01:47:47 +02:00
_, err := cmd.Matrix.Client().BanUser(cmd.Room.MxRoom().ID, &mautrix.ReqBanUser{Reason: reason, UserID: cmd.Args[0]})
if err != nil {
debug.Print("Error in ban call:", err)
2019-04-10 01:47:47 +02:00
cmd.Reply("Failed to ban user: %v", err)
}
}
func cmdUnban(cmd *Command) {
if len(cmd.Args) != 1 {
cmd.Reply("Usage: /unban <user>")
return
}
2019-04-10 01:47:47 +02:00
_, err := cmd.Matrix.Client().UnbanUser(cmd.Room.MxRoom().ID, &mautrix.ReqUnbanUser{UserID: cmd.Args[0]})
if err != nil {
debug.Print("Error in unban call:", err)
2019-04-10 01:47:47 +02:00
cmd.Reply("Failed to unban user: %v", err)
}
}
func cmdKick(cmd *Command) {
if len(cmd.Args) < 1 {
2019-03-30 17:51:26 +01:00
cmd.Reply("Usage: /kick <user> [reason]")
return
}
reason := "you are the weakest link, goodbye!"
if len(cmd.Args) >= 2 {
2019-03-30 17:51:26 +01:00
reason = strings.Join(cmd.Args[1:], " ")
}
2019-04-10 01:47:47 +02:00
_, err := cmd.Matrix.Client().KickUser(cmd.Room.MxRoom().ID, &mautrix.ReqKickUser{Reason: reason, UserID: cmd.Args[0]})
if err != nil {
debug.Print("Error in kick call:", err)
debug.Print("Failed to kick user:", err)
}
}
func cmdCreateRoom(cmd *Command) {
req := &mautrix.ReqCreateRoom{}
if len(cmd.Args) > 0 {
req.Name = strings.Join(cmd.Args, " ")
}
room, err := cmd.Matrix.CreateRoom(req)
if err != nil {
cmd.Reply("Failed to create room:", err)
return
}
cmd.MainView.SwitchRoom("", room)
}
2019-06-14 20:01:24 +02:00
func cmdPrivateMessage(cmd *Command) {
if len(cmd.Args) == 0 {
cmd.Reply("Usage: /pm <user id> [more user ids...]")
}
req := &mautrix.ReqCreateRoom{
Preset: "trusted_private_chat",
Invite: cmd.Args,
}
room, err := cmd.Matrix.CreateRoom(req)
if err != nil {
cmd.Reply("Failed to create room:", err)
return
}
cmd.MainView.SwitchRoom("", room)
}
2018-05-27 13:54:07 +02:00
func cmdJoin(cmd *Command) {
if len(cmd.Args) == 0 {
cmd.Reply("Usage: /join <room>")
return
}
identifer := cmd.Args[0]
server := ""
if len(cmd.Args) > 1 {
server = cmd.Args[1]
}
room, err := cmd.Matrix.JoinRoom(identifer, server)
debug.Print("Join room error:", err)
if err == nil {
cmd.MainView.AddRoom(room)
}
}
2019-03-19 15:57:50 +01:00
func cmdMSendEvent(cmd *Command) {
if len(cmd.Args) < 2 {
cmd.Reply("Usage: /msend <event type> <content>")
return
}
2019-03-30 17:51:26 +01:00
cmd.Args = append([]string{cmd.Room.MxRoom().ID}, cmd.Args...)
2019-03-19 15:57:50 +01:00
cmdSendEvent(cmd)
}
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]
2018-11-13 23:00:35 +01:00
eventType := mautrix.NewEventType(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)
}
}
2019-03-19 15:58:57 +01:00
func cmdMSetState(cmd *Command) {
if len(cmd.Args) < 2 {
cmd.Reply("Usage: /msetstate <event type> <state key> <content>")
return
}
2019-03-30 17:51:26 +01:00
cmd.Args = append([]string{cmd.Room.MxRoom().ID}, cmd.Args...)
2019-03-19 15:58:57 +01:00
cmdSetState(cmd)
}
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]
2018-11-13 23:00:35 +01:00
eventType := mautrix.NewEventType(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 cmdToggle(cmd *Command) {
2018-05-27 13:54:07 +02:00
if len(cmd.Args) == 0 {
2018-07-02 09:00:42 +02:00
cmd.Reply("Usage: /toggle <rooms/users/baremessages/images/typingnotif/emojis>")
2018-05-27 13:54:07 +02:00
return
}
switch cmd.Args[0] {
case "rooms":
2018-06-01 23:43:56 +02:00
cmd.Config.Preferences.HideRoomList = !cmd.Config.Preferences.HideRoomList
2018-05-27 13:54:07 +02:00
case "users":
2018-06-01 23:43:56 +02:00
cmd.Config.Preferences.HideUserList = !cmd.Config.Preferences.HideUserList
2018-05-27 13:54:07 +02:00
case "baremessages":
2018-06-01 23:43:56 +02:00
cmd.Config.Preferences.BareMessageView = !cmd.Config.Preferences.BareMessageView
case "images":
cmd.Config.Preferences.DisableImages = !cmd.Config.Preferences.DisableImages
case "typingnotif":
cmd.Config.Preferences.DisableTypingNotifs = !cmd.Config.Preferences.DisableTypingNotifs
2018-07-02 09:00:42 +02:00
case "emojis":
cmd.Config.Preferences.DisableEmojis = !cmd.Config.Preferences.DisableEmojis
2018-05-27 13:54:07 +02:00
default:
2018-07-02 09:00:42 +02:00
cmd.Reply("Usage: /toggle <rooms/users/baremessages/images/typingnotif/emojis>")
2018-05-27 13:54:07 +02:00
return
}
// is there a reason this is called twice?
// cmd.UI.Render()
2018-05-27 13:54:07 +02:00
cmd.UI.Render()
go cmd.Matrix.SendPreferencesToMatrix()
}
func cmdLogout(cmd *Command) {
cmd.Matrix.Logout()
}