Mercurial > emacs
annotate lib-src/b2m.pl @ 92998:98176f05203c
*** empty log message ***
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Sun, 16 Mar 2008 01:53:42 +0000 |
parents | 5714ff101fd9 |
children | 606f2d163a64 3a4bc081639c |
rev | line source |
---|---|
46032 | 1 #!/usr/bin/perl |
2 | |
3 # b2m.pl - Script to convert a Babyl file to an mbox file | |
4 | |
79748 | 5 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
75458
9f287ea4800c
Add missing Copyright header. Years from date of installation in
Glenn Morris <rgm@gnu.org>
parents:
64083
diff
changeset
|
6 # Free Software Foundation, Inc. |
9f287ea4800c
Add missing Copyright header. Years from date of installation in
Glenn Morris <rgm@gnu.org>
parents:
64083
diff
changeset
|
7 |
46032 | 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 | |
78257
1f2482de3237
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75458
diff
changeset
|
10 # the Free Software Foundation; either version 3, or (at your option) |
1f2482de3237
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75458
diff
changeset
|
11 # any later version. |
46032 | 12 |
13 # This program is distributed in the hope that it will be useful, but | |
14 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # General Public License for more details. | |
17 | |
18 # You should have received a copy of the GNU General Public License | |
78257
1f2482de3237
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75458
diff
changeset
|
19 # along with this program; see the file COPYING. If not, write to the |
1f2482de3237
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75458
diff
changeset
|
20 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
1f2482de3237
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75458
diff
changeset
|
21 # Boston, MA 02110-1301 USA. |
46032 | 22 |
23 # Maintained by Jonathan Kamens <jik@kamens.brookline.ma.us>. | |
24 | |
25 # Requires CPAN modules: MailTools (for Mail::Address), TimeDate (for | |
26 # Date::Parse). | |
27 | |
28 use warnings; | |
29 use strict; | |
30 use File::Basename; | |
31 use Getopt::Long; | |
32 use Mail::Address; | |
33 use Date::Parse; | |
34 | |
35 my($whoami) = basename $0; | |
64083 | 36 my($version) = '$Revision$'; |
46032 | 37 my($usage) = "Usage: $whoami [--help] [--version] [--[no]full-headers] [Babyl-file] |
38 \tBy default, full headers are printed.\n"; | |
39 | |
40 my($opt_help, $opt_version); | |
41 my($opt_full_headers) = 1; | |
42 | |
43 die $usage if (! GetOptions( | |
44 'help' => \$opt_help, | |
45 'version' => \$opt_version, | |
46 'full-headers!' => \$opt_full_headers, | |
47 )); | |
48 | |
49 if ($opt_help) { | |
50 print $usage; | |
51 exit; | |
52 } | |
53 elsif ($opt_version) { | |
54 print "$whoami version: $version\n"; | |
55 exit; | |
56 } | |
57 | |
58 die $usage if (@ARGV > 1); | |
59 | |
60 $/ = "\n\037"; | |
61 | |
62 if (<> !~ /^BABYL OPTIONS:/) { | |
63 die "$whoami: $ARGV is not a Babyl file\n$usage"; | |
64 } | |
65 | |
66 while (<>) { | |
67 my($msg_num) = $. - 1; | |
46184
81235cad75cb
Obey the rmail file and use the unpruned header properly.
Pavel Janík <Pavel@Janik.cz>
parents:
46032
diff
changeset
|
68 my($labels, $pruned, $full_header, $header); |
46032 | 69 my($from_line, $from_addr); |
70 my($time); | |
71 | |
72 # This will strip the initial form feed, any whitespace that may | |
73 # be following it, and then a newline | |
74 s/^\s+//; | |
75 # This will strip the ^_ off of the end of the message | |
76 s/\037$//; | |
77 | |
78 if (! s/(.*)\n//) { | |
79 malformatted: | |
80 warn "$whoami: message $msg_num in $ARGV is malformatted\n"; | |
81 next; | |
82 } | |
83 $labels = $1; | |
84 | |
46184
81235cad75cb
Obey the rmail file and use the unpruned header properly.
Pavel Janík <Pavel@Janik.cz>
parents:
46032
diff
changeset
|
85 # Strip the integer indicating whether the header is pruned |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48810
diff
changeset
|
86 $labels =~ s/^(\d+)[,\s]*//; |
46184
81235cad75cb
Obey the rmail file and use the unpruned header properly.
Pavel Janík <Pavel@Janik.cz>
parents:
46032
diff
changeset
|
87 $pruned = $1; |
81235cad75cb
Obey the rmail file and use the unpruned header properly.
Pavel Janík <Pavel@Janik.cz>
parents:
46032
diff
changeset
|
88 |
81235cad75cb
Obey the rmail file and use the unpruned header properly.
Pavel Janík <Pavel@Janik.cz>
parents:
46032
diff
changeset
|
89 s/(?:((?:.+\n)+)\n*)?\*\*\* EOOH \*\*\*\n+// || goto malformatted; |
46032 | 90 $full_header = $1; |
91 | |
92 if (s/((?:.+\n)+)\n+//) { | |
93 $header = $1; | |
94 } | |
95 else { | |
96 # Message has no body | |
97 $header = $_; | |
98 $_ = ''; | |
99 } | |
100 | |
46184
81235cad75cb
Obey the rmail file and use the unpruned header properly.
Pavel Janík <Pavel@Janik.cz>
parents:
46032
diff
changeset
|
101 # "$pruned eq '0'" is different from "! $pruned". We want to make |
81235cad75cb
Obey the rmail file and use the unpruned header properly.
Pavel Janík <Pavel@Janik.cz>
parents:
46032
diff
changeset
|
102 # sure that we found a valid label line which explicitly indicated |
81235cad75cb
Obey the rmail file and use the unpruned header properly.
Pavel Janík <Pavel@Janik.cz>
parents:
46032
diff
changeset
|
103 # that the header was not pruned. |
81235cad75cb
Obey the rmail file and use the unpruned header properly.
Pavel Janík <Pavel@Janik.cz>
parents:
46032
diff
changeset
|
104 if ((! $full_header) || ($pruned eq '0')) { |
46032 | 105 $full_header = $header; |
106 } | |
107 | |
48810
1626973bdb2b
Make sure every message ends with a blank line, because some mbox parsers
Pavel Janík <Pavel@Janik.cz>
parents:
46737
diff
changeset
|
108 # End message with two newlines (some mbox parsers require a blank |
1626973bdb2b
Make sure every message ends with a blank line, because some mbox parsers
Pavel Janík <Pavel@Janik.cz>
parents:
46737
diff
changeset
|
109 # line before the next "From " line). |
1626973bdb2b
Make sure every message ends with a blank line, because some mbox parsers
Pavel Janík <Pavel@Janik.cz>
parents:
46737
diff
changeset
|
110 s/\s+$/\n\n/; |
46032 | 111 |
112 # Quote "^From " | |
113 s/(^|\n)From /$1>From /g; | |
114 | |
115 # Strip extra commas and whitespace from the end | |
116 $labels =~ s/[,\s]+$//; | |
117 # Now collapse extra commas and whitespace in the remaining label string | |
118 $labels =~ s/[,\s]+/, /g; | |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48810
diff
changeset
|
119 |
46032 | 120 foreach my $rmail_header qw(summary-line x-coding-system) { |
121 $full_header =~ s/(^|\n)$rmail_header:.*\n/$1/i; | |
122 } | |
123 | |
124 if ($full_header =~ s/(^|\n)mail-from:\s*(From .*)\n/$1/i) { | |
125 ($from_line = $2) =~ s/\s*$/\n/; | |
126 } | |
127 else { | |
128 foreach my $addr_header qw(return-path from really-from sender) { | |
46737
7c794ace9e1a
Fix regexp for finding return address fields.
Pavel Janík <Pavel@Janik.cz>
parents:
46184
diff
changeset
|
129 if ($full_header =~ /(?:^|\n)$addr_header:\s*(.*\n(?:\B.*\n)*)/i) { |
46032 | 130 my($addr) = Mail::Address->parse($1); |
131 $from_addr = $addr->address($addr); | |
132 last; | |
133 } | |
134 } | |
135 | |
136 if (! $from_addr) { | |
137 $from_addr = "Babyl_to_mail_by_$whoami\@localhost"; | |
138 } | |
139 | |
140 if ($full_header =~ /(?:^|\n)date:\s*(\S.*\S)/i) { | |
141 $time = str2time($1); | |
142 } | |
143 | |
144 if (! $time) { | |
145 # No Date header or we failed to parse it | |
146 $time = time; | |
147 } | |
148 | |
149 $from_line = "From " . $from_addr . " " . localtime($time) . "\n"; | |
150 } | |
151 | |
152 print($from_line, ($opt_full_headers ? $full_header : $header), | |
153 ($labels ? "X-Babyl-Labels: $labels\n" : ""), "\n", | |
154 $_) || die "$whoami: error writing to stdout: $!\n"; | |
155 } | |
156 | |
157 close(STDOUT) || die "$whoami: Error closing stdout: $!\n"; | |
52401 | 158 |
159 # arch-tag: 8c7c8ab0-721c-46d7-ba3e-139801240aa8 |