Browse Source

Added auto-join

master
Gisle Aune 6 years ago
parent
commit
00fd13e1d9
  1. 12
      channel.go
  2. 31
      client.go

12
channel.go

@ -10,6 +10,7 @@ import (
type Channel struct {
name string
userlist *list.List
parted bool
}
// Kind returns "channel"
@ -27,6 +28,11 @@ func (channel *Channel) UserList() list.Immutable {
return channel.userlist.Immutable()
}
// Parted returnes whether the channel has been parted
func (channel *Channel) Parted() bool {
return channel.parted
}
// Handle handles messages routed to this channel by the client's event loop
func (channel *Channel) Handle(event *Event, client *Client) {
switch event.Name() {
@ -47,6 +53,12 @@ func (channel *Channel) Handle(event *Event, client *Client) {
}
case "packet.part", "packet.quit":
{
// Close the target if the client has left
if event.Nick == client.Nick() && event.Name() == "packet.part" {
channel.parted = true
client.RemoveTarget(channel)
}
channel.userlist.Remove(event.Nick)
}
case "packet.nick":

31
client.go

@ -503,6 +503,11 @@ func (client *Client) RemoveTarget(target Target) (id string, err error) {
client.targets = client.targets[:len(client.targets)-1]
delete(client.targteIds, target)
// Ensure the channel has been parted
if channel, ok := target.(*Channel); ok && !channel.parted {
client.SendQueuedf("/part %s")
}
return
}
}
@ -621,6 +626,11 @@ func (client *Client) handleEvent(event *Event) {
}
}
// For events that were created with targets, handle them now there now.
for _, target := range event.targets {
target.Handle(event, client)
}
switch event.name {
// Ping Pong
@ -1025,6 +1035,27 @@ func (client *Client) handleEvent(event *Event) {
{
client.handleInTargets(event.Nick, event)
}
// Auto-join
case "packet.376", "packet.422":
{
client.mutex.RLock()
channels := make([]string, 0, len(client.targets))
rejoinEvent := NewEvent("info", "rejoin")
for _, target := range client.targets {
if channel, ok := target.(*Channel); ok {
channels = append(channels, channel.Name())
rejoinEvent.targets = append(rejoinEvent.targets, target)
rejoinEvent.targetIds[target] = client.targteIds[target]
}
}
client.mutex.RUnlock()
client.Sendf("JOIN %s", strings.Join(channels, ","))
client.EmitNonBlocking(rejoinEvent)
}
}
if len(event.targets) == 0 {

Loading…
Cancel
Save