summaryrefslogtreecommitdiff
path: root/Build/source/utils/dialog/dialog-1.1-20100428/samples/copifuncs/copi.ifman2
blob: 0b6dbb77dede17b94952ab66cb1bac78e0896b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
if ( getpwuid($<) ne $ifowner ) { print "You must be owner of ifmail\n"; exit 1; }

if ( (@ARGV < 3) || $ARGV[0] eq "-?" || $ARGV[0] eq "-h" ) {
	&usage;
} 

$ARGV[0] =~ tr/A-Z/a-z/;
$ARGV[3] =~ tr/A-Z/a-z/;

&parsecfg;

if ( $logfile ne "" ) { 
	open(LOG, ">>".$logfile) || die "Can't open logfile";
}

if (substr($ARGV[1], 0, 1) ne "/") {
	$cwd=`pwd`;
	chop $cwd;
	$ARGV[1] = $cwd."/".$ARGV[1];
}

if ($ARGV[3] eq "" || $ARGV[3] eq "normal") {
	$flavour = 'f';
} elsif ($ARGV[3] eq "crash") {
	$flavour = 'c';
} elsif ($ARGV[3] eq "hold") {
	$flavour = 'h';
} else {
	print "Unknown flavour, assuming normal\n";
	$flavour = 'f';
}

if ($ARGV[0] eq "send") {
	&attach($ARGV[1], $ARGV[2]);
} elsif ($ARGV[0] eq "get") {
	&request($ARGV[1], $ARGV[2]);
} else {
	print "Unknown command, try ifman -h\n";
	exit 1;
}

close(LOG);

exit 0;

#######################################################################

sub attach {
	local($fspec, $address) = @_;

	$floname = &resolve($address);

	open(FLO, ">>".$outbound."/".$floname) || die "Can't open flo-file $outbound/$floname";
	open(FIND, "find $fspec -print |") || die "Can't generate list of files"; 

	if ( eof(FIND) ) {
		print "No matching files, nothing to send\n";
		exit 1;
	}

	while (<FIND>) {

		chop;
		$datestamp = `date \"+%D %T\"`;
		chop $datestamp;
		printf LOG "%s %s %s\n", $datestamp, $$, "ifman: sending $_ to $address";
		printf FLO "%s\n", $_;	
	}

	close(FLO);
	close(FIND);
}

sub request {
	local($fspec, $address) = @_;

	$reqname = &resolve($address);

	$reqname =~ s/\.[fch]lo/\.req/;
	
	open(REQ, ">>".$outbound."/".$reqname) || die "Can't open req-file";

	$datestamp = `date \"+%D %T\"`;
	chop $datestamp;
	printf LOG "%s %s %s\n", $datestamp, $$, "ifman: requesting $fspec from $address";
	printf REQ "%s\n", $fspec;	

	close(REQ);
}

sub resolve {
	local($addr) = @_;

	if ( index($addr, ":") >=0 ) {
		print "I cannot resolve addresses with zones!\n";
		exit 1;
	} elsif ( index($addr, "/") == -1 ) {
		print "Not a valid address!\n";
		exit 1;
	}

	($net, $node, $point) = split(/\/|\./, $addr); 

	if ( defined $point ) {
		$pointdir = sprintf("%04x%04x.pnt", $net, $node);
		if ( ! -e $outbound."/".$pointdir ) {
			mkdir ($outbound."/".$pointdir, 0755) || die "Can't create point directory";
		}
		$flo = sprintf("0000%04x.%01slo", $point, $flavour);
		return $pointdir."/".$flo;
	} else {
		$flo = sprintf("%04x%04x.%01slo", $net, $node, $flavour);
		return $flo;
	}
}

sub usage {
	print "ifmail manager script\n";
	print "usage: ifman <cmd> <filespec> <address> [flavour]\n";
	print "  commands: send, get\n";
	print "  flavours: normal, crash, hold. Default is normal.\n";
	print "Only 2d addresses with points are supported - no zones!\n";
	exit 1;
}

sub parsecfg {
	open(CFG, $cfgfile) || die "Can't open ifmail config file";

	while (<CFG>) {
		chop;
		if (/^#/) { next; }
		if (/^outbound\s+(\S+)/) { $outbound = $1; }
		if (/^logfile\s+(\S+)/) { $logfile = $1; }
	}
	
	close(CFG);
}