[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 |
|
---|
[202] | 132 | func (net *network) Stop() {
|
---|
| 133 | select {
|
---|
| 134 | case <-net.stopped:
|
---|
| 135 | return
|
---|
| 136 | default:
|
---|
| 137 | close(net.stopped)
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[279] | 140 | if net.conn != nil {
|
---|
| 141 | net.conn.Close()
|
---|
[202] | 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[222] | 145 | func (net *network) createUpdateChannel(ch *Channel) error {
|
---|
[267] | 146 | if current, ok := net.channels[ch.Name]; ok {
|
---|
| 147 | ch.ID = current.ID // update channel if it already exists
|
---|
| 148 | }
|
---|
| 149 | if err := net.user.srv.db.StoreChannel(net.ID, ch); err != nil {
|
---|
[222] | 150 | return err
|
---|
| 151 | }
|
---|
[267] | 152 | net.channels[ch.Name] = ch
|
---|
| 153 | return nil
|
---|
[222] | 154 | }
|
---|
| 155 |
|
---|
| 156 | func (net *network) deleteChannel(name string) error {
|
---|
[267] | 157 | if err := net.user.srv.db.DeleteChannel(net.ID, name); err != nil {
|
---|
| 158 | return err
|
---|
| 159 | }
|
---|
| 160 | delete(net.channels, name)
|
---|
| 161 | return nil
|
---|
[222] | 162 | }
|
---|
| 163 |
|
---|
[101] | 164 | type user struct {
|
---|
| 165 | User
|
---|
| 166 | srv *Server
|
---|
| 167 |
|
---|
[165] | 168 | events chan event
|
---|
[103] | 169 |
|
---|
[101] | 170 | networks []*network
|
---|
| 171 | downstreamConns []*downstreamConn
|
---|
[177] | 172 |
|
---|
| 173 | // LIST commands in progress
|
---|
[179] | 174 | pendingLISTs []pendingLIST
|
---|
[101] | 175 | }
|
---|
| 176 |
|
---|
[177] | 177 | type pendingLIST struct {
|
---|
| 178 | downstreamID uint64
|
---|
| 179 | // list of per-upstream LIST commands not yet sent or completed
|
---|
| 180 | pendingCommands map[int64]*irc.Message
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[101] | 183 | func newUser(srv *Server, record *User) *user {
|
---|
| 184 | return &user{
|
---|
[165] | 185 | User: *record,
|
---|
| 186 | srv: srv,
|
---|
| 187 | events: make(chan event, 64),
|
---|
[101] | 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | func (u *user) forEachNetwork(f func(*network)) {
|
---|
| 192 | for _, network := range u.networks {
|
---|
| 193 | f(network)
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
|
---|
| 198 | for _, network := range u.networks {
|
---|
[279] | 199 | if network.conn == nil {
|
---|
[101] | 200 | continue
|
---|
| 201 | }
|
---|
[279] | 202 | f(network.conn)
|
---|
[101] | 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
|
---|
| 207 | for _, dc := range u.downstreamConns {
|
---|
| 208 | f(dc)
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | func (u *user) getNetwork(name string) *network {
|
---|
| 213 | for _, network := range u.networks {
|
---|
| 214 | if network.Addr == name {
|
---|
| 215 | return network
|
---|
| 216 | }
|
---|
[201] | 217 | if network.Name != "" && network.Name == name {
|
---|
| 218 | return network
|
---|
| 219 | }
|
---|
[101] | 220 | }
|
---|
| 221 | return nil
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | func (u *user) run() {
|
---|
| 225 | networks, err := u.srv.db.ListNetworks(u.Username)
|
---|
| 226 | if err != nil {
|
---|
| 227 | u.srv.Logger.Printf("failed to list networks for user %q: %v", u.Username, err)
|
---|
| 228 | return
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | for _, record := range networks {
|
---|
[267] | 232 | channels, err := u.srv.db.ListChannels(record.ID)
|
---|
| 233 | if err != nil {
|
---|
| 234 | u.srv.Logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | network := newNetwork(u, &record, channels)
|
---|
[101] | 238 | u.networks = append(u.networks, network)
|
---|
| 239 |
|
---|
| 240 | go network.run()
|
---|
| 241 | }
|
---|
[103] | 242 |
|
---|
[165] | 243 | for e := range u.events {
|
---|
| 244 | switch e := e.(type) {
|
---|
[196] | 245 | case eventUpstreamConnected:
|
---|
[198] | 246 | uc := e.uc
|
---|
[199] | 247 |
|
---|
| 248 | uc.network.conn = uc
|
---|
| 249 |
|
---|
[198] | 250 | uc.updateAway()
|
---|
[218] | 251 |
|
---|
| 252 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[276] | 253 | dc.updateSupportedCaps()
|
---|
[223] | 254 | sendServiceNOTICE(dc, fmt.Sprintf("connected to %s", uc.network.GetName()))
|
---|
[218] | 255 | })
|
---|
| 256 | uc.network.lastError = nil
|
---|
[179] | 257 | case eventUpstreamDisconnected:
|
---|
| 258 | uc := e.uc
|
---|
[199] | 259 |
|
---|
| 260 | uc.network.conn = nil
|
---|
| 261 |
|
---|
[215] | 262 | for _, ml := range uc.messageLoggers {
|
---|
| 263 | if err := ml.Close(); err != nil {
|
---|
| 264 | uc.logger.Printf("failed to close message logger: %v", err)
|
---|
| 265 | }
|
---|
[179] | 266 | }
|
---|
[199] | 267 |
|
---|
[181] | 268 | uc.endPendingLISTs(true)
|
---|
[218] | 269 |
|
---|
[276] | 270 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 271 | dc.updateSupportedCaps()
|
---|
| 272 | })
|
---|
| 273 |
|
---|
[218] | 274 | if uc.network.lastError == nil {
|
---|
| 275 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 276 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s", uc.network.GetName()))
|
---|
[218] | 277 | })
|
---|
| 278 | }
|
---|
| 279 | case eventUpstreamConnectionError:
|
---|
| 280 | net := e.net
|
---|
| 281 |
|
---|
| 282 | if net.lastError == nil || net.lastError.Error() != e.err.Error() {
|
---|
| 283 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 284 | sendServiceNOTICE(dc, fmt.Sprintf("failed connecting/registering to %s: %v", net.GetName(), e.err))
|
---|
[218] | 285 | })
|
---|
| 286 | }
|
---|
| 287 | net.lastError = e.err
|
---|
| 288 | case eventUpstreamError:
|
---|
| 289 | uc := e.uc
|
---|
| 290 |
|
---|
| 291 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 292 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
|
---|
[218] | 293 | })
|
---|
| 294 | uc.network.lastError = e.err
|
---|
[165] | 295 | case eventUpstreamMessage:
|
---|
| 296 | msg, uc := e.msg, e.uc
|
---|
[175] | 297 | if uc.isClosed() {
|
---|
[133] | 298 | uc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 299 | break
|
---|
| 300 | }
|
---|
[103] | 301 | if err := uc.handleMessage(msg); err != nil {
|
---|
| 302 | uc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 303 | }
|
---|
[166] | 304 | case eventDownstreamConnected:
|
---|
| 305 | dc := e.dc
|
---|
[168] | 306 |
|
---|
| 307 | if err := dc.welcome(); err != nil {
|
---|
| 308 | dc.logger.Printf("failed to handle new registered connection: %v", err)
|
---|
| 309 | break
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[166] | 312 | u.downstreamConns = append(u.downstreamConns, dc)
|
---|
[198] | 313 |
|
---|
| 314 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 315 | uc.updateAway()
|
---|
| 316 | })
|
---|
[276] | 317 |
|
---|
| 318 | dc.updateSupportedCaps()
|
---|
[167] | 319 | case eventDownstreamDisconnected:
|
---|
| 320 | dc := e.dc
|
---|
[204] | 321 |
|
---|
[167] | 322 | for i := range u.downstreamConns {
|
---|
| 323 | if u.downstreamConns[i] == dc {
|
---|
| 324 | u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
|
---|
| 325 | break
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
[198] | 328 |
|
---|
[253] | 329 | // Save history if we're the last client with this name
|
---|
| 330 | skipHistory := make(map[*network]bool)
|
---|
| 331 | u.forEachDownstream(func(conn *downstreamConn) {
|
---|
| 332 | if dc.clientName == conn.clientName {
|
---|
| 333 | skipHistory[conn.network] = true
|
---|
| 334 | }
|
---|
| 335 | })
|
---|
| 336 |
|
---|
| 337 | dc.forEachNetwork(func(net *network) {
|
---|
| 338 | if skipHistory[net] || skipHistory[nil] {
|
---|
| 339 | return
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | net.offlineClients[dc.clientName] = struct{}{}
|
---|
| 343 | for _, history := range net.history {
|
---|
| 344 | history.offlineClients[dc.clientName] = history.ring.Cur()
|
---|
| 345 | }
|
---|
| 346 | })
|
---|
| 347 |
|
---|
[198] | 348 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 349 | uc.updateAway()
|
---|
| 350 | })
|
---|
[165] | 351 | case eventDownstreamMessage:
|
---|
| 352 | msg, dc := e.msg, e.dc
|
---|
[133] | 353 | if dc.isClosed() {
|
---|
| 354 | dc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 355 | break
|
---|
| 356 | }
|
---|
[103] | 357 | err := dc.handleMessage(msg)
|
---|
| 358 | if ircErr, ok := err.(ircError); ok {
|
---|
| 359 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 360 | dc.SendMessage(ircErr.Message)
|
---|
| 361 | } else if err != nil {
|
---|
| 362 | dc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 363 | dc.Close()
|
---|
| 364 | }
|
---|
[165] | 365 | default:
|
---|
| 366 | u.srv.Logger.Printf("received unknown event type: %T", e)
|
---|
[103] | 367 | }
|
---|
| 368 | }
|
---|
[101] | 369 | }
|
---|
| 370 |
|
---|
[120] | 371 | func (u *user) createNetwork(net *Network) (*network, error) {
|
---|
[144] | 372 | if net.ID != 0 {
|
---|
| 373 | panic("tried creating an already-existing network")
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[267] | 376 | network := newNetwork(u, net, nil)
|
---|
[101] | 377 | err := u.srv.db.StoreNetwork(u.Username, &network.Network)
|
---|
| 378 | if err != nil {
|
---|
| 379 | return nil, err
|
---|
| 380 | }
|
---|
[144] | 381 |
|
---|
[101] | 382 | u.networks = append(u.networks, network)
|
---|
[144] | 383 |
|
---|
[101] | 384 | go network.run()
|
---|
| 385 | return network, nil
|
---|
| 386 | }
|
---|
[202] | 387 |
|
---|
| 388 | func (u *user) deleteNetwork(id int64) error {
|
---|
| 389 | for i, net := range u.networks {
|
---|
| 390 | if net.ID != id {
|
---|
| 391 | continue
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | if err := u.srv.db.DeleteNetwork(net.ID); err != nil {
|
---|
| 395 | return err
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 399 | if dc.network != nil && dc.network == net {
|
---|
| 400 | dc.Close()
|
---|
| 401 | }
|
---|
| 402 | })
|
---|
| 403 |
|
---|
| 404 | net.Stop()
|
---|
| 405 | u.networks = append(u.networks[:i], u.networks[i+1:]...)
|
---|
| 406 | return nil
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | panic("tried deleting a non-existing network")
|
---|
| 410 | }
|
---|
[252] | 411 |
|
---|
| 412 | func (u *user) updatePassword(hashed string) error {
|
---|
| 413 | u.User.Password = hashed
|
---|
| 414 | return u.srv.db.UpdatePassword(&u.User)
|
---|
| 415 | }
|
---|