From 730f7472b30342b5ddfb0257167e3e7c60c806dc Mon Sep 17 00:00:00 2001 From: David Florness Date: Sat, 22 May 2021 18:51:42 -0400 Subject: [PATCH] deletemymsgs --- deletemymsgs/cmd.go | 70 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 5 ++-- 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 deletemymsgs/cmd.go diff --git a/deletemymsgs/cmd.go b/deletemymsgs/cmd.go new file mode 100644 index 0000000..520830c --- /dev/null +++ b/deletemymsgs/cmd.go @@ -0,0 +1,70 @@ +package deletemymsgs + +import ( + "fmt" + "os" + + "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("deletemymsgs") + x.Summary = "delete all messages we've sent to a particular room (be careful!)" + x.Usage = "" + + x.Method = func(args []string) error { + if len(args) != 1 { + return x.UsageError() + } + return deletemymsgs(id.RoomID(args[0])) + } +} + +func deletemymsgs(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) + syncer.OnEvent(func (source mautrix.EventSource, evt *event.Event) { + // skip events that we didn't send + if evt.Sender != client.UserID { + return + } + // skip events that aren't in this room + if evt.RoomID != roomID { + return + } + // only redact messages events (not state, ephemeral, etc.) + if evt.Type.Class != event.MessageEventType { + return + } + // don't redact redactions + if evt.Type.Type == "m.room.redaction" { + return + } + if evt.Unsigned.RedactedBecause != nil { + fmt.Printf("event %s already redacted\n", evt.ID) + return + } + _, err := client.RedactEvent(evt.RoomID, evt.ID) + if err != nil { + fmt.Fprintf(os.Stderr, "couldn't redact event %v: %s\n", evt, err) + return + } + fmt.Printf("redacted event %v\n", evt) + }) + + fmt.Printf("Deleting messages from room with ID %s...\n", roomID) + return client.Sync() +} diff --git a/main.go b/main.go index 6fbbdd4..41c47af 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,13 @@ package main import ( - "github.com/rwxrob/cmdtab" _ "git.hnitbjorg.xyz/~edwargix/axon/autojoin" + _ "git.hnitbjorg.xyz/~edwargix/axon/deletemymsgs" + "github.com/rwxrob/cmdtab" ) func main() { - x := cmdtab.New("axon", "autojoin") + x := cmdtab.New("axon", "autojoin", "deletemymsgs") x.Summary = "a set of simple Matrix utility programs for the Client-Server API" x.Usage = "" x.Version = "v0.0.0" -- 2.38.4