~kris/9p

glenda

ref: 4274d7daaea5bc33e171220337848ae9eddbb4f1 glenda/x/mux/man.go -rw-r--r-- 5.1 KiB
4274d7da — Sean Hinchee add g!sig command 7 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package mux

import (
	"github.com/bwmarrin/discordgo"
	"fmt"
	"net/http"
	"strings"
	"io/ioutil"
	"time"
	"strconv"
	"os/exec"
)

var desc []string = []string{
	"Section (1) for general publicly accessible commands.",
	"Section (2) for library functions, including system calls.",
	"Section (3) for kernel devices (accessed via bind (1)).",
	"Section (4) for file services (accessed via mount).",
	"Section (5) for the Plan 9 file protocol.",
	"Section (6) for file formats.",
	"Section (7) for databases and database access programs.",
	"Section (8) for things related to administering Plan 9."}


// Match a term other than the title of the manual page for a manual page (6a should match the 2a man)
func MatchMan(key string) {
	
}

// 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.postnix.us/9front/" + 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.us up?"
			goto 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.postnix.us/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 += desc[i-1] + "\n"
				}
				resp += string(url) + "\n"
			}
			time.Sleep(20 * time.Millisecond)
		}
		
		// No matches
		if !any {
			resp += "No matching manual page(s) found."
		}
		
	} else if len(ctx.Fields) == 1 {
		resp += "http://man.postnix.us/9front/\n"
		for i:=1; i < 9; i++ {
			resp += desc[i-1] + "\n"
		}
	} else {
		resp += "No op. Please use the 'man 3 srv' format or 'man srv' format."
	}

	URL:
	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) Sig(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := "\n```"

	if !(len(ctx.Fields) >= 2) {
		resp += "Usage: lookman key ...\n"
	} else {
		// g!sig vmx
		args := ctx.Fields[1:]
	
		// rc or get out
		out, err := exec.Command("./x/mux/man/sig", args...).Output()
		if err != nil {
			resp += "No sig script found."
			goto END
		} else {
			if len(out) < 2 {
				resp += "No matching definitions found."
				goto END
			}
			
			str := string(out)
			// lines := strings.Split(str, "\n")
			resp += str
		}
	} 

	END:
	resp += "```\n"
	ds.ChannelMessageSend(dm.ChannelID, resp)
}

// Call lookman on a given query
func (m *Mux) Lookman(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := "\n"

	if len(ctx.Fields) >= 2 {
		// g!lookman vmx
		s := ctx.Fields[1:]
		// Match 10 keys max
		MAXKEYS := 10
		args := make([]string, 0, MAXKEYS)
		for p, v := range s {
			if p < MAXKEYS {
				args = append(args, v)
			} else {
				break
			}
		}
	
		// rc or get out
		//fmt.Println("Running: ", args)
		out, err := exec.Command("./x/mux/lookman/lookman", args...).Output()
		if err != nil {
			resp += "No lookman script found."
			goto END
		} else {
			if len(out) < 2 {
				resp += "No matching manual page(s) found."
				goto END
			}
			
			top := true
			str := string(out)
			lines := strings.Split(str, "\n")
			for p, l := range lines {
				if p == len(lines) -1 {
					break
				}
				fields := strings.Split(l, " ")
				i := fields[1]
				page := fields[2]
				
				url := "http://man.postnix.us/9front/" + i + "/" + page + " # " + fields[4] + "\n"
	
				if top {
					top = false
					first, _ := strconv.Atoi(i)
					resp += desc[first-1] + "\n"
				}
				
				resp += url
			}
		}
	} else {
		resp += "Usage: lookman key ...\n"
	}
	
	if len(resp) > 2000 {
		resp = "\nError: Lookman output exceeded Discordâ„¢ 2000 character limit."
	}

	END:
	resp += "\n"
	_, _ = ds.ChannelMessageSend(dm.ChannelID, resp)
}

// Call lookman on a given query
func (m *Mux) Mkindex(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := "\n"

	out, err := exec.Command("./gendex").Output()
	if err != nil {
		fmt.Println(err)
		resp += "No mkindex script found."
		goto END
	} else {
		fmt.Println("Generating out: ", out)
		resp += "Ok."
	}

	END: 
	resp += "\n"
	ds.ChannelMessageSend(dm.ChannelID, resp)
}