-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrequest_write.go
110 lines (92 loc) · 2.41 KB
/
request_write.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package seabird
import (
"errors"
"fmt"
"strings"
irc "gopkg.in/irc.v3"
)
// Reply to a Request with a convenience wrapper around fmt.Sprintf.
func (r *Request) Replyf(format string, v ...interface{}) error {
if len(r.Message.Params) < 1 || len(r.Message.Params[0]) < 1 {
return errors.New("Invalid IRC message")
}
target := r.Message.Prefix.Name
if r.FromChannel() {
target = r.Message.Params[0]
}
fullMsg := fmt.Sprintf(format, v...)
for _, resp := range strings.Split(fullMsg, "\n") {
r.WriteMessage(&irc.Message{
Prefix: &irc.Prefix{},
Command: "PRIVMSG",
Params: []string{
target,
resp,
},
})
}
return nil
}
// MentionReply acts the same as Bot.Reply but it will prefix it with the user's
// nick if we are in a channel.
func (r *Request) MentionReplyf(format string, v ...interface{}) error {
if len(r.Message.Params) < 1 || len(r.Message.Params[0]) < 1 {
return errors.New("Invalid IRC message")
}
target := r.Message.Prefix.Name
prefix := ""
if r.FromChannel() {
target = r.Message.Params[0]
prefix = r.Message.Prefix.Name + ": "
}
fullMsg := fmt.Sprintf(format, v...)
for _, resp := range strings.Split(fullMsg, "\n") {
r.WriteMessage(&irc.Message{
Prefix: &irc.Prefix{},
Command: "PRIVMSG",
Params: []string{
target,
prefix + resp,
},
})
}
return nil
}
// PrivateReply is similar to Reply, but it will always send privately.
func (r *Request) PrivateReplyf(format string, v ...interface{}) {
r.WriteMessage(&irc.Message{
Prefix: &irc.Prefix{},
Command: "PRIVMSG",
Params: []string{
r.Message.Prefix.Name,
fmt.Sprintf(format, v...),
},
})
}
// CTCPReply is a convenience function to respond to CTCP requests.
func (r *Request) CTCPReplyf(format string, v ...interface{}) error {
if r.Message.Command != "CTCP" {
return errors.New("Invalid CTCP message")
}
r.WriteMessage(&irc.Message{
Prefix: &irc.Prefix{},
Command: "NOTICE",
Params: []string{
r.Message.Prefix.Name,
fmt.Sprintf(format, v...),
},
})
return nil
}
// Send is a simple function to send an IRC event.
func (r *Request) WriteMessage(m *irc.Message) {
r.bot.WriteMessage(m)
}
// Write will write an raw IRC message to the stream.
func (r *Request) Write(line string) {
r.bot.Write(line)
}
// Writef is a convenience method around fmt.Sprintf and Bot.Write.
func (r *Request) Writef(format string, args ...interface{}) {
r.bot.Writef(format, args...)
}