/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

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