goirc/rbot.go

114 lines
2.4 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
)
const confFile = "rbot.conf"
2010-10-15 21:13:53 +00:00
var trigger string
var sections []string
var conf *config.Config
2010-10-14 00:09:48 +00:00
2010-10-13 19:10:24 +00:00
func main() {
readConf()
trigger = readConfString("DEFAULT", "trigger")
2010-10-16 19:38:40 +00:00
readAuth()
2010-10-15 21:13:53 +00:00
sections = conf.Sections()
for _, s := range sections {
if strings.Index(s, " ") == -1 && s != "DEFAULT" {
// found a network
go connect(s)
2010-10-15 21:13:53 +00:00
}
}
<- make(chan bool)
}
func connect(network string) {
if !readConfBool(network, "autoconnect") {
2010-10-15 21:13:53 +00:00
return
}
server := readConfString(network, "server")
nick := readConfString(network, "nick")
user := readConfString(network, "user")
ssl := readConfBool(network, "ssl")
nickserv, _ := conf.String(network, "nickserv")
2010-10-13 19:10:24 +00:00
2010-10-14 00:09:48 +00:00
c := irc.New(nick, user, user)
c.Network = network
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)
if len(nickserv) > 0 {
conn.Privmsg("NickServ", "IDENTIFY " + nickserv)
} else {
autojoin(conn)
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)
c.AddHandler("mode", handleMode)
2010-10-27 05:49:57 +00:00
c.AddHandler("join", handleJoin)
c.AddHandler("invite", handleInvite)
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
func autojoin(conn *irc.Conn) {
for _, s := range sections {
split := strings.Split(s, " ", 2)
if len(split) == 2 && split[0] == conn.Network {
// found a channel
if readConfBool(s, "autojoin") {
fmt.Printf("Joining %s on %s\n", split[1], conn.Network)
conn.Join(split[1])
}
}
}
}
func readConf() {
var err os.Error
conf, err = config.ReadDefault("rbot.conf")
if (err != nil) {
fmt.Printf("Config error: %s\n", err)
os.Exit(1)
}
}
func readConfString(section, option string) string {
2010-10-15 21:13:53 +00:00
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(section, option string) bool {
2010-10-15 21:13:53 +00:00
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
}
func updateConf(section, option, value string) {
conf.AddOption(section, option, value)
if err := conf.WriteFile(confFile, 0644, ""); err != nil {
panic("Error while writing to " + confFile)
}
// config.WriteFile destroys the config, so
readConf()
}