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