M main.go => main.go +4 -5
@@ 3,14 3,14 @@ package main
import (
"flag"
"fmt"
+ "github.com/bwmarrin/discordgo"
+ "github.com/henesy/glenda/x/mux"
"log"
"os"
"os/signal"
+ sc "strconv"
"syscall"
"time"
- "github.com/bwmarrin/discordgo"
- "github.com/henesy/glenda/x/mux"
-sc "strconv"
)
const Version = "v0.1.1"
@@ 76,7 76,7 @@ func main() {
log.Printf("error opening connection to Discord, %s\n", err)
os.Exit(1)
}
-
+
// Init boot vars
mux.StartTime = time.Now()
@@ 98,4 98,3 @@ func main() {
// Exit Normally.
}
-
M misc.go => misc.go +0 -1
@@ 16,4 16,3 @@ func CommMux() {
time.Sleep(500 * time.Millisecond)
}
}
-
M mux.go => mux.go +15 -15
@@ 21,11 21,11 @@ func init() {
Router.Route("help", "Display this message.", Router.Help)
Router.Route("about", "General information about the bot.", Router.About)
-
+
Router.Route("newuser", "Information for new users of Plan 9.", Router.Newuser)
-
+
Router.Route("remindme", "Set a reminder for a given time interval ([int][hmsd] [reminder]).", Router.RemindMe)
-
+
Router.Route("man", "Link a given Plan 9 manual page (From 9front for now).", Router.Man)
Router.Route("lookman", "Search for a string in the Plan 9 manual pages (From 9front for now).", Router.Lookman)
@@ 33,30 33,30 @@ func init() {
Router.Route("sig", "Search for function definitions in the Plan 9 manual pages (From 9front for now).", Router.Sig)
Router.Route("fortune", "Display fortunes. Bonus files are (theo troll rsc terry rob ken henesy davros).", Router.Fortunes)
-
+
Router.Route("bullshit", "Print logical statements with sound grounding.", Router.Bullshit)
-
+
Router.Route("add", "Track an RSS feed.", Router.Add)
-
+
Router.Route("remove", "Remove a tracked RSS feed by ID.", Router.Remove)
-
+
Router.Route("list", "List RSS feeds Glenda is subscribed to by id.", Router.List)
-
+
Router.Route("last", "Show the last commit for a given RSS feed by id.", Router.Last)
Router.Route("dump", "Dump config to file.", Router.Dump)
-
+
Router.Route("mkindex", "Generate index for lookman from mirror.", Router.Mkindex)
-
+
Router.Route("subscribe", "Subscribe current channel to a given feed id.", Router.Subscribe)
-
+
Router.Route("unsubscribe", "Unsubscribe current channel to a given feed id.", Router.Unsubscribe)
-
+
Router.Route("beer", ":beer:", Router.Beer)
-
+
Router.Route("whiskey", ":tumbler_glass:", Router.Whiskey)
-
+
Router.Route("wine", ":wine_glass:", Router.Wine)
-
+
Router.Route("uptime", "Current bot uptime", Router.Uptime)
}
M x/mux/commits.go => x/mux/commits.go +13 -0
@@ 87,8 87,11 @@ func Notify(id int) {
}
fmt.Println("No new notifys for ", Config.Feeds[id].Feed.UpdateURL)
+
+ /* Enable for logging if subs break
fmt.Println(Config.Feeds[id].Feed.Items[0])
fmt.Println(Config.Feeds[id].Feed.Items[len(Config.Feeds[id].Feed.Items)-1])
+ */
}
@@ 146,6 149,11 @@ func (m *Mux) List(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
// Add a given feed to be tracked within glenda
func (m *Mux) Add(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
+ if !authorized(dm) {
+ ds.ChannelMessageSend(dm.ChannelID, "Only the bot owner can do that.")
+ return
+ }
+
//http://code.9front.org/hg/plan9front/rss-log
resp := "```\n"
// URL to feed should be last item
@@ 191,6 199,11 @@ func (m *Mux) Add(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
// Stop tracking a given feed
func (m *Mux) Remove(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
+ if !authorized(dm) {
+ ds.ChannelMessageSend(dm.ChannelID, "Only the bot owner can do that.")
+ return
+ }
+
//http://code.9front.org/hg/plan9front/rss-log
resp := "```\n"
// ID to feed should be last item
M x/mux/config.go => x/mux/config.go +33 -3
@@ 8,6 8,8 @@ import (
"fmt"
"strings"
"errors"
+ "strconv"
+ "time"
"github.com/SlyMarbo/rss"
"github.com/bwmarrin/discordgo"
)
@@ 44,10 46,38 @@ type Configuration struct {
// Initializes current config (called once at start) just .Read()?
func (c *Configuration) Init(s *discordgo.Session) {
c.Db = map[string]string {
- "owner": "188698402727526400", // Henesy
- "name": "glenda.cfg",
- "dir": "./cfg",
+ "owner": "188698402727526400", // Henesy
+ "name": "glenda.cfg", // Config file name
+ "dir": "./cfg", // Config dir
+ "delay": "20", // Minutes
+ "mansite": "http://man.postnix.pw/9front/", // Henesy's
+
+ "lookman": "./x/mux/lookman/lookman", // Vendored
+ "sig": "./x/mux/man/sig", // ^
+ "gendex": "./gendex", // ^
+ "bullshit": "./x/mux/misc/bullshit", // ^
+
+ "extrafortunes": "../sys/games/lib/fortunes", // Unix
+ "fortunes": "/usr/share/mirror/plan9front/lib/", // Plan 9
+
+ }
+
+ // Regularly dump the config
+ go func() {
+ delay, err := strconv.Atoi(c.Db["delay"])
+ if err != nil {
+ fmt.Println("bad delay value, using default")
+ delay = 20
+ }
+
+ select {
+ case <-dumpChan:
+ dump()
+
+ case <- time.After(time.Duration(delay) * time.Minute):
+ dump()
}
+ }()
err := c.Read()
if err != nil {
M x/mux/fortunes.go => x/mux/fortunes.go +3 -3
@@ 12,7 12,7 @@ func (m *Mux) Fortunes(ds *discordgo.Session, dm *discordgo.Message, ctx *Contex
resp := "```\n"
// Read relevant file
- path := "/usr/share/mirror/plan9front/lib/"
+ path := Config.Db["fortunes"]
f := ""
if strings.Contains(dm.Content, "theo") {
@@ 32,7 32,7 @@ func (m *Mux) Fortunes(ds *discordgo.Session, dm *discordgo.Message, ctx *Contex
} else if strings.Contains(dm.Content, "davros") {
f = "davros"
} else {
- f = "../sys/games/lib/fortunes"
+ f = Config.Db["extrafortunes"]
}
// Linux solution
@@ 54,7 54,7 @@ func (m *Mux) Fortunes(ds *discordgo.Session, dm *discordgo.Message, ctx *Contex
func (m *Mux) Bullshit(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
resp := "```\n"
- out, err := exec.Command("./x/mux/misc/bullshit").Output()
+ out, err := exec.Command(Config.Db["bs"]).Output()
if err != nil {
resp += "No bullshit script found."
goto END
M x/mux/man.go => x/mux/man.go +13 -8
@@ 40,12 40,12 @@ func (m *Mux) Man(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
page += ctx.Fields[len(ctx.Fields)-1]
// Should allow selection of manual categories beyond 9front
- url := "http://man.postnix.pw/9front/" + section + "/" + page
+ url := Config.Db["mansite"] + section + "/" + page
page, err := http.Get(url)
if err != nil {
fmt.Println("Error fetching URL, see: x/mux/man.go")
- resp += "Error fetching URL. Is man.postnix.pw up?"
+ resp += "Error fetching URL. Is " + Config.Db["mansite"] + " up?"
goto URL
}
defer page.Body.Close()
@@ 66,7 66,7 @@ func (m *Mux) Man(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
for i:=1; i < 9; i++ {
// Should allow selection of manual categories beyond 9front
- url := "http://man.postnix.pw/9front/" + strconv.Itoa(i) + "/" + page
+ url := Config.Db["mansite"] + strconv.Itoa(i) + "/" + page
page, err := http.Get(url)
defer page.Body.Close()
@@ 90,7 90,7 @@ func (m *Mux) Man(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
}
} else if len(ctx.Fields) == 1 {
- resp += "http://man.postnix.pw/9front/\n"
+ resp += Config.Db["mansite"] + "\n"
for i:=1; i < 9; i++ {
resp += desc[i-1] + "\n"
}
@@ 121,7 121,7 @@ func (m *Mux) Sig(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
args := ctx.Fields[1:]
// rc or get out
- out, err := exec.Command("./x/mux/man/sig", args...).Output()
+ out, err := exec.Command(Config.Db["sig"], args...).Output()
if err != nil {
resp += "No sig script found."
goto END
@@ 162,7 162,7 @@ func (m *Mux) Lookman(ds *discordgo.Session, dm *discordgo.Message, ctx *Context
// rc or get out
//fmt.Println("Running: ", args)
- out, err := exec.Command("./x/mux/lookman/lookman", args...).Output()
+ out, err := exec.Command(Config.Db["lookman"], args...).Output()
if err != nil {
resp += "No lookman script found."
goto END
@@ 183,7 183,7 @@ func (m *Mux) Lookman(ds *discordgo.Session, dm *discordgo.Message, ctx *Context
i := fields[1]
page := fields[2]
- url := "http://man.postnix.pw/9front/" + i + "/" + page + " # " + fields[4] + "\n"
+ url := Config.Db["mansite"] + i + "/" + page + " # " + fields[4] + "\n"
if top {
top = false
@@ 209,9 209,14 @@ func (m *Mux) Lookman(ds *discordgo.Session, dm *discordgo.Message, ctx *Context
// Call lookman on a given query
func (m *Mux) Mkindex(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
+ if !authorized(dm) {
+ ds.ChannelMessageSend(dm.ChannelID, "Only the bot owner can do that.")
+ return
+ }
+
resp := "\n"
- out, err := exec.Command("./gendex").Output()
+ out, err := exec.Command(Config.Db["gendex"]).Output()
if err != nil {
fmt.Println(err)
resp += "No mkindex script found."
M x/mux/misc.go => x/mux/misc.go +17 -0
@@ 59,8 59,25 @@ func (m *Mux) Uptime(ds *discordgo.Session, dm *discordgo.Message, ctx *Context)
return
}
+// Check authorization as owner
+func authorized(dm *discordgo.Message) bool {
+ user := dm.Author
+ id := user.ID
+
+ if id == Config.Db["owner"] {
+ return true
+ }
+
+ return false
+}
+
// Dump config to file
func (m *Mux) Dump(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
+ if !authorized(dm) {
+ ds.ChannelMessageSend(dm.ChannelID, "Only the bot owner can do that.")
+ return
+ }
+
resp := dump()
ds.ChannelMessageSend(dm.ChannelID, resp)