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