It's hierryfying and does not build… yet
parent
2c0955a168
commit
1ad852888a
|
@ -0,0 +1,22 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
. "github.com/arachnist/gorepost/config"
|
||||
"github.com/arachnist/gorepost/irc"
|
||||
)
|
||||
|
||||
func channeljoin(c Context, output chan irc.Message, msg irc.Message) {
|
||||
for _, channel := range C.Lookup(c, "Channels").([]string) {
|
||||
log.Println(c.Network, "joining channel", channel)
|
||||
output <- irc.Message{
|
||||
Command: "JOIN",
|
||||
Params: []string{channel},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
AddCallback("001", "channel join", channeljoin)
|
||||
}
|
|
@ -4,15 +4,16 @@ import (
|
|||
"log"
|
||||
"strings"
|
||||
|
||||
. "github.com/arachnist/gorepost/config"
|
||||
"github.com/arachnist/gorepost/irc"
|
||||
)
|
||||
|
||||
var Callbacks = make(map[string]map[string]func(chan irc.Message, irc.Message))
|
||||
var Callbacks = make(map[string]map[string]func(Context, chan irc.Message, irc.Message))
|
||||
|
||||
func AddCallback(command, name string, callback func(chan irc.Message, irc.Message)) {
|
||||
func AddCallback(command, name string, callback func(Context, chan irc.Message, irc.Message)) {
|
||||
log.Println("adding callback", command, name)
|
||||
if _, ok := Callbacks[command]; !ok {
|
||||
Callbacks[command] = make(map[string]func(chan irc.Message, irc.Message))
|
||||
Callbacks[command] = make(map[string]func(Context, chan irc.Message, irc.Message))
|
||||
}
|
||||
Callbacks[strings.ToUpper(command)][strings.ToUpper(name)] = callback
|
||||
}
|
||||
|
@ -21,14 +22,14 @@ func RemoveCallback(command, name string) {
|
|||
delete(Callbacks[command], name)
|
||||
}
|
||||
|
||||
func Dispatcher(quit chan struct{}, output chan irc.Message, input chan irc.Message) {
|
||||
func Dispatcher(quit chan struct{}, context Context, output chan irc.Message, input chan irc.Message) {
|
||||
log.Println("spawned Dispatcher")
|
||||
for {
|
||||
select {
|
||||
case msg := <-input:
|
||||
if Callbacks[msg.Command] != nil {
|
||||
for _, f := range Callbacks[msg.Command] {
|
||||
go f(output, msg)
|
||||
go f(context, output, msg)
|
||||
}
|
||||
}
|
||||
case <-quit:
|
||||
|
|
|
@ -3,10 +3,11 @@ package bot
|
|||
import (
|
||||
"strings"
|
||||
|
||||
. "github.com/arachnist/gorepost/config"
|
||||
"github.com/arachnist/gorepost/irc"
|
||||
)
|
||||
|
||||
func ping(output chan irc.Message, msg irc.Message) {
|
||||
func ping(c Context, output chan irc.Message, msg irc.Message) {
|
||||
if strings.Split(msg.Trailing, " ")[0] != ":ping" {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
. "github.com/arachnist/gorepost/config"
|
||||
"github.com/arachnist/gorepost/irc"
|
||||
)
|
||||
|
||||
func pingpong(output chan irc.Message, msg irc.Message) {
|
||||
func pingpong(c Context, output chan irc.Message, msg irc.Message) {
|
||||
output <- irc.Message{
|
||||
Command: "PONG",
|
||||
Trailing: msg.Trailing,
|
||||
|
|
|
@ -3,6 +3,9 @@ package config
|
|||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -17,20 +20,51 @@ type Config struct {
|
|||
Plugins []string
|
||||
Ignore []string
|
||||
Logpath string
|
||||
ConfigDir string
|
||||
}
|
||||
|
||||
func ReadConfig(path string) (Config, error) {
|
||||
var config Config
|
||||
type Context struct {
|
||||
Network string
|
||||
Target string
|
||||
Source string
|
||||
}
|
||||
|
||||
var C Config
|
||||
|
||||
func lookupvar(key, path string) interface{} {
|
||||
var f interface{}
|
||||
_, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return config, err
|
||||
return ""
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &config)
|
||||
if err != nil {
|
||||
return config, err
|
||||
}
|
||||
err = json.Unmarshal(data, &f)
|
||||
|
||||
return config, nil
|
||||
return f.(map[string]interface{})[key]
|
||||
}
|
||||
|
||||
func (c *Config) Lookup(context Context, key string) interface{} {
|
||||
var fpath string
|
||||
var value interface{}
|
||||
if context.Network != "" {
|
||||
if context.Source != "" {
|
||||
if context.Target != "" {
|
||||
fpath = path.Join(c.ConfigDir, context.Network, context.Source, context.Target+".json")
|
||||
|
||||
log.Println("Context:", context, "Looking up", key, "in", fpath)
|
||||
value = lookupvar(key, fpath)
|
||||
if value != nil {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return lookupvar(key, path.Join(c.ConfigDir, context.Network, context.Source+".json"))
|
||||
}
|
||||
return lookupvar(key, path.Join(c.ConfigDir, context.Network+".json"))
|
||||
}
|
||||
return lookupvar(key, path.Join(c.ConfigDir, "common.json"))
|
||||
}
|
||||
|
|
39
gorepost.go
39
gorepost.go
|
@ -5,41 +5,44 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/arachnist/gorepost/bot"
|
||||
"github.com/arachnist/gorepost/config"
|
||||
. "github.com/arachnist/gorepost/config"
|
||||
"github.com/arachnist/gorepost/irc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var exit chan struct{}
|
||||
var context Context
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatalln("Usage:", os.Args[0], "<config-file.json>")
|
||||
log.Fatalln("Usage:", os.Args[0], "<configuration directory>")
|
||||
}
|
||||
config, err := config.ReadConfig(os.Args[1])
|
||||
|
||||
d, err := os.Stat(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatalln("Error reading configuration from", os.Args[1], "error:", err.Error())
|
||||
}
|
||||
if !d.IsDir() {
|
||||
log.Fatalln("Not a directory:", os.Args[1])
|
||||
}
|
||||
|
||||
logfile, err := os.OpenFile(config.Logpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
C.ConfigDir = os.Args[1]
|
||||
|
||||
logfile, err := os.OpenFile(C.Lookup(context, "Logpath").(string), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatalln("Error opening", config.Logpath, "for writing, error:", err.Error())
|
||||
log.Fatalln("Error opening", C.Lookup(context, "Logpath").(string), "for writing, error:", err.Error())
|
||||
}
|
||||
log.SetOutput(logfile)
|
||||
|
||||
connections := make([]irc.Connection, len(config.Networks))
|
||||
for i, _ := range connections {
|
||||
network := config.Networks[i]
|
||||
connections[i].Setup(bot.Dispatcher, network, config.Servers[network], config.Nick, config.User, config.RealName)
|
||||
networks := C.Lookup(context, "Networks").([]string)
|
||||
connections := make([]irc.Connection, len(networks))
|
||||
for i, conn := range connections {
|
||||
context.Network = networks[i]
|
||||
|
||||
bot.AddCallback("001", "channel join", func(output chan irc.Message, msg irc.Message) {
|
||||
for _, channel := range config.Channels[network] {
|
||||
log.Println(network, "joining channel", channel)
|
||||
output <- irc.Message{
|
||||
Command: "JOIN",
|
||||
Params: []string{channel},
|
||||
}
|
||||
}
|
||||
})
|
||||
connections[i].Setup(bot.Dispatcher, networks[i],
|
||||
C.Lookup(context, "Servers").([]string),
|
||||
C.Lookup(context, "Nick").(string),
|
||||
C.Lookup(context, "User").(string),
|
||||
C.Lookup(context, "RealName").(string))
|
||||
}
|
||||
<-exit
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@ import (
|
|||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
. "github.com/arachnist/gorepost/config"
|
||||
)
|
||||
|
||||
const delim byte = '\n'
|
||||
|
@ -21,7 +23,7 @@ type Connection struct {
|
|||
Output chan Message
|
||||
reader *bufio.Reader
|
||||
writer *bufio.Writer
|
||||
dispatcher func(chan struct{}, chan Message, chan Message)
|
||||
dispatcher func(chan struct{}, Context, chan Message, chan Message)
|
||||
conn net.Conn
|
||||
reconnect chan struct{}
|
||||
Quit chan struct{}
|
||||
|
@ -137,7 +139,7 @@ func (c *Connection) Keeper(servers []string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Connection) Setup(dispatcher func(chan struct{}, chan Message, chan Message), network string, servers []string, nick string, user string, realname string) {
|
||||
func (c *Connection) Setup(dispatcher func(chan struct{}, Context, chan Message, chan Message), network string, servers []string, nick string, user string, realname string) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
c.reconnect = make(chan struct{}, 1)
|
||||
|
|
Loading…
Reference in New Issue