update timeout
This commit is contained in:
parent
f6cf8b46ec
commit
e45fd544b5
3 changed files with 122 additions and 57 deletions
148
bot.go
148
bot.go
|
|
@ -1,13 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"log"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
"bufio"
|
||||
"bytes"
|
||||
"strings"
|
||||
"database/sql"
|
||||
configo "github.com/distributedio/configo"
|
||||
|
|
@ -39,61 +37,116 @@ func scanline_privmsg (msg string) irc_msg {
|
|||
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)
|
||||
func bot_connect(bot *bot) {
|
||||
conn, err := net.Dial("tcp", (*bot).Conf.Server)
|
||||
if err != nil {
|
||||
LOG_ERR.Printf("opening database failed")
|
||||
return nil
|
||||
LOG_ERR.Printf("Error while dialing server.")
|
||||
(*bot).conn = nil
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
(*bot).conn = conn
|
||||
fmt.Fprintf((*bot).conn, "USER %s 1 1 1:%s\r\n", (*bot).Conf.Name, (*bot).Conf.Name)
|
||||
}
|
||||
|
||||
func bot_register(bot *bot) {
|
||||
/* connect to irc */
|
||||
fmt.Fprintf((*bot).conn, "NICK %s\r\n", (*bot).Conf.Name)
|
||||
for _, channel := range((*bot).Conf.Channels) {
|
||||
fmt.Fprintf((*bot).conn, "JOIN %s\r\n", channel)
|
||||
}
|
||||
}
|
||||
|
||||
func bot_listen(bot *bot) <-chan string {
|
||||
c := make(chan string)
|
||||
bot_register(bot)
|
||||
|
||||
go func(timeoutNormal time.Duration, timeoutVersion time.Duration) {
|
||||
bufReader := bufio.NewReader((*bot).conn)
|
||||
|
||||
timeoutDuration := timeoutNormal
|
||||
waitversion := false
|
||||
|
||||
defer func() {
|
||||
fmt.Println("Closing connection...")
|
||||
close(c)
|
||||
(*bot).conn.Close()
|
||||
}()
|
||||
|
||||
for {
|
||||
line, err := reader.ReadString('\n')
|
||||
(*bot).conn.SetReadDeadline(time.Now().Add(timeoutDuration))
|
||||
|
||||
line, err := bufReader.ReadString('\n')
|
||||
if err != nil {
|
||||
LOG_ERR.Printf("Error while reading bytes from connection.\n")
|
||||
close(c)
|
||||
return
|
||||
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
|
||||
fmt.Printf("Timeout!\n")
|
||||
if waitversion {
|
||||
fmt.Println("VERSION command did not return.")
|
||||
return
|
||||
}
|
||||
|
||||
n, err := (*bot).conn.Write([]byte("VERSION\n"))
|
||||
fmt.Printf("n: %d, err: %s\n", n, err)
|
||||
if err != nil {
|
||||
fmt.Println("Writing to channel failed.")
|
||||
return
|
||||
}
|
||||
// wait for return value of VERSION
|
||||
waitversion = true
|
||||
timeoutDuration = timeoutVersion
|
||||
} else {
|
||||
fmt.Println("Some more serious error occured while reading.")
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
c <- []byte(line)
|
||||
// not waiting for version answer right now
|
||||
timeoutDuration = timeoutNormal
|
||||
waitversion = false
|
||||
c <- 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 strings.Contains(line, "PING") {
|
||||
(*bot).conn.Write([]byte(strings.Replace(line, "PING", "PONG", 1)))
|
||||
} else if strings.Contains(line, "You have not registered") {
|
||||
// set nickname
|
||||
time.Sleep(10 * time.Second)
|
||||
bot_register(bot)
|
||||
} else if strings.Contains(line, "PRIVMSG") {
|
||||
msg := scanline_privmsg(line)
|
||||
parsemsg(&msg, (*bot))
|
||||
if msg.retmsg != "" {
|
||||
sendmsg(foo.conn, msg.channel, msg.retmsg)
|
||||
sendmsg((*bot).conn, msg.channel, msg.retmsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
} ()
|
||||
} (10 * time.Second, 5 * time.Second)
|
||||
// } (4 * time.Minute, 1 * time.Minute)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func connect(boddle *bot) <-chan []byte {
|
||||
conn, err := net.Dial("tcp", (*boddle).Conf.Server)
|
||||
func bot_run(bot *bot) {
|
||||
LOG_INFO.Println("Bot ready to run.")
|
||||
|
||||
LOG_INFO.Println("Opening database.")
|
||||
db, err = sql.Open("sqlite3", (*bot).Conf.Database)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error while dialing server.")
|
||||
LOG_ERR.Printf("Error while connecting to server.")
|
||||
return nil
|
||||
LOG_ERR.Println("Opening database failed.")
|
||||
return
|
||||
}
|
||||
|
||||
(*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)
|
||||
for {
|
||||
bot_connect(bot)
|
||||
if bot.conn == nil {
|
||||
LOG_INFO.Printf("Bot is nil. Try to connect again.")
|
||||
time.Sleep(10 * time.Second)
|
||||
continue
|
||||
}
|
||||
for val := range bot_listen(bot) {
|
||||
LOG_INFO.Printf("%s", val)
|
||||
}
|
||||
}
|
||||
|
||||
return listenToIRC((*boddle))
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
@ -104,24 +157,5 @@ func main() {
|
|||
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...")
|
||||
}
|
||||
bot_run(&boddle)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue