Browse Source

strings: Use inbuilt precis.Nickname encoding now

tags/v0.4.0
Daniel Oaks 7 years ago
parent
commit
c7fdb4656e
4 changed files with 19 additions and 93 deletions
  1. 1
    0
      CHANGELOG.md
  2. 13
    1
      irc/client.go
  3. 5
    3
      irc/strings.go
  4. 0
    89
      irc/strings_nickname.go

+ 1
- 0
CHANGELOG.md View File

@@ -24,6 +24,7 @@ New release of Oragono!
24 24
 ### Fixed
25 25
 * Fixed bug where `HELP` wouldn't correctly display for operators, and added more help topics.
26 26
 * Fixed display of large `MONITOR` lists.
27
+* Fixed bug where you would always have certain capabilities enabled.
27 28
 
28 29
 
29 30
 ## [0.3.0] - 2016-10-23

+ 13
- 1
irc/client.go View File

@@ -287,7 +287,19 @@ func (c *Client) ModeString() (str string) {
287 287
 // Friends refers to clients that share a channel with this client.
288 288
 func (client *Client) Friends(Capabilities ...Capability) ClientSet {
289 289
 	friends := make(ClientSet)
290
-	friends.Add(client)
290
+
291
+	// make sure that I have the right caps
292
+	hasCaps := true
293
+	for _, Cap := range Capabilities {
294
+		if !client.capabilities[Cap] {
295
+			hasCaps = false
296
+			break
297
+		}
298
+	}
299
+	if hasCaps {
300
+		friends.Add(client)
301
+	}
302
+
291 303
 	for channel := range client.channels {
292 304
 		for member := range channel.members {
293 305
 			// make sure they have all the required caps

+ 5
- 3
irc/strings.go View File

@@ -8,6 +8,8 @@ package irc
8 8
 import (
9 9
 	"errors"
10 10
 	"strings"
11
+
12
+	"golang.org/x/text/secure/precis"
11 13
 )
12 14
 
13 15
 var (
@@ -16,12 +18,12 @@ var (
16 18
 
17 19
 // Casefold returns a casefolded string, without doing any name or channel character checks.
18 20
 func Casefold(str string) (string, error) {
19
-	return NicknameProfile.String(str)
21
+	return precis.Nickname.CompareKey(str)
20 22
 }
21 23
 
22 24
 // CasefoldChannel returns a casefolded version of a channel name.
23 25
 func CasefoldChannel(name string) (string, error) {
24
-	lowered, err := NicknameProfile.String(name)
26
+	lowered, err := precis.Nickname.CompareKey(name)
25 27
 
26 28
 	if err != nil {
27 29
 		return "", err
@@ -45,7 +47,7 @@ func CasefoldChannel(name string) (string, error) {
45 47
 
46 48
 // CasefoldName returns a casefolded version of a nick/user name.
47 49
 func CasefoldName(name string) (string, error) {
48
-	lowered, err := NicknameProfile.String(name)
50
+	lowered, err := precis.Nickname.CompareKey(name)
49 51
 
50 52
 	if err != nil {
51 53
 		return "", err

+ 0
- 89
irc/strings_nickname.go View File

@@ -1,89 +0,0 @@
1
-// Copyright 2015 The Go Authors. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-//NOTE(dan): I need this because the default PRECIS API does not allow a way to retrieve the casefolded version of strings.
6
-// See also: https://github.com/golang/go/issues/17386
7
-
8
-// the content of this file is taken wholesale from the proper PRECIS API:
9
-// https://github.com/golang/text/tree/master/secure/precis
10
-
11
-package irc
12
-
13
-import (
14
-	"unicode"
15
-	"unicode/utf8"
16
-
17
-	"golang.org/x/text/secure/precis"
18
-	"golang.org/x/text/transform"
19
-	"golang.org/x/text/unicode/norm"
20
-)
21
-
22
-type nickAdditionalMapping struct {
23
-	// TODO: This transformer needs to be stateless somehow…
24
-	notStart  bool
25
-	prevSpace bool
26
-}
27
-
28
-func (t *nickAdditionalMapping) Reset() {
29
-	t.prevSpace = false
30
-	t.notStart = false
31
-}
32
-
33
-func (t *nickAdditionalMapping) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
34
-	// RFC 7700 §2.1.  Rules
35
-	//
36
-	// 2.  Additional Mapping Rule: The additional mapping rule consists of
37
-	//                              the following sub-rules.
38
-	//
39
-	//        1.  Any instances of non-ASCII space MUST be mapped to ASCII
40
-	//            space (U+0020); a non-ASCII space is any Unicode code point
41
-	//            having a general category of "Zs", naturally with the
42
-	//            exception of U+0020.
43
-	//
44
-	//        2.  Any instances of the ASCII space character at the beginning
45
-	//            or end of a nickname MUST be removed (e.g., "stpeter " is
46
-	//            mapped to "stpeter").
47
-	//
48
-	//        3.  Interior sequences of more than one ASCII space character
49
-	//            MUST be mapped to a single ASCII space character (e.g.,
50
-	//            "St  Peter" is mapped to "St Peter").
51
-
52
-	for nSrc < len(src) {
53
-		r, size := utf8.DecodeRune(src[nSrc:])
54
-		if size == 0 { // Incomplete UTF-8 encoding
55
-			if !atEOF {
56
-				return nDst, nSrc, transform.ErrShortSrc
57
-			}
58
-			size = 1
59
-		}
60
-		if unicode.Is(unicode.Zs, r) {
61
-			t.prevSpace = true
62
-		} else {
63
-			if t.prevSpace && t.notStart {
64
-				dst[nDst] = ' '
65
-				nDst += 1
66
-			}
67
-			if size != copy(dst[nDst:], src[nSrc:nSrc+size]) {
68
-				nDst += size
69
-				return nDst, nSrc, transform.ErrShortDst
70
-			}
71
-			nDst += size
72
-			t.prevSpace = false
73
-			t.notStart = true
74
-		}
75
-		nSrc += size
76
-	}
77
-	return nDst, nSrc, nil
78
-}
79
-
80
-var (
81
-	NicknameProfile = precis.NewFreeform(
82
-		precis.AdditionalMapping(func() transform.Transformer {
83
-			return &nickAdditionalMapping{}
84
-		}),
85
-		precis.LowerCase(),
86
-		precis.Norm(norm.NFKC),
87
-		precis.DisallowEmpty,
88
-	)
89
-)

Loading…
Cancel
Save