From fcefe8c2b3502d2da6b89efbc23ac8f5d239cb3e Mon Sep 17 00:00:00 2001 From: David Florness Date: Sat, 22 May 2021 20:13:31 -0400 Subject: [PATCH] Implement echo This just prints event metadata. --- echo/cmd.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 3 ++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 echo/cmd.go diff --git a/echo/cmd.go b/echo/cmd.go new file mode 100644 index 0000000..ba90c7c --- /dev/null +++ b/echo/cmd.go @@ -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() +} diff --git a/main.go b/main.go index 41c47af..ffde0a2 100644 --- a/main.go +++ b/main.go @@ -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 = "" x.Version = "v0.0.0" -- 2.38.4