[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
|
---|
[101] | 176 | net.user.srv.Logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), net.Addr)
|
---|
| 177 | time.Sleep(delay)
|
---|
| 178 | }
|
---|
| 179 | lastTry = time.Now()
|
---|
| 180 |
|
---|
| 181 | uc, err := connectToUpstream(net)
|
---|
| 182 | if err != nil {
|
---|
| 183 | net.user.srv.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
|
---|
| 236 | net.user.srv.Logger.Printf("network %q: detaching channel %q", net.GetName(), ch.Name)
|
---|
| 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
|
---|
| 259 | net.user.srv.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 |
|
---|
[453] | 279 | dc.sendTargetBacklog(net, ch.Name)
|
---|
[435] | 280 | })
|
---|
[222] | 281 | }
|
---|
| 282 |
|
---|
| 283 | func (net *network) deleteChannel(name string) error {
|
---|
[478] | 284 | ch := net.channels.Value(name)
|
---|
| 285 | if ch == nil {
|
---|
[416] | 286 | return fmt.Errorf("unknown channel %q", name)
|
---|
| 287 | }
|
---|
[435] | 288 | if net.conn != nil {
|
---|
[478] | 289 | uch := net.conn.channels.Value(ch.Name)
|
---|
| 290 | if uch != nil {
|
---|
[435] | 291 | uch.updateAutoDetach(0)
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[416] | 295 | if err := net.user.srv.db.DeleteChannel(ch.ID); err != nil {
|
---|
[267] | 296 | return err
|
---|
| 297 | }
|
---|
[478] | 298 | net.channels.Delete(name)
|
---|
[267] | 299 | return nil
|
---|
[222] | 300 | }
|
---|
| 301 |
|
---|
[478] | 302 | func (net *network) updateCasemapping(newCasemap casemapping) {
|
---|
| 303 | net.casemap = newCasemap
|
---|
| 304 | net.channels.SetCasemapping(newCasemap)
|
---|
[485] | 305 | net.delivered.m.SetCasemapping(newCasemap)
|
---|
[478] | 306 | if net.conn != nil {
|
---|
| 307 | net.conn.channels.SetCasemapping(newCasemap)
|
---|
| 308 | for _, entry := range net.conn.channels.innerMap {
|
---|
| 309 | uch := entry.value.(*upstreamChannel)
|
---|
| 310 | uch.Members.SetCasemapping(newCasemap)
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[489] | 315 | func (net *network) storeClientDeliveryReceipts(clientName string) {
|
---|
| 316 | if !net.user.hasPersistentMsgStore() {
|
---|
| 317 | return
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | var receipts []DeliveryReceipt
|
---|
| 321 | net.delivered.ForEachTarget(func(target string) {
|
---|
| 322 | msgID := net.delivered.LoadID(target, clientName)
|
---|
| 323 | if msgID == "" {
|
---|
| 324 | return
|
---|
| 325 | }
|
---|
| 326 | receipts = append(receipts, DeliveryReceipt{
|
---|
| 327 | Target: target,
|
---|
| 328 | InternalMsgID: msgID,
|
---|
| 329 | })
|
---|
| 330 | })
|
---|
| 331 |
|
---|
| 332 | if err := net.user.srv.db.StoreClientDeliveryReceipts(net.ID, clientName, receipts); err != nil {
|
---|
| 333 | net.user.srv.Logger.Printf("failed to store delivery receipts for user %q, client %q, network %q: %v", net.user.Username, clientName, net.GetName(), err)
|
---|
| 334 | }
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[101] | 337 | type user struct {
|
---|
| 338 | User
|
---|
| 339 | srv *Server
|
---|
| 340 |
|
---|
[165] | 341 | events chan event
|
---|
[377] | 342 | done chan struct{}
|
---|
[103] | 343 |
|
---|
[101] | 344 | networks []*network
|
---|
| 345 | downstreamConns []*downstreamConn
|
---|
[439] | 346 | msgStore messageStore
|
---|
[177] | 347 |
|
---|
| 348 | // LIST commands in progress
|
---|
[179] | 349 | pendingLISTs []pendingLIST
|
---|
[101] | 350 | }
|
---|
| 351 |
|
---|
[177] | 352 | type pendingLIST struct {
|
---|
| 353 | downstreamID uint64
|
---|
| 354 | // list of per-upstream LIST commands not yet sent or completed
|
---|
| 355 | pendingCommands map[int64]*irc.Message
|
---|
| 356 | }
|
---|
| 357 |
|
---|
[101] | 358 | func newUser(srv *Server, record *User) *user {
|
---|
[439] | 359 | var msgStore messageStore
|
---|
[423] | 360 | if srv.LogPath != "" {
|
---|
[439] | 361 | msgStore = newFSMessageStore(srv.LogPath, record.Username)
|
---|
[442] | 362 | } else {
|
---|
| 363 | msgStore = newMemoryMessageStore()
|
---|
[423] | 364 | }
|
---|
| 365 |
|
---|
[101] | 366 | return &user{
|
---|
[489] | 367 | User: *record,
|
---|
| 368 | srv: srv,
|
---|
| 369 | events: make(chan event, 64),
|
---|
| 370 | done: make(chan struct{}),
|
---|
| 371 | msgStore: msgStore,
|
---|
[101] | 372 | }
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | func (u *user) forEachNetwork(f func(*network)) {
|
---|
| 376 | for _, network := range u.networks {
|
---|
| 377 | f(network)
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
|
---|
| 382 | for _, network := range u.networks {
|
---|
[279] | 383 | if network.conn == nil {
|
---|
[101] | 384 | continue
|
---|
| 385 | }
|
---|
[279] | 386 | f(network.conn)
|
---|
[101] | 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
|
---|
| 391 | for _, dc := range u.downstreamConns {
|
---|
| 392 | f(dc)
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | func (u *user) getNetwork(name string) *network {
|
---|
| 397 | for _, network := range u.networks {
|
---|
| 398 | if network.Addr == name {
|
---|
| 399 | return network
|
---|
| 400 | }
|
---|
[201] | 401 | if network.Name != "" && network.Name == name {
|
---|
| 402 | return network
|
---|
| 403 | }
|
---|
[101] | 404 | }
|
---|
| 405 | return nil
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[313] | 408 | func (u *user) getNetworkByID(id int64) *network {
|
---|
| 409 | for _, net := range u.networks {
|
---|
| 410 | if net.ID == id {
|
---|
| 411 | return net
|
---|
| 412 | }
|
---|
| 413 | }
|
---|
| 414 | return nil
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[101] | 417 | func (u *user) run() {
|
---|
[423] | 418 | defer func() {
|
---|
| 419 | if u.msgStore != nil {
|
---|
| 420 | if err := u.msgStore.Close(); err != nil {
|
---|
| 421 | u.srv.Logger.Printf("failed to close message store for user %q: %v", u.Username, err)
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 | close(u.done)
|
---|
| 425 | }()
|
---|
[377] | 426 |
|
---|
[421] | 427 | networks, err := u.srv.db.ListNetworks(u.ID)
|
---|
[101] | 428 | if err != nil {
|
---|
| 429 | u.srv.Logger.Printf("failed to list networks for user %q: %v", u.Username, err)
|
---|
| 430 | return
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | for _, record := range networks {
|
---|
[283] | 434 | record := record
|
---|
[267] | 435 | channels, err := u.srv.db.ListChannels(record.ID)
|
---|
| 436 | if err != nil {
|
---|
| 437 | u.srv.Logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
|
---|
[359] | 438 | continue
|
---|
[267] | 439 | }
|
---|
| 440 |
|
---|
| 441 | network := newNetwork(u, &record, channels)
|
---|
[101] | 442 | u.networks = append(u.networks, network)
|
---|
| 443 |
|
---|
[489] | 444 | if u.hasPersistentMsgStore() {
|
---|
| 445 | receipts, err := u.srv.db.ListDeliveryReceipts(record.ID)
|
---|
| 446 | if err != nil {
|
---|
| 447 | u.srv.Logger.Printf("failed to load delivery receipts for user %q, network %q: %v", u.Username, network.GetName(), err)
|
---|
| 448 | return
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | for _, rcpt := range receipts {
|
---|
| 452 | network.delivered.StoreID(rcpt.Target, rcpt.Client, rcpt.InternalMsgID)
|
---|
| 453 | }
|
---|
| 454 | }
|
---|
| 455 |
|
---|
[101] | 456 | go network.run()
|
---|
| 457 | }
|
---|
[103] | 458 |
|
---|
[165] | 459 | for e := range u.events {
|
---|
| 460 | switch e := e.(type) {
|
---|
[196] | 461 | case eventUpstreamConnected:
|
---|
[198] | 462 | uc := e.uc
|
---|
[199] | 463 |
|
---|
| 464 | uc.network.conn = uc
|
---|
| 465 |
|
---|
[198] | 466 | uc.updateAway()
|
---|
[218] | 467 |
|
---|
| 468 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[276] | 469 | dc.updateSupportedCaps()
|
---|
[223] | 470 | sendServiceNOTICE(dc, fmt.Sprintf("connected to %s", uc.network.GetName()))
|
---|
[296] | 471 |
|
---|
| 472 | dc.updateNick()
|
---|
[218] | 473 | })
|
---|
| 474 | uc.network.lastError = nil
|
---|
[179] | 475 | case eventUpstreamDisconnected:
|
---|
[313] | 476 | u.handleUpstreamDisconnected(e.uc)
|
---|
| 477 | case eventUpstreamConnectionError:
|
---|
| 478 | net := e.net
|
---|
[199] | 479 |
|
---|
[313] | 480 | stopped := false
|
---|
| 481 | select {
|
---|
| 482 | case <-net.stopped:
|
---|
| 483 | stopped = true
|
---|
| 484 | default:
|
---|
[179] | 485 | }
|
---|
[199] | 486 |
|
---|
[313] | 487 | if !stopped && (net.lastError == nil || net.lastError.Error() != e.err.Error()) {
|
---|
[218] | 488 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 489 | sendServiceNOTICE(dc, fmt.Sprintf("failed connecting/registering to %s: %v", net.GetName(), e.err))
|
---|
[218] | 490 | })
|
---|
| 491 | }
|
---|
| 492 | net.lastError = e.err
|
---|
| 493 | case eventUpstreamError:
|
---|
| 494 | uc := e.uc
|
---|
| 495 |
|
---|
| 496 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 497 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
|
---|
[218] | 498 | })
|
---|
| 499 | uc.network.lastError = e.err
|
---|
[165] | 500 | case eventUpstreamMessage:
|
---|
| 501 | msg, uc := e.msg, e.uc
|
---|
[175] | 502 | if uc.isClosed() {
|
---|
[133] | 503 | uc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 504 | break
|
---|
| 505 | }
|
---|
[103] | 506 | if err := uc.handleMessage(msg); err != nil {
|
---|
| 507 | uc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 508 | }
|
---|
[435] | 509 | case eventChannelDetach:
|
---|
| 510 | uc, name := e.uc, e.name
|
---|
[478] | 511 | c := uc.network.channels.Value(name)
|
---|
| 512 | if c == nil || c.Detached {
|
---|
[435] | 513 | continue
|
---|
| 514 | }
|
---|
| 515 | uc.network.detach(c)
|
---|
| 516 | if err := uc.srv.db.StoreChannel(uc.network.ID, c); err != nil {
|
---|
| 517 | u.srv.Logger.Printf("failed to store updated detached channel %q: %v", c.Name, err)
|
---|
| 518 | }
|
---|
[166] | 519 | case eventDownstreamConnected:
|
---|
| 520 | dc := e.dc
|
---|
[168] | 521 |
|
---|
| 522 | if err := dc.welcome(); err != nil {
|
---|
| 523 | dc.logger.Printf("failed to handle new registered connection: %v", err)
|
---|
| 524 | break
|
---|
| 525 | }
|
---|
| 526 |
|
---|
[166] | 527 | u.downstreamConns = append(u.downstreamConns, dc)
|
---|
[198] | 528 |
|
---|
[467] | 529 | dc.forEachNetwork(func(network *network) {
|
---|
| 530 | if network.lastError != nil {
|
---|
| 531 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", network.GetName(), network.lastError))
|
---|
| 532 | }
|
---|
| 533 | })
|
---|
| 534 |
|
---|
[198] | 535 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 536 | uc.updateAway()
|
---|
| 537 | })
|
---|
[167] | 538 | case eventDownstreamDisconnected:
|
---|
| 539 | dc := e.dc
|
---|
[204] | 540 |
|
---|
[167] | 541 | for i := range u.downstreamConns {
|
---|
| 542 | if u.downstreamConns[i] == dc {
|
---|
| 543 | u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
|
---|
| 544 | break
|
---|
| 545 | }
|
---|
| 546 | }
|
---|
[198] | 547 |
|
---|
[489] | 548 | dc.forEachNetwork(func(net *network) {
|
---|
| 549 | net.storeClientDeliveryReceipts(dc.clientName)
|
---|
| 550 | })
|
---|
| 551 |
|
---|
[198] | 552 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 553 | uc.updateAway()
|
---|
| 554 | })
|
---|
[165] | 555 | case eventDownstreamMessage:
|
---|
| 556 | msg, dc := e.msg, e.dc
|
---|
[133] | 557 | if dc.isClosed() {
|
---|
| 558 | dc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 559 | break
|
---|
| 560 | }
|
---|
[103] | 561 | err := dc.handleMessage(msg)
|
---|
| 562 | if ircErr, ok := err.(ircError); ok {
|
---|
| 563 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 564 | dc.SendMessage(ircErr.Message)
|
---|
| 565 | } else if err != nil {
|
---|
| 566 | dc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 567 | dc.Close()
|
---|
| 568 | }
|
---|
[376] | 569 | case eventStop:
|
---|
| 570 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 571 | dc.Close()
|
---|
| 572 | })
|
---|
| 573 | for _, n := range u.networks {
|
---|
| 574 | n.stop()
|
---|
[489] | 575 |
|
---|
| 576 | n.delivered.ForEachClient(func(clientName string) {
|
---|
| 577 | n.storeClientDeliveryReceipts(clientName)
|
---|
| 578 | })
|
---|
[376] | 579 | }
|
---|
| 580 | return
|
---|
[165] | 581 | default:
|
---|
| 582 | u.srv.Logger.Printf("received unknown event type: %T", e)
|
---|
[103] | 583 | }
|
---|
| 584 | }
|
---|
[101] | 585 | }
|
---|
| 586 |
|
---|
[313] | 587 | func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
|
---|
| 588 | uc.network.conn = nil
|
---|
| 589 |
|
---|
| 590 | uc.endPendingLISTs(true)
|
---|
| 591 |
|
---|
[478] | 592 | for _, entry := range uc.channels.innerMap {
|
---|
| 593 | uch := entry.value.(*upstreamChannel)
|
---|
[435] | 594 | uch.updateAutoDetach(0)
|
---|
| 595 | }
|
---|
| 596 |
|
---|
[313] | 597 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 598 | dc.updateSupportedCaps()
|
---|
| 599 | })
|
---|
| 600 |
|
---|
| 601 | if uc.network.lastError == nil {
|
---|
| 602 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 603 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s", uc.network.GetName()))
|
---|
| 604 | })
|
---|
| 605 | }
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | func (u *user) addNetwork(network *network) {
|
---|
| 609 | u.networks = append(u.networks, network)
|
---|
| 610 | go network.run()
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | func (u *user) removeNetwork(network *network) {
|
---|
| 614 | network.stop()
|
---|
| 615 |
|
---|
| 616 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 617 | if dc.network != nil && dc.network == network {
|
---|
| 618 | dc.Close()
|
---|
| 619 | }
|
---|
| 620 | })
|
---|
| 621 |
|
---|
| 622 | for i, net := range u.networks {
|
---|
| 623 | if net == network {
|
---|
| 624 | u.networks = append(u.networks[:i], u.networks[i+1:]...)
|
---|
| 625 | return
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | panic("tried to remove a non-existing network")
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | func (u *user) createNetwork(record *Network) (*network, error) {
|
---|
| 633 | if record.ID != 0 {
|
---|
[144] | 634 | panic("tried creating an already-existing network")
|
---|
| 635 | }
|
---|
| 636 |
|
---|
[313] | 637 | network := newNetwork(u, record, nil)
|
---|
[421] | 638 | err := u.srv.db.StoreNetwork(u.ID, &network.Network)
|
---|
[101] | 639 | if err != nil {
|
---|
| 640 | return nil, err
|
---|
| 641 | }
|
---|
[144] | 642 |
|
---|
[313] | 643 | u.addNetwork(network)
|
---|
[144] | 644 |
|
---|
[101] | 645 | return network, nil
|
---|
| 646 | }
|
---|
[202] | 647 |
|
---|
[313] | 648 | func (u *user) updateNetwork(record *Network) (*network, error) {
|
---|
| 649 | if record.ID == 0 {
|
---|
| 650 | panic("tried updating a new network")
|
---|
| 651 | }
|
---|
[202] | 652 |
|
---|
[313] | 653 | network := u.getNetworkByID(record.ID)
|
---|
| 654 | if network == nil {
|
---|
| 655 | panic("tried updating a non-existing network")
|
---|
| 656 | }
|
---|
| 657 |
|
---|
[421] | 658 | if err := u.srv.db.StoreNetwork(u.ID, record); err != nil {
|
---|
[313] | 659 | return nil, err
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | // Most network changes require us to re-connect to the upstream server
|
---|
| 663 |
|
---|
[478] | 664 | channels := make([]Channel, 0, network.channels.Len())
|
---|
| 665 | for _, entry := range network.channels.innerMap {
|
---|
| 666 | ch := entry.value.(*Channel)
|
---|
[313] | 667 | channels = append(channels, *ch)
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | updatedNetwork := newNetwork(u, record, channels)
|
---|
| 671 |
|
---|
| 672 | // If we're currently connected, disconnect and perform the necessary
|
---|
| 673 | // bookkeeping
|
---|
| 674 | if network.conn != nil {
|
---|
| 675 | network.stop()
|
---|
| 676 | // Note: this will set network.conn to nil
|
---|
| 677 | u.handleUpstreamDisconnected(network.conn)
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | // Patch downstream connections to use our fresh updated network
|
---|
| 681 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 682 | if dc.network != nil && dc.network == network {
|
---|
| 683 | dc.network = updatedNetwork
|
---|
[202] | 684 | }
|
---|
[313] | 685 | })
|
---|
[202] | 686 |
|
---|
[313] | 687 | // We need to remove the network after patching downstream connections,
|
---|
| 688 | // otherwise they'll get closed
|
---|
| 689 | u.removeNetwork(network)
|
---|
[202] | 690 |
|
---|
[313] | 691 | // This will re-connect to the upstream server
|
---|
| 692 | u.addNetwork(updatedNetwork)
|
---|
| 693 |
|
---|
| 694 | return updatedNetwork, nil
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | func (u *user) deleteNetwork(id int64) error {
|
---|
| 698 | network := u.getNetworkByID(id)
|
---|
| 699 | if network == nil {
|
---|
| 700 | panic("tried deleting a non-existing network")
|
---|
[202] | 701 | }
|
---|
| 702 |
|
---|
[313] | 703 | if err := u.srv.db.DeleteNetwork(network.ID); err != nil {
|
---|
| 704 | return err
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | u.removeNetwork(network)
|
---|
| 708 | return nil
|
---|
[202] | 709 | }
|
---|
[252] | 710 |
|
---|
| 711 | func (u *user) updatePassword(hashed string) error {
|
---|
| 712 | u.User.Password = hashed
|
---|
[324] | 713 | return u.srv.db.StoreUser(&u.User)
|
---|
[252] | 714 | }
|
---|
[376] | 715 |
|
---|
| 716 | func (u *user) stop() {
|
---|
| 717 | u.events <- eventStop{}
|
---|
[377] | 718 | <-u.done
|
---|
[376] | 719 | }
|
---|
[489] | 720 |
|
---|
| 721 | func (u *user) hasPersistentMsgStore() bool {
|
---|
| 722 | if u.msgStore == nil {
|
---|
| 723 | return false
|
---|
| 724 | }
|
---|
| 725 | _, isMem := u.msgStore.(*memoryMessageStore)
|
---|
| 726 | return !isMem
|
---|
| 727 | }
|
---|