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