-
Notifications
You must be signed in to change notification settings - Fork 0
/
gochat.go
235 lines (194 loc) · 4.9 KB
/
gochat.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package gochat
import (
"bufio"
"errors"
"fmt"
"net"
"strings"
)
// IRC client config type used during the connection process
type ClientCfg struct {
Network string
Nick string
}
// IRC client type with in/out data channels
type Client struct {
Config ClientCfg
Channels map[string]*Channel
In chan string
Out chan string
conn net.Conn
}
// IRC client channel type with data channels
type Channel struct {
client *Client
Topic chan string
In chan string
Out chan string
}
// IRC message
type Message struct {
Raw string
Prefix string
Command string
Params string
}
// Construct a new Client instance
func NewClient(network, nick string) *Client {
c := new(Client)
c.Config.Network = network
c.Config.Nick = nick
c.In = make(chan string)
c.Out = make(chan string)
c.Channels = make(map[string]*Channel)
return c
}
// Open a TCP connection to the specified server
func (c *Client) Connect() error {
var err error
c.conn, err = net.Dial("tcp", c.Config.Network)
if err != nil {
return err
}
go c.receiver()
go c.transmitter()
// Send NICK and USER messages to server
// USER message just re-uses NICK for now
c.Nick(c.Config.Nick)
c.User(c.Config.Nick, c.Config.Nick)
return nil
}
// Close the TCP connection
func (c *Client) Close() {
c.conn.Close()
close(c.In)
close(c.Out)
}
// Sends a NICK message to the server
func (c *Client) Nick(nick string) {
c.Out <- "NICK " + nick
}
func (c *Client) User(nick, name string) {
c.Out <- "USER " + nick + " 0 * :" + name
}
// Join an IRC channel
func (c *Client) Join(name string) *Channel {
name = strings.ToUpper(name)
channel := Channel{
client: c,
Topic: make(chan string),
In: make(chan string),
Out: make(chan string),
}
c.Channels[name] = &channel
c.Out <- "JOIN " + name
return &channel
}
// Receiver functionality for the IRC client
// Sends raw IRC messages to the parser
func (c *Client) receiver() {
reader := bufio.NewReader(c.conn)
for {
data, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading from connection!")
return
}
go func() {
message, err := c.ParseMessage(data)
if err != nil {
fmt.Println(err)
} else {
switch {
case message.Command == "332":
params := message.Params
var msgto, topic string
// Find the target and advance the params past it
if index := strings.Index(params, " "); index != -1 {
params = params[index+1:]
} else {
fmt.Println("Could not get RPL_TOPIC target")
}
// Find the msgto. This is the channel name.
if index := strings.Index(params, " "); index != -1 {
msgto = strings.ToUpper(params[:index])
params = params[index+1:]
} else {
fmt.Println("Could not get RPL_TOPIC msgto")
}
// Find the topic text
if index := strings.Index(message.Params, ":"); index != -1 {
topic = params[index+1:]
} else {
fmt.Println("Could not get RPL_TOPIC text")
}
// Find the channel to send the topic to
if channel, ok := c.Channels[msgto]; ok == true {
channel.Topic <- topic
} else {
fmt.Printf("Could not find channel %v for RPL_TOPIC\n", msgto)
}
case message.Command == "PRIVMSG":
var target, text string
if index := strings.Index(message.Params, " "); index != -1 {
target = strings.ToUpper(message.Params[:index])
} else {
fmt.Println("Could not get PRIVMSG target")
}
if index := strings.Index(message.Params, ":"); index != -1 {
text = message.Params[index+1:]
} else {
fmt.Println("Could not get PRIVMSG text")
}
if channel, ok := c.Channels[target]; ok == true {
channel.Out <- text
} else {
fmt.Println("Could not find channel")
}
case message.Command == "PING":
c.pong(message.Params)
default:
fmt.Printf("%v %v\n", message.Prefix, message.Command)
}
}
}()
}
}
// Transmitter functionality for the IRC client
// Sends raw IRC messages to the server
func (c *Client) transmitter() {
writer := bufio.NewWriter(c.conn)
for message := range c.Out {
writer.WriteString(message + "\n")
writer.Flush()
}
}
// Creates and sends a PONG message
func (c *Client) pong(s string) {
c.Out <- "PONG " + s
}
// Parses raw IRC messages
func (c *Client) ParseMessage(data string) (*Message, error) {
if len(data) == 0 {
return nil, errors.New("Empty IRC message!")
}
message := Message{Raw: data}
if data[0] == ':' {
if end := strings.Index(data, " "); end != -1 {
message.Prefix = data[1:end]
data = data[end+1:]
} else {
return nil, errors.New("Expected a command or parameter after the prefix!")
}
}
if end := strings.IndexAny(data, " \n"); end != -1 {
message.Command = strings.ToUpper(data[:end])
data = data[end+1:]
} else {
return nil, errors.New("IRC message not terminated")
}
if len(data) > 0 {
message.Params = data
}
return &message, nil
}