Debian Codegraph @

porting/irclog2html.pl

raw
1 #!/usr/bin/perl
2
3 # irclog2html.pl Version 2.1mg - 27th July, 2001
4 # Copyright (C) 2000, Jeffrey W. Waugh
5
6 # Changed by Marius Gedminas ([email protected]) on 2005-01-09:
7 # changed time regexps to support ISO-8601 (YYYY-MM-DDTHH:MM:SS) timestamps
8 # commented out print $time;
9 # fixed bugs in code that tried to preserve colours accross nick changes
10
11 # Author:
12 # Jeff Waugh <[email protected]>
13
14 # Contributors:
15 # Rick Welykochy <[email protected]>
16 # Alexander Else <[email protected]>
17
18 # Released under the terms of the GNU GPL v2 or later
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # Usage: irclog2html filename
22
23 # irclog2html will write out a colourised irc log, appending a .html
24 # extension to the output file.
25
26
27 ####################################################################################
28 # Perl Configuration
29
30 use strict;
31 #$^W = 1; #RW# turn on warnings
32
33 my $VERSION = "2.1mg";
34 my $RELEASE = "27th July, 2001";
35
36
37 # Colouring stuff
38 my $a = 0.95; # tune these for the starting and ending concentrations of R,G,B
39 my $b = 0.5;
40 my $rgb = [ [$a,$b,$b], [$b,$a,$b], [$b,$b,$a], [$a,$a,$b], [$a,$b,$a], [$b,$a,$a] ];
41
42 my $rgbmax = 125; # tune these two for the outmost ranges of colour depth
43 my $rgbmin = 240;
44
45
46 ####################################################################################
47 # Preferences
48
49 # Comment out the "table" assignment to use the plain version
50
51 my %prefs_colours = (
52 "part" => "#000099",
53 "join" => "#009900",
54 "server" => "#009900",
55 "nickchange" => "#009900",
56 "action" => "#CC00CC",
57 );
58
59 my %prefs_colour_nick = (
60 "jdub" => "#993333",
61 "cantanker" => "#006600",
62 "chuckd" => "#339999",
63 );
64
65 my %prefs_styles = (
66 "simplett" => "Text style with little use of colour",
67 "tt" => "Text style using colours for each nick",
68 "simpletable" => "Table style, without heavy use of colour",
69 "table" => "Default style, using a table with bold colours",
70 );
71
72 my $STYLE = "table";
73
74
75 ####################################################################################
76 # Utility Functions & Variables
77
78 sub output_nicktext {
79 my ($nick, $text, $htmlcolour) = @_;
80
81 if ($STYLE eq "table") {
82 print OUTPUT "<tr><th bgcolor=\"$htmlcolour\"><font color=\"#ffffff\"><tt>$nick</tt></font></th>";
83 print OUTPUT "<td width=\"100%\" bgcolor=\"#eeeeee\"><tt><font color=\"$htmlcolour\">$text<\/font></tt></td></tr>\n";
84 } elsif ($STYLE eq "simpletable") {
85 print OUTPUT "<tr bgcolor=\"#eeeeee\"><th><font color=\"$htmlcolour\"><tt>$nick</tt></font></th>";
86 print OUTPUT "<td width=\"100%\"><tt>$text</tt></td></tr>\n";
87 } elsif ($STYLE eq "simplett") {
88 print OUTPUT "&lt\;$nick&gt\; $text<br>\n";
89 } else {
90 print OUTPUT "<font color=\"$htmlcolour\">&lt\;$nick&gt\;<\/font> <font color=\"#000000\">$text<\/font><br>\n";
91 }
92 }
93
94 sub output_servermsg {
95 my ($line) = @_;
96
97 if ($STYLE =~ /table/) {
98 print OUTPUT "<tr><td colspan=2><tt>$line</tt></td></tr>\n";
99 } else {
100 print OUTPUT "$line<br>\n";
101 }
102 }
103
104 sub html_rgb
105 {
106 my ($i,$ncolours) = @_;
107 $ncolours = 1 if $ncolours == 0;
108
109 my $n = $i % @$rgb;
110 my $m = $rgbmin + ($rgbmax - $rgbmin) * ($ncolours - $i) / $ncolours;
111
112 my $r = $rgb->[$n][0] * $m;
113 my $g = $rgb->[$n][1] * $m;
114 my $b = $rgb->[$n][2] * $m;
115 sprintf("#%02x%02x%02x",$r,$g,$b);
116 }
117
118 my $msg_usage = "Usage: irclog2html.pl [OPTION]... [FILE]
119 Colourises and converts IRC logs to HTML format for easy web reading.
120
121 -s, --style=[STYLE] format log according to specific style. style formats
122 described using irclog2html [-s|--style]
123
124 --colour-<attribute>=[COLOUR] format output colour scheme. attributes
125 described using irclog2html [--colour]
126
127 Report bugs to Jeff Waugh <jdub\@perkypants.org>.
128 ";
129
130 my $msg_styles = "The following styles are available for use with irclog2html.pl:
131
132 simplett
133 Text style with little use of colour
134
135 tt
136 Text style using colours for each nick
137
138 simpletable
139 Table style, without heavy use of colour
140
141 table
142 Default style, using a table with bold colours
143 ";
144
145 my $msg_colours = "The following attributes may be customized using the --colour
146 parameter:
147
148 join, part, action, server, nickchange
149 ";
150
151
152 ################################################################################
153 # Main
154
155 sub main {
156
157 my $inputfile;
158
159 my $nick;
160 my $time;
161 my $line;
162 my $text;
163
164 my $htmlcolour;
165 my $nickcount = 0;
166 my $NICKMAX = 30;
167
168 my %colours = %prefs_colours;
169 my %colour_nick = %prefs_colour_nick;
170 my %styles = %prefs_styles;
171
172
173 # Quit if there is no filename specified on the command line #
174 if ($#ARGV == -1) {
175 die "Required parameter missing\n\n$msg_usage";
176 }
177
178
179 # Loop through parameters, bringing filenames into $files #
180 my $count = 0;
181
182 while ($ARGV[$count]) {
183
184 if ($ARGV[$count] =~ /-s|--style.*/) {
185 $STYLE = $ARGV[$count];
186
187 if ($STYLE =~ /--style=.*/) {
188 $STYLE =~ s/--style=(.*)/$1/;
189
190 } else {
191 $count++;
192 $STYLE = $ARGV[$count];
193 }
194
195 if ($STYLE eq "") {
196 print $msg_styles;
197 return 0;
198
199 } elsif (!defined($styles{$STYLE})) {
200 die "irclog2html.pl: invalid style: `$STYLE'\n\n$msg_styles";
201 }
202
203 } elsif ($ARGV[$count] =~ /--colou?r.*/) {
204 my $colour_pref = $ARGV[$count];
205 my $colour = $colour_pref;
206
207 if ($colour_pref =~ /--colou?r$/) {
208 print $msg_colours;
209 return 0;
210
211 } else {
212 $colour_pref =~ s/--colou?r-(.*)?=.*/$1/;
213 $colour =~ s/--colou?r-.*?=(.*)/$1/;
214
215 $colours{$colour_pref} = $colour;
216 }
217
218 } else {
219 $inputfile = $ARGV[$count];
220 }
221 $count++;
222 }
223
224 # Open input and output files #
225 if (!$inputfile) {
226 # no file to open, print appropriate usage information
227 die "\n$msg_usage";
228
229 } elsif (!open(INPUT, $inputfile)) {
230 # not a vaild file to open, spew error and usage information
231 die "irclog2html.pl: cannot open $inputfile for reading\n\n$msg_usage";
232 }
233 if (!open(OUTPUT, ">$inputfile.html")) {
234 # can't open file for output, spew error
235 die "irclog2html.pl: cannot open $inputfile.html for writing\n";
236 }
237
238
239 # Begin output #
240 print OUTPUT qq{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
241 <html>
242 <head>
243 <title>$inputfile</title>
244 <meta name="generator" content="irclog2html.pl $VERSION by Jeff Waugh">
245 <meta name="version" content="$VERSION - $RELEASE">
246 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
247 </head>
248 <body text="#000000" bgcolor="#ffffff"><tt>
249 };
250
251 if ($STYLE =~ /table/) {
252 print OUTPUT "<table cellspacing=3 cellpadding=2 border=0>\n";
253 }
254
255 while ($line = <INPUT>) {
256
257 chomp $line;
258
259 if ($line ne "") {
260
261 # Replace ampersands, pointies, control characters #
262 $line =~ s/&/&amp\;/g;
263 $line =~ s/</&lt\;/g;
264 $line =~ s/>/&gt\;/g;
265 $line =~ s/[\x00-\x1f]+//g;
266
267 # Replace possible URLs with links #
268 $line =~ s/((http|https|ftp|gopher|news):\/\/\S*)/<a href="$1">$1<\/a>/g;
269
270 # Rip out the time #
271 if ($line =~ /^\[?(?:\d\d\d\d-\d\d-\d\dT)?\d\d:\d\d(:\d\d)?\]? +.*$/) {
272 $time = $line;
273 $time =~ s/^\[?(?:\d\d\d\d-\d\d-\d\dT)?(\d\d:\d\d(:\d\d)?)\]? +.*$/$1/;
274 $line =~ s/^\[?(?:\d\d\d\d-\d\d-\d\dT)?\d\d:\d\d(:\d\d)?\]? +(.*)$/$2/;
275 ## print $time;
276 }
277
278 # Colourise the comments
279 if ($line =~ /^&lt\;.*?&gt\;\s.*/) {
280
281 # Split $nick and $line
282 $nick = $line;
283 $nick =~ s/^&lt\;(.*?)&gt\;\s.*$/$1/;
284
285 # $nick =~ tr/[A-Z]/[a-z]/;
286 # <======= move this into another function when getting nick colour
287
288 $text = $line;
289 $text =~ s/^&lt\;.*?&gt\;\s(.*)$/$1/;
290 $text =~ s/ /&nbsp\;&nbsp\;/g;
291
292 $htmlcolour = $colour_nick{$nick};
293 if (!defined($htmlcolour)) {
294 # new nick
295 $nickcount++;
296
297 # if we've exceeded our estimate of the number of nicks, double it
298 $NICKMAX *= 2 if $nickcount >= $NICKMAX;
299
300 $htmlcolour = $colour_nick{$nick} = html_rgb($nickcount, $NICKMAX);
301 }
302 output_nicktext($nick, $text, $htmlcolour);
303
304 } else {
305 # Colourise the /me's #
306 if ($line =~ /^\* .*$/) {
307 $line =~ s/^(\*.*)$/<font color=\"$colours{"action"}\">$1<\/font>/;
308 }
309
310 # Colourise joined/left messages #
311 elsif ($line =~ /^(\*\*\*|--&gt;) .*joined/) {
312 $line =~ s/(^(\*\*\*|--&gt;) .*)/<font color=\"$colours{"join"}\">$1<\/font>/;
313 }
314 elsif ($line =~ /^(\*\*\*|&lt;--) .*left|quit/) {
315 $line =~ s/(^(\*\*\*|&lt;--) .*)/<font color=\"$colours{"part"}\">$1<\/font>/;
316 }
317
318 # Process changed nick results, and remember colours accordingly #
319 elsif ($line =~ /^(\*\*\*|---) (.*?) are|is now known as (.*)/) {
320 my $nick_old;
321 my $nick_new;
322
323 $nick_old = $line;
324 $nick_old =~ s/^(?:\*\*\*|---) (.*?) (?:are|is) now known as .*/$1/;
325
326 $nick_new = $line;
327 $nick_new =~ s/^(?:\*\*\*|---) .*? (?:are|is) now known as (.*)/$1/;
328
329 $colour_nick{$nick_new} = $colour_nick{$nick_old};
330 $colour_nick{$nick_old} = undef;
331
332 $line =~ s/^((\*\*\*|---) .*)/<font color=\"$colours{"nickchange"}\">$1<\/font>/
333 }
334 # server messages
335 elsif ($line =~ /^(\*\*\*|---) /) {
336 $line =~ s/^((\*\*\*|---) .*)$/<font color=\"$colours{"server"}\">$1<\/font>/;
337 }
338
339 output_servermsg($line);
340 }
341 }
342 }
343
344 if ($STYLE =~ /table/) {
345 print OUTPUT "</table>\n";
346 }
347
348 print OUTPUT qq{
349 <br>Generated by irclog2html.pl $VERSION by <a href="mailto:jdub\@NOSPAMperkypants.org">Jeff Waugh</a>
350 - find it at <a href="http://freshmeat.net/projects/irclog2html.pl/">freshmeat.net</a>!
351 </tt></body></html>};
352
353 close INPUT;
354 close OUTPUT;
355
356 return 0;
357 }
358
359 exit main;