request.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // This file is subject to a 1-clause BSD license.
  2. // Its contents can be found in the enclosed LICENSE file.
  3. package irc
  4. import (
  5. "fmt"
  6. "strings"
  7. )
  8. // RequestFunc defines a handler for a request binding.
  9. type RequestFunc func(ResponseWriter, *Request)
  10. // Request defines a single incoming message from a server.
  11. type Request struct {
  12. SenderName string // Nick name of sender.
  13. SenderMask string // Hostmask of sender.
  14. Type string // Type of message: "001", "PRIVMSG", "PING", etc.
  15. Target string // Receiver of reply.
  16. Data string // Message content.
  17. }
  18. // FromChannel returns true if this request came from a channel context
  19. // instead of a user or service.
  20. func (r *Request) FromChannel() bool {
  21. if len(r.Target) == 0 {
  22. return false
  23. }
  24. c := r.Target[0]
  25. return c == '#' || c == '&' || c == '!' || c == '+'
  26. }
  27. // Fields returns the message payload, but skips the first n words.
  28. // The result is returned as a slice of individual words.
  29. func (r *Request) Fields(n int) []string {
  30. words := strings.Fields(r.Data)
  31. if n < 0 || n >= len(words) {
  32. return nil
  33. }
  34. return words[n:]
  35. }
  36. // String returns a string representation of the request data.
  37. func (r *Request) String() string {
  38. return fmt.Sprintf("%s %s %s %s %s",
  39. r.SenderMask, r.SenderName, r.Type, r.Target, r.Data)
  40. }
  41. // IsPrivMsg returns true if the request comes from either a user or
  42. // a channel, as a PRIVMSG. This has its own method, because it is a
  43. // commonly used filter.
  44. func (r *Request) IsPrivMsg() bool { return r.Type == "PRIVMSG" }