-
Notifications
You must be signed in to change notification settings - Fork 1
/
irc.go
138 lines (125 loc) · 2.87 KB
/
irc.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package nopaste
import (
"fmt"
"log"
"net/http"
"time"
irc "github.com/thoj/go-ircevent"
)
const (
MsgBufferLen = 100
)
var (
IRCThrottleWindow = 1 * time.Second
)
type MessageChan interface {
PostNopaste(np nopasteContent, url string)
PostMsgr(np *http.Request)
}
type IRCMessage struct {
Channel string
Notice bool
Text string
}
type IRCMessageChan chan IRCMessage
func (ch IRCMessageChan) PostNopaste(np nopasteContent, url string) {
summary := np.Summary
nick := np.Nick
msg := IRCMessage{
Channel: np.Channel,
Text: fmt.Sprintf("%s %s %s", nick, summary, url),
Notice: false,
}
if np.Notice != "" {
// true if 'notice' argument has any value (includes '0', 'false', 'null'...)
msg.Notice = true
}
select {
case ch <- msg:
default:
log.Println("[warn] Can't send msg to IRC")
}
}
func (ch IRCMessageChan) PostMsgr(req *http.Request) {
msg := IRCMessage{
Channel: req.FormValue("channel"),
Text: req.FormValue("msg"),
Notice: true,
}
if _notice := req.FormValue("notice"); _notice == "" || _notice == "0" {
msg.Notice = false
}
select {
case ch <- msg:
default:
log.Println("[warn] Can't send msg to IRC")
}
}
func RunIRCAgent(c *Config, ch chan IRCMessage) {
log.Println("[info] running irc agent")
for {
agent := irc.IRC(c.IRC.Nick, c.IRC.Nick)
agent.UseTLS = c.IRC.Secure
agent.Password = c.IRC.Password
addr := fmt.Sprintf("%s:%d", c.IRC.Host, c.IRC.Port)
err := agent.Connect(addr)
if err != nil {
log.Println("[warn]", err)
time.Sleep(10 * time.Second)
continue
}
done := make(chan interface{})
go sendMsgToIRC(c, agent, ch, done)
agent.Loop()
close(done)
time.Sleep(10 * time.Second)
}
}
func sendMsgToIRC(c *Config, agent *irc.Connection, ch chan IRCMessage, done chan interface{}) {
joined := make(map[string]chan IRCMessage)
for {
select {
case <-done:
return
case msg := <-ch:
if _, ok := joined[msg.Channel]; !ok {
log.Println("[info] join", msg.Channel)
agent.Join(msg.Channel)
joined[msg.Channel] = make(chan IRCMessage, MsgBufferLen)
go sendMsgToIRCChannel(agent, joined[msg.Channel], done)
c.AddChannel(msg.Channel)
}
select {
case joined[msg.Channel] <- msg:
default:
log.Println("[warn] Can't send msg to IRC. Channel buffer flooding.")
}
}
}
}
func sendMsgToIRCChannel(agent *irc.Connection, ch chan IRCMessage, done chan interface{}) {
lastPostedAt := time.Now()
for {
select {
case <-done:
return
case msg := <-ch:
throttle(lastPostedAt, IRCThrottleWindow)
if msg.Notice {
agent.Notice(msg.Channel, msg.Text)
} else {
agent.Privmsg(msg.Channel, msg.Text)
}
lastPostedAt = time.Now()
}
}
}
func throttle(last time.Time, window time.Duration) {
now := time.Now()
diff := now.Sub(last)
if diff < window {
// throttle
log.Println("[info] throttled. sleeping", window-diff)
time.Sleep(window - diff)
}
}