-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
223 lines (207 loc) · 4.49 KB
/
server.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
package etcdircd
import (
"fmt"
"net"
"sync"
"time"
etcd "github.com/coreos/etcd/clientv3"
"github.com/golang/glog"
"gopkg.in/sorcix/irc.v2"
)
type Server struct {
ln net.Listener
conns map[*Conn]struct{}
created time.Time
cli *etcd.Client
ses *etcdSession
cfg Config
hostPfx irc.Prefix
}
func NewServer(cfg Config, ln net.Listener, cli *etcd.Client) *Server {
s := &Server{
ln: ln,
cli: cli,
ses: newEtcdSession(cli),
conns: make(map[*Conn]struct{}),
created: time.Now(),
cfg: cfg,
}
s.hostPfx = irc.Prefix{Name: s.cfg.HostName}
return s
}
func (s *Server) Close() {
s.ses.Close()
}
func (s *Server) Serve() (err error) {
defer s.Close()
var wg sync.WaitGroup
for {
c, cerr := s.ln.Accept()
if cerr != nil {
err = cerr
glog.V(9).Infof("server shutdown: %v", err)
break
}
wg.Add(1)
go func() {
defer func() {
c.Close()
wg.Done()
}()
s.handleConn(c)
}()
}
wg.Wait()
return err
}
func (s *Server) handleConn(c net.Conn) {
cn := NewConn(c)
cr := s.readConnRequest(cn)
if cr == nil {
return
}
cli, err := s.handshake(cn, cr)
if err != nil {
return
}
defer func() {
cli.Close()
glog.V(9).Infof("disconnected nick %v", cr.nick)
}()
for {
var err error
select {
case msg := <-cn.Reader():
switch msg.Command {
case irc.MOTD:
err = s.motd(cr.nick, cli)
default:
err = ClientDo(cli, msg)
}
case err = <-cn.Err():
case <-s.cli.Ctx().Done():
err = s.cli.Ctx().Err()
}
if err != nil {
glog.V(7).Infof("err %v", err)
return
}
}
}
type connectRequest struct {
nick string
user []string
}
func (s *Server) handshake(cn *Conn, cr *connectRequest) (Client, error) {
var cli Client
nn, hn, version := s.cfg.NetworkName, s.cfg.HostName, "etcdircd-0.0.0"
for {
newCli, err := newEtcdClient(s, cn, cr)
if newCli != nil {
cli = newCli
break
}
if err != nil {
return nil, err
}
msg := irc.Message{
Prefix: &s.hostPfx,
Command: irc.ERR_NICKNAMEINUSE,
Params: []string{cr.nick},
}
cn.SendMsg(s.cli.Ctx(), msg)
select {
case rmsg := <-cn.Reader():
switch rmsg.Command {
case "CAP":
case "NICK":
cr.nick = rmsg.Params[0]
default:
return nil, fmt.Errorf("bad handshake: %+v", rmsg)
}
case <-s.cli.Ctx().Done():
return nil, s.cli.Ctx().Err()
}
}
// User[2] is the host the client wants to access.
nnpfx := &irc.Prefix{Name: nn}
cli.SendMsg(s.cli.Ctx(), irc.Message{
Prefix: nnpfx,
Command: irc.RPL_WELCOME,
Params: []string{
cr.nick,
"Welcome to an Internet Relay Network " + cr.nick + "!" + cr.user[1] + "@masked",
},
})
cli.SendMsg(s.cli.Ctx(), irc.Message{
Prefix: nnpfx,
Command: irc.RPL_YOURHOST,
Params: []string{cr.nick, "Your host is " + hn + ", running version " + version},
})
date := s.created.Format("Mon Jan 2 15:04:05 -0700 MST 2006")
// 01:23:45 Jan 01 1900
cli.SendMsg(s.cli.Ctx(), irc.Message{
Prefix: nnpfx,
Command: irc.RPL_CREATED,
Params: []string{cr.nick, "This server was created " + date},
})
// cli modes; channel modes
cli.SendMsg(s.cli.Ctx(), irc.Message{
Prefix: nnpfx,
Command: irc.RPL_MYINFO,
Params: []string{cr.nick, nn + " " + version + " iE iE"},
})
if s.cfg.Motd != "" {
s.motd(cr.nick, cli)
}
cli.Send(s.cli.Ctx(), "PING", "hello")
return cli, s.cli.Ctx().Err()
}
func (s *Server) motd(nick string, cli Client) error {
if s.cfg.Motd == "" {
return cli.SendMsg(s.cli.Ctx(), irc.Message{
Prefix: &s.hostPfx,
Command: irc.ERR_NOMOTD,
Params: []string{nick, "MOTD file is missing"},
})
}
cli.SendMsg(s.cli.Ctx(), irc.Message{
Prefix: &s.hostPfx,
Command: irc.RPL_MOTDSTART,
Params: []string{nick, "- " + s.cfg.HostName + " Message of the day - "},
})
cli.SendMsg(s.cli.Ctx(), irc.Message{
Prefix: &s.hostPfx,
Command: irc.RPL_MOTD,
Params: []string{nick, s.cfg.Motd},
})
return cli.SendMsg(s.cli.Ctx(), irc.Message{
Prefix: &s.hostPfx,
Command: irc.RPL_ENDOFMOTD,
Params: []string{nick, "End of MOTD command"},
})
}
func (s *Server) readConnRequest(c *Conn) *connectRequest {
cr := &connectRequest{}
gotCap := false
for msg := range c.Reader() {
switch msg.Command {
case "CAP":
gotCap = gotCap || msg.Params[0] == "LS"
case "NICK":
cr.nick = msg.Params[0]
case "USER":
cr.user = msg.Params
}
if cr.nick != "" && cr.user != nil {
if gotCap {
c.Writer() <- irc.Message{
Command: ":servname CAP",
Params: []string{cr.nick, "LS", "[]"},
}
}
return cr
}
}
return nil
}