You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package utils
  4. import (
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "time"
  9. )
  10. const (
  11. IRCv3TimestampFormat = "2006-01-02T15:04:05.000Z"
  12. )
  13. var (
  14. ErrInvalidParams = errors.New("Invalid parameters")
  15. )
  16. // ArgsToStrings takes the arguments and splits them into a series of strings,
  17. // each argument separated by delim and each string bounded by maxLength.
  18. func ArgsToStrings(maxLength int, arguments []string, delim string) []string {
  19. var messages []string
  20. var buffer string
  21. for {
  22. if len(arguments) < 1 {
  23. break
  24. }
  25. if len(buffer) > 0 && maxLength < len(buffer)+len(delim)+len(arguments[0]) {
  26. messages = append(messages, buffer)
  27. buffer = ""
  28. continue
  29. }
  30. if len(buffer) > 1 {
  31. buffer += delim
  32. }
  33. buffer += arguments[0]
  34. arguments = arguments[1:]
  35. }
  36. if len(buffer) > 0 {
  37. messages = append(messages, buffer)
  38. }
  39. return messages
  40. }
  41. func StringToBool(str string) (result bool, err error) {
  42. switch strings.ToLower(str) {
  43. case "on", "true", "t", "yes", "y", "enabled":
  44. result = true
  45. case "off", "false", "f", "no", "n", "disabled":
  46. result = false
  47. default:
  48. err = ErrInvalidParams
  49. }
  50. return
  51. }
  52. // Checks that a parameter can be passed as a non-trailing, and returns "*"
  53. // if it can't. See #697.
  54. func SafeErrorParam(param string) string {
  55. if param == "" || param[0] == ':' || strings.IndexByte(param, ' ') != -1 {
  56. return "*"
  57. }
  58. return param
  59. }
  60. type IncompatibleSchemaError struct {
  61. CurrentVersion string
  62. RequiredVersion string
  63. }
  64. func (err *IncompatibleSchemaError) Error() string {
  65. return fmt.Sprintf("Database requires update. Expected schema v%s, got v%s", err.RequiredVersion, err.CurrentVersion)
  66. }
  67. func NanoToTimestamp(nanotime int64) string {
  68. return time.Unix(0, nanotime).UTC().Format(IRCv3TimestampFormat)
  69. }