[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 |
|
---|
[376] | 48 | type eventStop struct{}
|
---|
| 49 |
|
---|
[253] | 50 | type networkHistory struct {
|
---|
| 51 | offlineClients map[string]uint64 // indexed by client name
|
---|
| 52 | ring *Ring // can be nil if there are no offline clients
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[101] | 55 | type network struct {
|
---|
| 56 | Network
|
---|
[202] | 57 | user *user
|
---|
| 58 | stopped chan struct{}
|
---|
[131] | 59 |
|
---|
[253] | 60 | conn *upstreamConn
|
---|
[267] | 61 | channels map[string]*Channel
|
---|
[253] | 62 | history map[string]*networkHistory // indexed by entity
|
---|
| 63 | offlineClients map[string]struct{} // indexed by client name
|
---|
| 64 | lastError error
|
---|
[101] | 65 | }
|
---|
| 66 |
|
---|
[267] | 67 | func newNetwork(user *user, record *Network, channels []Channel) *network {
|
---|
| 68 | m := make(map[string]*Channel, len(channels))
|
---|
| 69 | for _, ch := range channels {
|
---|
[283] | 70 | ch := ch
|
---|
[267] | 71 | m[ch.Name] = &ch
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[101] | 74 | return &network{
|
---|
[253] | 75 | Network: *record,
|
---|
| 76 | user: user,
|
---|
| 77 | stopped: make(chan struct{}),
|
---|
[267] | 78 | channels: m,
|
---|
[253] | 79 | history: make(map[string]*networkHistory),
|
---|
| 80 | offlineClients: make(map[string]struct{}),
|
---|
[101] | 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[218] | 84 | func (net *network) forEachDownstream(f func(*downstreamConn)) {
|
---|
| 85 | net.user.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 86 | if dc.network != nil && dc.network != net {
|
---|
| 87 | return
|
---|
| 88 | }
|
---|
| 89 | f(dc)
|
---|
| 90 | })
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[311] | 93 | func (net *network) isStopped() bool {
|
---|
| 94 | select {
|
---|
| 95 | case <-net.stopped:
|
---|
| 96 | return true
|
---|
| 97 | default:
|
---|
| 98 | return false
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[101] | 102 | func (net *network) run() {
|
---|
| 103 | var lastTry time.Time
|
---|
| 104 | for {
|
---|
[311] | 105 | if net.isStopped() {
|
---|
[202] | 106 | return
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[101] | 109 | if dur := time.Now().Sub(lastTry); dur < retryConnectMinDelay {
|
---|
| 110 | delay := retryConnectMinDelay - dur
|
---|
| 111 | net.user.srv.Logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), net.Addr)
|
---|
| 112 | time.Sleep(delay)
|
---|
| 113 | }
|
---|
| 114 | lastTry = time.Now()
|
---|
| 115 |
|
---|
| 116 | uc, err := connectToUpstream(net)
|
---|
| 117 | if err != nil {
|
---|
| 118 | net.user.srv.Logger.Printf("failed to connect to upstream server %q: %v", net.Addr, err)
|
---|
[218] | 119 | net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to connect: %v", err)}
|
---|
[101] | 120 | continue
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | uc.register()
|
---|
[197] | 124 | if err := uc.runUntilRegistered(); err != nil {
|
---|
| 125 | uc.logger.Printf("failed to register: %v", err)
|
---|
[218] | 126 | net.user.events <- eventUpstreamConnectionError{net, fmt.Errorf("failed to register: %v", err)}
|
---|
[197] | 127 | uc.Close()
|
---|
| 128 | continue
|
---|
| 129 | }
|
---|
[101] | 130 |
|
---|
[311] | 131 | // TODO: this is racy with net.stopped. If the network is stopped
|
---|
| 132 | // before the user goroutine receives eventUpstreamConnected, the
|
---|
| 133 | // connection won't be closed.
|
---|
[196] | 134 | net.user.events <- eventUpstreamConnected{uc}
|
---|
[165] | 135 | if err := uc.readMessages(net.user.events); err != nil {
|
---|
[101] | 136 | uc.logger.Printf("failed to handle messages: %v", err)
|
---|
[218] | 137 | net.user.events <- eventUpstreamError{uc, fmt.Errorf("failed to handle messages: %v", err)}
|
---|
[101] | 138 | }
|
---|
| 139 | uc.Close()
|
---|
[179] | 140 | net.user.events <- eventUpstreamDisconnected{uc}
|
---|
[101] | 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[309] | 144 | func (net *network) stop() {
|
---|
[311] | 145 | if !net.isStopped() {
|
---|
[202] | 146 | close(net.stopped)
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[279] | 149 | if net.conn != nil {
|
---|
| 150 | net.conn.Close()
|
---|
[202] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[222] | 154 | func (net *network) createUpdateChannel(ch *Channel) error {
|
---|
[267] | 155 | if current, ok := net.channels[ch.Name]; ok {
|
---|
| 156 | ch.ID = current.ID // update channel if it already exists
|
---|
| 157 | }
|
---|
| 158 | if err := net.user.srv.db.StoreChannel(net.ID, ch); err != nil {
|
---|
[222] | 159 | return err
|
---|
| 160 | }
|
---|
[284] | 161 | prev := net.channels[ch.Name]
|
---|
[267] | 162 | net.channels[ch.Name] = ch
|
---|
[284] | 163 |
|
---|
| 164 | if prev != nil && prev.Detached != ch.Detached {
|
---|
| 165 | history := net.history[ch.Name]
|
---|
| 166 | if ch.Detached {
|
---|
| 167 | net.user.srv.Logger.Printf("network %q: detaching channel %q", net.GetName(), ch.Name)
|
---|
| 168 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 169 | net.offlineClients[dc.clientName] = struct{}{}
|
---|
| 170 | if history != nil {
|
---|
| 171 | history.offlineClients[dc.clientName] = history.ring.Cur()
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | dc.SendMessage(&irc.Message{
|
---|
| 175 | Prefix: dc.prefix(),
|
---|
| 176 | Command: "PART",
|
---|
| 177 | Params: []string{dc.marshalEntity(net, ch.Name), "Detach"},
|
---|
| 178 | })
|
---|
| 179 | })
|
---|
| 180 | } else {
|
---|
| 181 | net.user.srv.Logger.Printf("network %q: attaching channel %q", net.GetName(), ch.Name)
|
---|
| 182 |
|
---|
| 183 | var uch *upstreamChannel
|
---|
| 184 | if net.conn != nil {
|
---|
| 185 | uch = net.conn.channels[ch.Name]
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 189 | dc.SendMessage(&irc.Message{
|
---|
| 190 | Prefix: dc.prefix(),
|
---|
| 191 | Command: "JOIN",
|
---|
| 192 | Params: []string{dc.marshalEntity(net, ch.Name)},
|
---|
| 193 | })
|
---|
| 194 |
|
---|
| 195 | if uch != nil {
|
---|
| 196 | forwardChannel(dc, uch)
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | if history != nil {
|
---|
| 200 | dc.sendNetworkHistory(net)
|
---|
| 201 | }
|
---|
| 202 | })
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[267] | 206 | return nil
|
---|
[222] | 207 | }
|
---|
| 208 |
|
---|
| 209 | func (net *network) deleteChannel(name string) error {
|
---|
[267] | 210 | if err := net.user.srv.db.DeleteChannel(net.ID, name); err != nil {
|
---|
| 211 | return err
|
---|
| 212 | }
|
---|
| 213 | delete(net.channels, name)
|
---|
| 214 | return nil
|
---|
[222] | 215 | }
|
---|
| 216 |
|
---|
[101] | 217 | type user struct {
|
---|
| 218 | User
|
---|
| 219 | srv *Server
|
---|
| 220 |
|
---|
[165] | 221 | events chan event
|
---|
[377] | 222 | done chan struct{}
|
---|
[103] | 223 |
|
---|
[101] | 224 | networks []*network
|
---|
| 225 | downstreamConns []*downstreamConn
|
---|
[177] | 226 |
|
---|
| 227 | // LIST commands in progress
|
---|
[179] | 228 | pendingLISTs []pendingLIST
|
---|
[101] | 229 | }
|
---|
| 230 |
|
---|
[177] | 231 | type pendingLIST struct {
|
---|
| 232 | downstreamID uint64
|
---|
| 233 | // list of per-upstream LIST commands not yet sent or completed
|
---|
| 234 | pendingCommands map[int64]*irc.Message
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[101] | 237 | func newUser(srv *Server, record *User) *user {
|
---|
| 238 | return &user{
|
---|
[165] | 239 | User: *record,
|
---|
| 240 | srv: srv,
|
---|
| 241 | events: make(chan event, 64),
|
---|
[377] | 242 | done: make(chan struct{}),
|
---|
[101] | 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | func (u *user) forEachNetwork(f func(*network)) {
|
---|
| 247 | for _, network := range u.networks {
|
---|
| 248 | f(network)
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
|
---|
| 253 | for _, network := range u.networks {
|
---|
[279] | 254 | if network.conn == nil {
|
---|
[101] | 255 | continue
|
---|
| 256 | }
|
---|
[279] | 257 | f(network.conn)
|
---|
[101] | 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
|
---|
| 262 | for _, dc := range u.downstreamConns {
|
---|
| 263 | f(dc)
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | func (u *user) getNetwork(name string) *network {
|
---|
| 268 | for _, network := range u.networks {
|
---|
| 269 | if network.Addr == name {
|
---|
| 270 | return network
|
---|
| 271 | }
|
---|
[201] | 272 | if network.Name != "" && network.Name == name {
|
---|
| 273 | return network
|
---|
| 274 | }
|
---|
[101] | 275 | }
|
---|
| 276 | return nil
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[313] | 279 | func (u *user) getNetworkByID(id int64) *network {
|
---|
| 280 | for _, net := range u.networks {
|
---|
| 281 | if net.ID == id {
|
---|
| 282 | return net
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 | return nil
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[101] | 288 | func (u *user) run() {
|
---|
[377] | 289 | defer close(u.done)
|
---|
| 290 |
|
---|
[101] | 291 | networks, err := u.srv.db.ListNetworks(u.Username)
|
---|
| 292 | if err != nil {
|
---|
| 293 | u.srv.Logger.Printf("failed to list networks for user %q: %v", u.Username, err)
|
---|
| 294 | return
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | for _, record := range networks {
|
---|
[283] | 298 | record := record
|
---|
[267] | 299 | channels, err := u.srv.db.ListChannels(record.ID)
|
---|
| 300 | if err != nil {
|
---|
| 301 | u.srv.Logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
|
---|
[359] | 302 | continue
|
---|
[267] | 303 | }
|
---|
| 304 |
|
---|
| 305 | network := newNetwork(u, &record, channels)
|
---|
[101] | 306 | u.networks = append(u.networks, network)
|
---|
| 307 |
|
---|
| 308 | go network.run()
|
---|
| 309 | }
|
---|
[103] | 310 |
|
---|
[165] | 311 | for e := range u.events {
|
---|
| 312 | switch e := e.(type) {
|
---|
[196] | 313 | case eventUpstreamConnected:
|
---|
[198] | 314 | uc := e.uc
|
---|
[199] | 315 |
|
---|
| 316 | uc.network.conn = uc
|
---|
| 317 |
|
---|
[198] | 318 | uc.updateAway()
|
---|
[218] | 319 |
|
---|
| 320 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[276] | 321 | dc.updateSupportedCaps()
|
---|
[223] | 322 | sendServiceNOTICE(dc, fmt.Sprintf("connected to %s", uc.network.GetName()))
|
---|
[296] | 323 |
|
---|
| 324 | dc.updateNick()
|
---|
[218] | 325 | })
|
---|
| 326 | uc.network.lastError = nil
|
---|
[179] | 327 | case eventUpstreamDisconnected:
|
---|
[313] | 328 | u.handleUpstreamDisconnected(e.uc)
|
---|
| 329 | case eventUpstreamConnectionError:
|
---|
| 330 | net := e.net
|
---|
[199] | 331 |
|
---|
[313] | 332 | stopped := false
|
---|
| 333 | select {
|
---|
| 334 | case <-net.stopped:
|
---|
| 335 | stopped = true
|
---|
| 336 | default:
|
---|
[179] | 337 | }
|
---|
[199] | 338 |
|
---|
[313] | 339 | if !stopped && (net.lastError == nil || net.lastError.Error() != e.err.Error()) {
|
---|
[218] | 340 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 341 | sendServiceNOTICE(dc, fmt.Sprintf("failed connecting/registering to %s: %v", net.GetName(), e.err))
|
---|
[218] | 342 | })
|
---|
| 343 | }
|
---|
| 344 | net.lastError = e.err
|
---|
| 345 | case eventUpstreamError:
|
---|
| 346 | uc := e.uc
|
---|
| 347 |
|
---|
| 348 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 349 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
|
---|
[218] | 350 | })
|
---|
| 351 | uc.network.lastError = e.err
|
---|
[165] | 352 | case eventUpstreamMessage:
|
---|
| 353 | msg, uc := e.msg, e.uc
|
---|
[175] | 354 | if uc.isClosed() {
|
---|
[133] | 355 | uc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 356 | break
|
---|
| 357 | }
|
---|
[103] | 358 | if err := uc.handleMessage(msg); err != nil {
|
---|
| 359 | uc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 360 | }
|
---|
[166] | 361 | case eventDownstreamConnected:
|
---|
| 362 | dc := e.dc
|
---|
[168] | 363 |
|
---|
| 364 | if err := dc.welcome(); err != nil {
|
---|
| 365 | dc.logger.Printf("failed to handle new registered connection: %v", err)
|
---|
| 366 | break
|
---|
| 367 | }
|
---|
| 368 |
|
---|
[166] | 369 | u.downstreamConns = append(u.downstreamConns, dc)
|
---|
[198] | 370 |
|
---|
| 371 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 372 | uc.updateAway()
|
---|
| 373 | })
|
---|
[276] | 374 |
|
---|
| 375 | dc.updateSupportedCaps()
|
---|
[167] | 376 | case eventDownstreamDisconnected:
|
---|
| 377 | dc := e.dc
|
---|
[204] | 378 |
|
---|
[167] | 379 | for i := range u.downstreamConns {
|
---|
| 380 | if u.downstreamConns[i] == dc {
|
---|
| 381 | u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
|
---|
| 382 | break
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
[198] | 385 |
|
---|
[253] | 386 | // Save history if we're the last client with this name
|
---|
| 387 | skipHistory := make(map[*network]bool)
|
---|
| 388 | u.forEachDownstream(func(conn *downstreamConn) {
|
---|
| 389 | if dc.clientName == conn.clientName {
|
---|
| 390 | skipHistory[conn.network] = true
|
---|
| 391 | }
|
---|
| 392 | })
|
---|
| 393 |
|
---|
| 394 | dc.forEachNetwork(func(net *network) {
|
---|
| 395 | if skipHistory[net] || skipHistory[nil] {
|
---|
| 396 | return
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | net.offlineClients[dc.clientName] = struct{}{}
|
---|
[284] | 400 | for target, history := range net.history {
|
---|
| 401 | if ch, ok := net.channels[target]; ok && ch.Detached {
|
---|
| 402 | continue
|
---|
| 403 | }
|
---|
[253] | 404 | history.offlineClients[dc.clientName] = history.ring.Cur()
|
---|
| 405 | }
|
---|
| 406 | })
|
---|
| 407 |
|
---|
[198] | 408 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 409 | uc.updateAway()
|
---|
| 410 | })
|
---|
[165] | 411 | case eventDownstreamMessage:
|
---|
| 412 | msg, dc := e.msg, e.dc
|
---|
[133] | 413 | if dc.isClosed() {
|
---|
| 414 | dc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 415 | break
|
---|
| 416 | }
|
---|
[103] | 417 | err := dc.handleMessage(msg)
|
---|
| 418 | if ircErr, ok := err.(ircError); ok {
|
---|
| 419 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 420 | dc.SendMessage(ircErr.Message)
|
---|
| 421 | } else if err != nil {
|
---|
| 422 | dc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 423 | dc.Close()
|
---|
| 424 | }
|
---|
[376] | 425 | case eventStop:
|
---|
| 426 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 427 | dc.Close()
|
---|
| 428 | })
|
---|
| 429 | for _, n := range u.networks {
|
---|
| 430 | n.stop()
|
---|
| 431 | }
|
---|
| 432 | return
|
---|
[165] | 433 | default:
|
---|
| 434 | u.srv.Logger.Printf("received unknown event type: %T", e)
|
---|
[103] | 435 | }
|
---|
| 436 | }
|
---|
[101] | 437 | }
|
---|
| 438 |
|
---|
[313] | 439 | func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
|
---|
| 440 | uc.network.conn = nil
|
---|
| 441 |
|
---|
| 442 | for _, ml := range uc.messageLoggers {
|
---|
| 443 | if err := ml.Close(); err != nil {
|
---|
| 444 | uc.logger.Printf("failed to close message logger: %v", err)
|
---|
| 445 | }
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | uc.endPendingLISTs(true)
|
---|
| 449 |
|
---|
| 450 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 451 | dc.updateSupportedCaps()
|
---|
| 452 | })
|
---|
| 453 |
|
---|
| 454 | if uc.network.lastError == nil {
|
---|
| 455 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 456 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s", uc.network.GetName()))
|
---|
| 457 | })
|
---|
| 458 | }
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | func (u *user) addNetwork(network *network) {
|
---|
| 462 | u.networks = append(u.networks, network)
|
---|
| 463 | go network.run()
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | func (u *user) removeNetwork(network *network) {
|
---|
| 467 | network.stop()
|
---|
| 468 |
|
---|
| 469 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 470 | if dc.network != nil && dc.network == network {
|
---|
| 471 | dc.Close()
|
---|
| 472 | }
|
---|
| 473 | })
|
---|
| 474 |
|
---|
| 475 | for i, net := range u.networks {
|
---|
| 476 | if net == network {
|
---|
| 477 | u.networks = append(u.networks[:i], u.networks[i+1:]...)
|
---|
| 478 | return
|
---|
| 479 | }
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | panic("tried to remove a non-existing network")
|
---|
| 483 | }
|
---|
| 484 |
|
---|
| 485 | func (u *user) createNetwork(record *Network) (*network, error) {
|
---|
| 486 | if record.ID != 0 {
|
---|
[144] | 487 | panic("tried creating an already-existing network")
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[313] | 490 | network := newNetwork(u, record, nil)
|
---|
[101] | 491 | err := u.srv.db.StoreNetwork(u.Username, &network.Network)
|
---|
| 492 | if err != nil {
|
---|
| 493 | return nil, err
|
---|
| 494 | }
|
---|
[144] | 495 |
|
---|
[313] | 496 | u.addNetwork(network)
|
---|
[144] | 497 |
|
---|
[101] | 498 | return network, nil
|
---|
| 499 | }
|
---|
[202] | 500 |
|
---|
[313] | 501 | func (u *user) updateNetwork(record *Network) (*network, error) {
|
---|
| 502 | if record.ID == 0 {
|
---|
| 503 | panic("tried updating a new network")
|
---|
| 504 | }
|
---|
[202] | 505 |
|
---|
[313] | 506 | network := u.getNetworkByID(record.ID)
|
---|
| 507 | if network == nil {
|
---|
| 508 | panic("tried updating a non-existing network")
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 | if err := u.srv.db.StoreNetwork(u.Username, record); err != nil {
|
---|
| 512 | return nil, err
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | // Most network changes require us to re-connect to the upstream server
|
---|
| 516 |
|
---|
| 517 | channels := make([]Channel, 0, len(network.channels))
|
---|
| 518 | for _, ch := range network.channels {
|
---|
| 519 | channels = append(channels, *ch)
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | updatedNetwork := newNetwork(u, record, channels)
|
---|
| 523 |
|
---|
| 524 | // If we're currently connected, disconnect and perform the necessary
|
---|
| 525 | // bookkeeping
|
---|
| 526 | if network.conn != nil {
|
---|
| 527 | network.stop()
|
---|
| 528 | // Note: this will set network.conn to nil
|
---|
| 529 | u.handleUpstreamDisconnected(network.conn)
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | // Patch downstream connections to use our fresh updated network
|
---|
| 533 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 534 | if dc.network != nil && dc.network == network {
|
---|
| 535 | dc.network = updatedNetwork
|
---|
[202] | 536 | }
|
---|
[313] | 537 | })
|
---|
[202] | 538 |
|
---|
[313] | 539 | // We need to remove the network after patching downstream connections,
|
---|
| 540 | // otherwise they'll get closed
|
---|
| 541 | u.removeNetwork(network)
|
---|
[202] | 542 |
|
---|
[313] | 543 | // This will re-connect to the upstream server
|
---|
| 544 | u.addNetwork(updatedNetwork)
|
---|
| 545 |
|
---|
| 546 | return updatedNetwork, nil
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | func (u *user) deleteNetwork(id int64) error {
|
---|
| 550 | network := u.getNetworkByID(id)
|
---|
| 551 | if network == nil {
|
---|
| 552 | panic("tried deleting a non-existing network")
|
---|
[202] | 553 | }
|
---|
| 554 |
|
---|
[313] | 555 | if err := u.srv.db.DeleteNetwork(network.ID); err != nil {
|
---|
| 556 | return err
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | u.removeNetwork(network)
|
---|
| 560 | return nil
|
---|
[202] | 561 | }
|
---|
[252] | 562 |
|
---|
| 563 | func (u *user) updatePassword(hashed string) error {
|
---|
| 564 | u.User.Password = hashed
|
---|
[324] | 565 | return u.srv.db.StoreUser(&u.User)
|
---|
[252] | 566 | }
|
---|
[376] | 567 |
|
---|
| 568 | func (u *user) stop() {
|
---|
| 569 | u.events <- eventStop{}
|
---|
[377] | 570 | <-u.done
|
---|
[376] | 571 | }
|
---|