comparison lib-src/b2m.pl @ 46032:8440ad4c6055

New file.
author Pavel Janík <Pavel@Janik.cz>
date Wed, 26 Jun 2002 15:49:23 +0000
parents
children 81235cad75cb
comparison
equal deleted inserted replaced
46031:48f0d713026b 46032:8440ad4c6055
1 #!/usr/bin/perl
2
3 # b2m.pl - Script to convert a Babyl file to an mbox file
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 # USA.
19
20 # Maintained by Jonathan Kamens <jik@kamens.brookline.ma.us>.
21
22 # Requires CPAN modules: MailTools (for Mail::Address), TimeDate (for
23 # Date::Parse).
24
25 use warnings;
26 use strict;
27 use File::Basename;
28 use Getopt::Long;
29 use Mail::Address;
30 use Date::Parse;
31
32 my($whoami) = basename $0;
33 my($version) = '$Revision: 1.4 $';
34 my($usage) = "Usage: $whoami [--help] [--version] [--[no]full-headers] [Babyl-file]
35 \tBy default, full headers are printed.\n";
36
37 my($opt_help, $opt_version);
38 my($opt_full_headers) = 1;
39
40 die $usage if (! GetOptions(
41 'help' => \$opt_help,
42 'version' => \$opt_version,
43 'full-headers!' => \$opt_full_headers,
44 ));
45
46 if ($opt_help) {
47 print $usage;
48 exit;
49 }
50 elsif ($opt_version) {
51 print "$whoami version: $version\n";
52 exit;
53 }
54
55 die $usage if (@ARGV > 1);
56
57 $/ = "\n\037";
58
59 if (<> !~ /^BABYL OPTIONS:/) {
60 die "$whoami: $ARGV is not a Babyl file\n$usage";
61 }
62
63 while (<>) {
64 my($msg_num) = $. - 1;
65 my($labels, $full_header, $header);
66 my($from_line, $from_addr);
67 my($time);
68
69 # This will strip the initial form feed, any whitespace that may
70 # be following it, and then a newline
71 s/^\s+//;
72 # This will strip the ^_ off of the end of the message
73 s/\037$//;
74
75 if (! s/(.*)\n//) {
76 malformatted:
77 warn "$whoami: message $msg_num in $ARGV is malformatted\n";
78 next;
79 }
80 $labels = $1;
81
82 s/(?:((?:.+\n)+)\n+)?\*\*\* EOOH \*\*\*\n+// || goto malformatted;
83 $full_header = $1;
84
85 if (s/((?:.+\n)+)\n+//) {
86 $header = $1;
87 }
88 else {
89 # Message has no body
90 $header = $_;
91 $_ = '';
92 }
93
94 if (! $full_header) {
95 $full_header = $header;
96 }
97
98 # End message with a single newline
99 s/\s+$/\n/;
100
101 # Quote "^From "
102 s/(^|\n)From /$1>From /g;
103
104 # Strip the integer indicating whether the header is pruned
105 $labels =~ s/^\d+[,\s]*//;
106 # Strip extra commas and whitespace from the end
107 $labels =~ s/[,\s]+$//;
108 # Now collapse extra commas and whitespace in the remaining label string
109 $labels =~ s/[,\s]+/, /g;
110
111 foreach my $rmail_header qw(summary-line x-coding-system) {
112 $full_header =~ s/(^|\n)$rmail_header:.*\n/$1/i;
113 }
114
115 if ($full_header =~ s/(^|\n)mail-from:\s*(From .*)\n/$1/i) {
116 ($from_line = $2) =~ s/\s*$/\n/;
117 }
118 else {
119 foreach my $addr_header qw(return-path from really-from sender) {
120 if ($full_header =~ /(?:^|\n)$addr_header:\s*((?:\S.*\n)+)/i) {
121 my($addr) = Mail::Address->parse($1);
122 $from_addr = $addr->address($addr);
123 last;
124 }
125 }
126
127 if (! $from_addr) {
128 $from_addr = "Babyl_to_mail_by_$whoami\@localhost";
129 }
130
131 if ($full_header =~ /(?:^|\n)date:\s*(\S.*\S)/i) {
132 $time = str2time($1);
133 }
134
135 if (! $time) {
136 # No Date header or we failed to parse it
137 $time = time;
138 }
139
140 $from_line = "From " . $from_addr . " " . localtime($time) . "\n";
141 }
142
143 print($from_line, ($opt_full_headers ? $full_header : $header),
144 ($labels ? "X-Babyl-Labels: $labels\n" : ""), "\n",
145 $_) || die "$whoami: error writing to stdout: $!\n";
146 }
147
148 close(STDOUT) || die "$whoami: Error closing stdout: $!\n";