kunt

golang IRC bot
git clone git://git.2f30.org/kunt
Log | Files | Refs | LICENSE

commit 883b93a38d71e92b48a36daaada2261aade15933
parent 1513c7af25f7fc4805dcd864da443c52745f4682
Author: dsp <dsp@2f30.org>
Date:   Wed, 30 Apr 2014 18:25:56 +0100

Now Config support multiple channels,fixed a deadlock in JoinChannels

Diffstat:
Msrc/irc/irc.go | 51++++++++++++++++++++++++++++-----------------------
1 file changed, 28 insertions(+), 23 deletions(-)

diff --git a/src/irc/irc.go b/src/irc/irc.go @@ -6,13 +6,13 @@ package irc import ( "crypto/tls" + "encoding/json" "fmt" + "io/ioutil" "log" "net" "strings" "sync" - "io/ioutil" - "encoding/json" ) type Context struct { @@ -27,15 +27,16 @@ type Context struct { } type Config struct { - NetworkName string // network name - Nick string // nickname - User string // username - RealName string // real name - Pass string // password - Serv string // server address - Port string // server port - Ssl bool // enable/disable ssl - Ipv6 bool // enable/disable ipv6 (not implemented) + NetworkName string // network name + Nick string // nickname + User string // username + RealName string // real name + Pass string // password + Serv string // server address + Port string // server port + Ssl bool // enable/disable ssl + Ipv6 bool // enable/disable ipv6 (not implemented) + Channels []string // array of channels to join } type Channel struct { @@ -58,22 +59,28 @@ type network struct { } func LoadConfig(fname string) (con *Config, err error) { - bytes,err := ioutil.ReadFile(fname) + bytes, err := ioutil.ReadFile(fname) if err != nil { - log.Printf(" [LoadConfig] error reading file %s\n",fname) - return nil,err + log.Printf(" [LoadConfig] error reading file %s\n", fname) + return nil, err } con = new(Config) - err = json.Unmarshal(bytes,&con) + err = json.Unmarshal(bytes, &con) if err != nil { - log.Printf(" [LoadConfig] error reading file %s\n",fname) - return nil,err + log.Printf(" [LoadConfig] error reading file %s\n", fname) + return nil, err } return con, nil } // Create a new IrcContext func NewIrcContext(ircConfig Config) *Context { + chans := make([]Channel, len(ircConfig.Channels)) + + for i, _ := range chans { + chans[i] = Channel{ircConfig.Channels[i],"",false} + } + network := network{ name: ircConfig.NetworkName, nick: ircConfig.Nick, @@ -84,8 +91,9 @@ func NewIrcContext(ircConfig Config) *Context { port: ircConfig.Port, tls: ircConfig.Ssl, ipv6: ircConfig.Ipv6, - channels: make([]Channel, 0), + channels: chans, } + return &Context{ outgoingMsg: make(chan Message), incomingMsg: make(chan Message), @@ -163,11 +171,8 @@ func (i *Context) JoinChannel(c string, key string) { } func (i *Context) JoinChannels() { - i.Lock() - defer i.Unlock() - for j, v := range i.network.channels { - i.Join(v.name, v.key) - i.network.channels[j].joined = true + for _, v := range i.network.channels { + i.JoinChannel(v.name, v.key) } }