1 | #! perl |
1 | #! perl # mandatory depends=doclet |
2 | |
2 | |
3 | #-------------------------------------------------- |
3 | our $TOPIC; |
|
|
4 | our %DOCLET; |
|
|
5 | |
|
|
6 | our $HELP_CHANNEL = { |
|
|
7 | id => "help", |
|
|
8 | title => "Help", |
|
|
9 | reply => "help ", |
|
|
10 | tooltip => "Online Help", |
|
|
11 | }; |
|
|
12 | |
|
|
13 | # considerable duplication between load_doclets and load_topics |
|
|
14 | sub load_doclets { |
|
|
15 | %DOCLET = (); |
|
|
16 | |
|
|
17 | my @command_list; |
|
|
18 | |
|
|
19 | for ( |
|
|
20 | [0, "command_help"], |
|
|
21 | [1, "emote_help"], |
|
|
22 | [2, "dmcommand_help"], |
|
|
23 | ) { |
|
|
24 | my ($type, $path) = @$_; |
|
|
25 | |
|
|
26 | my $paragraphs = cf::pod::load_pod "$PODDIR/$path.pod" |
|
|
27 | or die "unable to load $path"; |
|
|
28 | |
|
|
29 | my $level = 1e9; |
|
|
30 | my $rpar; |
|
|
31 | |
|
|
32 | for my $par (@$paragraphs) { |
|
|
33 | if ($par->{type} eq "head2") { |
|
|
34 | # this code taken almost verbatim from DC/Protocol.pm |
|
|
35 | |
|
|
36 | if ($par->{markup} =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x) { |
|
|
37 | my $cmd = $1; |
|
|
38 | my @args = split /\|/, $2; |
|
|
39 | @args = (".*") unless @args; |
|
|
40 | |
|
|
41 | $_ = $_ eq ".*" ? "" : " $_" |
|
|
42 | for @args; |
|
|
43 | |
|
|
44 | my @variants = map "$cmd$_", sort { (length $a) <=> (length $b) } @args; |
|
|
45 | |
|
|
46 | $rpar = \($DOCLET{$cmd} = &cf::pod::as_cfpod ([$par])); |
|
|
47 | |
|
|
48 | push @command_list, [$type, \@variants]; |
|
|
49 | $level = $par->{level}; |
|
|
50 | } else { |
|
|
51 | cf::error "$par->{markup}: unparsable command heading"; |
|
|
52 | } |
|
|
53 | } elsif ($par->{level} > $level) { |
|
|
54 | $$rpar .= &cf::pod::as_cfpod ([$par]); |
|
|
55 | } |
|
|
56 | |
|
|
57 | cf::cede_to_tick; |
|
|
58 | } |
|
|
59 | } |
|
|
60 | |
|
|
61 | @command_list = sort { |
|
|
62 | $a->[0] <=> $b->[0] |
|
|
63 | or $a->[1] cmp $b->[1] |
|
|
64 | } @command_list; |
|
|
65 | |
|
|
66 | cf::cede_to_tick; |
|
|
67 | |
|
|
68 | cf::set_face "res/command_list" => cf::FT_RSRC, |
|
|
69 | JSON::XS->new->utf8->encode (\@command_list); |
|
|
70 | } |
|
|
71 | |
|
|
72 | our $DOCLET_HANDLER = ext::doclet::register command => sub { |
|
|
73 | my ($pl, $category, $command) = @_; |
|
|
74 | |
|
|
75 | my $guard = cf::lock_acquire "ext::help::loading"; |
|
|
76 | |
|
|
77 | $DOCLET{$command} |
|
|
78 | || "B<No documentation available for '$category/$command'>" |
|
|
79 | }; |
|
|
80 | |
|
|
81 | sub load_topics($$) { |
|
|
82 | my ($type, $path) = @_; |
|
|
83 | |
|
|
84 | my $paragraphs = cf::pod::load_pod "$PODDIR/$path.pod" |
|
|
85 | or die "unable to load $path"; |
|
|
86 | |
|
|
87 | my @topics; |
|
|
88 | my $level = 1e9; |
|
|
89 | |
|
|
90 | for my $par (@$paragraphs) { |
|
|
91 | cf::cede_to_tick; |
|
|
92 | if ($par->{type} eq "head2") { |
|
|
93 | if ($par->{markup} =~ /^(\S+)/) { |
|
|
94 | push @topics, $1 => [$type => $par]; |
|
|
95 | $level = $par->{level}; |
|
|
96 | } |
|
|
97 | } elsif ($par->{level} > $level) { |
|
|
98 | push @{ $topics[-1] }, $par; |
|
|
99 | } |
|
|
100 | } |
|
|
101 | |
|
|
102 | @topics |
|
|
103 | } |
|
|
104 | |
|
|
105 | sub reload() { |
|
|
106 | my $guard1 = cf::lock_acquire "ext::help::loading"; |
|
|
107 | my $guard2 = cf::lock_acquire "ext::resource"; |
|
|
108 | |
|
|
109 | local $Coro::current->{desc} = "help loader"; |
|
|
110 | |
|
|
111 | $TOPIC = { |
|
|
112 | (load_topics "DM Commands" => "dmcommand_help"), |
|
|
113 | (load_topics "Emotes" => "emote_help"), |
|
|
114 | (load_topics "Commands" => "command_help"), |
|
|
115 | (load_topics "Generic Help Topics" => "generic_help"), |
|
|
116 | }; |
|
|
117 | |
|
|
118 | load_doclets; |
|
|
119 | |
|
|
120 | () |
|
|
121 | } |
|
|
122 | |
|
|
123 | cf::post_init { |
|
|
124 | cf::async_ext { reload }; |
|
|
125 | Coro::cede; # make sure reload acquires the lock(s) |
|
|
126 | }; |
|
|
127 | |
4 | # cf::register_command help => sub { |
128 | cf::register_command help => sub { |
5 | # my ($pl, $topic) = @_; |
129 | my ($pl, $topic) = @_; |
6 | # |
130 | |
7 | # $pl->reply (undef, "Help on $topic"); |
131 | if (cf::lock_active "ext::help::loading") { |
8 | # }; |
132 | $pl->send_msg ($HELP_CHANNEL => "help files are being loaded currently, try again in a few seconds.", cf::NDI_REPLY | cf::NDI_CLEAR); |
9 | #-------------------------------------------------- |
133 | return; |
|
|
134 | } |
|
|
135 | |
|
|
136 | $topic = $1 if $topic =~ /(\S+)/; |
|
|
137 | |
|
|
138 | if (!length $topic) { |
|
|
139 | # sort.. |
|
|
140 | |
|
|
141 | my %topics; |
|
|
142 | while (my ($k, $v) = each %$TOPIC) { |
|
|
143 | push @{$topics{$v->[0]}}, $k; |
|
|
144 | } |
|
|
145 | |
|
|
146 | my $res; |
|
|
147 | while (my ($k, $v) = each %topics) { |
|
|
148 | $res .= "T<$k:>\n\n" . (join " ", sort @$v) . "\n\n"; |
|
|
149 | } |
|
|
150 | |
|
|
151 | $pl->send_msg ($HELP_CHANNEL => $res, cf::NDI_REPLY | cf::NDI_CLEAR); |
|
|
152 | |
|
|
153 | } elsif (my $item = $TOPIC->{$topic}) { |
|
|
154 | my ($type, @pars) = @$item; |
|
|
155 | $pl->send_msg ($HELP_CHANNEL => (cf::pod::as_cfpod \@pars), cf::NDI_REPLY | cf::NDI_CLEAR); |
|
|
156 | |
|
|
157 | } else { |
|
|
158 | $pl->send_msg ($HELP_CHANNEL => "'$topic' no such help topic, try just 'help' to get a list of topics.", cf::NDI_REPLY); |
|
|
159 | } |
|
|
160 | }; |
|
|
161 | |