kunt

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

commit a5aaff19f49aaae4e010aa6ca70a7b0c9d63cc93
parent eae639bb78048f812ec83c72aea87db48d04deb0
Author: sin <sin@2f30.org>
Date:   Wed,  8 May 2013 16:20:19 +0100

ensure proper locking in the irc package

Diffstat:
Msrc/irc/events.go | 4++++
Msrc/irc/irc.go | 10++++++++++
2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/src/irc/events.go b/src/irc/events.go @@ -10,6 +10,8 @@ type IrcEvent struct { } func (i *IrcContext) AddEventHandler(ev IrcEvent) error { + i.Lock() + defer i.Unlock() for j := 0; j < i.evnum; j++ { if i.ev[j].Command == ev.Command { return fmt.Errorf("Event %s already handled", @@ -22,6 +24,8 @@ func (i *IrcContext) AddEventHandler(ev IrcEvent) error { } func (i *IrcContext) DelEventHandler(ev *IrcEvent) error { + i.Lock() + defer i.Unlock() for j := 0; j < i.evnum; j++ { if &i.ev[j] == ev { i.ev = append(i.ev[:j], i.ev[j+1:]...) diff --git a/src/irc/irc.go b/src/irc/irc.go @@ -6,6 +6,7 @@ import ( "log" "net" "strings" + "sync" ) type IrcContext struct { @@ -16,6 +17,7 @@ type IrcContext struct { evnum int // number of callbacks network ircNetwork // irc network state intercepts []*chan IrcMessage // intercept chans + sync.Mutex } type IrcConfig struct { @@ -73,10 +75,14 @@ func NewIrcContext(ircConfig IrcConfig) *IrcContext { } func (i *IrcContext) AddIntercept(a *chan IrcMessage) { + i.Lock() + defer i.Unlock() i.intercepts = append(i.intercepts, a) } func (i *IrcContext) DelIntercept(ap *chan IrcMessage) error { + i.Lock() + defer i.Unlock() for j := 0; j < len(i.intercepts); j++ { if ap == i.intercepts[j] { if j == 0 { @@ -91,6 +97,8 @@ func (i *IrcContext) DelIntercept(ap *chan IrcMessage) error { } func (i *IrcContext) JoinChannel(channel string, key string) error { + i.Lock() + defer i.Unlock() found := false for _, v := range i.network.channels { if v.name == channel { @@ -119,6 +127,8 @@ func (i *IrcContext) JoinChannel(channel string, key string) error { } func (i *IrcContext) PartChannel(s string, text string) error { + i.Lock() + defer i.Unlock() for c, v := range i.network.channels { if v.name == s { if v.joined {