std sh ga-infounused
[sunny256-utils.git] / extract_mail
bloba6a225435a2959ba298f7f7cddda92a1c75143b0
1 #!/usr/bin/env perl
3 #=======================================================================
4 # extract_mail
5 # File ID: 953fa428-5d39-11df-8ca0-90e6ba3022ac
6 # Extracts messages from a mailbox into separate files
7 # ©opyleft 2002 Øyvind A. Holm <sunny@sunbase.org>
8 # License: GNU General Public License version 2 or later.
9 #=======================================================================
11 use strict;
12 use warnings;
13 use Time::Local;
15 my @Msg = ();
16 my $Date = "";
17 my $date_done = 0;
18 my $dest_dir = "out";
19 my $TZ_ext = "Z";
20 my %mon_str = (
21 "jan" => "01",
22 "feb" => "02",
23 "mar" => "03",
24 "apr" => "04",
25 "may" => "05",
26 "jun" => "06",
27 "jul" => "07",
28 "aug" => "08",
29 "sep" => "09",
30 "oct" => "10",
31 "nov" => "11",
32 "dec" => "12"
35 -d $dest_dir || mkdir($dest_dir, 0777) || die("mkdir($dest_dir): $!");
37 while (<>) {
38 if (/^From /) {
39 D("========= $_");
40 if (scalar(@Msg)) {
41 if (length($Date)) {
42 ($Date =~ /^\d{8}T\d{6}$/) || die("Line $.: Invalid date format: \"$Date\"");
43 my $file_name = "$dest_dir/mail.${Date}$TZ_ext.txt";
44 -e $file_name && (warn("$file_name: File already exists"), $Date .= "b");
45 open(ToFP, ">$file_name") || die("$file_name: Opening for write: $!");
46 for (@Msg) {
47 print(ToFP $_);
49 close(ToFP);
50 } else {
51 warn("Datoen var tom");
54 ($Date, $date_done, $TZ_ext) = ("", 0, "");
55 @Msg = ();
57 push(@Msg, $_);
58 if (/^Date: / && !$date_done) {
59 if (
60 /^\D* # Before the date
61 (\d+) # Day
62 \s+
63 (\S+) # Month
64 \s+
65 (\d+) # Year
66 \s+
67 (\d+) # Hours
69 (\d+) # Minutes
71 (\d+) # Seconds
72 \s*
73 (\S*) # Time zone
75 ) {
76 my ($Year, $Mon, $Day, $Hour, $Min, $Sec, $Timezone) = ($3, $2, $1, $4, $5, $6, "");
77 (defined($7) && ($Timezone = $7)) || warn("Line $.: Date missing time zone");
78 $Mon = lc($Mon);
79 D("Day = \"$Day\", Mon = \"$Mon\"\n");
80 defined($mon_str{$Mon}) || (die("Unknown month: \"$Mon\""), ($Mon = "XX"));
81 $Mon = $mon_str{$Mon};
82 my $curr_date = timelocal($Sec, $Min, $Hour, $Day, $Mon-1, $Year);
83 my $zone_diff = 0;
84 if ($Timezone =~ /^(\+|-)(\d\d)(\d\d)$/) {
85 $zone_diff = $2*3600 + $3*60;
86 ($1 eq "+") && ($zone_diff = 0-$zone_diff);
87 } elsif ($Timezone =~ /^GMT$/i) {
88 $zone_diff = 0;
89 } else {
90 warn("Line $.: Unknown timezone format");
91 $TZ_ext = "";
93 D("curr_date before tz: \"$curr_date\", Timezone = \"$Timezone\"\n");
94 $zone_diff = 0; # Temporary until the timezone problems are eliminated.
95 $curr_date += $zone_diff;
96 D("curr_date after tz : \"$curr_date\", Timezone = \"$Timezone\"\n");
97 D("zone_diff = \"$zone_diff\"\n");
98 ($Sec, $Min, $Hour, $Day, $Mon, $Year) = gmtime($curr_date);
99 $Year += 1900; # Urgh
100 $Mon = sprintf("%02u", $Mon+1); # Urgh II
101 $Day = sprintf("%02u", $Day);
102 $Hour = sprintf("%02u", $Hour);
103 $Min = sprintf("%02u", $Min);
104 $Sec = sprintf("%02u", $Sec);
105 $Date = "$Year$Mon${Day}T$Hour$Min$Sec";
106 $date_done = 1;
107 } else {
108 warn("Line $.: Weird date: \"$Date\"");
113 sub D {
114 print(STDERR @_);
117 =pod
119 =head1 LICENCE
121 This program is free software; you can redistribute it and/or modify it
122 under the terms of the GNU General Public License as published by the
123 Free Software Foundation; either version 2 of the License, or (at your
124 option) any later version.
126 This program is distributed in the hope that it will be useful, but
127 WITHOUT ANY WARRANTY; without even the implied warranty of
128 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
129 See the GNU General Public License for more details.
131 You should have received a copy of the GNU General Public License along
132 with this program; if not, write to the Free Software Foundation, Inc.,
133 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
135 =cut
137 #### End of file extract_mail ####