158 lines
3.9 KiB
Go
Raw Normal View History

2022-09-12 22:36:56 +02:00
package handler
2020-01-20 14:38:07 +00:00
import (
2021-10-05 22:38:24 +02:00
"m1k1o/neko/internal/types"
"m1k1o/neko/internal/types/event"
"m1k1o/neko/internal/types/message"
2020-01-20 14:38:07 +00:00
)
2020-01-24 15:47:37 +00:00
func (h *MessageHandler) controlRelease(id string, session types.Session) error {
2020-01-20 14:38:07 +00:00
// check if session is host
if !h.sessions.IsHost(id) {
2020-01-23 15:23:26 +00:00
h.logger.Debug().Str("id", id).Msg("is not the host")
2020-01-20 14:38:07 +00:00
return nil
}
// release host
h.logger.Debug().Str("id", id).Msgf("host called %s", event.CONTROL_RELEASE)
h.sessions.ClearHost()
// tell everyone
if err := h.sessions.Broadcast(
2020-01-20 14:38:07 +00:00
message.Control{
2020-01-20 16:22:24 +00:00
Event: event.CONTROL_RELEASE,
ID: id,
2020-01-20 14:38:07 +00:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
2020-01-20 14:38:07 +00:00
return err
}
return nil
}
func (h *MessageHandler) controlRequest(id string, session types.Session) error {
2020-01-20 14:38:07 +00:00
// check for host
if !h.sessions.HasHost() {
2021-11-16 22:50:11 +01:00
// check if control is locked or user is admin
2022-09-13 20:04:43 +02:00
if h.state.IsLocked("control") && !session.Admin() {
2021-11-16 22:50:11 +01:00
h.logger.Debug().Msg("control is locked")
return nil
}
2020-01-20 14:38:07 +00:00
// set host
2021-10-05 23:10:10 +02:00
err := h.sessions.SetHost(id)
if err != nil {
return err
}
2020-01-20 14:38:07 +00:00
// let everyone know
if err := h.sessions.Broadcast(
2020-01-20 14:38:07 +00:00
message.Control{
2020-01-20 16:22:24 +00:00
Event: event.CONTROL_LOCKED,
ID: id,
2020-01-20 14:38:07 +00:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
2020-01-20 14:38:07 +00:00
return err
}
return nil
}
// get host
host, ok := h.sessions.GetHost()
if ok {
// tell session there is a host
if err := session.Send(message.Control{
2020-01-20 16:22:24 +00:00
Event: event.CONTROL_REQUEST,
2020-01-24 15:47:37 +00:00
ID: host.ID(),
2020-01-20 14:38:07 +00:00
}); err != nil {
h.logger.Warn().Err(err).Str("id", id).Msgf("sending event %s has failed", event.CONTROL_REQUEST)
return err
}
// tell host session wants to be host
if err := host.Send(message.Control{
2020-01-20 16:22:24 +00:00
Event: event.CONTROL_REQUESTING,
ID: id,
2020-01-20 14:38:07 +00:00
}); err != nil {
2020-01-24 15:47:37 +00:00
h.logger.Warn().Err(err).Str("id", host.ID()).Msgf("sending event %s has failed", event.CONTROL_REQUESTING)
2020-01-20 14:38:07 +00:00
return err
}
}
return nil
}
2020-01-23 15:23:26 +00:00
2020-01-24 15:47:37 +00:00
func (h *MessageHandler) controlGive(id string, session types.Session, payload *message.Control) error {
2020-01-23 15:23:26 +00:00
// check if session is host
if !h.sessions.IsHost(id) {
h.logger.Debug().Str("id", id).Msg("is not the host")
return nil
}
if !h.sessions.Has(payload.ID) {
h.logger.Debug().Str("id", payload.ID).Msg("user does not exist")
return nil
}
2021-11-16 22:50:11 +01:00
// check if control is locked or giver is admin
2022-09-13 20:04:43 +02:00
if h.state.IsLocked("control") && !session.Admin() {
2021-11-16 22:50:11 +01:00
h.logger.Debug().Msg("control is locked")
return nil
}
2020-01-23 15:23:26 +00:00
// set host
2021-10-05 23:10:10 +02:00
err := h.sessions.SetHost(payload.ID)
if err != nil {
return err
}
2020-01-23 15:23:26 +00:00
// let everyone know
if err := h.sessions.Broadcast(
2020-01-23 15:23:26 +00:00
message.ControlTarget{
Event: event.CONTROL_GIVE,
ID: id,
Target: payload.ID,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
2020-01-23 15:23:26 +00:00
return err
}
return nil
}
2020-01-25 14:29:52 +00:00
func (h *MessageHandler) controlClipboard(id string, session types.Session, payload *message.Clipboard) error {
// check if session can access clipboard
if (!h.webrtc.ImplicitControl() && !h.sessions.IsHost(id)) || (h.webrtc.ImplicitControl() && !h.sessions.CanControl(id)) {
h.logger.Debug().Str("id", id).Msg("cannot access clipboard")
2020-01-25 14:29:52 +00:00
return nil
}
2022-09-12 22:12:47 +02:00
h.desktop.WriteClipboard(payload.Text)
2020-01-25 14:29:52 +00:00
return nil
}
2020-06-16 00:55:14 +02:00
func (h *MessageHandler) controlKeyboard(id string, session types.Session, payload *message.Keyboard) error {
// check if session can control keyboard
if (!h.webrtc.ImplicitControl() && !h.sessions.IsHost(id)) || (h.webrtc.ImplicitControl() && !h.sessions.CanControl(id)) {
h.logger.Debug().Str("id", id).Msg("cannot control keyboard")
2020-06-16 00:55:14 +02:00
return nil
}
2022-09-13 21:40:40 +02:00
h.desktop.SetKeyboardModifiers(types.KeyboardModifiers{
NumLock: payload.NumLock,
CapsLock: payload.CapsLock,
// TODO: ScrollLock is deprecated.
})
2020-06-20 00:57:37 +02:00
// change layout
if payload.Layout != nil {
2022-09-13 21:40:40 +02:00
return h.desktop.SetKeyboardMap(types.KeyboardMap{
Layout: *payload.Layout,
})
2020-06-20 00:57:37 +02:00
}
2020-06-16 00:55:14 +02:00
return nil
}