/
simple.go
54 lines (42 loc) · 1.16 KB
/
simple.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
package irccommand
import (
"awesome-dragon.science/go/irc/event"
"github.com/ergochat/irc-go/ircmsg"
)
// SimpleHandler is a wrapper around Handler that works with the raw ircmsg.Message directly.
type SimpleHandler struct {
*Handler
}
func (h *SimpleHandler) init() {
if h.Handler == nil {
h.Handler = new(Handler)
}
}
// OnMessage calls Handler.OnMessage
func (h *SimpleHandler) OnMessage(msg *event.Message) error {
h.init()
return h.Handler.OnMessage(msg)
}
// AddCallback adds a callback to the SimpleHandler instance
func (h *SimpleHandler) AddCallback(command string, callback func(*ircmsg.Message) error) int {
h.init()
return h.Handler.AddCallback(command, func(m *event.Message) error {
return callback(m.Raw)
})
}
// RemoveCallback calls Handler.RemoveCallback
func (h *SimpleHandler) RemoveCallback(id int) {
h.init()
h.Handler.RemoveCallback(id)
}
// WaitFor waits for the specified IRC command, and sends the
func (h *SimpleHandler) WaitFor(command string) <-chan *ircmsg.Message {
h.init()
outChan := make(chan *ircmsg.Message)
c := h.Handler.WaitFor(command)
go func() {
outChan <- (<-c).Raw
close(outChan)
}()
return outChan
}