mirror of
https://github.com/gissleh/irc.git
synced 2026-09-29 00:25:33 +00:00
Fixed crashes and other problems with the event handlers, fixed packet event not being created/propagated correctly
This commit is contained in:
@@ -85,6 +85,30 @@ func (client *Client) Context() context.Context {
|
|||||||
return client.ctx
|
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
|
// ISupport gets the client's ISupport. This is mutable, and changes to it
|
||||||
// *will* affect the client.
|
// *will* affect the client.
|
||||||
func (client *Client) ISupport() *isupport.ISupport {
|
func (client *Client) ISupport() *isupport.ISupport {
|
||||||
@@ -133,6 +157,13 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
line = replacer.Replace(line)
|
line = replacer.Replace(line)
|
||||||
|
|
||||||
|
event, err := ParsePacket(line)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
client.Emit(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
client.mutex.Lock()
|
client.mutex.Lock()
|
||||||
@@ -408,7 +439,7 @@ func (client *Client) handleEvent(event *Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch event.Name() {
|
switch event.name {
|
||||||
|
|
||||||
// Ping Pong
|
// Ping Pong
|
||||||
case "hook.tick":
|
case "hook.tick":
|
||||||
@@ -457,10 +488,10 @@ func (client *Client) handleEvent(event *Event) {
|
|||||||
case "packet.001":
|
case "packet.001":
|
||||||
{
|
{
|
||||||
client.mutex.Lock()
|
client.mutex.Lock()
|
||||||
client.nick = event.Args[1]
|
client.nick = event.Args[0]
|
||||||
client.mutex.Unlock()
|
client.mutex.Unlock()
|
||||||
|
|
||||||
client.Sendf("WHO %s", event.Args[1])
|
client.Sendf("WHO %s", event.Args[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
case "packet.443":
|
case "packet.443":
|
||||||
@@ -474,17 +505,21 @@ func (client *Client) handleEvent(event *Event) {
|
|||||||
|
|
||||||
// "AltN" -> "AltN+1", ...
|
// "AltN" -> "AltN+1", ...
|
||||||
prev := client.config.Nick
|
prev := client.config.Nick
|
||||||
|
sent := false
|
||||||
for _, alt := range client.config.Alternatives {
|
for _, alt := range client.config.Alternatives {
|
||||||
if nick == prev {
|
if nick == prev {
|
||||||
client.Sendf("NICK %s", nick)
|
client.Sendf("NICK %s", alt)
|
||||||
return
|
sent = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
prev = alt
|
prev = alt
|
||||||
}
|
}
|
||||||
|
|
||||||
// "LastAlt" -> "Nick23962"
|
if !sent {
|
||||||
client.Sendf("%s%05d", client.config.Nick, mathRand.Int31n(99999))
|
// "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()
|
client.mutex.RLock()
|
||||||
requestedCount := len(client.capsRequested)
|
requestedCount := len(client.capsRequested)
|
||||||
client.mutex.RUnlock()
|
client.mutex.RUnlock()
|
||||||
@@ -618,22 +653,22 @@ func (client *Client) handleEvent(event *Event) {
|
|||||||
user := event.Args[2]
|
user := event.Args[2]
|
||||||
host := event.Args[3]
|
host := event.Args[3]
|
||||||
|
|
||||||
client.mutex.Lock()
|
|
||||||
if nick == client.nick {
|
if nick == client.nick {
|
||||||
|
client.mutex.Lock()
|
||||||
client.user = user
|
client.user = user
|
||||||
client.host = host
|
client.host = host
|
||||||
|
client.mutex.Unlock()
|
||||||
}
|
}
|
||||||
client.mutex.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case "packet.chghost":
|
case "packet.chghost":
|
||||||
{
|
{
|
||||||
client.mutex.Lock()
|
|
||||||
if event.Nick == client.nick {
|
if event.Nick == client.nick {
|
||||||
|
client.mutex.Lock()
|
||||||
client.user = event.Args[1]
|
client.user = event.Args[1]
|
||||||
client.host = event.Args[2]
|
client.host = event.Args[2]
|
||||||
|
client.mutex.Unlock()
|
||||||
}
|
}
|
||||||
client.mutex.Unlock()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func (event *Event) Verb() string {
|
|||||||
|
|
||||||
// Name gets the event name, which is Kind and Verb separated by a dot.
|
// Name gets the event name, which is Kind and Verb separated by a dot.
|
||||||
func (event *Event) Name() string {
|
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.
|
// IsEither returns true if the event has the kind and one of the verbs.
|
||||||
|
|||||||
+4
-2
@@ -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`
|
// ParsePacket parses and irc line and returns an event that's either of kind `packet`, `ctcp` or `ctcpreply`
|
||||||
func ParsePacket(line string) (Event, error) {
|
func ParsePacket(line string) (Event, error) {
|
||||||
event := Event{Time: time.Now()}
|
event := NewEvent("packet", "")
|
||||||
|
event.Time = time.Now()
|
||||||
|
|
||||||
if len(line) == 0 {
|
if len(line) == 0 {
|
||||||
return event, errors.New("irc: empty line")
|
return event, errors.New("irc: empty line")
|
||||||
@@ -47,7 +48,7 @@ func ParsePacket(line string) (Event, error) {
|
|||||||
prefixTokens := strings.Split(split[0][1:], "!")
|
prefixTokens := strings.Split(split[0][1:], "!")
|
||||||
|
|
||||||
event.Nick = prefixTokens[0]
|
event.Nick = prefixTokens[0]
|
||||||
if len(split) > 1 {
|
if len(prefixTokens) > 1 {
|
||||||
userhost := strings.Split(prefixTokens[1], "@")
|
userhost := strings.Split(prefixTokens[1], "@")
|
||||||
|
|
||||||
if len(userhost) < 2 {
|
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
|
return event, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,4 +80,4 @@ JOIN #Test
|
|||||||
:archgisle.lan 366 Testify #Test :End of /NAMES list.
|
: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 +osv Testify Testify
|
||||||
:Gisle!~irce@10.32.0.1 MODE #Test +N-s
|
:Gisle!~irce@10.32.0.1 MODE #Test +N-s
|
||||||
```⏎
|
```
|
||||||
Reference in New Issue
Block a user