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.

41 lines
1.2 KiB

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