2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-10-23 12:24:02 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2017-10-05 15:47:43 +02:00
|
|
|
package utils
|
2016-10-23 12:24:02 +02:00
|
|
|
|
2019-05-20 08:56:49 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
2020-02-19 01:38:42 +01:00
|
|
|
"fmt"
|
2019-05-20 08:56:49 +02:00
|
|
|
"strings"
|
2020-02-19 01:38:42 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
IRCv3TimestampFormat = "2006-01-02T15:04:05.000Z"
|
2019-05-20 08:56:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidParams = errors.New("Invalid parameters")
|
|
|
|
)
|
|
|
|
|
2017-10-05 15:47:43 +02:00
|
|
|
// ArgsToStrings takes the arguments and splits them into a series of strings,
|
2016-10-23 12:24:02 +02:00
|
|
|
// each argument separated by delim and each string bounded by maxLength.
|
2017-10-05 15:47:43 +02:00
|
|
|
func ArgsToStrings(maxLength int, arguments []string, delim string) []string {
|
2016-10-23 12:24:02 +02:00
|
|
|
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
|
|
|
|
}
|
2019-05-20 08:56:49 +02:00
|
|
|
|
|
|
|
func StringToBool(str string) (result bool, err error) {
|
|
|
|
switch strings.ToLower(str) {
|
2020-02-19 06:54:42 +01:00
|
|
|
case "on", "true", "t", "yes", "y", "enabled":
|
2019-05-20 08:56:49 +02:00
|
|
|
result = true
|
2020-02-19 06:54:42 +01:00
|
|
|
case "off", "false", "f", "no", "n", "disabled":
|
2019-05-20 08:56:49 +02:00
|
|
|
result = false
|
|
|
|
default:
|
|
|
|
err = ErrInvalidParams
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-12-03 03:13:09 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2020-02-19 01:38:42 +01:00
|
|
|
|
|
|
|
type IncompatibleSchemaError struct {
|
|
|
|
CurrentVersion string
|
|
|
|
RequiredVersion string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *IncompatibleSchemaError) Error() string {
|
|
|
|
return fmt.Sprintf("Database requires update. Expected schema v%s, got v%s", err.RequiredVersion, err.CurrentVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NanoToTimestamp(nanotime int64) string {
|
2020-02-20 09:06:24 +01:00
|
|
|
return time.Unix(0, nanotime).UTC().Format(IRCv3TimestampFormat)
|
2020-02-19 01:38:42 +01:00
|
|
|
}
|
2020-03-19 22:09:52 +01:00
|
|
|
|
|
|
|
func BoolDefaultTrue(value *bool) bool {
|
|
|
|
if value != nil {
|
|
|
|
return *value
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|