瀏覽代碼

fix #1490

Track channel join times, use them to optionally enforce history access
restrictions
tags/v2.5.0-rc1
Shivaram Lingamneni 3 年之前
父節點
當前提交
4a48e52518
共有 12 個檔案被更改,包括 275 行新增93 行删除
  1. 12
    7
      default.yaml
  2. 7
    2
      irc/accounts.go
  3. 73
    45
      irc/channel.go
  4. 25
    1
      irc/chanserv.go
  5. 7
    7
      irc/client.go
  6. 58
    3
      irc/config.go
  7. 51
    1
      irc/database.go
  8. 1
    1
      irc/getters.go
  9. 3
    3
      irc/handlers.go
  10. 15
    6
      irc/server.go
  11. 15
    13
      irc/types.go
  12. 8
    4
      traditional.yaml

+ 12
- 7
default.yaml 查看文件

@@ -878,13 +878,18 @@ history:
878 878
         # (and will eventually be deleted from persistent storage, if that's enabled)
879 879
         expire-time: 1w
880 880
 
881
-        # if this is set, logged-in users cannot retrieve messages older than their
882
-        # account registration date, and logged-out users cannot retrieve messages
883
-        # older than their sign-on time (modulo grace-period, see below):
884
-        enforce-registration-date: false
885
-
886
-        # but if this is set, you can retrieve messages that are up to `grace-period`
887
-        # older than the above cutoff time. this is recommended to allow logged-out
881
+        # this restricts access to channel history (it can be overridden by channel
882
+        # owners). options are: 'none' (no restrictions), 'registration-time'
883
+        # (logged-in users cannot retrieve messages older than their account
884
+        # registration date, and anonymous users cannot retrieve messages older than
885
+        # their sign-on time, modulo the grace-period described below), and
886
+        # 'join-time' (users cannot retrieve messages older than the time they
887
+        # joined the channel, so only always-on clients can view history).
888
+        query-cutoff: 'none'
889
+
890
+        # if query-cutoff is set to 'registration-time', this allows retrieval
891
+        # of messages that are up to 'grace-period' older than the above cutoff.
892
+        # if you use 'registration-time', this is recommended to allow logged-out
888 893
         # users to do session resumption / query history after disconnections.
889 894
         grace-period: 1h
890 895
 

+ 7
- 2
irc/accounts.go 查看文件

@@ -544,7 +544,12 @@ func (am *AccountManager) setPassword(account string, password string, hasPrivs
544 544
 	return err
545 545
 }
546 546
 
547
-func (am *AccountManager) saveChannels(account string, channelToModes map[string]string) {
547
+type alwaysOnChannelStatus struct {
548
+	Modes    string
549
+	JoinTime int64
550
+}
551
+
552
+func (am *AccountManager) saveChannels(account string, channelToModes map[string]alwaysOnChannelStatus) {
548 553
 	j, err := json.Marshal(channelToModes)
549 554
 	if err != nil {
550 555
 		am.server.logger.Error("internal", "couldn't marshal channel-to-modes", account, err.Error())
@@ -558,7 +563,7 @@ func (am *AccountManager) saveChannels(account string, channelToModes map[string
558 563
 	})
559 564
 }
560 565
 
561
-func (am *AccountManager) loadChannels(account string) (channelToModes map[string]string) {
566
+func (am *AccountManager) loadChannels(account string) (channelToModes map[string]alwaysOnChannelStatus) {
562 567
 	key := fmt.Sprintf(keyAccountChannelToModes, account)
563 568
 	var channelsStr string
564 569
 	am.server.store.View(func(tx *buntdb.Tx) error {

+ 73
- 45
irc/channel.go 查看文件

@@ -20,7 +20,8 @@ import (
20 20
 )
21 21
 
22 22
 type ChannelSettings struct {
23
-	History HistoryStatus
23
+	History     HistoryStatus
24
+	QueryCutoff HistoryCutoff
24 25
 }
25 26
 
26 27
 // Channel represents a channel that clients can join.
@@ -109,7 +110,7 @@ func (channel *Channel) IsLoaded() bool {
109 110
 }
110 111
 
111 112
 func (channel *Channel) resizeHistory(config *Config) {
112
-	status, _ := channel.historyStatus(config)
113
+	status, _, _ := channel.historyStatus(config)
113 114
 	if status == HistoryEphemeral {
114 115
 		channel.history.Resize(config.History.ChannelLength, time.Duration(config.History.AutoresizeWindow))
115 116
 	} else {
@@ -443,11 +444,11 @@ func (channel *Channel) regenerateMembersCache() {
443 444
 // Names sends the list of users joined to the channel to the given client.
444 445
 func (channel *Channel) Names(client *Client, rb *ResponseBuffer) {
445 446
 	channel.stateMutex.RLock()
446
-	clientModes, isJoined := channel.members[client]
447
+	clientData, isJoined := channel.members[client]
447 448
 	channel.stateMutex.RUnlock()
448 449
 	isOper := client.HasMode(modes.Operator)
449 450
 	respectAuditorium := channel.flags.HasMode(modes.Auditorium) && !isOper &&
450
-		(!isJoined || clientModes.HighestChannelUserMode() == modes.Mode(0))
451
+		(!isJoined || clientData.modes.HighestChannelUserMode() == modes.Mode(0))
451 452
 	isMultiPrefix := rb.session.capabilities.Has(caps.MultiPrefix)
452 453
 	isUserhostInNames := rb.session.capabilities.Has(caps.UserhostInNames)
453 454
 
@@ -463,8 +464,9 @@ func (channel *Channel) Names(client *Client, rb *ResponseBuffer) {
463 464
 				nick = target.Nick()
464 465
 			}
465 466
 			channel.stateMutex.RLock()
466
-			modeSet := channel.members[target]
467
+			memberData, _ := channel.members[target]
467 468
 			channel.stateMutex.RUnlock()
469
+			modeSet := memberData.modes
468 470
 			if modeSet == nil {
469 471
 				continue
470 472
 			}
@@ -519,7 +521,7 @@ func channelUserModeHasPrivsOver(clientMode modes.Mode, targetMode modes.Mode) b
519 521
 // ClientIsAtLeast returns whether the client has at least the given channel privilege.
520 522
 func (channel *Channel) ClientIsAtLeast(client *Client, permission modes.Mode) bool {
521 523
 	channel.stateMutex.RLock()
522
-	clientModes := channel.members[client]
524
+	memberData := channel.members[client]
523 525
 	founder := channel.registeredFounder
524 526
 	channel.stateMutex.RUnlock()
525 527
 
@@ -528,7 +530,7 @@ func (channel *Channel) ClientIsAtLeast(client *Client, permission modes.Mode) b
528 530
 	}
529 531
 
530 532
 	for _, mode := range modes.ChannelUserModes {
531
-		if clientModes.HasMode(mode) {
533
+		if memberData.modes.HasMode(mode) {
532 534
 			return true
533 535
 		}
534 536
 		if mode == permission {
@@ -541,35 +543,37 @@ func (channel *Channel) ClientIsAtLeast(client *Client, permission modes.Mode) b
541 543
 func (channel *Channel) ClientPrefixes(client *Client, isMultiPrefix bool) string {
542 544
 	channel.stateMutex.RLock()
543 545
 	defer channel.stateMutex.RUnlock()
544
-	modes, present := channel.members[client]
546
+	memberData, present := channel.members[client]
545 547
 	if !present {
546 548
 		return ""
547 549
 	} else {
548
-		return modes.Prefixes(isMultiPrefix)
550
+		return memberData.modes.Prefixes(isMultiPrefix)
549 551
 	}
550 552
 }
551 553
 
552
-func (channel *Channel) ClientStatus(client *Client) (present bool, cModes modes.Modes) {
554
+func (channel *Channel) ClientStatus(client *Client) (present bool, joinTimeSecs int64, cModes modes.Modes) {
553 555
 	channel.stateMutex.RLock()
554 556
 	defer channel.stateMutex.RUnlock()
555
-	modes, present := channel.members[client]
556
-	return present, modes.AllModes()
557
+	memberData, present := channel.members[client]
558
+	return present, time.Unix(0, memberData.joinTime).Unix(), memberData.modes.AllModes()
557 559
 }
558 560
 
559 561
 // helper for persisting channel-user modes for always-on clients;
560 562
 // return the channel name and all channel-user modes for a client
561
-func (channel *Channel) nameAndModes(client *Client) (chname string, modeStr string) {
563
+func (channel *Channel) alwaysOnStatus(client *Client) (chname string, status alwaysOnChannelStatus) {
562 564
 	channel.stateMutex.RLock()
563 565
 	defer channel.stateMutex.RUnlock()
564 566
 	chname = channel.name
565
-	modeStr = channel.members[client].String()
567
+	data := channel.members[client]
568
+	status.Modes = data.modes.String()
569
+	status.JoinTime = data.joinTime
566 570
 	return
567 571
 }
568 572
 
569 573
 // overwrite any existing channel-user modes with the stored ones
570
-func (channel *Channel) setModesForClient(client *Client, modeStr string) {
574
+func (channel *Channel) setMemberStatus(client *Client, status alwaysOnChannelStatus) {
571 575
 	newModes := modes.NewModeSet()
572
-	for _, mode := range modeStr {
576
+	for _, mode := range status.Modes {
573 577
 		newModes.SetMode(modes.Mode(mode), true)
574 578
 	}
575 579
 	channel.stateMutex.Lock()
@@ -577,14 +581,17 @@ func (channel *Channel) setModesForClient(client *Client, modeStr string) {
577 581
 	if _, ok := channel.members[client]; !ok {
578 582
 		return
579 583
 	}
580
-	channel.members[client] = newModes
584
+	memberData := channel.members[client]
585
+	memberData.modes = newModes
586
+	memberData.joinTime = status.JoinTime
587
+	channel.members[client] = memberData
581 588
 }
582 589
 
583 590
 func (channel *Channel) ClientHasPrivsOver(client *Client, target *Client) bool {
584 591
 	channel.stateMutex.RLock()
585 592
 	founder := channel.registeredFounder
586
-	clientModes := channel.members[client]
587
-	targetModes := channel.members[target]
593
+	clientModes := channel.members[client].modes
594
+	targetModes := channel.members[target].modes
588 595
 	channel.stateMutex.RUnlock()
589 596
 
590 597
 	if founder != "" {
@@ -612,7 +619,7 @@ func (channel *Channel) modeStrings(client *Client) (result []string) {
612 619
 	channel.stateMutex.RLock()
613 620
 	defer channel.stateMutex.RUnlock()
614 621
 
615
-	isMember := hasPrivs || channel.members[client] != nil
622
+	isMember := hasPrivs || channel.members.Has(client)
616 623
 	showKey := isMember && (channel.key != "")
617 624
 	showUserLimit := channel.userLimit > 0
618 625
 	showForward := channel.forward != ""
@@ -660,18 +667,38 @@ func (channel *Channel) IsEmpty() bool {
660 667
 
661 668
 // figure out where history is being stored: persistent, ephemeral, or neither
662 669
 // target is only needed if we're doing persistent history
663
-func (channel *Channel) historyStatus(config *Config) (status HistoryStatus, target string) {
670
+func (channel *Channel) historyStatus(config *Config) (status HistoryStatus, target string, restrictions HistoryCutoff) {
664 671
 	if !config.History.Enabled {
665
-		return HistoryDisabled, ""
672
+		return HistoryDisabled, "", HistoryCutoffNone
666 673
 	}
667 674
 
668 675
 	channel.stateMutex.RLock()
669 676
 	target = channel.nameCasefolded
670
-	historyStatus := channel.settings.History
677
+	settings := channel.settings
671 678
 	registered := channel.registeredFounder != ""
672 679
 	channel.stateMutex.RUnlock()
673 680
 
674
-	return channelHistoryStatus(config, registered, historyStatus), target
681
+	restrictions = settings.QueryCutoff
682
+	if restrictions == HistoryCutoffDefault {
683
+		restrictions = config.History.Restrictions.queryCutoff
684
+	}
685
+
686
+	return channelHistoryStatus(config, registered, settings.History), target, restrictions
687
+}
688
+
689
+func (channel *Channel) joinTimeCutoff(client *Client) (present bool, cutoff time.Time) {
690
+	account := client.Account()
691
+
692
+	channel.stateMutex.RLock()
693
+	defer channel.stateMutex.RUnlock()
694
+	if data, ok := channel.members[client]; ok {
695
+		present = true
696
+		// report a cutoff of zero, i.e., no restriction, if the user is privileged
697
+		if !((account != "" && account == channel.registeredFounder) || data.modes.HasMode(modes.ChannelFounder) || data.modes.HasMode(modes.ChannelAdmin) || data.modes.HasMode(modes.ChannelOperator)) {
698
+			cutoff = time.Unix(0, data.joinTime)
699
+		}
700
+	}
701
+	return
675 702
 }
676 703
 
677 704
 func channelHistoryStatus(config *Config, registered bool, storedStatus HistoryStatus) (result HistoryStatus) {
@@ -697,7 +724,7 @@ func (channel *Channel) AddHistoryItem(item history.Item, account string) (err e
697 724
 		return
698 725
 	}
699 726
 
700
-	status, target := channel.historyStatus(channel.server.Config())
727
+	status, target, _ := channel.historyStatus(channel.server.Config())
701 728
 	if status == HistoryPersistent {
702 729
 		err = channel.server.historyDB.AddChannelItem(target, item, account)
703 730
 	} else if status == HistoryEphemeral {
@@ -785,7 +812,7 @@ func (channel *Channel) Join(client *Client, key string, isSajoin bool, rb *Resp
785 812
 				givenMode = persistentMode
786 813
 			}
787 814
 			if givenMode != 0 {
788
-				channel.members[client].SetMode(givenMode, true)
815
+				channel.members[client].modes.SetMode(givenMode, true)
789 816
 			}
790 817
 		}()
791 818
 
@@ -825,9 +852,9 @@ func (channel *Channel) Join(client *Client, key string, isSajoin bool, rb *Resp
825 852
 	for _, member := range channel.Members() {
826 853
 		if respectAuditorium {
827 854
 			channel.stateMutex.RLock()
828
-			memberModes, ok := channel.members[member]
855
+			memberData, ok := channel.members[member]
829 856
 			channel.stateMutex.RUnlock()
830
-			if !ok || memberModes.HighestChannelUserMode() == modes.Mode(0) {
857
+			if !ok || memberData.modes.HighestChannelUserMode() == modes.Mode(0) {
831 858
 				continue
832 859
 			}
833 860
 		}
@@ -955,7 +982,7 @@ func (channel *Channel) playJoinForSession(session *Session) {
955 982
 func (channel *Channel) Part(client *Client, message string, rb *ResponseBuffer) {
956 983
 	channel.stateMutex.RLock()
957 984
 	chname := channel.name
958
-	clientModes, ok := channel.members[client]
985
+	clientData, ok := channel.members[client]
959 986
 	channel.stateMutex.RUnlock()
960 987
 
961 988
 	if !ok {
@@ -974,15 +1001,15 @@ func (channel *Channel) Part(client *Client, message string, rb *ResponseBuffer)
974 1001
 		params = append(params, message)
975 1002
 	}
976 1003
 	respectAuditorium := channel.flags.HasMode(modes.Auditorium) &&
977
-		clientModes.HighestChannelUserMode() == modes.Mode(0)
1004
+		clientData.modes.HighestChannelUserMode() == modes.Mode(0)
978 1005
 	var cache MessageCache
979 1006
 	cache.Initialize(channel.server, splitMessage.Time, splitMessage.Msgid, details.nickMask, details.accountName, nil, "PART", params...)
980 1007
 	for _, member := range channel.Members() {
981 1008
 		if respectAuditorium {
982 1009
 			channel.stateMutex.RLock()
983
-			memberModes, ok := channel.members[member]
1010
+			memberData, ok := channel.members[member]
984 1011
 			channel.stateMutex.RUnlock()
985
-			if !ok || memberModes.HighestChannelUserMode() == modes.Mode(0) {
1012
+			if !ok || memberData.modes.HighestChannelUserMode() == modes.Mode(0) {
986 1013
 				continue
987 1014
 			}
988 1015
 		}
@@ -1022,12 +1049,12 @@ func (channel *Channel) Resume(session *Session, timestamp time.Time) {
1022 1049
 
1023 1050
 func (channel *Channel) resumeAndAnnounce(session *Session) {
1024 1051
 	channel.stateMutex.RLock()
1025
-	modeSet := channel.members[session.client]
1052
+	memberData, found := channel.members[session.client]
1026 1053
 	channel.stateMutex.RUnlock()
1027
-	if modeSet == nil {
1054
+	if !found {
1028 1055
 		return
1029 1056
 	}
1030
-	oldModes := modeSet.String()
1057
+	oldModes := memberData.modes.String()
1031 1058
 	if 0 < len(oldModes) {
1032 1059
 		oldModes = "+" + oldModes
1033 1060
 	}
@@ -1271,8 +1298,9 @@ func (channel *Channel) SetTopic(client *Client, topic string, rb *ResponseBuffe
1271 1298
 // CanSpeak returns true if the client can speak on this channel, otherwise it returns false along with the channel mode preventing the client from speaking.
1272 1299
 func (channel *Channel) CanSpeak(client *Client) (bool, modes.Mode) {
1273 1300
 	channel.stateMutex.RLock()
1274
-	clientModes, hasClient := channel.members[client]
1301
+	memberData, hasClient := channel.members[client]
1275 1302
 	channel.stateMutex.RUnlock()
1303
+	clientModes := memberData.modes
1276 1304
 
1277 1305
 	if !hasClient && channel.flags.HasMode(modes.NoOutside) {
1278 1306
 		// TODO: enforce regular +b bans on -n channels?
@@ -1347,9 +1375,9 @@ func (channel *Channel) SendSplitMessage(command string, minPrefixMode modes.Mod
1347 1375
 
1348 1376
 	if channel.flags.HasMode(modes.OpModerated) {
1349 1377
 		channel.stateMutex.RLock()
1350
-		cuModes := channel.members[client]
1378
+		cuData := channel.members[client]
1351 1379
 		channel.stateMutex.RUnlock()
1352
-		if cuModes.HighestChannelUserMode() == modes.Mode(0) {
1380
+		if cuData.modes.HighestChannelUserMode() == modes.Mode(0) {
1353 1381
 			// max(statusmsg_minmode, halfop)
1354 1382
 			if minPrefixMode == modes.Mode(0) || minPrefixMode == modes.Voice {
1355 1383
 				minPrefixMode = modes.Halfop
@@ -1402,9 +1430,9 @@ func (channel *Channel) applyModeToMember(client *Client, change modes.ModeChang
1402 1430
 	change.Arg = target.Nick()
1403 1431
 
1404 1432
 	channel.stateMutex.Lock()
1405
-	modeset, exists := channel.members[target]
1433
+	memberData, exists := channel.members[target]
1406 1434
 	if exists {
1407
-		if modeset.SetMode(change.Mode, change.Op == modes.Add) {
1435
+		if memberData.modes.SetMode(change.Mode, change.Op == modes.Add) {
1408 1436
 			applied = true
1409 1437
 			result = change
1410 1438
 		}
@@ -1590,19 +1618,19 @@ func (channel *Channel) auditoriumFriends(client *Client) (friends []*Client) {
1590 1618
 	channel.stateMutex.RLock()
1591 1619
 	defer channel.stateMutex.RUnlock()
1592 1620
 
1593
-	clientModes := channel.members[client]
1594
-	if clientModes == nil {
1621
+	clientData, found := channel.members[client]
1622
+	if !found {
1595 1623
 		return // non-members have no friends
1596 1624
 	}
1597 1625
 	if !channel.flags.HasMode(modes.Auditorium) {
1598 1626
 		return channel.membersCache // default behavior for members
1599 1627
 	}
1600
-	if clientModes.HighestChannelUserMode() != modes.Mode(0) {
1628
+	if clientData.modes.HighestChannelUserMode() != modes.Mode(0) {
1601 1629
 		return channel.membersCache // +v and up can see everyone in the auditorium
1602 1630
 	}
1603 1631
 	// without +v, your friends are those with +v and up
1604
-	for member, memberModes := range channel.members {
1605
-		if memberModes.HighestChannelUserMode() != modes.Mode(0) {
1632
+	for member, memberData := range channel.members {
1633
+		if memberData.modes.HighestChannelUserMode() != modes.Mode(0) {
1606 1634
 			friends = append(friends, member)
1607 1635
 		}
1608 1636
 	}

+ 25
- 1
irc/chanserv.go 查看文件

@@ -171,6 +171,16 @@ SET modifies a channel's settings. The following settings are available:`,
171 171
 2. 'ephemeral'  [a limited amount of temporary history, not stored on disk]
172 172
 3. 'on'         [history stored in a permanent database, if available]
173 173
 4. 'default'    [use the server default]`,
174
+				`$bQUERY-CUTOFF$b
175
+'query-cutoff' lets you restrict how much channel history can be retrieved
176
+by unprivileged users. Your options are:
177
+1. 'none'               [no restrictions]
178
+2. 'registration-time'  [users can view history from after their account was
179
+                         registered, plus a grace period]
180
+3. 'join-time'          [users can biew history from after they joined the
181
+                         channel; note that history will be effectively
182
+                         unavailable to clients that are not always-on]
183
+4. 'default'            [use the server default]`,
174 184
 			},
175 185
 			enabled:   chanregEnabled,
176 186
 			minParams: 3,
@@ -329,7 +339,7 @@ func csDeopHandler(service *ircService, server *Server, client *Client, command
329 339
 		target = client
330 340
 	}
331 341
 
332
-	present, cumodes := channel.ClientStatus(target)
342
+	present, _, cumodes := channel.ClientStatus(target)
333 343
 	if !present || len(cumodes) == 0 {
334 344
 		service.Notice(rb, client.t("Target has no privileges to remove"))
335 345
 		return
@@ -745,6 +755,13 @@ func displayChannelSetting(service *ircService, settingName string, settings Cha
745 755
 		effectiveValue := historyEnabled(config.History.Persistent.RegisteredChannels, settings.History)
746 756
 		service.Notice(rb, fmt.Sprintf(client.t("The stored channel history setting is: %s"), historyStatusToString(settings.History)))
747 757
 		service.Notice(rb, fmt.Sprintf(client.t("Given current server settings, the channel history setting is: %s"), historyStatusToString(effectiveValue)))
758
+	case "query-cutoff":
759
+		effectiveValue := settings.QueryCutoff
760
+		if effectiveValue == HistoryCutoffDefault {
761
+			effectiveValue = config.History.Restrictions.queryCutoff
762
+		}
763
+		service.Notice(rb, fmt.Sprintf(client.t("The stored channel history query cutoff setting is: %s"), historyCutoffToString(settings.QueryCutoff)))
764
+		service.Notice(rb, fmt.Sprintf(client.t("Given current server settings, the channel history query cutoff setting is: %s"), historyCutoffToString(effectiveValue)))
748 765
 	default:
749 766
 		service.Notice(rb, client.t("Invalid params"))
750 767
 	}
@@ -788,6 +805,13 @@ func csSetHandler(service *ircService, server *Server, client *Client, command s
788 805
 		}
789 806
 		channel.SetSettings(settings)
790 807
 		channel.resizeHistory(server.Config())
808
+	case "query-cutoff":
809
+		settings.QueryCutoff, err = historyCutoffFromString(value)
810
+		if err != nil {
811
+			err = errInvalidParams
812
+			break
813
+		}
814
+		channel.SetSettings(settings)
791 815
 	}
792 816
 
793 817
 	switch err {

+ 7
- 7
irc/client.go 查看文件

@@ -407,7 +407,7 @@ func (server *Server) RunClient(conn IRCConn) {
407 407
 	client.run(session)
408 408
 }
409 409
 
410
-func (server *Server) AddAlwaysOnClient(account ClientAccount, channelToModes map[string]string, lastSeen map[string]time.Time, uModes modes.Modes, realname string) {
410
+func (server *Server) AddAlwaysOnClient(account ClientAccount, channelToStatus map[string]alwaysOnChannelStatus, lastSeen map[string]time.Time, uModes modes.Modes, realname string) {
411 411
 	now := time.Now().UTC()
412 412
 	config := server.Config()
413 413
 	if lastSeen == nil && account.Settings.AutoreplayMissed {
@@ -471,12 +471,12 @@ func (server *Server) AddAlwaysOnClient(account ClientAccount, channelToModes ma
471 471
 	// XXX set this last to avoid confusing SetNick:
472 472
 	client.registered = true
473 473
 
474
-	for chname, modeStr := range channelToModes {
474
+	for chname, status := range channelToStatus {
475 475
 		// XXX we're using isSajoin=true, to make these joins succeed even without channel key
476 476
 		// this is *probably* ok as long as the persisted memberships are accurate
477 477
 		server.channels.Join(client, chname, "", true, nil)
478 478
 		if channel := server.channels.Get(chname); channel != nil {
479
-			channel.setModesForClient(client, modeStr)
479
+			channel.setMemberStatus(client, status)
480 480
 		} else {
481 481
 			server.logger.Error("internal", "could not create channel", chname)
482 482
 		}
@@ -966,7 +966,7 @@ func (session *Session) playResume() {
966 966
 		for _, member := range channel.auditoriumFriends(client) {
967 967
 			friends.Add(member)
968 968
 		}
969
-		status, _ := channel.historyStatus(config)
969
+		status, _, _ := channel.historyStatus(config)
970 970
 		if status == HistoryEphemeral {
971 971
 			lastDiscarded := channel.history.LastDiscarded()
972 972
 			if oldestLostMessage.Before(lastDiscarded) {
@@ -2000,10 +2000,10 @@ func (client *Client) performWrite(additionalDirtyBits uint) {
2000 2000
 
2001 2001
 	if (dirtyBits & IncludeChannels) != 0 {
2002 2002
 		channels := client.Channels()
2003
-		channelToModes := make(map[string]string, len(channels))
2003
+		channelToModes := make(map[string]alwaysOnChannelStatus, len(channels))
2004 2004
 		for _, channel := range channels {
2005
-			chname, modes := channel.nameAndModes(client)
2006
-			channelToModes[chname] = modes
2005
+			chname, status := channel.alwaysOnStatus(client)
2006
+			channelToModes[chname] = status
2007 2007
 		}
2008 2008
 		client.server.accounts.saveChannels(account, channelToModes)
2009 2009
 	}

+ 58
- 3
irc/config.go 查看文件

@@ -62,6 +62,45 @@ type listenerConfigBlock struct {
62 62
 	HideSTS   bool `yaml:"hide-sts"`
63 63
 }
64 64
 
65
+type HistoryCutoff uint
66
+
67
+const (
68
+	HistoryCutoffDefault HistoryCutoff = iota
69
+	HistoryCutoffNone
70
+	HistoryCutoffRegistrationTime
71
+	HistoryCutoffJoinTime
72
+)
73
+
74
+func historyCutoffToString(restriction HistoryCutoff) string {
75
+	switch restriction {
76
+	case HistoryCutoffDefault:
77
+		return "default"
78
+	case HistoryCutoffNone:
79
+		return "none"
80
+	case HistoryCutoffRegistrationTime:
81
+		return "registration-time"
82
+	case HistoryCutoffJoinTime:
83
+		return "join-time"
84
+	default:
85
+		return ""
86
+	}
87
+}
88
+
89
+func historyCutoffFromString(str string) (result HistoryCutoff, err error) {
90
+	switch strings.ToLower(str) {
91
+	case "default":
92
+		return HistoryCutoffDefault, nil
93
+	case "none", "disabled", "off", "false":
94
+		return HistoryCutoffNone, nil
95
+	case "registration-time":
96
+		return HistoryCutoffRegistrationTime, nil
97
+	case "join-time":
98
+		return HistoryCutoffJoinTime, nil
99
+	default:
100
+		return HistoryCutoffDefault, errInvalidParams
101
+	}
102
+}
103
+
65 104
 type PersistentStatus uint
66 105
 
67 106
 const (
@@ -615,9 +654,12 @@ type Config struct {
615 654
 		ChathistoryMax   int              `yaml:"chathistory-maxmessages"`
616 655
 		ZNCMax           int              `yaml:"znc-maxmessages"`
617 656
 		Restrictions     struct {
618
-			ExpireTime              custime.Duration `yaml:"expire-time"`
619
-			EnforceRegistrationDate bool             `yaml:"enforce-registration-date"`
620
-			GracePeriod             custime.Duration `yaml:"grace-period"`
657
+			ExpireTime custime.Duration `yaml:"expire-time"`
658
+			// legacy key, superceded by QueryCutoff:
659
+			EnforceRegistrationDate_ bool   `yaml:"enforce-registration-date"`
660
+			QueryCutoff              string `yaml:"query-cutoff"`
661
+			queryCutoff              HistoryCutoff
662
+			GracePeriod              custime.Duration `yaml:"grace-period"`
621 663
 		}
622 664
 		Persistent struct {
623 665
 			Enabled              bool
@@ -1354,6 +1396,19 @@ func LoadConfig(filename string) (config *Config, err error) {
1354 1396
 		config.History.ZNCMax = config.History.ChathistoryMax
1355 1397
 	}
1356 1398
 
1399
+	if config.History.Restrictions.QueryCutoff != "" {
1400
+		config.History.Restrictions.queryCutoff, err = historyCutoffFromString(config.History.Restrictions.QueryCutoff)
1401
+		if err != nil {
1402
+			return nil, fmt.Errorf("invalid value of history.query-restrictions: %w", err)
1403
+		}
1404
+	} else {
1405
+		if config.History.Restrictions.EnforceRegistrationDate_ {
1406
+			config.History.Restrictions.queryCutoff = HistoryCutoffRegistrationTime
1407
+		} else {
1408
+			config.History.Restrictions.queryCutoff = HistoryCutoffNone
1409
+		}
1410
+	}
1411
+
1357 1412
 	config.Roleplay.addSuffix = utils.BoolDefaultTrue(config.Roleplay.AddSuffix)
1358 1413
 
1359 1414
 	config.Datastore.MySQL.ExpireTime = time.Duration(config.History.Restrictions.ExpireTime)

+ 51
- 1
irc/database.go 查看文件

@@ -24,7 +24,7 @@ const (
24 24
 	// 'version' of the database schema
25 25
 	keySchemaVersion = "db.version"
26 26
 	// latest schema of the db
27
-	latestDbSchema = 19
27
+	latestDbSchema = 20
28 28
 
29 29
 	keyCloakSecret = "crypto.cloak_secret"
30 30
 )
@@ -963,6 +963,51 @@ func schemaChangeV18To19(config *Config, tx *buntdb.Tx) error {
963 963
 	return nil
964 964
 }
965 965
 
966
+// #1490: start tracking join times for always-on clients
967
+func schemaChangeV19To20(config *Config, tx *buntdb.Tx) error {
968
+	type joinData struct {
969
+		Modes    string
970
+		JoinTime int64
971
+	}
972
+
973
+	var accounts []string
974
+	var data []string
975
+
976
+	now := time.Now().UnixNano()
977
+
978
+	prefix := "account.channeltomodes "
979
+	tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
980
+		if !strings.HasPrefix(key, prefix) {
981
+			return false
982
+		}
983
+		accounts = append(accounts, strings.TrimPrefix(key, prefix))
984
+		data = append(data, value)
985
+		return true
986
+	})
987
+
988
+	for i, account := range accounts {
989
+		var existingMap map[string]string
990
+		err := json.Unmarshal([]byte(data[i]), &existingMap)
991
+		if err != nil {
992
+			return err
993
+		}
994
+		newMap := make(map[string]joinData)
995
+		for channel, modeStr := range existingMap {
996
+			newMap[channel] = joinData{
997
+				Modes:    modeStr,
998
+				JoinTime: now,
999
+			}
1000
+		}
1001
+		serialized, err := json.Marshal(newMap)
1002
+		if err != nil {
1003
+			return err
1004
+		}
1005
+		tx.Set(prefix+account, string(serialized), nil)
1006
+	}
1007
+
1008
+	return nil
1009
+}
1010
+
966 1011
 func getSchemaChange(initialVersion int) (result SchemaChange, ok bool) {
967 1012
 	for _, change := range allChanges {
968 1013
 		if initialVersion == change.InitialVersion {
@@ -1063,4 +1108,9 @@ var allChanges = []SchemaChange{
1063 1108
 		TargetVersion:  19,
1064 1109
 		Changer:        schemaChangeV18To19,
1065 1110
 	},
1111
+	{
1112
+		InitialVersion: 19,
1113
+		TargetVersion:  20,
1114
+		Changer:        schemaChangeV19To20,
1115
+	},
1066 1116
 }

+ 1
- 1
irc/getters.go 查看文件

@@ -522,7 +522,7 @@ func (channel *Channel) Founder() string {
522 522
 
523 523
 func (channel *Channel) HighestUserMode(client *Client) (result modes.Mode) {
524 524
 	channel.stateMutex.RLock()
525
-	clientModes := channel.members[client]
525
+	clientModes := channel.members[client].modes
526 526
 	channel.stateMutex.RUnlock()
527 527
 	return clientModes.HighestChannelUserMode()
528 528
 }

+ 3
- 3
irc/handlers.go 查看文件

@@ -985,8 +985,8 @@ func extjwtHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Re
985 985
 		claims["channel"] = channel.Name()
986 986
 		claims["joined"] = 0
987 987
 		claims["cmodes"] = []string{}
988
-		if present, cModes := channel.ClientStatus(client); present {
989
-			claims["joined"] = 1
988
+		if present, joinTimeSecs, cModes := channel.ClientStatus(client); present {
989
+			claims["joined"] = joinTimeSecs
990 990
 			var modeStrings []string
991 991
 			for _, cMode := range cModes {
992 992
 				modeStrings = append(modeStrings, string(cMode))
@@ -2649,7 +2649,7 @@ func renameHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Re
2649 2649
 	}
2650 2650
 
2651 2651
 	config := server.Config()
2652
-	status, _ := channel.historyStatus(config)
2652
+	status, _, _ := channel.historyStatus(config)
2653 2653
 	if status == HistoryPersistent {
2654 2654
 		rb.Add(nil, server.name, "FAIL", "RENAME", "CANNOT_RENAME", oldName, utils.SafeErrorParam(newName), client.t("Channels with persistent history cannot be renamed"))
2655 2655
 		return false

+ 15
- 6
irc/server.go 查看文件

@@ -850,6 +850,7 @@ func (server *Server) GetHistorySequence(providedChannel *Channel, client *Clien
850 850
 	var status HistoryStatus
851 851
 	var target, correspondent string
852 852
 	var hist *history.Buffer
853
+	restriction := HistoryCutoffNone
853 854
 	channel = providedChannel
854 855
 	if channel == nil {
855 856
 		if strings.HasPrefix(query, "#") {
@@ -859,12 +860,15 @@ func (server *Server) GetHistorySequence(providedChannel *Channel, client *Clien
859 860
 			}
860 861
 		}
861 862
 	}
863
+	var joinTimeCutoff time.Time
862 864
 	if channel != nil {
863
-		if !channel.hasClient(client) {
865
+		if present, cutoff := channel.joinTimeCutoff(client); present {
866
+			joinTimeCutoff = cutoff
867
+		} else {
864 868
 			err = errInsufficientPrivs
865 869
 			return
866 870
 		}
867
-		status, target = channel.historyStatus(config)
871
+		status, target, restriction = channel.historyStatus(config)
868 872
 		switch status {
869 873
 		case HistoryEphemeral:
870 874
 			hist = &channel.history
@@ -896,15 +900,20 @@ func (server *Server) GetHistorySequence(providedChannel *Channel, client *Clien
896 900
 		cutoff = time.Now().UTC().Add(-time.Duration(config.History.Restrictions.ExpireTime))
897 901
 	}
898 902
 	// #836: registration date cutoff is always enforced for DMs
899
-	if config.History.Restrictions.EnforceRegistrationDate || channel == nil {
903
+	// either way, take the later of the two cutoffs
904
+	if restriction == HistoryCutoffRegistrationTime || channel == nil {
900 905
 		regCutoff := client.historyCutoff()
901
-		// take the later of the two cutoffs
902 906
 		if regCutoff.After(cutoff) {
903 907
 			cutoff = regCutoff
904 908
 		}
909
+	} else if restriction == HistoryCutoffJoinTime {
910
+		if joinTimeCutoff.After(cutoff) {
911
+			cutoff = joinTimeCutoff
912
+		}
905 913
 	}
914
+
906 915
 	// #836 again: grace period is never applied to DMs
907
-	if !cutoff.IsZero() && channel != nil {
916
+	if !cutoff.IsZero() && channel != nil && restriction != HistoryCutoffJoinTime {
908 917
 		cutoff = cutoff.Add(-time.Duration(config.History.Restrictions.GracePeriod))
909 918
 	}
910 919
 
@@ -958,7 +967,7 @@ func (server *Server) DeleteMessage(target, msgid, accountName string) (err erro
958 967
 		if target[0] == '#' {
959 968
 			channel := server.channels.Get(target)
960 969
 			if channel != nil {
961
-				if status, _ := channel.historyStatus(config); status == HistoryEphemeral {
970
+				if status, _, _ := channel.historyStatus(config); status == HistoryEphemeral {
962 971
 					hist = &channel.history
963 972
 				}
964 973
 			}

+ 15
- 13
irc/types.go 查看文件

@@ -5,7 +5,11 @@
5 5
 
6 6
 package irc
7 7
 
8
-import "github.com/oragono/oragono/irc/modes"
8
+import (
9
+	"time"
10
+
11
+	"github.com/oragono/oragono/irc/modes"
12
+)
9 13
 
10 14
 type empty struct{}
11 15
 
@@ -28,12 +32,20 @@ func (clients ClientSet) Has(client *Client) bool {
28 32
 	return ok
29 33
 }
30 34
 
35
+type memberData struct {
36
+	modes    *modes.ModeSet
37
+	joinTime int64
38
+}
39
+
31 40
 // MemberSet is a set of members with modes.
32
-type MemberSet map[*Client]*modes.ModeSet
41
+type MemberSet map[*Client]memberData
33 42
 
34 43
 // Add adds the given client to this set.
35 44
 func (members MemberSet) Add(member *Client) {
36
-	members[member] = modes.NewModeSet()
45
+	members[member] = memberData{
46
+		modes:    modes.NewModeSet(),
47
+		joinTime: time.Now().UnixNano(),
48
+	}
37 49
 }
38 50
 
39 51
 // Remove removes the given client from this set.
@@ -47,15 +59,5 @@ func (members MemberSet) Has(member *Client) bool {
47 59
 	return ok
48 60
 }
49 61
 
50
-// AnyHasMode returns true if any of our clients has the given mode.
51
-func (members MemberSet) AnyHasMode(mode modes.Mode) bool {
52
-	for _, modes := range members {
53
-		if modes.HasMode(mode) {
54
-			return true
55
-		}
56
-	}
57
-	return false
58
-}
59
-
60 62
 // ChannelSet is a set of channels.
61 63
 type ChannelSet map[*Channel]empty

+ 8
- 4
traditional.yaml 查看文件

@@ -851,10 +851,14 @@ history:
851 851
         # (and will eventually be deleted from persistent storage, if that's enabled)
852 852
         expire-time: 1w
853 853
 
854
-        # if this is set, logged-in users cannot retrieve messages older than their
855
-        # account registration date, and logged-out users cannot retrieve messages
856
-        # older than their sign-on time (modulo grace-period, see below):
857
-        enforce-registration-date: false
854
+        # this restricts access to channel history (it can be overridden by channel
855
+        # owners). options are: 'none' (no restrictions), 'registration-time'
856
+        # (logged-in users cannot retrieve messages older than their account
857
+        # registration date, and anonymous users cannot retrieve messages older than
858
+        # their sign-on time, modulo the grace-period described below), and
859
+        # 'join-time' (users cannot retrieve messages older than the time they
860
+        # joined the channel, so only always-on clients can view history).
861
+        query-cutoff: 'none'
858 862
 
859 863
         # but if this is set, you can retrieve messages that are up to `grace-period`
860 864
         # older than the above cutoff time. this is recommended to allow logged-out

Loading…
取消
儲存