1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later
3 # version at your option.
4 # See the file README and COPYING for more information.
6 # [Other people have contributed to this file; their copyrights should
8 # Copyright 2004 by Collin Watson <cjwatson@debian.org>
9 # Copyright 2007 by Don Armstrong <don@donarmstrong.com>
18 use vars qw($VERSION $DEBUG @EXPORT @EXPORT_OK %EXPORT_TAGS);
19 use base qw(Exporter);
23 $DEBUG = 0 unless defined $DEBUG;
26 %EXPORT_TAGS = (write => [qw(write_log_records),
28 read => [qw(read_log_records),
30 misc => [qw(escape_log),
34 Exporter::export_ok_tags(qw(write read misc));
35 $EXPORT_TAGS{all} = [@EXPORT_OK];
40 use Debbugs::Common qw(getbuglocation getbugcomponent make_list);
41 use Params::Validate qw(:types validate_with);
42 use Encode qw(encode);
46 Debbugs::Log - an interface to debbugs .log files
50 The Debbugs::Log module provides a convenient way for scripts to read and
51 write the .log files used by debbugs to store the complete textual records
52 of all bug transactions.
54 =head2 The .log File Format
56 .log files consist of a sequence of records, of one of the following four
57 types. ^A, ^B, etc. represent those control characters.
67 C<[mail]> must start with /^Received: \(at \S+\) by \S+;/, and is copied to
72 Auto-forwarded messages are recorded like this:
78 C<[mail]> must contain /^X-Debian-Bugs(-\w+)?: This is an autoforward from
79 \S+/. The first line matching that is removed; all lines in the message body
80 that begin with 'X' will be copied to the output, minus the 'X'.
82 Nothing in debbugs actually generates this record type any more, but it may
83 still be in old .logs at some sites.
88 [recip]^D[recip]^D[...] OR -t
93 Each [recip] is output after "Message sent"; C<-t> represents the same
94 sendmail option, indicating that the recipients are taken from the headers
95 of the message itself.
103 [html] is copied unescaped to the output. The record immediately following
104 this one is considered "boring" and only shown in certain output modes.
106 (This is a design flaw in the log format, since it makes it difficult to
107 change the HTML presentation later, or to present the data in an entirely
112 No other types of records are permitted, and the file must end with a ^C
123 7 => 'incoming-recv',
126 =head2 Perl Record Representation
128 Each record is a hash. The C<type> field is C<incoming-recv>, C<autocheck>,
129 C<recips>, or C<html> as above; C<text> contains text from C<[mail]> or
130 C<[html]> as above; C<recips> is a reference to an array of recipients
131 (strings), or undef for C<-t>.
139 Creates a new log reader based on a .log filehandle.
141 my $log = Debbugs::Log->new($logfh);
142 my $log = Debbugs::Log->new(bug_num => $nnn);
143 my $log = Debbugs::Log->new(logfh => $logfh);
149 =item bug_num -- bug number
151 =item logfh -- log filehandle
153 =item log_name -- name of log
157 One of the above options must be passed.
166 ($param{logfh}) = @_;
169 %param = validate_with(params => \@_,
170 spec => {bug_num => {type => SCALAR,
173 logfh => {type => HANDLE,
176 log_name => {type => SCALAR,
182 if (grep({exists $param{$_} and defined $param{$_}} qw(bug_num logfh log_name)) ne 1) {
183 croak "Exactly one of bug_num, logfh, or log_name must be passed and must be defined";
186 my $class = ref($this) || $this;
190 if (exists $param{logfh}) {
191 $self->{logfh} = $param{logfh}
193 elsif (exists $param{log_name}) {
194 $self->{logfh} = IO::File->new($param{log_name},'r') or
195 die "Unable to open bug log $param{log_name} for reading: $!";
197 elsif (exists $param{bug_num}) {
198 my $location = getbuglocation($param{bug_num},'log');
199 my $bug_log = getbugcomponent($param{bug_num},'log',$location);
200 $self->{logfh} = IO::File->new($bug_log, 'r') or
201 die "Unable to open bug log $bug_log for reading: $!";
204 $self->{state} = 'kill-init';
205 $self->{linenum} = 0;
211 Reads and returns a single record from a log reader object. At end of file,
212 returns undef. Throws exceptions using die(), so you may want to wrap this
220 my $logfh = $this->{logfh};
222 # This comes from bugreport.cgi, but is much simpler since it doesn't
223 # worry about the details of output.
227 while (defined (my $line = <$logfh>)) {
230 if (length($line) == 1 and exists $states{ord($line)}) {
232 my $newstate = $states{ord($line)};
234 # disallowed transitions
235 $_ = "$this->{state} $newstate";
236 unless (/^(go|go-nox|html) kill-end$/ or
237 /^(kill-init|kill-end) (incoming-recv|autocheck|recips|html)$/ or
239 die "transition from $this->{state} to $newstate at $this->{linenum} disallowed";
242 $this->{state} = $newstate;
244 if ($this->{state} =~ /^(autocheck|recips|html|incoming-recv)$/) {
245 $record->{type} = $this->{state};
246 } elsif ($this->{state} eq 'kill-end') {
254 if ($this->{state} eq 'incoming-recv') {
256 unless (/^Received: \(at \S+\) by \S+;/) {
257 die "bad line '$pl' in state incoming-recv";
259 $this->{state} = 'go';
260 $record->{text} .= "$_\n";
261 } elsif ($this->{state} eq 'html') {
262 $record->{text} .= "$_\n";
263 } elsif ($this->{state} eq 'go') {
265 $record->{text} .= "$_\n";
266 } elsif ($this->{state} eq 'go-nox') {
267 $record->{text} .= "$_\n";
268 } elsif ($this->{state} eq 'recips') {
270 undef $record->{recips};
272 # preserve trailing null fields, e.g. #2298
273 $record->{recips} = [split /\04/, $_, -1];
275 $this->{state} = 'kill-body';
276 } elsif ($this->{state} eq 'autocheck') {
277 $record->{text} .= "$_\n";
278 next if !/^X-Debian-Bugs(-\w+)?: This is an autoforward from (\S+)/;
279 $this->{state} = 'autowait';
280 } elsif ($this->{state} eq 'autowait') {
281 $record->{text} .= "$_\n";
283 $this->{state} = 'go-nox';
285 die "state $this->{state} at line $this->{linenum} ('$_')";
288 die "state $this->{state} at end" unless $this->{state} eq 'kill-end';
297 =item read_log_records
299 Takes a .log filehandle as input, and returns an array of all records in
300 that file. Throws exceptions using die(), so you may want to wrap this in an
303 Uses exactly the same options as Debbugs::Log::new
311 ($param{logfh}) = @_;
314 %param = validate_with(params => \@_,
315 spec => {bug_num => {type => SCALAR,
318 logfh => {type => HANDLE,
321 log_name => {type => SCALAR,
327 if (grep({exists $param{$_} and defined $param{$_}} qw(bug_num logfh log_name)) ne 1) {
328 croak "Exactly one of bug_num, logfh, or log_name must be passed and must be defined";
332 my $reader = Debbugs::Log->new(%param);
333 while (defined(my $record = $reader->read_record())) {
334 push @records, $record;
339 =item write_log_records
341 Takes a filehandle and a list of records as input, and prints the .log
342 format representation of those records to that filehandle.
348 sub write_log_records
350 my %param = validate_with(params => \@_,
351 spec => {bug_num => {type => SCALAR,
354 logfh => {type => HANDLE,
357 log_name => {type => SCALAR,
360 records => {type => HASHREF|ARRAYREF,
364 if (grep({exists $param{$_} and defined $param{$_}} qw(bug_num logfh log_name)) ne 1) {
365 croak "Exactly one of bug_num, logfh, or log_name must be passed and must be defined";
368 if (exists $param{logfh}) {
369 $logfh = $param{logfh}
371 elsif (exists $param{log_name}) {
372 $logfh = IO::File->new(">>$param{log_name}") or
373 die "Unable to open bug log $param{log_name} for writing: $!";
375 elsif (exists $param{bug_num}) {
376 my $location = getbuglocation($param{bug_num},'log');
377 my $bug_log = getbugcomponent($param{bug_num},'log',$location);
378 $logfh = IO::File->new($bug_log, 'r') or
379 die "Unable to open bug log $bug_log for reading: $!";
381 my @records = make_list($param{records});
383 for my $record (@records) {
384 my $type = $record->{type};
385 croak "record type '$type' with no text field" unless defined $record->{text};
386 my ($text) = escape_log($record->{text});
387 if ($type eq 'autocheck') {
388 print {$logfh} "\01\n$text\03\n" or
389 die "Unable to write to logfile: $!";
390 } elsif ($type eq 'recips') {
391 print {$logfh} "\02\n";
392 my $recips = $record->{recips};
393 if (defined $recips) {
394 croak "recips not undef or array"
395 unless ref($recips) eq 'ARRAY';
396 print {$logfh} join("\04", @$recips) . "\n" or
397 die "Unable to write to logfile: $!";
399 print {$logfh} "-t\n" or
400 die "Unable to write to logfile: $!";
402 #$text =~ s/^([\01-\07\030])/\030$1/gm;
403 print {$logfh} "\05\n$text\03\n" or
404 die "Unable to write to logfile: $!";
405 } elsif ($type eq 'html') {
406 print {$logfh} "\06\n$text\03\n" or
407 die "Unable to write to logfile: $!";
408 } elsif ($type eq 'incoming-recv') {
409 #$text =~ s/^([\01-\07\030])/\030$1/gm;
410 print {$logfh} "\07\n$text\03\n" or
411 die "Unable to write to logfile: $!";
413 croak "unknown record type type '$type'";
422 print {$log} escape_log(@log)
424 Applies the log escape regex to the passed logfile.
430 return map { eval {$_ = encode("utf8",$_,Encode::FB_CROAK)}; s/^([\01-\07\030])/\030$1/gm; $_ } @log;
436 This module does none of the formatting that bugreport.cgi et al do. It's
437 simply a means for extracting and rewriting raw records.