2020-01-21 03:38:07 +13:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2021-10-06 09:38:24 +13:00
|
|
|
"m1k1o/neko/internal/types"
|
|
|
|
"m1k1o/neko/internal/types/event"
|
|
|
|
"m1k1o/neko/internal/types/message"
|
2020-01-21 03:38:07 +13:00
|
|
|
)
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (h *MessageHandler) controlRelease(id string, session types.Session) error {
|
2020-01-21 03:38:07 +13:00
|
|
|
|
|
|
|
// check if session is host
|
|
|
|
if !h.sessions.IsHost(id) {
|
2020-01-24 04:23:26 +13:00
|
|
|
h.logger.Debug().Str("id", id).Msg("is not the host")
|
2020-01-21 03:38:07 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// release host
|
|
|
|
h.logger.Debug().Str("id", id).Msgf("host called %s", event.CONTROL_RELEASE)
|
|
|
|
h.sessions.ClearHost()
|
|
|
|
|
|
|
|
// tell everyone
|
2020-02-27 01:46:10 +13:00
|
|
|
if err := h.sessions.Broadcast(
|
2020-01-21 03:38:07 +13:00
|
|
|
message.Control{
|
2020-01-21 05:22:24 +13:00
|
|
|
Event: event.CONTROL_RELEASE,
|
|
|
|
ID: id,
|
2020-01-21 03:38:07 +13:00
|
|
|
}, nil); err != nil {
|
2020-02-27 01:46:10 +13:00
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
|
2020-01-21 03:38:07 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:01:23 +12:00
|
|
|
func (h *MessageHandler) controlRequest(id string, session types.Session) error {
|
2020-01-21 03:38:07 +13:00
|
|
|
// check for host
|
|
|
|
if !h.sessions.HasHost() {
|
|
|
|
// set host
|
2021-10-06 10:10:10 +13:00
|
|
|
err := h.sessions.SetHost(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-21 03:38:07 +13:00
|
|
|
|
|
|
|
// let everyone know
|
2020-02-27 01:46:10 +13:00
|
|
|
if err := h.sessions.Broadcast(
|
2020-01-21 03:38:07 +13:00
|
|
|
message.Control{
|
2020-01-21 05:22:24 +13:00
|
|
|
Event: event.CONTROL_LOCKED,
|
|
|
|
ID: id,
|
2020-01-21 03:38:07 +13:00
|
|
|
}, nil); err != nil {
|
2020-02-27 01:46:10 +13:00
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
|
2020-01-21 03:38:07 +13: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-21 05:22:24 +13:00
|
|
|
Event: event.CONTROL_REQUEST,
|
2020-01-25 04:47:37 +13:00
|
|
|
ID: host.ID(),
|
2020-01-21 03:38:07 +13: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-21 05:22:24 +13:00
|
|
|
Event: event.CONTROL_REQUESTING,
|
|
|
|
ID: id,
|
2020-01-21 03:38:07 +13:00
|
|
|
}); err != nil {
|
2020-01-25 04:47:37 +13:00
|
|
|
h.logger.Warn().Err(err).Str("id", host.ID()).Msgf("sending event %s has failed", event.CONTROL_REQUESTING)
|
2020-01-21 03:38:07 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-24 04:23:26 +13:00
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (h *MessageHandler) controlGive(id string, session types.Session, payload *message.Control) error {
|
2020-01-24 04:23:26 +13: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
|
|
|
|
}
|
|
|
|
|
|
|
|
// set host
|
2021-10-06 10:10:10 +13:00
|
|
|
err := h.sessions.SetHost(payload.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-24 04:23:26 +13:00
|
|
|
|
|
|
|
// let everyone know
|
2020-02-27 01:46:10 +13:00
|
|
|
if err := h.sessions.Broadcast(
|
2020-01-24 04:23:26 +13:00
|
|
|
message.ControlTarget{
|
|
|
|
Event: event.CONTROL_GIVE,
|
|
|
|
ID: id,
|
|
|
|
Target: payload.ID,
|
|
|
|
}, nil); err != nil {
|
2020-02-27 01:46:10 +13:00
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
|
2020-01-24 04:23:26 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-26 03:29:52 +13:00
|
|
|
|
|
|
|
func (h *MessageHandler) controlClipboard(id string, session types.Session, payload *message.Clipboard) error {
|
|
|
|
// check if session is host
|
|
|
|
if !h.sessions.IsHost(id) {
|
|
|
|
h.logger.Debug().Str("id", id).Msg("is not the host")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
h.remote.WriteClipboard(payload.Text)
|
2020-01-26 03:29:52 +13:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-16 10:55:14 +12:00
|
|
|
|
|
|
|
func (h *MessageHandler) controlKeyboard(id string, session types.Session, payload *message.Keyboard) error {
|
|
|
|
// check if session is host
|
|
|
|
if !h.sessions.IsHost(id) {
|
|
|
|
h.logger.Debug().Str("id", id).Msg("is not the host")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:57:37 +12:00
|
|
|
// change layout
|
|
|
|
if payload.Layout != nil {
|
2020-06-21 13:09:53 +12:00
|
|
|
h.remote.SetKeyboardLayout(*payload.Layout)
|
2020-06-20 10:57:37 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// set num lock
|
2020-06-20 12:15:38 +12:00
|
|
|
var NumLock = 0
|
|
|
|
if payload.NumLock == nil {
|
|
|
|
NumLock = -1
|
|
|
|
} else if *payload.NumLock {
|
|
|
|
NumLock = 1
|
2020-06-20 10:57:37 +12:00
|
|
|
}
|
|
|
|
|
2020-06-21 13:01:15 +12:00
|
|
|
// set caps lock
|
|
|
|
var CapsLock = 0
|
|
|
|
if payload.CapsLock == nil {
|
|
|
|
CapsLock = -1
|
|
|
|
} else if *payload.CapsLock {
|
|
|
|
CapsLock = 1
|
|
|
|
}
|
|
|
|
|
2020-06-20 10:57:37 +12:00
|
|
|
// set scroll lock
|
2020-06-20 12:15:38 +12:00
|
|
|
var ScrollLock = 0
|
|
|
|
if payload.ScrollLock == nil {
|
|
|
|
ScrollLock = -1
|
|
|
|
} else if *payload.ScrollLock {
|
|
|
|
ScrollLock = 1
|
2020-06-20 10:57:37 +12:00
|
|
|
}
|
|
|
|
|
2020-06-22 00:45:04 +12:00
|
|
|
h.logger.Debug().
|
|
|
|
Int("NumLock", NumLock).
|
|
|
|
Int("CapsLock", CapsLock).
|
|
|
|
Int("ScrollLock", ScrollLock).
|
|
|
|
Msg("setting keyboard modifiers")
|
|
|
|
|
2020-06-21 13:01:15 +12:00
|
|
|
h.remote.SetKeyboardModifiers(NumLock, CapsLock, ScrollLock)
|
2020-06-16 10:55:14 +12:00
|
|
|
return nil
|
|
|
|
}
|