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