~kris/9p

glenda

9542834efb3f41ad464d3802a20f4cb04b404882 — Sean Hinchee 8 years ago 7151497
added glenda-mux communication; added more config code; boilerplate for rss continues
A config.go => config.go +58 -0
@@ 0,0 1,58 @@
package main

// General configuration utilities for Glenda

import (
	"encoding/json"
	"os"
	"fmt"
)

// Stores config for current state
type Configuration struct {
	LastChange string
}

// Initializes current config (called once at start)
func (c *Configuration) Init() {
}

// Writes current config
func (c *Configuration) Write() (rerr error) {
	rerr = nil
	f, err := os.Open("./cfg/glenda.cfg")
	defer f.Close()
	if err != nil {
		fmt.Println("Error opening config, see: config.go")
		fmt.Printf("%s\n", err)
		rerr = err
	}

	e := json.NewEncoder(f)
	err = e.Encode(Config)
	if err != nil {
		fmt.Println("Error writing config, see: config.go")
		fmt.Printf("%s\n", err)
		rerr = err
	}
	return
}

// Reads current config into memory
func (c *Configuration) Read() {

}

// Set up config for the first time (if one doesn't exist)
func (c *Configuration) Setup() {
	// Test/make cfg folder


	// Test/make cfg


}

// Global variables are bad
var Config Configuration


A glenda => glenda +0 -0
M main.go => main.go +5 -1
@@ 9,6 9,7 @@ import (
	"syscall"
	"github.com/bwmarrin/discordgo"
	"bitbucket.org/henesy/glenda/x/mux"
sc	"strconv"
)

const Version = "v0.1.0-alpha"


@@ 75,12 76,15 @@ func main() {
		os.Exit(1)
	}

	// Init Mux daemons
	go mux.Listener()
	go mux.CommMux()
	go CommMux()

	Session.UpdateStatus(0, "with #cat-v")

	// Wait for a CTRL-C
	log.Printf(`Now running. Press CTRL-C to exit.`)
	log.Printf(`Now running on PID ` + sc.Itoa(os.Getpid()) + `. Press CTRL-C to exit.`)
	sc := make(chan os.Signal, 1)
	signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
	<-sc

A misc.go => misc.go +29 -0
@@ 0,0 1,29 @@
package main

// Miscellaneous utilities for communicating with Mux

import (
	"bitbucket.org/henesy/glenda/x/mux"
	"time"
)

// Service for communicating with Mux (starts once)
func CommMux() {
	for {
		select {
		case r := <-mux.GlendaChan:
			if r == "dump" {
				err := Config.Write()
				if err != nil {
					mux.MuxChan <- "dump failed"
				} else {
					mux.MuxChan <- "dump ok"
				}
			}
		default:
		}

		time.Sleep(500 * time.Millisecond)
	}
}


M mux.go => mux.go +6 -1
@@ 18,6 18,11 @@ func init() {
	// Register the build-in help command.
	Router.Route("help", "Display this message.", Router.Help)

	Router.Route("fortune", "Display fortunes.", Router.Fortunes)
	Router.Route("fortune", "Display fortunes. Bonus files are (theo troll rsc bullshit terry rob).", Router.Fortunes)

	Router.Route("dump", "Dump config to file.", Router.Dump)

	Router.Route("about", "General information about the bot.", Router.About)

}


M x/mux/README.md => x/mux/README.md +1 -0
@@ 27,3 27,4 @@ just learning the DiscordGo API and easily integrated into their own projects.
Some inspiration was taken from the [chi](https://github.com/go-chi/chi) 
and [httprouter](https://github.com/julienschmidt/httprouter) routers when 
creating dgMux.


A x/mux/about.go => x/mux/about.go +23 -0
@@ 0,0 1,23 @@
package mux

import (
	"github.com/bwmarrin/discordgo"
)

// Display basic information about the bot
func (m *Mux) About(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ""

	resp += `Hi!
My name is Glenda, my namesake is Glenda the Plan 9 mascot! (http://glenda.cat-v.org)
	
My source code is located at https://bitbucket.org/henesy/glenda. I am written in Go (https://tour.golang.org).
	`

	resp += "\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}


M x/mux/commits.go => x/mux/commits.go +1 -3
@@ 26,9 26,6 @@ func Listener() {
	}
}

// Load JSON Configuration File (move me?)


/* Commands for Mux */

// Print last commit or 


@@ 43,3 40,4 @@ func (m *Mux) Commits(ds *discordgo.Session, dm *discordgo.Message, ctx *Context

	return
}


M x/mux/fortunes.go => x/mux/fortunes.go +1 -0
@@ 45,3 45,4 @@ func (m *Mux) Fortunes(ds *discordgo.Session, dm *discordgo.Message, ctx *Contex

	return
}


M x/mux/help.go => x/mux/help.go +1 -0
@@ 78,3 78,4 @@ func (m *Mux) Help(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {

	return
}


A x/mux/misc.go => x/mux/misc.go +57 -0
@@ 0,0 1,57 @@
package mux

import (
	"github.com/bwmarrin/discordgo"
	"time"
)

// Chan for communicating to Glenda
var GlendaChan chan string

// Chan for communicating to Mux
var MuxChan chan string

// Chan for communicating with Dump
var dumpChan chan string


// Multiplex internal channels, initialized once
func CommMux() {
	MuxChan = make(chan string, 5)
	GlendaChan = make(chan string, 5)
	dumpChan = make(chan string)

	// Listen for signals till death do us part
	for {
		select {
		case r := <-MuxChan:
			if r == "dump ok" {
				dumpChan <- "Ok."
			} else if r == "dump failed" {
				dumpChan <- "Dump Failed. Check Logs."
			}
		case <-dumpChan:
			GlendaChan <- "dump"
		default:
		}

		time.Sleep(500 * time.Millisecond)
	}
}

// Dump config to file
func (m *Mux) Dump(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ""

	dumpChan <- ""
	resp += <-dumpChan

	resp += "\n"

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}




M x/mux/mux.go => x/mux/mux.go +2 -1
@@ 46,7 46,7 @@ type Mux struct {
// New returns a new Discord message route mux
func New() *Mux {
	m := &Mux{}
	m.Prefix = "!g "
	m.Prefix = "g!"
	return m
}



@@ 200,3 200,4 @@ func (m *Mux) OnMessageCreate(ds *discordgo.Session, mc *discordgo.MessageCreate
	}

}