[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
|
---|
[103] | 222 |
|
---|
[101] | 223 | networks []*network
|
---|
| 224 | downstreamConns []*downstreamConn
|
---|
[177] | 225 |
|
---|
| 226 | // LIST commands in progress
|
---|
[179] | 227 | pendingLISTs []pendingLIST
|
---|
[101] | 228 | }
|
---|
| 229 |
|
---|
[177] | 230 | type pendingLIST struct {
|
---|
| 231 | downstreamID uint64
|
---|
| 232 | // list of per-upstream LIST commands not yet sent or completed
|
---|
| 233 | pendingCommands map[int64]*irc.Message
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[101] | 236 | func newUser(srv *Server, record *User) *user {
|
---|
| 237 | return &user{
|
---|
[165] | 238 | User: *record,
|
---|
| 239 | srv: srv,
|
---|
| 240 | events: make(chan event, 64),
|
---|
[101] | 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | func (u *user) forEachNetwork(f func(*network)) {
|
---|
| 245 | for _, network := range u.networks {
|
---|
| 246 | f(network)
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
|
---|
| 251 | for _, network := range u.networks {
|
---|
[279] | 252 | if network.conn == nil {
|
---|
[101] | 253 | continue
|
---|
| 254 | }
|
---|
[279] | 255 | f(network.conn)
|
---|
[101] | 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
|
---|
| 260 | for _, dc := range u.downstreamConns {
|
---|
| 261 | f(dc)
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | func (u *user) getNetwork(name string) *network {
|
---|
| 266 | for _, network := range u.networks {
|
---|
| 267 | if network.Addr == name {
|
---|
| 268 | return network
|
---|
| 269 | }
|
---|
[201] | 270 | if network.Name != "" && network.Name == name {
|
---|
| 271 | return network
|
---|
| 272 | }
|
---|
[101] | 273 | }
|
---|
| 274 | return nil
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[313] | 277 | func (u *user) getNetworkByID(id int64) *network {
|
---|
| 278 | for _, net := range u.networks {
|
---|
| 279 | if net.ID == id {
|
---|
| 280 | return net
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | return nil
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[101] | 286 | func (u *user) run() {
|
---|
| 287 | networks, err := u.srv.db.ListNetworks(u.Username)
|
---|
| 288 | if err != nil {
|
---|
| 289 | u.srv.Logger.Printf("failed to list networks for user %q: %v", u.Username, err)
|
---|
| 290 | return
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | for _, record := range networks {
|
---|
[283] | 294 | record := record
|
---|
[267] | 295 | channels, err := u.srv.db.ListChannels(record.ID)
|
---|
| 296 | if err != nil {
|
---|
| 297 | u.srv.Logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
|
---|
[359] | 298 | continue
|
---|
[267] | 299 | }
|
---|
| 300 |
|
---|
| 301 | network := newNetwork(u, &record, channels)
|
---|
[101] | 302 | u.networks = append(u.networks, network)
|
---|
| 303 |
|
---|
| 304 | go network.run()
|
---|
| 305 | }
|
---|
[103] | 306 |
|
---|
[165] | 307 | for e := range u.events {
|
---|
| 308 | switch e := e.(type) {
|
---|
[196] | 309 | case eventUpstreamConnected:
|
---|
[198] | 310 | uc := e.uc
|
---|
[199] | 311 |
|
---|
| 312 | uc.network.conn = uc
|
---|
| 313 |
|
---|
[198] | 314 | uc.updateAway()
|
---|
[218] | 315 |
|
---|
| 316 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[276] | 317 | dc.updateSupportedCaps()
|
---|
[223] | 318 | sendServiceNOTICE(dc, fmt.Sprintf("connected to %s", uc.network.GetName()))
|
---|
[296] | 319 |
|
---|
| 320 | dc.updateNick()
|
---|
[218] | 321 | })
|
---|
| 322 | uc.network.lastError = nil
|
---|
[179] | 323 | case eventUpstreamDisconnected:
|
---|
[313] | 324 | u.handleUpstreamDisconnected(e.uc)
|
---|
| 325 | case eventUpstreamConnectionError:
|
---|
| 326 | net := e.net
|
---|
[199] | 327 |
|
---|
[313] | 328 | stopped := false
|
---|
| 329 | select {
|
---|
| 330 | case <-net.stopped:
|
---|
| 331 | stopped = true
|
---|
| 332 | default:
|
---|
[179] | 333 | }
|
---|
[199] | 334 |
|
---|
[313] | 335 | if !stopped && (net.lastError == nil || net.lastError.Error() != e.err.Error()) {
|
---|
[218] | 336 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 337 | sendServiceNOTICE(dc, fmt.Sprintf("failed connecting/registering to %s: %v", net.GetName(), e.err))
|
---|
[218] | 338 | })
|
---|
| 339 | }
|
---|
| 340 | net.lastError = e.err
|
---|
| 341 | case eventUpstreamError:
|
---|
| 342 | uc := e.uc
|
---|
| 343 |
|
---|
| 344 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 345 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
|
---|
[218] | 346 | })
|
---|
| 347 | uc.network.lastError = e.err
|
---|
[165] | 348 | case eventUpstreamMessage:
|
---|
| 349 | msg, uc := e.msg, e.uc
|
---|
[175] | 350 | if uc.isClosed() {
|
---|
[133] | 351 | uc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 352 | break
|
---|
| 353 | }
|
---|
[103] | 354 | if err := uc.handleMessage(msg); err != nil {
|
---|
| 355 | uc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 356 | }
|
---|
[166] | 357 | case eventDownstreamConnected:
|
---|
| 358 | dc := e.dc
|
---|
[168] | 359 |
|
---|
| 360 | if err := dc.welcome(); err != nil {
|
---|
| 361 | dc.logger.Printf("failed to handle new registered connection: %v", err)
|
---|
| 362 | break
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[166] | 365 | u.downstreamConns = append(u.downstreamConns, dc)
|
---|
[198] | 366 |
|
---|
| 367 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 368 | uc.updateAway()
|
---|
| 369 | })
|
---|
[276] | 370 |
|
---|
| 371 | dc.updateSupportedCaps()
|
---|
[167] | 372 | case eventDownstreamDisconnected:
|
---|
| 373 | dc := e.dc
|
---|
[204] | 374 |
|
---|
[167] | 375 | for i := range u.downstreamConns {
|
---|
| 376 | if u.downstreamConns[i] == dc {
|
---|
| 377 | u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
|
---|
| 378 | break
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
[198] | 381 |
|
---|
[253] | 382 | // Save history if we're the last client with this name
|
---|
| 383 | skipHistory := make(map[*network]bool)
|
---|
| 384 | u.forEachDownstream(func(conn *downstreamConn) {
|
---|
| 385 | if dc.clientName == conn.clientName {
|
---|
| 386 | skipHistory[conn.network] = true
|
---|
| 387 | }
|
---|
| 388 | })
|
---|
| 389 |
|
---|
| 390 | dc.forEachNetwork(func(net *network) {
|
---|
| 391 | if skipHistory[net] || skipHistory[nil] {
|
---|
| 392 | return
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | net.offlineClients[dc.clientName] = struct{}{}
|
---|
[284] | 396 | for target, history := range net.history {
|
---|
| 397 | if ch, ok := net.channels[target]; ok && ch.Detached {
|
---|
| 398 | continue
|
---|
| 399 | }
|
---|
[253] | 400 | history.offlineClients[dc.clientName] = history.ring.Cur()
|
---|
| 401 | }
|
---|
| 402 | })
|
---|
| 403 |
|
---|
[198] | 404 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 405 | uc.updateAway()
|
---|
| 406 | })
|
---|
[165] | 407 | case eventDownstreamMessage:
|
---|
| 408 | msg, dc := e.msg, e.dc
|
---|
[133] | 409 | if dc.isClosed() {
|
---|
| 410 | dc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 411 | break
|
---|
| 412 | }
|
---|
[103] | 413 | err := dc.handleMessage(msg)
|
---|
| 414 | if ircErr, ok := err.(ircError); ok {
|
---|
| 415 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 416 | dc.SendMessage(ircErr.Message)
|
---|
| 417 | } else if err != nil {
|
---|
| 418 | dc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 419 | dc.Close()
|
---|
| 420 | }
|
---|
[376] | 421 | case eventStop:
|
---|
| 422 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 423 | dc.Close()
|
---|
| 424 | })
|
---|
| 425 | for _, n := range u.networks {
|
---|
| 426 | n.stop()
|
---|
| 427 | }
|
---|
| 428 | return
|
---|
[165] | 429 | default:
|
---|
| 430 | u.srv.Logger.Printf("received unknown event type: %T", e)
|
---|
[103] | 431 | }
|
---|
| 432 | }
|
---|
[101] | 433 | }
|
---|
| 434 |
|
---|
[313] | 435 | func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
|
---|
| 436 | uc.network.conn = nil
|
---|
| 437 |
|
---|
| 438 | for _, ml := range uc.messageLoggers {
|
---|
| 439 | if err := ml.Close(); err != nil {
|
---|
| 440 | uc.logger.Printf("failed to close message logger: %v", err)
|
---|
| 441 | }
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | uc.endPendingLISTs(true)
|
---|
| 445 |
|
---|
| 446 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 447 | dc.updateSupportedCaps()
|
---|
| 448 | })
|
---|
| 449 |
|
---|
| 450 | if uc.network.lastError == nil {
|
---|
| 451 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 452 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s", uc.network.GetName()))
|
---|
| 453 | })
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | func (u *user) addNetwork(network *network) {
|
---|
| 458 | u.networks = append(u.networks, network)
|
---|
| 459 | go network.run()
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | func (u *user) removeNetwork(network *network) {
|
---|
| 463 | network.stop()
|
---|
| 464 |
|
---|
| 465 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 466 | if dc.network != nil && dc.network == network {
|
---|
| 467 | dc.Close()
|
---|
| 468 | }
|
---|
| 469 | })
|
---|
| 470 |
|
---|
| 471 | for i, net := range u.networks {
|
---|
| 472 | if net == network {
|
---|
| 473 | u.networks = append(u.networks[:i], u.networks[i+1:]...)
|
---|
| 474 | return
|
---|
| 475 | }
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | panic("tried to remove a non-existing network")
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | func (u *user) createNetwork(record *Network) (*network, error) {
|
---|
| 482 | if record.ID != 0 {
|
---|
[144] | 483 | panic("tried creating an already-existing network")
|
---|
| 484 | }
|
---|
| 485 |
|
---|
[313] | 486 | network := newNetwork(u, record, nil)
|
---|
[101] | 487 | err := u.srv.db.StoreNetwork(u.Username, &network.Network)
|
---|
| 488 | if err != nil {
|
---|
| 489 | return nil, err
|
---|
| 490 | }
|
---|
[144] | 491 |
|
---|
[313] | 492 | u.addNetwork(network)
|
---|
[144] | 493 |
|
---|
[101] | 494 | return network, nil
|
---|
| 495 | }
|
---|
[202] | 496 |
|
---|
[313] | 497 | func (u *user) updateNetwork(record *Network) (*network, error) {
|
---|
| 498 | if record.ID == 0 {
|
---|
| 499 | panic("tried updating a new network")
|
---|
| 500 | }
|
---|
[202] | 501 |
|
---|
[313] | 502 | network := u.getNetworkByID(record.ID)
|
---|
| 503 | if network == nil {
|
---|
| 504 | panic("tried updating a non-existing network")
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | if err := u.srv.db.StoreNetwork(u.Username, record); err != nil {
|
---|
| 508 | return nil, err
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 | // Most network changes require us to re-connect to the upstream server
|
---|
| 512 |
|
---|
| 513 | channels := make([]Channel, 0, len(network.channels))
|
---|
| 514 | for _, ch := range network.channels {
|
---|
| 515 | channels = append(channels, *ch)
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | updatedNetwork := newNetwork(u, record, channels)
|
---|
| 519 |
|
---|
| 520 | // If we're currently connected, disconnect and perform the necessary
|
---|
| 521 | // bookkeeping
|
---|
| 522 | if network.conn != nil {
|
---|
| 523 | network.stop()
|
---|
| 524 | // Note: this will set network.conn to nil
|
---|
| 525 | u.handleUpstreamDisconnected(network.conn)
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | // Patch downstream connections to use our fresh updated network
|
---|
| 529 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 530 | if dc.network != nil && dc.network == network {
|
---|
| 531 | dc.network = updatedNetwork
|
---|
[202] | 532 | }
|
---|
[313] | 533 | })
|
---|
[202] | 534 |
|
---|
[313] | 535 | // We need to remove the network after patching downstream connections,
|
---|
| 536 | // otherwise they'll get closed
|
---|
| 537 | u.removeNetwork(network)
|
---|
[202] | 538 |
|
---|
[313] | 539 | // This will re-connect to the upstream server
|
---|
| 540 | u.addNetwork(updatedNetwork)
|
---|
| 541 |
|
---|
| 542 | return updatedNetwork, nil
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | func (u *user) deleteNetwork(id int64) error {
|
---|
| 546 | network := u.getNetworkByID(id)
|
---|
| 547 | if network == nil {
|
---|
| 548 | panic("tried deleting a non-existing network")
|
---|
[202] | 549 | }
|
---|
| 550 |
|
---|
[313] | 551 | if err := u.srv.db.DeleteNetwork(network.ID); err != nil {
|
---|
| 552 | return err
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | u.removeNetwork(network)
|
---|
| 556 | return nil
|
---|
[202] | 557 | }
|
---|
[252] | 558 |
|
---|
| 559 | func (u *user) updatePassword(hashed string) error {
|
---|
| 560 | u.User.Password = hashed
|
---|
[324] | 561 | return u.srv.db.StoreUser(&u.User)
|
---|
[252] | 562 | }
|
---|
[376] | 563 |
|
---|
| 564 | func (u *user) stop() {
|
---|
| 565 | u.events <- eventStop{}
|
---|
| 566 | }
|
---|