3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-28 05:32:49 +01:00
ergo/irc/utils/args.go
Shivaram Lingamneni aa8579b6e8 Assorted fixes
* Fix #679 (borked reply to `JOIN #chan,\r\n`)
* Replace invalid error parameters with *'s in various places
* Fix PART with no message sending an empty trailing parameter to the channel
* Fix some error responses not getting labeled
2019-12-02 21:13:09 -05:00

66 lines
1.3 KiB
Go

// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package utils
import (
"errors"
"strings"
)
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) > 1 {
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":
result = true
case "off", "false", "f", "no", "n":
result = false
default:
err = ErrInvalidParams
}
return
}
// Checks that a parameter can be passed as a non-trailing, and returns "*"
// if it can't. See #697.
func SafeErrorParam(param string) string {
if param == "" || param[0] == ':' || strings.IndexByte(param, ' ') != -1 {
return "*"
}
return param
}