2018-05-27 13:54:07 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 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"
|
2020-10-23 11:02:42 +02:00
|
|
|
"errors"
|
2018-06-11 18:38:19 +02:00
|
|
|
"fmt"
|
2019-06-15 18:10:18 +02:00
|
|
|
"io"
|
2019-06-16 18:32:57 +02:00
|
|
|
"math"
|
2019-04-07 17:21:38 +02:00
|
|
|
"os"
|
2020-09-01 15:33:39 +02:00
|
|
|
"path/filepath"
|
2020-03-20 13:32:29 +01:00
|
|
|
"regexp"
|
2019-04-07 17:21:38 +02:00
|
|
|
"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"
|
2018-06-30 22:26:40 +02:00
|
|
|
"strings"
|
2019-06-15 16:04:08 +02:00
|
|
|
"time"
|
2020-04-16 19:05:03 +02:00
|
|
|
"unicode"
|
2018-07-02 09:00:42 +02:00
|
|
|
|
|
|
|
"github.com/lucasb-eyer/go-colorful"
|
2020-03-20 13:32:29 +01:00
|
|
|
"github.com/russross/blackfriday/v2"
|
2019-01-17 13:13:25 +01:00
|
|
|
|
2019-04-10 00:04:39 +02:00
|
|
|
"maunium.net/go/mautrix"
|
2020-04-16 18:27:35 +02:00
|
|
|
"maunium.net/go/mautrix/event"
|
2020-03-20 13:32:29 +01:00
|
|
|
"maunium.net/go/mautrix/format"
|
2020-04-16 18:27:35 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
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, " ")
|
2020-04-16 18:27:35 +02:00
|
|
|
go cmd.Room.SendMessage(event.MsgEmote, text)
|
2018-05-27 13:54:07 +02:00
|
|
|
}
|
|
|
|
|
2018-06-11 18:38:19 +02:00
|
|
|
// 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{
|
2019-06-16 15:32:56 +02:00
|
|
|
{colorful.LinearRgb(1, 0, 0), 0 / 11.0},
|
|
|
|
{colorful.LinearRgb(1, 0.5, 0), 1 / 11.0},
|
|
|
|
{colorful.LinearRgb(1, 1, 0), 2 / 11.0},
|
|
|
|
{colorful.LinearRgb(0.5, 1, 0), 3 / 11.0},
|
|
|
|
{colorful.LinearRgb(0, 1, 0), 4 / 11.0},
|
|
|
|
{colorful.LinearRgb(0, 1, 0.5), 5 / 11.0},
|
|
|
|
{colorful.LinearRgb(0, 1, 1), 6 / 11.0},
|
|
|
|
{colorful.LinearRgb(0, 0.5, 1), 7 / 11.0},
|
|
|
|
{colorful.LinearRgb(0, 0, 1), 8 / 11.0},
|
|
|
|
{colorful.LinearRgb(0.5, 0, 1), 9 / 11.0},
|
|
|
|
{colorful.LinearRgb(1, 0, 1), 10 / 11.0},
|
|
|
|
{colorful.LinearRgb(1, 0, 0.5), 11 / 11.0},
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO this command definitely belongs in a plugin once we have a plugin system.
|
2020-04-16 18:27:35 +02:00
|
|
|
func makeRainbow(cmd *Command, msgtype event.MessageType) {
|
2019-06-16 15:32:56 +02:00
|
|
|
text := strings.Join(cmd.Args, " ")
|
2020-03-20 13:32:29 +01:00
|
|
|
|
|
|
|
render := NewRainbowRenderer(blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
|
|
|
|
Flags: blackfriday.UseXHTML,
|
|
|
|
}))
|
|
|
|
htmlBodyBytes := blackfriday.Run([]byte(text), format.Extensions, blackfriday.WithRenderer(render))
|
|
|
|
htmlBody := strings.TrimRight(string(htmlBodyBytes), "\n")
|
|
|
|
htmlBody = format.AntiParagraphRegex.ReplaceAllString(htmlBody, "$1")
|
|
|
|
text = format.HTMLToText(htmlBody)
|
|
|
|
|
|
|
|
count := strings.Count(htmlBody, render.ColorID)
|
|
|
|
i := -1
|
|
|
|
htmlBody = regexp.MustCompile(render.ColorID).ReplaceAllStringFunc(htmlBody, func(match string) string {
|
|
|
|
i++
|
|
|
|
return rainbow.GetInterpolatedColorFor(float64(i) / float64(count)).Hex()
|
|
|
|
})
|
|
|
|
|
|
|
|
go cmd.Room.SendMessageHTML(msgtype, text, htmlBody)
|
2019-06-16 15:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRainbow(cmd *Command) {
|
2020-04-16 18:27:35 +02:00
|
|
|
makeRainbow(cmd, event.MsgText)
|
2019-06-16 15:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRainbowMe(cmd *Command) {
|
2020-04-16 18:27:35 +02:00
|
|
|
makeRainbow(cmd, event.MsgEmote)
|
2019-06-16 15:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdNotice(cmd *Command) {
|
2020-04-16 18:27:35 +02:00
|
|
|
go cmd.Room.SendMessage(event.MsgNotice, strings.Join(cmd.Args, " "))
|
2019-06-16 15:32:56 +02:00
|
|
|
}
|
|
|
|
|
2020-02-22 00:17:52 +01:00
|
|
|
func cmdAccept(cmd *Command) {
|
|
|
|
room := cmd.Room.MxRoom()
|
|
|
|
if room.SessionMember.Membership != "invite" {
|
|
|
|
cmd.Reply("/accept can only be used in rooms you're invited to")
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
_, server, _ := room.SessionMember.Sender.Parse()
|
2020-02-22 00:17:52 +01:00
|
|
|
_, err := cmd.Matrix.JoinRoom(room.ID, server)
|
|
|
|
if err != nil {
|
2020-06-17 13:15:22 +02:00
|
|
|
cmd.Reply("Failed to accept invite: %v", err)
|
2020-02-22 00:17:52 +01:00
|
|
|
} else {
|
|
|
|
cmd.Reply("Successfully accepted invite")
|
|
|
|
}
|
2020-02-22 00:30:43 +01:00
|
|
|
cmd.MainView.UpdateTags(room)
|
|
|
|
go cmd.MainView.LoadHistory(room.ID)
|
2020-02-22 00:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdReject(cmd *Command) {
|
|
|
|
room := cmd.Room.MxRoom()
|
|
|
|
if room.SessionMember.Membership != "invite" {
|
|
|
|
cmd.Reply("/reject can only be used in rooms you're invited to")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := cmd.Matrix.LeaveRoom(room.ID)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Reply("Failed to reject invite: %v", err)
|
|
|
|
} else {
|
2020-06-07 23:19:50 +02:00
|
|
|
cmd.Reply("Successfully rejected invite")
|
2020-02-22 00:17:52 +01:00
|
|
|
}
|
2020-06-07 23:19:50 +02:00
|
|
|
cmd.MainView.RemoveRoom(room)
|
2020-02-22 00:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdID(cmd *Command) {
|
|
|
|
cmd.Reply("The internal ID of this room is %s", cmd.Room.MxRoom().ID)
|
|
|
|
}
|
|
|
|
|
2020-02-29 23:33:37 +01:00
|
|
|
type SelectReason string
|
|
|
|
|
|
|
|
const (
|
2020-04-08 14:30:29 +02:00
|
|
|
SelectReply SelectReason = "reply to"
|
|
|
|
SelectReact = "react to"
|
|
|
|
SelectRedact = "redact"
|
2020-08-31 00:23:14 +02:00
|
|
|
SelectEdit = "edit"
|
2020-04-08 14:30:29 +02:00
|
|
|
SelectDownload = "download"
|
|
|
|
SelectOpen = "open"
|
2020-04-26 19:02:35 +02:00
|
|
|
SelectCopy = "copy"
|
2020-02-29 23:33:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func cmdReply(cmd *Command) {
|
2020-03-01 16:11:56 +01:00
|
|
|
cmd.Room.StartSelecting(SelectReply, strings.Join(cmd.Args, " "))
|
2020-02-29 23:33:37 +01:00
|
|
|
}
|
|
|
|
|
2020-08-31 00:23:14 +02:00
|
|
|
func cmdEdit(cmd *Command) {
|
|
|
|
cmd.Room.StartSelecting(SelectEdit, "")
|
|
|
|
}
|
|
|
|
|
2020-02-29 23:33:37 +01:00
|
|
|
func cmdRedact(cmd *Command) {
|
2020-03-01 21:35:21 +01:00
|
|
|
cmd.Room.StartSelecting(SelectRedact, strings.Join(cmd.Args, " "))
|
2020-02-29 23:33:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 14:30:29 +02:00
|
|
|
func cmdDownload(cmd *Command) {
|
|
|
|
cmd.Room.StartSelecting(SelectDownload, strings.Join(cmd.Args, " "))
|
|
|
|
}
|
|
|
|
|
2020-09-01 15:33:39 +02:00
|
|
|
func cmdUpload(cmd *Command) {
|
|
|
|
if len(cmd.Args) == 0 {
|
|
|
|
cmd.Reply("Usage: /upload <file>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := filepath.Abs(cmd.RawArgs)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Reply("Failed to get absolute path: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-02 01:12:09 +02:00
|
|
|
go cmd.Room.SendMessageMedia(path)
|
2020-09-01 15:33:39 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 14:30:29 +02:00
|
|
|
func cmdOpen(cmd *Command) {
|
|
|
|
cmd.Room.StartSelecting(SelectOpen, strings.Join(cmd.Args, " "))
|
|
|
|
}
|
|
|
|
|
2020-04-26 19:02:35 +02:00
|
|
|
func cmdCopy(cmd *Command) {
|
2020-04-26 21:19:41 +02:00
|
|
|
register := strings.Join(cmd.Args, " ")
|
|
|
|
if len(register) == 0 {
|
|
|
|
register = "clipboard"
|
|
|
|
}
|
2020-06-17 13:15:22 +02:00
|
|
|
if register == "clipboard" || register == "primary" {
|
2020-04-26 21:19:41 +02:00
|
|
|
cmd.Room.StartSelecting(SelectCopy, register)
|
2020-04-26 19:02:35 +02:00
|
|
|
} else {
|
2020-04-26 21:19:41 +02:00
|
|
|
cmd.Reply("Usage: /copy [register], where register is either \"clipboard\" or \"primary\". Defaults to \"clipboard\".")
|
2020-04-26 19:02:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 23:33:37 +01:00
|
|
|
func cmdReact(cmd *Command) {
|
|
|
|
if len(cmd.Args) == 0 {
|
|
|
|
cmd.Reply("Usage: /react <reaction>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-01 16:11:56 +01:00
|
|
|
cmd.Room.StartSelecting(SelectReact, strings.Join(cmd.Args, " "))
|
2020-02-29 23:33:37 +01:00
|
|
|
}
|
|
|
|
|
2020-06-17 13:15:22 +02:00
|
|
|
func readRoomAlias(cmd *Command) (alias id.RoomAlias, err error) {
|
|
|
|
param := strings.Join(cmd.Args[1:], " ")
|
|
|
|
if strings.ContainsRune(param, ':') {
|
|
|
|
if param[0] != '#' {
|
2020-10-23 11:02:42 +02:00
|
|
|
return "", errors.New("full aliases must start with #")
|
2020-06-17 13:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
alias = id.RoomAlias(param)
|
|
|
|
} else {
|
|
|
|
_, homeserver, _ := cmd.Matrix.Client().UserID.Parse()
|
|
|
|
alias = id.NewRoomAlias(param, homeserver)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdAlias(cmd *Command) {
|
|
|
|
if len(cmd.Args) < 2 {
|
|
|
|
cmd.Reply("Usage: /alias <add|remove> <localpart>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
alias, err := readRoomAlias(cmd)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Reply(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
subcmd := strings.ToLower(cmd.Args[0])
|
|
|
|
switch subcmd {
|
|
|
|
case "add", "create":
|
|
|
|
cmdAddAlias(cmd, alias)
|
|
|
|
case "remove", "delete", "del", "rm":
|
|
|
|
cmdRemoveAlias(cmd, alias)
|
|
|
|
case "resolve", "get":
|
|
|
|
cmdResolveAlias(cmd, alias)
|
|
|
|
default:
|
|
|
|
cmd.Reply("Usage: /alias <add|remove|resolve> <localpart>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func niceError(err error) string {
|
|
|
|
httpErr, ok := err.(mautrix.HTTPError)
|
|
|
|
if ok && httpErr.RespError != nil {
|
|
|
|
return httpErr.RespError.Error()
|
|
|
|
}
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdAddAlias(cmd *Command, alias id.RoomAlias) {
|
|
|
|
_, err := cmd.Matrix.Client().CreateAlias(alias, cmd.Room.MxRoom().ID)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Reply("Failed to create alias: %v", niceError(err))
|
|
|
|
} else {
|
|
|
|
cmd.Reply("Created alias %s", alias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRemoveAlias(cmd *Command, alias id.RoomAlias) {
|
|
|
|
_, err := cmd.Matrix.Client().DeleteAlias(alias)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Reply("Failed to delete alias: %v", niceError(err))
|
|
|
|
} else {
|
|
|
|
cmd.Reply("Deleted alias %s", alias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdResolveAlias(cmd *Command, alias id.RoomAlias) {
|
|
|
|
resp, err := cmd.Matrix.Client().ResolveAlias(alias)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Reply("Failed to resolve alias: %v", niceError(err))
|
|
|
|
} else {
|
|
|
|
roomIDText := string(resp.RoomID)
|
|
|
|
if resp.RoomID == cmd.Room.MxRoom().ID {
|
|
|
|
roomIDText += " (this room)"
|
|
|
|
}
|
|
|
|
cmd.Reply("Alias %s points to room %s\nThere are %d servers in the room.", alias, roomIDText, len(resp.Servers))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 18:32:57 +02:00
|
|
|
func cmdTags(cmd *Command) {
|
|
|
|
tags := cmd.Room.MxRoom().RawTags
|
2020-02-22 00:17:52 +01:00
|
|
|
if len(cmd.Args) > 0 && cmd.Args[0] == "--internal" {
|
|
|
|
tags = cmd.Room.MxRoom().Tags()
|
|
|
|
}
|
2019-06-16 18:32:57 +02:00
|
|
|
if len(tags) == 0 {
|
|
|
|
if cmd.Room.MxRoom().IsDirect {
|
|
|
|
cmd.Reply("This room has no tags, but it's marked as a direct chat.")
|
|
|
|
} else {
|
|
|
|
cmd.Reply("This room has no tags.")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var resp strings.Builder
|
|
|
|
resp.WriteString("Tags in this room:\n")
|
|
|
|
for _, tag := range tags {
|
|
|
|
if tag.Order != "" {
|
|
|
|
_, _ = fmt.Fprintf(&resp, "%s (order: %s)\n", tag.Tag, tag.Order)
|
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprintf(&resp, "%s (no order)\n", tag.Tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd.Reply(strings.TrimSpace(resp.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdTag(cmd *Command) {
|
|
|
|
if len(cmd.Args) == 0 {
|
|
|
|
cmd.Reply("Usage: /tag <tag> [order]")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
order := math.NaN()
|
|
|
|
if len(cmd.Args) > 1 {
|
|
|
|
var err error
|
|
|
|
order, err = strconv.ParseFloat(cmd.Args[1], 64)
|
|
|
|
if err != nil {
|
|
|
|
cmd.Reply("%s is not a valid order: %v", cmd.Args[1], err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 00:17:52 +01:00
|
|
|
var err error
|
|
|
|
if len(cmd.Args) > 2 && cmd.Args[2] == "--reset" {
|
2020-04-16 18:27:35 +02:00
|
|
|
tags := event.Tags{
|
2020-02-22 00:17:52 +01:00
|
|
|
cmd.Args[0]: {Order: json.Number(fmt.Sprintf("%f", order))},
|
|
|
|
}
|
|
|
|
for _, tag := range cmd.Room.MxRoom().RawTags {
|
2020-04-16 18:27:35 +02:00
|
|
|
tags[tag.Tag] = event.Tag{Order: tag.Order}
|
2020-02-22 00:17:52 +01:00
|
|
|
}
|
|
|
|
err = cmd.Matrix.Client().SetTags(cmd.Room.MxRoom().ID, tags)
|
|
|
|
} else {
|
|
|
|
err = cmd.Matrix.Client().AddTag(cmd.Room.MxRoom().ID, cmd.Args[0], order)
|
|
|
|
}
|
2019-06-16 18:32:57 +02:00
|
|
|
if err != nil {
|
2020-05-07 10:56:21 +02:00
|
|
|
cmd.Reply("Failed to add tag: %v", err)
|
2019-06-16 18:32:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdUntag(cmd *Command) {
|
|
|
|
if len(cmd.Args) == 0 {
|
|
|
|
cmd.Reply("Usage: /untag <tag>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := cmd.Matrix.Client().RemoveTag(cmd.Room.MxRoom().ID, cmd.Args[0])
|
|
|
|
if err != nil {
|
2020-05-07 10:56:21 +02:00
|
|
|
cmd.Reply("Failed to remove tag: %v", err)
|
2019-06-16 18:32:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 15:32:56 +02:00
|
|
|
func cmdRoomNick(cmd *Command) {
|
|
|
|
room := cmd.Room.MxRoom()
|
|
|
|
member := room.GetMember(room.SessionUserID)
|
|
|
|
member.Displayname = strings.Join(cmd.Args, " ")
|
2020-04-16 18:27:35 +02:00
|
|
|
_, err := cmd.Matrix.Client().SendStateEvent(room.ID, event.StateMember, string(room.SessionUserID), member)
|
2019-06-16 15:32:56 +02:00
|
|
|
if err != nil {
|
2020-05-07 10:56:21 +02:00
|
|
|
cmd.Reply("Failed to set room nick: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdFingerprint(cmd *Command) {
|
|
|
|
c := cmd.Matrix.Crypto()
|
|
|
|
if c == nil {
|
|
|
|
cmd.Reply("Encryption support is not enabled")
|
|
|
|
} else {
|
|
|
|
cmd.Reply("Device ID: %s\nFingerprint: %s", cmd.Matrix.Client().DeviceID, c.Fingerprint())
|
2019-06-16 15:32:56 +02:00
|
|
|
}
|
2018-06-11 18:38:19 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 17:21:38 +02:00
|
|
|
func cmdHeapProfile(cmd *Command) {
|
2020-05-06 22:30:16 +02:00
|
|
|
if len(cmd.Args) == 0 || cmd.Args[0] != "nogc" {
|
|
|
|
runtime.GC()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2020-09-08 02:49:25 +02:00
|
|
|
view := cmd.MainView
|
|
|
|
view.ShowModal(NewHelpModal(view))
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 16:06:40 +01:00
|
|
|
func cmdInvite(cmd *Command) {
|
|
|
|
if len(cmd.Args) != 1 {
|
|
|
|
cmd.Reply("Usage: /invite <user id>")
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
_, err := cmd.Matrix.Client().InviteUser(cmd.Room.MxRoom().ID, &mautrix.ReqInviteUser{UserID: id.UserID(cmd.Args[0])})
|
2019-03-19 16:06:40 +01:00
|
|
|
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)
|
2019-03-19 16:06:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdBan(cmd *Command) {
|
|
|
|
if len(cmd.Args) < 1 {
|
2019-03-30 17:51:26 +01:00
|
|
|
cmd.Reply("Usage: /ban <user> [reason]")
|
2019-03-19 16:06:40 +01:00
|
|
|
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-03-19 16:06:40 +01:00
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
_, err := cmd.Matrix.Client().BanUser(cmd.Room.MxRoom().ID, &mautrix.ReqBanUser{Reason: reason, UserID: id.UserID(cmd.Args[0])})
|
2019-03-19 16:06:40 +01:00
|
|
|
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)
|
2019-03-19 16:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdUnban(cmd *Command) {
|
|
|
|
if len(cmd.Args) != 1 {
|
|
|
|
cmd.Reply("Usage: /unban <user>")
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
_, err := cmd.Matrix.Client().UnbanUser(cmd.Room.MxRoom().ID, &mautrix.ReqUnbanUser{UserID: id.UserID(cmd.Args[0])})
|
2019-03-19 16:06:40 +01:00
|
|
|
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)
|
2019-03-19 16:06:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdKick(cmd *Command) {
|
|
|
|
if len(cmd.Args) < 1 {
|
2019-03-30 17:51:26 +01:00
|
|
|
cmd.Reply("Usage: /kick <user> [reason]")
|
2019-03-19 16:06:40 +01:00
|
|
|
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-03-19 16:06:40 +01:00
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
_, err := cmd.Matrix.Client().KickUser(cmd.Room.MxRoom().ID, &mautrix.ReqKickUser{Reason: reason, UserID: id.UserID(cmd.Args[0])})
|
2019-03-19 16:06:40 +01:00
|
|
|
if err != nil {
|
|
|
|
debug.Print("Error in kick call:", err)
|
|
|
|
debug.Print("Failed to kick user:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 03:14:32 +02:00
|
|
|
func cmdCreateRoom(cmd *Command) {
|
2019-06-14 19:56:41 +02:00
|
|
|
req := &mautrix.ReqCreateRoom{}
|
|
|
|
if len(cmd.Args) > 0 {
|
|
|
|
req.Name = strings.Join(cmd.Args, " ")
|
2019-06-14 03:14:32 +02:00
|
|
|
}
|
2019-06-14 19:56:41 +02:00
|
|
|
room, err := cmd.Matrix.CreateRoom(req)
|
|
|
|
if err != nil {
|
2020-05-07 10:56:21 +02:00
|
|
|
cmd.Reply("Failed to create room: %v", err)
|
2019-06-14 19:56:41 +02:00
|
|
|
return
|
2019-06-14 03:14:32 +02:00
|
|
|
}
|
2019-06-14 19:56:41 +02:00
|
|
|
cmd.MainView.SwitchRoom("", room)
|
2019-06-14 03:14:32 +02:00
|
|
|
}
|
|
|
|
|
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...]")
|
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
invites := make([]id.UserID, len(cmd.Args))
|
|
|
|
for i, userID := range cmd.Args {
|
|
|
|
invites[i] = id.UserID(userID)
|
|
|
|
_, _, err := invites[i].Parse()
|
|
|
|
if err != nil {
|
|
|
|
cmd.Reply("%s isn't a valid user ID", userID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 20:01:24 +02:00
|
|
|
req := &mautrix.ReqCreateRoom{
|
2021-03-11 21:42:46 +01:00
|
|
|
Preset: "trusted_private_chat",
|
|
|
|
Invite: invites,
|
2021-01-23 20:05:21 +01:00
|
|
|
IsDirect: true,
|
2019-06-14 20:01:24 +02:00
|
|
|
}
|
|
|
|
room, err := cmd.Matrix.CreateRoom(req)
|
|
|
|
if err != nil {
|
2020-05-07 10:56:21 +02:00
|
|
|
cmd.Reply("Failed to create room: %v", err)
|
2019-06-14 20:01:24 +02:00
|
|
|
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
|
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
identifer := id.RoomID(cmd.Args[0])
|
2018-05-27 13:54:07 +02:00
|
|
|
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
|
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
cmd.Args = append([]string{string(cmd.Room.MxRoom().ID)}, cmd.Args...)
|
2019-03-19 15:57:50 +01:00
|
|
|
cmdSendEvent(cmd)
|
|
|
|
}
|
|
|
|
|
2018-05-27 14:30:52 +02:00
|
|
|
func cmdSendEvent(cmd *Command) {
|
|
|
|
if len(cmd.Args) < 3 {
|
|
|
|
cmd.Reply("Usage: /send <room id> <event type> <content>")
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
roomID := id.RoomID(cmd.Args[0])
|
|
|
|
eventType := event.NewEventType(cmd.Args[1])
|
2019-03-19 16:06:18 +01:00
|
|
|
rawContent := strings.Join(cmd.Args[2:], " ")
|
2018-05-27 14:30:52 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-04-16 18:27:35 +02:00
|
|
|
cmd.Args = append([]string{string(cmd.Room.MxRoom().ID)}, cmd.Args...)
|
2019-03-19 15:58:57 +01:00
|
|
|
cmdSetState(cmd)
|
|
|
|
}
|
|
|
|
|
2018-05-27 14:30:52 +02:00
|
|
|
func cmdSetState(cmd *Command) {
|
|
|
|
if len(cmd.Args) < 4 {
|
|
|
|
cmd.Reply("Usage: /setstate <room id> <event type> <state key/`-`> <content>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
roomID := id.RoomID(cmd.Args[0])
|
|
|
|
eventType := event.NewEventType(cmd.Args[1])
|
2018-05-27 14:30:52 +02:00
|
|
|
stateKey := cmd.Args[2]
|
|
|
|
if stateKey == "-" {
|
|
|
|
stateKey = ""
|
|
|
|
}
|
2019-03-19 16:06:18 +01:00
|
|
|
rawContent := strings.Join(cmd.Args[3:], " ")
|
2018-05-27 14:30:52 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:42:55 +02:00
|
|
|
type ToggleMessage interface {
|
2020-04-16 19:05:03 +02:00
|
|
|
Name() string
|
2020-04-16 18:42:55 +02:00
|
|
|
Format(state bool) string
|
|
|
|
}
|
|
|
|
|
|
|
|
type HideMessage string
|
|
|
|
|
|
|
|
func (hm HideMessage) Format(state bool) string {
|
|
|
|
if state {
|
|
|
|
return string(hm) + " is now hidden"
|
|
|
|
} else {
|
|
|
|
return string(hm) + " is now visible"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 19:05:03 +02:00
|
|
|
func (hm HideMessage) Name() string {
|
|
|
|
return string(hm)
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:42:55 +02:00
|
|
|
type SimpleToggleMessage string
|
|
|
|
|
|
|
|
func (stm SimpleToggleMessage) Format(state bool) string {
|
|
|
|
if state {
|
|
|
|
return "Disabled " + string(stm)
|
|
|
|
} else {
|
|
|
|
return "Enabled " + string(stm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 19:05:03 +02:00
|
|
|
func (stm SimpleToggleMessage) Name() string {
|
|
|
|
return string(unicode.ToUpper(rune(stm[0]))) + string(stm[1:])
|
|
|
|
}
|
|
|
|
|
2021-03-11 21:42:46 +01:00
|
|
|
type NewlineKeybindMessage string
|
|
|
|
|
|
|
|
func (nkm NewlineKeybindMessage) Format(state bool) string {
|
|
|
|
if state {
|
|
|
|
return "Now using <enter> to create new line and <alt+enter> to send"
|
|
|
|
} else {
|
|
|
|
return "Now using <enter> to send and <alt+enter> to create new line"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nkm NewlineKeybindMessage) Name() string {
|
|
|
|
return string(nkm)
|
|
|
|
}
|
|
|
|
|
2020-04-16 18:42:55 +02:00
|
|
|
var toggleMsg = map[string]ToggleMessage{
|
2020-04-16 19:05:03 +02:00
|
|
|
"rooms": HideMessage("Room list sidebar"),
|
|
|
|
"users": HideMessage("User list sidebar"),
|
2020-04-16 18:42:55 +02:00
|
|
|
"baremessages": SimpleToggleMessage("bare message view"),
|
|
|
|
"images": SimpleToggleMessage("image rendering"),
|
|
|
|
"typingnotif": SimpleToggleMessage("typing notifications"),
|
|
|
|
"emojis": SimpleToggleMessage("emoji shortcode conversion"),
|
|
|
|
"html": SimpleToggleMessage("HTML input"),
|
|
|
|
"markdown": SimpleToggleMessage("markdown input"),
|
|
|
|
"downloads": SimpleToggleMessage("automatic downloads"),
|
|
|
|
"notifications": SimpleToggleMessage("desktop notifications"),
|
2020-07-25 19:54:32 +02:00
|
|
|
"unverified": SimpleToggleMessage("sending messages to unverified devices"),
|
2020-08-17 00:57:25 +02:00
|
|
|
"showurls": SimpleToggleMessage("show URLs in text format"),
|
2021-03-11 21:42:46 +01:00
|
|
|
"newline": NewlineKeybindMessage("should <alt+enter> make a new line or send the message"),
|
2020-04-16 18:42:55 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:05:03 +02:00
|
|
|
func makeUsage() string {
|
|
|
|
var buf strings.Builder
|
|
|
|
buf.WriteString("Usage: /toggle <things...>\n\n")
|
|
|
|
buf.WriteString("List of Things:\n")
|
|
|
|
for key, value := range toggleMsg {
|
|
|
|
_, _ = fmt.Fprintf(&buf, "* %s - %s\n", key, value.Name())
|
|
|
|
}
|
|
|
|
return buf.String()[:buf.Len()-1]
|
|
|
|
}
|
|
|
|
|
2018-06-30 22:26:40 +02:00
|
|
|
func cmdToggle(cmd *Command) {
|
2018-05-27 13:54:07 +02:00
|
|
|
if len(cmd.Args) == 0 {
|
2020-04-16 19:05:03 +02:00
|
|
|
cmd.Reply(makeUsage())
|
2018-05-27 13:54:07 +02:00
|
|
|
return
|
|
|
|
}
|
2020-04-16 19:05:03 +02:00
|
|
|
for _, thing := range cmd.Args {
|
|
|
|
var val *bool
|
|
|
|
switch thing {
|
|
|
|
case "rooms":
|
|
|
|
val = &cmd.Config.Preferences.HideRoomList
|
|
|
|
case "users":
|
|
|
|
val = &cmd.Config.Preferences.HideUserList
|
|
|
|
case "baremessages":
|
|
|
|
val = &cmd.Config.Preferences.BareMessageView
|
|
|
|
case "images":
|
|
|
|
val = &cmd.Config.Preferences.DisableImages
|
|
|
|
case "typingnotif":
|
|
|
|
val = &cmd.Config.Preferences.DisableTypingNotifs
|
|
|
|
case "emojis":
|
|
|
|
val = &cmd.Config.Preferences.DisableEmojis
|
|
|
|
case "html":
|
|
|
|
val = &cmd.Config.Preferences.DisableHTML
|
|
|
|
case "markdown":
|
|
|
|
val = &cmd.Config.Preferences.DisableMarkdown
|
|
|
|
case "downloads":
|
|
|
|
val = &cmd.Config.Preferences.DisableDownloads
|
|
|
|
case "notifications":
|
|
|
|
val = &cmd.Config.Preferences.DisableNotifications
|
2020-07-25 19:54:32 +02:00
|
|
|
case "unverified":
|
|
|
|
val = &cmd.Config.SendToVerifiedOnly
|
2020-08-15 06:59:02 +02:00
|
|
|
case "showurls":
|
2020-08-17 00:57:25 +02:00
|
|
|
val = &cmd.Config.Preferences.DisableShowURLs
|
2021-03-08 06:33:24 +01:00
|
|
|
case "newline":
|
2021-03-11 21:42:46 +01:00
|
|
|
val = &cmd.Config.Preferences.AltEnterToSend
|
2020-04-16 19:05:03 +02:00
|
|
|
default:
|
|
|
|
cmd.Reply("Unknown toggle %s. Use /toggle without arguments for a list of togglable things.", thing)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*val = !(*val)
|
|
|
|
debug.Print(thing, *val)
|
|
|
|
cmd.Reply(toggleMsg[thing].Format(*val))
|
2018-05-27 13:54:07 +02:00
|
|
|
}
|
|
|
|
cmd.UI.Render()
|
|
|
|
go cmd.Matrix.SendPreferencesToMatrix()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdLogout(cmd *Command) {
|
|
|
|
cmd.Matrix.Logout()
|
|
|
|
}
|