~kris/9p

glenda

5b5de9767260b46583f15a538672a32c72c951bf — Sean Hinchee 8 years ago 1562c9d
added rudimentary man support
4 files changed, 96 insertions(+), 4 deletions(-)

M TODO
M mux.go
M x/mux/commits.go
A x/mux/man.go
M TODO => TODO +0 -2
@@ 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])

M mux.go => mux.go +2 -0
@@ 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)
	

M x/mux/commits.go => x/mux/commits.go +0 -2
@@ 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])

A x/mux/man.go => x/mux/man.go +94 -0
@@ 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) {
	
}