]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/dialog/samples/copifuncs/copi.ifman2
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / dialog / samples / copifuncs / copi.ifman2
1 if ( getpwuid($<) ne $ifowner ) { print "You must be owner of ifmail\n"; exit 1; }
2
3 if ( (@ARGV < 3) || $ARGV[0] eq "-?" || $ARGV[0] eq "-h" ) {
4         &usage;
5
6
7 $ARGV[0] =~ tr/A-Z/a-z/;
8 $ARGV[3] =~ tr/A-Z/a-z/;
9
10 &parsecfg;
11
12 if ( $logfile ne "" ) { 
13         open(LOG, ">>".$logfile) || die "Can't open logfile";
14 }
15
16 if (substr($ARGV[1], 0, 1) ne "/") {
17         $cwd=`pwd`;
18         chop $cwd;
19         $ARGV[1] = $cwd."/".$ARGV[1];
20 }
21
22 if ($ARGV[3] eq "" || $ARGV[3] eq "normal") {
23         $flavour = 'f';
24 } elsif ($ARGV[3] eq "crash") {
25         $flavour = 'c';
26 } elsif ($ARGV[3] eq "hold") {
27         $flavour = 'h';
28 } else {
29         print "Unknown flavour, assuming normal\n";
30         $flavour = 'f';
31 }
32
33 if ($ARGV[0] eq "send") {
34         &attach($ARGV[1], $ARGV[2]);
35 } elsif ($ARGV[0] eq "get") {
36         &request($ARGV[1], $ARGV[2]);
37 } else {
38         print "Unknown command, try ifman -h\n";
39         exit 1;
40 }
41
42 close(LOG);
43
44 exit 0;
45
46 #######################################################################
47
48 sub attach {
49         local($fspec, $address) = @_;
50
51         $floname = &resolve($address);
52
53         open(FLO, ">>".$outbound."/".$floname) || die "Can't open flo-file $outbound/$floname";
54         open(FIND, "find $fspec -print |") || die "Can't generate list of files"; 
55
56         if ( eof(FIND) ) {
57                 print "No matching files, nothing to send\n";
58                 exit 1;
59         }
60
61         while (<FIND>) {
62
63                 chop;
64                 $datestamp = `date \"+%D %T\"`;
65                 chop $datestamp;
66                 printf LOG "%s %s %s\n", $datestamp, $$, "ifman: sending $_ to $address";
67                 printf FLO "%s\n", $_;  
68         }
69
70         close(FLO);
71         close(FIND);
72 }
73
74 sub request {
75         local($fspec, $address) = @_;
76
77         $reqname = &resolve($address);
78
79         $reqname =~ s/\.[fch]lo/\.req/;
80         
81         open(REQ, ">>".$outbound."/".$reqname) || die "Can't open req-file";
82
83         $datestamp = `date \"+%D %T\"`;
84         chop $datestamp;
85         printf LOG "%s %s %s\n", $datestamp, $$, "ifman: requesting $fspec from $address";
86         printf REQ "%s\n", $fspec;      
87
88         close(REQ);
89 }
90
91 sub resolve {
92         local($addr) = @_;
93
94         if ( index($addr, ":") >=0 ) {
95                 print "I cannot resolve addresses with zones!\n";
96                 exit 1;
97         } elsif ( index($addr, "/") == -1 ) {
98                 print "Not a valid address!\n";
99                 exit 1;
100         }
101
102         ($net, $node, $point) = split(/\/|\./, $addr); 
103
104         if ( defined $point ) {
105                 $pointdir = sprintf("%04x%04x.pnt", $net, $node);
106                 if ( ! -e $outbound."/".$pointdir ) {
107                         mkdir ($outbound."/".$pointdir, 0755) || die "Can't create point directory";
108                 }
109                 $flo = sprintf("0000%04x.%01slo", $point, $flavour);
110                 return $pointdir."/".$flo;
111         } else {
112                 $flo = sprintf("%04x%04x.%01slo", $net, $node, $flavour);
113                 return $flo;
114         }
115 }
116
117 sub usage {
118         print "ifmail manager script\n";
119         print "usage: ifman <cmd> <filespec> <address> [flavour]\n";
120         print "  commands: send, get\n";
121         print "  flavours: normal, crash, hold. Default is normal.\n";
122         print "Only 2d addresses with points are supported - no zones!\n";
123         exit 1;
124 }
125
126 sub parsecfg {
127         open(CFG, $cfgfile) || die "Can't open ifmail config file";
128
129         while (<CFG>) {
130                 chop;
131                 if (/^#/) { next; }
132                 if (/^outbound\s+(\S+)/) { $outbound = $1; }
133                 if (/^logfile\s+(\S+)/) { $logfile = $1; }
134         }
135         
136         close(CFG);
137 }