PageRenderTime 127ms CodeModel.GetById 123ms app.highlight 0ms RepoModel.GetById 1ms app.codeStats 1ms

/contrib/dialog/samples/copifuncs/copi.ifman2

https://bitbucket.org/freebsd/freebsd-head/
Unknown | 137 lines | 106 code | 31 blank | 0 comment | 0 complexity | d9d03b168795130823020af19cf40173 MD5 | raw file
  1if ( getpwuid($<) ne $ifowner ) { print "You must be owner of ifmail\n"; exit 1; }
  2
  3if ( (@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
 12if ( $logfile ne "" ) { 
 13	open(LOG, ">>".$logfile) || die "Can't open logfile";
 14}
 15
 16if (substr($ARGV[1], 0, 1) ne "/") {
 17	$cwd=`pwd`;
 18	chop $cwd;
 19	$ARGV[1] = $cwd."/".$ARGV[1];
 20}
 21
 22if ($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
 33if ($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
 42close(LOG);
 43
 44exit 0;
 45
 46#######################################################################
 47
 48sub 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
 74sub 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
 91sub 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
117sub 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
126sub 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}