[f1e629d] | 1 | # $Id$ |
---|
| 2 | # |
---|
| 3 | # This is all linked into the binary and evaluated when perl starts up... |
---|
| 4 | # |
---|
| 5 | ##################################################################### |
---|
| 6 | ##################################################################### |
---|
[b6c067a] | 7 | # XXX NOTE: This file is sourced before almost any barnowl |
---|
| 8 | # architecture is loaded. This means, for example, that it cannot |
---|
[0337203] | 9 | # execute any owl commands. Any code that needs to do so should live |
---|
| 10 | # in BarnOwl::Hooks::_startup |
---|
[f1e629d] | 11 | |
---|
[c681337] | 12 | use strict; |
---|
| 13 | use warnings; |
---|
| 14 | |
---|
[8203afd] | 15 | package BarnOwl; |
---|
[f1e629d] | 16 | |
---|
[74fc22a] | 17 | =head1 NAME |
---|
| 18 | |
---|
| 19 | BarnOwl |
---|
| 20 | |
---|
| 21 | =head1 DESCRIPTION |
---|
| 22 | |
---|
| 23 | The BarnOwl module contains the core of BarnOwl's perl |
---|
| 24 | bindings. Source in this module is also run at startup to bootstrap |
---|
| 25 | barnowl by defining things like the default style. |
---|
| 26 | |
---|
| 27 | =for NOTE |
---|
| 28 | These following functions are defined in perlglue.xs. Keep the |
---|
| 29 | documentation here in sync with the user-visible commands defined |
---|
| 30 | there! |
---|
| 31 | |
---|
| 32 | =head2 command STRING |
---|
| 33 | |
---|
| 34 | Executes a BarnOwl command in the same manner as if the user had |
---|
| 35 | executed it at the BarnOwl command prompt. If the command returns a |
---|
| 36 | value, return it as a string, otherwise return undef. |
---|
| 37 | |
---|
| 38 | =head2 getcurmsg |
---|
| 39 | |
---|
| 40 | Returns the current message as a C<BarnOwl::Message> subclass, or |
---|
| 41 | undef if there is no message selected |
---|
| 42 | |
---|
| 43 | =head2 getnumcols |
---|
| 44 | |
---|
| 45 | Returns the width of the display window BarnOwl is currently using |
---|
| 46 | |
---|
| 47 | =head2 getidletime |
---|
| 48 | |
---|
| 49 | Returns the length of time since the user has pressed a key, in |
---|
| 50 | seconds. |
---|
| 51 | |
---|
| 52 | =head2 zephyr_getrealm |
---|
| 53 | |
---|
| 54 | Returns the zephyr realm barnowl is running in |
---|
| 55 | |
---|
| 56 | =head2 zephyr_getsender |
---|
| 57 | |
---|
| 58 | Returns the fully-qualified name of the zephyr sender barnowl is |
---|
| 59 | running as, e.g. C<nelhage@ATHENA.MIT.EDU> |
---|
| 60 | |
---|
| 61 | =head2 zephyr_zwrite COMMAND MESSAGE |
---|
| 62 | |
---|
| 63 | Sends a zephyr programmatically. C<COMMAND> should be a C<zwrite> |
---|
| 64 | command line, and C<MESSAGE> is the zephyr body to send. |
---|
| 65 | |
---|
| 66 | =head2 ztext_stylestrip STRING |
---|
| 67 | |
---|
| 68 | Strips zephyr formatting from a string and returns the result |
---|
| 69 | |
---|
[acb13bb] | 70 | =head2 zephyr_getsubs |
---|
| 71 | |
---|
| 72 | Returns the list of subscription triples <class,instance,recipient>, |
---|
| 73 | separated by newlines. |
---|
| 74 | |
---|
[74fc22a] | 75 | =head2 queue_message MESSAGE |
---|
| 76 | |
---|
| 77 | Enqueue a message in the BarnOwl message list, logging it and |
---|
| 78 | processing it appropriately. C<MESSAGE> should be an instance of |
---|
| 79 | BarnOwl::Message or a subclass. |
---|
| 80 | |
---|
| 81 | =head2 admin_message HEADER BODY |
---|
| 82 | |
---|
| 83 | Display a BarnOwl B<Admin> message, with the given header and body. |
---|
| 84 | |
---|
| 85 | =head2 start_question PROMPT CALLBACK |
---|
| 86 | |
---|
| 87 | Displays C<PROMPT> on the screen and lets the user enter a line of |
---|
| 88 | text, and calls C<CALLBACK>, which must be a perl subroutine |
---|
| 89 | reference, with the text the user entered |
---|
| 90 | |
---|
| 91 | =head2 start_password PROMPT CALLBACK |
---|
| 92 | |
---|
| 93 | Like C<start_question>, but echoes the user's input as C<*>s when they |
---|
| 94 | input. |
---|
| 95 | |
---|
[9815e2e] | 96 | =head2 start_edit_win PROMPT CALLBACK |
---|
[74fc22a] | 97 | |
---|
| 98 | Like C<start_question>, but displays C<PROMPT> on a line of its own |
---|
| 99 | and opens the editwin. If the user cancels the edit win, C<CALLBACK> |
---|
| 100 | is not invoked. |
---|
| 101 | |
---|
| 102 | =head2 get_data_dir |
---|
| 103 | |
---|
| 104 | Returns the BarnOwl system data directory, where system libraries and |
---|
| 105 | modules are stored |
---|
| 106 | |
---|
| 107 | =head2 get_config_dir |
---|
| 108 | |
---|
| 109 | Returns the BarnOwl user configuration directory, where user modules |
---|
| 110 | and configuration are stored (by default, C<$HOME/.owl>) |
---|
| 111 | |
---|
| 112 | =head2 popless_text TEXT |
---|
| 113 | |
---|
| 114 | Show a popup window containing the given C<TEXT> |
---|
| 115 | |
---|
| 116 | =head2 popless_ztext TEXT |
---|
| 117 | |
---|
| 118 | Show a popup window containing the provided zephyr-formatted C<TEXT> |
---|
| 119 | |
---|
| 120 | =head2 error STRING |
---|
| 121 | |
---|
| 122 | Reports an error and log it in `show errors'. Note that in any |
---|
| 123 | callback or hook called in perl code from BarnOwl, a C<die> will be |
---|
| 124 | caught and passed to C<error>. |
---|
| 125 | |
---|
| 126 | =head2 getnumcolors |
---|
| 127 | |
---|
| 128 | Returns the number of colors this BarnOwl is capable of displaying |
---|
| 129 | |
---|
[b07d8c8] | 130 | =head2 add_dispatch FD CALLBACK |
---|
| 131 | |
---|
| 132 | Adds a file descriptor to C<BarnOwl>'s internal C<select()> |
---|
| 133 | loop. C<CALLBACK> will be invoked whenever data is available to be |
---|
| 134 | read from C<FD>. |
---|
| 135 | |
---|
| 136 | =head2 remove_dispatch FD |
---|
| 137 | |
---|
| 138 | Remove a file descriptor previously registered via C<add_dispatch> |
---|
| 139 | |
---|
[0eaa488] | 140 | =head2 create_style NAME OBJECT |
---|
| 141 | |
---|
| 142 | Creates a new barnowl style with the given NAME defined by the given |
---|
| 143 | object. The object must have a C<description> method which returns a |
---|
| 144 | string description of the style, and a and C<format_message> method |
---|
| 145 | which accepts a C<BarnOwl::Message> object and returns a string that |
---|
| 146 | is the result of formatting the message for display. |
---|
| 147 | |
---|
[74fc22a] | 148 | =cut |
---|
| 149 | |
---|
| 150 | |
---|
[8862725] | 151 | BEGIN { |
---|
[f1e629d] | 152 | # bootstrap in C bindings and glue |
---|
[8203afd] | 153 | *owl:: = \*BarnOwl::; |
---|
| 154 | bootstrap BarnOwl 1.2; |
---|
[8862725] | 155 | }; |
---|
| 156 | |
---|
[b363d83] | 157 | use lib(get_data_dir() . "/lib"); |
---|
| 158 | use lib(get_config_dir() . "/lib"); |
---|
[8862725] | 159 | |
---|
[b363d83] | 160 | # perlconfig.c will set this to the value of the -c command-line |
---|
| 161 | # switch, if present. |
---|
[00f9a7d] | 162 | our $configfile; |
---|
| 163 | |
---|
[2e3b9c2] | 164 | if(!$configfile && -f $ENV{HOME} . "/.barnowlconf") { |
---|
| 165 | $configfile = $ENV{HOME} . "/.barnowlconf"; |
---|
| 166 | } |
---|
| 167 | $configfile ||= $ENV{HOME}."/.owlconf"; |
---|
[d03091c] | 168 | |
---|
[186cdc4] | 169 | # populate global variable space for legacy owlconf files |
---|
[f1e629d] | 170 | sub _receive_msg_legacy_wrap { |
---|
| 171 | my ($m) = @_; |
---|
| 172 | $m->legacy_populate_global(); |
---|
[0337203] | 173 | return &BarnOwl::Hooks::_receive_msg($m); |
---|
[f1e629d] | 174 | } |
---|
| 175 | |
---|
[74fc22a] | 176 | =head2 AUTOLOAD |
---|
| 177 | |
---|
| 178 | BarnOwl.pm has a C<AUTOLOAD> method that translates unused names in |
---|
| 179 | the BarnOwl:: namespace to a call to BarnOwl::command() with that |
---|
| 180 | command. Underscores are also translated to C<->s, so you can do |
---|
| 181 | e.g. C<BarnOwl::start_command()> and it will be translated into |
---|
| 182 | C<start-command>. |
---|
| 183 | |
---|
| 184 | So, if you're looking for functionality that you can't find in the |
---|
| 185 | perl interface, check C<:show commands> or C<commands.c> in the |
---|
| 186 | BarnOwl source tree -- there's a good chance it exists as a BarnOwl |
---|
| 187 | command. |
---|
| 188 | |
---|
| 189 | =head3 BUGS |
---|
| 190 | |
---|
| 191 | There are horrible quoting issues here. The AUTOLOAD simple joins your |
---|
| 192 | commands with spaces and passes them unmodified to C<::command> |
---|
| 193 | |
---|
| 194 | =cut |
---|
| 195 | |
---|
[8203afd] | 196 | # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo"); |
---|
[f1e629d] | 197 | sub AUTOLOAD { |
---|
[c681337] | 198 | our $AUTOLOAD; |
---|
[f1e629d] | 199 | my $called = $AUTOLOAD; |
---|
| 200 | $called =~ s/.*:://; |
---|
[e7ac2b6] | 201 | $called =~ s/_/-/g; |
---|
[8203afd] | 202 | return &BarnOwl::command("$called ".join(" ",@_)); |
---|
[f1e629d] | 203 | } |
---|
| 204 | |
---|
[6922edd] | 205 | =head2 new_command NAME FUNC [{ARGS}] |
---|
| 206 | |
---|
| 207 | Add a new owl command. When owl executes the command NAME, FUNC will |
---|
| 208 | be called with the arguments passed to the command, with NAME as the |
---|
| 209 | first argument. |
---|
| 210 | |
---|
| 211 | ARGS should be a hashref containing any or all of C<summary>, |
---|
[74fc22a] | 212 | C<usage>, or C<description> keys: |
---|
| 213 | |
---|
| 214 | =over 4 |
---|
| 215 | |
---|
| 216 | =item summary |
---|
| 217 | |
---|
| 218 | A one-line summary of the purpose of the command |
---|
| 219 | |
---|
| 220 | =item usage |
---|
| 221 | |
---|
| 222 | A one-line usage synopsis, showing available options and syntax |
---|
| 223 | |
---|
| 224 | =item description |
---|
| 225 | |
---|
| 226 | A longer description of the syntax and semantics of the command, |
---|
| 227 | explaining usage and options |
---|
| 228 | |
---|
| 229 | =back |
---|
[6922edd] | 230 | |
---|
| 231 | =cut |
---|
| 232 | |
---|
| 233 | sub new_command { |
---|
| 234 | my $name = shift; |
---|
| 235 | my $func = shift; |
---|
| 236 | my $args = shift || {}; |
---|
| 237 | my %args = ( |
---|
[8757122] | 238 | summary => "", |
---|
| 239 | usage => "", |
---|
| 240 | description => "", |
---|
[6922edd] | 241 | %{$args} |
---|
| 242 | ); |
---|
| 243 | |
---|
[25cccdc] | 244 | BarnOwl::Internal::new_command($name, $func, $args{summary}, $args{usage}, $args{description}); |
---|
[6922edd] | 245 | } |
---|
| 246 | |
---|
[cd57601] | 247 | =head2 new_variable_int NAME [{ARGS}] |
---|
| 248 | |
---|
| 249 | =head2 new_variable_bool NAME [{ARGS}] |
---|
| 250 | |
---|
| 251 | =head2 new_variable_string NAME [{ARGS}] |
---|
| 252 | |
---|
| 253 | Add a new owl variable, either an int, a bool, or a string, with the |
---|
| 254 | specified name. |
---|
| 255 | |
---|
| 256 | ARGS can optionally contain the following keys: |
---|
| 257 | |
---|
| 258 | =over 4 |
---|
| 259 | |
---|
| 260 | =item default |
---|
| 261 | |
---|
| 262 | The default and initial value for the variable |
---|
| 263 | |
---|
| 264 | =item summary |
---|
| 265 | |
---|
| 266 | A one-line summary of the variable's purpose |
---|
| 267 | |
---|
| 268 | =item description |
---|
| 269 | |
---|
| 270 | A longer description of the function of the variable |
---|
| 271 | |
---|
| 272 | =back |
---|
| 273 | |
---|
| 274 | =cut |
---|
| 275 | |
---|
[a695a68] | 276 | sub new_variable_int { |
---|
[25cccdc] | 277 | unshift @_, \&BarnOwl::Internal::new_variable_int, 0; |
---|
[a695a68] | 278 | goto \&_new_variable; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | sub new_variable_bool { |
---|
[25cccdc] | 282 | unshift @_, \&BarnOwl::Internal::new_variable_bool, 0; |
---|
[a695a68] | 283 | goto \&_new_variable; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | sub new_variable_string { |
---|
[25cccdc] | 287 | unshift @_, \&BarnOwl::Internal::new_variable_string, ""; |
---|
[a695a68] | 288 | goto \&_new_variable; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | sub _new_variable { |
---|
| 292 | my $func = shift; |
---|
| 293 | my $default_default = shift; |
---|
| 294 | my $name = shift; |
---|
| 295 | my $args = shift || {}; |
---|
| 296 | my %args = ( |
---|
| 297 | summary => "", |
---|
| 298 | description => "", |
---|
| 299 | default => $default_default, |
---|
| 300 | %{$args}); |
---|
| 301 | $func->($name, $args{default}, $args{summary}, $args{description}); |
---|
| 302 | } |
---|
| 303 | |
---|
[5b75e8b] | 304 | =head2 quote STRING |
---|
| 305 | |
---|
| 306 | Return a version of STRING fully quoted to survive processing by |
---|
| 307 | BarnOwl's command parser. |
---|
| 308 | |
---|
| 309 | =cut |
---|
| 310 | |
---|
| 311 | sub quote { |
---|
| 312 | my $str = shift; |
---|
[740d5f7] | 313 | return "''" if $str eq ''; |
---|
| 314 | if ($str !~ /['" ]/) { |
---|
[5b75e8b] | 315 | return "$str"; |
---|
| 316 | } |
---|
| 317 | if ($str !~ /'/) { |
---|
| 318 | return "'$str'"; |
---|
| 319 | } |
---|
| 320 | $str =~ s/"/"'"'"/g; |
---|
| 321 | return '"' . $str . '"'; |
---|
| 322 | } |
---|
| 323 | |
---|
[f1e629d] | 324 | ##################################################################### |
---|
| 325 | ##################################################################### |
---|
| 326 | |
---|
[8203afd] | 327 | package BarnOwl::Message; |
---|
[f1e629d] | 328 | |
---|
[dd16bdd] | 329 | sub new { |
---|
| 330 | my $class = shift; |
---|
| 331 | my %args = (@_); |
---|
| 332 | if($class eq __PACKAGE__ && $args{type}) { |
---|
[8203afd] | 333 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
---|
[dd16bdd] | 334 | } |
---|
| 335 | return bless {%args}, $class; |
---|
| 336 | } |
---|
| 337 | |
---|
[f1e629d] | 338 | sub type { return shift->{"type"}; } |
---|
| 339 | sub direction { return shift->{"direction"}; } |
---|
| 340 | sub time { return shift->{"time"}; } |
---|
| 341 | sub id { return shift->{"id"}; } |
---|
| 342 | sub body { return shift->{"body"}; } |
---|
| 343 | sub sender { return shift->{"sender"}; } |
---|
| 344 | sub recipient { return shift->{"recipient"}; } |
---|
| 345 | sub login { return shift->{"login"}; } |
---|
[216c734] | 346 | sub is_private { return shift->{"private"}; } |
---|
[f1e629d] | 347 | |
---|
| 348 | sub is_login { return shift->login eq "login"; } |
---|
| 349 | sub is_logout { return shift->login eq "logout"; } |
---|
| 350 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
---|
| 351 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
---|
| 352 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
---|
| 353 | |
---|
| 354 | sub is_deleted { return shift->{"deleted"}; } |
---|
| 355 | |
---|
| 356 | sub is_admin { return (shift->{"type"} eq "admin"); } |
---|
| 357 | sub is_generic { return (shift->{"type"} eq "generic"); } |
---|
[421c8ef7] | 358 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
---|
[467aa16] | 359 | sub is_aim { return (shift->{"type"} eq "AIM"); } |
---|
[dd16bdd] | 360 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
---|
[421c8ef7] | 361 | sub is_icq { return (shift->{"type"} eq "icq"); } |
---|
| 362 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
---|
| 363 | sub is_msn { return (shift->{"type"} eq "msn"); } |
---|
| 364 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
---|
[f1e629d] | 365 | |
---|
| 366 | # These are overridden by appropriate message types |
---|
| 367 | sub is_ping { return 0; } |
---|
| 368 | sub is_mail { return 0; } |
---|
[3c9012b] | 369 | sub is_personal { return shift->is_private; } |
---|
[f1e629d] | 370 | sub class { return undef; } |
---|
| 371 | sub instance { return undef; } |
---|
| 372 | sub realm { return undef; } |
---|
| 373 | sub opcode { return undef; } |
---|
| 374 | sub header { return undef; } |
---|
| 375 | sub host { return undef; } |
---|
| 376 | sub hostname { return undef; } |
---|
| 377 | sub auth { return undef; } |
---|
| 378 | sub fields { return undef; } |
---|
| 379 | sub zsig { return undef; } |
---|
| 380 | sub zwriteline { return undef; } |
---|
[87c6ef1] | 381 | sub login_host { return undef; } |
---|
| 382 | sub login_tty { return undef; } |
---|
[f1e629d] | 383 | |
---|
[57cf4f9] | 384 | # This is for back-compat with old messages that set these properties |
---|
| 385 | # New protocol implementations are encourages to user override these |
---|
| 386 | # methods. |
---|
| 387 | sub replycmd { return shift->{replycmd}}; |
---|
| 388 | sub replysendercmd { return shift->{replysendercmd}}; |
---|
| 389 | |
---|
[ae47efb] | 390 | sub pretty_sender { return shift->sender; } |
---|
| 391 | sub pretty_recipient { return shift->recipient; } |
---|
[f1e629d] | 392 | |
---|
[dfaa47d] | 393 | # Override if you want a context (instance, network, etc.) on personals |
---|
| 394 | sub personal_context { return ""; } |
---|
[df7018f] | 395 | # extra short version, for use where space is especially tight |
---|
| 396 | # (eg, the oneline style) |
---|
| 397 | sub short_personal_context { return ""; } |
---|
[dfaa47d] | 398 | |
---|
[f1e629d] | 399 | sub delete { |
---|
| 400 | my ($m) = @_; |
---|
[8203afd] | 401 | &BarnOwl::command("delete --id ".$m->id); |
---|
[f1e629d] | 402 | } |
---|
| 403 | |
---|
| 404 | sub undelete { |
---|
| 405 | my ($m) = @_; |
---|
[8203afd] | 406 | &BarnOwl::command("undelete --id ".$m->id); |
---|
[f1e629d] | 407 | } |
---|
| 408 | |
---|
| 409 | # Serializes the message into something similar to the zwgc->vt format |
---|
| 410 | sub serialize { |
---|
| 411 | my ($this) = @_; |
---|
| 412 | my $s; |
---|
| 413 | for my $f (keys %$this) { |
---|
| 414 | my $val = $this->{$f}; |
---|
| 415 | if (ref($val) eq "ARRAY") { |
---|
| 416 | for my $i (0..@$val-1) { |
---|
| 417 | my $aval; |
---|
| 418 | $aval = $val->[$i]; |
---|
| 419 | $aval =~ s/\n/\n$f.$i: /g; |
---|
[186cdc4] | 420 | $s .= "$f.$i: $aval\n"; |
---|
[f1e629d] | 421 | } |
---|
| 422 | } else { |
---|
| 423 | $val =~ s/\n/\n$f: /g; |
---|
| 424 | $s .= "$f: $val\n"; |
---|
| 425 | } |
---|
| 426 | } |
---|
| 427 | return $s; |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | # Populate the annoying legacy global variables |
---|
| 431 | sub legacy_populate_global { |
---|
| 432 | my ($m) = @_; |
---|
[8203afd] | 433 | $BarnOwl::direction = $m->direction ; |
---|
| 434 | $BarnOwl::type = $m->type ; |
---|
| 435 | $BarnOwl::id = $m->id ; |
---|
| 436 | $BarnOwl::class = $m->class ; |
---|
| 437 | $BarnOwl::instance = $m->instance ; |
---|
| 438 | $BarnOwl::recipient = $m->recipient ; |
---|
| 439 | $BarnOwl::sender = $m->sender ; |
---|
| 440 | $BarnOwl::realm = $m->realm ; |
---|
| 441 | $BarnOwl::opcode = $m->opcode ; |
---|
| 442 | $BarnOwl::zsig = $m->zsig ; |
---|
| 443 | $BarnOwl::msg = $m->body ; |
---|
| 444 | $BarnOwl::time = $m->time ; |
---|
| 445 | $BarnOwl::host = $m->host ; |
---|
| 446 | $BarnOwl::login = $m->login ; |
---|
| 447 | $BarnOwl::auth = $m->auth ; |
---|
[f1e629d] | 448 | if ($m->fields) { |
---|
[8203afd] | 449 | @BarnOwl::fields = @{$m->fields}; |
---|
[f1e629d] | 450 | @main::fields = @{$m->fields}; |
---|
| 451 | } else { |
---|
[8203afd] | 452 | @BarnOwl::fields = undef; |
---|
[f1e629d] | 453 | @main::fields = undef; |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | |
---|
[25729b2] | 457 | sub smartfilter { |
---|
[0337203] | 458 | die("smartfilter not supported for this message\n"); |
---|
[25729b2] | 459 | } |
---|
| 460 | |
---|
[6e6ded7] | 461 | # Display fields -- overridden by subclasses when needed |
---|
| 462 | sub login_type {""} |
---|
| 463 | sub login_extra {""} |
---|
| 464 | sub long_sender {""} |
---|
| 465 | |
---|
| 466 | # The context in which a non-personal message was sent, e.g. a chat or |
---|
| 467 | # class |
---|
| 468 | sub context {""} |
---|
| 469 | |
---|
| 470 | # Some indicator of context *within* $self->context. e.g. the zephyr |
---|
| 471 | # instance |
---|
| 472 | sub subcontext {""} |
---|
| 473 | |
---|
[f1e629d] | 474 | ##################################################################### |
---|
| 475 | ##################################################################### |
---|
| 476 | |
---|
[8203afd] | 477 | package BarnOwl::Message::Admin; |
---|
[f1e629d] | 478 | |
---|
[8203afd] | 479 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 480 | |
---|
| 481 | sub header { return shift->{"header"}; } |
---|
| 482 | |
---|
| 483 | ##################################################################### |
---|
| 484 | ##################################################################### |
---|
| 485 | |
---|
[8203afd] | 486 | package BarnOwl::Message::Generic; |
---|
[f1e629d] | 487 | |
---|
[8203afd] | 488 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 489 | |
---|
| 490 | ##################################################################### |
---|
| 491 | ##################################################################### |
---|
| 492 | |
---|
[186cdc4] | 493 | package BarnOwl::Message::Loopback; |
---|
| 494 | |
---|
| 495 | use base qw( BarnOwl::Message ); |
---|
| 496 | |
---|
[cb06a43] | 497 | # all loopback messages are private |
---|
| 498 | sub is_private { |
---|
[186cdc4] | 499 | return 1; |
---|
| 500 | } |
---|
| 501 | |
---|
[740d5f7] | 502 | sub replycmd {return 'loopwrite';} |
---|
| 503 | sub replysendercmd {return 'loopwrite';} |
---|
| 504 | |
---|
[186cdc4] | 505 | ##################################################################### |
---|
| 506 | ##################################################################### |
---|
| 507 | |
---|
[8203afd] | 508 | package BarnOwl::Message::AIM; |
---|
[f1e629d] | 509 | |
---|
[8203afd] | 510 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 511 | |
---|
[cb06a43] | 512 | # all non-loginout AIM messages are private for now... |
---|
| 513 | sub is_private { |
---|
[f1e629d] | 514 | return !(shift->is_loginout); |
---|
| 515 | } |
---|
| 516 | |
---|
[740d5f7] | 517 | sub replycmd { |
---|
| 518 | my $self = shift; |
---|
| 519 | if ($self->is_incoming) { |
---|
| 520 | return "aimwrite " . BarnOwl::quote($self->sender); |
---|
| 521 | } else { |
---|
| 522 | return "aimwrite " . BarnOwl::quote($self->recipient); |
---|
| 523 | } |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | sub replysendercmd { |
---|
| 527 | return shift->replycmd; |
---|
| 528 | } |
---|
| 529 | |
---|
[f1e629d] | 530 | ##################################################################### |
---|
| 531 | ##################################################################### |
---|
| 532 | |
---|
[8203afd] | 533 | package BarnOwl::Message::Zephyr; |
---|
[f1e629d] | 534 | |
---|
[740d5f7] | 535 | use constant WEBZEPHYR_PRINCIPAL => "daemon.webzephyr"; |
---|
| 536 | use constant WEBZEPHYR_CLASS => "webzephyr"; |
---|
| 537 | use constant WEBZEPHYR_OPCODE => "webzephyr"; |
---|
| 538 | |
---|
[8203afd] | 539 | use base qw( BarnOwl::Message ); |
---|
[f1e629d] | 540 | |
---|
[740d5f7] | 541 | sub strip_realm { |
---|
| 542 | my $sender = shift; |
---|
| 543 | my $realm = BarnOwl::zephyr_getrealm(); |
---|
| 544 | $sender =~ s/\@$realm$//; |
---|
| 545 | return $sender; |
---|
| 546 | } |
---|
| 547 | |
---|
[6e6ded7] | 548 | sub login_type { |
---|
| 549 | return (shift->zsig eq "") ? "(PSEUDO)" : ""; |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | sub login_extra { |
---|
| 553 | my $m = shift; |
---|
| 554 | return undef if (!$m->is_loginout); |
---|
| 555 | my $s = lc($m->host); |
---|
| 556 | $s .= " " . $m->login_tty if defined $m->login_tty; |
---|
| 557 | return $s; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | sub long_sender { |
---|
| 561 | my $m = shift; |
---|
| 562 | return $m->zsig; |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | sub context { |
---|
| 566 | return shift->class; |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | sub subcontext { |
---|
| 570 | return shift->instance; |
---|
| 571 | } |
---|
| 572 | |
---|
[186cdc4] | 573 | sub login_tty { |
---|
[f1e629d] | 574 | my ($m) = @_; |
---|
| 575 | return undef if (!$m->is_loginout); |
---|
| 576 | return $m->fields->[2]; |
---|
| 577 | } |
---|
| 578 | |
---|
[186cdc4] | 579 | sub login_host { |
---|
[f1e629d] | 580 | my ($m) = @_; |
---|
| 581 | return undef if (!$m->is_loginout); |
---|
| 582 | return $m->fields->[0]; |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | sub zwriteline { return shift->{"zwriteline"}; } |
---|
| 586 | |
---|
| 587 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
---|
| 588 | |
---|
[186cdc4] | 589 | sub is_personal { |
---|
[f1e629d] | 590 | my ($m) = @_; |
---|
| 591 | return ((lc($m->class) eq "message") |
---|
| 592 | && $m->is_private); |
---|
| 593 | } |
---|
| 594 | |
---|
[186cdc4] | 595 | sub is_mail { |
---|
[f1e629d] | 596 | my ($m) = @_; |
---|
| 597 | return ((lc($m->class) eq "mail") && $m->is_private); |
---|
| 598 | } |
---|
| 599 | |
---|
| 600 | sub pretty_sender { |
---|
| 601 | my ($m) = @_; |
---|
[740d5f7] | 602 | return strip_realm($m->sender); |
---|
[f1e629d] | 603 | } |
---|
| 604 | |
---|
[ae47efb] | 605 | sub pretty_recipient { |
---|
| 606 | my ($m) = @_; |
---|
[740d5f7] | 607 | return strip_realm($m->recipient); |
---|
[ae47efb] | 608 | } |
---|
| 609 | |
---|
[6223638] | 610 | # Portion of the reply command that preserves the context |
---|
| 611 | sub context_reply_cmd { |
---|
| 612 | my $m = shift; |
---|
| 613 | my $class = ""; |
---|
| 614 | if (lc($m->class) ne "message") { |
---|
| 615 | $class = "-c " . BarnOwl::quote($m->class); |
---|
| 616 | } |
---|
| 617 | my $instance = ""; |
---|
| 618 | if (lc($m->instance) ne "personal") { |
---|
| 619 | $instance = "-i " . BarnOwl::quote($m->instance); |
---|
| 620 | } |
---|
| 621 | if (($class eq "") or ($instance eq "")) { |
---|
| 622 | return $class . $instance; |
---|
[dfaa47d] | 623 | } else { |
---|
[6223638] | 624 | return $class . " " . $instance; |
---|
[dfaa47d] | 625 | } |
---|
| 626 | } |
---|
| 627 | |
---|
[6223638] | 628 | sub personal_context { |
---|
| 629 | my ($m) = @_; |
---|
| 630 | return $m->context_reply_cmd(); |
---|
| 631 | } |
---|
| 632 | |
---|
[df7018f] | 633 | sub short_personal_context { |
---|
| 634 | my ($m) = @_; |
---|
| 635 | if(lc($m->class) eq 'message') |
---|
| 636 | { |
---|
| 637 | if(lc($m->instance) eq 'personal') |
---|
| 638 | { |
---|
| 639 | return ''; |
---|
| 640 | } else { |
---|
| 641 | return $m->instance; |
---|
| 642 | } |
---|
| 643 | } else { |
---|
| 644 | return $m->class; |
---|
| 645 | } |
---|
| 646 | } |
---|
| 647 | |
---|
[f1e629d] | 648 | # These are arguably zephyr-specific |
---|
| 649 | sub class { return shift->{"class"}; } |
---|
| 650 | sub instance { return shift->{"instance"}; } |
---|
| 651 | sub realm { return shift->{"realm"}; } |
---|
| 652 | sub opcode { return shift->{"opcode"}; } |
---|
| 653 | sub host { return shift->{"hostname"}; } |
---|
| 654 | sub hostname { return shift->{"hostname"}; } |
---|
| 655 | sub header { return shift->{"header"}; } |
---|
| 656 | sub auth { return shift->{"auth"}; } |
---|
| 657 | sub fields { return shift->{"fields"}; } |
---|
| 658 | sub zsig { return shift->{"zsig"}; } |
---|
| 659 | |
---|
[740d5f7] | 660 | sub zephyr_cc { |
---|
| 661 | my $self = shift; |
---|
| 662 | return $1 if $self->body =~ /^\s*cc:\s+([^\n]+)/i; |
---|
| 663 | return undef; |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | sub replycmd { |
---|
| 667 | my $self = shift; |
---|
| 668 | my $sender = shift; |
---|
| 669 | $sender = 0 unless defined $sender; |
---|
| 670 | my ($class, $instance, $to, $cc); |
---|
| 671 | if($self->is_outgoing) { |
---|
| 672 | return $self->{zwriteline}; |
---|
| 673 | } |
---|
| 674 | |
---|
| 675 | if($sender && $self->opcode eq WEBZEPHYR_OPCODE) { |
---|
| 676 | $class = WEBZEPHYR_CLASS; |
---|
[ca53b77] | 677 | $instance = $self->pretty_sender; |
---|
[2b6de9d] | 678 | $instance =~ s/-webzephyr$//; |
---|
[740d5f7] | 679 | $to = WEBZEPHYR_PRINCIPAL; |
---|
| 680 | } elsif($self->class eq WEBZEPHYR_CLASS |
---|
| 681 | && $self->is_loginout) { |
---|
| 682 | $class = WEBZEPHYR_CLASS; |
---|
| 683 | $instance = $self->instance; |
---|
| 684 | $to = WEBZEPHYR_PRINCIPAL; |
---|
[f0f919d] | 685 | } elsif($self->is_loginout) { |
---|
| 686 | $class = 'MESSAGE'; |
---|
| 687 | $instance = 'PERSONAL'; |
---|
| 688 | $to = $self->sender; |
---|
| 689 | } elsif($sender && !$self->is_private) { |
---|
| 690 | # Possible future feature: (Optionally?) include the class and/or |
---|
| 691 | # instance of the message being replied to in the instance of the |
---|
| 692 | # outgoing personal reply |
---|
[740d5f7] | 693 | $class = 'MESSAGE'; |
---|
| 694 | $instance = 'PERSONAL'; |
---|
| 695 | $to = $self->sender; |
---|
| 696 | } else { |
---|
| 697 | $class = $self->class; |
---|
| 698 | $instance = $self->instance; |
---|
| 699 | $to = $self->recipient; |
---|
| 700 | $cc = $self->zephyr_cc(); |
---|
| 701 | if($to eq '*' || $to eq '') { |
---|
| 702 | $to = ''; |
---|
| 703 | } elsif($to !~ /^@/) { |
---|
| 704 | $to = $self->sender; |
---|
| 705 | } |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | my $cmd; |
---|
| 709 | if(lc $self->opcode eq 'crypt') { |
---|
| 710 | $cmd = 'zcrypt'; |
---|
| 711 | } else { |
---|
| 712 | $cmd = 'zwrite'; |
---|
| 713 | } |
---|
| 714 | |
---|
[6223638] | 715 | my $context_part = $self->context_reply_cmd(); |
---|
| 716 | $cmd .= " " . $context_part unless ($context_part eq ''); |
---|
[740d5f7] | 717 | if ($to ne '') { |
---|
| 718 | $to = strip_realm($to); |
---|
| 719 | if (defined $cc) { |
---|
| 720 | my @cc = grep /^[^-]/, ($to, split /\s+/, $cc); |
---|
| 721 | my %cc = map {$_ => 1} @cc; |
---|
| 722 | delete $cc{strip_realm(BarnOwl::zephyr_getsender())}; |
---|
| 723 | @cc = keys %cc; |
---|
| 724 | $cmd .= " -C " . join(" ", @cc); |
---|
| 725 | } else { |
---|
| 726 | if(BarnOwl::getvar('smartstrip') eq 'on') { |
---|
| 727 | $to = BarnOwl::zephyr_smartstrip_user($to); |
---|
| 728 | } |
---|
| 729 | $cmd .= " $to"; |
---|
| 730 | } |
---|
| 731 | } |
---|
| 732 | return $cmd; |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | sub replysendercmd { |
---|
| 736 | my $self = shift; |
---|
| 737 | return $self->replycmd(1); |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | ##################################################################### |
---|
[f1e629d] | 741 | ##################################################################### |
---|
| 742 | ##################################################################### |
---|
[7e470da] | 743 | |
---|
[0337203] | 744 | package BarnOwl::Hook; |
---|
[7e470da] | 745 | |
---|
[1a64de6] | 746 | =head1 BarnOwl::Hook |
---|
| 747 | |
---|
| 748 | =head1 DESCRIPTION |
---|
| 749 | |
---|
| 750 | A C<BarnOwl::Hook> represents a list of functions to be triggered on |
---|
| 751 | some event. C<BarnOwl> exports a default set of these (see |
---|
| 752 | C<BarnOwl::Hooks>), but can also be created and used by module code. |
---|
| 753 | |
---|
| 754 | =head2 new |
---|
| 755 | |
---|
| 756 | Creates a new Hook object |
---|
| 757 | |
---|
| 758 | =cut |
---|
| 759 | |
---|
[0337203] | 760 | sub new { |
---|
| 761 | my $class = shift; |
---|
| 762 | return bless [], $class; |
---|
| 763 | } |
---|
[7e470da] | 764 | |
---|
[1a64de6] | 765 | =head2 run [ARGS] |
---|
| 766 | |
---|
| 767 | Calls each of the functions registered with this hook with the given |
---|
| 768 | arguments. |
---|
| 769 | |
---|
| 770 | =cut |
---|
| 771 | |
---|
[0337203] | 772 | sub run { |
---|
| 773 | my $self = shift; |
---|
| 774 | my @args = @_; |
---|
[167044b] | 775 | return map {$self->_run($_,@args)} @$self; |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | sub _run { |
---|
| 779 | my $self = shift; |
---|
| 780 | my $fn = shift; |
---|
| 781 | my @args = @_; |
---|
| 782 | no strict 'refs'; |
---|
| 783 | return $fn->(@args); |
---|
[7e470da] | 784 | } |
---|
[0337203] | 785 | |
---|
[1a64de6] | 786 | =head2 add SUBREF |
---|
| 787 | |
---|
| 788 | Registers a given subroutine with this hook |
---|
| 789 | |
---|
| 790 | =cut |
---|
| 791 | |
---|
[0337203] | 792 | sub add { |
---|
| 793 | my $self = shift; |
---|
| 794 | my $func = shift; |
---|
[167044b] | 795 | die("Not a coderef!") unless ref($func) eq 'CODE' || !ref($func); |
---|
| 796 | return if grep {$_ eq $func} @$self; |
---|
[0337203] | 797 | push @$self, $func; |
---|
[7e470da] | 798 | } |
---|
| 799 | |
---|
[1a64de6] | 800 | =head2 clear |
---|
| 801 | |
---|
| 802 | Remove all functions registered with this hook. |
---|
| 803 | |
---|
| 804 | =cut |
---|
| 805 | |
---|
[0337203] | 806 | sub clear { |
---|
| 807 | my $self = shift; |
---|
| 808 | @$self = (); |
---|
[7e470da] | 809 | } |
---|
| 810 | |
---|
[0337203] | 811 | package BarnOwl::Hooks; |
---|
[7e470da] | 812 | |
---|
[1a64de6] | 813 | =head1 BarnOwl::Hooks |
---|
| 814 | |
---|
| 815 | =head1 DESCRIPTION |
---|
| 816 | |
---|
| 817 | C<BarnOwl::Hooks> exports a set of C<BarnOwl::Hook> objects made |
---|
| 818 | available by BarnOwl internally. |
---|
| 819 | |
---|
| 820 | =head2 USAGE |
---|
| 821 | |
---|
| 822 | Modules wishing to respond to events in BarnOwl should register |
---|
| 823 | functions with these hooks. |
---|
| 824 | |
---|
| 825 | =head2 EXPORTS |
---|
| 826 | |
---|
| 827 | None by default. Either import the hooks you need explicitly, or refer |
---|
| 828 | to them with fully-qualified names. Available hooks are: |
---|
| 829 | |
---|
| 830 | =over 4 |
---|
| 831 | |
---|
| 832 | =item $startup |
---|
| 833 | |
---|
| 834 | Called on BarnOwl startup, and whenever modules are |
---|
| 835 | reloaded. Functions registered with the C<$startup> hook get a true |
---|
| 836 | argument if this is a reload, and false if this is a true startup |
---|
| 837 | |
---|
| 838 | =item $shutdown |
---|
| 839 | |
---|
| 840 | Called before BarnOwl shutdown |
---|
| 841 | |
---|
| 842 | =item $receiveMessage |
---|
| 843 | |
---|
[e9708d2] | 844 | Called with a C<BarnOwl::Message> object every time BarnOwl receives a |
---|
| 845 | new incoming message. |
---|
| 846 | |
---|
| 847 | =item $newMessage |
---|
| 848 | |
---|
| 849 | Called with a C<BarnOwl::Message> object every time BarnOwl appends |
---|
| 850 | I<any> new message to the message list. |
---|
[1a64de6] | 851 | |
---|
| 852 | =item $mainLoop |
---|
| 853 | |
---|
[b07d8c8] | 854 | Called on every pass through the C<BarnOwl> main loop. This is |
---|
| 855 | guaranteed to be called at least once/sec and may be called more |
---|
| 856 | frequently. |
---|
[1a64de6] | 857 | |
---|
| 858 | =item $getBuddyList |
---|
| 859 | |
---|
| 860 | Called to display buddy lists for all protocol handlers. The result |
---|
| 861 | from every function registered with this hook will be appended and |
---|
| 862 | displayed in a popup window, with zephyr formatting parsed. |
---|
| 863 | |
---|
[799b60e] | 864 | =item $getQuickstart |
---|
| 865 | |
---|
| 866 | Called by :show quickstart to display 2-5 lines of help on how to |
---|
| 867 | start using the protocol. The result from every function registered |
---|
| 868 | with this hook will be appended and displayed in an admin message, |
---|
| 869 | with zephyr formatting parsed. The format should be |
---|
| 870 | "@b(Protocol:)\nSome text.\nMore text.\n" |
---|
| 871 | |
---|
[1a64de6] | 872 | =back |
---|
| 873 | |
---|
| 874 | =cut |
---|
| 875 | |
---|
[0337203] | 876 | use Exporter; |
---|
| 877 | |
---|
| 878 | our @EXPORT_OK = qw($startup $shutdown |
---|
[0f9eca7] | 879 | $receiveMessage $newMessage |
---|
[799b60e] | 880 | $mainLoop $getBuddyList |
---|
| 881 | $getQuickstart); |
---|
[0337203] | 882 | |
---|
| 883 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
---|
| 884 | |
---|
| 885 | our $startup = BarnOwl::Hook->new; |
---|
| 886 | our $shutdown = BarnOwl::Hook->new; |
---|
| 887 | our $receiveMessage = BarnOwl::Hook->new; |
---|
[0f9eca7] | 888 | our $newMessage = BarnOwl::Hook->new; |
---|
[0337203] | 889 | our $mainLoop = BarnOwl::Hook->new; |
---|
| 890 | our $getBuddyList = BarnOwl::Hook->new; |
---|
[799b60e] | 891 | our $getQuickstart = BarnOwl::Hook->new; |
---|
[0337203] | 892 | |
---|
| 893 | # Internal startup/shutdown routines called by the C code |
---|
[7e470da] | 894 | |
---|
[2650a10] | 895 | sub _load_perl_commands { |
---|
| 896 | # Load builtin perl commands |
---|
| 897 | BarnOwl::new_command(style => \&BarnOwl::Style::style_command, |
---|
| 898 | { |
---|
| 899 | summary => "creates a new style", |
---|
| 900 | usage => "style <name> perl <function_name>", |
---|
| 901 | description => |
---|
| 902 | "A style named <name> will be created that will\n" . |
---|
| 903 | "format messages using the perl function <function_name>.\n\n" . |
---|
| 904 | "SEE ALSO: show styles, view -s, filter -s\n\n" . |
---|
| 905 | "DEPRECATED in favor of BarnOwl::create_style(NAME, OBJECT)", |
---|
| 906 | }); |
---|
| 907 | } |
---|
| 908 | |
---|
[b6c067a] | 909 | sub _load_owlconf { |
---|
| 910 | # load the config file |
---|
| 911 | if ( -r $BarnOwl::configfile ) { |
---|
| 912 | undef $@; |
---|
[e8bc8ac] | 913 | package main; |
---|
[b6c067a] | 914 | do $BarnOwl::configfile; |
---|
[e2257be] | 915 | if($@) { |
---|
| 916 | BarnOwl::error("In startup: $@\n"); |
---|
| 917 | return; |
---|
| 918 | } |
---|
[e8bc8ac] | 919 | package BarnOwl; |
---|
[39dc159] | 920 | if(*BarnOwl::format_msg{CODE}) { |
---|
| 921 | # if the config defines a legacy formatting function, add 'perl' as a style |
---|
[b67ab6b] | 922 | BarnOwl::create_style("perl", BarnOwl::Style::Legacy->new( |
---|
| 923 | "BarnOwl::format_msg", |
---|
| 924 | "User-defined perl style that calls BarnOwl::format_msg" |
---|
| 925 | . " with legacy global variable support", |
---|
| 926 | 1)); |
---|
| 927 | BarnOwl::set("-q default_style perl"); |
---|
[39dc159] | 928 | } |
---|
[b6c067a] | 929 | } |
---|
| 930 | } |
---|
| 931 | |
---|
[b0c8011] | 932 | # These are the internal hooks called by the barnowl C code, which |
---|
| 933 | # take care of dispatching to the appropriate perl hooks, and deal |
---|
| 934 | # with compatibility by calling the old, fixed-name hooks. |
---|
| 935 | |
---|
[0337203] | 936 | sub _startup { |
---|
[2650a10] | 937 | _load_perl_commands(); |
---|
[0337203] | 938 | _load_owlconf(); |
---|
[8203afd] | 939 | |
---|
[0337203] | 940 | if(eval {require BarnOwl::ModuleLoader}) { |
---|
| 941 | eval { |
---|
| 942 | BarnOwl::ModuleLoader->load_all; |
---|
| 943 | }; |
---|
[f6b319c] | 944 | BarnOwl::error("$@") if $@; |
---|
| 945 | |
---|
[0337203] | 946 | } else { |
---|
| 947 | BarnOwl::error("Can't load BarnOwl::ModuleLoader, loadable module support disabled:\n$@"); |
---|
| 948 | } |
---|
| 949 | |
---|
[836e6263] | 950 | $startup->run(0); |
---|
[8203afd] | 951 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
---|
| 952 | } |
---|
| 953 | |
---|
[0337203] | 954 | sub _shutdown { |
---|
| 955 | $shutdown->run; |
---|
| 956 | |
---|
[8203afd] | 957 | BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; |
---|
| 958 | } |
---|
| 959 | |
---|
[0337203] | 960 | sub _receive_msg { |
---|
[7e470da] | 961 | my $m = shift; |
---|
[0337203] | 962 | |
---|
| 963 | $receiveMessage->run($m); |
---|
| 964 | |
---|
[8203afd] | 965 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
---|
[7e470da] | 966 | } |
---|
| 967 | |
---|
[0f9eca7] | 968 | sub _new_msg { |
---|
| 969 | my $m = shift; |
---|
| 970 | |
---|
| 971 | $newMessage->run($m); |
---|
| 972 | |
---|
| 973 | BarnOwl::new_msg($m) if *BarnOwl::new_msg{CODE}; |
---|
| 974 | } |
---|
| 975 | |
---|
[0337203] | 976 | sub _mainloop_hook { |
---|
| 977 | $mainLoop->run; |
---|
| 978 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
---|
| 979 | } |
---|
[7e470da] | 980 | |
---|
[0337203] | 981 | sub _get_blist { |
---|
| 982 | return join("\n", $getBuddyList->run); |
---|
[7e470da] | 983 | } |
---|
[dd16bdd] | 984 | |
---|
[799b60e] | 985 | sub _get_quickstart { |
---|
| 986 | return join("\n", $getQuickstart->run); |
---|
| 987 | } |
---|
| 988 | |
---|
[b6c067a] | 989 | ################################################################################ |
---|
| 990 | # Built-in perl styles |
---|
| 991 | ################################################################################ |
---|
| 992 | package BarnOwl::Style::Default; |
---|
| 993 | ################################################################################ |
---|
| 994 | # Branching point for various formatting functions in this style. |
---|
| 995 | ################################################################################ |
---|
[426735d] | 996 | sub format_message |
---|
[b6c067a] | 997 | { |
---|
[864ed35] | 998 | my $self = shift; |
---|
[811ad93] | 999 | my $m = shift; |
---|
| 1000 | my $fmt; |
---|
[b6c067a] | 1001 | |
---|
[6e6ded7] | 1002 | if ( $m->is_loginout) { |
---|
[811ad93] | 1003 | $fmt = $self->format_login($m); |
---|
[18fb3d4f] | 1004 | } elsif($m->is_ping && $m->is_personal) { |
---|
[811ad93] | 1005 | $fmt = $self->format_ping($m); |
---|
[6e6ded7] | 1006 | } elsif($m->is_admin) { |
---|
[811ad93] | 1007 | $fmt = $self->format_admin($m); |
---|
[6e6ded7] | 1008 | } else { |
---|
[811ad93] | 1009 | $fmt = $self->format_chat($m); |
---|
[b6c067a] | 1010 | } |
---|
[811ad93] | 1011 | $fmt = BarnOwl::Style::boldify($fmt) if $self->should_bold($m); |
---|
| 1012 | return $fmt; |
---|
| 1013 | } |
---|
| 1014 | |
---|
| 1015 | sub should_bold { |
---|
| 1016 | my $self = shift; |
---|
| 1017 | my $m = shift; |
---|
| 1018 | return $m->is_personal && $m->direction eq "in"; |
---|
[b6c067a] | 1019 | } |
---|
| 1020 | |
---|
[864ed35] | 1021 | sub description {"Default style";} |
---|
| 1022 | |
---|
[b67ab6b] | 1023 | BarnOwl::create_style("default", "BarnOwl::Style::Default"); |
---|
[b6c067a] | 1024 | |
---|
| 1025 | ################################################################################ |
---|
| 1026 | |
---|
[426735d] | 1027 | sub format_time { |
---|
[6b3878b] | 1028 | my $self = shift; |
---|
[6e6ded7] | 1029 | my $m = shift; |
---|
[b6c067a] | 1030 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
---|
[6e6ded7] | 1031 | return $time; |
---|
[b6c067a] | 1032 | } |
---|
| 1033 | |
---|
[426735d] | 1034 | sub format_login { |
---|
[864ed35] | 1035 | my $self = shift; |
---|
[b6c067a] | 1036 | my $m = shift; |
---|
[6e6ded7] | 1037 | return sprintf( |
---|
| 1038 | '@b<%s%s> for @b(%s) (%s) %s', |
---|
| 1039 | uc( $m->login ), |
---|
| 1040 | $m->login_type, |
---|
| 1041 | $m->pretty_sender, |
---|
| 1042 | $m->login_extra, |
---|
[426735d] | 1043 | $self->format_time($m) |
---|
[6e6ded7] | 1044 | ); |
---|
[b6c067a] | 1045 | } |
---|
| 1046 | |
---|
[864ed35] | 1047 | sub format_ping { |
---|
| 1048 | my $self = shift; |
---|
| 1049 | my $m = shift; |
---|
[33539f7] | 1050 | my $personal_context = $m->personal_context; |
---|
| 1051 | $personal_context = ' [' . $personal_context . ']' if $personal_context; |
---|
| 1052 | return "\@b(PING)" . $personal_context . " from \@b(" . $m->pretty_sender . ")"; |
---|
[864ed35] | 1053 | } |
---|
| 1054 | |
---|
| 1055 | sub format_admin { |
---|
| 1056 | my $self = shift; |
---|
| 1057 | my $m = shift; |
---|
[811ad93] | 1058 | return "\@bold(OWL ADMIN)\n" . $self->indent_body($m); |
---|
[864ed35] | 1059 | } |
---|
| 1060 | |
---|
[426735d] | 1061 | sub format_chat { |
---|
[864ed35] | 1062 | my $self = shift; |
---|
[b6c067a] | 1063 | my $m = shift; |
---|
[811ad93] | 1064 | my $header = $self->chat_header($m); |
---|
| 1065 | return $header . "\n". $self->indent_body($m); |
---|
| 1066 | } |
---|
| 1067 | |
---|
| 1068 | sub chat_header { |
---|
| 1069 | my $self = shift; |
---|
| 1070 | my $m = shift; |
---|
[6e6ded7] | 1071 | my $header; |
---|
| 1072 | if ( $m->is_personal ) { |
---|
[dfaa47d] | 1073 | my $personal_context = $m->personal_context; |
---|
| 1074 | $personal_context = ' [' . $personal_context . ']' if $personal_context; |
---|
| 1075 | |
---|
[6e6ded7] | 1076 | if ( $m->direction eq "out" ) { |
---|
[dfaa47d] | 1077 | $header = ucfirst $m->type . $personal_context . " sent to " . $m->pretty_recipient; |
---|
[6e6ded7] | 1078 | } else { |
---|
[dfaa47d] | 1079 | $header = ucfirst $m->type . $personal_context . " from " . $m->pretty_sender; |
---|
[6e6ded7] | 1080 | } |
---|
| 1081 | } else { |
---|
| 1082 | $header = $m->context; |
---|
[37dd88c] | 1083 | if(defined $m->subcontext) { |
---|
[c39999f] | 1084 | $header .= ' / ' . $m->subcontext; |
---|
[6e6ded7] | 1085 | } |
---|
[c39999f] | 1086 | $header .= ' / @b{' . $m->pretty_sender . '}'; |
---|
[6e6ded7] | 1087 | } |
---|
[b6c067a] | 1088 | |
---|
[b51d257] | 1089 | if($m->opcode) { |
---|
| 1090 | $header .= " [" . $m->opcode . "]"; |
---|
| 1091 | } |
---|
[426735d] | 1092 | $header .= " " . $self->format_time($m); |
---|
[811ad93] | 1093 | $header .= $self->format_sender($m); |
---|
| 1094 | return $header; |
---|
| 1095 | } |
---|
| 1096 | |
---|
| 1097 | sub format_sender { |
---|
| 1098 | my $self = shift; |
---|
| 1099 | my $m = shift; |
---|
[0449730] | 1100 | my $sender = $m->long_sender; |
---|
| 1101 | $sender =~ s/\n.*$//s; |
---|
[19aeff4] | 1102 | if (BarnOwl::getvar('colorztext') eq 'on') { |
---|
| 1103 | return " (" . $sender . '@color[default]' . ")"; |
---|
| 1104 | } else { |
---|
| 1105 | return " ($sender)"; |
---|
| 1106 | } |
---|
[b6c067a] | 1107 | } |
---|
| 1108 | |
---|
[426735d] | 1109 | sub indent_body |
---|
[b6c067a] | 1110 | { |
---|
[811ad93] | 1111 | my $self = shift; |
---|
[b6c067a] | 1112 | my $m = shift; |
---|
[186cdc4] | 1113 | |
---|
[b6c067a] | 1114 | my $body = $m->body; |
---|
[f6b319c] | 1115 | if ($m->{should_wordwrap}) { |
---|
[7743955] | 1116 | $body = BarnOwl::wordwrap($body, BarnOwl::getnumcols()-9); |
---|
[f6b319c] | 1117 | } |
---|
[186cdc4] | 1118 | # replace newline followed by anything with |
---|
[b6c067a] | 1119 | # newline plus four spaces and that thing. |
---|
| 1120 | $body =~ s/\n(.)/\n $1/g; |
---|
[f2d72128] | 1121 | # Trim trailing newlines. |
---|
| 1122 | $body =~ s/\n*$//; |
---|
[b6c067a] | 1123 | return " ".$body; |
---|
| 1124 | } |
---|
| 1125 | |
---|
[864ed35] | 1126 | package BarnOwl::Style::Basic; |
---|
| 1127 | our @ISA=qw(BarnOwl::Style::Default); |
---|
| 1128 | |
---|
| 1129 | sub description {"Compatability alias for the default style";} |
---|
| 1130 | |
---|
[b67ab6b] | 1131 | BarnOwl::create_style("basic", "BarnOwl::Style::Basic"); |
---|
[864ed35] | 1132 | |
---|
[f2d72128] | 1133 | package BarnOwl::Style::OneLine; |
---|
[811ad93] | 1134 | # Inherit format_message to dispatch |
---|
| 1135 | our @ISA = qw(BarnOwl::Style::Default); |
---|
[f2d72128] | 1136 | |
---|
[811ad93] | 1137 | use constant BASE_FORMAT => '%s %-13.13s %-11.11s %-12.12s '; |
---|
[f2d72128] | 1138 | |
---|
[864ed35] | 1139 | sub description {"Formats for one-line-per-message"} |
---|
| 1140 | |
---|
[b67ab6b] | 1141 | BarnOwl::create_style("oneline", "BarnOwl::Style::OneLine"); |
---|
[f2d72128] | 1142 | |
---|
| 1143 | ################################################################################ |
---|
| 1144 | |
---|
[5008e51] | 1145 | sub maybe { |
---|
| 1146 | my $thing = shift; |
---|
| 1147 | return defined($thing) ? $thing : ""; |
---|
| 1148 | } |
---|
| 1149 | |
---|
[426735d] | 1150 | sub format_login { |
---|
[864ed35] | 1151 | my $self = shift; |
---|
[f2d72128] | 1152 | my $m = shift; |
---|
| 1153 | return sprintf( |
---|
| 1154 | BASE_FORMAT, |
---|
| 1155 | '<', |
---|
| 1156 | $m->type, |
---|
| 1157 | uc( $m->login ), |
---|
| 1158 | $m->pretty_sender) |
---|
| 1159 | . ($m->login_extra ? "at ".$m->login_extra : ''); |
---|
| 1160 | } |
---|
| 1161 | |
---|
[426735d] | 1162 | sub format_ping { |
---|
[2017d07] | 1163 | my $self = shift; |
---|
[f2d72128] | 1164 | my $m = shift; |
---|
| 1165 | return sprintf( |
---|
| 1166 | BASE_FORMAT, |
---|
| 1167 | '<', |
---|
| 1168 | $m->type, |
---|
| 1169 | 'PING', |
---|
| 1170 | $m->pretty_sender) |
---|
| 1171 | } |
---|
| 1172 | |
---|
[426735d] | 1173 | sub format_chat |
---|
[f2d72128] | 1174 | { |
---|
[864ed35] | 1175 | my $self = shift; |
---|
[f2d72128] | 1176 | my $m = shift; |
---|
| 1177 | my $dir = lc($m->{direction}); |
---|
| 1178 | my $dirsym = '-'; |
---|
| 1179 | if ($dir eq 'in') { |
---|
| 1180 | $dirsym = '<'; |
---|
| 1181 | } |
---|
| 1182 | elsif ($dir eq 'out') { |
---|
| 1183 | $dirsym = '>'; |
---|
| 1184 | } |
---|
| 1185 | |
---|
| 1186 | my $line; |
---|
| 1187 | if ($m->is_personal) { |
---|
[df7018f] | 1188 | |
---|
| 1189 | # Figure out what to show in the subcontext column |
---|
[f2d72128] | 1190 | $line= sprintf(BASE_FORMAT, |
---|
[811ad93] | 1191 | $dirsym, |
---|
| 1192 | $m->type, |
---|
[df7018f] | 1193 | maybe($m->short_personal_context), |
---|
[811ad93] | 1194 | ($dir eq 'out' |
---|
| 1195 | ? $m->pretty_recipient |
---|
| 1196 | : $m->pretty_sender)); |
---|
[f2d72128] | 1197 | } |
---|
| 1198 | else { |
---|
| 1199 | $line = sprintf(BASE_FORMAT, |
---|
[811ad93] | 1200 | $dirsym, |
---|
[5008e51] | 1201 | maybe($m->context), |
---|
| 1202 | maybe($m->subcontext), |
---|
[811ad93] | 1203 | ($dir eq 'out' |
---|
| 1204 | ? $m->pretty_recipient |
---|
| 1205 | : $m->pretty_sender)); |
---|
[f2d72128] | 1206 | } |
---|
| 1207 | |
---|
| 1208 | my $body = $m->{body}; |
---|
| 1209 | $body =~ tr/\n/ /; |
---|
| 1210 | $line .= $body; |
---|
| 1211 | return $line; |
---|
| 1212 | } |
---|
| 1213 | |
---|
[811ad93] | 1214 | # Format owl admin messages |
---|
[426735d] | 1215 | sub format_admin |
---|
[f2d72128] | 1216 | { |
---|
[864ed35] | 1217 | my $self = shift; |
---|
[f2d72128] | 1218 | my $m = shift; |
---|
[811ad93] | 1219 | my $line = sprintf(BASE_FORMAT, '<', 'ADMIN', '', ''); |
---|
[f2d72128] | 1220 | my $body = $m->{body}; |
---|
| 1221 | $body =~ tr/\n/ /; |
---|
| 1222 | return $line.$body; |
---|
| 1223 | } |
---|
[b6c067a] | 1224 | |
---|
| 1225 | package BarnOwl::Style; |
---|
| 1226 | |
---|
| 1227 | # This takes a zephyr to be displayed and modifies it to be displayed |
---|
| 1228 | # entirely in bold. |
---|
[426735d] | 1229 | sub boldify |
---|
[b6c067a] | 1230 | { |
---|
| 1231 | local $_ = shift; |
---|
| 1232 | if ( !(/\)/) ) { |
---|
| 1233 | return '@b(' . $_ . ')'; |
---|
| 1234 | } elsif ( !(/\>/) ) { |
---|
| 1235 | return '@b<' . $_ . '>'; |
---|
| 1236 | } elsif ( !(/\}/) ) { |
---|
| 1237 | return '@b{' . $_ . '}'; |
---|
| 1238 | } elsif ( !(/\]/) ) { |
---|
| 1239 | return '@b[' . $_ . ']'; |
---|
| 1240 | } else { |
---|
| 1241 | my $txt = "\@b($_"; |
---|
| 1242 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
---|
| 1243 | return $txt . ')'; |
---|
| 1244 | } |
---|
| 1245 | } |
---|
| 1246 | |
---|
[2650a10] | 1247 | sub style_command { |
---|
| 1248 | my $command = shift; |
---|
| 1249 | if(scalar @_ != 3 || $_[1] ne 'perl') { |
---|
| 1250 | die("Usage: style <name> perl <function>\n"); |
---|
| 1251 | } |
---|
| 1252 | my $name = shift; |
---|
| 1253 | my $perl = shift; |
---|
| 1254 | my $fn = shift; |
---|
| 1255 | { |
---|
[b441079] | 1256 | # For historical reasons, assume unqualified references are |
---|
| 1257 | # in main:: |
---|
| 1258 | package main; |
---|
[2650a10] | 1259 | no strict 'refs'; |
---|
| 1260 | unless(*{$fn}{CODE}) { |
---|
| 1261 | die("Unable to create style '$name': no perl function '$fn'\n"); |
---|
| 1262 | } |
---|
| 1263 | } |
---|
| 1264 | BarnOwl::create_style($name, BarnOwl::Style::Legacy->new($fn)); |
---|
| 1265 | } |
---|
| 1266 | |
---|
[b67ab6b] | 1267 | package BarnOwl::Style::Legacy; |
---|
| 1268 | |
---|
| 1269 | sub new { |
---|
| 1270 | my $class = shift; |
---|
| 1271 | my $func = shift; |
---|
| 1272 | my $desc = shift; |
---|
| 1273 | my $useglobals = shift; |
---|
| 1274 | $useglobals = 0 unless defined($useglobals); |
---|
| 1275 | return bless {function => $func, |
---|
| 1276 | description => $desc, |
---|
| 1277 | useglobals => $useglobals}, $class; |
---|
| 1278 | } |
---|
| 1279 | |
---|
[2650a10] | 1280 | sub description { |
---|
| 1281 | my $self = shift; |
---|
| 1282 | return $self->{description} || |
---|
| 1283 | ("User-defined perl style that calls " . $self->{function}); |
---|
| 1284 | }; |
---|
[b67ab6b] | 1285 | |
---|
| 1286 | sub format_message { |
---|
| 1287 | my $self = shift; |
---|
| 1288 | if($self->{useglobals}) { |
---|
| 1289 | $_[0]->legacy_populate_global(); |
---|
| 1290 | } |
---|
[b441079] | 1291 | { |
---|
| 1292 | package main; |
---|
| 1293 | no strict 'refs'; |
---|
| 1294 | goto \&{$self->{function}}; |
---|
| 1295 | } |
---|
[b67ab6b] | 1296 | } |
---|
| 1297 | |
---|
[1631825] | 1298 | package BarnOwl::Timer; |
---|
| 1299 | |
---|
| 1300 | sub new { |
---|
| 1301 | my $class = shift; |
---|
| 1302 | my $args = shift; |
---|
| 1303 | |
---|
| 1304 | my $cb = $args->{cb}; |
---|
| 1305 | die("Invalid callback pased to BarnOwl::Timer\n") unless ref($cb) eq 'CODE'; |
---|
| 1306 | |
---|
| 1307 | my $self = {cb => $cb}; |
---|
| 1308 | |
---|
| 1309 | bless($self, $class); |
---|
| 1310 | |
---|
| 1311 | $self->{timer} = BarnOwl::Internal::add_timer($args->{after} || 0, |
---|
| 1312 | $args->{interval} || 0, |
---|
| 1313 | $self); |
---|
| 1314 | return $self; |
---|
| 1315 | } |
---|
| 1316 | |
---|
| 1317 | sub do_callback { |
---|
| 1318 | my $self = shift; |
---|
| 1319 | $self->{cb}->($self); |
---|
| 1320 | } |
---|
| 1321 | |
---|
| 1322 | sub DESTROY { |
---|
| 1323 | my $self = shift; |
---|
| 1324 | if(defined($self->{timer})) { |
---|
| 1325 | BarnOwl::Internal::remove_timer($self->{timer}); |
---|
| 1326 | } |
---|
| 1327 | } |
---|
| 1328 | |
---|
[b6c067a] | 1329 | |
---|
[f1e629d] | 1330 | # switch to package main when we're done |
---|
| 1331 | package main; |
---|
[0337203] | 1332 | |
---|
| 1333 | # Shove a bunch of fake entries into @INC so modules can use or |
---|
| 1334 | # require them without choking |
---|
| 1335 | $::INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm |
---|
| 1336 | BarnOwl/Message.pm BarnOwl/Style.pm)); |
---|
[f1e629d] | 1337 | |
---|
| 1338 | 1; |
---|
[0337203] | 1339 | |
---|