~edwargix/axon

fcefe8c2b3502d2da6b89efbc23ac8f5d239cb3e — David Florness 3 years ago faf48f5
Implement echo

This just prints event metadata.
2 files changed, 61 insertions(+), 1 deletions(-)

A echo/cmd.go
M main.go
A echo/cmd.go => echo/cmd.go +59 -0
@@ 0,0 1,59 @@
package print

import (
	"fmt"

	"git.hnitbjorg.xyz/~edwargix/axon/shared"
	"github.com/rwxrob/cmdtab"
	"maunium.net/go/mautrix"
	"maunium.net/go/mautrix/event"
	"maunium.net/go/mautrix/id"
)

func init() {
	x := cmdtab.New("echo")
	x.Summary = "print all events, optionally filtering for a specific room"
	x.Usage = "[roomID]"

	x.Method = func(args []string) error {
		if len(args) > 1 || (len(args) > 0 && args[0] == "-h") {
			return x.UsageError()
		}
		var roomID *id.RoomID
		if len(args) == 1 {
			r := id.RoomID(args[0])
			roomID = &r
		}
		return echo(roomID)
	}
}

func echo(roomID *id.RoomID) error {
	authInfo, err := shared.GetAuthInfo()
	if err != nil {
		return err
	}

	client, err := mautrix.NewClient(authInfo.Homeserver, authInfo.UserID, authInfo.AccessToken)
	if err != nil {
		return err
	}

	syncer := client.Syncer.(*mautrix.DefaultSyncer)
	// stop mautrix from ignoring unparsable events
	syncer.ParseEventContent = false
	syncer.OnEvent(func (source mautrix.EventSource, evt *event.Event) {
		// filter by room if needed
		if roomID != nil && evt.RoomID != *roomID {
			return
		}
		fmt.Printf("%[5]d: <%[1]s> %[4]s (%[2]s/%[3]s)\n",
			evt.Sender,
			evt.Type.String(),
			evt.ID,
			evt.Content.AsMessage().Body,
			source)
	})

	return client.Sync()
}

M main.go => main.go +2 -1
@@ 3,11 3,12 @@ package main
import (
	_ "git.hnitbjorg.xyz/~edwargix/axon/autojoin"
	_ "git.hnitbjorg.xyz/~edwargix/axon/deletemymsgs"
	_ "git.hnitbjorg.xyz/~edwargix/axon/echo"
	"github.com/rwxrob/cmdtab"
)

func main() {
	x := cmdtab.New("axon", "autojoin", "deletemymsgs")
	x := cmdtab.New("axon", "autojoin", "deletemymsgs", "echo")
	x.Summary = "a set of simple Matrix utility programs for the Client-Server API"
	x.Usage = "<cmd>"
	x.Version = "v0.0.0"