Commit 740a9808 authored by Mike Jones's avatar Mike Jones 🌶

In theory, connect to message queue

parent eb730a6c
package main
import (
"flag"
"fmt"
"log"
goschema "git.netsplit.uk/stella-irc/ioman/pkg/dragcarschema"
"github.com/boltdb/bolt"
"github.com/nats-io/go-nats"
"github.com/nats-io/go-nats/encoders/protobuf"
"git.netsplit.uk/stella-irc/modlastfm/internal/app/modlastfm/gauntlet"
"git.netsplit.uk/stella-irc/modlastfm/internal/app/modlastfm/store"
"git.netsplit.uk/stella-irc/modlastfm/internal/app/modlastfm/util"
)
type Message struct {
Raw string
Target string
}
func main() {
log.Println("Initialising stella-irc Last.fm module, hold tight...")
url := flag.String("url", nats.DefaultURL, "URL For NATS server")
flag.Parse()
nc, err := nats.Connect(*url, nats.UserInfo("nats_client", "shutthefuckupbrandon"))
if err != nil {
log.Fatalf("Failed to connect to nats: %s (URL: %s)", err.Error(), *url)
}
ec, err := nats.NewEncodedConn(nc, protobuf.PROTOBUF_ENCODER)
if err != nil {
log.Fatalf("Error starting connection: %s", err.Error())
}
log.Println("Connected to nats!")
defer ec.Close()
config := util.NewConfig()
db, err := bolt.Open(config.DBPath, 0600, nil)
......@@ -29,10 +59,23 @@ func main() {
// initialise commands
gauntlet.New(config, st)
// TODO: collect this from IRC input
out := gauntlet.Run("Mike", `l`, st)
inputChannel := make(chan *goschema.IRCMessage)
ec.BindRecvChan("messaging.input", inputChannel)
outputChannel := make(chan *goschema.IRCMessage)
ec.BindSendChan("messaging.output", outputChannel)
listen(inputChannel, func(m *goschema.IRCMessage) {
if m != nil {
out := gauntlet.Run(m.Nick, m.Raw, st)
outputChannel <- &goschema.IRCMessage{Raw: fmt.Sprintf(out), Target: m.Target}
}
})
}
if out != "" {
log.Println(out)
func listen(channel chan *goschema.IRCMessage, sendFunc func(*goschema.IRCMessage)) {
for msg := range channel {
sendFunc(msg)
}
}
......@@ -63,7 +63,7 @@ func (b *BoltStore) GetAssociatedUser(nickname string) (string, error) {
// RemoveAssociatedUser() deletes a username set against a nickname (woo, GDPR
// compliance!)
func (b *BoltStore) RemoveAssociatedUser(nickname string) error {
return b.db.Update(func (tx *bolt.Tx) error {
return b.db.Update(func(tx *bolt.Tx) error {
userBucket := tx.Bucket([]byte(userBucketKey))
if userBucket == nil {
......@@ -81,4 +81,4 @@ func (b *BoltStore) init() error {
return err
})
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment