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", "chghost",
"account-tag", "account-tag",
"echo-message", "echo-message",
"draft/languages",
} }
// ErrNoConnection is returned if you try to do something requiring a connection, // 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 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. // CapEnabled returns whether an IRCv3 capability is enabled.
func (client *Client) CapEnabled(cap string) bool { func (client *Client) CapEnabled(cap string) bool {
client.mutex.RLock() client.mutex.RLock()
@ -246,14 +255,14 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
for { for {
line, err := reader.ReadString('\n') line, err := reader.ReadString('\n')
if err != nil { if err != nil {
client.EmitNonBlocking(NewErrorEvent("read", "Read failed: " + err.Error()))
client.EmitNonBlocking(NewErrorEvent("read", "Read failed: "+err.Error()))
break break
} }
line = replacer.Replace(line) line = replacer.Replace(line)
event, err := ParsePacket(line) event, err := ParsePacket(line)
if err != nil { if err != nil {
client.EmitNonBlocking(NewErrorEvent("parse", "Read failed: " + err.Error()))
client.EmitNonBlocking(NewErrorEvent("parse", "Read failed: "+err.Error()))
continue continue
} }
@ -911,9 +920,9 @@ func (client *Client) handleEvent(event *Event) {
} }
for i := range supportedCaps { for i := range supportedCaps {
if supportedCaps[i] == token {
if supportedCaps[i] == key {
client.mutex.Lock() client.mutex.Lock()
client.capsRequested = append(client.capsRequested, token)
client.capsRequested = append(client.capsRequested, key)
client.mutex.Unlock() client.mutex.Unlock()
break break
@ -945,10 +954,53 @@ func (client *Client) handleEvent(event *Event) {
client.capEnabled[token] = true client.capEnabled[token] = true
} }
client.mutex.Unlock() 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") _ = client.Send("CAP END")
} }
}
case "NAK": case "NAK":
{ {
// Remove offenders // 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 flagUser = flag.String("user", "test", "The client user/ident")
var flagPass = flag.String("pass", "", "The server password") var flagPass = flag.String("pass", "", "The server password")
var flagServer = flag.String("server", "localhost:6667", "The server to connect to") 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() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -32,6 +32,7 @@ func main() {
User: *flagUser, User: *flagUser,
Alternatives: strings.Split(*flagAlts, ","), Alternatives: strings.Split(*flagAlts, ","),
Password: *flagPass, Password: *flagPass,
Languages: []string{"no_NB", "no", "en_US", "en"},
}) })
client.AddHandler(handlers.Input) client.AddHandler(handlers.Input)
@ -94,6 +95,6 @@ func main() {
break 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 // The rate (lines per second) to send with Client.SendQueued. Default is 2, which is how
// clients that don't excess flood does it. // clients that don't excess flood does it.
SendRate int `json:"sendRate"` SendRate int `json:"sendRate"`
// Languages to request.
Languages []string `json:"languages"`
} }
// WithDefaults returns the config with the default values // WithDefaults returns the config with the default values

Loading…
Cancel
Save