func learnFileLines(b *cobe.Cobe2Brain, path string) error { f, err := os.Open(path) if err != nil { return err } s := bufio.NewScanner(bufio.NewReader(f)) for s.Scan() { fmt.Println(s.Text()) b.Learn(s.Text()) } return nil }
func RunForever(b *cobe.Cobe2Brain, o *Options) { stop := make(chan bool) conn := irc.SimpleClient(o.Nick) conn.Config().Server = o.Server conn.Me().Ident = o.Nick conn.Me().Name = o.Nick conn.HandleFunc("connected", func(conn *irc.Conn, line *irc.Line) { log.Printf("Connected to %s. Joining %s.", o.Server, strings.Join(o.Channels, ", ")) for _, channel := range o.Channels { conn.Join(channel) } }) conn.HandleFunc("disconnected", func(conn *irc.Conn, line *irc.Line) { log.Printf("Disconnected from %s.", o.Server) backoffConnect(conn, o) }) conn.HandleFunc("kick", func(conn *irc.Conn, line *irc.Line) { if line.Args[1] == o.Nick { var channel = line.Args[0] log.Printf("Kicked from %s. Rejoining.", channel) conn.Join(channel) } }) // The space after comma/colon is needed so we won't treat // urls as messages spoken to http. userMsg := regexp.MustCompile(`^(\S+)[,:]\s(.*?)$`) conn.HandleFunc("privmsg", func(conn *irc.Conn, line *irc.Line) { user := line.Nick if in(o.Ignore, user) { log.Printf("Ignoring privmsg from %s", user) return } target := line.Args[0] if !in(o.Channels, target) { log.Printf("Ignoring privmsg on %s", target) return } var to, msg string groups := userMsg.FindStringSubmatch(line.Args[1]) if len(groups) > 0 { to = groups[1] msg = groups[2] } else { msg = line.Args[1] } msg = strings.TrimSpace(msg) log.Printf("Learn: %s", msg) b.Learn(msg) if to == o.Nick { reply := b.Reply(msg) log.Printf("Reply: %s", reply) conn.Privmsg(target, fmt.Sprintf("%s: %s", user, reply)) } }) backoffConnect(conn, o) <-stop }