Преглед на файлове

Merge pull request #811 from slingamn/history_nits.4

tweaks to persistent history
tags/v2.0.0-rc1
Shivaram Lingamneni преди 4 години
родител
ревизия
ec1a718275
No account linked to committer's email address
променени са 5 файла, в които са добавени 106 реда и са изтрити 87 реда
  1. 16
    18
      irc/channel.go
  2. 12
    20
      irc/client.go
  3. 25
    10
      irc/config.go
  4. 13
    6
      irc/handlers.go
  5. 40
    33
      irc/server.go

+ 16
- 18
irc/channel.go Целия файл

@@ -113,8 +113,8 @@ func (channel *Channel) IsLoaded() bool {
113 113
 }
114 114
 
115 115
 func (channel *Channel) resizeHistory(config *Config) {
116
-	_, ephemeral, _ := channel.historyStatus(config)
117
-	if ephemeral {
116
+	status, _ := channel.historyStatus(config)
117
+	if status == HistoryEphemeral {
118 118
 		channel.history.Resize(config.History.ChannelLength, config.History.AutoresizeWindow)
119 119
 	} else {
120 120
 		channel.history.Resize(0, 0)
@@ -588,9 +588,9 @@ func (channel *Channel) IsEmpty() bool {
588 588
 
589 589
 // figure out where history is being stored: persistent, ephemeral, or neither
590 590
 // target is only needed if we're doing persistent history
591
-func (channel *Channel) historyStatus(config *Config) (persistent, ephemeral bool, target string) {
592
-	if !config.History.Persistent.Enabled {
593
-		return false, config.History.Enabled, ""
591
+func (channel *Channel) historyStatus(config *Config) (status HistoryStatus, target string) {
592
+	if !config.History.Enabled {
593
+		return HistoryDisabled, ""
594 594
 	}
595 595
 
596 596
 	channel.stateMutex.RLock()
@@ -599,18 +599,17 @@ func (channel *Channel) historyStatus(config *Config) (persistent, ephemeral boo
599 599
 	registered := channel.registeredFounder != ""
600 600
 	channel.stateMutex.RUnlock()
601 601
 
602
-	historyStatus = historyEnabled(config.History.Persistent.RegisteredChannels, historyStatus)
603
-
604 602
 	// ephemeral history: either the channel owner explicitly set the ephemeral preference,
605 603
 	// or persistent history is disabled for unregistered channels
606 604
 	if registered {
607
-		ephemeral = (historyStatus == HistoryEphemeral)
608
-		persistent = (historyStatus == HistoryPersistent)
605
+		return historyEnabled(config.History.Persistent.RegisteredChannels, historyStatus), target
609 606
 	} else {
610
-		ephemeral = config.History.Enabled && !config.History.Persistent.UnregisteredChannels
611
-		persistent = config.History.Persistent.UnregisteredChannels
607
+		if config.History.Persistent.UnregisteredChannels {
608
+			return HistoryPersistent, target
609
+		} else {
610
+			return HistoryEphemeral, target
611
+		}
612 612
 	}
613
-	return
614 613
 }
615 614
 
616 615
 func (channel *Channel) AddHistoryItem(item history.Item) (err error) {
@@ -618,14 +617,13 @@ func (channel *Channel) AddHistoryItem(item history.Item) (err error) {
618 617
 		return
619 618
 	}
620 619
 
621
-	persistent, ephemeral, target := channel.historyStatus(channel.server.Config())
622
-	if ephemeral {
620
+	status, target := channel.historyStatus(channel.server.Config())
621
+	if status == HistoryPersistent {
622
+		err = channel.server.historyDB.AddChannelItem(target, item)
623
+	} else if status == HistoryEphemeral {
623 624
 		channel.history.Add(item)
624 625
 	}
625
-	if persistent {
626
-		return channel.server.historyDB.AddChannelItem(target, item)
627
-	}
628
-	return nil
626
+	return
629 627
 }
630 628
 
631 629
 // Join joins the given client to this channel (if they can be joined).

+ 12
- 20
irc/client.go Целия файл

@@ -354,8 +354,8 @@ func (server *Server) AddAlwaysOnClient(account ClientAccount, chnames []string,
354 354
 }
355 355
 
356 356
 func (client *Client) resizeHistory(config *Config) {
357
-	_, ephemeral, _ := client.historyStatus(config)
358
-	if ephemeral {
357
+	status, _ := client.historyStatus(config)
358
+	if status == HistoryEphemeral {
359 359
 		client.history.Resize(config.History.ClientLength, config.History.AutoresizeWindow)
360 360
 	} else {
361 361
 		client.history.Resize(0, 0)
@@ -749,16 +749,16 @@ func (session *Session) playResume() {
749 749
 		for _, member := range channel.Members() {
750 750
 			friends.Add(member)
751 751
 		}
752
-		_, ephemeral, _ := channel.historyStatus(config)
753
-		if ephemeral {
752
+		status, _ := channel.historyStatus(config)
753
+		if status == HistoryEphemeral {
754 754
 			lastDiscarded := channel.history.LastDiscarded()
755 755
 			if oldestLostMessage.Before(lastDiscarded) {
756 756
 				oldestLostMessage = lastDiscarded
757 757
 			}
758 758
 		}
759 759
 	}
760
-	_, cEphemeral, _ := client.historyStatus(config)
761
-	if cEphemeral {
760
+	cHistoryStatus, _ := client.historyStatus(config)
761
+	if cHistoryStatus == HistoryEphemeral {
762 762
 		lastDiscarded := client.history.LastDiscarded()
763 763
 		if oldestLostMessage.Before(lastDiscarded) {
764 764
 			oldestLostMessage = lastDiscarded
@@ -1551,29 +1551,21 @@ func (client *Client) attemptAutoOper(session *Session) {
1551 1551
 	}
1552 1552
 }
1553 1553
 
1554
-func (client *Client) historyStatus(config *Config) (persistent, ephemeral bool, target string) {
1554
+func (client *Client) historyStatus(config *Config) (status HistoryStatus, target string) {
1555 1555
 	if !config.History.Enabled {
1556
-		return
1557
-	} else if !config.History.Persistent.Enabled {
1558
-		ephemeral = true
1559
-		return
1556
+		return HistoryDisabled, ""
1560 1557
 	}
1561 1558
 
1562 1559
 	client.stateMutex.RLock()
1563
-	alwaysOn := client.alwaysOn
1560
+	loggedIn := client.account != ""
1564 1561
 	historyStatus := client.accountSettings.DMHistory
1565 1562
 	target = client.nickCasefolded
1566 1563
 	client.stateMutex.RUnlock()
1567 1564
 
1568
-	if !alwaysOn {
1569
-		ephemeral = true
1570
-		return
1565
+	if !loggedIn {
1566
+		return HistoryEphemeral, ""
1571 1567
 	}
1572
-
1573
-	historyStatus = historyEnabled(config.History.Persistent.DirectMessages, historyStatus)
1574
-	ephemeral = (historyStatus == HistoryEphemeral)
1575
-	persistent = (historyStatus == HistoryPersistent)
1576
-	return
1568
+	return historyEnabled(config.History.Persistent.DirectMessages, historyStatus), target
1577 1569
 }
1578 1570
 
1579 1571
 // these are bit flags indicating what part of the client status is "dirty"

+ 25
- 10
irc/config.go Целия файл

@@ -185,25 +185,40 @@ func historyStatusToString(status HistoryStatus) string {
185 185
 	}
186 186
 }
187 187
 
188
+// XXX you must have already checked History.Enabled before calling this
188 189
 func historyEnabled(serverSetting PersistentStatus, localSetting HistoryStatus) (result HistoryStatus) {
189
-	if serverSetting == PersistentDisabled {
190
-		return HistoryDisabled
191
-	} else if serverSetting == PersistentMandatory {
190
+	switch serverSetting {
191
+	case PersistentMandatory:
192 192
 		return HistoryPersistent
193
-	} else if serverSetting == PersistentOptOut {
193
+	case PersistentOptOut:
194 194
 		if localSetting == HistoryDefault {
195 195
 			return HistoryPersistent
196 196
 		} else {
197 197
 			return localSetting
198 198
 		}
199
-	} else if serverSetting == PersistentOptIn {
200
-		if localSetting >= HistoryEphemeral {
201
-			return localSetting
202
-		} else {
199
+	case PersistentOptIn:
200
+		switch localSetting {
201
+		case HistoryPersistent:
202
+			return HistoryPersistent
203
+		case HistoryEphemeral, HistoryDefault:
204
+			return HistoryEphemeral
205
+		default:
203 206
 			return HistoryDisabled
204 207
 		}
205
-	} else {
206
-		return HistoryDisabled
208
+	case PersistentDisabled:
209
+		if localSetting == HistoryDisabled {
210
+			return HistoryDisabled
211
+		} else {
212
+			return HistoryEphemeral
213
+		}
214
+	default:
215
+		// PersistentUnspecified: shouldn't happen because the deserializer converts it
216
+		// to PersistentDisabled
217
+		if localSetting == HistoryDefault {
218
+			return HistoryEphemeral
219
+		} else {
220
+			return localSetting
221
+		}
207 222
 	}
208 223
 }
209 224
 

+ 13
- 6
irc/handlers.go Целия файл

@@ -551,8 +551,15 @@ func chathistoryHandler(server *Server, client *Client, msg ircmsg.IrcMessage, r
551 551
 	if maxChathistoryLimit == 0 {
552 552
 		return
553 553
 	}
554
+	preposition := strings.ToLower(msg.Params[0])
555
+	target = msg.Params[1]
554 556
 
555 557
 	parseQueryParam := func(param string) (msgid string, timestamp time.Time, err error) {
558
+		if param == "*" && (preposition == "before" || preposition == "between") {
559
+			// XXX compatibility with kiwi, which as of February 2020 is
560
+			// using BEFORE * as a synonym for LATEST *
561
+			return
562
+		}
556 563
 		err = utils.ErrInvalidParams
557 564
 		pieces := strings.SplitN(param, "=", 2)
558 565
 		if len(pieces) < 2 {
@@ -580,8 +587,6 @@ func chathistoryHandler(server *Server, client *Client, msg ircmsg.IrcMessage, r
580 587
 		return
581 588
 	}
582 589
 
583
-	preposition := strings.ToLower(msg.Params[0])
584
-	target = msg.Params[1]
585 590
 	channel, sequence, err = server.GetHistorySequence(nil, client, target)
586 591
 	if err != nil || sequence == nil {
587 592
 		return
@@ -1995,17 +2000,19 @@ func dispatchMessageToTarget(client *Client, tags map[string]string, histType hi
1995 2000
 		}
1996 2001
 		targetedItem := item
1997 2002
 		targetedItem.Params[0] = tnick
1998
-		cPersistent, cEphemeral, _ := client.historyStatus(config)
1999
-		tPersistent, tEphemeral, _ := user.historyStatus(config)
2003
+		cStatus, _ := client.historyStatus(config)
2004
+		tStatus, _ := user.historyStatus(config)
2000 2005
 		// add to ephemeral history
2001
-		if cEphemeral {
2006
+		if cStatus == HistoryEphemeral {
2002 2007
 			targetedItem.CfCorrespondent = tDetails.nickCasefolded
2003 2008
 			client.history.Add(targetedItem)
2004 2009
 		}
2005
-		if tEphemeral && client != user {
2010
+		if tStatus == HistoryEphemeral && client != user {
2006 2011
 			item.CfCorrespondent = details.nickCasefolded
2007 2012
 			user.history.Add(item)
2008 2013
 		}
2014
+		cPersistent := cStatus == HistoryPersistent
2015
+		tPersistent := tStatus == HistoryPersistent
2009 2016
 		if cPersistent || tPersistent {
2010 2017
 			item.CfCorrespondent = ""
2011 2018
 			server.historyDB.AddDirectMessage(details.nickCasefolded, user.NickCasefolded(), cPersistent, tPersistent, targetedItem)

+ 40
- 33
irc/server.go Целия файл

@@ -864,46 +864,52 @@ func (server *Server) setupListeners(config *Config) (err error) {
864 864
 // privilege checking.
865 865
 func (server *Server) GetHistorySequence(providedChannel *Channel, client *Client, target string) (channel *Channel, sequence history.Sequence, err error) {
866 866
 	config := server.Config()
867
+	// 4 cases: {persistent, ephemeral} x {normal, conversation}
868
+	// with ephemeral history, recipient is implicit in the choice of `hist`,
869
+	// and sender is "" if we're retrieving a channel or *, and the correspondent's name
870
+	// if we're retrieving a DM conversation ("query buffer"). with persistent history,
871
+	// recipient is always nonempty, and sender is either empty or nonempty as before.
872
+	var status HistoryStatus
867 873
 	var sender, recipient string
868 874
 	var hist *history.Buffer
869
-	if target == "*" {
870
-		persistent, ephemeral, target := client.historyStatus(config)
871
-		if persistent {
872
-			recipient = target
873
-		} else if ephemeral {
874
-			hist = &client.history
875
-		} else {
876
-			return
877
-		}
878
-	} else {
879
-		channel = providedChannel
880
-		if channel == nil {
875
+	channel = providedChannel
876
+	if channel == nil {
877
+		if strings.HasPrefix(target, "#") {
881 878
 			channel = server.channels.Get(target)
882
-		}
883
-		if channel != nil {
884
-			if !channel.hasClient(client) {
885
-				err = errInsufficientPrivs
879
+			if channel == nil {
886 880
 				return
887 881
 			}
888
-			persistent, ephemeral, cfTarget := channel.historyStatus(config)
889
-			if persistent {
890
-				recipient = cfTarget
891
-			} else if ephemeral {
892
-				hist = &channel.history
893
-			} else {
894
-				return
895
-			}
896
-		} else {
897
-			sender = client.NickCasefolded()
898
-			var cfTarget string
899
-			cfTarget, err = CasefoldName(target)
882
+		}
883
+	}
884
+	if channel != nil {
885
+		if !channel.hasClient(client) {
886
+			err = errInsufficientPrivs
887
+			return
888
+		}
889
+		status, recipient = channel.historyStatus(config)
890
+		switch status {
891
+		case HistoryEphemeral:
892
+			hist = &channel.history
893
+		case HistoryPersistent:
894
+			// already set `recipient`
895
+		default:
896
+			return
897
+		}
898
+	} else {
899
+		status, recipient = client.historyStatus(config)
900
+		if target != "*" {
901
+			sender, err = CasefoldName(target)
900 902
 			if err != nil {
901 903
 				return
902 904
 			}
903
-			recipient = cfTarget
904
-			if !client.AlwaysOn() {
905
-				hist = &client.history
906
-			}
905
+		}
906
+		switch status {
907
+		case HistoryEphemeral:
908
+			hist = &client.history
909
+		case HistoryPersistent:
910
+			// already set `recipient`, and `sender` if necessary
911
+		default:
912
+			return
907 913
 		}
908 914
 	}
909 915
 
@@ -921,8 +927,9 @@ func (server *Server) GetHistorySequence(providedChannel *Channel, client *Clien
921 927
 	if !cutoff.IsZero() {
922 928
 		cutoff = cutoff.Add(-time.Duration(config.History.Restrictions.GracePeriod))
923 929
 	}
930
+
924 931
 	if hist != nil {
925
-		sequence = hist.MakeSequence(recipient, cutoff)
932
+		sequence = hist.MakeSequence(sender, cutoff)
926 933
 	} else if recipient != "" {
927 934
 		sequence = server.historyDB.MakeSequence(sender, recipient, cutoff)
928 935
 	}

Loading…
Отказ
Запис