kunt

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

commit 743755dffd7f149781d5c6f52d6b2f232252ad80
parent e1967c5e1d907594d55e30010579bf997f72e734
Author: sin <sin@2f30.org>
Date:   Sun May  5 13:32:22 +0100

various irc updates

Diffstat:
src/irc/irc.go | 31++++++++++++++++---------------
src/irc/message.go | 58+++++++++++++++++++++++++++++++++++++++++++++++-----------
src/kunt/kunt.go | 13+++++++++++--
3 files changed, 74 insertions(+), 28 deletions(-)
diff --git a/src/irc/irc.go b/src/irc/irc.go @@ -21,6 +21,7 @@ type IrcConfig 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 @@ -30,6 +31,7 @@ type IrcConfig struct { type ircChan struct { name string // name of irc channel + key string // password for irc channel joined bool // whether we have joined this channel or not } @@ -37,6 +39,7 @@ type ircNetwork struct { name string // network name nick string // nickname user string // username + realName string // real name pass string // password serv string // server address port string // server port @@ -51,6 +54,7 @@ func NewIrcContext(ircConfig IrcConfig) *IrcContext { name: ircConfig.NetworkName, nick: ircConfig.Nick, user: ircConfig.User, + realName: ircConfig.RealName, pass: ircConfig.Pass, serv: ircConfig.Serv, port: ircConfig.Port, @@ -85,13 +89,13 @@ func (i *IrcContext) DelIntercept(ap *chan IrcMessage) error { return fmt.Errorf("Intercept not found") } -func (i *IrcContext) AddChannel(s string) error { +func (i *IrcContext) AddChannel(s string, key string) error { for _, v := range i.network.channels { if v.name == s { return fmt.Errorf("Channel %s already added", s) } } - i.network.channels = append(i.network.channels, ircChan{s, false}) + i.network.channels = append(i.network.channels, ircChan{s, key, false}) return nil } @@ -99,7 +103,7 @@ func (i *IrcContext) JoinChannel(s string) error { for c, v := range i.network.channels { if v.name == s { if !v.joined { - i.Join(v.name) + i.Join(v.name, v.key) i.network.channels[c].joined = true return nil } else { @@ -122,11 +126,11 @@ func (i *IrcContext) JoinChannels() error { return nil } -func (i *IrcContext) PartChannel(s string) error { +func (i *IrcContext) PartChannel(s string, text string) error { for c, v := range i.network.channels { if v.name == s { if v.joined { - i.Part(s) + i.Part(s, text) i.network.channels[c].joined = false return nil } else { @@ -137,15 +141,6 @@ func (i *IrcContext) PartChannel(s string) error { return fmt.Errorf("Can't find channel %s", s) } -func (i *IrcContext) PartChannels(s []string) error { - for _, v := range s { - if err := i.PartChannel(v); err != nil { - return err - } - } - return nil -} - // Connect to the server. Do not join any channels by default. func (i *IrcContext) Connect() error { service := i.network.serv + ":" + i.network.port @@ -173,9 +168,15 @@ func (i *IrcContext) Connect() error { go i.outgoingMsgLoop() go i.RxRawMessages() + return nil +} + +func (i *IrcContext) Login() { + if i.network.pass != "" { + i.Pass() + } i.Nick() i.User() - return nil } func (i *IrcContext) read(buf []byte) (int, error) { diff --git a/src/irc/message.go b/src/irc/message.go @@ -17,7 +17,6 @@ const ( IRCTEXT ) -// Send PONG reply func (i *IrcContext) Pong(host string) { msg := IrcMessage{ Command: "PONG", @@ -26,7 +25,14 @@ func (i *IrcContext) Pong(host string) { i.outgoingMsg <- msg } -// Send the nickname +func (i *IrcContext) Pass() { + msg := IrcMessage{ + Command: "PASS", + Args: []string{i.network.pass}, + } + i.outgoingMsg <- msg +} + func (i *IrcContext) Nick() { msg := IrcMessage{ Command: "NICK", @@ -35,38 +41,45 @@ func (i *IrcContext) Nick() { i.outgoingMsg <- msg } -// Send the username func (i *IrcContext) User() { msg := IrcMessage{ Command: "USER", Args: []string{ i.network.user, "* 8", - ":" + i.network.nick, + ":" + i.network.realName, }, } i.outgoingMsg <- msg } -// Join a channel -func (i *IrcContext) Join(channel string) { +func (i *IrcContext) Join(channel string, key string) { msg := IrcMessage{ Command: "JOIN", - Args: []string{channel}, + Args: []string{channel, key}, } i.outgoingMsg <- msg } -// Part a channel -func (i *IrcContext) Part(channel string) { +func (i *IrcContext) Part(channel string, text string) { msg := IrcMessage{ Command: "PART", - Args: []string{channel}, + Args: []string{ + channel, + ":" + text, + }, + } + i.outgoingMsg <- msg +} + +func (i *IrcContext) Quit(text string) { + msg := IrcMessage{ + Command: "QUIT", + Args: []string{":" + text}, } i.outgoingMsg <- msg } -// Send a PRIVMSG func (i *IrcContext) Privmsg(channel string, text string) { msg := IrcMessage{ Command: "PRIVMSG", @@ -78,9 +91,32 @@ func (i *IrcContext) Privmsg(channel string, text string) { i.outgoingMsg <- msg } +func (i *IrcContext) Notice(nick string, text string) { + msg := IrcMessage{ + Command: "NOTICE", + Args: []string{ + nick, + ":" + text, + }, + } + i.outgoingMsg <- msg +} + +func (i *IrcContext) Topic(channel string, text string) { + msg := IrcMessage{ + Command: "TOPIC", + Args: []string{ + channel, + ":" + text, + }, + } + i.outgoingMsg <- msg +} + func (i *IrcContext) ParseRawMessage(raw string) IrcMessage { var msg IrcMessage msg.Raw = raw + raw = strings.TrimSpace(raw) if raw[0] == ':' { if i := strings.Index(raw, " "); i != -1 { msg.Source = raw[1:i] diff --git a/src/kunt/kunt.go b/src/kunt/kunt.go @@ -362,6 +362,7 @@ func main() { NetworkName: "grnet", Nick: botname, User: "z0mg", + RealName: botname, Serv: hostport[0], Port: hostport[1], Tls: *sslon, @@ -381,8 +382,16 @@ func main() { kunt.ircCtx.Pong(msg.Args[0]) }}) - kunt.ircCtx.Connect() - kunt.ircCtx.AddChannel("#2f30") + kunt.ircCtx.AddEventHandler(irc.IrcEvent{"ERROR", func(msg irc.IrcMessage) { + os.Exit(0) + }}) + + err = kunt.ircCtx.Connect() + if err != nil { + log.Fatal(err) + } + kunt.ircCtx.Login() + kunt.ircCtx.AddChannel("#2f30", "") kunt.ircCtx.JoinChannels() donechan := make(chan bool)