diff options
author | Robert S. Gerus <ar@bash.org.pl> | 2015-12-23 07:56:04 +0100 |
---|---|---|
committer | Robert S. Gerus <ar@bash.org.pl> | 2015-12-23 07:56:04 +0100 |
commit | 29cdd169f61ff5e4301798d15f070c786d6d9327 (patch) | |
tree | 0f0b0563408643057dc4c381d8735e69cde29c20 | |
parent | 09286f3defa2f9368b645a4905a19cb0f521dafd (diff) | |
parent | c6643487807a150a637c0c158e73d1a415f3560e (diff) | |
download | gorepost-29cdd169f61ff5e4301798d15f070c786d6d9327.tar.gz gorepost-29cdd169f61ff5e4301798d15f070c786d6d9327.tar.bz2 gorepost-29cdd169f61ff5e4301798d15f070c786d6d9327.tar.xz gorepost-29cdd169f61ff5e4301798d15f070c786d6d9327.zip |
Merge pull request #50 from arachnist/rework-initialization
Rework initialization of some plugins.
Fixes #42
-rw-r--r-- | bot/.testconfig.json | 2 | ||||
-rw-r--r-- | bot/bot.go | 17 | ||||
-rw-r--r-- | bot/jan.go | 11 | ||||
-rw-r--r-- | bot/papiez.go | 11 | ||||
-rw-r--r-- | bot/seen.go | 13 |
5 files changed, 25 insertions, 29 deletions
diff --git a/bot/.testconfig.json b/bot/.testconfig.json index 1fdd524..7b78565 100644 --- a/bot/.testconfig.json +++ b/bot/.testconfig.json @@ -12,5 +12,7 @@ "DictionaryObjects":".dicts/objects", "DictionaryVerbs":".dicts/verbs", "DictionaryAdjectives":".dicts/adjectives", + "KTHost":"127.0.0.1", + "KTPort":1337, "Nick":"gorepost" } @@ -6,13 +6,20 @@ import ( ) var cfg *dyncfg.Dyncfg -var cfgLock sync.Mutex +var initLock sync.Mutex +var initList []func() + +func addInit(f func()) { + initLock.Lock() + defer initLock.Unlock() + + initList = append(initList, f) +} func Initialize(config *dyncfg.Dyncfg) { cfg = config - cfgLock.Unlock() -} -func init() { - cfgLock.Lock() + for _, f := range initList { + f() + } } @@ -8,7 +8,6 @@ import ( "log" "math/rand" "strings" - "sync" "time" "github.com/arachnist/gorepost/irc" @@ -16,7 +15,6 @@ import ( var objects []string var predicates []string -var janLock sync.RWMutex func jan(output func(irc.Message), msg irc.Message) { args := strings.Split(msg.Trailing, " ") @@ -24,9 +22,6 @@ func jan(output func(irc.Message), msg irc.Message) { return } - janLock.RLock() - defer janLock.RUnlock() - var predicate string var object string @@ -49,9 +44,6 @@ func jan(output func(irc.Message), msg irc.Message) { } func lazyJanInit() { - defer janLock.Unlock() - cfgLock.Lock() - defer cfgLock.Unlock() var err error rand.Seed(time.Now().UnixNano()) objects, err = readLines(cfg.LookupString(nil, "DictionaryObjects")) @@ -68,7 +60,6 @@ func lazyJanInit() { } func init() { - janLock.Lock() log.Println("Defering \"jan\" initialization") - go lazyJanInit() + addInit(lazyJanInit) } diff --git a/bot/papiez.go b/bot/papiez.go index f38ee37..82d93e0 100644 --- a/bot/papiez.go +++ b/bot/papiez.go @@ -8,14 +8,12 @@ import ( "log" "math/rand" "strings" - "sync" "time" "github.com/arachnist/gorepost/irc" ) var adjectives []string -var papiezLock sync.RWMutex func papiez(output func(irc.Message), msg irc.Message) { args := strings.Split(msg.Trailing, " ") @@ -23,18 +21,12 @@ func papiez(output func(irc.Message), msg irc.Message) { return } - papiezLock.RLock() - defer papiezLock.RUnlock() - choice := "Papież " + adjectives[rand.Intn(len(adjectives))] output(reply(msg, choice)) } func lazyPapiezInit() { - defer papiezLock.Unlock() - cfgLock.Lock() - defer cfgLock.Unlock() var err error rand.Seed(time.Now().UnixNano()) adjectives, err = readLines(cfg.LookupString(nil, "DictionaryAdjectives")) @@ -46,7 +38,6 @@ func lazyPapiezInit() { } func init() { - papiezLock.Lock() log.Println("Defering \"papiez\" initialization") - go lazyPapiezInit() + addInit(lazyPapiezInit) } diff --git a/bot/seen.go b/bot/seen.go index 0bb4e1d..b36b2ab 100644 --- a/bot/seen.go +++ b/bot/seen.go @@ -93,12 +93,12 @@ func seen(output func(irc.Message), msg irc.Message) { output(reply(msg, r)) } -func init() { +func seenInit() { var err error - var ktHost = "127.0.0.1" - var ktPort = 1337 + var ktHost = cfg.LookupString(nil, "KTHost") + var ktPort = cfg.LookupInt(nil, "KTPort") - log.Println("SEEN: connecting to KT") + log.Println("seen: connecting to KT") k, err = kt.NewConn(ktHost, ktPort, 4, 2*time.Second) if err != nil { log.Println("error connecting to kyoto tycoon", err) @@ -113,3 +113,8 @@ func init() { addCallback("QUIT", "seenrecord", seenrecord) addCallback("NOTICE", "seenrecord", seenrecord) } + +func init() { + log.Println("Defering \"papiez\" initialization") + addInit(seenInit) +} |