kunt

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

commit 3fbe627e0a441b9e7a8314df7651cfdcaa78df62
parent 583ce1ab52fdf6d7df3662ddf9171c9283ce2363
Author: sin <sin@2f30.org>
Date:   Thu, 18 Apr 2013 11:33:38 +0100

irc: Update irc package

Diffstat:
Msrc/irc/events.go | 13+++++++++++++
Msrc/irc/irc.go | 58+++++++++++++++++++++++++++++++++++++++++-----------------
2 files changed, 54 insertions(+), 17 deletions(-)

diff --git a/src/irc/events.go b/src/irc/events.go @@ -17,3 +17,16 @@ func (i *IrcContext) AddEventHandler(ev IrcEvent) int { } return -1 } + +func (i *IrcContext) DelEventHandler(ev *IrcEvent) int { + if i.evnum == 0 { + return -1 //empty + } + for j := 0; j < i.evnum; j++ { + if &i.ev[j] == ev { + i.ev = append(i.ev[:j], i.ev[j+1:]...) + return 0 + } + } + return -1 +} diff --git a/src/irc/irc.go b/src/irc/irc.go @@ -8,12 +8,13 @@ import ( ) type IrcContext struct { - conn net.Conn // Actual connection - outgoingMsg chan IrcMessage // TX messaging channel - incomingMsg chan IrcMessage // RX messaging channel - ev [MAX_EVENTS]IrcEvent // array of callbacks - evnum int // number of callbacks - network ircNetwork // irc network state + conn net.Conn // Actual connection + outgoingMsg chan IrcMessage // TX messaging channel + incomingMsg chan IrcMessage // RX messaging channel + ev []IrcEvent // slice of callbacks + evnum int // number of callbacks + network ircNetwork // irc network state + intercepts []*chan IrcMessage // intercept chans } type IrcConfig struct { @@ -60,10 +61,32 @@ func NewIrcContext(ircConfig IrcConfig) *IrcContext { return &IrcContext{ outgoingMsg: make(chan IrcMessage), incomingMsg: make(chan IrcMessage), + ev: make([]IrcEvent, 0, MAX_EVENTS), network: network, + intercepts: make([]*chan IrcMessage, 0), } } +//Appends a new channel pointer to intercept rx +func (i *IrcContext) AddIntercept(a *chan IrcMessage) { + i.intercepts = append(i.intercepts, a) +} + +//Deletes based on pointer equality. caller must hold ref. +func (i *IrcContext) DelIntercept(ap *chan IrcMessage) int { + for j := 0; j < len(i.intercepts); j++ { + if ap == i.intercepts[j] { + if j == 0 { + i.intercepts = make([]*chan IrcMessage, 0) + return 0 + } + i.intercepts = append(i.intercepts[:j], i.intercepts[j+1:]...) + return 0 + } + } + return -1 //not found +} + // Add a channel to the set of channels func (i *IrcContext) AddChannel(s string) int { for c := 0; c < i.network.channum; c++ { @@ -133,6 +156,7 @@ func (i *IrcContext) read(buf []byte) (int, error) { // and form messages. func (i *IrcContext) rxLoop() error { var msg IrcMessage + var cmdpos, lastddot int for { var buf [512]byte @@ -156,18 +180,18 @@ func (i *IrcContext) rxLoop() error { } // At the moment - handle only PRIVMSG - if strings.Index(s, "PRIVMSG") != -1 { - sf := strings.FieldsFunc(s, func(r rune) bool { - switch r { - case ' ', '\t', '\n': - return true - } - return false - }) - msg.Prefix = sf[0] - msg.Cmd = sf[1] - msg.Params = []string{sf[2], sf[3]} + cmdpos = strings.Index(s, "PRIVMSG ") // the space is needed for discarding init msgs. + if cmdpos != -1 { + //XXX this is too fragile but soon le parser will save us. + msg.Prefix = s[1 : cmdpos-1] //discard the first : + lastddot = strings.Index(s[1:], ":") + msg.Cmd = "PRIVMSG" + msg.Params = []string{s[cmdpos+8 : lastddot], + s[lastddot+1:]} i.incomingMsg <- msg + for _, v := range i.intercepts { + *v <- msg + } } } return nil