Put the protocol independent stuff into a library

This commit is contained in:
an 2017-06-15 09:01:25 +02:00
parent a84bad5d7b
commit fa6081e5ec
8 changed files with 2 additions and 266 deletions

View File

@ -1,20 +0,0 @@
FROM ubuntu:15.10
MAINTAINER Andreas Neue <an@dnix.de>
RUN apt-get -y --force-yes update
RUN apt-get -y --force-yes upgrade
RUN apt-get install -y ca-certificates
RUN apt-get install -y fortune vim
RUN apt-get clean
RUN mkdir /flokatirc
COPY flokatirc /flokatirc/flokatirc
COPY newsfeeds.conf /flokatirc/newsfeeds.conf
COPY fortunes /flokatirc/fortunes
ENV PATH $PATH:/flokatirc
WORKDIR /flokatirc
CMD [ "/flokatirc/flokatirc", "-server=irc.dnix.de:6667", "-name=Flokati", "-chan=#test", "-nsname=N", "-nspass=t0ps3cr37" ]

View File

@ -1,43 +0,0 @@
GOPATH := ${PWD}:${GOPATH}
export GOPATH
default: build
build: genversion genbuilddate test
go build -v flokatirc
build-win: test genversion
GOOS=windows GOARCH=amd64 go build -v -o flokatirc.exe flokatirc
docker: build
./dockerbuild.sh
genversion:
./genversion.sh
genbuilddate:
./genbuilddate.sh
fmt:
go fmt ./...
fix:
go fix ./...
imports:
find . -type f -name "*.go" -exec goimports -w {} \;
doc:
godoc -http=:6060 -index
test:
go test ./...
commit:
git commit -a -F changes.log
rm changes.log
touch changes.log
./genversion.sh
push:
git push

View File

@ -1,6 +1,6 @@
## flokatirc ## flokatilib
*an incredibly friendly, silver-tongued, and elegantly proportioned IRC community bot* *Modules and utilities for the incredibly friendly, silver-tongued, and elegantly proportioned community bot*
#### LICENSE #### LICENSE

View File

@ -1,6 +0,0 @@
#!/bin/sh
VERSION=`git rev-parse --abbrev-ref HEAD`
BUILD=`git rev-list HEAD --count`
echo $VERSION-$BUILD
docker build -t dr.dnix.de/flokatirc:$VERSION-$BUILD -f Dockerfile .
docker push dr.dnix.de/flokatirc:$VERSION-$BUILD

View File

@ -1,13 +0,0 @@
#!/bin/sh
BUILDDATEDIR="version"
BUILDDATEGO="version/builddate.go"
if [ ! -d "$BUILDDATEDIR" ]; then
mkdir $BUILDDATEDIR
fi
echo "package version" >$BUILDDATEGO
echo "" >>$BUILDDATEGO
echo "const (" >>$BUILDDATEGO
echo -n " FlokatiBuilddate = \"" >>$BUILDDATEGO
echo -n `date` >>$BUILDDATEGO
echo "\"" >>$BUILDDATEGO
echo ")" >>$BUILDDATEGO

View File

@ -1,16 +0,0 @@
#!/bin/sh
VERSIONDIR="version"
VERSIONGO="version/version.go"
if [ ! -d "$VERSIONDIR" ]; then
mkdir $VERSIONDIR
fi
echo "package version" >$VERSIONGO
echo "" >>$VERSIONGO
echo "const (" >>$VERSIONGO
echo -n " FlokatiVersion = \"" >>$VERSIONGO
echo -n `git rev-parse --abbrev-ref HEAD` >>$VERSIONGO
echo "\"" >>$VERSIONGO
echo -n " FlokatiBuild = \"" >>$VERSIONGO
echo -n `git rev-list HEAD --count` >>$VERSIONGO
echo "\"" >>$VERSIONGO
echo ")" >>$VERSIONGO

165
main.go
View File

@ -1,165 +0,0 @@
// vi:ts=4:sts=4:sw=4:noet:tw=72
//
// flokatirc
//
// Copyright (c) 2015,2016 Andreas Neue <an@dnix.de>
package main
import (
"flag"
"fmt"
"runtime"
"strings"
"time"
"code.dnix.de/an/xlog"
"github.com/nickvanw/ircx"
"github.com/sorcix/irc"
"github.com/sorcix/irc/ctcp"
"flokatirc/modules"
"flokatirc/version"
)
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")
mods = flag.String("mods", "", "Modules to load")
params = flag.String("params", "", "Module params")
autocmd = flag.String("autocmd", "", "Autosend IRC command")
)
func init() {
flag.Parse()
}
var (
sayCh chan string
)
func main() {
sayCh = make(chan string, 1024)
xlog.Init(xlog.DEBUG)
//bot := ircx.Classic(*server, *name)
cfg := ircx.Config{User: *name, MaxRetries: 1000}
bot := ircx.New(*server, *name, cfg)
xlog.Info("%s started", SoftwareInfo())
xlog.Info("Logging in")
if err := bot.Connect(); err != nil {
xlog.Fatal("Unable to dial IRC Server: %v", err)
}
//mods := strings.Split(*modules, ",")
//TODO: implement more robust list parsing
modules.Init(sayCh, *mods, *params)
modules.ModParams["_nick"] = *name
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)
}
}
}()
go Ping(bot)
if *autocmd != "" {
println(*autocmd)
bot.Sender.Send(&irc.Message{Command: *autocmd})
}
RegisterHandlers(bot)
bot.HandleLoop()
xlog.Info("Exiting")
}
func RegisterHandlers(bot *ircx.Bot) {
xlog.Info("Registering handlers")
bot.HandleFunc(irc.RPL_WELCOME, ConnectHandler)
bot.HandleFunc(irc.PING, PingHandler)
bot.HandleFunc(irc.PRIVMSG, PrivmsgHandler)
}
func Ping(bot *ircx.Bot) {
for {
time.Sleep(1 * time.Minute)
bot.Sender.Send(&irc.Message{Command: irc.PING})
}
}
func ConnectHandler(s ircx.Sender, m *irc.Message) {
if *nspass != "" {
xlog.Info("Authenticating with NickServ: %v, %v", *name, *nspass)
s.Send(&irc.Message{
Command: irc.PRIVMSG,
Params: []string{*nsname},
Trailing: "IDENTIFY " + *name + " " + *nspass,
})
}
xlog.Info("Joining channels: %v", *channels)
for _, ch := range strings.Split(*channels, ",") {
s.Send(&irc.Message{
Command: irc.JOIN,
Params: []string{ch},
})
}
time.Sleep(2 * time.Second)
msg := ctcp.Action(fmt.Sprintf("running on %s", SoftwareInfo()))
sayCh <- fmt.Sprintf("%s\n%s", "*", msg)
}
func PingHandler(s ircx.Sender, m *irc.Message) {
xlog.Info("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
HandleMessage(m)
modules.HandleMessage(m)
}
func HandleMessage(m *irc.Message) {
tok := strings.Split(m.Trailing, " ")
if len(tok) < 1 {
return
}
switch tok[0] {
case "!version":
msg := ctcp.Action(fmt.Sprintf("running on %s", SoftwareInfo()))
sayCh <- fmt.Sprintf("%s\n%s", "*", msg)
//sayCh <- fmt.Sprintf("%s\n%s", "*", SoftwareInfo())
default:
}
}
func SoftwareInfo() string {
return fmt.Sprintf("flokatirc %s-%s (built %s [%s])", version.FlokatiVersion,
version.FlokatiBuild, version.FlokatiBuilddate, runtime.Version())
}

View File

@ -1 +0,0 @@
https://example.com/rss