[ee183be] | 1 | use strict; |
---|
| 2 | use warnings; |
---|
| 3 | |
---|
| 4 | package BarnOwl::Style::Default; |
---|
[5550eb0] | 5 | use POSIX qw(strftime); |
---|
| 6 | |
---|
[ee183be] | 7 | ################################################################################ |
---|
| 8 | # Branching point for various formatting functions in this style. |
---|
| 9 | ################################################################################ |
---|
| 10 | sub format_message |
---|
| 11 | { |
---|
| 12 | my $self = shift; |
---|
| 13 | my $m = shift; |
---|
| 14 | my $fmt; |
---|
| 15 | |
---|
| 16 | if ( $m->is_loginout) { |
---|
| 17 | $fmt = $self->format_login($m); |
---|
| 18 | } elsif($m->is_ping && $m->is_personal) { |
---|
| 19 | $fmt = $self->format_ping($m); |
---|
| 20 | } elsif($m->is_admin) { |
---|
| 21 | $fmt = $self->format_admin($m); |
---|
| 22 | } else { |
---|
| 23 | $fmt = $self->format_chat($m); |
---|
| 24 | } |
---|
| 25 | $fmt = BarnOwl::Style::boldify($fmt) if $self->should_bold($m); |
---|
| 26 | return $fmt; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | sub should_bold { |
---|
| 30 | my $self = shift; |
---|
| 31 | my $m = shift; |
---|
| 32 | return $m->is_personal && $m->direction eq "in"; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | sub description {"Default style";} |
---|
| 36 | |
---|
| 37 | BarnOwl::create_style("default", "BarnOwl::Style::Default"); |
---|
| 38 | |
---|
| 39 | ################################################################################ |
---|
| 40 | |
---|
| 41 | sub format_time { |
---|
| 42 | my $self = shift; |
---|
| 43 | my $m = shift; |
---|
[0b2afba] | 44 | my $dateformat = BarnOwl::time_format('get_time_format'); |
---|
| 45 | return strftime($dateformat, localtime($m->unix_time)); |
---|
[ee183be] | 46 | } |
---|
| 47 | |
---|
| 48 | sub format_login { |
---|
| 49 | my $self = shift; |
---|
| 50 | my $m = shift; |
---|
| 51 | return sprintf( |
---|
| 52 | '@b<%s%s> for @b(%s) (%s) %s', |
---|
| 53 | uc( $m->login ), |
---|
| 54 | $m->login_type, |
---|
| 55 | $m->pretty_sender, |
---|
| 56 | $m->login_extra, |
---|
| 57 | $self->format_time($m) |
---|
| 58 | ); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | sub format_ping { |
---|
| 62 | my $self = shift; |
---|
| 63 | my $m = shift; |
---|
| 64 | my $personal_context = $m->personal_context; |
---|
| 65 | $personal_context = ' [' . $personal_context . ']' if $personal_context; |
---|
| 66 | return "\@b(PING)" . $personal_context . " from \@b(" . $m->pretty_sender . ")"; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | sub format_admin { |
---|
| 70 | my $self = shift; |
---|
| 71 | my $m = shift; |
---|
| 72 | return "\@bold(OWL ADMIN)\n" . $self->indent_body($m); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | sub format_chat { |
---|
| 76 | my $self = shift; |
---|
| 77 | my $m = shift; |
---|
| 78 | my $header = $self->chat_header($m); |
---|
| 79 | return $header . "\n". $self->indent_body($m); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | sub chat_header { |
---|
| 83 | my $self = shift; |
---|
| 84 | my $m = shift; |
---|
| 85 | my $header; |
---|
| 86 | if ( $m->is_personal ) { |
---|
| 87 | my $personal_context = $m->personal_context; |
---|
| 88 | $personal_context = ' [' . $personal_context . ']' if $personal_context; |
---|
| 89 | |
---|
| 90 | if ( $m->direction eq "out" ) { |
---|
| 91 | $header = ucfirst $m->type . $personal_context . " sent to " . $m->pretty_recipient; |
---|
| 92 | } else { |
---|
| 93 | $header = ucfirst $m->type . $personal_context . " from " . $m->pretty_sender; |
---|
| 94 | } |
---|
| 95 | } else { |
---|
| 96 | $header = $m->context; |
---|
| 97 | if(defined $m->subcontext) { |
---|
| 98 | $header .= ' / ' . $m->subcontext; |
---|
| 99 | } |
---|
| 100 | $header .= ' / @b{' . $m->pretty_sender . '}'; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | if($m->opcode) { |
---|
| 104 | $header .= " [" . $m->opcode . "]"; |
---|
| 105 | } |
---|
| 106 | $header .= " " . $self->format_time($m); |
---|
| 107 | $header .= $self->format_sender($m); |
---|
| 108 | return $header; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | sub format_sender { |
---|
| 112 | my $self = shift; |
---|
| 113 | my $m = shift; |
---|
| 114 | my $sender = $m->long_sender; |
---|
| 115 | $sender =~ s/\n.*$//s; |
---|
| 116 | if (BarnOwl::getvar('colorztext') eq 'on') { |
---|
| 117 | return " (" . $sender . '@color[default]' . ")"; |
---|
| 118 | } else { |
---|
| 119 | return " ($sender)"; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | sub indent_body |
---|
| 124 | { |
---|
| 125 | my $self = shift; |
---|
| 126 | my $m = shift; |
---|
| 127 | |
---|
| 128 | my $body = $m->body; |
---|
| 129 | if ($m->{should_wordwrap}) { |
---|
| 130 | $body = BarnOwl::wordwrap($body, BarnOwl::getnumcols()-9); |
---|
| 131 | } |
---|
| 132 | # replace newline followed by anything with |
---|
| 133 | # newline plus four spaces and that thing. |
---|
| 134 | $body =~ s/\n(.)/\n $1/g; |
---|
| 135 | # Trim trailing newlines. |
---|
| 136 | $body =~ s/\n*$//; |
---|
| 137 | return " ".$body; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | 1; |
---|