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