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