2a86e0dee74164210f3db4a538c1cde8ec2fda40
[spider.git] / perl / importwwv.pl
1 #!/usr/bin/perl
2 #
3 # Process and import for mail WWV and Solar Data
4 #
5 # This program takes a mail message on its standard input
6 # and, if it is WWV or Solar info, imports it into the local
7 # spider chat_import queue.
8 #
9 # Both the "tmp" and the "chat_import" directories should be
10 # chmod 1777 
11 #
12 # Copyright (c) 2004 Dirk Koopman G1TLH
13 #
14 # $Id$
15 #
16
17 use strict;
18 use Mail::Internet;
19 use Mail::Header;
20
21 our $root;
22
23 # search local then perl directories
24 BEGIN {
25         # root of directory tree for this system
26         $root = "/spider"; 
27         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
28         
29         unshift @INC, "$root/perl";     # this IS the right way round!
30         unshift @INC, "$root/local";
31 }
32
33 my $import = "$root/chat_import";
34 my $tmp = "$root/tmp";
35
36 my $msg = Mail::Internet->new(\*STDIN) or die "Mail::Internet $!";
37 my $head = $msg->head->header_hashref;
38
39 if ($head) {
40         if ($head->{From}->[0] =~ /wwv/i || $head->{'From '}->[0] =~ /wwv/i) {
41                 process_wwv($msg);
42         } elsif ($head->{From}->[0] =~ /rwc\.boulder/i || $head->{'From '}->[0] =~ /rwc\.boulder/i) {
43                 process_solar($msg);
44         }
45 }
46
47 exit(0);
48
49 sub process_wwv
50 {
51         my $msg = shift;
52         my @out;
53         my $state;
54         
55         foreach (@{$msg->body}) {
56                 next if /^\s*:/;
57                 next if /^\s#/;
58                 next if /^\s*\r?\n$/s;
59                 if (/follow/) {
60                         $state = 1;
61                         next;
62                 }
63                 if ($state) {
64                         my $l = $_;
65                         $l =~ s/\s*\.?\r?\n$//;
66                         push @out, $l;
67                 }
68         }
69         out(@out) if @out;
70 }
71
72 sub process_solar
73 {
74         my $msg = shift;
75         my @out;
76         my $state;
77         
78         foreach (@{$msg->body}) {
79                 if (!$state && /Space\s+Weather\s+Message\s+Code:/i) {
80                         $state = 1;
81                 }
82                 if ($state == 1 && /^[A-Z]+:/) {
83                         $state = 2;
84                 }
85                 if ($state == 2 && /^\s*\r?\n$/s) {
86                         last;
87                 }
88                 if ($state > 1) {
89                         my $l = $_;
90                         $l =~ s/\r?\n$//;
91                         push @out, $l;
92                 }
93         }
94         out(@out) if @out;
95 }
96
97 sub out
98 {
99         my $fn = "solar.txt.$$";
100    
101         open OUT, ">$tmp/$fn" or die "import $tmp/$fn $!";
102         print OUT map { "$_\n" } @_;
103         close OUT;
104
105         # Note we do this this way to make the appearance of
106         # the file in /spider/chat_import atomic. Otherwise there
107         # exists the possiblity of race conditions and other nasties
108         link "$tmp/$fn", "$import/$fn";
109         unlink "$tmp/$fn";
110 }
111