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.

39 lines
1.2 KiB

  1. package types
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. // A Command is something that a user invokes by sending a message starting with '!'
  7. // followed by a list of strings that name the command, followed by a list of argument
  8. // strings. The argument strings may be quoted using '\"' and '\'' in the same way
  9. // that they are quoted in the unix shell.
  10. type Command struct {
  11. Path []string
  12. Arguments []string
  13. Help string
  14. Command func(roomID, userID string, arguments []string) (content interface{}, err error)
  15. }
  16. // An Expansion is something that actives when the user sends any message
  17. // containing a string matching a given pattern. For example an RFC expansion
  18. // might expand "RFC 6214" into "Adaptation of RFC 1149 for IPv6" and link to
  19. // the appropriate RFC.
  20. type Expansion struct {
  21. Regexp *regexp.Regexp
  22. Expand func(roomID, userID string, matchingGroups []string) interface{}
  23. }
  24. // Matches if the arguments start with the path of the command.
  25. func (command *Command) Matches(arguments []string) bool {
  26. if len(arguments) < len(command.Path) {
  27. return false
  28. }
  29. for i, segment := range command.Path {
  30. if strings.ToLower(segment) != strings.ToLower(arguments[i]) {
  31. return false
  32. }
  33. }
  34. return true
  35. }