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