Browse Source

Fixed crashes and other problems with the event handlers, fixed packet event not being created/propagated correctly

master
Gisle Aune 4 years ago
parent
commit
ef8d03bb70
  1. 59
      client.go
  2. 2
      event.go
  3. 6
      event_packet.go
  4. 2
      notes/protocol-samples.md

59
client.go

@ -85,6 +85,30 @@ func (client *Client) Context() context.Context {
return client.ctx
}
// Nick gets the nick of the client
func (client *Client) Nick() string {
client.mutex.RLock()
defer client.mutex.RUnlock()
return client.nick
}
// User gets the user/ident of the client
func (client *Client) User() string {
client.mutex.RLock()
defer client.mutex.RUnlock()
return client.user
}
// Host gets the hostname of the client
func (client *Client) Host() string {
client.mutex.RLock()
defer client.mutex.RUnlock()
return client.host
}
// ISupport gets the client's ISupport. This is mutable, and changes to it
// *will* affect the client.
func (client *Client) ISupport() *isupport.ISupport {
@ -133,6 +157,13 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
break
}
line = replacer.Replace(line)
event, err := ParsePacket(line)
if err != nil {
continue
}
client.Emit(event)
}
client.mutex.Lock()
@ -408,7 +439,7 @@ func (client *Client) handleEvent(event *Event) {
}
}
switch event.Name() {
switch event.name {
// Ping Pong
case "hook.tick":
@ -457,10 +488,10 @@ func (client *Client) handleEvent(event *Event) {
case "packet.001":
{
client.mutex.Lock()
client.nick = event.Args[1]
client.nick = event.Args[0]
client.mutex.Unlock()
client.Sendf("WHO %s", event.Args[1])
client.Sendf("WHO %s", event.Args[0])
}
case "packet.443":
@ -474,17 +505,21 @@ func (client *Client) handleEvent(event *Event) {
// "AltN" -> "AltN+1", ...
prev := client.config.Nick
sent := false
for _, alt := range client.config.Alternatives {
if nick == prev {
client.Sendf("NICK %s", nick)
return
client.Sendf("NICK %s", alt)
sent = true
break
}
prev = alt
}
// "LastAlt" -> "Nick23962"
client.Sendf("%s%05d", client.config.Nick, mathRand.Int31n(99999))
if !sent {
// "LastAlt" -> "Nick23962"
client.Sendf("NICK %s%05d", client.config.Nick, mathRand.Int31n(99999))
}
}
}
@ -533,7 +568,7 @@ func (client *Client) handleEvent(event *Event) {
}
}
if len(event.Args) < 2 || event.Args[2] != "*" {
if len(event.Args) < 3 || event.Args[2] != "*" {
client.mutex.RLock()
requestedCount := len(client.capsRequested)
client.mutex.RUnlock()
@ -618,22 +653,22 @@ func (client *Client) handleEvent(event *Event) {
user := event.Args[2]
host := event.Args[3]
client.mutex.Lock()
if nick == client.nick {
client.mutex.Lock()
client.user = user
client.host = host
client.mutex.Unlock()
}
client.mutex.Unlock()
}
case "packet.chghost":
{
client.mutex.Lock()
if event.Nick == client.nick {
client.mutex.Lock()
client.user = event.Args[1]
client.host = event.Args[2]
client.mutex.Unlock()
}
client.mutex.Unlock()
}
}
}

2
event.go

@ -52,7 +52,7 @@ func (event *Event) Verb() string {
// Name gets the event name, which is Kind and Verb separated by a dot.
func (event *Event) Name() string {
return event.kind + "." + event.verb
return event.name
}
// IsEither returns true if the event has the kind and one of the verbs.

6
event_packet.go

@ -10,7 +10,8 @@ var unescapeTags = strings.NewReplacer("\\\\", "\\", "\\:", ";", "\\s", " ", "\\
// ParsePacket parses and irc line and returns an event that's either of kind `packet`, `ctcp` or `ctcpreply`
func ParsePacket(line string) (Event, error) {
event := Event{Time: time.Now()}
event := NewEvent("packet", "")
event.Time = time.Now()
if len(line) == 0 {
return event, errors.New("irc: empty line")
@ -47,7 +48,7 @@ func ParsePacket(line string) (Event, error) {
prefixTokens := strings.Split(split[0][1:], "!")
event.Nick = prefixTokens[0]
if len(split) > 1 {
if len(prefixTokens) > 1 {
userhost := strings.Split(prefixTokens[1], "@")
if len(userhost) < 2 {
@ -90,5 +91,6 @@ func ParsePacket(line string) (Event, error) {
}
}
event.name = event.kind + "." + strings.ToLower(event.verb)
return event, nil
}

2
notes/protocol-samples.md

@ -80,4 +80,4 @@ JOIN #Test
:archgisle.lan 366 Testify #Test :End of /NAMES list.
:Gisle!~irce@10.32.0.1 MODE #Test +osv Testify Testify
:Gisle!~irce@10.32.0.1 MODE #Test +N-s
```
```
Loading…
Cancel
Save