flokati/main.go

165 lines
3.7 KiB
Go
Raw Normal View History

2016-01-11 06:18:52 +00:00
// vi:ts=4:sts=4:sw=4:noet:tw=72
2016-01-10 17:45:21 +00:00
//
// flokatirc
//
2016-01-11 06:18:52 +00:00
// Copyright (c) 2015,2016 Andreas Neue <an@dnix.de>
2016-01-10 17:45:21 +00:00
package main
import (
"flag"
2016-01-19 18:51:07 +00:00
"fmt"
2016-01-10 17:45:21 +00:00
"log"
"strings"
"time"
"github.com/nickvanw/ircx"
"github.com/sorcix/irc"
2016-01-19 18:51:07 +00:00
"flokatirc/version"
2016-01-10 17:45:21 +00:00
modfortune "flokatirc/modules/fortune"
2016-01-22 21:32:49 +00:00
modrss "flokatirc/modules/rss"
2016-01-10 17:45:21 +00:00
modsaytime "flokatirc/modules/saytime"
modsc "flokatirc/modules/sc"
modstoll "flokatirc/modules/stoll"
)
var (
name = flag.String("name", "Flokati", "Nickname to use")
server = flag.String("server", "chat.freenode.org:6667", "Host:Port to connect to")
channels = flag.String("chan", "#test", "Channels to join")
nsname = flag.String("nsname", "NickServ", "NickServ name")
nspass = flag.String("nspass", "", "NickServ password")
2016-01-22 21:32:49 +00:00
modules = flag.String("modules", "rss,starcitizen,fortune,saytime", "Module list, comma separated")
2016-01-10 17:45:21 +00:00
)
func init() {
flag.Parse()
}
2016-01-19 18:51:07 +00:00
var (
sayCh chan string
)
2016-01-10 17:45:21 +00:00
func main() {
2016-01-19 18:51:07 +00:00
sayCh = make(chan string, 1024)
2016-01-10 17:45:21 +00:00
//bot := ircx.Classic(*server, *name)
2016-01-18 22:28:13 +00:00
cfg := ircx.Config{User: *name, MaxRetries: 1000}
2016-01-10 17:45:21 +00:00
bot := ircx.New(*server, *name, cfg)
2016-01-20 06:34:40 +00:00
log.Printf("%s started", SoftwareInfo())
2016-01-10 17:45:21 +00:00
log.Println("Logging in")
if err := bot.Connect(); err != nil {
log.Panicln("Unable to dial IRC Server:", err)
}
//mods := strings.Split(*modules, ",")
//TODO: implement more robust list parsing
//XXX: this sucks
2016-01-22 21:32:49 +00:00
if strings.Contains(*modules, "rss") {
go modrss.Init(sayCh, "newsfeeds.conf")
2016-01-10 17:45:21 +00:00
}
if strings.Contains(*modules, "starcitizen") {
go modsc.Init(sayCh)
}
if strings.Contains(*modules, "fortune") {
go modfortune.Init(sayCh)
}
if strings.Contains(*modules, "stoll") {
go modstoll.Init(sayCh)
}
if strings.Contains(*modules, "saytime") {
go modsaytime.Init(sayCh)
}
go func() {
for {
var targets string
line := strings.Split(<-sayCh, "\n")
if len(line) < 2 {
continue
}
if line[0] != "*" {
targets = line[0]
} else {
targets = *channels
}
for _, tar := range strings.Split(targets, ",") {
bot.Sender.Send(&irc.Message{
Command: irc.PRIVMSG,
Params: []string{tar},
Trailing: line[1],
})
time.Sleep(1 * time.Second)
}
}
}()
RegisterHandlers(bot)
bot.HandleLoop()
log.Println("Exiting")
}
func RegisterHandlers(bot *ircx.Bot) {
log.Println("Registering handlers")
2016-01-18 22:28:13 +00:00
bot.HandleFunc(irc.RPL_WELCOME, ConnectHandler)
2016-01-10 17:45:21 +00:00
bot.HandleFunc(irc.PING, PingHandler)
bot.HandleFunc(irc.PRIVMSG, PrivmsgHandler)
}
2016-01-18 22:28:13 +00:00
func ConnectHandler(s ircx.Sender, m *irc.Message) {
2016-01-10 17:45:21 +00:00
if *nspass != "" {
log.Printf("Authenticating with NickServ: %v, %v", *name, *nspass)
s.Send(&irc.Message{
Command: irc.PRIVMSG,
Params: []string{*nsname},
Trailing: "IDENTIFY " + *name + " " + *nspass,
})
}
log.Printf("Joining channels: %v", *channels)
for _, ch := range strings.Split(*channels, ",") {
s.Send(&irc.Message{
Command: irc.JOIN,
Params: []string{ch},
})
}
2016-01-20 06:34:40 +00:00
time.Sleep(5 * time.Second)
sayCh <- fmt.Sprintf("%s\n*** %s", "*", SoftwareInfo())
2016-01-10 17:45:21 +00:00
}
func PingHandler(s ircx.Sender, m *irc.Message) {
log.Println("PingPong")
s.Send(&irc.Message{
Command: irc.PONG,
Params: m.Params,
Trailing: m.Trailing,
})
}
func PrivmsgHandler(s ircx.Sender, m *irc.Message) {
//TODO: implement message handler table
2016-01-20 06:34:40 +00:00
HandleMessage(m)
2016-01-10 17:45:21 +00:00
modsc.HandleMessage(m)
modfortune.HandleMessage(m)
modstoll.HandleMessage(m)
modsaytime.HandleMessage(m)
}
2016-01-20 06:34:40 +00:00
func HandleMessage(m *irc.Message) {
tok := strings.Split(m.Trailing, " ")
if len(tok) < 1 {
return
}
switch tok[0] {
case "!version":
sayCh <- fmt.Sprintf("%s\n*** %s", "*", SoftwareInfo())
default:
}
}
func SoftwareInfo() string {
return fmt.Sprintf("flokatirc %s-%s", version.FlokatiVersion, version.FlokatiBuild)
}