Debian Codegraph @

podebconf-report-po

raw
1 #!/usr/bin/perl -w
2
3 # podebconf-report-po, Send outdated debconf PO files to the last translator
4 # Copyright (C) 2004-2006 Fabio Tranchitella <[email protected]>
5 # Denis Barbier <[email protected]>
6 # Copyright (C) 2007-2008 Nicolas François <[email protected]>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Library General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #
22
23 ## Release information
24 my $PROGRAM = "podebconf-report-po";
25 my $VERSION = "0.14";
26
27 ## Loaded modules, require libmail-sendmail-perl
28 use strict;
29 my $no_zlib = 0;
30 eval q{use Compress::Zlib;};
31 if ($@) {
32 $no_zlib = 1;
33 eval q{ sub Compress::Zlib::memGzip { return shift; } };
34 }
35 my $no_encode = 0;
36 eval q{use Encode;};
37 if ($@) {
38 $no_encode = 1;
39 }
40 use MIME::Base64;
41 use MIME::QuotedPrint;
42 use Getopt::Long;
43 use POSIX;
44 use File::Temp 'tempfile';
45 use Cwd 'abs_path';
46
47 ## Global variables
48 my $CONF_ARG;
49 if (-e $ENV{'HOME'}."/.podebconf-report-po.conf") {
50 $CONF_ARG = $ENV{'HOME'}."/.podebconf-report-po.conf";
51 }
52 my $NO_CONF;
53 my $HELP_ARG = 0;
54 my $VERSION_ARG = 0;
55 my $VERBOSE_ARG;
56 my $NO_VERBOSE;
57 my $SUBMIT_ARG = 0;
58 my $FORCE_ARG;
59 my $NO_FORCE;
60 my $CALL;
61 my $CALL_WITH_TRANSLATORS;
62 my $CALL_WITHOUT_TRANSLATORS;
63 my $POTFILE = "";
64 my $LANGS = "";
65 my $LANGUAGETEAM_ARG;
66 my $NO_LANGUAGETEAM;
67 my $SMTP_ARG;
68 my $TEMPLATE_ARG;
69 my $TEMPLATE_CALL;
70 my $TEMPLATE_TRANSLATORS;
71 my $TEMPLATE_SUBMIT;
72 my $NO_TEMPLATE;
73 my $DEFAULT_ARG;
74 my $NO_DEFAULT;
75 my $PACKAGE_ARG = "";
76 my $SUMMARY_ARG;
77 my $NO_SUMMARY;
78 my $FROM_ARG;
79 my $BTS_ARG = "";
80 my $DEADLINE_ARG;
81 my $NO_DEADLINE;
82 my $PODIR_ARG = "";
83 my $GZIP_ARG;
84 my $NO_GZIP;
85 my $UTF8;
86 my $NO_UTF8;
87 my $MBOX = "";
88 my $MUTT = 0;
89 my @ADDLANGUAGETEAM = ();
90 my $SEND_MESSAGE = 0;
91 my $NOT_DEBCONF = 0;
92
93 my @TOPDIRS = qw{../.. .. .};
94
95 my $PODIR = '';
96
97 my $EDITOR = '/usr/bin/sensible-editor';
98
99 ## Default subjects (used if the specified template does not contain a
100 ## Subject field).
101 my $SUBJECT_TRANSLATOR;
102 my $SUBJECT_SUBMIT;
103 my $SUBJECT_CALL;
104
105 my $SUBJECT = '';
106 my $BODY = '';
107 # Warnings may be deleted from screen when entering editor,
108 # so display them when it is closed.
109 my $warn = '';
110
111 ## Handle options
112 GetOptions
113 (
114 "conf=s" => \$CONF_ARG,
115 "noconf" => \$NO_CONF,
116 "help" => \$HELP_ARG,
117 "version" => \$VERSION_ARG,
118 "v|verbose" => \$VERBOSE_ARG,
119 "noverbose" => \$NO_VERBOSE,
120 "f|force" => \$FORCE_ARG,
121 "noforce" => \$NO_FORCE,
122 "podir=s" => \$PODIR_ARG,
123 "smtp=s" => \$SMTP_ARG,
124 "template=s" => \$TEMPLATE_ARG,
125 "templatetranslators=s" => \$TEMPLATE_TRANSLATORS,
126 "templatecall=s" => \$TEMPLATE_CALL,
127 "templatesubmit=s" => \$TEMPLATE_SUBMIT,
128 "notemplate" => \$NO_TEMPLATE,
129 "default" => \$DEFAULT_ARG,
130 "nodefault" => \$NO_DEFAULT,
131 "gzip" => \$GZIP_ARG,
132 "nogzip" => \$NO_GZIP,
133 "langs=s" => \$LANGS,
134 "languageteam" => \$LANGUAGETEAM_ARG,
135 "nolanguageteam" => \$NO_LANGUAGETEAM,
136 "addlanguageteam=s"=>\@ADDLANGUAGETEAM,
137 "package=s" => \$PACKAGE_ARG,
138 "deadline=s" => \$DEADLINE_ARG,
139 "nodeadline" => \$NO_DEADLINE,
140 "call:s" => \$CALL,
141 "withtranslators" => \$CALL_WITH_TRANSLATORS,
142 "withouttranslators" => \$CALL_WITHOUT_TRANSLATORS,
143 "potfile=s" => \$POTFILE,
144 "summary" => \$SUMMARY_ARG,
145 "nosummary" => \$NO_SUMMARY,
146 "from=s" => \$FROM_ARG,
147 "bts=s" => \$BTS_ARG,
148 "submit" => \$SUBMIT_ARG,
149 "postpone=s" => \$MBOX,
150 "mutt" => \$MUTT,
151 "utf8" => \$UTF8,
152 "noutf8" => \$NO_UTF8,
153 "notdebconf" => \$NOT_DEBCONF,
154 "sendmessage" => \$SEND_MESSAGE
155 ) or &Help_InvalidOption;
156
157 &Help_PrintVersion if $VERSION_ARG;
158 &Help_PrintHelp if $HELP_ARG;
159
160 eval q{use Mail::Sendmail;};
161 die "$PROGRAM: This program requires the libmail-sendmail-perl package.\n".
162 "$PROGRAM: Aborting!\n" if $@;
163
164 # Check invalid set of options
165 if ($LANGUAGETEAM_ARG && defined $CALL && not $CALL_WITH_TRANSLATORS) {
166 die "In the --call mode, the --languageteam option is only valid if --withtranslators is specified.\n";
167 }
168
169 ## Try to locate the PO directory
170 if ($PODIR_ARG eq "") {
171 my $dir = getcwd;
172
173 if ($NOT_DEBCONF) {
174 if ($dir =~ m/\/po$/) {
175 $PODIR = ".";
176 } elsif (-d "$dir/po") {
177 $PODIR = "po";
178 }
179 } else {
180 if ($dir =~ m/\/po$/) {
181 $PODIR = ".";
182 } elsif (-d "$dir/debian/po") {
183 $PODIR = "debian/po";
184 } elsif (-d "$dir/po") {
185 $PODIR = "po";
186 }
187 }
188 } else {
189 $PODIR = $PODIR_ARG;
190 }
191 die "Directory po not found, exiting!\n" if $PODIR eq "";
192 die "Wrong argument: $PODIR is not a directory!\n" unless -d $PODIR;
193
194 ## Try to detect if it is a debconf template translation
195 unless ($NOT_DEBCONF) {
196 my $dir = abs_path($PODIR);
197 if ($dir =~ m/\/po$/ and $dir !~ m/\/debian\/po$/) {
198 $NOT_DEBCONF = 1;
199 }
200 }
201
202 ## Define the default subjects (if not set in the template)
203 if ($NOT_DEBCONF) {
204 $SUBJECT_TRANSLATOR = "<package_and_version>: Please update the PO translation for the package <package>";
205 $SUBJECT_SUBMIT = "PO translations for the package <package> are outdated";
206 $SUBJECT_CALL = "<package_and_version>: Please translate the package <package>";
207 } else {
208 $SUBJECT_TRANSLATOR = "<package_and_version>: Please update debconf PO translation for the package <package>";
209 $SUBJECT_SUBMIT = "debconf PO translations for the package <package> are outdated";
210 $SUBJECT_CALL = "<package_and_version>: Please translate debconf PO for the package <package>";
211 }
212
213 my $conf = "";
214
215 unless ($NO_CONF or !defined $CONF_ARG) {
216 open (CNF, "< $CONF_ARG")
217 or die ("Couldn't read $CONF_ARG: $!\nExiting!\n");
218 while (<CNF>) {
219 $conf .= $_;
220 }
221 close(CNF)
222 or die ("Couldn't close $CONF_ARG: $!\nExiting!\n");
223
224 $conf =~ s/^\s*#.*$//m;
225 $conf =~ s/\s*$//m;
226 }
227
228 if ($conf =~ m/^smtp\s*(?:\s|=)\s*(.*)$/m) {
229 $SMTP_ARG = $1;
230 } elsif (!defined $SMTP_ARG) {
231 $SMTP_ARG = "";
232 }
233
234 if (defined $FROM_ARG) {
235 # Use the from parameter from the command line
236 } elsif ($conf =~ m/^from\s*(?:\s|=)\s*(.*)$/m) {
237 $FROM_ARG = $1;
238 } elsif (!defined $FROM_ARG) {
239 # This part comes from devscripts' bts
240 if ($ENV{'DEBEMAIL'} || $ENV{'EMAIL'}) {
241 my ($email, $name);
242 if (exists $ENV{'DEBFULLNAME'}) { $name = $ENV{'DEBFULLNAME'}; }
243 if (exists $ENV{'DEBEMAIL'}) {
244 $email = $ENV{'DEBEMAIL'};
245 if ($email =~ /^(.*?)\s+<(.*)>\s*$/) {
246 $name ||= $1;
247 $email = $2;
248 }
249 }
250 if (exists $ENV{'EMAIL'}) {
251 if ($ENV{'EMAIL'} =~ /^(.*?)\s+<(.*)>\s*$/) {
252 $name ||= $1;
253 $email ||= $2;
254 } else {
255 $email ||= $ENV{'EMAIL'};
256 }
257 }
258 if (! $name) {
259 # Perhaps not ideal, but it will have to do
260 $name = (getpwuid($<))[6];
261 $name =~ s/,.*//;
262 }
263 $FROM_ARG = $name ? "$name <$email>" : $email;
264 } else {
265 # We will try below to use the Maintainer: control field
266 $FROM_ARG = "";
267 }
268 }
269
270 if (defined $NO_VERBOSE) {
271 $VERBOSE_ARG = 0;
272 } elsif ($conf =~ m/^verbose$/m) {
273 $VERBOSE_ARG = 1;
274 } elsif (!defined $VERBOSE_ARG) {
275 $VERBOSE_ARG = 0;
276 }
277
278 if (defined $NO_FORCE) {
279 $FORCE_ARG = 0;
280 } elsif ($conf =~ m/^force$/m) {
281 $FORCE_ARG = 1;
282 } elsif (!defined $FORCE_ARG) {
283 $FORCE_ARG = 0;
284 }
285
286 if (defined $NO_TEMPLATE) {
287 $TEMPLATE_ARG = "";
288 } elsif ($conf =~ m/^template\s*(?:\s|=)\s*(.*)$/m) {
289 $TEMPLATE_ARG = $1;
290 } elsif (!defined $TEMPLATE_ARG) {
291 $TEMPLATE_ARG = "";
292 }
293
294 if (defined $TEMPLATE_TRANSLATORS) {
295 # Command line has the highest priority
296 } elsif ($conf =~ m/^templatetranslators\s*(?:\s|=)\s*(.*)$/m) {
297 $TEMPLATE_TRANSLATORS = $1;
298 } else {
299 if ($NOT_DEBCONF) {
300 $TEMPLATE_TRANSLATORS ="/usr/share/po-debconf/templates/translators-po";
301 } else {
302 $TEMPLATE_TRANSLATORS ="/usr/share/po-debconf/templates/translators";
303 }
304 }
305
306 if (defined $TEMPLATE_SUBMIT) {
307 # Command line has the highest priority
308 } elsif ($conf =~ m/^templatesubmit\s*(?:\s|=)\s*(.*)$/m) {
309 $TEMPLATE_SUBMIT = $1;
310 } else {
311 $TEMPLATE_SUBMIT ="/usr/share/po-debconf/templates/submit";
312 }
313
314 if (defined $TEMPLATE_CALL) {
315 # Command line has the highest priority
316 } elsif ($conf =~ m/^templatecall\s*(?:\s|=)\s*(.*)$/m) {
317 $TEMPLATE_CALL = $1;
318 } else {
319 if ($NOT_DEBCONF) {
320 $TEMPLATE_CALL ="/usr/share/po-debconf/templates/call-po";
321 } else {
322 $TEMPLATE_CALL ="/usr/share/po-debconf/templates/call";
323 }
324 }
325
326 if ($TEMPLATE_ARG ne "") {
327 $TEMPLATE_TRANSLATORS = $TEMPLATE_ARG;
328 $TEMPLATE_SUBMIT = $TEMPLATE_ARG;
329 $TEMPLATE_CALL = $TEMPLATE_ARG;
330 }
331
332 if (defined $NO_DEFAULT) {
333 $DEFAULT_ARG = 0;
334 } elsif ($conf =~ m/^default$/m) {
335 $DEFAULT_ARG = 1;
336 } elsif (!defined $DEFAULT_ARG) {
337 $DEFAULT_ARG = 0;
338 }
339
340 if (defined $NO_GZIP) {
341 $GZIP_ARG = 0;
342 } elsif ($conf =~ m/^gzip$/m) {
343 $GZIP_ARG = 1;
344 } elsif (!defined $GZIP_ARG) {
345 $GZIP_ARG = 0;
346 }
347
348 if (defined $NO_DEADLINE) {
349 undef $DEADLINE_ARG;
350 } elsif (defined $DEADLINE_ARG) {
351 # Use the specified deadline
352 } elsif ($conf =~ m/^nodeadline$/m) {
353 undef $DEADLINE_ARG;
354 } elsif ($conf =~ m/^deadline\s*(?:\s|=)\s*(.*)$/m) {
355 $DEADLINE_ARG = $1;
356 } else {
357 print "
358 You should specify a deadline to help translators organize their work.
359 This deadline is usually the date you are planning to make the next
360 release (or the day before).
361 Deadline? [+10days] ";
362 chomp($DEADLINE_ARG = <STDIN>);
363
364 # The default deadline is +10days
365 if ($DEADLINE_ARG eq "") {
366 $DEADLINE_ARG = "+10days";
367 }
368 }
369
370 if (defined $NO_LANGUAGETEAM) {
371 $LANGUAGETEAM_ARG = 0;
372 } elsif (defined $LANGUAGETEAM_ARG) {
373 $LANGUAGETEAM_ARG = 1;
374 } elsif ($conf =~ m/^languageteam$/m) {
375 $LANGUAGETEAM_ARG = 1;
376 } elsif ($conf =~ m/^nolanguageteam$/m) {
377 $LANGUAGETEAM_ARG = 0;
378 } else {
379 $LANGUAGETEAM_ARG = 1;
380 }
381
382 while ($conf =~ m/^addlanguageteam\s*(?:\s|=)\s*(.*)$/gm) {
383 push @ADDLANGUAGETEAM, $1;
384 }
385
386 if (defined $NO_SUMMARY) {
387 $SUMMARY_ARG = 0;
388 } elsif ($conf =~ m/^summary$/m) {
389 $SUMMARY_ARG = 1;
390 } elsif (!defined $SUMMARY_ARG) {
391 $SUMMARY_ARG = 0;
392 }
393
394 if (defined $NO_UTF8) {
395 $UTF8 = 0;
396 } elsif ($conf =~ m/^utf8$/m) {
397 $UTF8 = 1;
398 } elsif (!defined $UTF8) {
399 $UTF8 = 0;
400 }
401
402 if (defined $CALL_WITHOUT_TRANSLATORS) {
403 $CALL_WITH_TRANSLATORS = 0;
404 } elsif (defined $CALL_WITH_TRANSLATORS) {
405 $CALL_WITH_TRANSLATORS = 1;
406 } elsif ($conf =~ m/^withtranslators$/m) {
407 $CALL_WITH_TRANSLATORS = 1;
408 } elsif ($conf =~ m/^withouttranslators$/m) {
409 $CALL_WITH_TRANSLATORS = 0;
410 } else {
411 # The default. (it will be removed when not in --call mode)
412 $CALL_WITH_TRANSLATORS = 1;
413 }
414
415 # Disable --withtranslators if no call for translations are requested with
416 # --call.
417 $CALL_WITH_TRANSLATORS = 0 unless defined $CALL;
418
419 if ($no_encode and $UTF8) {
420 $warn .= "--utf8 requires the Encode perl module. ".
421 "Turning this option off.\n";
422 $UTF8 = 0;
423 }
424
425 if ($MUTT) {
426 $MBOX = qx/mutt -Q postponed/;
427 if ($MBOX =~ m/^postponed="(.*)"$/) {
428 $MBOX = $1;
429 } else {
430 $MBOX = $ENV{'HOME'}."/postponed";
431 warn "Could not find mutt's postpone mailbox with ".
432 " 'mutt -Q postponed'. Using $MBOX.";
433 }
434 }
435
436 ## Try to find default editor
437 $EDITOR = $ENV{'EDITOR'} if exists($ENV{'EDITOR'});
438 $EDITOR = $ENV{'VISUAL'} if exists($ENV{'VISUAL'});
439
440 if ($no_zlib && $GZIP_ARG) {
441 $warn .=
442 "Warning: This program requires the libcompress-zlib-perl package in order\n".
443 " to support the --gzip flag, but it is not installed.\n".
444 " PO files will not be compressed!\n\n";
445 $GZIP_ARG = 0;
446 }
447
448 if ($POTFILE eq "") {
449 opendir(DIR, $PODIR);
450 foreach my $potFile (grep(/\.pot$/, readdir(DIR))) {
451 if (length $POTFILE) {
452 die "Too many pot file found.\n".
453 "Please specify one with the --potfile option.\n";
454 }
455 $POTFILE = $potFile;
456 }
457 closedir(DIR);
458 opendir(DIR, $PODIR);
459 if (length $POTFILE) {
460 print "Using $POTFILE for the call for translation\n";
461 } else {
462 warn "No POT file found. You should specify one with the ".
463 "--potfile option, or specify in the mail how to ".
464 "retrieve it."
465 }
466 closedir(DIR);
467 }
468
469 ## Try to find the maintainer e-mail address and the package name
470
471 # Package version
472 my $PKG_VERSION = "N/A";
473 # Expanded into "<package> <version>" if version is found, <package> otherwise
474 my $PACKAGE_AND_VERSION = "";
475 if ($PACKAGE_ARG =~ s/_(.*)//) {
476 $PKG_VERSION = $1;
477 }
478
479 if ($PACKAGE_ARG eq "" or $FROM_ARG eq "") {
480 my $CONTROL = '';
481 foreach my $d (@TOPDIRS) {
482 $CONTROL = "$d/debian/control" if (-f "$d/debian/control");
483 }
484 if ($CONTROL eq '') {
485 foreach my $d (@TOPDIRS) {
486 $CONTROL = "$d/debian/control.in" if (-f "$d/debian/control.in");
487 }
488 }
489
490 if (-f $CONTROL) {
491 ## Only read the first stanza
492 local $/ = "\n\n";
493 open (CNTRL, "< $CONTROL")
494 or die "Unable to read $CONTROL: $!\n";
495 my $text = <CNTRL>;
496 close (CNTRL)
497 or die "Unable to close $CONTROL: $!\n";
498 if ($PACKAGE_ARG eq "" && $text =~ m/^Source: (.*)/m) {
499 $PACKAGE_ARG = $1;
500 }
501
502 if ($FROM_ARG eq "" && $text =~ m/^Maintainer: (.*)/m) {
503 $FROM_ARG = $1;
504 }
505 }
506 }
507 if ($PKG_VERSION eq "N/A") {
508 my $CHANGELOG = '';
509 foreach my $d (@TOPDIRS) {
510 $CHANGELOG = "$d/debian/changelog" if (-f "$d/debian/changelog");
511 }
512 if (-f $CHANGELOG) {
513 # Version information is not vital, do not abort
514 # if it cannot be retrieved.
515 if (open (CHG, "< $CHANGELOG")) {
516 while (<CHG>) {
517 if (m/^$PACKAGE_ARG\s+\((.*)\)\s/) {
518 $PKG_VERSION = $1;
519 }
520 last if m/^ --/;
521 }
522 }
523 close (CHG);
524 }
525 }
526 if ($PKG_VERSION eq 'N/A' and $PACKAGE_ARG eq '') {
527 # Try to read the PACKAGEand VERSION from the .pot file
528 Verbose("PODIR: $PODIR");
529 Verbose("POTFILE: $POTFILE");
530 my $content = &ReadFile($PODIR . "/" . $POTFILE);
531 $content =~ m/\n"Project-Id-Version:\s*([^"]*)\s*\\n"\n/i;
532 my $package_and_version = $1;
533 if ($package_and_version =~ m/^(.+)\s+(.+?)$/i) {
534 $PACKAGE_ARG = $1;
535 $PKG_VERSION = $2;
536 } else {
537 $PACKAGE_ARG = $package_and_version;
538 }
539 }
540 $PACKAGE_AND_VERSION = $PACKAGE_ARG .
541 ($PKG_VERSION ne 'N/A' ? " ".$PKG_VERSION : "");
542 Verbose("Package: $PACKAGE_ARG");
543 Verbose("Version: $PKG_VERSION");
544 Verbose("Maintainer: $FROM_ARG");
545
546 # If the specified deadline starts with a '+', it is an offset from now.
547 if ($DEADLINE_ARG =~ m/^\+/) {
548 my $cmd = "LC_ALL=C date -R -d \"$DEADLINE_ARG\"";
549 $DEADLINE_ARG = qx/$cmd/;
550 if ($? != 0) {
551 die "podebconf-report-po: failed to execute '$cmd': $!.\n";
552 }
553 chomp $DEADLINE_ARG;
554 }
555
556 if ($DEADLINE_ARG ne "") {
557 $DEADLINE_ARG = "\n\nThe deadline for receiving the updated translation is\n$DEADLINE_ARG.";
558 }
559
560 my $REPLY = '';
561 if ($BTS_ARG =~ m/^\d+$/) {
562 $BTS_ARG .= "\@bugs.debian.org";
563 $REPLY = "Please respect the Reply-To: field and send your updated translation to\n$BTS_ARG.";
564 } else {
565 $REPLY = "Please send the updated file to me, or submit it as a wishlist bug\nagainst <package>.";
566 }
567
568 PREPARE_MAIL:
569 if ($SUBMIT_ARG) {
570 $BODY = &ReadFile($TEMPLATE_SUBMIT);
571 $SUBJECT = $SUBJECT_SUBMIT;
572 } elsif (defined $CALL) {
573 $CALL="Debian Internationalization <debian-i18n\@lists.debian.org>"
574 unless length $CALL;
575 $BODY = &ReadFile($TEMPLATE_CALL);
576 $SUBJECT = $SUBJECT_CALL;
577 } else {
578 $CALL="";
579 $BODY = &ReadFile($TEMPLATE_TRANSLATORS);
580 $SUBJECT = $SUBJECT_TRANSLATOR;
581 }
582
583 ## Apply the values to the subject and to the body of the message
584
585 $SUBJECT =~ s/<package>/$PACKAGE_ARG/g;
586 $SUBJECT =~ s/<version>/$PKG_VERSION/g;
587 $SUBJECT =~ s/<package_and_version>/$PACKAGE_AND_VERSION/g;
588
589 ## Check every file with .po extension in $PODIR ...
590 Verbose("Checking for PO files in $PODIR");
591 opendir(DIR, $PODIR);
592 my $poFiles = {};
593 my $statistics = "language translated fuzzy untranslated\n".
594 "-----------------------------------------------------\n";
595 if (defined $CALL and length $CALL) {
596 foreach my $poFile (grep(/\.po$/, sort readdir(DIR))) {
597 $poFiles->{$poFile} = {};
598 my $cmd = "LC_ALL=C /usr/bin/msgfmt -o /dev/null --stat $PODIR/$poFile 2>&1";
599 my $stats = qx/$cmd/;
600 chomp $stats;
601 my ($t, $f, $u) = ("", "", "");
602 my $lang = $poFile;
603 $lang =~ s/\.po$//;
604 if ($stats =~ s/^([0-9]+) translated message[s ,]*//) {
605 $t = $1;
606 }
607 if ($stats =~ s/^([0-9]+) fuzzy translation[s ,]*//) {
608 $f = $1;
609 }
610 if ($stats =~ s/^([0-9]+) untranslated message[s ,]*//) {
611 $u = $1;
612 }
613 $statistics .= sprintf(" %-10s%10s %10s %10s\n", $lang, $t, $f, $u);
614 }
615 } else {
616 foreach my $poFile (grep(/\.po$/, readdir(DIR))) {
617 local $/ = "\n\n";
618 $poFiles->{$poFile} = {};
619 my $outdated = 0;
620 my $found_header = 0;
621 open (PO, "< $PODIR/$poFile")
622 or die "Unable to read $PODIR/$poFile: $!\n";
623 while (<PO>) {
624 if ($found_header == 0 && m/msgid ""\nmsgstr/s) {
625 $found_header = 1;
626 # Concatenate lines
627 s/"\n"//g;
628 if (m/\\nLast-Translator: (.*?)\\n/ && $1 ne 'FULL NAME <EMAIL@ADDRESS>') {
629 $poFiles->{$poFile}->{translator} = $1;
630 } else {
631 $warn .= "Warning: $poFile: Unable to determine last translator. Skipping file!\n";
632 last;
633 }
634 if (m/\\nContent-Type: [^;]*; charset=(.*?)\\n/) {
635 $poFiles->{$poFile}->{charset} = $1;
636 } else {
637 $warn .= "Warning: $poFile: Unable to determine charset. Skipping file!\n";
638 last;
639 }
640 if ($LANGUAGETEAM_ARG && m/\\nLanguage-Team: (.*?)\\n/) {
641 $poFiles->{$poFile}->{team} = $1
642 if $1 ne 'LANGUAGE <[email protected]>';
643 }
644
645 my $lang = $poFile;
646 $lang =~ s/\.po$//;
647 if ($LANGUAGETEAM_ARG) {
648 foreach my $lang_list (@ADDLANGUAGETEAM) {
649 next unless ($lang_list =~ m/^$lang:(.*)$/);
650
651 my $list = $1;
652 my $list_addr = $1;
653 $list_addr =~ s/^.*?<([^<>]*)>.*?$/$1/;
654 $poFiles->{$poFile}->{team} = ""
655 unless (defined $poFiles->{$poFile}->{team});
656 next if ($poFiles->{$poFile}->{team} =~ m/(^|<)\Q$list_addr\E(>|$)/);
657
658 if (length $poFiles->{$poFile}->{team}) {
659 $poFiles->{$poFile}->{team} .= ", ";
660 }
661 $poFiles->{$poFile}->{team} .= $list;
662 }
663 }
664 next;
665 }
666 # Ignore outdated msgids
667 next unless m/^msgid /m;
668 # Check for fuzzy or missing translations
669 s/\n+$//s;
670 if (m/^#, .*fuzzy/m or m/\nmsgstr ""$/s) {
671 $outdated = 1;
672 last;
673 }
674 }
675 if ($UTF8) {
676 Encode::from_to($poFiles->{$poFile}->{translator},
677 $poFiles->{$poFile}->{charset},
678 "UTF-8");
679 Encode::from_to($poFiles->{$poFile}->{team},
680 $poFiles->{$poFile}->{charset},
681 "UTF-8");
682 }
683 close (PO)
684 or die "Unable to close $PODIR/$poFile: $!\n";
685 delete $poFiles->{$poFile} unless $outdated;
686 }
687 closedir(DIR);
688 if (keys %$poFiles) {
689 print "Outdated files: ".join(' ', keys %$poFiles)."\n";
690 } else {
691 print "No outdated files\n";
692 exit(0);
693 }
694 }
695
696 my %langs=();
697 foreach (split(",", $LANGS)) {
698 $langs{$_.".po"} = 1;
699 }
700
701 my $filelist = '';
702 if ($SUBMIT_ARG or length $CALL) {
703 $filelist = join(' ', sort keys %$poFiles)."\n";
704 } else {
705 # Try to detect invalid emails.
706 # This is very frequent for language teams.
707 my $warn_invalid_email = 0;
708 foreach my $poFile (sort keys %$poFiles) {
709 if ($poFiles->{$poFile}->{translator} !~ /^(.*<)?[^@]+@[^@]+\.[A-Za-z]{2,4}>?$/) {
710 $warn_invalid_email = 1;
711 last;
712 }
713 if (defined($poFiles->{$poFile}->{team}) and
714 $poFiles->{$poFile}->{team} !~ /^(.*<)?[^@]+@[^@]+\.[A-Za-z]{2,4}>?$/) {
715 $warn_invalid_email = 1;
716 last;
717 }
718 }
719 if ($warn_invalid_email) {
720 $filelist .= "#\n"
721 ."# WARNING: Some email addresses seem to be invalid.\n"
722 ."# You should remove them and inform the translators separately.\n"
723 ."#\n";
724 }
725 # Add the list of PO files.
726 foreach my $poFile (sort keys %$poFiles) {
727 $filelist .= '### ';
728 $filelist .= '[' .((!%langs or $langs{$poFile})?'*':' '). '] ';
729 $filelist .= $poFile . ': ' . $poFiles->{$poFile}->{translator};
730 $filelist .= ', ' . $poFiles->{$poFile}->{team} if defined($poFiles->{$poFile}->{team});
731 $filelist .= "\n";
732 }
733 # Remove non-ASCII characters
734 $filelist = DropNonASCII($filelist)
735 unless ($UTF8);
736 }
737 $filelist =~ s/\n$//s;
738
739 my %headers = ();
740
741 OPEN_EDITOR:
742 ReplaceTags();
743 $BODY = &OpenEditor($EDITOR, $BODY) if not $DEFAULT_ARG;
744 ReplaceTags();
745
746 %headers = &ParseHeaders($BODY);
747 my %To = &ParseTo($BODY);
748
749 print STDERR $warn if $warn ne '';
750
751 my @mails = ();
752 if ($SUBMIT_ARG) {
753 my %mail = (
754 From => $FROM_ARG,
755 To => "maintonly\@bugs.debian.org",
756 Subject => $SUBJECT,
757 'X-Mail-Originator' => "$PROGRAM $VERSION"
758 );
759 $mail{body} = encode_qp(&RemoveHeaders($BODY));
760 @mails = (\%mail);
761 } elsif (length $CALL) {
762 my %mail = (
763 From => $FROM_ARG,
764 To => $CALL,
765 Subject => $SUBJECT,
766 'X-Mail-Originator' => "$PROGRAM $VERSION"
767 );
768 my $ext = ($GZIP_ARG ? '.gz' : '');
769 my $file = $POTFILE;
770 my $content = &ReadFile($PODIR . "/" . $file);
771 $content = Compress::Zlib::memGzip($content) if $GZIP_ARG;
772 my $file_encoded = encode_base64($content);
773 my $contentType = ($GZIP_ARG ? "application/octet-stream": "text/x-gettext; name=\"$file\"; charset=\"US-ASCII\"");
774 my $boundary = "=" . time() . "=";
775 $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
776 my $charset = $UTF8?"utf-8":"us-ascii";
777 my $body = &RemoveHeaders($BODY);
778 $mail{body} = <<_EOF_;
779 --$boundary
780 Content-Type: text/plain; charset="$charset"
781 Content-Transfer-Encoding: quoted-printable
782
783 $body
784
785 _EOF_
786 if ($SEND_MESSAGE) {
787 $mail{body} .= <<_EOF_;
788 --$boundary--
789 _EOF_
790 } else {
791 $mail{body} .= <<_EOF_;
792 --$boundary
793 Content-Type: $contentType
794 Content-Transfer-Encoding: base64
795 Content-Disposition: attachment; filename="$file$ext"
796
797 $file_encoded
798 --$boundary--
799 _EOF_
800 }
801 @mails = (\%mail);
802 } else {
803 my $body = encode_qp(&RemoveHeaders($BODY));
804 my $ext = ($GZIP_ARG ? '.gz' : '');
805 foreach my $file (keys %$poFiles) {
806 if (defined $To{$file}) {
807 my $content = &ReadFile($PODIR . "/" . $file);
808 $content = Compress::Zlib::memGzip($content) if $GZIP_ARG;
809 my $file_encoded = encode_base64($content);
810 my $contentType = ($GZIP_ARG ? "application/octet-stream" : "text/x-gettext; name=\"$file\"; charset=\"$poFiles->{$file}->{charset}\"");
811 my %mail = (
812 From => $FROM_ARG,
813 To => $To{$file},
814 Subject => $SUBJECT,
815 'X-Mail-Originator' => "$PROGRAM $VERSION"
816 );
817
818 my $boundary = "=" . time() . "=";
819 $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
820 my $charset = $UTF8?"utf-8":"us-ascii";
821 $mail{body} = <<_EOF_;
822 --$boundary
823 Content-Type: text/plain; charset="$charset"
824 Content-Transfer-Encoding: quoted-printable
825
826 $body
827
828 _EOF_
829 if ($SEND_MESSAGE) {
830 $mail{body} .= <<_EOF_;
831 --$boundary--
832 _EOF_
833 } else {
834 $mail{body} .= <<_EOF_;
835 --$boundary
836 Content-Type: $contentType
837 Content-Transfer-Encoding: base64
838 Content-Disposition: attachment; filename="$file$ext"
839
840 $file_encoded
841 --$boundary--
842 _EOF_
843 }
844
845 push(@mails, \%mail);
846 }
847 }
848 }
849
850 # Add mail headers and remove non-ASCII characters
851 foreach my $refmail (@mails) {
852 foreach my $h (keys(%headers)) {
853 if ($UTF8) {
854 $refmail->{$h} = encode_qp($headers{$h}, "");
855 $refmail->{$h} =~ s/=$//m;
856 $refmail->{$h} =~ s/(\S*=\S*)/=?utf-8?Q?$1?=/g;
857 } else {
858 $refmail->{$h} = &DropNonASCII($headers{$h});
859 }
860 }
861 foreach my $h (qw(From To Subject)) {
862 if ($UTF8) {
863 unless ($refmail->{$h} =~ m/=\?utf-8\?Q\?/) {
864 $refmail->{$h} = encode_qp($refmail->{$h}, "");
865 $refmail->{$h} =~ s/=$//m;
866 $refmail->{$h} =~ s/(\S*=\S*)/=?utf-8?Q?$1?=/g;
867 }
868 } else {
869 $refmail->{$h} = &DropNonASCII($refmail->{$h});
870 }
871 }
872 $refmail->{smtp} = $SMTP_ARG if ($SMTP_ARG ne '');
873 }
874
875 if (!$FORCE_ARG) {
876 my $answers = ($DEFAULT_ARG)?"[y/N/?]":"[y/N/e/?]";
877 my $with_mutt = "";
878 $with_mutt = " (with mutt)" if $MUTT;
879 QUESTION:
880 print "The following files have been selected:\n";
881 foreach my $poFile (sort keys %$poFiles) {
882 next unless defined $To{$poFile};
883 print " $poFile To: $To{$poFile}\n";
884 }
885 print "End of files\n";
886 if ($SUBMIT_ARG) {
887 print "Ready to send$with_mutt the bug report against the package $PACKAGE_ARG, are you sure? $answers ";
888 } elsif (length $CALL) {
889 print "Ready to send$with_mutt the call for translation to $CALL, are you sure? $answers ";
890 } else {
891 print "Ready to send$with_mutt the emails, are you sure? $answers ";
892 }
893 my $line = <>;
894 chop $line;
895 if ($line eq "e" or $line eq "E") {
896 goto OPEN_EDITOR unless ($DEFAULT_ARG);
897 } elsif ($line eq "?") {
898 print "y send the mail(s).\n".
899 "? display this help message.\n".
900 ($DEFAULT_ARG?"":"e reopen the editor.\n").
901 "N exit, without sending mails.\n";
902 goto QUESTION;
903 }
904 exit(0) if ($line ne "Y" and $line ne "y");
905 }
906
907 # Make Perl compiler quiet
908 print $Mail::Sendmail::error . $Mail::Sendmail::error if 0;
909 foreach my $mail (@mails) {
910 if (defined $MBOX and length $MBOX) {
911 Postpone($mail);
912 } else {
913 sendmail(%{$mail})
914 or print "Couldn't send the email: $Mail::Sendmail::error\n";
915 }
916 }
917 if ($SUMMARY_ARG and not $CALL) {
918 my %summary = (
919 From => $FROM_ARG,
920 To => $FROM_ARG,
921 Subject => $SUBJECT,
922 'X-Mail-Originator' => "$PROGRAM $VERSION"
923 );
924 $summary{body} = "List of outdated files:\n";
925 foreach my $poFile (sort keys %$poFiles) {
926 $summary{body} .= ' ' . $poFile . ': ' . $poFiles->{$poFile}->{translator};
927 $summary{body} .= ', ' . $poFiles->{$poFile}->{team} if defined($poFiles->{$poFile}->{team});
928 $summary{body} .= "\n";
929 }
930 $summary{body} .= "Translators received the mail below.\n\n";
931 $summary{body} .= encode_qp(&RemoveHeaders($BODY));
932 if (defined $MBOX and length $MBOX) {
933 Postpone(\%summary);
934 } else {
935 sendmail(%summary)
936 or print "Couldn't send the email: $Mail::Sendmail::error\n";
937 }
938 }
939
940 if ($MUTT) {
941 if (system("mutt -p") >> 8 != 0) {
942 die "Problem running mutt -p: $!\n";
943 }
944 }
945
946 if ($CALL_WITH_TRANSLATORS) {
947 print "Now, prepare the mail for translators...";
948 undef $CALL;
949 undef $CALL_WITH_TRANSLATORS;
950 goto PREPARE_MAIL;
951 }
952
953 exit(0);
954
955 ###############################################################################
956
957 sub ReplaceTags {
958 $BODY =~ s/<reply>/$REPLY/g;
959 $BODY =~ s/<reply-to>/$BTS_ARG/g;
960 $BODY =~ s/\n# Reply-To: \n/\n/;
961 $BODY =~ s/<subject>/$SUBJECT/g;
962 $BODY =~ s/<package>/$PACKAGE_ARG/g if ($PACKAGE_ARG ne '');
963 $BODY =~ s/<version>/$PKG_VERSION/g if ($PKG_VERSION ne '');
964 $BODY =~ s/<package_and_version>/$PACKAGE_AND_VERSION/g
965 if ($PACKAGE_AND_VERSION ne '');
966 $BODY =~ s/<from>/$FROM_ARG/g;
967 $BODY =~ s/\n<deadline>/$DEADLINE_ARG/g;
968 $BODY =~ s/<statistics>\n/$statistics/g;
969 $BODY =~ s/<filelist>/$filelist/g;
970 }
971
972 sub OpenEditor
973 {
974 my $editor = shift;
975 my $body = shift;
976 my $opts = "";
977 my ($fh, $tmpnam) = tempfile("podebconf-report-po.mail.tmp.XXXXXX",
978 UNLINK => 0,
979 TMPDIR => 1);
980
981 print $fh $body;
982 close($fh)
983 or die ("Couldn't close $tmpnam: $!\nExiting!\n");
984
985 $opts = "-f" if ($editor eq "vim");
986 system("$editor $opts $tmpnam");
987
988 $body = &ReadFile($tmpnam) if (-f $tmpnam);
989 unlink($tmpnam);
990
991 return $body;
992 }
993
994 sub ParseHeaders
995 {
996 my $body = shift;
997 my %headers = ();
998
999 while ($body =~ s/^#[ \t]*([^\n]*)\n//s) {
1000 my $comment = $1;
1001 if ($comment =~ m/^([a-zA-Z0-9_-]+):\s*([^\n]+)$/) {
1002 $headers{$1} = $2;
1003 }
1004 }
1005 return %headers;
1006 }
1007
1008 sub ParseTo
1009 {
1010 my $body = shift;
1011 my %To = ();
1012
1013 while ($body =~ s/^#[ \t]*([^\n]*)\n//s) {
1014 my $comment = $1;
1015 if ($comment =~ s/^##[ \t]*\[(?:\*|x|X)\][ \t]*([^:]*):[ \t]*([^\n]*)$//s) {
1016 $To{$1} = $2;
1017 }
1018 }
1019 return %To;
1020 }
1021
1022 sub RemoveHeaders
1023 {
1024 my $body = shift;
1025 # First remove comments
1026 1 while $body =~ s/^#[^\n]*\n//s;
1027 # Optional empty lines
1028 $body =~ s/^\s+//s;
1029 return $body;
1030 }
1031
1032 sub DropNonASCII {
1033 my $text = shift;
1034 $text =~ s/[\x80-\xff]/?/g;
1035 return $text;
1036 }
1037
1038 sub ReadFile
1039 {
1040 my $file = shift;
1041 local $/ = undef;
1042 open(FILE, "< $file")
1043 or die ("Couldn't read $file: $!\nExiting!\n");
1044 my $body = <FILE>;
1045 close(FILE)
1046 or die ("Couldn't close $file: $!\nExiting!\n");
1047 return $body;
1048 }
1049
1050 ## Handle invalid arguments
1051 sub Help_InvalidOption
1052 {
1053 print STDERR "Try `${PROGRAM} --help' for more information.\n";
1054 exit 1;
1055 }
1056
1057 ## Print the usage message and exit
1058 sub Help_PrintHelp
1059 {
1060 print <<_EOF_;
1061
1062 Usage: ${PROGRAM} [OPTIONS]
1063 Send outdated debconf PO files to the last translators.
1064
1065 Options:
1066 --addlanguageteam=LANG:LIST
1067 Send a copy of the messages for language LANG also
1068 to the LIST (unless it is already the language team).
1069 --bts=BUGNUMBER specify the Debian bug number to set as reply-to
1070 --call[=LIST] send a call for translations to the LIST (or to
1071 the Debian I18N mailing list by default
1072 --conf
1073 --noconf
1074 --deadline=DEADLINE specify the deadline for receiving the updated
1075 translations
1076 --default don't open the editor and use the template as is
1077 --nodefault
1078 -f, --force send the email without confirmation
1079 --noforce
1080 --from=MAINTAINER specify the name and the email address of the sender
1081 --gzip compress PO files with gzip
1082 --nogzip
1083 --help display this help and exit
1084 --langs=LANGUAGES restrict sending emails only to these languages
1085 --languageteam send the email also to the Language Team
1086 --nolanguageteam
1087 --mutt send the mails with mutt. This set the --postpone
1088 argument to the mutt's postponed parameter or
1089 \$HOME/postponed
1090 --notdebconf this is not for debconf translation. Do not
1091 mention debconf in the subject and message
1092 --package=PACKAGE specify the name of the package
1093 --podir=DIRECTORY specify where are located the PO files
1094 --postpone=MBOX do not send emails, append them in MBOX. This file
1095 can be used as a postponed mailbox with mutt -p.
1096 --potfile=FILE when used with --call, specifies the POT file to
1097 attach to the call for translations
1098 --sendmessage only send a message, without any attachment
1099 --smtp=SERVER specify SMTP server for mailing (default localhost)
1100 --submit send a bug report against the package with a report
1101 of the outdated debconf translations
1102 --summary send a status report to the maintainer with the list
1103 of emails sent to translators
1104 --nosummary
1105 --utf8 send the mail in UTF-8
1106 --noutf8
1107 --version display version information and exit
1108 -v, --verbose display additional information
1109 --noverbose
1110 --templatecall=TEMPLATE
1111 specify file to use it as template for the emails
1112 --templatesubmit=TEMPLATE
1113 specify file to use it as template for the emails
1114 --templatetranslators=TEMPLATE
1115 specify file to use it as template for the emails
1116 --template=TEMPLATE specify file to use it as template for the emails
1117 --notemplate
1118 --withtranslators when used with --call, specifies that request for
1119 translation updates must be sent to the translators
1120 --withouttranslators
1121
1122 _EOF_
1123 exit 0;
1124 }
1125
1126 ## Print the version text and exit
1127 sub Help_PrintVersion
1128 {
1129 print <<_EOF_;
1130 ${PROGRAM} $VERSION
1131 Copyright (C) 2004-2006 Fabio Tranchitella and Denis Barbier.
1132 Copyright (C) 2007 Nicolas François
1133 This is free software; see the source for copying conditions. There is NO
1134 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1135 _EOF_
1136 exit 0;
1137 }
1138
1139 sub Verbose
1140 {
1141 my $msg = shift;
1142 return unless $VERBOSE_ARG;
1143 $msg =~ s/^/**${PROGRAM}: /mg;
1144 print STDERR $msg."\n";
1145 }
1146
1147 sub Postpone {
1148 my $mail = shift;
1149 if (eval { require Mail::Box::Manager }) {
1150 import Mail::Box::Manager;
1151 my $mgr = new Mail::Box::Manager;
1152
1153 my $folder = $mgr->open(folder => $MBOX,
1154 access => 'a',
1155 create => 1);
1156 my $msg = Mail::Message->build
1157 ( From => ${$mail}{'From'}
1158 , To => ${$mail}{'To'}
1159 , Subject => ${$mail}{'Subject'}
1160 , 'X-Mail-Originator' => ${$mail}{'X-Mail-Originator'}
1161 , 'Content-Type' => ${$mail}{'content-type'}
1162 , data => ${$mail}{body}
1163 );
1164 $mgr->appendMessage($folder, $msg);
1165 $folder->write();
1166 $mgr->close();
1167 } else {
1168 die "The --postpone and --mutt options require the ".
1169 "perl Mail::Box package. Please install the Debian ".
1170 "libmail-box-perl package if you want to use these ".
1171 "options. No mail written or sent.";
1172 }
1173 }
1174