8
8
"errors"
9
9
"fmt"
10
10
"net"
11
+ "net/mail"
11
12
"path/filepath"
12
13
"reflect"
13
14
"regexp"
@@ -20,13 +21,12 @@ import (
20
21
21
22
"github.com/tinode/chat/server/auth"
22
23
"github.com/tinode/chat/server/store/types"
23
- "github.com/ttacon/libphonenumber "
24
+ "github.com/nyaruka/phonenumbers "
24
25
25
26
"golang.org/x/crypto/acme/autocert"
26
27
)
27
28
28
29
var tagPrefixRegexp = regexp .MustCompile (`^([a-z]\w{0,5}):\S` )
29
- var emailRegexp = regexp .MustCompile ("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\ .[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" )
30
30
31
31
const nullValue = "\u2421 "
32
32
@@ -403,20 +403,25 @@ func versionToString(vers int) string {
403
403
// against the email, telephone number or login patterns.
404
404
// On success, prepends the token with the corresponding prefix.
405
405
func rewriteToken (orig string ) string {
406
- if orig == "" || strings . HasPrefix (orig , "basic:" ) ||
407
- strings . HasPrefix ( orig , "email:" ) || strings . HasPrefix ( orig , "tel:" ) {
406
+ if orig == "" || tagPrefixRegexp . MatchString (orig ) {
407
+ // It either empty or already has a prefix. E.g. basic:alice.
408
408
return orig
409
409
}
410
410
// Is it email?
411
- if emailRegexp . MatchString (orig ) {
411
+ if r , err := mail . ParseAddress (orig ); err == nil && r . Address == orig {
412
412
return "email:" + orig
413
413
}
414
414
// TODO: pass region as a param.
415
- if num , err := libphonenumber .Parse (orig , "US" ); err == nil {
415
+ // 1. As provided by the client (e.g. as specified or inferred
416
+ // from client's phone number or location).
417
+ // 2. Use value from .conf file.
418
+ // 3. Fallback to US as a last resort.
419
+ if num , err := phonenumbers .Parse (orig , "US" ); err == nil {
416
420
// It's a phone number.
417
- return "tel:" + libphonenumber .Format (num , libphonenumber .E164 )
421
+ return "tel:" + phonenumbers .Format (num , phonenumbers .E164 )
418
422
}
419
423
// Does it look like a username/login?
424
+ // TODO: use configured authenticators to check if orig may a valid user name.
420
425
runes := []rune (orig )
421
426
if len (runes ) >= 3 && unicode .IsLetter (runes [0 ]) {
422
427
// Check if the remaining chars are letters or digits.
0 commit comments