flokati/main.go

151 lines
3.3 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-02-28 11:34:38 +00:00
"runtime"
2016-01-10 17:45:21 +00:00
"strings"
"time"
2016-03-12 12:09:25 +00:00
"code.dnix.de/an/xlog"
2016-01-10 17:45:21 +00:00
"github.com/nickvanw/ircx"
"github.com/sorcix/irc"
2016-02-28 12:00:08 +00:00
"github.com/sorcix/irc/ctcp"
2016-01-10 17:45:21 +00:00
2016-02-15 23:17:42 +00:00
"flokatirc/modules"
2016-01-19 18:51:07 +00:00
"flokatirc/version"
2016-01-10 17:45:21 +00:00
)
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-02-15 23:17:42 +00:00
params = flag.String("params", "", "Module params")
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
2016-03-12 12:09:25 +00:00
xlog.Init(xlog.INFO)
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-03-12 12:09:25 +00:00
xlog.Info("%s started", SoftwareInfo())
xlog.Info("Logging in")
2016-01-10 17:45:21 +00:00
if err := bot.Connect(); err != nil {
2016-03-12 12:09:25 +00:00
xlog.Fatal("Unable to dial IRC Server: %v", err)
2016-01-10 17:45:21 +00:00
}
//mods := strings.Split(*modules, ",")
//TODO: implement more robust list parsing
modules.Init(sayCh, *params)
2016-01-10 17:45:21 +00:00
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()
2016-03-12 12:09:25 +00:00
xlog.Info("Exiting")
2016-01-10 17:45:21 +00:00
}
func RegisterHandlers(bot *ircx.Bot) {
2016-03-12 12:09:25 +00:00
xlog.Info("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 != "" {
2016-03-12 12:09:25 +00:00
xlog.Info("Authenticating with NickServ: %v, %v", *name, *nspass)
2016-01-10 17:45:21 +00:00
s.Send(&irc.Message{
Command: irc.PRIVMSG,
Params: []string{*nsname},
Trailing: "IDENTIFY " + *name + " " + *nspass,
})
}
2016-03-12 12:09:25 +00:00
xlog.Info("Joining channels: %v", *channels)
2016-01-10 17:45:21 +00:00
for _, ch := range strings.Split(*channels, ",") {
s.Send(&irc.Message{
Command: irc.JOIN,
Params: []string{ch},
})
}
2016-02-28 12:00:08 +00:00
time.Sleep(2 * time.Second)
2016-03-12 12:09:25 +00:00
msg := ctcp.Action(fmt.Sprintf("running on %s", SoftwareInfo()))
2016-02-28 12:00:08 +00:00
sayCh <- fmt.Sprintf("%s\n%s", "*", msg)
2016-01-10 17:45:21 +00:00
}
func PingHandler(s ircx.Sender, m *irc.Message) {
2016-03-12 12:09:25 +00:00
xlog.Info("PingPong")
2016-01-10 17:45:21 +00:00
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-02-15 23:17:42 +00:00
modules.HandleMessage(m)
2016-01-10 17:45:21 +00:00
}
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":
2016-03-12 12:09:25 +00:00
msg := ctcp.Action(fmt.Sprintf("running on %s", SoftwareInfo()))
2016-02-28 12:00:08 +00:00
sayCh <- fmt.Sprintf("%s\n%s", "*", msg)
//sayCh <- fmt.Sprintf("%s\n%s", "*", SoftwareInfo())
2016-01-20 06:34:40 +00:00
default:
}
}
func SoftwareInfo() string {
2016-02-28 11:38:32 +00:00
return fmt.Sprintf("flokatirc %s-%s (built %s [%s])", version.FlokatiVersion,
2016-02-28 11:34:38 +00:00
version.FlokatiBuild, version.FlokatiBuilddate, runtime.Version())
2016-01-20 06:34:40 +00:00
}