2020-11-02 04:09:48 +13:00
|
|
|
package handler
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/types/event"
|
|
|
|
"demodesk/neko/internal/types/message"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) adminLock(session types.Session) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("user not admin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.locked {
|
|
|
|
h.logger.Debug().Msg("server already locked...")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
h.locked = true
|
|
|
|
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.Admin{
|
|
|
|
Event: event.ADMIN_LOCK,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_LOCK)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) adminUnlock(session types.Session) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("user not admin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !h.locked {
|
|
|
|
h.logger.Debug().Msg("server not locked...")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
h.locked = false
|
|
|
|
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.Admin{
|
|
|
|
Event: event.ADMIN_UNLOCK,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_UNLOCK)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) adminControl(session types.Session) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("user not admin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:53:19 +13:00
|
|
|
host := h.sessions.GetHost()
|
|
|
|
h.sessions.SetHost(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2020-11-01 11:53:19 +13:00
|
|
|
if host != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.AdminTarget{
|
|
|
|
Event: event.ADMIN_CONTROL,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
Target: host.ID(),
|
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_CONTROL)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.Admin{
|
|
|
|
Event: event.ADMIN_CONTROL,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_CONTROL)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) adminRelease(session types.Session) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("user not admin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:53:19 +13:00
|
|
|
host := h.sessions.GetHost()
|
2020-10-23 03:54:50 +13:00
|
|
|
h.sessions.ClearHost()
|
|
|
|
|
2020-11-01 11:53:19 +13:00
|
|
|
if host != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.AdminTarget{
|
|
|
|
Event: event.ADMIN_RELEASE,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
Target: host.ID(),
|
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_RELEASE)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.Admin{
|
|
|
|
Event: event.ADMIN_RELEASE,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_RELEASE)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) adminGive(session types.Session, payload *message.Admin) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("user not admin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:53:19 +13:00
|
|
|
target, ok := h.sessions.Get(payload.ID)
|
|
|
|
if !ok {
|
2020-11-02 06:12:16 +13:00
|
|
|
h.logger.Debug().Str("id", payload.ID).Msg("can't find target session")
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:53:19 +13:00
|
|
|
h.sessions.SetHost(target)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.AdminTarget{
|
|
|
|
Event: event.CONTROL_GIVE,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-11-01 11:53:19 +13:00
|
|
|
Target: target.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) adminMute(session types.Session, payload *message.Admin) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("user not admin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target, ok := h.sessions.Get(payload.ID)
|
|
|
|
if !ok {
|
|
|
|
h.logger.Debug().Str("id", payload.ID).Msg("can't find session id")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.Admin() {
|
|
|
|
h.logger.Debug().Msg("target is an admin, baling")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target.SetMuted(true)
|
|
|
|
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.AdminTarget{
|
|
|
|
Event: event.ADMIN_MUTE,
|
|
|
|
Target: target.ID(),
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_UNMUTE)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) adminUnmute(session types.Session, payload *message.Admin) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("user not admin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target, ok := h.sessions.Get(payload.ID)
|
|
|
|
if !ok {
|
|
|
|
h.logger.Debug().Str("id", payload.ID).Msg("can't find target session")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target.SetMuted(false)
|
|
|
|
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.AdminTarget{
|
|
|
|
Event: event.ADMIN_UNMUTE,
|
|
|
|
Target: target.ID(),
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_UNMUTE)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) adminKick(session types.Session, payload *message.Admin) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("user not admin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target, ok := h.sessions.Get(payload.ID)
|
|
|
|
if !ok {
|
2020-11-02 06:12:16 +13:00
|
|
|
h.logger.Debug().Str("id", payload.ID).Msg("can't find target session")
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.Admin() {
|
|
|
|
h.logger.Debug().Msg("target is an admin, baling")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 09:20:42 +13:00
|
|
|
if err := target.Disconnect("kicked"); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.AdminTarget{
|
|
|
|
Event: event.ADMIN_KICK,
|
|
|
|
Target: target.ID(),
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, []string{payload.ID}); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_KICK)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) adminBan(session types.Session, payload *message.Admin) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("user not admin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target, ok := h.sessions.Get(payload.ID)
|
|
|
|
if !ok {
|
2020-11-02 06:12:16 +13:00
|
|
|
h.logger.Debug().Str("id", payload.ID).Msg("can't find target session")
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.Admin() {
|
|
|
|
h.logger.Debug().Msg("target is an admin, baling")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
remote := target.Address()
|
|
|
|
if remote == "" {
|
|
|
|
h.logger.Debug().Msg("no remote address, baling")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
address := strings.SplitN(remote, ":", -1)
|
|
|
|
if len(address[0]) < 1 {
|
|
|
|
h.logger.Debug().Str("address", remote).Msg("no remote address, baling")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
h.logger.Debug().Str("address", remote).Msg("adding address to banned")
|
|
|
|
|
|
|
|
h.banned[address[0]] = true
|
|
|
|
|
2020-11-01 09:20:42 +13:00
|
|
|
if err := target.Disconnect("banned"); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.AdminTarget{
|
|
|
|
Event: event.ADMIN_BAN,
|
|
|
|
Target: target.ID(),
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, []string{payload.ID}); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_BAN)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|