telnetroulette/commands.go
2024-06-08 11:52:12 +02:00

122 lines
3.1 KiB
Go

package main
import (
"math/rand"
"strconv"
"sync"
)
type CommandHandlerFunc func(args []string, clientID int) string
type CommandRegistry struct {
commands map[string]CommandHandlerFunc
mutex sync.Mutex // Mutex to safely access commands map
}
func NewCommandRegistry() *CommandRegistry {
return &CommandRegistry{
commands: make(map[string]CommandHandlerFunc),
}
}
func (cr *CommandRegistry) RegisterCommand(command string, handler CommandHandlerFunc) {
cr.mutex.Lock()
defer cr.mutex.Unlock()
cr.commands[command] = handler
}
func (cr *CommandRegistry) GetCommandHandler(command string, clientID int) (CommandHandlerFunc, bool, int) {
cr.mutex.Lock()
defer cr.mutex.Unlock()
handler, ok := cr.commands[command]
return handler, ok, clientID
}
func RegisterCommands(registry *CommandRegistry) {
registry.RegisterCommand("help", HelpCommand)
registry.RegisterCommand("setname", SetNameCommand)
registry.RegisterCommand("list", ListClientsCommand)
registry.RegisterCommand("tell", TellClientCommand)
registry.RegisterCommand("clear", ClearCommand)
registry.RegisterCommand("rng", RandomCommand)
// Add more commands here...
}
func HelpCommand(args []string, clientID int) string {
// Handle help command
if len(clients) > 0 {
return "Just help yourself, " + clients[clientID].Username
}
return "No clients available"
}
func RandomCommand(args []string, clientID int) string {
// Handle help command
if len(args) == 2 {
from, err1 := strconv.Atoi(args[0])
to, err2 := strconv.Atoi(args[1])
if err1 != nil || err2 != nil {
return "Invalid argument"
}
if to-from <= 0 {
return "Invalid range"
}
return "The number is " + strconv.Itoa(rand.Intn(to-from)+from)
}
return "No range provided"
}
func ClearCommand(args []string, clientID int) string {
// Handle help command
return "\033[H\033[2J\033[K"
}
func SetNameCommand(args []string, clientID int) string {
// Handle help command
if len(args) == 1 {
newName := args[0]
clients[clientID].Username = newName
return "Name set to " + newName
}
return "No name provided"
}
func ListClientsCommand(args []string, clientID int) string {
message := ""
for i, client := range clients {
message += strconv.Itoa(i) + ": " + client.Username + "\n"
}
return message
}
func TellClientCommand(args []string, clientID int) string {
message := "Message not delivered"
if len(args) >= 2 {
clientTo := args[0]
//if integer assume clientID
var toClientID int
var toClientName string
toClientID, err := strconv.Atoi(clientTo)
if err != nil {
toClientID = getIDByName(clientTo)
toClientName = clientTo
}
if toClientID < len(clients) && toClientID >= 0 {
toClientName = clients[toClientID].Username
myUsername := clients[clientID].Username
newMessage := ""
//all the remaining arguments are a part of the message, join them with space
for i, arg := range args {
if i > 0 {
newMessage += arg + " "
}
}
clients[toClientID].send("\a\r\n<" + myUsername + "(" + strconv.Itoa(clientID) + ")> " + newMessage + "\r\n")
return "Message sent to " + toClientName
} else {
message = "Invalid recipient"
}
}
return message
}