home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / LharcUUCP1_0.lha / LharcUUCP / source / LharcUUCP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  12.4 KB  |  372 lines

  1. /*
  2.  *      $Filename: LharcUUCP.c $
  3.  *      $Revision: 1.0 $
  4.  *      $Date: 1994/04/02 18:04:38 $
  5.  *
  6.  *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
  7.  *
  8.  *      This program is free software; you can redistribute it and/or
  9.  *      modify it under the terms of the GNU General Public License as
  10.  *      published by the Free Software Foundation; either version 2 of
  11.  *      the License, or (at your option) any later version.
  12.  *
  13.  *      This program is distributed in the hope that it will be useful,
  14.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *      General Public License for more details.
  17.  *
  18.  *      You should have received a copy of the GNU General Public License
  19.  *      along with this program; if not, write to the Free Software
  20.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *      $Id: LharcUUCP.c,v 1.0 1994/04/02 18:04:38 simons Rel simons $
  23.  *
  24.  */
  25.  
  26.  
  27. /************************************* includes ***********/
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <dos.h>
  33. #include <libraries/netsupport.h>
  34. #include <proto/netsupport.h>
  35.  
  36. #include "protos.h"
  37. #include "LharcUUCP_rev.h"
  38.  
  39. /************************************* defines ************/
  40. #define BUFFERSIZE 128
  41. #define LHARC "LhA m"           /* archiver command, moving enabled */
  42. #define LHARCPOSTFIX ".lha"
  43. #define LHARCUUCPMANAGER "postmaster"
  44. #define LHARCUUCPDAEMON "lharcuucp-daemon"
  45.  
  46. #define USAGE VERS " -- written by Peter Simons <simons@peti.GUN.de>\n" \
  47.               "Usage: LharcUUCP system [[system] [...]]\n"
  48.  
  49. /************************************* global variables ***/
  50. static const char __RCSId[] = "$Id: LharcUUCP.c,v 1.0 1994/04/02 18:04:38 simons Rel simons $";
  51. static const char __DOSVer[] = VERSTAG;
  52. char *homesystem, *spooldir;
  53. int processed_flag, break_detected = 0;
  54.  
  55. /************************************* program ************/
  56. int main(int argc, char **argv)
  57. {
  58.         char currentdir[512];
  59.  
  60.         if (argc < 2) {
  61.                 fprintf(stderr, USAGE);
  62.                 return 10;
  63.         }
  64.  
  65.         if (atexit(FreeSystem))
  66.                 return 20;
  67.  
  68.         if (!InitSystem())
  69.                 return 20;
  70.  
  71.         if (!(homesystem = getenv("HOSTNAME")))
  72.                 if (!(homesystem = GetConfigEntry(NULL, "NodeName", NULL))) {
  73.                         fprintf(stderr, "LharcUUCP: Can't determine the name of this machine!\n");
  74.                         return 10;
  75.                 }
  76.         homesystem = strdup(homesystem);
  77.         spooldir = strdup(GetConfig(NULL, UUSPOOL, NULL, "UUSPOOL:"));
  78.  
  79.         if (getcd(0, currentdir))
  80.                 return 10;
  81.  
  82.         while (--argc && !break_detected)
  83.                 PackSpoolJobs(argv[argc]);
  84.  
  85.         chdir(currentdir);
  86.         if (break_detected)
  87.                 fprintf(stderr, "*** Break has been detected!\n");
  88.  
  89.         return 0;
  90. }
  91.  
  92.  
  93. /************************************* subroutines ********/
  94. void PostArchive(char *systemname, char *postfix, char grade)
  95. {
  96.         FILE *cfh, *xfh;
  97.         char cfilename[64], xfilename[64], *trunc_sysname;
  98.         char dremote[64];
  99.  
  100.         trunc_sysname = TruncName(systemname);
  101.  
  102.         sprintf(cfilename, "C.%s%c0000", trunc_sysname, grade);
  103.         LockFile(cfilename);
  104.         if (!(cfh = fopen(cfilename, "r"))) {
  105.                 fclose(cfh);
  106.  
  107.                 /*
  108.                  * When the fopen() call succedes, does the file already
  109.                  * exist and we're finished.
  110.                  */
  111.  
  112.                 sprintf(xfilename, "D.%sX%s", trunc_sysname, SeqToName(GetSequence(1)));
  113.                 sprintf(dremote, "%s%s", TruncName(homesystem), SeqToName(GetSequence(1)));
  114.  
  115.                 if (cfh = fopen(cfilename, "w")) {
  116.                         fprintf(cfh, "S %s%s D.%s " LHARCUUCPDAEMON " - %s%s 0666\n",
  117.                                 systemname, postfix, dremote, systemname, postfix);
  118.                         fprintf(cfh, "S %s X.%s%s " LHARCUUCPDAEMON " - %s 0666\n",
  119.                                 xfilename, TruncName(homesystem), SeqToName(GetSequence(1)), xfilename);
  120.                         fclose(cfh);
  121.                 }
  122.  
  123.                 LockFile(xfilename);
  124.                 if (xfh = fopen(xfilename, "w")) {
  125.                         fprintf(xfh, "U " LHARCUUCPMANAGER " %s\nF D.%s\nC rlharc\n", homesystem, dremote);
  126.                         fclose(xfh);
  127.                 }
  128.                 else
  129.                         remove(cfilename);
  130.                 UnLockFile(xfilename);
  131.         }
  132.         else
  133.                 fclose(cfh);
  134.  
  135.         UnLockFile(cfilename);
  136. }
  137.  
  138.  
  139. void PackSpoolJobs(char *systemname)
  140. {
  141.         char currentdir[BUFFERSIZE], *cfile, *proclock;
  142.         int success;
  143.         char owngrade, grade;
  144.  
  145.         processed_flag = 0;
  146.  
  147.         if (strlen(systemname)+strlen(spooldir)+1 > BUFFERSIZE) {
  148.                 fprintf(stderr, "LharcUUCP: Directoryname is too long for internal buffer.\n");
  149.                 return;         /* Buffer overflow! */
  150.         }
  151.  
  152.  
  153.         /*
  154.          * Make sure we're the only LharcUUCP for this system.
  155.          */
  156.  
  157.         if (!(proclock = malloc(strlen("LharcUUCP") + strlen(systemname) + 5)))
  158.                 return;
  159.         sprintf(proclock, "%s_%s", "LharcUUCP", systemname);
  160.         LockFile(proclock);
  161.         printf("%s: Processing queued jobs for >%s<.\n", "LharcUUCP", systemname);
  162.  
  163.  
  164.         /*
  165.          * Change into the appropiate directory, holding the spool jobs
  166.          * for the specified system.
  167.          */
  168.  
  169.         sprintf(currentdir, "%s%s", spooldir, systemname);
  170.         if (chdir(currentdir)) {
  171.                 sprintf(currentdir, "%s%s", spooldir, TruncName(systemname));
  172.                 if (chdir(currentdir)) {
  173.                         fprintf(stderr, "LharcUUCP: System >%s< has no spool directory?\n", systemname);
  174.                         strcpy(currentdir, spooldir);
  175.                         if (chdir(currentdir)) {
  176.                                 fprintf(stderr, "LharcUUCP: Cd to >%s< failed.\n", spooldir);
  177.                                 UnLockFile(proclock);
  178.                                 free(proclock);
  179.                                 return;
  180.                         }
  181.                 }
  182.         }
  183.  
  184.  
  185.         /*
  186.          * Determine the configured grades for our process.
  187.          */
  188.  
  189.         {
  190.                 char *string, buf[80];
  191.  
  192.                 string = GetConfig(NULL, "BatchOnlyGrade", NULL, NULL);
  193.                 grade = (string && isalnum(string[0])) ? string[0] : 'z';
  194.  
  195.                 sprintf(buf, "%s_%s", "BatchOnlyGrade", systemname);
  196.                 string = GetConfig(NULL, buf, NULL, NULL);
  197.                 grade = (string && isalnum(string[0])) ? string[0] : grade;
  198.  
  199.                 string = GetConfig(NULL, "LharcBatchGrade", NULL, NULL);
  200.                 owngrade = (string && isalnum(string[0])) ? string[0] : 'A';
  201.         }
  202.  
  203.  
  204.  
  205.         /*
  206.          * Build a pattern matching 'C.system*', with the systemname
  207.          * truncated to 7 characters and scan the spooldirectory for
  208.          * queued jobs we can pack.
  209.          */
  210.  
  211.         {
  212.                 char pattern[64];
  213.                 sprintf(pattern, "C.%s#?", TruncName(systemname));
  214.                 success = InitDirScan(pattern);
  215.         }
  216.  
  217.         if (success)
  218.                 while ((cfile = ScanNextFile()) && !break_detected)
  219.                         LharcJob(systemname, cfile, grade);
  220.         else
  221.                 fprintf(stderr, "%s: Directory scan for >%s< failed!\n", "LharcUUCP", currentdir);
  222.  
  223.         EndDirScan();
  224.  
  225.         if (processed_flag)
  226.                 PostArchive(systemname, LHARCPOSTFIX, owngrade);
  227.  
  228.         UnLockFile(proclock);
  229.         free(proclock);
  230. }
  231.  
  232.  
  233. void LharcJob(char *systemname, char *cfilename, char grade)
  234. {
  235.         FILE *cfilehandle;
  236.         char command[2], from[256], to[256], from2[256], to2[256];
  237.         char cmdbuf[256], archivename[64];
  238.         int success = 0;
  239.  
  240.         printf("LharcUUCP: processing <%s>\n", cfilename);
  241.  
  242.         if (IsFileLocked(cfilename))
  243.                 return;         /* uucico is processing this job right now,
  244.                                  * so we better leave it alone.
  245.                                  */
  246.         LockFile(cfilename);
  247.  
  248.         if(!IsValidJob(cfilename, systemname, grade)) {
  249.                 UnLockFile(cfilename);
  250.                 return;
  251.         }
  252.  
  253.         if (cfilehandle = fopen(cfilename, "r")) {
  254.                 fscanf(cfilehandle, "%s%s%s", command, from, to);
  255.                 if (GoNextLine(cfilehandle) != EOF) {
  256.                         fscanf(cfilehandle, "%s%s%s", command, from2, to2);
  257.  
  258.                         LockFile(from);
  259.                         LockFile(to);
  260.                         LockFile(from2);
  261.                         LockFile(to2);
  262.                         if (!(rename(from, to)))
  263.                                 if (!(rename(from2, to2))) {
  264.                                         success = 1;
  265.                                         processed_flag = 1;
  266.  
  267.  
  268.                                         /* Free unused files a soon as
  269.                                          * possible.
  270.                                          */
  271.  
  272.                                         fclose(cfilehandle);
  273.                                         remove(cfilename);
  274.                                         UnLockFile(from);
  275.                                         UnLockFile(from2);
  276.                                         UnLockFile(cfilename);
  277.  
  278.  
  279.                                         /* Batch the processed files.
  280.                                          */
  281.  
  282.                                         sprintf(cmdbuf, "%s %s %s %s", LHARC, systemname, to, to2);
  283.                                         printf("%s\n", cmdbuf);
  284.                                         sprintf(archivename, "%s%s", systemname, LHARCPOSTFIX);
  285.                                         LockFile(archivename);
  286.                                         system(cmdbuf);
  287.                                         UnLockFile(archivename);
  288.                                 }
  289.                                 else
  290.                                         rename(to, from);
  291.  
  292.  
  293.                         /* When one of the 'renames' failed, we still
  294.                          * have to close and unlock the files.
  295.                          */
  296.  
  297.                         if (!success) {
  298.                                 UnLockFile(from);
  299.                                 UnLockFile(from2);
  300.                                 fclose(cfilehandle);
  301.                                 UnLockFile(cfilename);
  302.                         }
  303.  
  304.                         UnLockFile(to);         /* These must be freed */
  305.                         UnLockFile(to2);        /* in any case.        */
  306.                 }
  307.         }
  308. }
  309.  
  310.  
  311.  /*
  312.   * IsValidJob() checks the provided C.* file and tests if this job may
  313.   * may be processed by LharcUUCP. Currently, we only process mail and
  314.   * news. UUCP- and lharc-jobs are left alone.
  315.   */
  316.  
  317. int IsValidJob(char *cfilename, char *systemname, char grade)
  318. {
  319.  
  320.         /*
  321.          * Test if the grade of the commandfile is lower or the same as
  322.          * our max. grade.
  323.          */
  324.  
  325.         if (cfilename[strlen(TruncName(systemname)) + 2] > grade)
  326.                 return 0;
  327.  
  328.  
  329.         /*
  330.          * Check the number part of the filename. C.*0000 are the C.*
  331.          * files for the processed archives and won't be touched.
  332.          */
  333.  
  334.         if (!strncmp(&cfilename[strlen(cfilename) - 4], "0000", 4))
  335.                 return 0;
  336.  
  337.  
  338.         /*
  339.          * Check the contents of the file. Neither target nor source file
  340.          * may contain paths and we can't batch read-requests of course.
  341.          */
  342.  
  343.         {
  344.                 FILE *fh;
  345.                 int success = 0;
  346.                 char from[256], to[256];
  347.  
  348.                 if (fh = fopen(cfilename, "r")) {
  349.                         while (1) {
  350.                                 if (fgetc(fh) == 'S') {
  351.                                         fscanf(fh, "%s%s", from, to);
  352.                                         if (IsPath(from) || IsPath(to))
  353.                                                 break;
  354.                                         GoNextLine(fh);
  355.                                         if (feof(fh)) {
  356.                                                 success = 1;
  357.                                                 break;
  358.                                         }
  359.                                 }
  360.                                 else
  361.                                         break;
  362.                         }
  363.                         fclose(fh);
  364.                 }
  365.                 if (!success)
  366.                         return 0;
  367.         }
  368.  
  369.         return 1;
  370. }
  371.  
  372.