[ee183be] | 1 | use strict; |
---|
| 2 | use warnings; |
---|
| 3 | |
---|
| 4 | package BarnOwl; |
---|
| 5 | |
---|
[2be605a] | 6 | use base qw(Exporter); |
---|
| 7 | our @EXPORT_OK = qw(command getcurmsg getnumcols getidletime |
---|
| 8 | zephyr_getsender zephyr_getrealm zephyr_zwrite |
---|
| 9 | zephyr_stylestrip zephyr_smartstrip_user zephyr_getsubs |
---|
| 10 | queue_message admin_message |
---|
| 11 | start_question start_password start_edit_win |
---|
| 12 | get_data_dir get_config_dir popless_text popless_ztext |
---|
[eede1bf] | 13 | error debug |
---|
[2be605a] | 14 | create_style getnumcolors wordwrap |
---|
[ffc4df6] | 15 | add_dispatch remove_dispatch |
---|
| 16 | add_io_dispatch remove_io_dispatch |
---|
[2be605a] | 17 | new_command |
---|
| 18 | new_variable_int new_variable_bool new_variable_string |
---|
[4df918b] | 19 | quote redisplay); |
---|
[2be605a] | 20 | our %EXPORT_TAGS = (all => \@EXPORT_OK); |
---|
| 21 | |
---|
[fd8dfe7] | 22 | BEGIN { |
---|
| 23 | # bootstrap in C bindings and glue |
---|
| 24 | *owl:: = \*BarnOwl::; |
---|
| 25 | bootstrap BarnOwl 1.2; |
---|
| 26 | }; |
---|
| 27 | |
---|
| 28 | use lib(get_data_dir() . "/lib"); |
---|
| 29 | use lib(get_config_dir() . "/lib"); |
---|
| 30 | |
---|
| 31 | use BarnOwl::Hook; |
---|
| 32 | use BarnOwl::Hooks; |
---|
| 33 | use BarnOwl::Message; |
---|
| 34 | use BarnOwl::Style; |
---|
[df569c5] | 35 | use BarnOwl::Zephyr; |
---|
[fd8dfe7] | 36 | use BarnOwl::Timer; |
---|
[cf26b72] | 37 | use BarnOwl::Editwin; |
---|
[8eac1a5] | 38 | use BarnOwl::Completion; |
---|
[fd8dfe7] | 39 | |
---|
[ee183be] | 40 | =head1 NAME |
---|
| 41 | |
---|
| 42 | BarnOwl |
---|
| 43 | |
---|
| 44 | =head1 DESCRIPTION |
---|
| 45 | |
---|
| 46 | The BarnOwl module contains the core of BarnOwl's perl |
---|
| 47 | bindings. Source in this module is also run at startup to bootstrap |
---|
| 48 | barnowl by defining things like the default style. |
---|
| 49 | |
---|
| 50 | =for NOTE |
---|
| 51 | These following functions are defined in perlglue.xs. Keep the |
---|
| 52 | documentation here in sync with the user-visible commands defined |
---|
| 53 | there! |
---|
| 54 | |
---|
| 55 | =head2 command STRING |
---|
| 56 | |
---|
| 57 | Executes a BarnOwl command in the same manner as if the user had |
---|
| 58 | executed it at the BarnOwl command prompt. If the command returns a |
---|
| 59 | value, return it as a string, otherwise return undef. |
---|
| 60 | |
---|
| 61 | =head2 getcurmsg |
---|
| 62 | |
---|
| 63 | Returns the current message as a C<BarnOwl::Message> subclass, or |
---|
| 64 | undef if there is no message selected |
---|
| 65 | =head2 getnumcols |
---|
| 66 | |
---|
| 67 | Returns the width of the display window BarnOwl is currently using |
---|
| 68 | |
---|
| 69 | =head2 getidletime |
---|
| 70 | |
---|
| 71 | Returns the length of time since the user has pressed a key, in |
---|
| 72 | seconds. |
---|
| 73 | |
---|
| 74 | =head2 zephyr_getrealm |
---|
| 75 | |
---|
| 76 | Returns the zephyr realm barnowl is running in |
---|
| 77 | |
---|
| 78 | =head2 zephyr_getsender |
---|
| 79 | |
---|
| 80 | Returns the fully-qualified name of the zephyr sender barnowl is |
---|
| 81 | running as, e.g. C<nelhage@ATHENA.MIT.EDU> |
---|
| 82 | |
---|
| 83 | =head2 zephyr_zwrite COMMAND MESSAGE |
---|
| 84 | |
---|
| 85 | Sends a zephyr programmatically. C<COMMAND> should be a C<zwrite> |
---|
| 86 | command line, and C<MESSAGE> is the zephyr body to send. |
---|
| 87 | |
---|
| 88 | =head2 ztext_stylestrip STRING |
---|
| 89 | |
---|
| 90 | Strips zephyr formatting from a string and returns the result |
---|
| 91 | |
---|
| 92 | =head2 zephyr_getsubs |
---|
| 93 | |
---|
| 94 | Returns the list of subscription triples <class,instance,recipient>, |
---|
| 95 | separated by newlines. |
---|
| 96 | |
---|
| 97 | =head2 queue_message MESSAGE |
---|
| 98 | |
---|
| 99 | Enqueue a message in the BarnOwl message list, logging it and |
---|
| 100 | processing it appropriately. C<MESSAGE> should be an instance of |
---|
| 101 | BarnOwl::Message or a subclass. |
---|
| 102 | |
---|
| 103 | =head2 admin_message HEADER BODY |
---|
| 104 | |
---|
| 105 | Display a BarnOwl B<Admin> message, with the given header and body. |
---|
| 106 | |
---|
| 107 | =head2 start_question PROMPT CALLBACK |
---|
| 108 | |
---|
| 109 | Displays C<PROMPT> on the screen and lets the user enter a line of |
---|
| 110 | text, and calls C<CALLBACK>, which must be a perl subroutine |
---|
| 111 | reference, with the text the user entered |
---|
| 112 | |
---|
| 113 | =head2 start_password PROMPT CALLBACK |
---|
| 114 | |
---|
| 115 | Like C<start_question>, but echoes the user's input as C<*>s when they |
---|
| 116 | input. |
---|
| 117 | |
---|
| 118 | =head2 start_edit_win PROMPT CALLBACK |
---|
| 119 | |
---|
| 120 | Like C<start_question>, but displays C<PROMPT> on a line of its own |
---|
| 121 | and opens the editwin. If the user cancels the edit win, C<CALLBACK> |
---|
| 122 | is not invoked. |
---|
| 123 | |
---|
| 124 | =head2 get_data_dir |
---|
| 125 | |
---|
| 126 | Returns the BarnOwl system data directory, where system libraries and |
---|
| 127 | modules are stored |
---|
| 128 | |
---|
| 129 | =head2 get_config_dir |
---|
| 130 | |
---|
| 131 | Returns the BarnOwl user configuration directory, where user modules |
---|
| 132 | and configuration are stored (by default, C<$HOME/.owl>) |
---|
| 133 | |
---|
| 134 | =head2 popless_text TEXT |
---|
| 135 | |
---|
| 136 | Show a popup window containing the given C<TEXT> |
---|
| 137 | |
---|
| 138 | =head2 popless_ztext TEXT |
---|
| 139 | |
---|
| 140 | Show a popup window containing the provided zephyr-formatted C<TEXT> |
---|
| 141 | |
---|
| 142 | =head2 error STRING |
---|
| 143 | |
---|
| 144 | Reports an error and log it in `show errors'. Note that in any |
---|
| 145 | callback or hook called in perl code from BarnOwl, a C<die> will be |
---|
| 146 | caught and passed to C<error>. |
---|
| 147 | |
---|
[eede1bf] | 148 | =head2 debug STRING |
---|
| 149 | |
---|
| 150 | Logs a debugging message to BarnOwl's debug log |
---|
| 151 | |
---|
[ee183be] | 152 | =head2 getnumcolors |
---|
| 153 | |
---|
| 154 | Returns the number of colors this BarnOwl is capable of displaying |
---|
| 155 | |
---|
| 156 | =head2 add_dispatch FD CALLBACK |
---|
| 157 | |
---|
| 158 | Adds a file descriptor to C<BarnOwl>'s internal C<select()> |
---|
| 159 | loop. C<CALLBACK> will be invoked whenever data is available to be |
---|
| 160 | read from C<FD>. |
---|
| 161 | |
---|
[ffc4df6] | 162 | C<add_dispatch> has been deprecated in favor of C<add_io_dispatch>, |
---|
| 163 | and is now a wrapper for it called with C<mode> set to C<'r'>. |
---|
| 164 | |
---|
| 165 | =cut |
---|
| 166 | |
---|
| 167 | sub add_dispatch { |
---|
| 168 | my $fd = shift; |
---|
| 169 | my $cb = shift; |
---|
| 170 | add_io_dispatch($fd, 'r', $cb); |
---|
| 171 | } |
---|
| 172 | |
---|
[ee183be] | 173 | =head2 remove_dispatch FD |
---|
| 174 | |
---|
| 175 | Remove a file descriptor previously registered via C<add_dispatch> |
---|
| 176 | |
---|
[ffc4df6] | 177 | C<remove_dispatch> has been deprecated in favor of |
---|
| 178 | C<remove_io_dispatch>. |
---|
| 179 | |
---|
| 180 | =cut |
---|
| 181 | |
---|
| 182 | *remove_dispatch = \&remove_io_dispatch; |
---|
| 183 | |
---|
| 184 | =head2 add_io_dispatch FD MODE CB |
---|
| 185 | |
---|
| 186 | Adds a file descriptor to C<BarnOwl>'s internal C<select()> |
---|
| 187 | loop. <MODE> can be 'r', 'w', or 'rw'. C<CALLBACK> will be invoked |
---|
| 188 | whenever C<FD> becomes ready, as specified by <MODE>. |
---|
| 189 | |
---|
| 190 | Only one callback can be registered per FD. If a new callback is |
---|
| 191 | registered, the old one is removed. |
---|
| 192 | |
---|
| 193 | =cut |
---|
| 194 | |
---|
| 195 | sub add_io_dispatch { |
---|
| 196 | my $fd = shift; |
---|
| 197 | my $modeStr = shift; |
---|
| 198 | my $cb = shift; |
---|
| 199 | my $mode = 0; |
---|
| 200 | |
---|
| 201 | $mode |= 0x1 if ($modeStr =~ /r/i); # Read |
---|
| 202 | $mode |= 0x2 if ($modeStr =~ /w/i); # Write |
---|
| 203 | if ($mode) { |
---|
| 204 | $mode |= 0x4; # Exceptional |
---|
| 205 | BarnOwl::Internal::add_io_dispatch($fd, $mode, $cb); |
---|
| 206 | } else { |
---|
| 207 | die("Invalid I/O Dispatch mode: $modeStr"); |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | =head2 remove_io_dispatch FD |
---|
| 212 | |
---|
| 213 | Remove a file descriptor previously registered via C<add_io_dispatch> |
---|
| 214 | |
---|
[ee183be] | 215 | =head2 create_style NAME OBJECT |
---|
| 216 | |
---|
| 217 | Creates a new barnowl style with the given NAME defined by the given |
---|
| 218 | object. The object must have a C<description> method which returns a |
---|
| 219 | string description of the style, and a and C<format_message> method |
---|
| 220 | which accepts a C<BarnOwl::Message> object and returns a string that |
---|
| 221 | is the result of formatting the message for display. |
---|
| 222 | |
---|
[4df918b] | 223 | =head2 redisplay |
---|
| 224 | |
---|
| 225 | Redraw all of the messages on screen. This is useful if you've just |
---|
| 226 | changed how a style renders messages. |
---|
| 227 | |
---|
[ee183be] | 228 | =cut |
---|
| 229 | |
---|
| 230 | # perlconfig.c will set this to the value of the -c command-line |
---|
| 231 | # switch, if present. |
---|
| 232 | our $configfile; |
---|
| 233 | |
---|
[d7bcff8] | 234 | our @all_commands; |
---|
| 235 | |
---|
[ee183be] | 236 | if(!$configfile && -f $ENV{HOME} . "/.barnowlconf") { |
---|
| 237 | $configfile = $ENV{HOME} . "/.barnowlconf"; |
---|
| 238 | } |
---|
| 239 | $configfile ||= $ENV{HOME}."/.owlconf"; |
---|
| 240 | |
---|
| 241 | # populate global variable space for legacy owlconf files |
---|
| 242 | sub _receive_msg_legacy_wrap { |
---|
| 243 | my ($m) = @_; |
---|
| 244 | $m->legacy_populate_global(); |
---|
| 245 | return &BarnOwl::Hooks::_receive_msg($m); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | =head2 new_command NAME FUNC [{ARGS}] |
---|
| 249 | |
---|
| 250 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
| 251 | be called with the arguments passed to the command, with NAME as the |
---|
| 252 | first argument. |
---|
| 253 | |
---|
| 254 | ARGS should be a hashref containing any or all of C<summary>, |
---|
| 255 | C<usage>, or C<description> keys: |
---|
| 256 | |
---|
| 257 | =over 4 |
---|
| 258 | |
---|
| 259 | =item summary |
---|
| 260 | |
---|
| 261 | A one-line summary of the purpose of the command |
---|
| 262 | |
---|
| 263 | =item usage |
---|
| 264 | |
---|
| 265 | A one-line usage synopsis, showing available options and syntax |
---|
| 266 | |
---|
| 267 | =item description |
---|
| 268 | |
---|
| 269 | A longer description of the syntax and semantics of the command, |
---|
| 270 | explaining usage and options |
---|
| 271 | |
---|
| 272 | =back |
---|
| 273 | |
---|
| 274 | =cut |
---|
| 275 | |
---|
| 276 | sub new_command { |
---|
| 277 | my $name = shift; |
---|
| 278 | my $func = shift; |
---|
| 279 | my $args = shift || {}; |
---|
| 280 | my %args = ( |
---|
| 281 | summary => "", |
---|
| 282 | usage => "", |
---|
| 283 | description => "", |
---|
| 284 | %{$args} |
---|
| 285 | ); |
---|
| 286 | |
---|
| 287 | BarnOwl::Internal::new_command($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | =head2 new_variable_int NAME [{ARGS}] |
---|
| 291 | |
---|
| 292 | =head2 new_variable_bool NAME [{ARGS}] |
---|
| 293 | |
---|
| 294 | =head2 new_variable_string NAME [{ARGS}] |
---|
| 295 | |
---|
| 296 | Add a new owl variable, either an int, a bool, or a string, with the |
---|
| 297 | specified name. |
---|
| 298 | |
---|
| 299 | ARGS can optionally contain the following keys: |
---|
| 300 | |
---|
| 301 | =over 4 |
---|
| 302 | |
---|
| 303 | =item default |
---|
| 304 | |
---|
| 305 | The default and initial value for the variable |
---|
| 306 | |
---|
| 307 | =item summary |
---|
| 308 | |
---|
| 309 | A one-line summary of the variable's purpose |
---|
| 310 | |
---|
| 311 | =item description |
---|
| 312 | |
---|
| 313 | A longer description of the function of the variable |
---|
| 314 | |
---|
| 315 | =back |
---|
| 316 | |
---|
| 317 | =cut |
---|
| 318 | |
---|
| 319 | sub new_variable_int { |
---|
| 320 | unshift @_, \&BarnOwl::Internal::new_variable_int, 0; |
---|
| 321 | goto \&_new_variable; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | sub new_variable_bool { |
---|
| 325 | unshift @_, \&BarnOwl::Internal::new_variable_bool, 0; |
---|
| 326 | goto \&_new_variable; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | sub new_variable_string { |
---|
| 330 | unshift @_, \&BarnOwl::Internal::new_variable_string, ""; |
---|
| 331 | goto \&_new_variable; |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | sub _new_variable { |
---|
| 335 | my $func = shift; |
---|
| 336 | my $default_default = shift; |
---|
| 337 | my $name = shift; |
---|
| 338 | my $args = shift || {}; |
---|
| 339 | my %args = ( |
---|
| 340 | summary => "", |
---|
| 341 | description => "", |
---|
| 342 | default => $default_default, |
---|
| 343 | %{$args}); |
---|
| 344 | $func->($name, $args{default}, $args{summary}, $args{description}); |
---|
| 345 | } |
---|
| 346 | |
---|
[fc92e6e2] | 347 | =head2 quote LIST |
---|
[ee183be] | 348 | |
---|
[fc92e6e2] | 349 | Quotes each of the strings in LIST and returns a string that will be |
---|
| 350 | correctly decoded to LIST by the BarnOwl command parser. For example: |
---|
| 351 | |
---|
| 352 | quote('zwrite', 'andersk', '-m', 'Hello, world!') |
---|
| 353 | # returns "zwrite andersk -m 'Hello, world!'" |
---|
[ee183be] | 354 | |
---|
| 355 | =cut |
---|
| 356 | |
---|
| 357 | sub quote { |
---|
[fc92e6e2] | 358 | my @quoted; |
---|
| 359 | for my $str (@_) { |
---|
| 360 | if ($str eq '') { |
---|
| 361 | push @quoted, "''"; |
---|
| 362 | } elsif ($str !~ /['" ]/) { |
---|
| 363 | push @quoted, "$str"; |
---|
| 364 | } elsif ($str !~ /'/) { |
---|
| 365 | push @quoted, "'$str'"; |
---|
| 366 | } else { |
---|
| 367 | (my $qstr = $str) =~ s/"/"'"'"/g; |
---|
| 368 | push @quoted, '"' . $qstr . '"'; |
---|
| 369 | } |
---|
[ee183be] | 370 | } |
---|
[fc92e6e2] | 371 | return join(' ', @quoted); |
---|
[ee183be] | 372 | } |
---|
| 373 | |
---|
[22b54a7] | 374 | =head2 Modify filters by appending text |
---|
| 375 | |
---|
| 376 | =cut |
---|
| 377 | |
---|
[cff58b4] | 378 | sub register_builtin_commands { |
---|
[0b2afba] | 379 | # Filter modification |
---|
[cff58b4] | 380 | BarnOwl::new_command("filterappend", |
---|
| 381 | sub { filter_append_helper('appending', '', @_); }, |
---|
| 382 | { |
---|
| 383 | summary => "append '<text>' to filter", |
---|
| 384 | usage => "filterappend <filter> <text>", |
---|
| 385 | }); |
---|
| 386 | |
---|
| 387 | BarnOwl::new_command("filterand", |
---|
| 388 | sub { filter_append_helper('and-ing', 'and', @_); }, |
---|
| 389 | { |
---|
| 390 | summary => "append 'and <text>' to filter", |
---|
| 391 | usage => "filterand <filter> <text>", |
---|
| 392 | }); |
---|
| 393 | |
---|
| 394 | BarnOwl::new_command("filteror", |
---|
| 395 | sub { filter_append_helper('or-ing', 'or', @_); }, |
---|
| 396 | { |
---|
| 397 | summary => "append 'or <text>' to filter", |
---|
| 398 | usage => "filteror <filter> <text>", |
---|
| 399 | }); |
---|
| 400 | |
---|
[0b2afba] | 401 | # Date formatting |
---|
| 402 | BarnOwl::new_command("showdate", |
---|
| 403 | sub { BarnOwl::time_format('showdate', '%Y-%m-%d %H:%M'); }, |
---|
| 404 | { |
---|
| 405 | summary => "Show date in timestamps for supporting styles.", |
---|
| 406 | usage => "showdate", |
---|
| 407 | }); |
---|
| 408 | |
---|
| 409 | BarnOwl::new_command("hidedate", |
---|
| 410 | sub { BarnOwl::time_format('hidedate', '%H:%M'); }, |
---|
| 411 | { |
---|
| 412 | summary => "Don't show date in timestamps for supporting styles.", |
---|
| 413 | usage => "hidedate", |
---|
| 414 | }); |
---|
| 415 | |
---|
| 416 | BarnOwl::new_command("timeformat", |
---|
| 417 | \&BarnOwl::time_format, |
---|
| 418 | { |
---|
| 419 | summary => "Set the format for timestamps and re-display messages", |
---|
| 420 | usage => "timeformat <format>", |
---|
| 421 | }); |
---|
| 422 | |
---|
[cff58b4] | 423 | } |
---|
| 424 | |
---|
| 425 | $BarnOwl::Hooks::startup->add("BarnOwl::register_builtin_commands"); |
---|
[22b54a7] | 426 | |
---|
| 427 | =head3 filter_append_helper ACTION SEP FUNC FILTER APPEND_TEXT |
---|
| 428 | |
---|
| 429 | Helper to append to filters. |
---|
| 430 | |
---|
| 431 | =cut |
---|
| 432 | |
---|
| 433 | sub filter_append_helper |
---|
| 434 | { |
---|
| 435 | my $action = shift; |
---|
| 436 | my $sep = shift; |
---|
| 437 | my $func = shift; |
---|
| 438 | my $filter = shift; |
---|
| 439 | my @append = @_; |
---|
| 440 | my $oldfilter = BarnOwl::getfilter($filter); |
---|
| 441 | chomp $oldfilter; |
---|
[0b5168d] | 442 | my $newfilter = "$oldfilter $sep " . quote(@append); |
---|
| 443 | my $msgtext = "To filter " . quote($filter) . " $action\n" . quote(@append) . "\nto get\n$newfilter"; |
---|
[22b54a7] | 444 | if (BarnOwl::getvar('showfilterchange') eq 'on') { |
---|
| 445 | BarnOwl::admin_message("Filter", $msgtext); |
---|
| 446 | } |
---|
| 447 | BarnOwl::filter($filter, $newfilter); |
---|
| 448 | return; |
---|
| 449 | } |
---|
| 450 | BarnOwl::new_variable_bool("showfilterchange", |
---|
| 451 | { default => 1, |
---|
| 452 | summary => 'Show modifications to filters by filterappend and friends.'}); |
---|
[ee183be] | 453 | |
---|
[0b2afba] | 454 | =head3 time_format FORMAT |
---|
| 455 | |
---|
| 456 | Set the format for displaying times (variable timeformat) and redisplay |
---|
| 457 | messages. |
---|
| 458 | |
---|
| 459 | =cut |
---|
| 460 | |
---|
[d694c55] | 461 | my $timeformat = '%H:%M'; |
---|
[0b2afba] | 462 | |
---|
| 463 | sub time_format |
---|
| 464 | { |
---|
| 465 | my $function = shift; |
---|
| 466 | my $format = shift; |
---|
| 467 | if(!$format) |
---|
| 468 | { |
---|
| 469 | return $timeformat; |
---|
| 470 | } |
---|
| 471 | if(shift) |
---|
| 472 | { |
---|
| 473 | return "Wrong number of arguments for command"; |
---|
| 474 | } |
---|
| 475 | $timeformat = $format; |
---|
| 476 | redisplay(); |
---|
| 477 | } |
---|
| 478 | |
---|
[de3f641] | 479 | =head3 default_zephyr_signature |
---|
| 480 | |
---|
| 481 | Compute the default zephyr signature. |
---|
| 482 | |
---|
| 483 | =cut |
---|
| 484 | |
---|
| 485 | sub default_zephyr_signature |
---|
| 486 | { |
---|
[77c87b2] | 487 | my $zsig = getvar('zsig'); |
---|
[3c428d4] | 488 | if (!$zsig) { |
---|
| 489 | if (my $zsigproc = getvar('zsigproc')) { |
---|
| 490 | $zsig = `$zsigproc`; |
---|
| 491 | } elsif (!defined($zsig = get_zephyr_variable('zwrite-signature'))) { |
---|
| 492 | $zsig = ((getpwuid($<))[6]); |
---|
| 493 | $zsig =~ s/,.*//; |
---|
| 494 | } |
---|
[de3f641] | 495 | } |
---|
[77c87b2] | 496 | chomp($zsig); |
---|
| 497 | return $zsig; |
---|
[de3f641] | 498 | } |
---|
| 499 | |
---|
[7589f0a] | 500 | # Stub for owl::startup / BarnOwl::startup, so it isn't bound to the |
---|
| 501 | # startup command. This may be redefined in a user's configfile. |
---|
| 502 | sub startup |
---|
| 503 | { |
---|
| 504 | } |
---|
| 505 | |
---|
[ee183be] | 506 | 1; |
---|