[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 |
|
---|
[499] | 354 | func (net *network) isHighlight(msg *irc.Message) bool {
|
---|
| 355 | if msg.Command != "PRIVMSG" && msg.Command != "NOTICE" {
|
---|
| 356 | return false
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | text := msg.Params[1]
|
---|
| 360 |
|
---|
| 361 | nick := net.Nick
|
---|
| 362 | if net.conn != nil {
|
---|
| 363 | nick = net.conn.nick
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | // TODO: use case-mapping aware comparison here
|
---|
| 367 | return msg.Prefix.Name != nick && isHighlight(text, nick)
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | func (net *network) detachedMessageNeedsRelay(ch *Channel, msg *irc.Message) bool {
|
---|
| 371 | highlight := net.isHighlight(msg)
|
---|
| 372 | return ch.RelayDetached == FilterMessage || ((ch.RelayDetached == FilterHighlight || ch.RelayDetached == FilterDefault) && highlight)
|
---|
| 373 | }
|
---|
| 374 |
|
---|
[101] | 375 | type user struct {
|
---|
| 376 | User
|
---|
[493] | 377 | srv *Server
|
---|
| 378 | logger Logger
|
---|
[101] | 379 |
|
---|
[165] | 380 | events chan event
|
---|
[377] | 381 | done chan struct{}
|
---|
[103] | 382 |
|
---|
[101] | 383 | networks []*network
|
---|
| 384 | downstreamConns []*downstreamConn
|
---|
[439] | 385 | msgStore messageStore
|
---|
[177] | 386 |
|
---|
| 387 | // LIST commands in progress
|
---|
[179] | 388 | pendingLISTs []pendingLIST
|
---|
[101] | 389 | }
|
---|
| 390 |
|
---|
[177] | 391 | type pendingLIST struct {
|
---|
| 392 | downstreamID uint64
|
---|
| 393 | // list of per-upstream LIST commands not yet sent or completed
|
---|
| 394 | pendingCommands map[int64]*irc.Message
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[101] | 397 | func newUser(srv *Server, record *User) *user {
|
---|
[493] | 398 | logger := &prefixLogger{srv.Logger, fmt.Sprintf("user %q: ", record.Username)}
|
---|
| 399 |
|
---|
[439] | 400 | var msgStore messageStore
|
---|
[423] | 401 | if srv.LogPath != "" {
|
---|
[439] | 402 | msgStore = newFSMessageStore(srv.LogPath, record.Username)
|
---|
[442] | 403 | } else {
|
---|
| 404 | msgStore = newMemoryMessageStore()
|
---|
[423] | 405 | }
|
---|
| 406 |
|
---|
[101] | 407 | return &user{
|
---|
[489] | 408 | User: *record,
|
---|
| 409 | srv: srv,
|
---|
[493] | 410 | logger: logger,
|
---|
[489] | 411 | events: make(chan event, 64),
|
---|
| 412 | done: make(chan struct{}),
|
---|
| 413 | msgStore: msgStore,
|
---|
[101] | 414 | }
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | func (u *user) forEachNetwork(f func(*network)) {
|
---|
| 418 | for _, network := range u.networks {
|
---|
| 419 | f(network)
|
---|
| 420 | }
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
|
---|
| 424 | for _, network := range u.networks {
|
---|
[279] | 425 | if network.conn == nil {
|
---|
[101] | 426 | continue
|
---|
| 427 | }
|
---|
[279] | 428 | f(network.conn)
|
---|
[101] | 429 | }
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
|
---|
| 433 | for _, dc := range u.downstreamConns {
|
---|
| 434 | f(dc)
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | func (u *user) getNetwork(name string) *network {
|
---|
| 439 | for _, network := range u.networks {
|
---|
| 440 | if network.Addr == name {
|
---|
| 441 | return network
|
---|
| 442 | }
|
---|
[201] | 443 | if network.Name != "" && network.Name == name {
|
---|
| 444 | return network
|
---|
| 445 | }
|
---|
[101] | 446 | }
|
---|
| 447 | return nil
|
---|
| 448 | }
|
---|
| 449 |
|
---|
[313] | 450 | func (u *user) getNetworkByID(id int64) *network {
|
---|
| 451 | for _, net := range u.networks {
|
---|
| 452 | if net.ID == id {
|
---|
| 453 | return net
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
| 456 | return nil
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[101] | 459 | func (u *user) run() {
|
---|
[423] | 460 | defer func() {
|
---|
| 461 | if u.msgStore != nil {
|
---|
| 462 | if err := u.msgStore.Close(); err != nil {
|
---|
[493] | 463 | u.logger.Printf("failed to close message store for user %q: %v", u.Username, err)
|
---|
[423] | 464 | }
|
---|
| 465 | }
|
---|
| 466 | close(u.done)
|
---|
| 467 | }()
|
---|
[377] | 468 |
|
---|
[421] | 469 | networks, err := u.srv.db.ListNetworks(u.ID)
|
---|
[101] | 470 | if err != nil {
|
---|
[493] | 471 | u.logger.Printf("failed to list networks for user %q: %v", u.Username, err)
|
---|
[101] | 472 | return
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | for _, record := range networks {
|
---|
[283] | 476 | record := record
|
---|
[267] | 477 | channels, err := u.srv.db.ListChannels(record.ID)
|
---|
| 478 | if err != nil {
|
---|
[493] | 479 | u.logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
|
---|
[359] | 480 | continue
|
---|
[267] | 481 | }
|
---|
| 482 |
|
---|
| 483 | network := newNetwork(u, &record, channels)
|
---|
[101] | 484 | u.networks = append(u.networks, network)
|
---|
| 485 |
|
---|
[489] | 486 | if u.hasPersistentMsgStore() {
|
---|
| 487 | receipts, err := u.srv.db.ListDeliveryReceipts(record.ID)
|
---|
| 488 | if err != nil {
|
---|
[493] | 489 | u.logger.Printf("failed to load delivery receipts for user %q, network %q: %v", u.Username, network.GetName(), err)
|
---|
[489] | 490 | return
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | for _, rcpt := range receipts {
|
---|
| 494 | network.delivered.StoreID(rcpt.Target, rcpt.Client, rcpt.InternalMsgID)
|
---|
| 495 | }
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[101] | 498 | go network.run()
|
---|
| 499 | }
|
---|
[103] | 500 |
|
---|
[165] | 501 | for e := range u.events {
|
---|
| 502 | switch e := e.(type) {
|
---|
[196] | 503 | case eventUpstreamConnected:
|
---|
[198] | 504 | uc := e.uc
|
---|
[199] | 505 |
|
---|
| 506 | uc.network.conn = uc
|
---|
| 507 |
|
---|
[198] | 508 | uc.updateAway()
|
---|
[218] | 509 |
|
---|
| 510 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[276] | 511 | dc.updateSupportedCaps()
|
---|
[223] | 512 | sendServiceNOTICE(dc, fmt.Sprintf("connected to %s", uc.network.GetName()))
|
---|
[296] | 513 |
|
---|
| 514 | dc.updateNick()
|
---|
[218] | 515 | })
|
---|
| 516 | uc.network.lastError = nil
|
---|
[179] | 517 | case eventUpstreamDisconnected:
|
---|
[313] | 518 | u.handleUpstreamDisconnected(e.uc)
|
---|
| 519 | case eventUpstreamConnectionError:
|
---|
| 520 | net := e.net
|
---|
[199] | 521 |
|
---|
[313] | 522 | stopped := false
|
---|
| 523 | select {
|
---|
| 524 | case <-net.stopped:
|
---|
| 525 | stopped = true
|
---|
| 526 | default:
|
---|
[179] | 527 | }
|
---|
[199] | 528 |
|
---|
[313] | 529 | if !stopped && (net.lastError == nil || net.lastError.Error() != e.err.Error()) {
|
---|
[218] | 530 | net.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 531 | sendServiceNOTICE(dc, fmt.Sprintf("failed connecting/registering to %s: %v", net.GetName(), e.err))
|
---|
[218] | 532 | })
|
---|
| 533 | }
|
---|
| 534 | net.lastError = e.err
|
---|
| 535 | case eventUpstreamError:
|
---|
| 536 | uc := e.uc
|
---|
| 537 |
|
---|
| 538 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[223] | 539 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
|
---|
[218] | 540 | })
|
---|
| 541 | uc.network.lastError = e.err
|
---|
[165] | 542 | case eventUpstreamMessage:
|
---|
| 543 | msg, uc := e.msg, e.uc
|
---|
[175] | 544 | if uc.isClosed() {
|
---|
[133] | 545 | uc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 546 | break
|
---|
| 547 | }
|
---|
[103] | 548 | if err := uc.handleMessage(msg); err != nil {
|
---|
| 549 | uc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 550 | }
|
---|
[435] | 551 | case eventChannelDetach:
|
---|
| 552 | uc, name := e.uc, e.name
|
---|
[478] | 553 | c := uc.network.channels.Value(name)
|
---|
| 554 | if c == nil || c.Detached {
|
---|
[435] | 555 | continue
|
---|
| 556 | }
|
---|
| 557 | uc.network.detach(c)
|
---|
| 558 | if err := uc.srv.db.StoreChannel(uc.network.ID, c); err != nil {
|
---|
[493] | 559 | u.logger.Printf("failed to store updated detached channel %q: %v", c.Name, err)
|
---|
[435] | 560 | }
|
---|
[166] | 561 | case eventDownstreamConnected:
|
---|
| 562 | dc := e.dc
|
---|
[168] | 563 |
|
---|
| 564 | if err := dc.welcome(); err != nil {
|
---|
| 565 | dc.logger.Printf("failed to handle new registered connection: %v", err)
|
---|
| 566 | break
|
---|
| 567 | }
|
---|
| 568 |
|
---|
[166] | 569 | u.downstreamConns = append(u.downstreamConns, dc)
|
---|
[198] | 570 |
|
---|
[467] | 571 | dc.forEachNetwork(func(network *network) {
|
---|
| 572 | if network.lastError != nil {
|
---|
| 573 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", network.GetName(), network.lastError))
|
---|
| 574 | }
|
---|
| 575 | })
|
---|
| 576 |
|
---|
[198] | 577 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 578 | uc.updateAway()
|
---|
| 579 | })
|
---|
[167] | 580 | case eventDownstreamDisconnected:
|
---|
| 581 | dc := e.dc
|
---|
[204] | 582 |
|
---|
[167] | 583 | for i := range u.downstreamConns {
|
---|
| 584 | if u.downstreamConns[i] == dc {
|
---|
| 585 | u.downstreamConns = append(u.downstreamConns[:i], u.downstreamConns[i+1:]...)
|
---|
| 586 | break
|
---|
| 587 | }
|
---|
| 588 | }
|
---|
[198] | 589 |
|
---|
[489] | 590 | dc.forEachNetwork(func(net *network) {
|
---|
| 591 | net.storeClientDeliveryReceipts(dc.clientName)
|
---|
| 592 | })
|
---|
| 593 |
|
---|
[198] | 594 | u.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 595 | uc.updateAway()
|
---|
| 596 | })
|
---|
[165] | 597 | case eventDownstreamMessage:
|
---|
| 598 | msg, dc := e.msg, e.dc
|
---|
[133] | 599 | if dc.isClosed() {
|
---|
| 600 | dc.logger.Printf("ignoring message on closed connection: %v", msg)
|
---|
| 601 | break
|
---|
| 602 | }
|
---|
[103] | 603 | err := dc.handleMessage(msg)
|
---|
| 604 | if ircErr, ok := err.(ircError); ok {
|
---|
| 605 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 606 | dc.SendMessage(ircErr.Message)
|
---|
| 607 | } else if err != nil {
|
---|
| 608 | dc.logger.Printf("failed to handle message %q: %v", msg, err)
|
---|
| 609 | dc.Close()
|
---|
| 610 | }
|
---|
[376] | 611 | case eventStop:
|
---|
| 612 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 613 | dc.Close()
|
---|
| 614 | })
|
---|
| 615 | for _, n := range u.networks {
|
---|
| 616 | n.stop()
|
---|
[489] | 617 |
|
---|
| 618 | n.delivered.ForEachClient(func(clientName string) {
|
---|
| 619 | n.storeClientDeliveryReceipts(clientName)
|
---|
| 620 | })
|
---|
[376] | 621 | }
|
---|
| 622 | return
|
---|
[165] | 623 | default:
|
---|
[494] | 624 | panic(fmt.Sprintf("received unknown event type: %T", e))
|
---|
[103] | 625 | }
|
---|
| 626 | }
|
---|
[101] | 627 | }
|
---|
| 628 |
|
---|
[313] | 629 | func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
|
---|
| 630 | uc.network.conn = nil
|
---|
| 631 |
|
---|
| 632 | uc.endPendingLISTs(true)
|
---|
| 633 |
|
---|
[478] | 634 | for _, entry := range uc.channels.innerMap {
|
---|
| 635 | uch := entry.value.(*upstreamChannel)
|
---|
[435] | 636 | uch.updateAutoDetach(0)
|
---|
| 637 | }
|
---|
| 638 |
|
---|
[313] | 639 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 640 | dc.updateSupportedCaps()
|
---|
| 641 | })
|
---|
| 642 |
|
---|
| 643 | if uc.network.lastError == nil {
|
---|
| 644 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 645 | sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s", uc.network.GetName()))
|
---|
| 646 | })
|
---|
| 647 | }
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | func (u *user) addNetwork(network *network) {
|
---|
| 651 | u.networks = append(u.networks, network)
|
---|
| 652 | go network.run()
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | func (u *user) removeNetwork(network *network) {
|
---|
| 656 | network.stop()
|
---|
| 657 |
|
---|
| 658 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 659 | if dc.network != nil && dc.network == network {
|
---|
| 660 | dc.Close()
|
---|
| 661 | }
|
---|
| 662 | })
|
---|
| 663 |
|
---|
| 664 | for i, net := range u.networks {
|
---|
| 665 | if net == network {
|
---|
| 666 | u.networks = append(u.networks[:i], u.networks[i+1:]...)
|
---|
| 667 | return
|
---|
| 668 | }
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | panic("tried to remove a non-existing network")
|
---|
| 672 | }
|
---|
| 673 |
|
---|
[500] | 674 | func (u *user) checkNetwork(record *Network) error {
|
---|
| 675 | for _, net := range u.networks {
|
---|
| 676 | if net.GetName() == record.GetName() && net.ID != record.ID {
|
---|
| 677 | return fmt.Errorf("a network with the name %q already exists", record.GetName())
|
---|
| 678 | }
|
---|
| 679 | }
|
---|
| 680 | return nil
|
---|
| 681 | }
|
---|
| 682 |
|
---|
[313] | 683 | func (u *user) createNetwork(record *Network) (*network, error) {
|
---|
| 684 | if record.ID != 0 {
|
---|
[144] | 685 | panic("tried creating an already-existing network")
|
---|
| 686 | }
|
---|
| 687 |
|
---|
[500] | 688 | if err := u.checkNetwork(record); err != nil {
|
---|
| 689 | return nil, err
|
---|
| 690 | }
|
---|
| 691 |
|
---|
[313] | 692 | network := newNetwork(u, record, nil)
|
---|
[421] | 693 | err := u.srv.db.StoreNetwork(u.ID, &network.Network)
|
---|
[101] | 694 | if err != nil {
|
---|
| 695 | return nil, err
|
---|
| 696 | }
|
---|
[144] | 697 |
|
---|
[313] | 698 | u.addNetwork(network)
|
---|
[144] | 699 |
|
---|
[101] | 700 | return network, nil
|
---|
| 701 | }
|
---|
[202] | 702 |
|
---|
[313] | 703 | func (u *user) updateNetwork(record *Network) (*network, error) {
|
---|
| 704 | if record.ID == 0 {
|
---|
| 705 | panic("tried updating a new network")
|
---|
| 706 | }
|
---|
[202] | 707 |
|
---|
[500] | 708 | if err := u.checkNetwork(record); err != nil {
|
---|
| 709 | return nil, err
|
---|
| 710 | }
|
---|
| 711 |
|
---|
[313] | 712 | network := u.getNetworkByID(record.ID)
|
---|
| 713 | if network == nil {
|
---|
| 714 | panic("tried updating a non-existing network")
|
---|
| 715 | }
|
---|
| 716 |
|
---|
[421] | 717 | if err := u.srv.db.StoreNetwork(u.ID, record); err != nil {
|
---|
[313] | 718 | return nil, err
|
---|
| 719 | }
|
---|
| 720 |
|
---|
| 721 | // Most network changes require us to re-connect to the upstream server
|
---|
| 722 |
|
---|
[478] | 723 | channels := make([]Channel, 0, network.channels.Len())
|
---|
| 724 | for _, entry := range network.channels.innerMap {
|
---|
| 725 | ch := entry.value.(*Channel)
|
---|
[313] | 726 | channels = append(channels, *ch)
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | updatedNetwork := newNetwork(u, record, channels)
|
---|
| 730 |
|
---|
| 731 | // If we're currently connected, disconnect and perform the necessary
|
---|
| 732 | // bookkeeping
|
---|
| 733 | if network.conn != nil {
|
---|
| 734 | network.stop()
|
---|
| 735 | // Note: this will set network.conn to nil
|
---|
| 736 | u.handleUpstreamDisconnected(network.conn)
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | // Patch downstream connections to use our fresh updated network
|
---|
| 740 | u.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 741 | if dc.network != nil && dc.network == network {
|
---|
| 742 | dc.network = updatedNetwork
|
---|
[202] | 743 | }
|
---|
[313] | 744 | })
|
---|
[202] | 745 |
|
---|
[313] | 746 | // We need to remove the network after patching downstream connections,
|
---|
| 747 | // otherwise they'll get closed
|
---|
| 748 | u.removeNetwork(network)
|
---|
[202] | 749 |
|
---|
[313] | 750 | // This will re-connect to the upstream server
|
---|
| 751 | u.addNetwork(updatedNetwork)
|
---|
| 752 |
|
---|
| 753 | return updatedNetwork, nil
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | func (u *user) deleteNetwork(id int64) error {
|
---|
| 757 | network := u.getNetworkByID(id)
|
---|
| 758 | if network == nil {
|
---|
| 759 | panic("tried deleting a non-existing network")
|
---|
[202] | 760 | }
|
---|
| 761 |
|
---|
[313] | 762 | if err := u.srv.db.DeleteNetwork(network.ID); err != nil {
|
---|
| 763 | return err
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | u.removeNetwork(network)
|
---|
| 767 | return nil
|
---|
[202] | 768 | }
|
---|
[252] | 769 |
|
---|
| 770 | func (u *user) updatePassword(hashed string) error {
|
---|
| 771 | u.User.Password = hashed
|
---|
[324] | 772 | return u.srv.db.StoreUser(&u.User)
|
---|
[252] | 773 | }
|
---|
[376] | 774 |
|
---|
| 775 | func (u *user) stop() {
|
---|
| 776 | u.events <- eventStop{}
|
---|
[377] | 777 | <-u.done
|
---|
[376] | 778 | }
|
---|
[489] | 779 |
|
---|
| 780 | func (u *user) hasPersistentMsgStore() bool {
|
---|
| 781 | if u.msgStore == nil {
|
---|
| 782 | return false
|
---|
| 783 | }
|
---|
| 784 | _, isMem := u.msgStore.(*memoryMessageStore)
|
---|
| 785 | return !isMem
|
---|
| 786 | }
|
---|