1
0
Fork 0
forked from External/ergo

use ergochat/irc-go instead of goshuirc/irc-go

This commit is contained in:
Shivaram Lingamneni 2021-06-18 02:41:57 -04:00
parent 66af8cd63c
commit 4910aefa37
32 changed files with 95 additions and 53 deletions

9
vendor/github.com/ergochat/irc-go/ircutils/doc.go generated vendored Normal file
View file

@ -0,0 +1,9 @@
// written by Daniel Oaks <daniel@danieloaks.net>
// released under the ISC license
/*
Package ircutils provides small, useful utility functions and classes.
This package is in an alpha stage.
*/
package ircutils

View file

@ -0,0 +1,41 @@
// written by Daniel Oaks <daniel@danieloaks.net>
// released under the ISC license
package ircutils
import "strings"
var allowedHostnameChars = "abcdefghijklmnopqrstuvwxyz1234567890-."
// HostnameIsValid provides a way for servers to check whether a looked-up client
// hostname is valid (see InspIRCd #1033 for why this is required).
//
// This function shouldn't be called by clients since they don't need to validate
// hostnames for IRC use, just by servers that need to confirm hostnames of incoming
// clients.
//
// In addition to this function, servers should impose their own limits on max
// hostname length -- this function limits it to 200 but most servers will probably
// want to make it smaller than that.
func HostnameIsValid(hostname string) bool {
// IRC hostnames specifically require a period, rough limit of 200 chars
if !strings.Contains(hostname, ".") || len(hostname) < 1 || len(hostname) > 200 {
return false
}
// ensure each part of hostname is valid
for _, part := range strings.Split(hostname, ".") {
if len(part) < 1 || len(part) > 63 || strings.HasPrefix(part, "-") || strings.HasSuffix(part, "-") {
return false
}
}
// ensure all chars of hostname are valid
for _, char := range strings.Split(strings.ToLower(hostname), "") {
if !strings.Contains(allowedHostnameChars, char) {
return false
}
}
return true
}

62
vendor/github.com/ergochat/irc-go/ircutils/unicode.go generated vendored Normal file
View file

@ -0,0 +1,62 @@
// Copyright (c) 2021 Shivaram Lingamneni
// Released under the MIT License
package ircutils
import (
"strings"
"unicode"
"unicode/utf8"
)
// truncate a message, taking care not to make valid UTF8 into invalid UTF8
func TruncateUTF8Safe(message string, byteLimit int) (result string) {
if len(message) <= byteLimit {
return message
}
message = message[:byteLimit]
for i := 0; i < (utf8.UTFMax - 1); i++ {
r, n := utf8.DecodeLastRuneInString(message)
if r == utf8.RuneError && n <= 1 {
message = message[:len(message)-1]
} else {
break
}
}
return message
}
// Sanitizes human-readable text to make it safe for IRC;
// assumes UTF-8 and uses the replacement character where
// applicable.
func SanitizeText(message string, byteLimit int) (result string) {
var buf strings.Builder
for _, r := range message {
if r == '\x00' || r == '\r' {
continue
} else if r == '\n' {
if buf.Len()+2 <= byteLimit {
buf.WriteString(" ")
continue
} else {
break
}
} else if unicode.IsSpace(r) {
if buf.Len()+1 <= byteLimit {
buf.WriteString(" ")
} else {
break
}
} else {
rLen := utf8.RuneLen(r)
if buf.Len()+rLen <= byteLimit {
buf.WriteRune(r)
} else {
break
}
}
}
return buf.String()
}

56
vendor/github.com/ergochat/irc-go/ircutils/userhost.go generated vendored Normal file
View file

@ -0,0 +1,56 @@
// written by Daniel Oaks <daniel@danieloaks.net>
// released under the ISC license
package ircutils
import "strings"
// UserHost holds a username+host combination
type UserHost struct {
Nick string
User string
Host string
}
// ParseUserhost takes a userhost string and returns a UserHost instance.
func ParseUserhost(userhost string) UserHost {
var uh UserHost
if len(userhost) == 0 {
return uh
}
if strings.Contains(userhost, "!") {
usersplit := strings.SplitN(userhost, "!", 2)
var rest string
if len(usersplit) == 2 {
uh.Nick = usersplit[0]
rest = usersplit[1]
} else {
rest = usersplit[0]
}
hostsplit := strings.SplitN(rest, "@", 2)
if len(hostsplit) == 2 {
uh.User = hostsplit[0]
uh.Host = hostsplit[1]
} else {
uh.User = hostsplit[0]
}
} else {
hostsplit := strings.SplitN(userhost, "@", 2)
if len(hostsplit) == 2 {
uh.Nick = hostsplit[0]
uh.Host = hostsplit[1]
} else {
uh.User = hostsplit[0]
}
}
return uh
}
// // Canonical returns the canonical string representation of the userhost.
// func (uh *UserHost) Canonical() string {
// return ""
// }