goirc/rbot.go

86 lines
1.9 KiB
Go
Raw Normal View History

2010-10-13 19:10:24 +00:00
package main
import (
"irc"
"fmt"
2010-10-14 00:09:48 +00:00
"os"
2010-10-15 21:13:53 +00:00
"strings"
2010-10-14 00:09:48 +00:00
"github.com/kless/goconfig/config"
2010-10-13 19:10:24 +00:00
)
2010-10-15 21:13:53 +00:00
var trigger string
var sections []string
2010-10-14 00:09:48 +00:00
2010-10-13 19:10:24 +00:00
func main() {
2010-10-15 21:13:53 +00:00
conf, err := config.ReadDefault("rbot.conf")
if (err != nil) {
fmt.Printf("Config error: %s\n", err)
os.Exit(1)
}
trigger = readConfString(conf, "DEFAULT", "trigger")
sections = conf.Sections()
for _, s := range sections {
if strings.Index(s, " ") == -1 && s != "DEFAULT" {
// found a network
go connect(conf, s)
}
}
<- make(chan bool)
}
func connect(conf *config.Config, network string) {
if !readConfBool(conf, network, "autoconnect") {
return
}
server := readConfString(conf, network, "server")
nick := readConfString(conf, network, "nick")
user := readConfString(conf, network, "user")
ssl := readConfBool(conf, network, "ssl")
2010-10-13 19:10:24 +00:00
2010-10-14 00:09:48 +00:00
c := irc.New(nick, user, user)
2010-10-13 19:10:24 +00:00
c.AddHandler("connected",
func(conn *irc.Conn, line *irc.Line) {
2010-10-15 21:13:53 +00:00
fmt.Printf("Connected to %s!\n", conn.Host)
for _, s := range sections {
split := strings.Split(s, " ", 2)
if len(split) == 2 && split[0] == network {
// found a channel
if readConfBool(conf, s, "autojoin") {
fmt.Printf("Joining %s on %s\n", split[1], network)
conn.Join(split[1])
}
}
2010-10-14 00:09:48 +00:00
}
2010-10-13 19:10:24 +00:00
})
2010-10-14 01:16:16 +00:00
c.AddHandler("privmsg", handlePrivmsg)
2010-10-13 19:10:24 +00:00
for {
fmt.Printf("Connecting to %s...\n", server)
if err := c.Connect(server, ssl, ""); err != nil {
fmt.Printf("Connection error: %s\n", err)
break
}
for err := range c.Err {
fmt.Printf("goirc error: %s\n", err)
}
}
}
2010-10-14 00:09:48 +00:00
2010-10-15 21:13:53 +00:00
func readConfString(conf *config.Config, section, option string) string {
value, err := conf.String(section, option)
if err != nil {
panic(fmt.Sprintf("Config error: %s", err));
2010-10-14 00:09:48 +00:00
}
2010-10-15 21:13:53 +00:00
return value
}
func readConfBool(conf *config.Config, section, option string) bool {
value, err := conf.Bool(section, option)
if err != nil {
panic(fmt.Sprintf("Config error: %s", err));
2010-10-14 00:09:48 +00:00
}
2010-10-15 21:13:53 +00:00
return value
2010-10-14 00:09:48 +00:00
}