diff --git a/auth.go b/auth.go index 84ad719..89caaa0 100644 --- a/auth.go +++ b/auth.go @@ -8,8 +8,8 @@ import ( "github.com/kless/goconfig/config" ) -var auth *config.Config const authFile = "auth.conf" +var auth *config.Config func readAuth() { var err os.Error; @@ -38,8 +38,7 @@ func addAccess(conn *irc.Conn, channel, nick, flags string) (string, string) { } auth.AddOption(section, n.Host, nflags) - err := auth.WriteFile(authFile, 0644, "") - if err != nil { + if err := auth.WriteFile(authFile, 0644, ""); err != nil { say(conn, channel, "Error while writing to %s", authFile) } // config.WriteFile destroys the config, so diff --git a/handler.go b/handler.go index b4afc5f..d0bc1ee 100644 --- a/handler.go +++ b/handler.go @@ -18,6 +18,7 @@ var commands = map [string]func(*irc.Conn, string, string, string) { "flags": flags, "add": add, "topic": topic, + "appendtopic": appendtopic, } const googleAPIKey = "ABQIAAAA6-N_jl4ETgtMf2M52JJ_WRQjQjNunkAJHIhTdFoxe8Di7fkkYhRRcys7ZxNbH3MIy_MKKcEO4-9_Ag" @@ -77,7 +78,11 @@ func command(conn *irc.Conn, nick, text, target string) { func say(conn *irc.Conn, target, message string, a ...interface{}) { text := strings.Replace(fmt.Sprintf(message, a...), "\n", " ", -1) - conn.Privmsg(target, text) + if isChannel(target) { + conn.Privmsg(target, text) + } else { + conn.Notice(target, text) + } } func youtube(conn *irc.Conn, nick, video, channel string) { @@ -237,13 +242,34 @@ func flags(conn *irc.Conn, nick, args, channel string) { } func topic(conn *irc.Conn, nick, args, channel string) { - if !isChannel(channel) { + if !isChannel(channel) || !hasAccess(conn, channel, nick, "t") { return } - if hasAccess(conn, channel, nick, "t") { - if args != "" { - conn.Topic(channel, args) - } - // TODO: appendtopic, query for topic with blank args + section := conn.Network + " " + channel + if args != "" { + updateConf(section, "basetopic", args) + conn.Topic(channel, args) + } else { + basetopic, _ := conf.String(section, "basetopic") + say(conn, nick, "Basetopic: %s", basetopic) } } +func appendtopic(conn *irc.Conn, nick, args, channel string) { + if !isChannel(channel) || !hasAccess(conn, channel, nick, "t") { + return + } + c := conn.GetChannel(channel) + if c == nil { + say(conn, channel, "Error while getting channel information for %s", channel) + return + } + + section := conn.Network + " " + channel + basetopic, _ := conf.String(section, "basetopic") + if !strings.HasPrefix(c.Topic, basetopic) { + basetopic = c.Topic + say(conn, nick, "New basetopic: %s", basetopic) + updateConf(section, "basetopic", basetopic) + } + conn.Topic(channel, basetopic + args) +} diff --git a/rbot.go b/rbot.go index de5a643..3b3b230 100644 --- a/rbot.go +++ b/rbot.go @@ -8,18 +8,13 @@ import ( "github.com/kless/goconfig/config" ) +const confFile = "rbot.conf" var trigger string var sections []string var conf *config.Config func main() { - var err os.Error; - conf, err = config.ReadDefault("rbot.conf") - if (err != nil) { - fmt.Printf("Config error: %s\n", err) - os.Exit(1) - } - + readConf() trigger = readConfString("DEFAULT", "trigger") readAuth() @@ -84,6 +79,14 @@ func autojoin(conn *irc.Conn) { } } +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 { value, err := conf.String(section, option) if err != nil { @@ -98,3 +101,11 @@ func readConfBool(section, option string) bool { } return value } +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() +}