| 1 | # $Id$ |
|---|
| 2 | # |
|---|
| 3 | # This is all linked into the binary and evaluated when perl starts up... |
|---|
| 4 | # |
|---|
| 5 | ##################################################################### |
|---|
| 6 | ##################################################################### |
|---|
| 7 | # XXX NOTE: This file is sourced before almost any barnowl |
|---|
| 8 | # architecture is loaded. This means, for example, that it cannot |
|---|
| 9 | # execute any owl commands. Any code that needs to do so should live |
|---|
| 10 | # in BarnOwl::Hooks::_startup |
|---|
| 11 | |
|---|
| 12 | use strict; |
|---|
| 13 | use warnings; |
|---|
| 14 | |
|---|
| 15 | package BarnOwl; |
|---|
| 16 | |
|---|
| 17 | BEGIN { |
|---|
| 18 | # bootstrap in C bindings and glue |
|---|
| 19 | *owl:: = \*BarnOwl::; |
|---|
| 20 | bootstrap BarnOwl 1.2; |
|---|
| 21 | }; |
|---|
| 22 | |
|---|
| 23 | use lib(get_data_dir()."/lib"); |
|---|
| 24 | use lib($ENV{HOME}."/.owl/lib"); |
|---|
| 25 | |
|---|
| 26 | our $configfile; |
|---|
| 27 | |
|---|
| 28 | if(!$configfile && -f $ENV{HOME} . "/.barnowlconf") { |
|---|
| 29 | $configfile = $ENV{HOME} . "/.barnowlconf"; |
|---|
| 30 | } |
|---|
| 31 | $configfile ||= $ENV{HOME}."/.owlconf"; |
|---|
| 32 | |
|---|
| 33 | # populate global variable space for legacy owlconf files |
|---|
| 34 | sub _format_msg_legacy_wrap { |
|---|
| 35 | my ($m) = @_; |
|---|
| 36 | $m->legacy_populate_global(); |
|---|
| 37 | return &BarnOwl::format_msg($m); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | # populate global variable space for legacy owlconf files |
|---|
| 41 | sub _receive_msg_legacy_wrap { |
|---|
| 42 | my ($m) = @_; |
|---|
| 43 | $m->legacy_populate_global(); |
|---|
| 44 | return &BarnOwl::Hooks::_receive_msg($m); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | # make BarnOwl::<command>("foo") be aliases to BarnOwl::command("<command> foo"); |
|---|
| 48 | sub AUTOLOAD { |
|---|
| 49 | our $AUTOLOAD; |
|---|
| 50 | my $called = $AUTOLOAD; |
|---|
| 51 | $called =~ s/.*:://; |
|---|
| 52 | $called =~ s/_/-/g; |
|---|
| 53 | return &BarnOwl::command("$called ".join(" ",@_)); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | =head2 new_command NAME FUNC [{ARGS}] |
|---|
| 57 | |
|---|
| 58 | Add a new owl command. When owl executes the command NAME, FUNC will |
|---|
| 59 | be called with the arguments passed to the command, with NAME as the |
|---|
| 60 | first argument. |
|---|
| 61 | |
|---|
| 62 | ARGS should be a hashref containing any or all of C<summary>, |
|---|
| 63 | C<usage>, or C<description> keys. |
|---|
| 64 | |
|---|
| 65 | =cut |
|---|
| 66 | |
|---|
| 67 | sub new_command { |
|---|
| 68 | my $name = shift; |
|---|
| 69 | my $func = shift; |
|---|
| 70 | my $args = shift || {}; |
|---|
| 71 | my %args = ( |
|---|
| 72 | summary => "", |
|---|
| 73 | usage => "", |
|---|
| 74 | description => "", |
|---|
| 75 | %{$args} |
|---|
| 76 | ); |
|---|
| 77 | |
|---|
| 78 | BarnOwl::new_command_internal($name, $func, $args{summary}, $args{usage}, $args{description}); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | =head2 new_variable_int NAME [{ARGS}] |
|---|
| 82 | |
|---|
| 83 | =head2 new_variable_bool NAME [{ARGS}] |
|---|
| 84 | |
|---|
| 85 | =head2 new_variable_string NAME [{ARGS}] |
|---|
| 86 | |
|---|
| 87 | Add a new owl variable, either an int, a bool, or a string, with the |
|---|
| 88 | specified name. |
|---|
| 89 | |
|---|
| 90 | ARGS can optionally contain the following keys: |
|---|
| 91 | |
|---|
| 92 | =over 4 |
|---|
| 93 | |
|---|
| 94 | =item default |
|---|
| 95 | |
|---|
| 96 | The default and initial value for the variable |
|---|
| 97 | |
|---|
| 98 | =item summary |
|---|
| 99 | |
|---|
| 100 | A one-line summary of the variable's purpose |
|---|
| 101 | |
|---|
| 102 | =item description |
|---|
| 103 | |
|---|
| 104 | A longer description of the function of the variable |
|---|
| 105 | |
|---|
| 106 | =back |
|---|
| 107 | |
|---|
| 108 | =cut |
|---|
| 109 | |
|---|
| 110 | sub new_variable_int { |
|---|
| 111 | unshift @_, \&BarnOwl::new_variable_int_internal, 0; |
|---|
| 112 | goto \&_new_variable; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | sub new_variable_bool { |
|---|
| 116 | unshift @_, \&BarnOwl::new_variable_bool_internal, 0; |
|---|
| 117 | goto \&_new_variable; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | sub new_variable_string { |
|---|
| 121 | unshift @_, \&BarnOwl::new_variable_string_internal, ""; |
|---|
| 122 | goto \&_new_variable; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | sub _new_variable { |
|---|
| 126 | my $func = shift; |
|---|
| 127 | my $default_default = shift; |
|---|
| 128 | my $name = shift; |
|---|
| 129 | my $args = shift || {}; |
|---|
| 130 | my %args = ( |
|---|
| 131 | summary => "", |
|---|
| 132 | description => "", |
|---|
| 133 | default => $default_default, |
|---|
| 134 | %{$args}); |
|---|
| 135 | $func->($name, $args{default}, $args{summary}, $args{description}); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | ##################################################################### |
|---|
| 139 | ##################################################################### |
|---|
| 140 | |
|---|
| 141 | package BarnOwl::Message; |
|---|
| 142 | |
|---|
| 143 | sub new { |
|---|
| 144 | my $class = shift; |
|---|
| 145 | my %args = (@_); |
|---|
| 146 | if($class eq __PACKAGE__ && $args{type}) { |
|---|
| 147 | $class = "BarnOwl::Message::" . ucfirst $args{type}; |
|---|
| 148 | } |
|---|
| 149 | return bless {%args}, $class; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | sub type { return shift->{"type"}; } |
|---|
| 153 | sub direction { return shift->{"direction"}; } |
|---|
| 154 | sub time { return shift->{"time"}; } |
|---|
| 155 | sub id { return shift->{"id"}; } |
|---|
| 156 | sub body { return shift->{"body"}; } |
|---|
| 157 | sub sender { return shift->{"sender"}; } |
|---|
| 158 | sub recipient { return shift->{"recipient"}; } |
|---|
| 159 | sub login { return shift->{"login"}; } |
|---|
| 160 | sub is_private { return shift->{"private"}; } |
|---|
| 161 | |
|---|
| 162 | sub is_login { return shift->login eq "login"; } |
|---|
| 163 | sub is_logout { return shift->login eq "logout"; } |
|---|
| 164 | sub is_loginout { my $m=shift; return ($m->is_login or $m->is_logout); } |
|---|
| 165 | sub is_incoming { return (shift->{"direction"} eq "in"); } |
|---|
| 166 | sub is_outgoing { return (shift->{"direction"} eq "out"); } |
|---|
| 167 | |
|---|
| 168 | sub is_deleted { return shift->{"deleted"}; } |
|---|
| 169 | |
|---|
| 170 | sub is_admin { return (shift->{"type"} eq "admin"); } |
|---|
| 171 | sub is_generic { return (shift->{"type"} eq "generic"); } |
|---|
| 172 | sub is_zephyr { return (shift->{"type"} eq "zephyr"); } |
|---|
| 173 | sub is_aim { return (shift->{"type"} eq "AIM"); } |
|---|
| 174 | sub is_jabber { return (shift->{"type"} eq "jabber"); } |
|---|
| 175 | sub is_icq { return (shift->{"type"} eq "icq"); } |
|---|
| 176 | sub is_yahoo { return (shift->{"type"} eq "yahoo"); } |
|---|
| 177 | sub is_msn { return (shift->{"type"} eq "msn"); } |
|---|
| 178 | sub is_loopback { return (shift->{"type"} eq "loopback"); } |
|---|
| 179 | |
|---|
| 180 | # These are overridden by appropriate message types |
|---|
| 181 | sub is_ping { return 0; } |
|---|
| 182 | sub is_mail { return 0; } |
|---|
| 183 | sub is_personal { return shift->is_private; } |
|---|
| 184 | sub class { return undef; } |
|---|
| 185 | sub instance { return undef; } |
|---|
| 186 | sub realm { return undef; } |
|---|
| 187 | sub opcode { return undef; } |
|---|
| 188 | sub header { return undef; } |
|---|
| 189 | sub host { return undef; } |
|---|
| 190 | sub hostname { return undef; } |
|---|
| 191 | sub auth { return undef; } |
|---|
| 192 | sub fields { return undef; } |
|---|
| 193 | sub zsig { return undef; } |
|---|
| 194 | sub zwriteline { return undef; } |
|---|
| 195 | sub login_host { return undef; } |
|---|
| 196 | sub login_tty { return undef; } |
|---|
| 197 | |
|---|
| 198 | sub pretty_sender { return shift->sender; } |
|---|
| 199 | sub pretty_recipient { return shift->recipient; } |
|---|
| 200 | |
|---|
| 201 | sub delete { |
|---|
| 202 | my ($m) = @_; |
|---|
| 203 | &BarnOwl::command("delete --id ".$m->id); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | sub undelete { |
|---|
| 207 | my ($m) = @_; |
|---|
| 208 | &BarnOwl::command("undelete --id ".$m->id); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | # Serializes the message into something similar to the zwgc->vt format |
|---|
| 212 | sub serialize { |
|---|
| 213 | my ($this) = @_; |
|---|
| 214 | my $s; |
|---|
| 215 | for my $f (keys %$this) { |
|---|
| 216 | my $val = $this->{$f}; |
|---|
| 217 | if (ref($val) eq "ARRAY") { |
|---|
| 218 | for my $i (0..@$val-1) { |
|---|
| 219 | my $aval; |
|---|
| 220 | $aval = $val->[$i]; |
|---|
| 221 | $aval =~ s/\n/\n$f.$i: /g; |
|---|
| 222 | $s .= "$f.$i: $aval\n"; |
|---|
| 223 | } |
|---|
| 224 | } else { |
|---|
| 225 | $val =~ s/\n/\n$f: /g; |
|---|
| 226 | $s .= "$f: $val\n"; |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | return $s; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | # Populate the annoying legacy global variables |
|---|
| 233 | sub legacy_populate_global { |
|---|
| 234 | my ($m) = @_; |
|---|
| 235 | $BarnOwl::direction = $m->direction ; |
|---|
| 236 | $BarnOwl::type = $m->type ; |
|---|
| 237 | $BarnOwl::id = $m->id ; |
|---|
| 238 | $BarnOwl::class = $m->class ; |
|---|
| 239 | $BarnOwl::instance = $m->instance ; |
|---|
| 240 | $BarnOwl::recipient = $m->recipient ; |
|---|
| 241 | $BarnOwl::sender = $m->sender ; |
|---|
| 242 | $BarnOwl::realm = $m->realm ; |
|---|
| 243 | $BarnOwl::opcode = $m->opcode ; |
|---|
| 244 | $BarnOwl::zsig = $m->zsig ; |
|---|
| 245 | $BarnOwl::msg = $m->body ; |
|---|
| 246 | $BarnOwl::time = $m->time ; |
|---|
| 247 | $BarnOwl::host = $m->host ; |
|---|
| 248 | $BarnOwl::login = $m->login ; |
|---|
| 249 | $BarnOwl::auth = $m->auth ; |
|---|
| 250 | if ($m->fields) { |
|---|
| 251 | @BarnOwl::fields = @{$m->fields}; |
|---|
| 252 | @main::fields = @{$m->fields}; |
|---|
| 253 | } else { |
|---|
| 254 | @BarnOwl::fields = undef; |
|---|
| 255 | @main::fields = undef; |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | sub smartfilter { |
|---|
| 260 | die("smartfilter not supported for this message\n"); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | # Display fields -- overridden by subclasses when needed |
|---|
| 264 | sub login_type {""} |
|---|
| 265 | sub login_extra {""} |
|---|
| 266 | sub long_sender {""} |
|---|
| 267 | |
|---|
| 268 | # The context in which a non-personal message was sent, e.g. a chat or |
|---|
| 269 | # class |
|---|
| 270 | sub context {""} |
|---|
| 271 | |
|---|
| 272 | # Some indicator of context *within* $self->context. e.g. the zephyr |
|---|
| 273 | # instance |
|---|
| 274 | sub subcontext {""} |
|---|
| 275 | |
|---|
| 276 | ##################################################################### |
|---|
| 277 | ##################################################################### |
|---|
| 278 | |
|---|
| 279 | package BarnOwl::Message::Admin; |
|---|
| 280 | |
|---|
| 281 | use base qw( BarnOwl::Message ); |
|---|
| 282 | |
|---|
| 283 | sub header { return shift->{"header"}; } |
|---|
| 284 | |
|---|
| 285 | ##################################################################### |
|---|
| 286 | ##################################################################### |
|---|
| 287 | |
|---|
| 288 | package BarnOwl::Message::Generic; |
|---|
| 289 | |
|---|
| 290 | use base qw( BarnOwl::Message ); |
|---|
| 291 | |
|---|
| 292 | ##################################################################### |
|---|
| 293 | ##################################################################### |
|---|
| 294 | |
|---|
| 295 | package BarnOwl::Message::Loopback; |
|---|
| 296 | |
|---|
| 297 | use base qw( BarnOwl::Message ); |
|---|
| 298 | |
|---|
| 299 | # all loopback messages are private |
|---|
| 300 | sub is_private { |
|---|
| 301 | return 1; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | ##################################################################### |
|---|
| 305 | ##################################################################### |
|---|
| 306 | |
|---|
| 307 | package BarnOwl::Message::AIM; |
|---|
| 308 | |
|---|
| 309 | use base qw( BarnOwl::Message ); |
|---|
| 310 | |
|---|
| 311 | # all non-loginout AIM messages are private for now... |
|---|
| 312 | sub is_private { |
|---|
| 313 | return !(shift->is_loginout); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | ##################################################################### |
|---|
| 317 | ##################################################################### |
|---|
| 318 | |
|---|
| 319 | package BarnOwl::Message::Zephyr; |
|---|
| 320 | |
|---|
| 321 | use base qw( BarnOwl::Message ); |
|---|
| 322 | |
|---|
| 323 | sub login_type { |
|---|
| 324 | return (shift->zsig eq "") ? "(PSEUDO)" : ""; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | sub login_extra { |
|---|
| 328 | my $m = shift; |
|---|
| 329 | return undef if (!$m->is_loginout); |
|---|
| 330 | my $s = lc($m->host); |
|---|
| 331 | $s .= " " . $m->login_tty if defined $m->login_tty; |
|---|
| 332 | return $s; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | sub long_sender { |
|---|
| 336 | my $m = shift; |
|---|
| 337 | return $m->zsig; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | sub context { |
|---|
| 341 | return shift->class; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | sub subcontext { |
|---|
| 345 | return shift->instance; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | sub login_tty { |
|---|
| 349 | my ($m) = @_; |
|---|
| 350 | return undef if (!$m->is_loginout); |
|---|
| 351 | return $m->fields->[2]; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | sub login_host { |
|---|
| 355 | my ($m) = @_; |
|---|
| 356 | return undef if (!$m->is_loginout); |
|---|
| 357 | return $m->fields->[0]; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | sub zwriteline { return shift->{"zwriteline"}; } |
|---|
| 361 | |
|---|
| 362 | sub is_ping { return (lc(shift->opcode) eq "ping"); } |
|---|
| 363 | |
|---|
| 364 | sub is_personal { |
|---|
| 365 | my ($m) = @_; |
|---|
| 366 | return ((lc($m->class) eq "message") |
|---|
| 367 | && (lc($m->instance) eq "personal") |
|---|
| 368 | && $m->is_private); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | sub is_mail { |
|---|
| 372 | my ($m) = @_; |
|---|
| 373 | return ((lc($m->class) eq "mail") && $m->is_private); |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | sub pretty_sender { |
|---|
| 377 | my ($m) = @_; |
|---|
| 378 | my $sender = $m->sender; |
|---|
| 379 | my $realm = BarnOwl::zephyr_getrealm(); |
|---|
| 380 | $sender =~ s/\@$realm$//; |
|---|
| 381 | return $sender; |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | sub pretty_recipient { |
|---|
| 385 | my ($m) = @_; |
|---|
| 386 | my $recip = $m->recipient; |
|---|
| 387 | my $realm = BarnOwl::zephyr_getrealm(); |
|---|
| 388 | $recip =~ s/\@$realm$//; |
|---|
| 389 | return $recip; |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | # These are arguably zephyr-specific |
|---|
| 393 | sub class { return shift->{"class"}; } |
|---|
| 394 | sub instance { return shift->{"instance"}; } |
|---|
| 395 | sub realm { return shift->{"realm"}; } |
|---|
| 396 | sub opcode { return shift->{"opcode"}; } |
|---|
| 397 | sub host { return shift->{"hostname"}; } |
|---|
| 398 | sub hostname { return shift->{"hostname"}; } |
|---|
| 399 | sub header { return shift->{"header"}; } |
|---|
| 400 | sub auth { return shift->{"auth"}; } |
|---|
| 401 | sub fields { return shift->{"fields"}; } |
|---|
| 402 | sub zsig { return shift->{"zsig"}; } |
|---|
| 403 | |
|---|
| 404 | ##################################################################### |
|---|
| 405 | ##################################################################### |
|---|
| 406 | ################################################################################ |
|---|
| 407 | |
|---|
| 408 | package BarnOwl::Hook; |
|---|
| 409 | |
|---|
| 410 | sub new { |
|---|
| 411 | my $class = shift; |
|---|
| 412 | return bless [], $class; |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | sub run { |
|---|
| 416 | my $self = shift; |
|---|
| 417 | my @args = @_; |
|---|
| 418 | return map {$_->(@args)} @$self; |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | sub add { |
|---|
| 422 | my $self = shift; |
|---|
| 423 | my $func = shift; |
|---|
| 424 | die("Not a coderef!") unless ref($func) eq 'CODE'; |
|---|
| 425 | push @$self, $func; |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | sub clear { |
|---|
| 429 | my $self = shift; |
|---|
| 430 | @$self = (); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | package BarnOwl::Hooks; |
|---|
| 434 | |
|---|
| 435 | use Exporter; |
|---|
| 436 | |
|---|
| 437 | our @EXPORT_OK = qw($startup $shutdown |
|---|
| 438 | $receiveMessage $mainLoop |
|---|
| 439 | $getBuddyList); |
|---|
| 440 | |
|---|
| 441 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
|---|
| 442 | |
|---|
| 443 | our $startup = BarnOwl::Hook->new; |
|---|
| 444 | our $shutdown = BarnOwl::Hook->new; |
|---|
| 445 | our $receiveMessage = BarnOwl::Hook->new; |
|---|
| 446 | our $mainLoop = BarnOwl::Hook->new; |
|---|
| 447 | our $getBuddyList = BarnOwl::Hook->new; |
|---|
| 448 | |
|---|
| 449 | # Internal startup/shutdown routines called by the C code |
|---|
| 450 | |
|---|
| 451 | sub _load_owlconf { |
|---|
| 452 | # load the config file |
|---|
| 453 | if ( -r $BarnOwl::configfile ) { |
|---|
| 454 | undef $@; |
|---|
| 455 | package main; |
|---|
| 456 | do $BarnOwl::configfile; |
|---|
| 457 | die $@ if $@; |
|---|
| 458 | package BarnOwl; |
|---|
| 459 | if(*BarnOwl::format_msg{CODE}) { |
|---|
| 460 | # if the config defines a legacy formatting function, add 'perl' as a style |
|---|
| 461 | BarnOwl::_create_style("perl", "BarnOwl::_format_msg_legacy_wrap", |
|---|
| 462 | "User-defined perl style that calls BarnOwl::format_msg" |
|---|
| 463 | . " with legacy global variable support"); |
|---|
| 464 | BarnOwl::set("-q default_style perl"); |
|---|
| 465 | } |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | sub _startup { |
|---|
| 470 | _load_owlconf(); |
|---|
| 471 | |
|---|
| 472 | if(eval {require BarnOwl::ModuleLoader}) { |
|---|
| 473 | eval { |
|---|
| 474 | BarnOwl::ModuleLoader->load_all; |
|---|
| 475 | }; |
|---|
| 476 | BarnOwl::error("$@") if $@; |
|---|
| 477 | |
|---|
| 478 | } else { |
|---|
| 479 | BarnOwl::error("Can't load BarnOwl::ModuleLoader, loadable module support disabled:\n$@"); |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | $startup->run(0); |
|---|
| 483 | BarnOwl::startup() if *BarnOwl::startup{CODE}; |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | sub _shutdown { |
|---|
| 487 | $shutdown->run; |
|---|
| 488 | |
|---|
| 489 | BarnOwl::shutdown() if *BarnOwl::shutdown{CODE}; |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | sub _receive_msg { |
|---|
| 493 | my $m = shift; |
|---|
| 494 | |
|---|
| 495 | $receiveMessage->run($m); |
|---|
| 496 | |
|---|
| 497 | BarnOwl::receive_msg($m) if *BarnOwl::receive_msg{CODE}; |
|---|
| 498 | } |
|---|
| 499 | |
|---|
| 500 | sub _mainloop_hook { |
|---|
| 501 | $mainLoop->run; |
|---|
| 502 | BarnOwl::mainloop_hook() if *BarnOwl::mainloop_hook{CODE}; |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | sub _get_blist { |
|---|
| 506 | return join("\n", $getBuddyList->run); |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | ################################################################################ |
|---|
| 510 | # Built-in perl styles |
|---|
| 511 | ################################################################################ |
|---|
| 512 | package BarnOwl::Style::Default; |
|---|
| 513 | ################################################################################ |
|---|
| 514 | # Branching point for various formatting functions in this style. |
|---|
| 515 | ################################################################################ |
|---|
| 516 | sub format_message($) |
|---|
| 517 | { |
|---|
| 518 | my $m = shift; |
|---|
| 519 | |
|---|
| 520 | if ( $m->is_loginout) { |
|---|
| 521 | return format_login($m); |
|---|
| 522 | } elsif($m->is_ping) { |
|---|
| 523 | return ( "\@b(PING) from \@b(" . $m->pretty_sender . ")\n" ); |
|---|
| 524 | } elsif($m->is_admin) { |
|---|
| 525 | return "\@bold(OWL ADMIN)\n" . indentBody($m); |
|---|
| 526 | } else { |
|---|
| 527 | return format_chat($m); |
|---|
| 528 | } |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | BarnOwl::_create_style("default", "BarnOwl::Style::Default::format_message", "Default style"); |
|---|
| 532 | |
|---|
| 533 | ################################################################################ |
|---|
| 534 | |
|---|
| 535 | sub time_hhmm { |
|---|
| 536 | my $m = shift; |
|---|
| 537 | my ($time) = $m->time =~ /(\d\d:\d\d)/; |
|---|
| 538 | return $time; |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | sub format_login($) { |
|---|
| 542 | my $m = shift; |
|---|
| 543 | return sprintf( |
|---|
| 544 | '@b<%s%s> for @b(%s) (%s) %s', |
|---|
| 545 | uc( $m->login ), |
|---|
| 546 | $m->login_type, |
|---|
| 547 | $m->pretty_sender, |
|---|
| 548 | $m->login_extra, |
|---|
| 549 | time_hhmm($m) |
|---|
| 550 | ); |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | sub format_chat($) { |
|---|
| 554 | my $m = shift; |
|---|
| 555 | my $header; |
|---|
| 556 | if ( $m->is_personal ) { |
|---|
| 557 | if ( $m->direction eq "out" ) { |
|---|
| 558 | $header = ucfirst $m->type . " sent to " . $m->pretty_recipient; |
|---|
| 559 | } else { |
|---|
| 560 | $header = ucfirst $m->type . " from " . $m->pretty_sender; |
|---|
| 561 | } |
|---|
| 562 | } else { |
|---|
| 563 | $header = $m->context; |
|---|
| 564 | if(defined $m->subcontext) { |
|---|
| 565 | $header .= ' / ' . $m->subcontext; |
|---|
| 566 | } |
|---|
| 567 | $header .= ' / @b{' . $m->pretty_sender . '}'; |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | $header .= " " . time_hhmm($m); |
|---|
| 571 | my $sender = $m->long_sender; |
|---|
| 572 | $sender =~ s/\n.*$//s; |
|---|
| 573 | $header .= " " x (4 - ((length $header) % 4)); |
|---|
| 574 | $header .= "(" . $sender . '@color[default]' . ")"; |
|---|
| 575 | my $message = $header . "\n". indentBody($m); |
|---|
| 576 | if($m->is_personal && $m->direction eq "in") { |
|---|
| 577 | $message = BarnOwl::Style::boldify($message); |
|---|
| 578 | } |
|---|
| 579 | return $message; |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | sub indentBody($) |
|---|
| 583 | { |
|---|
| 584 | my $m = shift; |
|---|
| 585 | |
|---|
| 586 | my $body = $m->body; |
|---|
| 587 | if ($m->{should_wordwrap}) { |
|---|
| 588 | $body = BarnOwl::wordwrap($body, BarnOwl::getnumcols()-8); |
|---|
| 589 | } |
|---|
| 590 | # replace newline followed by anything with |
|---|
| 591 | # newline plus four spaces and that thing. |
|---|
| 592 | $body =~ s/\n(.)/\n $1/g; |
|---|
| 593 | |
|---|
| 594 | return " ".$body; |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | package BarnOwl::Style; |
|---|
| 599 | |
|---|
| 600 | # This takes a zephyr to be displayed and modifies it to be displayed |
|---|
| 601 | # entirely in bold. |
|---|
| 602 | sub boldify($) |
|---|
| 603 | { |
|---|
| 604 | local $_ = shift; |
|---|
| 605 | if ( !(/\)/) ) { |
|---|
| 606 | return '@b(' . $_ . ')'; |
|---|
| 607 | } elsif ( !(/\>/) ) { |
|---|
| 608 | return '@b<' . $_ . '>'; |
|---|
| 609 | } elsif ( !(/\}/) ) { |
|---|
| 610 | return '@b{' . $_ . '}'; |
|---|
| 611 | } elsif ( !(/\]/) ) { |
|---|
| 612 | return '@b[' . $_ . ']'; |
|---|
| 613 | } else { |
|---|
| 614 | my $txt = "\@b($_"; |
|---|
| 615 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
|---|
| 616 | return $txt . ')'; |
|---|
| 617 | } |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | # switch to package main when we're done |
|---|
| 622 | package main; |
|---|
| 623 | |
|---|
| 624 | # Shove a bunch of fake entries into @INC so modules can use or |
|---|
| 625 | # require them without choking |
|---|
| 626 | $::INC{$_} = 1 for (qw(BarnOwl.pm BarnOwl/Hooks.pm |
|---|
| 627 | BarnOwl/Message.pm BarnOwl/Style.pm)); |
|---|
| 628 | |
|---|
| 629 | 1; |
|---|
| 630 | |
|---|