Browse Source

fix query messages not being put in right target.

master
Gisle Aune 4 years ago
parent
commit
e06199c378
  1. 19
      client.go
  2. 20
      client_test.go
  3. 32
      internal/irctest/eventlog.go

19
client.go

@ -11,6 +11,7 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"log"
mathRand "math/rand" mathRand "math/rand"
"net" "net"
"sort" "sort"
@ -1362,8 +1363,8 @@ func (client *Client) handleEvent(event *Event) {
target := Target(client.status) target := Target(client.status)
targetName := event.Arg(0) targetName := event.Arg(0)
if targetName == client.nick { if targetName == client.nick {
target := client.Target("query", targetName)
if target == nil {
queryTarget := client.Target("query", targetName)
if queryTarget == nil {
query := &Query{ query := &Query{
id: client.id, id: client.id,
user: list.User{ user: list.User{
@ -1379,8 +1380,10 @@ func (client *Client) handleEvent(event *Event) {
_ = client.AddTarget(query) _ = client.AddTarget(query)
event.RenderTags["spawned"] = query.id event.RenderTags["spawned"] = query.id
target = query
queryTarget = query
} }
target = queryTarget
} else { } else {
channel := client.Channel(targetName) channel := client.Channel(targetName)
if channel != nil { if channel != nil {
@ -1389,11 +1392,10 @@ func (client *Client) handleEvent(event *Event) {
} }
target = channel target = channel
} else {
target = client.status
} }
} }
log.Println(targetName == client.nick)
client.handleInTarget(target, event) client.handleInTarget(target, event)
} }
@ -1540,13 +1542,8 @@ func (client *Client) handleInTarget(target Target, event *Event) {
return return
} }
client.mutex.RLock()
target.Handle(event, client)
event.targets = append(event.targets, target) event.targets = append(event.targets, target)
client.mutex.RUnlock()
target.Handle(event, client)
} }
func generateClientID(prefix string) string { func generateClientID(prefix string) string {

20
client_test.go

@ -21,8 +21,12 @@ func TestClient(t *testing.T) {
AutoJoinInvites: true, AutoJoinInvites: true,
}) })
logger := irctest.EventLog{}
client.AddHandler(handlers.Input) client.AddHandler(handlers.Input)
client.AddHandler(handlers.MRoleplay) client.AddHandler(handlers.MRoleplay)
client.AddHandler(handlers.CTCP)
client.AddHandler(logger.Handler)
t.Logf("Client.ID = %#+v", client.ID()) t.Logf("Client.ID = %#+v", client.ID())
if client.ID() == "" { if client.ID() == "" {
@ -188,6 +192,14 @@ func TestClient(t *testing.T) {
return errors.New("Hunter2 should have changed the host: " + userHunter2.Host) return errors.New("Hunter2 should have changed the host: " + userHunter2.Host)
} }
event := logger.Last("packet", "PRIVMSG")
if event == nil {
return errors.New("did not find last channel message")
}
if event.ChannelTarget() == nil {
return errors.New("event lacks channel target")
}
return nil return nil
}}, }},
{Server: ":Hunter2!~test2@172.17.37.1 PRIVMSG Test768 :Hello, World"}, {Server: ":Hunter2!~test2@172.17.37.1 PRIVMSG Test768 :Hello, World"},
@ -199,6 +211,14 @@ func TestClient(t *testing.T) {
return errors.New("Did not find query") return errors.New("Did not find query")
} }
event := logger.Last("packet", "PRIVMSG")
if event == nil {
return errors.New("did not find last query message")
}
if event.QueryTarget() == nil {
return errors.New("event lacks query target")
}
return nil return nil
}}, }},
{Server: ":Hunter2!~test2@172.17.37.1 NICK SevenAsterisks"}, {Server: ":Hunter2!~test2@172.17.37.1 NICK SevenAsterisks"},

32
internal/irctest/eventlog.go

@ -0,0 +1,32 @@
package irctest
import "github.com/gissleh/irc"
type EventLog struct {
events []*irc.Event
}
func (l *EventLog) First(kind, verb string) *irc.Event {
for _, e := range l.events {
if e.Verb() == verb && e.Kind() == kind {
return e
}
}
return nil
}
func (l *EventLog) Last(kind, verb string) *irc.Event {
for i := len(l.events) - 1; i >= 0; i-- {
e := l.events[i]
if e.Verb() == verb && e.Kind() == kind {
return e
}
}
return nil
}
func (l *EventLog) Handler(event *irc.Event, _ *irc.Client) {
l.events = append(l.events, event)
}
Loading…
Cancel
Save