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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"sync"
"time"
"github.com/go-redis/redis"
"gopkg.in/irc.v3"
)
var messageBeat chan bool
var firstMessage chan bool
var client *irc.Client
var safeLock sync.Mutex
func main() {
nick := flag.String("nick", "NONE", "the ircnick you want")
from := flag.String("ip", "[::1]", "src address")
flag.Parse()
localAddrDialier := &net.Dialer{
LocalAddr: &net.TCPAddr{
IP: net.ParseIP(*from),
Port: 0,
},
}
conn, err := tls.DialWithDialer(localAddrDialier, "tcp", "chat.freenode.net:6697", &tls.Config{})
if err != nil {
log.Fatalln(err)
}
messageBeat = make(chan bool)
firstMessage = make(chan bool, 10)
go ircKeepalive()
redisc := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("127.0.0.1:%d", 6379),
Password: "", // no password set
DB: 0, // use default DB
})
go func() {
for {
time.Sleep(time.Second)
r := redisc.Ping()
if r.Err() != nil {
redisc = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("127.0.0.1:%d", 6379),
Password: "", // no password set
DB: 0, // use default DB
})
}
redisc.Set(fmt.Sprintf("alive-%s", *nick), "yes", time.Second*5)
}
}()
if *nick == "NONE" {
log.Fatalf("You must set a nick")
}
go func() {
<-firstMessage
for {
psub := redisc.Subscribe(fmt.Sprintf("irc-%s", *nick))
for {
msg, err := psub.ReceiveMessage()
if err != nil {
break
}
client.WriteMessage(&irc.Message{
Command: "PRIVMSG",
Params: []string{
"##tvl-ebooks",
msg.Payload,
},
})
}
time.Sleep(time.Second * 10)
}
}()
go func() {
<-firstMessage
for {
psub := redisc.Subscribe(fmt.Sprintf("raw-irc-%s", *nick))
for {
msg, err := psub.ReceiveMessage()
if err != nil {
break
}
im := irc.Message{}
err = json.Unmarshal([]byte(msg.Payload), &im)
if err == nil {
client.WriteMessage(&im)
}
}
time.Sleep(time.Second * 10)
}
}()
seenMsgBefore := false
config := irc.ClientConfig{
Nick: *nick,
User: *nick,
Name: fmt.Sprintf("%s Ebooks", *nick),
Handler: irc.HandlerFunc(func(c *irc.Client, m *irc.Message) {
b, _ := json.Marshal(m)
log.Printf("%#v", string(b))
messageBeat <- true
if !seenMsgBefore {
firstMessage <- true
seenMsgBefore = true
}
res := redisc.Publish("ebook", string(b))
if res.Err() != nil {
log.Printf("Publish error! %#v", err)
}
if m.Command == "001" {
// 001 is a welcome event, so we join channels there
c.Write("JOIN ##tvl-ebooks")
}
// else if m.Command == "PRIVMSG" && c.FromChannel(m) {
// // // Create a handler on all messages.
// // c.WriteMessage(&irc.Message{
// // Command: "PRIVMSG",
// // Params: []string{
// // m.Params[0],
// // m.Trailing(),
// // },
// // })
// }
}),
}
// Create the client
client = irc.NewClient(conn, config)
err = client.Run()
if err != nil {
log.Fatalln(err)
}
}
func ircKeepalive() {
tt := time.NewTimer(time.Second)
lastPing := time.Now()
for {
select {
case <-tt.C:
if time.Since(lastPing) > time.Minute*5 {
log.Fatalf("It's been too long since the last IRC message, blowing up")
}
break
case <-messageBeat:
lastPing = time.Now()
}
}
}
|