From 5b5de9767260b46583f15a538672a32c72c951bf Mon Sep 17 00:00:00 2001 From: Sean Hinchee Date: Tue, 30 Jan 2018 07:14:30 +0000 Subject: [PATCH] added rudimentary man support --- TODO | 2 -- mux.go | 2 ++ x/mux/commits.go | 2 -- x/mux/man.go | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 x/mux/man.go diff --git a/TODO b/TODO index 5c206d63875b1b0125c5b3b9e56dd679d61f7551..a0414c433dad1f4a2ebf9e5808642a6dc1bc8e88 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,5 @@ Features * g!sum acid(1) -* g!man acid(1) or g!man 1 acid -* rss feed subscription (tracking commits) * Server greeting * 9front/plan9 source searching * various cat-v fetches (harmful, page search [get lucky]) diff --git a/mux.go b/mux.go index 2eb1da42c573d0271f60a95aeaf216e30a8f0da1..31e2d8641982c28642a3139e6bef178c8afef803 100644 --- a/mux.go +++ b/mux.go @@ -29,6 +29,8 @@ func init() { Router.Route("dump", "Dump config to file.", Router.Dump) Router.Route("subscribe", "Subscribe current channel to a given feed id.", Router.Subscribe) + + Router.Route("man", "Display a given Plan 9 manual page (From 9front for now).", Router.Man) Router.Route("about", "General information about the bot.", Router.About) diff --git a/x/mux/commits.go b/x/mux/commits.go index eaff1ccc9f55fd124dd97797ea0c257e4c09d9a6..24deb7f2b7c3a0581c7b4721754c2dc850aae135 100644 --- a/x/mux/commits.go +++ b/x/mux/commits.go @@ -64,8 +64,6 @@ func (m *Mux) Last(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { resp += "**" + Config.Feeds[id].Title + ": **" + "\n" resp += Config.Feeds[id].Items[0].Date.String() + "\n\n" resp += "`" + Config.Feeds[id].Items[0].Title + "`" + "\n" - //resp += Config.Feeds[id].Items[0].Summary + "\n" - //resp += Config.Feeds[id].Items[0].Content + "\n" resp += "\n" + Config.Feeds[id].Items[0].Link + "\n" Config.Feeds[id].Items[0].Read = true fmt.Println("Last-ing notification: ", Config.Feeds[id].Items[0]) diff --git a/x/mux/man.go b/x/mux/man.go new file mode 100644 index 0000000000000000000000000000000000000000..587b7c19add3147748e43ea231443c05f5b7501c --- /dev/null +++ b/x/mux/man.go @@ -0,0 +1,94 @@ +package mux + +import ( + "github.com/bwmarrin/discordgo" + "fmt" + "net/http" + "strings" + "io/ioutil" + "time" + "strconv" +) + + +// Fetch a man page +func (m *Mux) Man(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { + resp := "\n" + var err error + + page := "" + section := "" + if len(ctx.Fields) == 3 { + // `man 1 bc` format + section += ctx.Fields[len(ctx.Fields)-2] + page += ctx.Fields[len(ctx.Fields)-1] + + // Should allow selection of manual categories beyond 9front + url := "http://man.cat-v.org/9front/" + section + "/" + page + + page, err := http.Get(url) + defer page.Body.Close() + body, err := ioutil.ReadAll(page.Body) + + if err != nil || strings.Contains(string(body), "The requested document at") { + fmt.Println("Error finding man page, see: x/mux/man.go") + fmt.Println("%s\n", err) + resp += "Invalid manual page requested." + } else { + resp += string(url) + } + + } else if len(ctx.Fields) == 2 { + // `man srv` format + page += ctx.Fields[len(ctx.Fields)-1] + any := false + + for i:=1; i < 9; i++ { + // Should allow selection of manual categories beyond 9front + url := "http://man.cat-v.org/9front/" + strconv.Itoa(i) + "/" + page + + page, err := http.Get(url) + defer page.Body.Close() + body, err := ioutil.ReadAll(page.Body) + + if err != nil || strings.Contains(string(body), "The requested document at") { + continue + } else { + if !any { + any = true + resp += ".\n" + } + resp += string(url) + "\n" + } + time.Sleep(1 * time.Second) + } + + // No matches + if !any { + resp += "No matching manual page(s) found." + } + + } else { + resp += "No op. Please use the 'man 3 srv' format or 'man srv' format." + } + + resp += "\n" + + _, err = ds.ChannelMessageSend(dm.ChannelID, resp) + if err != nil { + fmt.Println("Error sending man(1) output as message, see: x/mux/man.go") + fmt.Println(err) + } + + return +} + +// Fetch a summary of a man page +func (m *Mux) Sum(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { + +} + +// Call lookman on a given query +func (m *Mux) Lookman(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) { + +}