<commit_before>package chat

import (
	"github.com/spring1843/chat-server/src/plugins/command"
	"github.com/spring1843/chat-server/src/shared/logs"
	"github.com/spring1843/pomain/src/shared/errs"
)

// HandleNewInput interprets user input and lets chatServer handle it
func (u *User) HandleNewInput(chatServer *Server, userInput string) (bool, error) {
	if u.GetNickName() == "" {
		// This is from a user who is not identified yet, we do not do anything
		// about his input
		logs.Infof("Unidentified user sent input %q", userInput)
		u.SetIncoming(userInput)
		return true, nil
	}

	if command.IsInputExecutable(userInput) {
		return u.handleCommandInput(chatServer, userInput)
	}

	// If it's not a command it's a chat message to broadcast into the channel
	if u.GetChannel() == "" {
		u.SetOutgoing("You need to join a channel, use /join #channel or use /help for more info.")
		return false, errs.New("User is not in a channel, and input is not a command")
	}
	return u.handleBroadCastInput(chatServer, userInput)
}

func (u *User) handleBroadCastInput(chatServer *Server, userInput string) (bool, error) {
	channel, err := chatServer.GetChannel(u.GetChannel())
	if err != nil {
		return false, errs.Wrap(err, "Error getting channel from server")
	}
	channel.Broadcast(chatServer, `@`+u.GetNickName()+`: `+userInput)
	return true, nil
}

func (u *User) handleCommandInput(chatServer *Server, input string) (bool, error) {
	userCommand, err := command.FromString(input)
	if err != nil {
		u.SetOutgoingf("Invalid command, use /help for more info. Error: %s", err.Error())
		logs.ErrIfErrf(err, "Failed executing %s command by @s", input, u.GetNickName())
		return false, errs.Wrap(err, "Error getting command from user input.")
	}

	commandParams, err := u.GetCommandParams(chatServer, input, userCommand)
	if err != nil {
		u.SetOutgoingf("Error executing your command. %s", err)
		return false, errs.Wrap(err, "Couldn't get command params")
	}

	if err = userCommand.Execute(*commandParams); err != nil {
		logs.ErrIfErrf(err, "error \t @%s command=%s error=%s", u.GetNickName(), input)
		return false, errs.Wrapf(err, "Couldn't execute command %q.", input)
	}

	logs.Infof("User @%s executed command: %s", u.GetNickName(), input)<commit_msg>Add formatting to error<commit_after>package chat

import (
	"github.com/spring1843/chat-server/src/plugins/command"
	"github.com/spring1843/chat-server/src/shared/logs"
	"github.com/spring1843/pomain/src/shared/errs"
)

// HandleNewInput interprets user input and lets chatServer handle it
func (u *User) HandleNewInput(chatServer *Server, userInput string) (bool, error) {
	if u.GetNickName() == "" {
		// This is from a user who is not identified yet, we do not do anything
		// about his input
		logs.Infof("Unidentified user sent input %q", userInput)
		u.SetIncoming(userInput)
		return true, nil
	}

	if command.IsInputExecutable(userInput) {
		return u.handleCommandInput(chatServer, userInput)
	}

	// If it's not a command it's a chat message to broadcast into the channel
	if u.GetChannel() == "" {
		u.SetOutgoing("You need to join a channel, use /join #channel or use /help for more info.")
		return false, errs.New("User is not in a channel, and input is not a command")
	}
	return u.handleBroadCastInput(chatServer, userInput)
}

func (u *User) handleBroadCastInput(chatServer *Server, userInput string) (bool, error) {
	channel, err := chatServer.GetChannel(u.GetChannel())
	if err != nil {
		return false, errs.Wrap(err, "Error getting channel from server")
	}
	channel.Broadcast(chatServer, `@`+u.GetNickName()+`: `+userInput)
	return true, nil
}

func (u *User) handleCommandInput(chatServer *Server, input string) (bool, error) {
	userCommand, err := command.FromString(input)
	if err != nil {
		u.SetOutgoingf("Invalid command, use /help for more info. Error: %s", err.Error())
		logs.ErrIfErrf(err, "Failed executing %s command by @%s", input, u.GetNickName())
		return false, errs.Wrap(err, "Error getting command from user input.")
	}

	commandParams, err := u.GetCommandParams(chatServer, input, userCommand)
	if err != nil {
		u.SetOutgoingf("Error executing your command. %s", err)
		return false, errs.Wrap(err, "Couldn't get command params")
	}

	if err = userCommand.Execute(*commandParams); err != nil {
		logs.ErrIfErrf(err, "error \t @%s command=%s error=%s", u.GetNickName(), input)
		return false, errs.Wrapf(err, "Couldn't execute command %q.", input)
	}

	logs.Infof("User @%s executed command: %s", u.GetNickName(), input)
{'fraction_non_alphanumeric': 0.13284542027056997, 'fraction_numerical': 0.0053226879574184965, 'mean_word_length': 4.665829145728643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2189340', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1481, 'n_words': 458}