kill bot after 10 minutes idling
should prevent 'ping loss' due to weird network setup
This commit is contained in:
parent
9e09e78ab8
commit
f6cf8b46ec
8 changed files with 187 additions and 230 deletions
118
bot.go
118
bot.go
|
|
@ -6,22 +6,94 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
"bufio"
|
||||
"bytes"
|
||||
"strings"
|
||||
"database/sql"
|
||||
configo "github.com/distributedio/configo"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var dead = true
|
||||
|
||||
type Config struct {
|
||||
Name string `cfg:"name; boddle; printableascii; IRC nick of bot"`
|
||||
Channels []string `cfg:"channels; required; printableascii; Channel list to join to"`
|
||||
Server string `cfg:"server; required; netaddr; Server name to connect to"`
|
||||
Database string `cfg:"database; ./boddle.db; path; Path to database"`
|
||||
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))
|
||||
}
|
||||
|
||||
type bot struct {
|
||||
Conf Config
|
||||
conn net.Conn
|
||||
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() {
|
||||
|
|
@ -35,23 +107,21 @@ func main() {
|
|||
LOG_INFO.Println("bot started!")
|
||||
|
||||
var botchan <-chan []byte
|
||||
var line []byte
|
||||
dead = true
|
||||
|
||||
for {
|
||||
if dead {
|
||||
LOG_WARN.Println("died.")
|
||||
for {
|
||||
botchan = connect(boddle)
|
||||
if botchan != nil {
|
||||
break
|
||||
}
|
||||
time.Sleep(10 * time.Second)
|
||||
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
|
||||
}
|
||||
LOG_INFO.Println("reconnected.")
|
||||
dead = false
|
||||
}
|
||||
line = <-botchan
|
||||
fmt.Fprintf(os.Stderr, string(line))
|
||||
Reconnect:
|
||||
fmt.Println("trying to reconnect...")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue