2013-12-10 23:37:07 +00:00
|
|
|
// © 2013 the AlePale Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
2012-08-17 20:38:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-05-09 22:30:15 +00:00
|
|
|
"flag"
|
2013-06-02 01:59:55 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
2014-04-20 16:54:01 +00:00
|
|
|
|
|
|
|
"code.google.com/p/velour/irc"
|
|
|
|
"github.com/chrissexton/alepale/bot"
|
|
|
|
"github.com/chrissexton/alepale/config"
|
|
|
|
"github.com/chrissexton/alepale/plugins"
|
2012-08-17 20:38:15 +00:00
|
|
|
)
|
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
const (
|
|
|
|
// DefaultPort is the port used to connect to
|
|
|
|
// the server if one is not specified.
|
|
|
|
defaultPort = "6667"
|
|
|
|
|
|
|
|
// InitialTimeout is the initial amount of time
|
|
|
|
// to delay before reconnecting. Each failed
|
|
|
|
// reconnection doubles the timout until
|
|
|
|
// a connection is made successfully.
|
|
|
|
initialTimeout = 2 * time.Second
|
2012-08-17 20:38:15 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
// PingTime is the amount of inactive time
|
|
|
|
// to wait before sending a ping to the server.
|
|
|
|
pingTime = 120 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Client *irc.Client
|
|
|
|
Bot *bot.Bot
|
|
|
|
Config *config.Config
|
|
|
|
)
|
2012-08-17 20:38:15 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
var cfile = flag.String("config", "config.json",
|
|
|
|
"Config file to load. (Defaults to config.json)")
|
2012-08-17 20:38:15 +00:00
|
|
|
flag.Parse() // parses the logging flags.
|
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
Config = config.Readconfig(Version, *cfile)
|
2012-08-17 20:38:15 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
Client, err = irc.DialServer(Config.Server,
|
|
|
|
Config.Nick,
|
|
|
|
Config.FullName,
|
|
|
|
Config.Pass)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
Bot = bot.NewBot(Config, Client)
|
|
|
|
// Bot.AddHandler(plugins.NewTestPlugin(Bot))
|
|
|
|
Bot.AddHandler("admin", plugins.NewAdminPlugin(Bot))
|
|
|
|
Bot.AddHandler("first", plugins.NewFirstPlugin(Bot))
|
|
|
|
Bot.AddHandler("downtime", plugins.NewDowntimePlugin(Bot))
|
|
|
|
Bot.AddHandler("talker", plugins.NewTalkerPlugin(Bot))
|
|
|
|
Bot.AddHandler("dice", plugins.NewDicePlugin(Bot))
|
|
|
|
Bot.AddHandler("beers", plugins.NewBeersPlugin(Bot))
|
|
|
|
Bot.AddHandler("counter", plugins.NewCounterPlugin(Bot))
|
|
|
|
Bot.AddHandler("remember", plugins.NewRememberPlugin(Bot))
|
|
|
|
Bot.AddHandler("skeleton", plugins.NewSkeletonPlugin(Bot))
|
|
|
|
Bot.AddHandler("your", plugins.NewYourPlugin(Bot))
|
|
|
|
// catches anything left, will always return true
|
|
|
|
Bot.AddHandler("factoid", plugins.NewFactoidPlugin(Bot))
|
|
|
|
|
|
|
|
handleConnection()
|
2012-08-17 20:38:15 +00:00
|
|
|
|
|
|
|
// And a signal on disconnect
|
|
|
|
quit := make(chan bool)
|
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
// Wait for disconnect
|
|
|
|
<-quit
|
|
|
|
}
|
2012-08-17 20:38:15 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
func handleConnection() {
|
|
|
|
t := time.NewTimer(pingTime)
|
2013-01-23 00:22:32 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
defer func() {
|
|
|
|
t.Stop()
|
|
|
|
close(Client.Out)
|
|
|
|
for err := range Client.Errors {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-04-20 16:54:01 +00:00
|
|
|
ratePerSec := Config.RatePerSec
|
|
|
|
throttle := time.Tick(time.Second * time.Duration(1.0/ratePerSec))
|
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg, ok := <-Client.In:
|
|
|
|
if !ok { // disconnect
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Stop()
|
|
|
|
t = time.NewTimer(pingTime)
|
|
|
|
handleMsg(msg)
|
|
|
|
|
|
|
|
case <-t.C:
|
2014-04-20 16:54:01 +00:00
|
|
|
<-throttle // rate limit our Service.Method RPCs
|
2013-06-02 01:59:55 +00:00
|
|
|
Client.Out <- irc.Msg{Cmd: irc.PING, Args: []string{Client.Server}}
|
|
|
|
t = time.NewTimer(pingTime)
|
|
|
|
|
|
|
|
case err, ok := <-Client.Errors:
|
|
|
|
if ok && err != io.EOF {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-23 00:22:32 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
// HandleMsg handles IRC messages from the server.
|
|
|
|
func handleMsg(msg irc.Msg) {
|
|
|
|
switch msg.Cmd {
|
|
|
|
case irc.ERROR:
|
|
|
|
log.Println(1, "Received error: "+msg.Raw)
|
2013-01-23 00:22:32 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
case irc.PING:
|
|
|
|
Client.Out <- irc.Msg{Cmd: irc.PONG}
|
2012-08-23 20:28:45 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
case irc.PONG:
|
|
|
|
// OK, ignore
|
2013-02-18 19:08:08 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
case irc.ERR_NOSUCHNICK:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
2012-08-25 04:46:13 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
case irc.ERR_NOSUCHCHANNEL:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
2013-01-23 15:53:31 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
case irc.RPL_MOTD:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
2012-08-17 20:38:15 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
case irc.RPL_NAMREPLY:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
2012-08-17 20:38:15 +00:00
|
|
|
|
2013-06-02 01:59:55 +00:00
|
|
|
case irc.RPL_TOPIC:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.KICK:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.TOPIC:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.MODE:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.JOIN:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.PART:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.QUIT:
|
|
|
|
os.Exit(1)
|
|
|
|
|
|
|
|
case irc.NOTICE:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.PRIVMSG:
|
|
|
|
Bot.MsgRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.NICK:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.RPL_WHOREPLY:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
case irc.RPL_ENDOFWHO:
|
|
|
|
Bot.EventRecieved(Client, msg)
|
|
|
|
|
|
|
|
default:
|
|
|
|
cmd := irc.CmdNames[msg.Cmd]
|
|
|
|
log.Println("(" + cmd + ") " + msg.Raw)
|
|
|
|
}
|
2012-08-17 20:38:15 +00:00
|
|
|
}
|