package main import ( "os" "log" "fmt" "net" "time" "bufio" "bytes" "strings" "database/sql" configo "github.com/distributedio/configo" _ "github.com/mattn/go-sqlite3" ) func sendmsg (conn net.Conn, channel string, msg string) { mesg := fmt.Sprintf("PRIVMSG %s :%s\r\n", channel, msg) fmt.Printf(mesg) conn.Write([]byte(mesg)) } func scanline_privmsg (msg string) irc_msg { var irc irc_msg msg = msg[1:] t := strings.Split(msg, "!") irc.author, irc.channel = t[0], t[1] t = strings.Split(irc.channel, "PRIVMSG ") irc.channel = t[1] t = strings.Split(irc.channel, " :") irc.channel, irc.msg = t[0], t[1] t = strings.Split(irc.msg, "\r\n") irc.msg = strings.TrimSpace(t[0]) if ! strings.HasPrefix(irc.channel, "#") { irc.channel = irc.author } irc.retmsg = "" return irc } func listenToIRC (foo bot) <-chan []byte { c := make(chan []byte) reader := bufio.NewReader(foo.conn) db, err = sql.Open("sqlite3", foo.Conf.Database) if err != nil { LOG_ERR.Printf("opening database failed") return nil } go func() { for { line, err := reader.ReadString('\n') if err != nil { LOG_ERR.Printf("Error while reading bytes from connection.\n") close(c) return } c <- []byte(line) if bytes.Contains([]byte(line), []byte("PING :")) { v := []byte(bytes.Replace([]byte(line), []byte("PING"), []byte("PONG"), 1)) fmt.Printf(string(v[:])) foo.conn.Write(v) } else if bytes.Contains([]byte(line), []byte("PRIVMSG")) { msg := scanline_privmsg(string(line)) parsemsg(&msg, foo) if msg.retmsg != "" { sendmsg(foo.conn, msg.channel, msg.retmsg) } } } } () return c } func connect(boddle *bot) <-chan []byte { conn, err := net.Dial("tcp", (*boddle).Conf.Server) if err != nil { fmt.Fprintf(os.Stderr, "Error while dialing server.") LOG_ERR.Printf("Error while connecting to server.") return nil } (*boddle).conn = conn /* connect to irc */ fmt.Fprintf((*boddle).conn, "NICK %s\r\n", (*boddle).Conf.Name) fmt.Fprintf((*boddle).conn, "USER %s 1 1 1:%s\r\n", (*boddle).Conf.Name, (*boddle).Conf.Name) for _, channel := range((*boddle).Conf.Channels) { fmt.Fprintf((*boddle).conn, "JOIN %s\r\n", channel) } return listenToIRC((*boddle)) } func main() { LOG_init() var boddle bot if err := configo.Load("./boddle.toml", &boddle.Conf); err != nil { log.Fatal(err) } LOG_INFO.Println("bot started!") var botchan <-chan []byte for { botchan = connect(&boddle) for { select { case res := <-botchan: fmt.Println(string(res)) case <-time.After(10 * time.Minute): fmt.Println("timeout - 10 Minutes no ping received.") // close channel, close connection boddle.conn.Close() goto Reconnect } } Reconnect: fmt.Println("trying to reconnect...") } }