2024-03-10 17:21:27 -05:00
|
|
|
// package main is the main package for the LapisDeploy program
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2024-03-10 21:22:05 -05:00
|
|
|
"time"
|
2024-03-10 17:21:27 -05:00
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/mautrix/event"
|
2024-03-10 21:22:05 -05:00
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
"maunium.net/go/mautrix/format"
|
2024-03-10 17:21:27 -05:00
|
|
|
)
|
|
|
|
|
2024-03-10 21:22:05 -05:00
|
|
|
type MatrixMessage struct {
|
|
|
|
text string
|
|
|
|
}
|
|
|
|
|
|
|
|
// I/O queues
|
|
|
|
var MatrixIn []MatrixMessage
|
|
|
|
var MatrixOut []MatrixMessage
|
|
|
|
|
|
|
|
// sendMessage sends a message through Matrix
|
|
|
|
func sendMessage(msg MatrixMessage) {
|
|
|
|
MatrixOut = append(MatrixOut, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initMatrix starts a Matrix bot.
|
2024-03-10 17:21:27 -05:00
|
|
|
func initMatrix() {
|
|
|
|
client, err := mautrix.NewClient(configuration.matrix.homeserver, "", "")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
resp, err := client.Login(context.Background(), &mautrix.ReqLogin{
|
|
|
|
Type: "m.login.password",
|
|
|
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username},
|
|
|
|
Password: configuration.matrix.password,
|
|
|
|
StoreCredentials: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
log.Printf("[Matrix] Logged in as %s", resp.UserID)
|
|
|
|
|
|
|
|
syncer := client.Syncer.(*mautrix.DefaultSyncer)
|
|
|
|
syncer.OnEventType(event.StateMember, func(ctx context.Context, evt *event.Event) {
|
|
|
|
if evt.GetStateKey() == client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
|
|
|
|
if evt.RoomID.String() == configuration.matrix.room_id {
|
|
|
|
_, err := client.JoinRoomByID(ctx, evt.RoomID)
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("[Matrix] Joined room '%s' (Invited by '%s')", evt.RoomID.String(), evt.Sender.String())
|
|
|
|
} else {
|
|
|
|
log.Printf("[Matrix] Failed to join room '%s' after invite (Invited by '%s')", evt.RoomID.String(), evt.Sender.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("[Matrix] Rejected invite to room '%s' (Invited by '%s')", evt.RoomID.String(), evt.Sender.String())
|
|
|
|
}
|
|
|
|
}
|
2024-03-10 21:22:05 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
go func () {
|
|
|
|
for range time.Tick(time.Second * 3) {
|
|
|
|
log.Printf("Scanning for queued messages...")
|
|
|
|
for _, msg := range MatrixOut {
|
|
|
|
_, err := client.SendMessageEvent(context.Background(), id.RoomID(configuration.matrix.room_id), event.EventMessage, format.RenderMarkdown(msg.text, true, false))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[Matrix] ERROR : %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Print("[Matrix] Sent queued message successfully.")
|
|
|
|
}
|
|
|
|
MatrixOut = []MatrixMessage{}
|
|
|
|
}
|
|
|
|
}()
|
2024-03-10 17:21:27 -05:00
|
|
|
|
|
|
|
log.Print("[Matrix] Running!")
|
|
|
|
err = client.Sync()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|