From 7e9f2acbbf00e909d844de81e71be69b4913695a Mon Sep 17 00:00:00 2001 From: Henesy Date: Sun, 15 Nov 2020 05:55:00 +0000 Subject: [PATCH] shorten reminder logging ;; add owner privilege ;; modularize many paths into cfg --- main.go | 9 ++++----- misc.go | 1 - mux.go | 30 +++++++++++++++--------------- x/mux/commits.go | 13 +++++++++++++ x/mux/config.go | 36 +++++++++++++++++++++++++++++++++--- x/mux/fortunes.go | 6 +++--- x/mux/man.go | 21 +++++++++++++-------- x/mux/misc.go | 17 +++++++++++++++++ 8 files changed, 98 insertions(+), 35 deletions(-) diff --git a/main.go b/main.go index 2b390c2fb75e5f3f0a7dfe0af3d5aa5751748da2..8d2e5d9473eb910a9f40c3df7951e00765f6f54d 100644 --- a/main.go +++ b/main.go @@ -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. } - diff --git a/misc.go b/misc.go index 68b99b37d887e34a43ee8a4c9c1c8276e6fd4923..e93cd3e374b9668d92f51ae7280420bcea8d1194 100644 --- a/misc.go +++ b/misc.go @@ -16,4 +16,3 @@ func CommMux() { time.Sleep(500 * time.Millisecond) } } - diff --git a/mux.go b/mux.go index 4cf802dbb869e3e3a587bd08df0546cc350a8721..e002a6933560b10866a0024e2427a1abe5b82029 100644 --- a/mux.go +++ b/mux.go @@ -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) } diff --git a/x/mux/commits.go b/x/mux/commits.go index ce757e6349be03ba5641ec75f93acd1f54b5d323..c24c62b90629ab08bb89a5b3fa53505c94c0c340 100644 --- a/x/mux/commits.go +++ b/x/mux/commits.go @@ -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 diff --git a/x/mux/config.go b/x/mux/config.go index d02c511680992a0e3735e27a95e6078b19965296..82b4d88507aa52e0618ecd013d342a3ccccdebb6 100644 --- a/x/mux/config.go +++ b/x/mux/config.go @@ -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 { diff --git a/x/mux/fortunes.go b/x/mux/fortunes.go index 4afb4ecbd9bf7a02701c84254bda303a000ff1e6..f0fff50e3d2c4a02b3a4465ba2c1d3b7c00014d6 100644 --- a/x/mux/fortunes.go +++ b/x/mux/fortunes.go @@ -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 diff --git a/x/mux/man.go b/x/mux/man.go index 4d389c0df2a7e0bdb353d295f76014373194e4f9..8bc4bbef1bcd1174a948c76dfa6517cf0d9ecb1c 100644 --- a/x/mux/man.go +++ b/x/mux/man.go @@ -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." diff --git a/x/mux/misc.go b/x/mux/misc.go index 7fef7700e019779ad61caf8b08f93e120377c1a3..6326f1317e9486b2f16ecc4661d1c9dd56083ade 100644 --- a/x/mux/misc.go +++ b/x/mux/misc.go @@ -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)