Browse Source

add draft/language ircv3 feature

master
Gisle Aune 4 years ago
parent
commit
533a41679d
  1. 60
      client.go
  2. BIN
      cmd/ircrepl/ircrepl
  3. 5
      cmd/ircrepl/main.go
  4. 3
      config.go

60
client.go

@ -33,6 +33,7 @@ var supportedCaps = []string{
"chghost",
"account-tag",
"echo-message",
"draft/languages",
}
// ErrNoConnection is returned if you try to do something requiring a connection,
@ -158,6 +159,14 @@ func (client *Client) ISupport() *isupport.ISupport {
return &client.isupport
}
// CapData returns if there was any additional CAP data for the given capability.
func (client *Client) CapData(cap string) string {
client.mutex.RLock()
defer client.mutex.RUnlock()
return client.capData[cap]
}
// CapEnabled returns whether an IRCv3 capability is enabled.
func (client *Client) CapEnabled(cap string) bool {
client.mutex.RLock()
@ -246,14 +255,14 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
for {
line, err := reader.ReadString('\n')
if err != nil {
client.EmitNonBlocking(NewErrorEvent("read", "Read failed: " + err.Error()))
client.EmitNonBlocking(NewErrorEvent("read", "Read failed: "+err.Error()))
break
}
line = replacer.Replace(line)
event, err := ParsePacket(line)
if err != nil {
client.EmitNonBlocking(NewErrorEvent("parse", "Read failed: " + err.Error()))
client.EmitNonBlocking(NewErrorEvent("parse", "Read failed: "+err.Error()))
continue
}
@ -911,9 +920,9 @@ func (client *Client) handleEvent(event *Event) {
}
for i := range supportedCaps {
if supportedCaps[i] == token {
if supportedCaps[i] == key {
client.mutex.Lock()
client.capsRequested = append(client.capsRequested, token)
client.capsRequested = append(client.capsRequested, key)
client.mutex.Unlock()
break
@ -945,10 +954,53 @@ func (client *Client) handleEvent(event *Event) {
client.capEnabled[token] = true
}
client.mutex.Unlock()
// Special cases for supported tokens
switch token {
case "draft/languages":
{
if len(client.config.Languages) == 0 {
break
}
// draft/languages=15,en,~bs,~de,~el,~en-AU,~es,~fi,~fr-FR,~it,~no,~pl,~pt-BR,~ro,~tr-TR,~zh-CN
langData := strings.Split(client.capData[token], ",")
if len(langData) < 0 {
break
}
maxCount, err := strconv.Atoi(langData[0])
if err != nil {
break
}
languages := make([]string, 0, maxCount)
LanguageLoop:
for _, lang := range client.config.Languages {
for _, lang2 := range langData[1:] {
if strings.HasPrefix(lang2, "~") {
lang2 = lang2[1:]
}
if strings.EqualFold(lang, lang2) {
languages = append(languages, lang)
if len(languages) >= maxCount {
break LanguageLoop
}
}
}
}
if len(languages) > 0 {
_ = client.Send("LANGUAGE " + strings.Join(languages, " "))
}
}
}
}
if !client.Ready() {
_ = client.Send("CAP END")
}
}
case "NAK":
{
// Remove offenders

BIN
cmd/ircrepl/ircrepl

5
cmd/ircrepl/main.go

@ -19,7 +19,7 @@ var flagAlts = flag.String("alts", "Test2,Test3,Test4,Test5", "Alternative nicks
var flagUser = flag.String("user", "test", "The client user/ident")
var flagPass = flag.String("pass", "", "The server password")
var flagServer = flag.String("server", "localhost:6667", "The server to connect to")
var flagSsl = flag.Bool("ssl", false, "Wether to connect securely")
var flagSsl = flag.Bool("ssl", false, "Whether to connect securely")
func main() {
ctx, cancel := context.WithCancel(context.Background())
@ -32,6 +32,7 @@ func main() {
User: *flagUser,
Alternatives: strings.Split(*flagAlts, ","),
Password: *flagPass,
Languages: []string{"no_NB", "no", "en_US", "en"},
})
client.AddHandler(handlers.Input)
@ -94,6 +95,6 @@ func main() {
break
}
client.EmitInput(string(line[:len(line)-1]), target)
client.EmitInput(line[:len(line)-1], target)
}
}

3
config.go

@ -30,6 +30,9 @@ type Config struct {
// The rate (lines per second) to send with Client.SendQueued. Default is 2, which is how
// clients that don't excess flood does it.
SendRate int `json:"sendRate"`
// Languages to request.
Languages []string `json:"languages"`
}
// WithDefaults returns the config with the default values

Loading…
Cancel
Save