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")
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")
)
@ -57,7 +58,7 @@ func main() {
//mods := strings.Split(*modules, ",")
//TODO: implement more robust list parsing
modules.Init(sayCh, *params)
modules.Init(sayCh, *mods, *params)
go func() {
for {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,21 +13,44 @@ import (
)
var (
SayCh chan string
MsgHandlers = make(map[string]func(*irc.Message))
ModParams = make(map[string]string)
SayCh chan string
MsgFuncs = make(map[string]func(*irc.Message))
RunFuncs = make(map[string]func())
ModParams = make(map[string]string)
)
func Init(ch chan string, params string) {
func Init(ch chan string, mods, params string) {
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, "!") {
kv := strings.Split(param, ":")
ModParams[kv[0]] = kv[1]
}
for _, fn := range RunFuncs {
go fn()
}
}
func HandleMessage(m *irc.Message) {
for _, fn := range MsgHandlers {
for _, fn := range MsgFuncs {
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
func init() {
MsgHandlers["rss"] = rssHandleMessage
go rssRun()
xlog.Info("RSS module initialized")
MsgFuncs["rss"] = rssHandleMessage
RunFuncs["rss"] = rssRun
}
func rssRun() {

View File

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

View File

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

View File

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

View File

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

View File

@ -6,6 +6,7 @@ import (
"bytes"
"math/rand"
"strconv"
"strings"
"time"
)
@ -52,3 +53,14 @@ func Random(min, max int) int {
rand.Seed(time.Now().Unix())
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
}