1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl::Style::Default; |
---|
5 | use POSIX qw(strftime); |
---|
6 | |
---|
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; |
---|
44 | return strftime('%H:%M', localtime($m->unix_time)); |
---|
45 | } |
---|
46 | |
---|
47 | sub format_login { |
---|
48 | my $self = shift; |
---|
49 | my $m = shift; |
---|
50 | return sprintf( |
---|
51 | '@b<%s%s> for @b(%s) (%s) %s', |
---|
52 | uc( $m->login ), |
---|
53 | $m->login_type, |
---|
54 | $m->pretty_sender, |
---|
55 | $m->login_extra, |
---|
56 | $self->format_time($m) |
---|
57 | ); |
---|
58 | } |
---|
59 | |
---|
60 | sub format_ping { |
---|
61 | my $self = shift; |
---|
62 | my $m = shift; |
---|
63 | my $personal_context = $m->personal_context; |
---|
64 | $personal_context = ' [' . $personal_context . ']' if $personal_context; |
---|
65 | return "\@b(PING)" . $personal_context . " from \@b(" . $m->pretty_sender . ")"; |
---|
66 | } |
---|
67 | |
---|
68 | sub format_admin { |
---|
69 | my $self = shift; |
---|
70 | my $m = shift; |
---|
71 | return "\@bold(OWL ADMIN)\n" . $self->indent_body($m); |
---|
72 | } |
---|
73 | |
---|
74 | sub format_chat { |
---|
75 | my $self = shift; |
---|
76 | my $m = shift; |
---|
77 | my $header = $self->chat_header($m); |
---|
78 | return $header . "\n". $self->indent_body($m); |
---|
79 | } |
---|
80 | |
---|
81 | sub chat_header { |
---|
82 | my $self = shift; |
---|
83 | my $m = shift; |
---|
84 | my $header; |
---|
85 | if ( $m->is_personal ) { |
---|
86 | my $personal_context = $m->personal_context; |
---|
87 | $personal_context = ' [' . $personal_context . ']' if $personal_context; |
---|
88 | |
---|
89 | if ( $m->direction eq "out" ) { |
---|
90 | $header = ucfirst $m->type . $personal_context . " sent to " . $m->pretty_recipient; |
---|
91 | } else { |
---|
92 | $header = ucfirst $m->type . $personal_context . " from " . $m->pretty_sender; |
---|
93 | } |
---|
94 | } else { |
---|
95 | $header = $m->context; |
---|
96 | if(defined $m->subcontext) { |
---|
97 | $header .= ' / ' . $m->subcontext; |
---|
98 | } |
---|
99 | $header .= ' / @b{' . $m->pretty_sender . '}'; |
---|
100 | } |
---|
101 | |
---|
102 | if($m->opcode) { |
---|
103 | $header .= " [" . $m->opcode . "]"; |
---|
104 | } |
---|
105 | $header .= " " . $self->format_time($m); |
---|
106 | $header .= $self->format_sender($m); |
---|
107 | return $header; |
---|
108 | } |
---|
109 | |
---|
110 | sub format_sender { |
---|
111 | my $self = shift; |
---|
112 | my $m = shift; |
---|
113 | my $sender = $m->long_sender; |
---|
114 | $sender =~ s/\n.*$//s; |
---|
115 | if (BarnOwl::getvar('colorztext') eq 'on') { |
---|
116 | return " (" . $sender . '@color[default]' . ")"; |
---|
117 | } else { |
---|
118 | return " ($sender)"; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | sub indent_body |
---|
123 | { |
---|
124 | my $self = shift; |
---|
125 | my $m = shift; |
---|
126 | |
---|
127 | my $body = $m->body; |
---|
128 | if ($m->{should_wordwrap}) { |
---|
129 | $body = BarnOwl::wordwrap($body, BarnOwl::getnumcols()-9); |
---|
130 | } |
---|
131 | # replace newline followed by anything with |
---|
132 | # newline plus four spaces and that thing. |
---|
133 | $body =~ s/\n(.)/\n $1/g; |
---|
134 | # Trim trailing newlines. |
---|
135 | $body =~ s/\n*$//; |
---|
136 | return " ".$body; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | 1; |
---|