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