1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl; |
---|
5 | |
---|
6 | BEGIN { |
---|
7 | # bootstrap in C bindings and glue |
---|
8 | *owl:: = \*BarnOwl::; |
---|
9 | bootstrap BarnOwl 1.2; |
---|
10 | }; |
---|
11 | |
---|
12 | use lib(get_data_dir() . "/lib"); |
---|
13 | use lib(get_config_dir() . "/lib"); |
---|
14 | |
---|
15 | use BarnOwl::Hook; |
---|
16 | use BarnOwl::Hooks; |
---|
17 | use BarnOwl::Message; |
---|
18 | use BarnOwl::Style; |
---|
19 | use BarnOwl::Timer; |
---|
20 | |
---|
21 | =head1 NAME |
---|
22 | |
---|
23 | BarnOwl |
---|
24 | |
---|
25 | =head1 DESCRIPTION |
---|
26 | |
---|
27 | The BarnOwl module contains the core of BarnOwl's perl |
---|
28 | bindings. Source in this module is also run at startup to bootstrap |
---|
29 | barnowl by defining things like the default style. |
---|
30 | |
---|
31 | =for NOTE |
---|
32 | These following functions are defined in perlglue.xs. Keep the |
---|
33 | documentation here in sync with the user-visible commands defined |
---|
34 | there! |
---|
35 | |
---|
36 | =head2 command STRING |
---|
37 | |
---|
38 | Executes a BarnOwl command in the same manner as if the user had |
---|
39 | executed it at the BarnOwl command prompt. If the command returns a |
---|
40 | value, return it as a string, otherwise return undef. |
---|
41 | |
---|
42 | =head2 getcurmsg |
---|
43 | |
---|
44 | Returns the current message as a C<BarnOwl::Message> subclass, or |
---|
45 | undef if there is no message selected |
---|
46 | |
---|
47 | =head2 getnumcols |
---|
48 | |
---|
49 | Returns the width of the display window BarnOwl is currently using |
---|
50 | |
---|
51 | =head2 getidletime |
---|
52 | |
---|
53 | Returns the length of time since the user has pressed a key, in |
---|
54 | seconds. |
---|
55 | |
---|
56 | =head2 zephyr_getrealm |
---|
57 | |
---|
58 | Returns the zephyr realm barnowl is running in |
---|
59 | |
---|
60 | =head2 zephyr_getsender |
---|
61 | |
---|
62 | Returns the fully-qualified name of the zephyr sender barnowl is |
---|
63 | running as, e.g. C<nelhage@ATHENA.MIT.EDU> |
---|
64 | |
---|
65 | =head2 zephyr_zwrite COMMAND MESSAGE |
---|
66 | |
---|
67 | Sends a zephyr programmatically. C<COMMAND> should be a C<zwrite> |
---|
68 | command line, and C<MESSAGE> is the zephyr body to send. |
---|
69 | |
---|
70 | =head2 ztext_stylestrip STRING |
---|
71 | |
---|
72 | Strips zephyr formatting from a string and returns the result |
---|
73 | |
---|
74 | =head2 zephyr_getsubs |
---|
75 | |
---|
76 | Returns the list of subscription triples <class,instance,recipient>, |
---|
77 | separated by newlines. |
---|
78 | |
---|
79 | =head2 queue_message MESSAGE |
---|
80 | |
---|
81 | Enqueue a message in the BarnOwl message list, logging it and |
---|
82 | processing it appropriately. C<MESSAGE> should be an instance of |
---|
83 | BarnOwl::Message or a subclass. |
---|
84 | |
---|
85 | =head2 admin_message HEADER BODY |
---|
86 | |
---|
87 | Display a BarnOwl B<Admin> message, with the given header and body. |
---|
88 | |
---|
89 | =head2 start_question PROMPT CALLBACK |
---|
90 | |
---|
91 | Displays C<PROMPT> on the screen and lets the user enter a line of |
---|
92 | text, and calls C<CALLBACK>, which must be a perl subroutine |
---|
93 | reference, with the text the user entered |
---|
94 | |
---|
95 | =head2 start_password PROMPT CALLBACK |
---|
96 | |
---|
97 | Like C<start_question>, but echoes the user's input as C<*>s when they |
---|
98 | input. |
---|
99 | |
---|
100 | =head2 start_edit_win PROMPT CALLBACK |
---|
101 | |
---|
102 | Like C<start_question>, but displays C<PROMPT> on a line of its own |
---|
103 | and opens the editwin. If the user cancels the edit win, C<CALLBACK> |
---|
104 | is not invoked. |
---|
105 | |
---|
106 | =head2 get_data_dir |
---|
107 | |
---|
108 | Returns the BarnOwl system data directory, where system libraries and |
---|
109 | modules are stored |
---|
110 | |
---|
111 | =head2 get_config_dir |
---|
112 | |
---|
113 | Returns the BarnOwl user configuration directory, where user modules |
---|
114 | and configuration are stored (by default, C<$HOME/.owl>) |
---|
115 | |
---|
116 | =head2 popless_text TEXT |
---|
117 | |
---|
118 | Show a popup window containing the given C<TEXT> |
---|
119 | |
---|
120 | =head2 popless_ztext TEXT |
---|
121 | |
---|
122 | Show a popup window containing the provided zephyr-formatted C<TEXT> |
---|
123 | |
---|
124 | =head2 error STRING |
---|
125 | |
---|
126 | Reports an error and log it in `show errors'. Note that in any |
---|
127 | callback or hook called in perl code from BarnOwl, a C<die> will be |
---|
128 | caught and passed to C<error>. |
---|
129 | |
---|
130 | =head2 getnumcolors |
---|
131 | |
---|
132 | Returns the number of colors this BarnOwl is capable of displaying |
---|
133 | |
---|
134 | =head2 add_dispatch FD CALLBACK |
---|
135 | |
---|
136 | Adds a file descriptor to C<BarnOwl>'s internal C<select()> |
---|
137 | loop. C<CALLBACK> will be invoked whenever data is available to be |
---|
138 | read from C<FD>. |
---|
139 | |
---|
140 | =head2 remove_dispatch FD |
---|
141 | |
---|
142 | Remove a file descriptor previously registered via C<add_dispatch> |
---|
143 | |
---|
144 | =head2 create_style NAME OBJECT |
---|
145 | |
---|
146 | Creates a new barnowl style with the given NAME defined by the given |
---|
147 | object. The object must have a C<description> method which returns a |
---|
148 | string description of the style, and a and C<format_message> method |
---|
149 | which accepts a C<BarnOwl::Message> object and returns a string that |
---|
150 | is the result of formatting the message for display. |
---|
151 | |
---|
152 | =cut |
---|
153 | |
---|
154 | # perlconfig.c will set this to the value of the -c command-line |
---|
155 | # switch, if present. |
---|
156 | our $configfile; |
---|
157 | |
---|
158 | if(!$configfile && -f $ENV{HOME} . "/.barnowlconf") { |
---|
159 | $configfile = $ENV{HOME} . "/.barnowlconf"; |
---|
160 | } |
---|
161 | $configfile ||= $ENV{HOME}."/.owlconf"; |
---|
162 | |
---|
163 | # populate global variable space for legacy owlconf files |
---|
164 | sub _receive_msg_legacy_wrap { |
---|
165 | my ($m) = @_; |
---|
166 | $m->legacy_populate_global(); |
---|
167 | return &BarnOwl::Hooks::_receive_msg($m); |
---|
168 | } |
---|
169 | |
---|
170 | =head2 AUTOLOAD |
---|
171 | |
---|
172 | BarnOwl.pm has a C<AUTOLOAD> method that translates unused names in |
---|
173 | the BarnOwl:: namespace to a call to BarnOwl::command() with that |
---|
174 | command. Underscores are also translated to C<->s, so you can do |
---|
175 | e.g. C<BarnOwl::start_command()> and it will be translated into |
---|
176 | C<start-command>. |
---|
177 | |
---|
178 | So, if you're looking for functionality that you can't find in the |
---|
179 | perl interface, check C<:show commands> or C<commands.c> in the |
---|
180 | BarnOwl source tree -- there's a good chance it exists as a BarnOwl |
---|
181 | command. |
---|
182 | |
---|
183 | =head3 BUGS |
---|
184 | |
---|
185 | There are horrible quoting issues here. The AUTOLOAD simple joins your |
---|
186 | commands with spaces and passes them unmodified to C<::command> |
---|
187 | |
---|
188 | =cut |
---|
189 | |
---|
190 | # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo"); |
---|
191 | sub AUTOLOAD { |
---|
192 | our $AUTOLOAD; |
---|
193 | my $called = $AUTOLOAD; |
---|
194 | $called =~ s/.*:://; |
---|
195 | $called =~ s/_/-/g; |
---|
196 | return &BarnOwl::command("$called ".join(" ",@_)); |
---|
197 | } |
---|
198 | |
---|
199 | =head2 new_command NAME FUNC [{ARGS}] |
---|
200 | |
---|
201 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
202 | be called with the arguments passed to the command, with NAME as the |
---|
203 | first argument. |
---|
204 | |
---|
205 | ARGS should be a hashref containing any or all of C<summary>, |
---|
206 | C<usage>, or C<description> keys: |
---|
207 | |
---|
208 | =over 4 |
---|
209 | |
---|
210 | =item summary |
---|
211 | |
---|
212 | A one-line summary of the purpose of the command |
---|
213 | |
---|
214 | =item usage |
---|
215 | |
---|
216 | A one-line usage synopsis, showing available options and syntax |
---|
217 | |
---|
218 | =item description |
---|
219 | |
---|
220 | A longer description of the syntax and semantics of the command, |
---|
221 | explaining usage and options |
---|
222 | |
---|
223 | =back |
---|
224 | |
---|
225 | =cut |
---|
226 | |
---|
227 | sub new_command { |
---|
228 | my $name = shift; |
---|
229 | my $func = shift; |
---|
230 | my $args = shift || {}; |
---|
231 | my %args = ( |
---|
232 | summary => "", |
---|
233 | usage => "", |
---|
234 | description => "", |
---|
235 | %{$args} |
---|
236 | ); |
---|
237 | |
---|
238 | BarnOwl::Internal::new_command($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
239 | } |
---|
240 | |
---|
241 | =head2 new_variable_int NAME [{ARGS}] |
---|
242 | |
---|
243 | =head2 new_variable_bool NAME [{ARGS}] |
---|
244 | |
---|
245 | =head2 new_variable_string NAME [{ARGS}] |
---|
246 | |
---|
247 | Add a new owl variable, either an int, a bool, or a string, with the |
---|
248 | specified name. |
---|
249 | |
---|
250 | ARGS can optionally contain the following keys: |
---|
251 | |
---|
252 | =over 4 |
---|
253 | |
---|
254 | =item default |
---|
255 | |
---|
256 | The default and initial value for the variable |
---|
257 | |
---|
258 | =item summary |
---|
259 | |
---|
260 | A one-line summary of the variable's purpose |
---|
261 | |
---|
262 | =item description |
---|
263 | |
---|
264 | A longer description of the function of the variable |
---|
265 | |
---|
266 | =back |
---|
267 | |
---|
268 | =cut |
---|
269 | |
---|
270 | sub new_variable_int { |
---|
271 | unshift @_, \&BarnOwl::Internal::new_variable_int, 0; |
---|
272 | goto \&_new_variable; |
---|
273 | } |
---|
274 | |
---|
275 | sub new_variable_bool { |
---|
276 | unshift @_, \&BarnOwl::Internal::new_variable_bool, 0; |
---|
277 | goto \&_new_variable; |
---|
278 | } |
---|
279 | |
---|
280 | sub new_variable_string { |
---|
281 | unshift @_, \&BarnOwl::Internal::new_variable_string, ""; |
---|
282 | goto \&_new_variable; |
---|
283 | } |
---|
284 | |
---|
285 | sub _new_variable { |
---|
286 | my $func = shift; |
---|
287 | my $default_default = shift; |
---|
288 | my $name = shift; |
---|
289 | my $args = shift || {}; |
---|
290 | my %args = ( |
---|
291 | summary => "", |
---|
292 | description => "", |
---|
293 | default => $default_default, |
---|
294 | %{$args}); |
---|
295 | $func->($name, $args{default}, $args{summary}, $args{description}); |
---|
296 | } |
---|
297 | |
---|
298 | =head2 quote STRING |
---|
299 | |
---|
300 | Return a version of STRING fully quoted to survive processing by |
---|
301 | BarnOwl's command parser. |
---|
302 | |
---|
303 | =cut |
---|
304 | |
---|
305 | sub quote { |
---|
306 | my $str = shift; |
---|
307 | return "''" if $str eq ''; |
---|
308 | if ($str !~ /['" ]/) { |
---|
309 | return "$str"; |
---|
310 | } |
---|
311 | if ($str !~ /'/) { |
---|
312 | return "'$str'"; |
---|
313 | } |
---|
314 | $str =~ s/"/"'"'"/g; |
---|
315 | return '"' . $str . '"'; |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | 1; |
---|