New mod management, new parameter -mods

This commit is contained in:
Andreas Neue 2016-03-19 23:49:41 +01:00
parent 1dad661ceb
commit 7bffa566c5
13 changed files with 57 additions and 43 deletions

View File

@ -29,6 +29,7 @@ var (
channels = flag.String("chan", "#test", "Channels to join") channels = flag.String("chan", "#test", "Channels to join")
nsname = flag.String("nsname", "NickServ", "NickServ name") nsname = flag.String("nsname", "NickServ", "NickServ name")
nspass = flag.String("nspass", "", "NickServ password") nspass = flag.String("nspass", "", "NickServ password")
mods = flag.String("mods", "", "Modules to load")
params = flag.String("params", "", "Module params") params = flag.String("params", "", "Module params")
) )
@ -57,7 +58,7 @@ func main() {
//mods := strings.Split(*modules, ",") //mods := strings.Split(*modules, ",")
//TODO: implement more robust list parsing //TODO: implement more robust list parsing
modules.Init(sayCh, *params) modules.Init(sayCh, *mods, *params)
go func() { go func() {
for { for {

View File

@ -5,16 +5,13 @@ package modules
import ( import (
"strings" "strings"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
var () var ()
func init() { func init() {
MsgHandlers["announcements"] = anncouncementsHandleMessage MsgFuncs["announcements"] = anncouncementsHandleMessage
xlog.Info("Announcements module initialized")
} }
func anncouncementsHandleMessage(m *irc.Message) { func anncouncementsHandleMessage(m *irc.Message) {

View File

@ -12,8 +12,6 @@ import (
"sync" "sync"
"time" "time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
"github.com/sorcix/irc/ctcp" "github.com/sorcix/irc/ctcp"
) )
@ -29,9 +27,8 @@ var (
) )
func init() { func init() {
MsgHandlers["coffee"] = coffeeHandleMessage MsgFuncs["coffee"] = coffeeHandleMessage
r.names = make(map[string]bool) r.names = make(map[string]bool)
xlog.Info("Coffee module initialized")
} }
func coffeeHandleMessage(m *irc.Message) { func coffeeHandleMessage(m *irc.Message) {
@ -108,4 +105,3 @@ func printAction(s string) {
msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf(s)) msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf(s))
SayCh <- fmt.Sprintf("%s\n%s", "*", msg) SayCh <- fmt.Sprintf("%s\n%s", "*", msg)
} }

View File

@ -8,14 +8,11 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
func init() { func init() {
MsgHandlers["fortune"] = fortuneHandleMessage MsgFuncs["fortune"] = fortuneHandleMessage
xlog.Info("Fortune module initialized")
} }
func fortuneHandleMessage(m *irc.Message) { func fortuneHandleMessage(m *irc.Message) {

View File

@ -7,14 +7,11 @@ import (
"strings" "strings"
"time" "time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
func init() { func init() {
MsgHandlers["fuzzytime"] = fuzzytimeHandleMessage MsgFuncs["fuzzytime"] = fuzzytimeHandleMessage
xlog.Info("Fuzzytime module initialized")
} }
func fuzzytimeHandleMessage(m *irc.Message) { func fuzzytimeHandleMessage(m *irc.Message) {

View File

@ -8,8 +8,6 @@ import (
"strings" "strings"
"time" "time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
@ -18,8 +16,8 @@ var (
) )
func init() { func init() {
MsgHandlers["gogs"] = gogsHandleMessage MsgFuncs["gogs"] = gogsHandleMessage
xlog.Info("Gogs module initialized") RunFuncs["gogs"] = gogsConfig
} }
func gogsConfig() { func gogsConfig() {

View File

@ -14,20 +14,43 @@ import (
var ( var (
SayCh chan string SayCh chan string
MsgHandlers = make(map[string]func(*irc.Message)) MsgFuncs = make(map[string]func(*irc.Message))
RunFuncs = make(map[string]func())
ModParams = make(map[string]string) ModParams = make(map[string]string)
) )
func Init(ch chan string, params string) { func Init(ch chan string, mods, params string) {
SayCh = ch SayCh = ch
for mod, _ := range MsgFuncs {
if !contains(strings.Split(mods, ","), mod) {
delete(MsgFuncs, mod)
}
}
for mod, _ := range RunFuncs {
if !contains(strings.Split(mods, ","), mod) {
delete(RunFuncs, mod)
}
}
for _, param := range strings.Split(params, "!") { for _, param := range strings.Split(params, "!") {
kv := strings.Split(param, ":") kv := strings.Split(param, ":")
ModParams[kv[0]] = kv[1] ModParams[kv[0]] = kv[1]
} }
for _, fn := range RunFuncs {
go fn()
}
} }
func HandleMessage(m *irc.Message) { func HandleMessage(m *irc.Message) {
for _, fn := range MsgHandlers { for _, fn := range MsgFuncs {
fn(m) fn(m)
} }
} }
func contains(sa []string, s string) bool {
for _, a := range sa {
if a == s {
return true
}
}
return false
}

View File

@ -24,9 +24,8 @@ import (
var hideOutput = true var hideOutput = true
func init() { func init() {
MsgHandlers["rss"] = rssHandleMessage MsgFuncs["rss"] = rssHandleMessage
go rssRun() RunFuncs["rss"] = rssRun
xlog.Info("RSS module initialized")
} }
func rssRun() { func rssRun() {

View File

@ -48,9 +48,8 @@ var (
) )
func init() { func init() {
MsgHandlers["sc"] = scHandleMessage MsgFuncs["sc"] = scHandleMessage
go scScrapeLoop() RunFuncs["sc"] = scScrapeLoop
xlog.Info("SC module initialized")
} }
func scHandleMessage(m *irc.Message) { func scHandleMessage(m *irc.Message) {

View File

@ -7,8 +7,6 @@ import (
"fmt" "fmt"
"strings" "strings"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
@ -522,8 +520,7 @@ var quotes = [][]string{
} }
func init() { func init() {
MsgHandlers["stoll"] = stollHandleMessage MsgFuncs["stoll"] = stollHandleMessage
xlog.Info("Stoll module initialized")
} }
func stollHandleMessage(m *irc.Message) { func stollHandleMessage(m *irc.Message) {

View File

@ -137,9 +137,8 @@ var (
) )
func init() { func init() {
MsgHandlers["twitch"] = twitchHandleMessage MsgFuncs["twitch"] = twitchHandleMessage
go pollStreamData() RunFuncs["twitch"] = pollStreamData
xlog.Info("Twitch module initialized")
} }
func twitchHandleMessage(m *irc.Message) { func twitchHandleMessage(m *irc.Message) {

View File

@ -71,9 +71,8 @@ type WeatherObject struct {
} }
func init() { func init() {
MsgHandlers["weather"] = weatherHandleMessage MsgFuncs["weather"] = weatherHandleMessage
go weatherConfig() RunFuncs["weather"] = weatherConfig
xlog.Info("Weather module initialized")
} }
func weatherConfig() { func weatherConfig() {

View File

@ -6,6 +6,7 @@ import (
"bytes" "bytes"
"math/rand" "math/rand"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -52,3 +53,14 @@ func Random(min, max int) int {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
return rand.Intn(max-min) + min return rand.Intn(max-min) + min
} }
func ReplaceUmlauts(s string) string {
ret := strings.Replace(s, "Ä", "Ae", -1)
ret = strings.Replace(ret, "Ö", "Oe", -1)
ret = strings.Replace(ret, "Ü", "Ue", -1)
ret = strings.Replace(ret, "ä", "ae", -1)
ret = strings.Replace(ret, "ö", "oe", -1)
ret = strings.Replace(ret, "ü", "ue", -1)
ret = strings.Replace(ret, "ß", "ss", -1)
return ret
}