diff --git a/internal/matrix/client.go b/internal/matrix/client.go index 8084fc7..a4fd52e 100644 --- a/internal/matrix/client.go +++ b/internal/matrix/client.go @@ -20,6 +20,7 @@ type Client struct { config *config.MatrixConfig db *sql.DB deviceID id.DeviceID + greetedRooms []id.RoomID olmMachine *crypto.OlmMachine store *sqlStore syncer *mautrix.DefaultSyncer @@ -50,7 +51,7 @@ func NewClient(config *config.MatrixConfig, db *sql.DB) (*Client, error) { deviceID := loadDeviceID(db, userID) - return &Client{client, config, db, deviceID, nil, store, syncer, userID}, nil + return &Client{client, config, db, deviceID, nil, nil, store, syncer, userID}, nil } func (c *Client) Login() error { @@ -123,9 +124,39 @@ func (c *Client) Sync(ctx context.Context, wg *sync.WaitGroup) { } } -func (c *Client) Broadcast(message *event.MessageEventContent) { +func (c *Client) Send(roomID id.RoomID, message *event.MessageEventContent) { content := event.Content{Parsed: message} + if c.store.IsEncrypted(roomID) { + encrypted, err := c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content) + if err == crypto.NoGroupSession || err == crypto.SessionExpired || err == crypto.SessionNotShared { + err = c.olmMachine.ShareGroupSession(roomID, c.store.GetRoomMembers(roomID)) + if err != nil { + log.Print(err) + return + } + + encrypted, err = c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content) + } + + if err != nil { + log.Print(err) + return + } + + _, err = c.client.SendMessageEvent(roomID, event.EventEncrypted, encrypted) + if err != nil { + log.Print(err) + } + } else { + _, err := c.client.SendMessageEvent(roomID, event.EventMessage, &content) + if err != nil { + log.Print(err) + } + } +} + +func (c *Client) Broadcast(message *event.MessageEventContent) { for _, roomID := range c.store.FindAllSharedRooms(c.userID) { allowed := false for _, room := range c.config.AllowedRooms { @@ -139,33 +170,7 @@ func (c *Client) Broadcast(message *event.MessageEventContent) { continue } - if c.store.IsEncrypted(roomID) { - encrypted, err := c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content) - if err == crypto.NoGroupSession || err == crypto.SessionExpired || err == crypto.SessionNotShared { - err = c.olmMachine.ShareGroupSession(roomID, c.store.GetRoomMembers(roomID)) - if err != nil { - log.Print(err) - continue - } - - encrypted, err = c.olmMachine.EncryptMegolmEvent(roomID, event.EventMessage, &content) - } - - if err != nil { - log.Print(err) - continue - } - - _, err = c.client.SendMessageEvent(roomID, event.EventEncrypted, encrypted) - if err != nil { - log.Print(err) - } - } else { - _, err := c.client.SendMessageEvent(roomID, event.EventMessage, &content) - if err != nil { - log.Print(err) - } - } + c.Send(roomID, message) } } @@ -190,6 +195,20 @@ func (c *Client) handleMemberEvent(source mautrix.EventSource, evt *event.Event) if err != nil { log.Print(err) } + + for _, room := range c.greetedRooms { + if room == evt.RoomID { + return + } + } + + message := event.MessageEventContent{ + MsgType: event.MsgText, + Body: "Ich heiße Marvin \U0001f41e und werde ab jetzt neue Ticketbestellungen verkünden.", + } + c.Send(evt.RoomID, &message) + + c.greetedRooms = append(c.greetedRooms, evt.RoomID) } else { _, err := c.client.LeaveRoom(evt.RoomID) if err != nil {