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