1# Get help for the epic client from the wiki.
2# 2009 Jul 17 skullY@EFnet
3
4package help
5
6### Variables
7@help.server = "epicsol.org"
8@help.baseuri = "/"
9@help.headers.1 = "Host: $help.server"
10@help.headers.2 = "Accept: text/*"
11@help.headers.3 = "User-Agent: ircII $J [$V] [$info(i)] [$info(r)]"
12@help.headers.4 = "Connection: Close"
13@help.headers.5 = "X-DOKUWIKI-DO: export_raw"
14@help.prompt="'q' to quit, 'n' to stop prompting, or any other key to continue"
15
16### End user aliases
17alias help (page default "new_user") { help.get $page }
18
19### Useful aliases
20# help.sendline: Send a line terminated with CRLF to the specified connection.
21alias help.sendline (refnum, line) {
22	msg =$refnum ${line}$chr(13)
23	#xecho -b help: help.sendline: $refnum $line
24}
25
26# http.get: Fetch a help page
27alias help.get (page default "new_user") {
28	xecho -b Fetching help page ${page}, one moment please...
29	@help.conn = connect($help.server 80)
30	if (help.conn == "") {
31		xecho -b Help: Could not connect to $help.server
32		return
33	}
34	@help.state[$help.conn][headers] = 1;
35	@help.state[$help.conn][line] = 0;
36	@help.state[$help.conn][paginateoutput] = 1;
37	@help.state[$help.conn][stopoutput] = 0;
38	@help.state[$help.conn][page] = "$help.baseuri/$page";
39
40	^on ^dcc_raw "$help.conn $help.server E 80" {
41		help.sendline $0 GET $help.state[$0][page] HTTP/1.1
42		foreach help.headers xx {
43			help.sendline $0 $help.headers[$xx]
44		}
45		# FIXME: should be able to do this through help.sendline
46		msg =$0 $chr(13)
47	}
48
49	^on ^dcc_raw "$help.conn $help.server D *" {
50		@:line = "$3-"
51		if (help.state[$0][headers]) {
52			if (line == "") {
53				@help.state[$0][headers] = 0
54			}
55			return
56		}
57		if (line == "The end!") {
58			return
59		}
60
61		# Stuff to strip
62		@:line = strip(= $line)
63		@:line = sar(g/\\\\//$line)
64		# Underline
65		@:line = sar(g/__//$line)
66		# Various types of emphasis
67		@:line = sar(g,//,,$line)
68		@:line = sar(g/%%//$line)
69		# Links
70		@:line = sar(g/[[//$line)
71		@:line = sar(g/]]//$line)
72		# Tables
73		# FIXME: Doesn't seem to work?
74		if (left(1 $line) == "^") {
75			@:line = sar(g/|/    /$line)
76		} elif (left(1 $line) == "|") {
77			@:line = sar(g/^/    /$line)
78		}
79
80		# Store the line in the array
81		@setitem(help_$0 $help.state[$help.conn][line] $line)
82		@help.state[$help.conn][line]++
83	}
84
85	^on ^dcc_raw "$help.conn $help.server C" {
86		@height = windowctl(get 0 display_size)
87		@numlines = help.state[$0][line] - 4
88
89		# Show the help page
90		for i from 0 to $numlines {
91			if ((i > 0) && (i % height == 0)) {
92				# FIXME: need a safe way to pause the loop
93				#help.prompt $0
94				if (help.state[$0][stopoutput]) {
95					break
96				}
97			}
98			xecho -v --  [$getitem(help_$0 $i)]
99		}
100
101		# Cleanup
102		^on dcc_raw -"$0 $1 E 80"
103		^on dcc_raw -"$0 $1 D *"
104		^on dcc_raw -"$0 $1 C"
105		foreach help.state[$0] xx {
106			^assign -help.state[$0][$xx]
107		}
108		@delarray(help_$0)
109	}
110}
111
112alias help.prompt (conn) {
113	if (help.state[$conn][paginateoutput]) {
114		input_char -noecho "$help.prompt" {
115			switch ($0) {
116				(q) {
117					@help.state[$conn][stopoutput] = 1;
118				}
119				(n) {
120					@help.state[$conn][paginateoutput] = 0;
121				}
122			}
123		}
124	}
125}
126