[ee183be] | 1 | use strict; |
---|
| 2 | use warnings; |
---|
| 3 | |
---|
| 4 | package BarnOwl; |
---|
| 5 | |
---|
[2be605a] | 6 | use base qw(Exporter); |
---|
[b303ba2] | 7 | our @EXPORT_OK = qw(command getcurmsg getnumcols getnumlines getidletime |
---|
[ecd4edf] | 8 | register_idle_watcher unregister_idle_watcher |
---|
[2be605a] | 9 | zephyr_getsender zephyr_getrealm zephyr_zwrite |
---|
| 10 | zephyr_stylestrip zephyr_smartstrip_user zephyr_getsubs |
---|
| 11 | queue_message admin_message |
---|
[e89ec48] | 12 | start_edit |
---|
[2be605a] | 13 | start_question start_password start_edit_win |
---|
| 14 | get_data_dir get_config_dir popless_text popless_ztext |
---|
[eede1bf] | 15 | error debug |
---|
[2be605a] | 16 | create_style getnumcolors wordwrap |
---|
[1ced34f] | 17 | message_matches_filter |
---|
[ffc4df6] | 18 | add_dispatch remove_dispatch |
---|
| 19 | add_io_dispatch remove_io_dispatch |
---|
[2be605a] | 20 | new_command |
---|
| 21 | new_variable_int new_variable_bool new_variable_string |
---|
[e26abb6] | 22 | new_variable_enum |
---|
[4df918b] | 23 | quote redisplay); |
---|
[2be605a] | 24 | our %EXPORT_TAGS = (all => \@EXPORT_OK); |
---|
| 25 | |
---|
[fd8dfe7] | 26 | BEGIN { |
---|
| 27 | # bootstrap in C bindings and glue |
---|
| 28 | *owl:: = \*BarnOwl::; |
---|
| 29 | bootstrap BarnOwl 1.2; |
---|
| 30 | }; |
---|
| 31 | |
---|
| 32 | use lib(get_data_dir() . "/lib"); |
---|
| 33 | use lib(get_config_dir() . "/lib"); |
---|
| 34 | |
---|
[9179fd7] | 35 | use Glib; |
---|
| 36 | use AnyEvent; |
---|
| 37 | |
---|
[fd8dfe7] | 38 | use BarnOwl::Hook; |
---|
| 39 | use BarnOwl::Hooks; |
---|
[978a164] | 40 | use BarnOwl::Logging; |
---|
[fd8dfe7] | 41 | use BarnOwl::Message; |
---|
| 42 | use BarnOwl::Style; |
---|
[df569c5] | 43 | use BarnOwl::Zephyr; |
---|
[fd8dfe7] | 44 | use BarnOwl::Timer; |
---|
[cf26b72] | 45 | use BarnOwl::Editwin; |
---|
[8eac1a5] | 46 | use BarnOwl::Completion; |
---|
[b30c256] | 47 | use BarnOwl::Help; |
---|
[9d6e37c] | 48 | use BarnOwl::DeferredLogging; |
---|
[fd8dfe7] | 49 | |
---|
[6700c605] | 50 | use List::Util qw(max); |
---|
[ecd4edf] | 51 | use Tie::RefHash; |
---|
[6700c605] | 52 | |
---|
[ee183be] | 53 | =head1 NAME |
---|
| 54 | |
---|
| 55 | BarnOwl |
---|
| 56 | |
---|
| 57 | =head1 DESCRIPTION |
---|
| 58 | |
---|
| 59 | The BarnOwl module contains the core of BarnOwl's perl |
---|
| 60 | bindings. Source in this module is also run at startup to bootstrap |
---|
[b8a3e00] | 61 | BarnOwl by defining things like the default style. |
---|
[ee183be] | 62 | |
---|
| 63 | =for NOTE |
---|
| 64 | These following functions are defined in perlglue.xs. Keep the |
---|
| 65 | documentation here in sync with the user-visible commands defined |
---|
| 66 | there! |
---|
| 67 | |
---|
| 68 | =head2 command STRING |
---|
| 69 | |
---|
| 70 | Executes a BarnOwl command in the same manner as if the user had |
---|
| 71 | executed it at the BarnOwl command prompt. If the command returns a |
---|
| 72 | value, return it as a string, otherwise return undef. |
---|
| 73 | |
---|
| 74 | =head2 getcurmsg |
---|
| 75 | |
---|
| 76 | Returns the current message as a C<BarnOwl::Message> subclass, or |
---|
| 77 | undef if there is no message selected |
---|
| 78 | =head2 getnumcols |
---|
| 79 | |
---|
| 80 | Returns the width of the display window BarnOwl is currently using |
---|
| 81 | |
---|
| 82 | =head2 getidletime |
---|
| 83 | |
---|
| 84 | Returns the length of time since the user has pressed a key, in |
---|
| 85 | seconds. |
---|
| 86 | |
---|
| 87 | =head2 zephyr_getrealm |
---|
| 88 | |
---|
[b8a3e00] | 89 | Returns the zephyr realm BarnOwl is running in |
---|
[ee183be] | 90 | |
---|
| 91 | =head2 zephyr_getsender |
---|
| 92 | |
---|
[b8a3e00] | 93 | Returns the fully-qualified name of the zephyr sender BarnOwl is |
---|
[ee183be] | 94 | running as, e.g. C<nelhage@ATHENA.MIT.EDU> |
---|
| 95 | |
---|
| 96 | =head2 zephyr_zwrite COMMAND MESSAGE |
---|
| 97 | |
---|
| 98 | Sends a zephyr programmatically. C<COMMAND> should be a C<zwrite> |
---|
| 99 | command line, and C<MESSAGE> is the zephyr body to send. |
---|
| 100 | |
---|
[374089a] | 101 | =cut |
---|
| 102 | |
---|
| 103 | sub zephyr_zwrite { |
---|
| 104 | my ($command, $message) = @_; |
---|
| 105 | my $ret = BarnOwl::Internal::zephyr_zwrite($command, $message); |
---|
| 106 | die "Error sending zephyr" unless $ret == 0; |
---|
| 107 | } |
---|
| 108 | |
---|
[ee183be] | 109 | =head2 ztext_stylestrip STRING |
---|
| 110 | |
---|
| 111 | Strips zephyr formatting from a string and returns the result |
---|
| 112 | |
---|
| 113 | =head2 zephyr_getsubs |
---|
| 114 | |
---|
| 115 | Returns the list of subscription triples <class,instance,recipient>, |
---|
| 116 | separated by newlines. |
---|
| 117 | |
---|
| 118 | =head2 queue_message MESSAGE |
---|
| 119 | |
---|
| 120 | Enqueue a message in the BarnOwl message list, logging it and |
---|
| 121 | processing it appropriately. C<MESSAGE> should be an instance of |
---|
[e89ec48] | 122 | BarnOwl::Message or a subclass. |
---|
[ee183be] | 123 | |
---|
| 124 | =head2 admin_message HEADER BODY |
---|
| 125 | |
---|
| 126 | Display a BarnOwl B<Admin> message, with the given header and body. |
---|
| 127 | |
---|
[e89ec48] | 128 | =head2 start_edit %ARGS |
---|
[ee183be] | 129 | |
---|
[e89ec48] | 130 | Displays a prompt on the screen and lets the user enter text, |
---|
| 131 | and calls a callback when the editwin is closed. |
---|
[ee183be] | 132 | |
---|
[e89ec48] | 133 | C<%ARGS> must contain the following keys: |
---|
| 134 | |
---|
| 135 | =over 4 |
---|
| 136 | |
---|
| 137 | =item prompt |
---|
| 138 | |
---|
| 139 | The line to display on the screen |
---|
| 140 | |
---|
| 141 | =item type |
---|
| 142 | |
---|
| 143 | One of: |
---|
| 144 | |
---|
| 145 | =over 4 |
---|
| 146 | |
---|
| 147 | =item edit_win |
---|
| 148 | |
---|
| 149 | Displays the prompt on a line of its own and opens the edit_win. |
---|
| 150 | |
---|
| 151 | =item question |
---|
| 152 | |
---|
| 153 | Displays prompt on the screen and lets the user enter a line of |
---|
| 154 | text. |
---|
| 155 | |
---|
| 156 | =item password |
---|
[ee183be] | 157 | |
---|
[e89ec48] | 158 | Like question, but echoes the user's input as C<*>s when they |
---|
[ee183be] | 159 | input. |
---|
| 160 | |
---|
[e89ec48] | 161 | =back |
---|
| 162 | |
---|
| 163 | =item callback |
---|
| 164 | |
---|
| 165 | A Perl subroutine that is called when the user closes the edit_win. |
---|
[7803326] | 166 | C<CALLBACK> gets called with two parameters: the text the user entered, |
---|
| 167 | and a C<SUCCESS> boolean parameter which is false if the user canceled |
---|
| 168 | the edit_win and true otherwise. |
---|
[e89ec48] | 169 | |
---|
| 170 | =back |
---|
| 171 | |
---|
| 172 | =head2 start_question PROMPT CALLBACK |
---|
| 173 | |
---|
| 174 | =head2 start_password PROMPT CALLBACK |
---|
| 175 | |
---|
[ee183be] | 176 | =head2 start_edit_win PROMPT CALLBACK |
---|
| 177 | |
---|
[7803326] | 178 | Roughly equivalent to C<start_edit> called with the appropriate parameters. |
---|
| 179 | C<CALLBACK> is only called on success, for compatibility. |
---|
| 180 | |
---|
| 181 | These are deprecated wrappers around L<BarnOwl::start_edit>, and should not |
---|
| 182 | be uesd in new code. |
---|
[e89ec48] | 183 | |
---|
| 184 | =cut |
---|
| 185 | |
---|
| 186 | sub start_edit { |
---|
| 187 | my %args = (@_); |
---|
| 188 | BarnOwl::Internal::start_edit($args{type}, $args{prompt}, $args{callback}); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | sub start_question { |
---|
| 192 | my ($prompt, $callback) = @_; |
---|
[7803326] | 193 | BarnOwl::start_edit(type => 'question', prompt => $prompt, callback => sub { |
---|
| 194 | my ($text, $success) = @_; |
---|
| 195 | $callback->($text) if $success; |
---|
| 196 | }); |
---|
[e89ec48] | 197 | } |
---|
| 198 | |
---|
| 199 | sub start_password { |
---|
| 200 | my ($prompt, $callback) = @_; |
---|
[7803326] | 201 | BarnOwl::start_edit(type => 'password', prompt => $prompt, callback => sub { |
---|
| 202 | my ($text, $success) = @_; |
---|
| 203 | $callback->($text) if $success; |
---|
| 204 | }); |
---|
[e89ec48] | 205 | } |
---|
| 206 | |
---|
| 207 | sub start_edit_win { |
---|
| 208 | my ($prompt, $callback) = @_; |
---|
[7803326] | 209 | BarnOwl::start_edit(type => 'edit_win', prompt => $prompt, callback => sub { |
---|
| 210 | my ($text, $success) = @_; |
---|
| 211 | $callback->($text) if $success; |
---|
| 212 | }); |
---|
[e89ec48] | 213 | } |
---|
[ee183be] | 214 | |
---|
| 215 | =head2 get_data_dir |
---|
| 216 | |
---|
| 217 | Returns the BarnOwl system data directory, where system libraries and |
---|
| 218 | modules are stored |
---|
| 219 | |
---|
| 220 | =head2 get_config_dir |
---|
| 221 | |
---|
| 222 | Returns the BarnOwl user configuration directory, where user modules |
---|
| 223 | and configuration are stored (by default, C<$HOME/.owl>) |
---|
| 224 | |
---|
| 225 | =head2 popless_text TEXT |
---|
| 226 | |
---|
| 227 | Show a popup window containing the given C<TEXT> |
---|
| 228 | |
---|
| 229 | =head2 popless_ztext TEXT |
---|
| 230 | |
---|
| 231 | Show a popup window containing the provided zephyr-formatted C<TEXT> |
---|
| 232 | |
---|
| 233 | =head2 error STRING |
---|
| 234 | |
---|
| 235 | Reports an error and log it in `show errors'. Note that in any |
---|
| 236 | callback or hook called in perl code from BarnOwl, a C<die> will be |
---|
| 237 | caught and passed to C<error>. |
---|
| 238 | |
---|
[eede1bf] | 239 | =head2 debug STRING |
---|
| 240 | |
---|
| 241 | Logs a debugging message to BarnOwl's debug log |
---|
| 242 | |
---|
[ee183be] | 243 | =head2 getnumcolors |
---|
| 244 | |
---|
| 245 | Returns the number of colors this BarnOwl is capable of displaying |
---|
| 246 | |
---|
[1ced34f] | 247 | =head2 message_matches_filter MESSAGE FILTER_NAME [QUIET = 0] |
---|
| 248 | |
---|
| 249 | Returns 1 if C<FILTER_NAME> is the name of a valid filter, and |
---|
| 250 | C<MESSAGE> matches that filter. Returns 0 otherwise. If |
---|
| 251 | C<QUIET> is false, this method displays an error message if |
---|
| 252 | if C<FILTER_NAME> does not name a valid filter. |
---|
| 253 | |
---|
[ee183be] | 254 | =head2 add_dispatch FD CALLBACK |
---|
| 255 | |
---|
| 256 | Adds a file descriptor to C<BarnOwl>'s internal C<select()> |
---|
| 257 | loop. C<CALLBACK> will be invoked whenever data is available to be |
---|
| 258 | read from C<FD>. |
---|
| 259 | |
---|
[bcde7926] | 260 | C<add_dispatch> has been deprecated in favor of C<AnyEvent>, and is |
---|
| 261 | now a wrapper for C<add_io_dispatch> called with C<mode> set to |
---|
| 262 | C<'r'>. |
---|
[ffc4df6] | 263 | |
---|
| 264 | =cut |
---|
| 265 | |
---|
| 266 | sub add_dispatch { |
---|
| 267 | my $fd = shift; |
---|
| 268 | my $cb = shift; |
---|
| 269 | add_io_dispatch($fd, 'r', $cb); |
---|
| 270 | } |
---|
| 271 | |
---|
[ee183be] | 272 | =head2 remove_dispatch FD |
---|
| 273 | |
---|
| 274 | Remove a file descriptor previously registered via C<add_dispatch> |
---|
| 275 | |
---|
[bcde7926] | 276 | C<remove_dispatch> has been deprecated in favor of C<AnyEvent>. |
---|
[ffc4df6] | 277 | |
---|
| 278 | =cut |
---|
| 279 | |
---|
| 280 | *remove_dispatch = \&remove_io_dispatch; |
---|
| 281 | |
---|
| 282 | =head2 add_io_dispatch FD MODE CB |
---|
| 283 | |
---|
| 284 | Adds a file descriptor to C<BarnOwl>'s internal C<select()> |
---|
| 285 | loop. <MODE> can be 'r', 'w', or 'rw'. C<CALLBACK> will be invoked |
---|
| 286 | whenever C<FD> becomes ready, as specified by <MODE>. |
---|
| 287 | |
---|
| 288 | Only one callback can be registered per FD. If a new callback is |
---|
| 289 | registered, the old one is removed. |
---|
| 290 | |
---|
[bcde7926] | 291 | C<add_io_dispatch> has been deprecated in favor of C<AnyEvent>. |
---|
| 292 | |
---|
[ffc4df6] | 293 | =cut |
---|
| 294 | |
---|
[bcde7926] | 295 | our %_io_dispatches; |
---|
| 296 | |
---|
[ffc4df6] | 297 | sub add_io_dispatch { |
---|
| 298 | my $fd = shift; |
---|
| 299 | my $modeStr = shift; |
---|
| 300 | my $cb = shift; |
---|
[bcde7926] | 301 | my @modes; |
---|
| 302 | |
---|
| 303 | push @modes, 'r' if $modeStr =~ /r/i; # Read |
---|
| 304 | push @modes, 'w' if $modeStr =~ /w/i; # Write |
---|
| 305 | if (@modes) { |
---|
| 306 | BarnOwl::remove_io_dispatch($fd); |
---|
| 307 | for my $mode (@modes) { |
---|
| 308 | push @{$_io_dispatches{$fd}}, AnyEvent->io(fh => $fd, |
---|
| 309 | poll => $mode, |
---|
| 310 | cb => $cb); |
---|
| 311 | } |
---|
[ffc4df6] | 312 | } else { |
---|
| 313 | die("Invalid I/O Dispatch mode: $modeStr"); |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | =head2 remove_io_dispatch FD |
---|
| 318 | |
---|
| 319 | Remove a file descriptor previously registered via C<add_io_dispatch> |
---|
| 320 | |
---|
[bcde7926] | 321 | C<remove_io_dispatch> has been deprecated in favor of C<AnyEvent>. |
---|
| 322 | |
---|
| 323 | =cut |
---|
| 324 | |
---|
| 325 | sub remove_io_dispatch { |
---|
| 326 | my $fd = shift; |
---|
| 327 | undef $_ foreach @{$_io_dispatches{$fd}}; |
---|
| 328 | delete $_io_dispatches{$fd}; |
---|
| 329 | } |
---|
| 330 | |
---|
[ee183be] | 331 | =head2 create_style NAME OBJECT |
---|
| 332 | |
---|
[b8a3e00] | 333 | Creates a new BarnOwl style with the given NAME defined by the given |
---|
[ee183be] | 334 | object. The object must have a C<description> method which returns a |
---|
| 335 | string description of the style, and a and C<format_message> method |
---|
| 336 | which accepts a C<BarnOwl::Message> object and returns a string that |
---|
| 337 | is the result of formatting the message for display. |
---|
| 338 | |
---|
[4df918b] | 339 | =head2 redisplay |
---|
| 340 | |
---|
| 341 | Redraw all of the messages on screen. This is useful if you've just |
---|
| 342 | changed how a style renders messages. |
---|
| 343 | |
---|
[ee183be] | 344 | =cut |
---|
| 345 | |
---|
| 346 | # perlconfig.c will set this to the value of the -c command-line |
---|
| 347 | # switch, if present. |
---|
| 348 | our $configfile; |
---|
| 349 | |
---|
[d7bcff8] | 350 | our @all_commands; |
---|
| 351 | |
---|
[8135737] | 352 | if(!$configfile) { |
---|
| 353 | if (-f get_config_dir() . "/init.pl") { |
---|
| 354 | $configfile = get_config_dir() . "/init.pl"; |
---|
| 355 | } elsif (-f $ENV{HOME} . "/.barnowlconf") { |
---|
| 356 | $configfile = $ENV{HOME} . "/.barnowlconf"; |
---|
| 357 | } else { |
---|
| 358 | $configfile = $ENV{HOME}."/.owlconf"; |
---|
| 359 | } |
---|
[ee183be] | 360 | } |
---|
| 361 | |
---|
| 362 | # populate global variable space for legacy owlconf files |
---|
| 363 | sub _receive_msg_legacy_wrap { |
---|
| 364 | my ($m) = @_; |
---|
| 365 | $m->legacy_populate_global(); |
---|
| 366 | return &BarnOwl::Hooks::_receive_msg($m); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | =head2 new_command NAME FUNC [{ARGS}] |
---|
| 370 | |
---|
| 371 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
| 372 | be called with the arguments passed to the command, with NAME as the |
---|
| 373 | first argument. |
---|
| 374 | |
---|
| 375 | ARGS should be a hashref containing any or all of C<summary>, |
---|
| 376 | C<usage>, or C<description> keys: |
---|
| 377 | |
---|
| 378 | =over 4 |
---|
| 379 | |
---|
| 380 | =item summary |
---|
| 381 | |
---|
| 382 | A one-line summary of the purpose of the command |
---|
| 383 | |
---|
| 384 | =item usage |
---|
| 385 | |
---|
| 386 | A one-line usage synopsis, showing available options and syntax |
---|
| 387 | |
---|
| 388 | =item description |
---|
| 389 | |
---|
| 390 | A longer description of the syntax and semantics of the command, |
---|
| 391 | explaining usage and options |
---|
| 392 | |
---|
| 393 | =back |
---|
| 394 | |
---|
| 395 | =cut |
---|
| 396 | |
---|
| 397 | sub new_command { |
---|
| 398 | my $name = shift; |
---|
| 399 | my $func = shift; |
---|
| 400 | my $args = shift || {}; |
---|
| 401 | my %args = ( |
---|
| 402 | summary => "", |
---|
| 403 | usage => "", |
---|
| 404 | description => "", |
---|
| 405 | %{$args} |
---|
| 406 | ); |
---|
| 407 | |
---|
| 408 | BarnOwl::Internal::new_command($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | =head2 new_variable_int NAME [{ARGS}] |
---|
| 412 | |
---|
| 413 | =head2 new_variable_bool NAME [{ARGS}] |
---|
| 414 | |
---|
| 415 | =head2 new_variable_string NAME [{ARGS}] |
---|
| 416 | |
---|
[e26abb6] | 417 | =head2 new_variable_enum NAME [{ARGS}] |
---|
| 418 | |
---|
| 419 | Add a new owl variable, either an int, a bool, a string, or an enum with the |
---|
[ee183be] | 420 | specified name. |
---|
| 421 | |
---|
[e26abb6] | 422 | For new_variable_enum, ARGS is required to contain a validsettings key pointing |
---|
| 423 | to an array reference. For all four, it can optionally contain the following |
---|
| 424 | keys: |
---|
[ee183be] | 425 | |
---|
| 426 | =over 4 |
---|
| 427 | |
---|
| 428 | =item default |
---|
| 429 | |
---|
| 430 | The default and initial value for the variable |
---|
| 431 | |
---|
| 432 | =item summary |
---|
| 433 | |
---|
| 434 | A one-line summary of the variable's purpose |
---|
| 435 | |
---|
| 436 | =item description |
---|
| 437 | |
---|
| 438 | A longer description of the function of the variable |
---|
| 439 | |
---|
| 440 | =back |
---|
| 441 | |
---|
[104a4eb] | 442 | In addition, new_variable_string optionally accepts a string validsettings |
---|
| 443 | parameter, in case people want to set it to "<path>". |
---|
| 444 | |
---|
[ee183be] | 445 | =cut |
---|
| 446 | |
---|
| 447 | sub new_variable_int { |
---|
[523146b] | 448 | my ($name, $args) = @_; |
---|
| 449 | my $storage = defined($args->{default}) ? $args->{default} : 0; |
---|
| 450 | BarnOwl::new_variable_full($name, { |
---|
| 451 | %{$args}, |
---|
| 452 | get_tostring => sub { "$storage" }, |
---|
| 453 | set_fromstring => sub { |
---|
[4953c44] | 454 | die "Expected integer" unless $_[0] =~ /^-?[0-9]+$/; |
---|
[523146b] | 455 | $storage = 0 + $_[0]; |
---|
| 456 | }, |
---|
| 457 | validsettings => "<int>", |
---|
| 458 | takes_on_off => 0, |
---|
| 459 | }); |
---|
[ee183be] | 460 | } |
---|
| 461 | |
---|
| 462 | sub new_variable_bool { |
---|
[523146b] | 463 | my ($name, $args) = @_; |
---|
| 464 | my $storage = defined($args->{default}) ? $args->{default} : 0; |
---|
| 465 | BarnOwl::new_variable_full($name, { |
---|
| 466 | %{$args}, |
---|
| 467 | get_tostring => sub { $storage ? "on" : "off" }, |
---|
| 468 | set_fromstring => sub { |
---|
[4953c44] | 469 | die "Valid settings are on/off" unless $_[0] eq "on" || $_[0] eq "off"; |
---|
[523146b] | 470 | $storage = $_[0] eq "on"; |
---|
| 471 | }, |
---|
| 472 | validsettings => "on,off", |
---|
| 473 | takes_on_off => 1, |
---|
| 474 | }); |
---|
[ee183be] | 475 | } |
---|
| 476 | |
---|
| 477 | sub new_variable_string { |
---|
[523146b] | 478 | my ($name, $args) = @_; |
---|
| 479 | my $storage = defined($args->{default}) ? $args->{default} : ""; |
---|
| 480 | BarnOwl::new_variable_full($name, { |
---|
[104a4eb] | 481 | # Allow people to override this one if they /reaaally/ want to for |
---|
| 482 | # some reason. Though we still reserve the right to interpret this |
---|
| 483 | # value in interesting ways for tab-completion purposes. |
---|
| 484 | validsettings => "<string>", |
---|
[523146b] | 485 | %{$args}, |
---|
| 486 | get_tostring => sub { $storage }, |
---|
[4953c44] | 487 | set_fromstring => sub { $storage = $_[0]; }, |
---|
[523146b] | 488 | takes_on_off => 0, |
---|
| 489 | }); |
---|
[ee183be] | 490 | } |
---|
| 491 | |
---|
[e26abb6] | 492 | sub new_variable_enum { |
---|
| 493 | my ($name, $args) = @_; |
---|
| 494 | |
---|
| 495 | # Gather the valid settings. |
---|
| 496 | die "validsettings is required" unless defined($args->{validsettings}); |
---|
| 497 | my %valid; |
---|
| 498 | map { $valid{$_} = 1 } @{$args->{validsettings}}; |
---|
| 499 | |
---|
| 500 | my $storage = (defined($args->{default}) ? |
---|
| 501 | $args->{default} : |
---|
| 502 | $args->{validsettings}->[0]); |
---|
| 503 | BarnOwl::new_variable_full($name, { |
---|
| 504 | %{$args}, |
---|
| 505 | get_tostring => sub { $storage }, |
---|
| 506 | set_fromstring => sub { |
---|
[4953c44] | 507 | die "Invalid input" unless $valid{$_[0]}; |
---|
[e26abb6] | 508 | $storage = $_[0]; |
---|
| 509 | }, |
---|
| 510 | validsettings => join(",", @{$args->{validsettings}}) |
---|
| 511 | }); |
---|
| 512 | } |
---|
| 513 | |
---|
[523146b] | 514 | =head2 new_variable_full NAME {ARGS} |
---|
| 515 | |
---|
| 516 | Create a variable, in full generality. The keyword arguments have types below: |
---|
| 517 | |
---|
| 518 | get_tostring : () -> string |
---|
| 519 | set_fromstring : string -> int |
---|
| 520 | -- optional -- |
---|
| 521 | summary : string |
---|
| 522 | description : string |
---|
| 523 | validsettings : string |
---|
| 524 | takes_on_off : int |
---|
[69f74c2] | 525 | |
---|
[523146b] | 526 | The get/set functions are required. Note that the caller manages storage for the |
---|
| 527 | variable. get_tostring/set_fromstring both convert AND store the value. |
---|
[4953c44] | 528 | set_fromstring dies on failure. |
---|
[523146b] | 529 | |
---|
| 530 | If the variable takes parameters 'on' and 'off' (i.e. is boolean-looking), set |
---|
| 531 | takes_on_off to 1. This makes :set VAR and :unset VAR work. set_fromstring will |
---|
| 532 | be called with those arguments. |
---|
| 533 | |
---|
| 534 | =cut |
---|
| 535 | |
---|
| 536 | sub new_variable_full { |
---|
[ee183be] | 537 | my $name = shift; |
---|
| 538 | my $args = shift || {}; |
---|
| 539 | my %args = ( |
---|
[523146b] | 540 | summary => "", |
---|
[ee183be] | 541 | description => "", |
---|
[523146b] | 542 | takes_on_off => 0, |
---|
| 543 | validsettings => "<string>", |
---|
[ee183be] | 544 | %{$args}); |
---|
[69f74c2] | 545 | |
---|
[523146b] | 546 | die "get_tostring required" unless $args{get_tostring}; |
---|
| 547 | die "set_fromstring required" unless $args{set_fromstring}; |
---|
[69f74c2] | 548 | |
---|
[523146b] | 549 | # Strip off the bogus dummy argument. Aargh perl-Glib. |
---|
| 550 | my $get_tostring_fn = sub { $args{get_tostring}->() }; |
---|
[69f74c2] | 551 | my $set_fromstring_fn = sub { |
---|
[523146b] | 552 | my ($dummy, $val) = @_; |
---|
[4953c44] | 553 | # Translate from user-supplied die-on-failure callback to expected |
---|
| 554 | # non-zero on error. Less of a nuisance than interacting with ERRSV. |
---|
| 555 | eval { $args{set_fromstring}->($val) }; |
---|
| 556 | # TODO: Consider changing B::I::new_variable to expect string|NULL with |
---|
| 557 | # string as the error message. That can then be translated to a GError in |
---|
| 558 | # owl_variable_set_fromstring. For now the string is ignored. |
---|
| 559 | return ($@ ? -1 : 0); |
---|
[69f74c2] | 560 | }; |
---|
| 561 | |
---|
[523146b] | 562 | BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $args{validsettings}, |
---|
| 563 | $args{takes_on_off}, $get_tostring_fn, $set_fromstring_fn, undef); |
---|
[ee183be] | 564 | } |
---|
| 565 | |
---|
[fc92e6e2] | 566 | =head2 quote LIST |
---|
[ee183be] | 567 | |
---|
[fc92e6e2] | 568 | Quotes each of the strings in LIST and returns a string that will be |
---|
| 569 | correctly decoded to LIST by the BarnOwl command parser. For example: |
---|
| 570 | |
---|
| 571 | quote('zwrite', 'andersk', '-m', 'Hello, world!') |
---|
| 572 | # returns "zwrite andersk -m 'Hello, world!'" |
---|
[ee183be] | 573 | |
---|
| 574 | =cut |
---|
| 575 | |
---|
| 576 | sub quote { |
---|
[fc92e6e2] | 577 | my @quoted; |
---|
| 578 | for my $str (@_) { |
---|
| 579 | if ($str eq '') { |
---|
| 580 | push @quoted, "''"; |
---|
[ef4700c] | 581 | } elsif ($str !~ /['" \n\t]/) { |
---|
[fc92e6e2] | 582 | push @quoted, "$str"; |
---|
| 583 | } elsif ($str !~ /'/) { |
---|
| 584 | push @quoted, "'$str'"; |
---|
| 585 | } else { |
---|
| 586 | (my $qstr = $str) =~ s/"/"'"'"/g; |
---|
| 587 | push @quoted, '"' . $qstr . '"'; |
---|
| 588 | } |
---|
[ee183be] | 589 | } |
---|
[fc92e6e2] | 590 | return join(' ', @quoted); |
---|
[ee183be] | 591 | } |
---|
| 592 | |
---|
[22b54a7] | 593 | =head2 Modify filters by appending text |
---|
| 594 | |
---|
| 595 | =cut |
---|
| 596 | |
---|
[cff58b4] | 597 | sub register_builtin_commands { |
---|
[0b2afba] | 598 | # Filter modification |
---|
[cff58b4] | 599 | BarnOwl::new_command("filterappend", |
---|
| 600 | sub { filter_append_helper('appending', '', @_); }, |
---|
| 601 | { |
---|
| 602 | summary => "append '<text>' to filter", |
---|
| 603 | usage => "filterappend <filter> <text>", |
---|
| 604 | }); |
---|
| 605 | |
---|
| 606 | BarnOwl::new_command("filterand", |
---|
| 607 | sub { filter_append_helper('and-ing', 'and', @_); }, |
---|
| 608 | { |
---|
| 609 | summary => "append 'and <text>' to filter", |
---|
| 610 | usage => "filterand <filter> <text>", |
---|
| 611 | }); |
---|
| 612 | |
---|
| 613 | BarnOwl::new_command("filteror", |
---|
| 614 | sub { filter_append_helper('or-ing', 'or', @_); }, |
---|
| 615 | { |
---|
| 616 | summary => "append 'or <text>' to filter", |
---|
| 617 | usage => "filteror <filter> <text>", |
---|
| 618 | }); |
---|
| 619 | |
---|
[0b2afba] | 620 | # Date formatting |
---|
| 621 | BarnOwl::new_command("showdate", |
---|
| 622 | sub { BarnOwl::time_format('showdate', '%Y-%m-%d %H:%M'); }, |
---|
| 623 | { |
---|
| 624 | summary => "Show date in timestamps for supporting styles.", |
---|
| 625 | usage => "showdate", |
---|
| 626 | }); |
---|
| 627 | |
---|
| 628 | BarnOwl::new_command("hidedate", |
---|
| 629 | sub { BarnOwl::time_format('hidedate', '%H:%M'); }, |
---|
| 630 | { |
---|
| 631 | summary => "Don't show date in timestamps for supporting styles.", |
---|
| 632 | usage => "hidedate", |
---|
| 633 | }); |
---|
| 634 | |
---|
| 635 | BarnOwl::new_command("timeformat", |
---|
| 636 | \&BarnOwl::time_format, |
---|
| 637 | { |
---|
| 638 | summary => "Set the format for timestamps and re-display messages", |
---|
| 639 | usage => "timeformat <format>", |
---|
| 640 | }); |
---|
| 641 | |
---|
[6700c605] | 642 | # Receive window scrolling |
---|
| 643 | BarnOwl::new_command("recv:shiftleft", |
---|
| 644 | \&BarnOwl::recv_shift_left, |
---|
| 645 | { |
---|
| 646 | summary => "scrolls receive window to the left", |
---|
| 647 | usage => "recv:shiftleft [<amount>]", |
---|
| 648 | description => <<END_DESCR |
---|
| 649 | By default, scroll left by 10 columns. Passing no arguments or 0 activates this default behavior. |
---|
| 650 | Otherwise, scroll by the number of columns specified as the argument. |
---|
| 651 | END_DESCR |
---|
| 652 | }); |
---|
| 653 | |
---|
| 654 | BarnOwl::new_command("recv:shiftright", |
---|
| 655 | \&BarnOwl::recv_shift_right, |
---|
| 656 | { |
---|
| 657 | summary => "scrolls receive window to the right", |
---|
| 658 | usage => "recv:shiftright [<amount>]", |
---|
| 659 | description => <<END_DESCR |
---|
| 660 | By default, scroll right by 10 columns. Passing no arguments or 0 activates this default behavior. |
---|
| 661 | Otherwise, scroll by the number of columns specified as the argument. |
---|
| 662 | END_DESCR |
---|
| 663 | }); |
---|
| 664 | |
---|
[cff58b4] | 665 | } |
---|
| 666 | |
---|
| 667 | $BarnOwl::Hooks::startup->add("BarnOwl::register_builtin_commands"); |
---|
[22b54a7] | 668 | |
---|
| 669 | =head3 filter_append_helper ACTION SEP FUNC FILTER APPEND_TEXT |
---|
| 670 | |
---|
| 671 | Helper to append to filters. |
---|
| 672 | |
---|
| 673 | =cut |
---|
| 674 | |
---|
| 675 | sub filter_append_helper |
---|
| 676 | { |
---|
| 677 | my $action = shift; |
---|
| 678 | my $sep = shift; |
---|
| 679 | my $func = shift; |
---|
| 680 | my $filter = shift; |
---|
| 681 | my @append = @_; |
---|
| 682 | my $oldfilter = BarnOwl::getfilter($filter); |
---|
| 683 | chomp $oldfilter; |
---|
[0b5168d] | 684 | my $newfilter = "$oldfilter $sep " . quote(@append); |
---|
| 685 | my $msgtext = "To filter " . quote($filter) . " $action\n" . quote(@append) . "\nto get\n$newfilter"; |
---|
[22b54a7] | 686 | if (BarnOwl::getvar('showfilterchange') eq 'on') { |
---|
| 687 | BarnOwl::admin_message("Filter", $msgtext); |
---|
| 688 | } |
---|
[1b9a163] | 689 | set_filter($filter, $newfilter); |
---|
[22b54a7] | 690 | return; |
---|
| 691 | } |
---|
| 692 | BarnOwl::new_variable_bool("showfilterchange", |
---|
| 693 | { default => 1, |
---|
| 694 | summary => 'Show modifications to filters by filterappend and friends.'}); |
---|
[ee183be] | 695 | |
---|
[1b9a163] | 696 | sub set_filter |
---|
| 697 | { |
---|
| 698 | my $filtername = shift; |
---|
| 699 | my $filtertext = shift; |
---|
| 700 | my $cmd = 'filter ' . BarnOwl::quote($filtername) . ' ' . $filtertext; |
---|
| 701 | BarnOwl::command($cmd); |
---|
| 702 | } |
---|
| 703 | |
---|
[0b2afba] | 704 | =head3 time_format FORMAT |
---|
| 705 | |
---|
| 706 | Set the format for displaying times (variable timeformat) and redisplay |
---|
| 707 | messages. |
---|
| 708 | |
---|
| 709 | =cut |
---|
| 710 | |
---|
[d694c55] | 711 | my $timeformat = '%H:%M'; |
---|
[0b2afba] | 712 | |
---|
| 713 | sub time_format |
---|
| 714 | { |
---|
| 715 | my $function = shift; |
---|
| 716 | my $format = shift; |
---|
| 717 | if(!$format) |
---|
| 718 | { |
---|
| 719 | return $timeformat; |
---|
| 720 | } |
---|
| 721 | if(shift) |
---|
| 722 | { |
---|
| 723 | return "Wrong number of arguments for command"; |
---|
| 724 | } |
---|
| 725 | $timeformat = $format; |
---|
| 726 | redisplay(); |
---|
| 727 | } |
---|
| 728 | |
---|
[6700c605] | 729 | =head3 Receive window scrolling |
---|
| 730 | |
---|
| 731 | Permit scrolling the receive window left or right by arbitrary |
---|
| 732 | amounts (with a default of 10 characters). |
---|
| 733 | |
---|
| 734 | =cut |
---|
| 735 | |
---|
| 736 | sub recv_shift_left |
---|
| 737 | { |
---|
| 738 | my $func = shift; |
---|
| 739 | my $delta = shift; |
---|
[365950b] | 740 | $delta = 10 unless defined($delta) && int($delta) > 0; |
---|
[6700c605] | 741 | my $shift = BarnOwl::recv_getshift(); |
---|
| 742 | if($shift > 0) { |
---|
| 743 | BarnOwl::recv_setshift(max(0, $shift-$delta)); |
---|
| 744 | } else { |
---|
| 745 | return "Already full left"; |
---|
| 746 | } |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | sub recv_shift_right |
---|
| 750 | { |
---|
| 751 | my $func = shift; |
---|
| 752 | my $delta = shift; |
---|
[675a998] | 753 | $delta = 10 unless defined($delta) && int($delta) > 0; |
---|
[6700c605] | 754 | my $shift = BarnOwl::recv_getshift(); |
---|
| 755 | BarnOwl::recv_setshift($shift+$delta); |
---|
| 756 | } |
---|
| 757 | |
---|
[de3f641] | 758 | =head3 default_zephyr_signature |
---|
| 759 | |
---|
| 760 | Compute the default zephyr signature. |
---|
| 761 | |
---|
| 762 | =cut |
---|
| 763 | |
---|
| 764 | sub default_zephyr_signature |
---|
| 765 | { |
---|
[77c87b2] | 766 | my $zsig = getvar('zsig'); |
---|
[785ee77] | 767 | if (!defined($zsig) || $zsig eq '') { |
---|
| 768 | my $zsigproc = getvar('zsigproc'); |
---|
| 769 | if (defined($zsigproc) && $zsigproc ne '') { |
---|
[3c428d4] | 770 | $zsig = `$zsigproc`; |
---|
| 771 | } elsif (!defined($zsig = get_zephyr_variable('zwrite-signature'))) { |
---|
| 772 | $zsig = ((getpwuid($<))[6]); |
---|
| 773 | $zsig =~ s/,.*//; |
---|
| 774 | } |
---|
[de3f641] | 775 | } |
---|
[77c87b2] | 776 | chomp($zsig); |
---|
| 777 | return $zsig; |
---|
[de3f641] | 778 | } |
---|
| 779 | |
---|
[b120bd3] | 780 | =head3 random_zephyr_signature |
---|
| 781 | |
---|
| 782 | Retrieve a random line from ~/.zsigs (except those beginning with '#') |
---|
| 783 | and use it as the zephyr signature. |
---|
| 784 | |
---|
| 785 | =cut |
---|
| 786 | |
---|
| 787 | sub random_zephyr_signature |
---|
| 788 | { |
---|
| 789 | my $zsigfile = "$ENV{'HOME'}/.zsigs"; |
---|
| 790 | open my $file, '<', $zsigfile or die "Error opening file $zsigfile: $!"; |
---|
| 791 | my @lines = grep !(/^#/ || /^\s*$/), <$file>; |
---|
| 792 | close $file; |
---|
| 793 | return '' if !@lines; |
---|
| 794 | my $zsig = "$lines[int(rand(scalar @lines))]"; |
---|
| 795 | chomp $zsig; |
---|
| 796 | return $zsig; |
---|
| 797 | } |
---|
| 798 | |
---|
[ecd4edf] | 799 | =head3 register_idle_watcher %ARGS |
---|
| 800 | |
---|
| 801 | Call a callback whenever the amount of time the user becomes idle or comes |
---|
| 802 | back from being idle. |
---|
| 803 | |
---|
| 804 | You must include the following parameters: |
---|
| 805 | |
---|
| 806 | =over 4 |
---|
| 807 | |
---|
| 808 | =item name |
---|
| 809 | |
---|
| 810 | The name given to the idle watcher |
---|
| 811 | |
---|
| 812 | =item after |
---|
| 813 | |
---|
| 814 | How long the user must be idle, in seconds, before the callback is called. |
---|
| 815 | If the value is too small, you may have spurious or inaccurate calls. |
---|
| 816 | (The current lower limit is about 1 second.) |
---|
| 817 | |
---|
| 818 | =item callback |
---|
| 819 | |
---|
| 820 | The Perl subroutine that gets called when the user has been idle for C<AFTER> |
---|
| 821 | seconds, or comes back from being idle. The subroutine is passed one parameter, |
---|
| 822 | which is true if the user is currently idle, and false otherwise. |
---|
| 823 | |
---|
| 824 | =back |
---|
| 825 | |
---|
| 826 | This method returns a unique identifier which may be passed to |
---|
| 827 | L<BarnOwl::unregister_idle_watcher>. |
---|
| 828 | |
---|
| 829 | =cut |
---|
| 830 | |
---|
| 831 | =head3 unregister_idle_watcher UNIQUE_ID [...] |
---|
| 832 | |
---|
| 833 | Removed and returns the idle watcher specified by C<UNIQUE_ID>. |
---|
| 834 | You may specify multiple unique ids. |
---|
| 835 | |
---|
| 836 | =cut |
---|
| 837 | |
---|
| 838 | my %idle_watchers; |
---|
| 839 | tie %idle_watchers, 'Tie::RefHash'; |
---|
| 840 | |
---|
| 841 | $BarnOwl::Hooks::wakeup->add(sub { |
---|
| 842 | foreach my $idle_watcher (values %idle_watchers) { |
---|
| 843 | _wakeup_idle_watcher($idle_watcher); |
---|
| 844 | } |
---|
| 845 | }); |
---|
| 846 | |
---|
| 847 | sub _wakeup_idle_watcher { |
---|
| 848 | my ($idle_watcher, $offset) = @_; |
---|
| 849 | $offset = 0 unless defined $offset; |
---|
| 850 | # go unidle |
---|
| 851 | $idle_watcher->{idle_timer}->stop if $idle_watcher->{idle_timer}; |
---|
| 852 | undef $idle_watcher->{idle_timer}; |
---|
| 853 | $idle_watcher->{callback}->(0) if $idle_watcher->{is_idle}; |
---|
| 854 | $idle_watcher->{is_idle} = 0; |
---|
| 855 | |
---|
| 856 | # queue going idle |
---|
| 857 | $idle_watcher->{idle_timer} = BarnOwl::Timer->new({ |
---|
| 858 | name => $idle_watcher->{name}, |
---|
| 859 | after => $idle_watcher->{after} - $offset, |
---|
| 860 | cb => sub { |
---|
| 861 | $idle_watcher->{is_idle} = 1; |
---|
| 862 | $idle_watcher->{callback}->(1); |
---|
| 863 | } |
---|
| 864 | }); |
---|
| 865 | } |
---|
| 866 | |
---|
| 867 | sub register_idle_watcher { |
---|
| 868 | my %args = (@_); |
---|
| 869 | $idle_watchers{\%args} = \%args; |
---|
| 870 | _wakeup_idle_watcher(\%args, BarnOwl::getidletime); # make sure to queue up the idle/unidle events from this idle watcher |
---|
| 871 | return \%args; |
---|
| 872 | } |
---|
| 873 | |
---|
| 874 | sub unregister_idle_watcher { |
---|
| 875 | my ($id) = @_; |
---|
| 876 | $idle_watchers{$id}->{idle_timer}->stop if $idle_watchers{$id}->{idle_timer}; |
---|
| 877 | return delete $idle_watchers{$id}; |
---|
| 878 | } |
---|
| 879 | |
---|
[7589f0a] | 880 | # Stub for owl::startup / BarnOwl::startup, so it isn't bound to the |
---|
| 881 | # startup command. This may be redefined in a user's configfile. |
---|
| 882 | sub startup |
---|
| 883 | { |
---|
| 884 | } |
---|
| 885 | |
---|
[ee183be] | 886 | 1; |
---|