Quellcode durchsuchen

Merge pull request #1496 from slingamn/jointime.1

fix #1490
tags/v2.5.0-rc1
Shivaram Lingamneni vor 3 Jahren
Ursprung
Commit
2e7cf3cc1e
Es ist kein Account mit der E-Mail-Adresse des Committers verbunden
12 geänderte Dateien mit 275 neuen und 93 gelöschten Zeilen
  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 Datei anzeigen

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

+ 7
- 2
irc/accounts.go Datei anzeigen

@@ -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 Datei anzeigen

@@ -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 Datei anzeigen

@@ -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,
@@ -340,7 +350,7 @@ func csDeopHandler(service *ircService, server *Server, client *Client, command
340 350
 		target = client
341 351
 	}
342 352
 
343
-	present, cumodes := channel.ClientStatus(target)
353
+	present, _, cumodes := channel.ClientStatus(target)
344 354
 	if !present || len(cumodes) == 0 {
345 355
 		service.Notice(rb, client.t("Target has no privileges to remove"))
346 356
 		return
@@ -764,6 +774,13 @@ func displayChannelSetting(service *ircService, settingName string, settings Cha
764 774
 		effectiveValue := historyEnabled(config.History.Persistent.RegisteredChannels, settings.History)
765 775
 		service.Notice(rb, fmt.Sprintf(client.t("The stored channel history setting is: %s"), historyStatusToString(settings.History)))
766 776
 		service.Notice(rb, fmt.Sprintf(client.t("Given current server settings, the channel history setting is: %s"), historyStatusToString(effectiveValue)))
777
+	case "query-cutoff":
778
+		effectiveValue := settings.QueryCutoff
779
+		if effectiveValue == HistoryCutoffDefault {
780
+			effectiveValue = config.History.Restrictions.queryCutoff
781
+		}
782
+		service.Notice(rb, fmt.Sprintf(client.t("The stored channel history query cutoff setting is: %s"), historyCutoffToString(settings.QueryCutoff)))
783
+		service.Notice(rb, fmt.Sprintf(client.t("Given current server settings, the channel history query cutoff setting is: %s"), historyCutoffToString(effectiveValue)))
767 784
 	default:
768 785
 		service.Notice(rb, client.t("Invalid params"))
769 786
 	}
@@ -807,6 +824,13 @@ func csSetHandler(service *ircService, server *Server, client *Client, command s
807 824
 		}
808 825
 		channel.SetSettings(settings)
809 826
 		channel.resizeHistory(server.Config())
827
+	case "query-cutoff":
828
+		settings.QueryCutoff, err = historyCutoffFromString(value)
829
+		if err != nil {
830
+			err = errInvalidParams
831
+			break
832
+		}
833
+		channel.SetSettings(settings)
810 834
 	}
811 835
 
812 836
 	switch err {

+ 7
- 7
irc/client.go Datei anzeigen

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

+ 58
- 3
irc/config.go Datei anzeigen

@@ -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
@@ -1358,6 +1400,19 @@ func LoadConfig(filename string) (config *Config, err error) {
1358 1400
 		config.History.ZNCMax = config.History.ChathistoryMax
1359 1401
 	}
1360 1402
 
1403
+	if config.History.Restrictions.QueryCutoff != "" {
1404
+		config.History.Restrictions.queryCutoff, err = historyCutoffFromString(config.History.Restrictions.QueryCutoff)
1405
+		if err != nil {
1406
+			return nil, fmt.Errorf("invalid value of history.query-restrictions: %w", err)
1407
+		}
1408
+	} else {
1409
+		if config.History.Restrictions.EnforceRegistrationDate_ {
1410
+			config.History.Restrictions.queryCutoff = HistoryCutoffRegistrationTime
1411
+		} else {
1412
+			config.History.Restrictions.queryCutoff = HistoryCutoffNone
1413
+		}
1414
+	}
1415
+
1361 1416
 	config.Roleplay.addSuffix = utils.BoolDefaultTrue(config.Roleplay.AddSuffix)
1362 1417
 
1363 1418
 	config.Datastore.MySQL.ExpireTime = time.Duration(config.History.Restrictions.ExpireTime)

+ 51
- 1
irc/database.go Datei anzeigen

@@ -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 Datei anzeigen

@@ -528,7 +528,7 @@ func (channel *Channel) Founder() string {
528 528
 
529 529
 func (channel *Channel) HighestUserMode(client *Client) (result modes.Mode) {
530 530
 	channel.stateMutex.RLock()
531
-	clientModes := channel.members[client]
531
+	clientModes := channel.members[client].modes
532 532
 	channel.stateMutex.RUnlock()
533 533
 	return clientModes.HighestChannelUserMode()
534 534
 }

+ 3
- 3
irc/handlers.go Datei anzeigen

@@ -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))
@@ -2660,7 +2660,7 @@ func renameHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *Re
2660 2660
 	}
2661 2661
 
2662 2662
 	config := server.Config()
2663
-	status, _ := channel.historyStatus(config)
2663
+	status, _, _ := channel.historyStatus(config)
2664 2664
 	if status == HistoryPersistent {
2665 2665
 		rb.Add(nil, server.name, "FAIL", "RENAME", "CANNOT_RENAME", oldName, utils.SafeErrorParam(newName), client.t("Channels with persistent history cannot be renamed"))
2666 2666
 		return false

+ 15
- 6
irc/server.go Datei anzeigen

@@ -858,6 +858,7 @@ func (server *Server) GetHistorySequence(providedChannel *Channel, client *Clien
858 858
 	var status HistoryStatus
859 859
 	var target, correspondent string
860 860
 	var hist *history.Buffer
861
+	restriction := HistoryCutoffNone
861 862
 	channel = providedChannel
862 863
 	if channel == nil {
863 864
 		if strings.HasPrefix(query, "#") {
@@ -867,12 +868,15 @@ func (server *Server) GetHistorySequence(providedChannel *Channel, client *Clien
867 868
 			}
868 869
 		}
869 870
 	}
871
+	var joinTimeCutoff time.Time
870 872
 	if channel != nil {
871
-		if !channel.hasClient(client) {
873
+		if present, cutoff := channel.joinTimeCutoff(client); present {
874
+			joinTimeCutoff = cutoff
875
+		} else {
872 876
 			err = errInsufficientPrivs
873 877
 			return
874 878
 		}
875
-		status, target = channel.historyStatus(config)
879
+		status, target, restriction = channel.historyStatus(config)
876 880
 		switch status {
877 881
 		case HistoryEphemeral:
878 882
 			hist = &channel.history
@@ -904,15 +908,20 @@ func (server *Server) GetHistorySequence(providedChannel *Channel, client *Clien
904 908
 		cutoff = time.Now().UTC().Add(-time.Duration(config.History.Restrictions.ExpireTime))
905 909
 	}
906 910
 	// #836: registration date cutoff is always enforced for DMs
907
-	if config.History.Restrictions.EnforceRegistrationDate || channel == nil {
911
+	// either way, take the later of the two cutoffs
912
+	if restriction == HistoryCutoffRegistrationTime || channel == nil {
908 913
 		regCutoff := client.historyCutoff()
909
-		// take the later of the two cutoffs
910 914
 		if regCutoff.After(cutoff) {
911 915
 			cutoff = regCutoff
912 916
 		}
917
+	} else if restriction == HistoryCutoffJoinTime {
918
+		if joinTimeCutoff.After(cutoff) {
919
+			cutoff = joinTimeCutoff
920
+		}
913 921
 	}
922
+
914 923
 	// #836 again: grace period is never applied to DMs
915
-	if !cutoff.IsZero() && channel != nil {
924
+	if !cutoff.IsZero() && channel != nil && restriction != HistoryCutoffJoinTime {
916 925
 		cutoff = cutoff.Add(-time.Duration(config.History.Restrictions.GracePeriod))
917 926
 	}
918 927
 
@@ -966,7 +975,7 @@ func (server *Server) DeleteMessage(target, msgid, accountName string) (err erro
966 975
 		if target[0] == '#' {
967 976
 			channel := server.channels.Get(target)
968 977
 			if channel != nil {
969
-				if status, _ := channel.historyStatus(config); status == HistoryEphemeral {
978
+				if status, _, _ := channel.historyStatus(config); status == HistoryEphemeral {
970 979
 					hist = &channel.history
971 980
 				}
972 981
 			}

+ 15
- 13
irc/types.go Datei anzeigen

@@ -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 Datei anzeigen

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

Laden…
Abbrechen
Speichern