From 9d25b1569c89091ceafdba485668da7aa5a337e7 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Wed, 10 Jan 2024 18:10:28 +0100 Subject: [PATCH] respond to ping message --- cmd/nourybot/commands.go | 31 ++++++++++++++++++ cmd/nourybot/events.go | 9 ++++++ main.go => cmd/nourybot/main.go | 56 +++++++++++++++++++++++++-------- 3 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 cmd/nourybot/commands.go create mode 100644 cmd/nourybot/events.go rename main.go => cmd/nourybot/main.go (73%) diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go new file mode 100644 index 0000000..b85da68 --- /dev/null +++ b/cmd/nourybot/commands.go @@ -0,0 +1,31 @@ +package main + +import ( + "strings" + + "maunium.net/go/mautrix/event" +) + +func (app *Application) ParseCommand(evt *event.Event) { + // commandName is the actual name of the command without the prefix. + // e.g. `!ping` would be `ping`. + //commandName := strings.ToLower(strings.SplitN(evt.Content.AsMessage().Body, " ", 2)[0][1:]) + + // cmdParams are additional command parameters. + // e.g. `!weather san antonio` + // cmdParam[0] is `san` and cmdParam[1] = `antonio`. + cmdParams := strings.SplitN(evt.Content.AsMessage().Body, " ", 500) + app.Log.Info().Msgf("cmdParams: %s", cmdParams) + + // msgLen is the amount of words in a message without the prefix. + // Useful to check if enough cmdParams are provided. + //msgLen := len(strings.SplitN(evt.Content.AsMessage().Body, " ", -2)) + + app.Log.Info().Msgf("Command: %s", evt.Content.AsMessage().Body) + + switch evt.Content.AsMessage().Body { + case "!xd": + app.SendText(evt, "xd !") + return + } +} diff --git a/cmd/nourybot/events.go b/cmd/nourybot/events.go new file mode 100644 index 0000000..a8f053c --- /dev/null +++ b/cmd/nourybot/events.go @@ -0,0 +1,9 @@ +package main + +import "maunium.net/go/mautrix/event" + +func (app *Application) ParseEvent(evt *event.Event) { + // TODO: + // Log the events or whatever, I don't even know what events there all are rn. + app.Log.Info().Msgf("Event: %s", evt.Content.AsMessage().Body) +} diff --git a/main.go b/cmd/nourybot/main.go similarity index 73% rename from main.go rename to cmd/nourybot/main.go index 085a995..190ef52 100644 --- a/main.go +++ b/cmd/nourybot/main.go @@ -32,6 +32,11 @@ var debug = flag.Bool("debug", false, "Enable debug logs") //var database = flag.String("database", "test.db", "SQLite database path") +type Application struct { + MatrixClient *mautrix.Client + Log zerolog.Logger +} + func main() { flag.Parse() err := godotenv.Load() @@ -48,6 +53,7 @@ func main() { if err != nil { panic(err) } + rl, err := readline.New("[no room]> ") if err != nil { panic(err) @@ -62,22 +68,34 @@ func main() { } client.Log = log + app := &Application{ + MatrixClient: client, + Log: log, + } var lastRoomID id.RoomID - syncer := client.Syncer.(*mautrix.DefaultSyncer) + syncer := app.MatrixClient.Syncer.(*mautrix.DefaultSyncer) syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) { lastRoomID = evt.RoomID rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID)) - log.Info(). - Str("sender", evt.Sender.String()). - Str("type", evt.Type.String()). - Str("id", evt.ID.String()). - Str("body", evt.Content.AsMessage().Body). - Msg("Received message") + if evt.Content.AsMessage().Body[:1] == "!" { + app.Log.Info(). + Str("sender", evt.Sender.String()). + Str("type", evt.Type.String()). + Str("id", evt.ID.String()). + Str("body", evt.Content.AsMessage().Body). + Msg("Received xdddddddddddddddddddddddd message") + + app.ParseCommand(evt) + return + } else { + app.ParseEvent(evt) + return + } }) syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) { - if evt.GetStateKey() == client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite { - _, err := client.JoinRoomByID(context.TODO(), evt.RoomID) + if evt.GetStateKey() == app.MatrixClient.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite { + _, err := app.MatrixClient.JoinRoomByID(context.TODO(), evt.RoomID) if err == nil { lastRoomID = evt.RoomID rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID)) @@ -92,9 +110,10 @@ func main() { Msg("Failed to join room after invite") } } + }) - cryptoHelper, err := cryptohelper.NewCryptoHelper(client, []byte("meow"), database) + cryptoHelper, err := cryptohelper.NewCryptoHelper(app.MatrixClient, []byte("meow"), database) if err != nil { panic(err) } @@ -116,7 +135,7 @@ func main() { panic(err) } // Set the client crypto helper in order to automatically encrypt outgoing messages - client.Crypto = cryptoHelper + app.MatrixClient.Crypto = cryptoHelper log.Info().Msg("Now running") syncCtx, cancelSync := context.WithCancel(context.Background()) @@ -124,7 +143,7 @@ func main() { syncStopWait.Add(1) go func() { - err = client.SyncWithContext(syncCtx) + err = app.MatrixClient.SyncWithContext(syncCtx) defer syncStopWait.Done() if err != nil && !errors.Is(err, context.Canceled) { panic(err) @@ -140,7 +159,7 @@ func main() { log.Error().Msg("Wait for an incoming message before sending messages") continue } - resp, err := client.SendText(context.TODO(), lastRoomID, line) + resp, err := app.MatrixClient.SendText(context.TODO(), lastRoomID, line) if err != nil { log.Error().Err(err).Msg("Failed to send event") } else { @@ -154,3 +173,14 @@ func main() { log.Error().Err(err).Msg("Error closing database") } } + +func (app Application) SendText(evt *event.Event, message string) { + room := evt.RoomID + + resp, err := app.MatrixClient.SendText(context.TODO(), room, message) + if err != nil { + app.Log.Error().Err(err).Msg("Failed to send event") + } else { + app.Log.Info().Str("event_id", resp.EventID.String()).Msg("Event sent") + } +}