2020-11-02 04:09:48 +13:00
|
|
|
package handler
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
import (
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/pkg/types"
|
|
|
|
"github.com/demodesk/neko/pkg/types/event"
|
|
|
|
"github.com/demodesk/neko/pkg/types/message"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-12-03 06:59:54 +13:00
|
|
|
func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
|
|
|
|
h.sessions.Broadcast(
|
2021-09-02 07:58:39 +12:00
|
|
|
event.SESSION_CREATED,
|
2021-03-14 12:05:31 +13:00
|
|
|
message.SessionData{
|
2020-12-04 04:10:52 +13:00
|
|
|
ID: session.ID(),
|
2021-03-14 12:45:51 +13:00
|
|
|
Profile: session.Profile(),
|
|
|
|
State: session.State(),
|
2022-07-28 22:20:20 +12:00
|
|
|
})
|
2020-12-03 06:59:54 +13:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MessageHandlerCtx) SessionDeleted(session types.Session) error {
|
|
|
|
h.sessions.Broadcast(
|
2021-09-02 07:58:39 +12:00
|
|
|
event.SESSION_DELETED,
|
2021-03-14 12:05:31 +13:00
|
|
|
message.SessionID{
|
2021-09-02 07:58:39 +12:00
|
|
|
ID: session.ID(),
|
2022-07-28 22:20:20 +12:00
|
|
|
})
|
2020-12-03 06:59:54 +13:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
|
2020-12-01 08:07:19 +13:00
|
|
|
if err := h.systemInit(session); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-14 12:45:51 +13:00
|
|
|
if session.Profile().IsAdmin {
|
2020-12-01 08:07:19 +13:00
|
|
|
if err := h.systemAdmin(session); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
2024-04-22 06:10:16 +12:00
|
|
|
|
|
|
|
// update settings in atomic way
|
2024-05-05 10:03:32 +12:00
|
|
|
h.sessions.UpdateSettingsFunc(session, func(settings *types.Settings) bool {
|
2024-04-22 06:10:16 +12:00
|
|
|
// if control protection & locked controls: unlock controls
|
|
|
|
if settings.LockedControls && settings.ControlProtection {
|
|
|
|
settings.LockedControls = false
|
|
|
|
return true // update settings
|
|
|
|
}
|
|
|
|
return false // do not update settings
|
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-12-04 04:42:27 +13:00
|
|
|
return h.SessionStateChanged(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 08:53:25 +13:00
|
|
|
func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
// clear host if exists
|
2020-11-02 08:53:25 +13:00
|
|
|
if session.IsHost() {
|
2020-12-02 22:46:00 +13:00
|
|
|
h.desktop.ResetKeys()
|
2020-10-23 03:54:50 +13:00
|
|
|
h.sessions.ClearHost()
|
|
|
|
}
|
|
|
|
|
2024-04-22 06:10:16 +12:00
|
|
|
if session.Profile().IsAdmin {
|
|
|
|
hasAdmin := false
|
|
|
|
h.sessions.Range(func(s types.Session) bool {
|
|
|
|
if s.Profile().IsAdmin && s.ID() != session.ID() && s.State().IsConnected {
|
|
|
|
hasAdmin = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
// update settings in atomic way
|
2024-05-05 10:03:32 +12:00
|
|
|
h.sessions.UpdateSettingsFunc(session, func(settings *types.Settings) bool {
|
2024-04-22 06:10:16 +12:00
|
|
|
// if control protection & not locked controls & no admin: lock controls
|
|
|
|
if !settings.LockedControls && settings.ControlProtection && !hasAdmin {
|
|
|
|
settings.LockedControls = true
|
|
|
|
return true // update settings
|
|
|
|
}
|
|
|
|
return false // do not update settings
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-04 04:42:27 +13:00
|
|
|
return h.SessionStateChanged(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2020-12-03 06:59:54 +13:00
|
|
|
|
2020-12-04 04:10:52 +13:00
|
|
|
func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error {
|
2020-12-03 06:59:54 +13:00
|
|
|
h.sessions.Broadcast(
|
2021-09-02 07:58:39 +12:00
|
|
|
event.SESSION_PROFILE,
|
2020-12-03 06:59:54 +13:00
|
|
|
message.MemberProfile{
|
2020-12-09 03:14:20 +13:00
|
|
|
ID: session.ID(),
|
2021-09-02 07:58:39 +12:00
|
|
|
MemberProfile: session.Profile(),
|
2022-07-28 22:20:20 +12:00
|
|
|
})
|
2020-12-03 06:59:54 +13:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-04 04:10:52 +13:00
|
|
|
|
|
|
|
func (h *MessageHandlerCtx) SessionStateChanged(session types.Session) error {
|
|
|
|
h.sessions.Broadcast(
|
2021-09-02 07:58:39 +12:00
|
|
|
event.SESSION_STATE,
|
2021-03-14 11:42:16 +13:00
|
|
|
message.SessionState{
|
|
|
|
ID: session.ID(),
|
2021-09-02 07:58:39 +12:00
|
|
|
SessionState: session.State(),
|
2022-07-28 22:20:20 +12:00
|
|
|
})
|
2020-12-04 04:10:52 +13:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|