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