Debian Codegraph @

perl/lib/BarnOwl/Style/Default.pm

raw
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 $fmt = $self->humanize($fmt);
27 return $fmt;
28 }
29
30 sub should_bold {
31 my $self = shift;
32 my $m = shift;
33 return $m->is_personal && $m->direction eq "in";
34 }
35
36 sub maybe {
37 my $x = shift;
38 return defined($x) ? $x : "";
39 }
40
41 sub description {"Default style";}
42
43 BarnOwl::create_style("default", "BarnOwl::Style::Default");
44
45 ################################################################################
46
47 sub format_time {
48 my $self = shift;
49 my $m = shift;
50 my $dateformat = BarnOwl::time_format('get_time_format');
51 return strftime($dateformat, localtime($m->unix_time));
52 }
53
54 sub format_login {
55 my $self = shift;
56 my $m = shift;
57 return sprintf(
58 '@b<%s%s> for @b(%s) (%s) %s',
59 uc( $m->login ),
60 $m->login_type,
61 $m->pretty_sender,
62 $m->login_extra,
63 $self->format_time($m)
64 );
65 }
66
67 sub format_ping {
68 my $self = shift;
69 my $m = shift;
70 my $personal_context = $m->personal_context;
71 $personal_context = ' [' . $personal_context . ']' if $personal_context;
72 return "\@b(PING)" . $personal_context . " from \@b(" . $m->pretty_sender . ")";
73 }
74
75 sub format_admin {
76 my $self = shift;
77 my $m = shift;
78 return "\@bold(OWL ADMIN)\n" . $self->indent_body($m);
79 }
80
81 sub format_chat {
82 my $self = shift;
83 my $m = shift;
84 my $header = $self->chat_header($m);
85 return $header . "\n". $self->indent_body($m);
86 }
87
88 sub chat_header {
89 my $self = shift;
90 my $m = shift;
91 my $header;
92 if ( $m->is_personal ) {
93 my $personal_context = $m->personal_context;
94 $personal_context = ' [' . $self->humanize($personal_context, 1) . ']' if $personal_context;
95
96 if ( $m->direction eq "out" ) {
97 $header = ucfirst $m->type . $personal_context . " sent to " . $m->pretty_recipient;
98 } else {
99 $header = ucfirst $m->type . $personal_context . " from ";
100 if(defined($m->auth) && ($m->auth ne "YES")) {
101 $header .= "UNAUTH: ";
102 }
103 $header .= maybe($m->pretty_sender);
104 }
105 } else {
106 $header = $self->humanize($m->context, 1);
107 if(defined $m->subcontext) {
108 $header .= ' / ' . $self->humanize($m->subcontext, 1);
109 }
110 $header .= ' / ';
111 if(defined($m->auth) && ($m->auth ne "YES")) {
112 $header .= "UNAUTH: ";
113 }
114 $header .= '@b{' . maybe($m->pretty_sender) . '}';
115 if (defined($m->realm) && $m->realm ne BarnOwl::zephyr_getrealm()) {
116 $header .= ' {' . $self->humanize($m->realm, 1) . '}';
117 }
118 }
119
120 if($m->opcode) {
121 $header .= " [" . $self->humanize($m->opcode, 1) . "]";
122 }
123 $header .= " " . $self->format_time($m);
124 $header .= $self->format_sender($m);
125 return $header;
126 }
127
128 sub format_sender {
129 my $self = shift;
130 my $m = shift;
131 my $sender = $self->humanize($m->long_sender, 1);
132 if (BarnOwl::getvar('colorztext') eq 'on') {
133 return " (" . $sender . '@color[default]' . ")";
134 } else {
135 return " ($sender)";
136 }
137 }
138
139 sub indent_body
140 {
141 my $self = shift;
142 my $m = shift;
143
144 my $body = $m->body;
145 if ($m->{should_wordwrap}) {
146 $body = BarnOwl::wordwrap($body, BarnOwl::getnumcols()-9);
147 }
148 # replace newline followed by anything with
149 # newline plus four spaces and that thing.
150 $body =~ s/\n(.)/\n $1/g;
151 # Trim trailing newlines.
152 $body =~ s/\n*$//;
153 return " ".$body;
154 }
155
156 =head3 humanize STRING [one_line]
157
158 Method that takes a STRING with control characters and makes it human
159 readable in such a way as to not do anything funky with the terminal.
160 If one_line is true, be more conservative about what we treat as
161 control character.
162
163 =cut
164
165 sub humanize
166 {
167 my $self = shift;
168 my $s = shift;
169 my $oneline = shift;
170 sub _humanize_char
171 {
172 my $c = ord(shift);
173
174 if ($c < ord(' ')) {
175 return ('^' . chr($c + ord('@')));
176 } elsif ($c == 255) {
177 return ('^?');
178 } else {
179 return (sprintf('\\x{%x}', $c));
180 }
181 }
182 my $colorize = (BarnOwl::getvar('colorztext') eq 'on')
183 ? '@color(cyan)' : '';
184
185 my $chars = $oneline ? qr/[[:cntrl:]]/ : qr/[^[:print:]\n]|[\r\cK\f]/;
186
187 $s =~ s/($chars)/
188 "\@b($colorize" . _humanize_char($1) . ')'/eg;
189
190 return $s;
191 }
192
193 =head3 humanize_short STRING
194
195 As above, but always be conservative, and replace with a '?' instead
196 of something more elaborate.
197
198 =cut
199
200 sub humanize_short
201 {
202 my $self = shift;
203 my $s = shift;
204
205 $s =~ s/[[:cntrl:]]/?/g;
206
207 return $s;
208 }
209
210 1;