forked from thoj/go-ircevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.go
205 lines (187 loc) · 4.77 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2009 Thomas Jager <[email protected]> All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package irc
import (
"bufio"
"fmt"
"net"
"strings"
"time"
)
const (
VERSION = "GolangBOT v1.0"
)
var error_ bool
func reader(irc *IRCConnection) {
br := bufio.NewReader(irc.socket)
for !error_ {
msg, err := br.ReadString('\n')
if err != nil {
irc.Error <- err
break
}
irc.lastMessage = time.Now()
msg = msg[0 : len(msg)-2] //Remove \r\n
event := &IRCEvent{Raw: msg}
if msg[0] == ':' {
if i := strings.Index(msg, " "); i > -1 {
event.Source = msg[1:i]
msg = msg[i+1 : len(msg)]
} else {
fmt.Printf("Misformed msg from server: %#s\n", msg)
}
if i, j := strings.Index(event.Source, "!"), strings.Index(event.Source, "@"); i > -1 && j > -1 {
event.Nick = event.Source[0:i]
event.User = event.Source[i+1 : j]
event.Host = event.Source[j+1 : len(event.Source)]
}
}
args := strings.SplitN(msg, " :", 2)
if len(args) > 1 {
event.Message = args[1]
}
args = strings.Split(args[0], " ")
event.Code = strings.ToUpper(args[0])
if len(args) > 1 {
event.Arguments = args[1:len(args)]
}
irc.RunCallbacks(event)
}
irc.syncreader <- true
}
func writer(irc *IRCConnection) {
b, ok := <-irc.pwrite
for !error_ && ok {
if b == "" || irc.socket == nil {
break
}
_, err := irc.socket.Write([]byte(b))
if err != nil {
fmt.Printf("%s\n", err)
irc.Error <- err
break
}
b, ok = <-irc.pwrite
}
irc.syncwriter <- true
}
//Pings the server if we have not recived any messages for 5 minutes
func pinger(i *IRCConnection) {
i.ticker = time.Tick(1 * time.Minute) //Tick every minute.
i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
for {
select {
case <-i.ticker:
//Ping if we haven't recived anything from the server within 4 minutes
if time.Since(i.lastMessage) >= (4 * time.Minute) {
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
}
case <-i.ticker2:
//Ping every 15 minutes.
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
//Try to recapture nickname if it's not as configured.
if i.nick != i.nickcurrent {
i.nickcurrent = i.nick
i.SendRaw(fmt.Sprintf("NICK %s", i.nick))
}
}
}
}
func (irc *IRCConnection) Cycle() {
irc.SendRaw("QUIT")
irc.Reconnect()
}
func (irc *IRCConnection) Quit() {
irc.quitting = true
irc.SendRaw("QUIT")
}
func (irc *IRCConnection) Join(channel string) {
irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel)
}
func (irc *IRCConnection) Part(channel string) {
irc.pwrite <- fmt.Sprintf("PART %s\r\n", channel)
}
func (irc *IRCConnection) Notice(target, message string) {
irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message)
}
func (irc *IRCConnection) Privmsg(target, message string) {
irc.pwrite <- fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message)
}
func (irc *IRCConnection) SendRaw(message string) {
fmt.Printf("--> %s\n", message)
irc.pwrite <- fmt.Sprintf("%s\r\n", message)
}
func (i *IRCConnection) Reconnect() error {
close(i.pwrite)
close(i.pread)
<-i.syncreader
<-i.syncwriter
for {
fmt.Printf("Reconnecting to %s\n", i.server)
var err error
i.socket, err = net.Dial("tcp", i.server)
if err == nil {
break
}
fmt.Printf("Error: %s\n", err)
}
error_ = false
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
go reader(i)
go writer(i)
i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick)
i.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user)
return nil
}
func (i *IRCConnection) Loop() {
for !i.quitting {
e := <-i.Error
if i.quitting {
break
}
fmt.Printf("Error: %s\n", e)
error_ = true
i.Reconnect()
}
close(i.pwrite)
close(i.pread)
<-i.syncreader
<-i.syncwriter
}
func (i *IRCConnection) Connect(server string) error {
i.server = server
fmt.Printf("Connecting to %s\n", i.server)
var err error
i.socket, err = net.Dial("tcp", i.server)
if err != nil {
return err
}
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
i.pread = make(chan string, 100)
i.pwrite = make(chan string, 100)
i.Error = make(chan error, 10)
i.syncreader = make(chan bool)
i.syncwriter = make(chan bool)
go reader(i)
go writer(i)
go pinger(i)
i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick)
i.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user)
if len(i.Password) > 0 {
i.pwrite <- fmt.Sprintf("PASS %s\r\n", i.Password)
}
return nil
}
func IRC(nick, user string) *IRCConnection {
irc := new(IRCConnection)
irc.registered = false
irc.pread = make(chan string, 100)
irc.pwrite = make(chan string, 100)
irc.Error = make(chan error)
irc.nick = nick
irc.user = user
irc.VerboseCallbackHandler = true
irc.setupCallbacks()
return irc
}