3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 13:29:27 +01:00

Merge pull request #1474 from slingamn/issue1472_help_nick

fix #1472
This commit is contained in:
Shivaram Lingamneni 2020-12-30 02:31:06 -05:00 committed by GitHub
commit 0db4b42ffe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 59 deletions

View File

@ -1015,16 +1015,17 @@ func extjwtHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Re
}
// HELP [<query>]
// HELPOP [<query>]
func helpHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *ResponseBuffer) bool {
argument := strings.ToLower(strings.TrimSpace(strings.Join(msg.Params, " ")))
if len(argument) < 1 {
if len(msg.Params) == 0 {
client.sendHelp("HELPOP", client.t(`HELPOP <argument>
Get an explanation of <argument>, or "index" for a list of help topics.`), rb)
return false
}
argument := strings.ToLower(strings.TrimSpace(msg.Params[0]))
// handle index
if argument == "index" {
client.sendHelp("HELP", server.helpIndexManager.GetIndex(client.Languages(), client.HasMode(modes.Operator)), rb)
@ -1035,14 +1036,12 @@ Get an explanation of <argument>, or "index" for a list of help topics.`), rb)
if exists && (!helpHandler.oper || (helpHandler.oper && client.HasMode(modes.Operator))) {
if helpHandler.textGenerator != nil {
client.sendHelp(strings.ToUpper(argument), helpHandler.textGenerator(client), rb)
client.sendHelp(argument, helpHandler.textGenerator(client), rb)
} else {
client.sendHelp(strings.ToUpper(argument), client.t(helpHandler.text), rb)
client.sendHelp(argument, client.t(helpHandler.text), rb)
}
} else {
args := msg.Params
args = append(args, client.t("Help not found"))
rb.Add(nil, server.name, ERR_HELPNOTFOUND, args...)
rb.Add(nil, server.name, ERR_HELPNOTFOUND, client.Nick(), strings.ToUpper(utils.SafeErrorParam(argument)), client.t("Help not found"))
}
return false
@ -1865,7 +1864,7 @@ func monitorListHandler(server *Server, client *Client, msg ircmsg.IrcMessage, r
nickList = append(nickList, replynick)
}
for _, line := range utils.ArgsToStrings(maxLastArgLength, nickList, ",") {
for _, line := range utils.BuildTokenLines(maxLastArgLength, nickList, ",") {
rb.Add(nil, server.name, RPL_MONLIST, nick, line)
}
@ -1891,12 +1890,12 @@ func monitorStatusHandler(server *Server, client *Client, msg ircmsg.IrcMessage,
}
if len(online) > 0 {
for _, line := range utils.ArgsToStrings(maxLastArgLength, online, ",") {
for _, line := range utils.BuildTokenLines(maxLastArgLength, online, ",") {
rb.Add(nil, server.name, RPL_MONONLINE, client.Nick(), line)
}
}
if len(offline) > 0 {
for _, line := range utils.ArgsToStrings(maxLastArgLength, offline, ",") {
for _, line := range utils.BuildTokenLines(maxLastArgLength, offline, ",") {
rb.Add(nil, server.name, RPL_MONOFFLINE, client.Nick(), line)
}
}

View File

@ -752,22 +752,19 @@ func (hm *HelpIndexManager) GenerateIndices(lm *languages.Manager) {
}
// sendHelp sends the client help of the given string.
func (client *Client) sendHelp(name string, text string, rb *ResponseBuffer) {
splitName := strings.Split(name, " ")
func (client *Client) sendHelp(helpEntry string, text string, rb *ResponseBuffer) {
helpEntry = strings.ToUpper(helpEntry)
nick := client.Nick()
textLines := strings.Split(text, "\n")
for i, line := range textLines {
args := splitName
args = append(args, line)
if i == 0 {
rb.Add(nil, client.server.name, RPL_HELPSTART, args...)
rb.Add(nil, client.server.name, RPL_HELPSTART, nick, helpEntry, line)
} else {
rb.Add(nil, client.server.name, RPL_HELPTXT, args...)
rb.Add(nil, client.server.name, RPL_HELPTXT, nick, helpEntry, line)
}
}
args := splitName
args = append(args, client.t("End of /HELPOP"))
rb.Add(nil, client.server.name, RPL_ENDOFHELP, args...)
rb.Add(nil, client.server.name, RPL_ENDOFHELP, nick, helpEntry, client.t("End of /HELPOP"))
}
// GetHelpIndex returns the help index for the given language.

View File

@ -18,37 +18,6 @@ var (
ErrInvalidParams = errors.New("Invalid parameters")
)
// ArgsToStrings takes the arguments and splits them into a series of strings,
// each argument separated by delim and each string bounded by maxLength.
func ArgsToStrings(maxLength int, arguments []string, delim string) []string {
var messages []string
var buffer string
for {
if len(arguments) < 1 {
break
}
if len(buffer) > 0 && maxLength < len(buffer)+len(delim)+len(arguments[0]) {
messages = append(messages, buffer)
buffer = ""
continue
}
if len(buffer) > 0 {
buffer += delim
}
buffer += arguments[0]
arguments = arguments[1:]
}
if len(buffer) > 0 {
messages = append(messages, buffer)
}
return messages
}
func StringToBool(str string) (result bool, err error) {
switch strings.ToLower(str) {
case "on", "true", "t", "yes", "y", "enabled":

View File

@ -5,14 +5,6 @@ package utils
import "testing"
func TestArgsToStrings(t *testing.T) {
val := ArgsToStrings(512, []string{"a", "b", "c"}, ",")
assertEqual(val, []string{"a,b,c"}, t)
val = ArgsToStrings(10, []string{"abcd", "efgh", "ijkl"}, ",")
assertEqual(val, []string{"abcd,efgh", "ijkl"}, t)
}
func TestStringToBool(t *testing.T) {
val, err := StringToBool("on")
assertEqual(val, true, t)

View File

@ -132,3 +132,14 @@ func (t *TokenLineBuilder) Lines() (result []string) {
}
return
}
// BuildTokenLines is a convenience to apply TokenLineBuilder to a predetermined
// slice of tokens.
func BuildTokenLines(lineLen int, tokens []string, delim string) []string {
var tl TokenLineBuilder
tl.Initialize(lineLen, delim)
for _, arg := range tokens {
tl.Add(arg)
}
return tl.Lines()
}

View File

@ -35,3 +35,11 @@ func TestTokenLineBuilder(t *testing.T) {
t.Errorf("text incorrectly split into lines: %s instead of %s", joined, monteCristo)
}
}
func TestBuildTokenLines(t *testing.T) {
val := BuildTokenLines(512, []string{"a", "b", "c"}, ",")
assertEqual(val, []string{"a,b,c"}, t)
val = BuildTokenLines(10, []string{"abcd", "efgh", "ijkl"}, ",")
assertEqual(val, []string{"abcd,efgh", "ijkl"}, t)
}