[101] | 1 | package soju
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
[218] | 4 | "fmt"
|
---|
[101] | 5 | "time"
|
---|
[103] | 6 |
|
---|
| 7 | "gopkg.in/irc.v3"
|
---|
[101] | 8 | )
|
---|
| 9 |
|
---|
[165] | 10 | type event interface{}
|
---|
| 11 |
|
---|
| 12 | type eventUpstreamMessage struct {
|
---|
[103] | 13 | msg *irc.Message
|
---|
| 14 | uc *upstreamConn
|
---|
| 15 | }
|
---|
| 16 |
|
---|
[218] | 17 | type eventUpstreamConnectionError struct {
|
---|
| 18 | net *network
|
---|
| 19 | err error
|
---|
| 20 | }
|
---|
| 21 |
|
---|
[196] | 22 | type eventUpstreamConnected struct {
|
---|
| 23 | uc *upstreamConn
|
---|
| 24 | }
|
---|
| 25 |
|
---|
[179] | 26 | type eventUpstreamDisconnected struct {
|
---|
| 27 | uc *upstreamConn
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[218] | 30 | type eventUpstreamError struct {
|
---|
| 31 | uc *upstreamConn
|
---|
| 32 | err error
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[165] | 35 | type eventDownstreamMessage struct {
|
---|
[103] | 36 | msg *irc.Message
|
---|
| 37 | dc *downstreamConn
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[166] | 40 | type eventDownstreamConnected struct {
|
---|
| 41 | dc *downstreamConn
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[167] | 44 | type eventDownstreamDisconnected struct {
|
---|
| 45 | dc *downstreamConn
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[253] | 48 | type networkHistory struct {
|
---|
| 49 | offlineClients map[string]uint64 // indexed by client name
|
---|
| 50 | ring *Ring // can be nil if there are no offline clients
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[101] | 53 | type network struct {
|
---|
| 54 | Network
|
---|
[202] | 55 | user *user
|
---|
| 56 | stopped chan struct{}
|
---|
[131] | 57 |
|
---|
[253] | 58 | conn *upstreamConn
|
---|
[267] | 59 | channels map[string]*Channel
|
---|
[253] | 60 | history map[string]*networkHistory // indexed by entity
|
---|
| 61 | offlineClients map[string]struct{} // indexed by client name
|
---|
| 62 | lastError error
|
---|
[101] | 63 | }
|
---|
| 64 |
|
---|
[267] | 65 | func newNetwork(user *user, record *Network, channels []Channel) *network {
|
---|
| 66 | m := make(map[string]*Channel, len(channels))
|
---|
| 67 | for _, ch := range channels {
|
---|
| 68 | m[ch.Name] = &ch
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[101] | 71 | return &network{
|
---|
[253] | 72 | Network: *record,
|
---|
| 73 | user: user,
|
---|
| 74 | stopped: make(chan struct{}),
|
---|
[267] | 75 | channels: m,
|
---|
[253] | 76 | history: make(map[string]*networkHistory),
|
---|
| 77 | offlineClients: make(map[string]struct{}),
|
---|
[101] | 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[218] | 81 | func (net *network) forEachDownstream(f func(*downstreamConn)) {
|
---|
| 82 | net.user.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 83 | if dc.network != nil && dc.network != net {
|
---|
| 84 | return
|
---|
| 85 | }
|
---|
| 86 | f(dc)
|
---|
| 87 | })
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[101] | 90 | func (net *network) run() {
|
---|
| 91 | var lastTry time.Time
|
---|
| 92 | for {
|
---|
[202] | 93 | select {
|
---|
| 94 | case <-net.stopped:
|
---|
| 95 | return
|
---|
| 96 | default:
|
---|
| 97 | // This space is intentionally left blank
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[101] | 100 | if dur := time.Now().Sub(lastTry); dur < retryConnectMinDelay {
|
---|
| 101 | delay := retryConnectMinDelay - dur
|
---|
| 102 | net.user.srv.Logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), net.Addr)
|
---|
| 103 | time.Sleep(delay)
|
---|
| 104 | }
|
---|
| 105 | lastTry = time.Now()
|
---|
| 106 |
|
---|
| 107 | uc, err := connectToUpstream(net)
|
---|
| 108 | if err != nil {
|
---|
| 109 | net.user.srv.Logger.Printf("failed to connect to upstream server %q: %v", net.Addr, err)
|
---|
[218] | 110 | net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to connect: %v", err)}
|
---|
[101] | 111 | continue
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | uc.register()
|
---|
[197] | 115 | if err := uc.runUntilRegistered(); err != nil {
|
---|
| 116 | uc.logger.Printf("failed to register: %v", err)
|
---|
[218] | 117 | net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to register: %v", err)}
|
---|
[197] | 118 | uc.Close()
|
---|
| 119 | continue
|
---|
| 120 | }
|
---|
[101] | 121 |
|
---|
[196] | 122 | net.user.events <- eventUpstreamConnected{uc}
|
---|
[165] | 123 | if err := uc.readMessages(net.user.events); err != nil {
|
---|
[101] | 124 | uc.logger.Printf("failed to handle messages: %v", err)
|
---|
[218] | 125 | net.user.events <- eventUpstreamError{uc, fmt.Errorf("failed to handle messages: %v", err)}
|
---|
[101] | 126 | }
|
---|
| 127 | uc.Close()
|
---|
[179] | 128 | net.user.events <- eventUpstreamDisconnected{uc}
|
---|
[101] | 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[136] | 132 | func (net *network) upstream() *upstreamConn {
|
---|
| 133 | return net.conn
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[202] | 136 | func (net *network) Stop() {
|
---|
| 137 | select {
|
---|
| 138 | case <-net.stopped:
|
---|
| 139 | return
|
---|
| 140 | default:
|
---|
| 141 | close(net.stopped)
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | if uc := net.upstream(); uc != nil {
|
---|
| 145 | uc.Close()
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[222] | 149 | func (net *network) createUpdateChannel(ch *Channel) error {
|
---|
[267] | 150 | if current, ok := net.channels[ch.Name]; ok {
|
---|
| 151 | ch.ID = current.ID // update channel if it already exists
|
---|
| 152 | }
|
---|
| 153 | if err := net.user.srv.db.StoreChannel(net.ID, ch); err != nil {
|
---|
[222] | 154 | return err
|
---|
| 155 | }
|
---|
[267] | 156 | net.channels[ch.Name] = ch
|
---|
| 157 | return nil
|
---|
[222] | 158 | }
|
---|
| 159 |
|
---|
| 160 | func (net *network) deleteChannel(name string) error {
|
---|
[267] | 161 | if err := net.user.srv.db.DeleteChannel(net.ID, name); err != nil {
|
---|
| 162 | return err
|
---|
| 163 | }
|
---|
| 164 | delete(net.channels, name)
|
---|
| 165 | return nil
|
---|
[222] | 166 | }
|
---|
| 167 |
|
---|
[101] | 168 | type user struct {
|
---|
| 169 | User
|
---|
| 170 | srv *Server
|
---|
| 171 |
|
---|
[165] | 172 | events chan event
|
---|
[103] | 173 |
|
---|
[101] | 174 | networks []*network
|
---|
| 175 | downstreamConns []*downstreamConn
|
---|
[177] | 176 |
|
---|
| 177 | // LIST commands in progress
|
---|
[179] | 178 | pendingLISTs []pendingLIST
|
---|
[101] | 179 | }
|
---|
| 180 |
|
---|
[177] | 181 | type pendingLIST struct {
|
---|
| 182 | downstreamID uint64
|
---|
| 183 | // list of per-upstream LIST commands not yet sent or completed
|
---|
| 184 | pendingCommands map[int64]*irc.Message
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[101] | 187 | func newUser(srv *Server, record *User) *user {
|
---|
| 188 | return &user{
|
---|
[165] | 189 | User: *record,
|
---|
| 190 | srv: srv,
|
---|
| 191 | events: make(chan event, 64),
|
---|
[101] | 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | func (u *user) forEachNetwork(f func(*network)) {
|
---|
| 196 | for _, network := range u.networks {
|
---|
| 197 | f(network)
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
|
---|
| 202 | for _, network := range u.networks {
|
---|
[136] | 203 | uc := network.upstream()
|
---|
[197] | 204 | if uc == nil {
|
---|
[101] | 205 | continue
|
---|
| 206 | }
|
---|
| 207 | f(uc)
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
|
---|
| 212 | for _, dc := range u.downstreamConns {
|
---|
| 213 | f(dc)
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | func (u *user) getNetwork(name string) *network {
|
---|
| 218 | for _, network := range u.networks {
|
---|
| 219 | if network.Addr == name {
|
---|
| 220 | return network
|
---|
| 221 | }
|
---|
[201] | 222 | if network.Name != "" && network.Name == name {
|
---|
| 223 | return network
|
---|
| 224 | }
|
---|
[101] | 225 | }
|
---|
| 226 | return nil
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | func (u *user) run() {
|
---|
| 230 | networks, err := u.srv.db.ListNetworks(u.Username)
|
---|
| 231 | if err != nil {
|
---|
| 232 | u.srv.Logger.Printf("failed to list networks for user %q: %v", u.Username, err)
|
---|
| 233 | return
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | for _, record := range networks {
|
---|
[267] | 237 | channels, err := u.srv.db.ListChannels(record.ID)
|
---|
| 238 | if err != nil {
|
---|
| 239 | u.srv.Logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | network := newNetwork(u, &record, channels)
|
---|
[101] | 243 | u.networks = append(u.networks, network)
|
---|
| 244 |
|
---|
| 245 | go network.run()
|
---|
| 246 | }
|
---|
[103] | 247 |
|
---|
[165] | 248 | for e := range u.events {
|
---|
| 249 | switch e := e.(type) {
|
---|
[196] | 250 | case eventUpstreamConnected:
|
---|
[198] | 251 | uc := e.uc
|
---|
[199] | 252 |
|
---|
| 253 | uc.network.conn = uc
|
---|
| 254 |
|
---|
[198] | 255 | uc.updateAway()
|
---|
[218] | 256 |
|
---|
| 257 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[276] | 258 | dc.updateSupportedCaps()
|
---|
[223] | 259 | sendServiceNOTICE(dc, fmt.Sprintf("connected to %s", uc.network.GetName()))
|
---|
[218] | 260 | })
|
---|
| 261 | uc.network.lastError = nil
|
---|
[179] | 262 | case eventUpstreamDisconnected:
|
---|
| 263 | uc := e.uc
|
---|
[199] | 264 |
|
---|
| 265 | uc.network.conn = nil
|
---|
| 266 |
|
---|
[215] | 267 | for _, ml := range uc.messageLoggers {
|
---|
| 268 | if err := ml.Close(); err != nil {
|
---|
| 269 | uc.logger.Printf("failed to close message logger: %v", err)
|
---|
| 270 | }
|
---|
[179] | 271 | }
|
---|
[199] | 272 |
|
---|
[181] | 273 | uc.endPendingLISTs(true)
|
---|
[218] | 274 |
|
---|
[276] | 275 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 276 | dc.updateSupportedCaps()
|
---|
| 277 | })
|
---|
| 278 |
|
---|
[218] | 279 | if uc.network.lastError == nil {
|
---|
| 280 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 281 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s", uc.network.GetName()))
|
---|
[218] | 282 | })
|
---|
| 283 | }
|
---|
| 284 | case eventUpstreamConnectionError:
|
---|
| 285 | net := e.net
|
---|
| 286 |
|
---|
| 287 | if net.lastError == nil || net.lastError.Error() != e.err.Error() {
|
---|
| 288 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 289 | sendServiceNOTICE(dc, fmt.Sprintf("failed connecting/registering to %s: %v", net.GetName(), e.err))
|
---|
[218] | 290 | })
|
---|
| 291 | }
|
---|
| 292 | net.lastError = e.err
|
---|
| 293 | case eventUpstreamError:
|
---|
| 294 | uc := e.uc
|
---|
| 295 |
|
---|
| 296 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 297 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
|
---|
[218] | 298 | })
|
---|
| 299 | uc.network.lastError = e.err
|
---|
[165] | 300 | case eventUpstreamMessage:
|
---|
| 301 | msg, uc := e.msg, e.uc
|
---|
[175] | 302 | if uc.isClosed() {
|
---|
[133] | 303 | uc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 304 | break
|
---|
| 305 | }
|
---|
[103] | 306 | if err := uc.handleMessage(msg); err != nil {
|
---|
| 307 | uc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 308 | }
|
---|
[166] | 309 | case eventDownstreamConnected:
|
---|
| 310 | dc := e.dc
|
---|
[168] | 311 |
|
---|
| 312 | if err := dc.welcome(); err != nil {
|
---|
| 313 | dc.logger.Printf("failed to handle new registered connection: %v", err)
|
---|
| 314 | break
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[166] | 317 | u.downstreamConns = append(u.downstreamConns, dc)
|
---|
[198] | 318 |
|
---|
| 319 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 320 | uc.updateAway()
|
---|
| 321 | })
|
---|
[276] | 322 |
|
---|
| 323 | dc.updateSupportedCaps()
|
---|
[167] | 324 | case eventDownstreamDisconnected:
|
---|
| 325 | dc := e.dc
|
---|
[204] | 326 |
|
---|
[167] | 327 | for i := range u.downstreamConns {
|
---|
| 328 | if u.downstreamConns[i] == dc {
|
---|
| 329 | u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
|
---|
| 330 | break
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
[198] | 333 |
|
---|
[253] | 334 | // Save history if we're the last client with this name
|
---|
| 335 | skipHistory := make(map[*network]bool)
|
---|
| 336 | u.forEachDownstream(func(conn *downstreamConn) {
|
---|
| 337 | if dc.clientName == conn.clientName {
|
---|
| 338 | skipHistory[conn.network] = true
|
---|
| 339 | }
|
---|
| 340 | })
|
---|
| 341 |
|
---|
| 342 | dc.forEachNetwork(func(net *network) {
|
---|
| 343 | if skipHistory[net] || skipHistory[nil] {
|
---|
| 344 | return
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | net.offlineClients[dc.clientName] = struct{}{}
|
---|
| 348 | for _, history := range net.history {
|
---|
| 349 | history.offlineClients[dc.clientName] = history.ring.Cur()
|
---|
| 350 | }
|
---|
| 351 | })
|
---|
| 352 |
|
---|
[198] | 353 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 354 | uc.updateAway()
|
---|
| 355 | })
|
---|
[165] | 356 | case eventDownstreamMessage:
|
---|
| 357 | msg, dc := e.msg, e.dc
|
---|
[133] | 358 | if dc.isClosed() {
|
---|
| 359 | dc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 360 | break
|
---|
| 361 | }
|
---|
[103] | 362 | err := dc.handleMessage(msg)
|
---|
| 363 | if ircErr, ok := err.(ircError); ok {
|
---|
| 364 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 365 | dc.SendMessage(ircErr.Message)
|
---|
| 366 | } else if err != nil {
|
---|
| 367 | dc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 368 | dc.Close()
|
---|
| 369 | }
|
---|
[165] | 370 | default:
|
---|
| 371 | u.srv.Logger.Printf("received unknown event type: %T", e)
|
---|
[103] | 372 | }
|
---|
| 373 | }
|
---|
[101] | 374 | }
|
---|
| 375 |
|
---|
[120] | 376 | func (u *user) createNetwork(net *Network) (*network, error) {
|
---|
[144] | 377 | if net.ID != 0 {
|
---|
| 378 | panic("tried creating an already-existing network")
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[267] | 381 | network := newNetwork(u, net, nil)
|
---|
[101] | 382 | err := u.srv.db.StoreNetwork(u.Username, &network.Network)
|
---|
| 383 | if err != nil {
|
---|
| 384 | return nil, err
|
---|
| 385 | }
|
---|
[144] | 386 |
|
---|
[101] | 387 | u.networks = append(u.networks, network)
|
---|
[144] | 388 |
|
---|
[101] | 389 | go network.run()
|
---|
| 390 | return network, nil
|
---|
| 391 | }
|
---|
[202] | 392 |
|
---|
| 393 | func (u *user) deleteNetwork(id int64) error {
|
---|
| 394 | for i, net := range u.networks {
|
---|
| 395 | if net.ID != id {
|
---|
| 396 | continue
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | if err := u.srv.db.DeleteNetwork(net.ID); err != nil {
|
---|
| 400 | return err
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 404 | if dc.network != nil && dc.network == net {
|
---|
| 405 | dc.Close()
|
---|
| 406 | }
|
---|
| 407 | })
|
---|
| 408 |
|
---|
| 409 | net.Stop()
|
---|
| 410 | u.networks = append(u.networks[:i], u.networks[i+1:]...)
|
---|
| 411 | return nil
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | panic("tried deleting a non-existing network")
|
---|
| 415 | }
|
---|
[252] | 416 |
|
---|
| 417 | func (u *user) updatePassword(hashed string) error {
|
---|
| 418 | u.User.Password = hashed
|
---|
| 419 | return u.srv.db.UpdatePassword(&u.User)
|
---|
| 420 | }
|
---|