home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Filename: LharcUUCP.c $
- * $Revision: 1.0 $
- * $Date: 1994/04/02 18:04:38 $
- *
- * Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id: LharcUUCP.c,v 1.0 1994/04/02 18:04:38 simons Rel simons $
- *
- */
-
-
- /************************************* includes ***********/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <dos.h>
- #include <libraries/netsupport.h>
- #include <proto/netsupport.h>
-
- #include "protos.h"
- #include "LharcUUCP_rev.h"
-
- /************************************* defines ************/
- #define BUFFERSIZE 128
- #define LHARC "LhA m" /* archiver command, moving enabled */
- #define LHARCPOSTFIX ".lha"
- #define LHARCUUCPMANAGER "postmaster"
- #define LHARCUUCPDAEMON "lharcuucp-daemon"
-
- #define USAGE VERS " -- written by Peter Simons <simons@peti.GUN.de>\n" \
- "Usage: LharcUUCP system [[system] [...]]\n"
-
- /************************************* global variables ***/
- static const char __RCSId[] = "$Id: LharcUUCP.c,v 1.0 1994/04/02 18:04:38 simons Rel simons $";
- static const char __DOSVer[] = VERSTAG;
- char *homesystem, *spooldir;
- int processed_flag, break_detected = 0;
-
- /************************************* program ************/
- int main(int argc, char **argv)
- {
- char currentdir[512];
-
- if (argc < 2) {
- fprintf(stderr, USAGE);
- return 10;
- }
-
- if (atexit(FreeSystem))
- return 20;
-
- if (!InitSystem())
- return 20;
-
- if (!(homesystem = getenv("HOSTNAME")))
- if (!(homesystem = GetConfigEntry(NULL, "NodeName", NULL))) {
- fprintf(stderr, "LharcUUCP: Can't determine the name of this machine!\n");
- return 10;
- }
- homesystem = strdup(homesystem);
- spooldir = strdup(GetConfig(NULL, UUSPOOL, NULL, "UUSPOOL:"));
-
- if (getcd(0, currentdir))
- return 10;
-
- while (--argc && !break_detected)
- PackSpoolJobs(argv[argc]);
-
- chdir(currentdir);
- if (break_detected)
- fprintf(stderr, "*** Break has been detected!\n");
-
- return 0;
- }
-
-
- /************************************* subroutines ********/
- void PostArchive(char *systemname, char *postfix, char grade)
- {
- FILE *cfh, *xfh;
- char cfilename[64], xfilename[64], *trunc_sysname;
- char dremote[64];
-
- trunc_sysname = TruncName(systemname);
-
- sprintf(cfilename, "C.%s%c0000", trunc_sysname, grade);
- LockFile(cfilename);
- if (!(cfh = fopen(cfilename, "r"))) {
- fclose(cfh);
-
- /*
- * When the fopen() call succedes, does the file already
- * exist and we're finished.
- */
-
- sprintf(xfilename, "D.%sX%s", trunc_sysname, SeqToName(GetSequence(1)));
- sprintf(dremote, "%s%s", TruncName(homesystem), SeqToName(GetSequence(1)));
-
- if (cfh = fopen(cfilename, "w")) {
- fprintf(cfh, "S %s%s D.%s " LHARCUUCPDAEMON " - %s%s 0666\n",
- systemname, postfix, dremote, systemname, postfix);
- fprintf(cfh, "S %s X.%s%s " LHARCUUCPDAEMON " - %s 0666\n",
- xfilename, TruncName(homesystem), SeqToName(GetSequence(1)), xfilename);
- fclose(cfh);
- }
-
- LockFile(xfilename);
- if (xfh = fopen(xfilename, "w")) {
- fprintf(xfh, "U " LHARCUUCPMANAGER " %s\nF D.%s\nC rlharc\n", homesystem, dremote);
- fclose(xfh);
- }
- else
- remove(cfilename);
- UnLockFile(xfilename);
- }
- else
- fclose(cfh);
-
- UnLockFile(cfilename);
- }
-
-
- void PackSpoolJobs(char *systemname)
- {
- char currentdir[BUFFERSIZE], *cfile, *proclock;
- int success;
- char owngrade, grade;
-
- processed_flag = 0;
-
- if (strlen(systemname)+strlen(spooldir)+1 > BUFFERSIZE) {
- fprintf(stderr, "LharcUUCP: Directoryname is too long for internal buffer.\n");
- return; /* Buffer overflow! */
- }
-
-
- /*
- * Make sure we're the only LharcUUCP for this system.
- */
-
- if (!(proclock = malloc(strlen("LharcUUCP") + strlen(systemname) + 5)))
- return;
- sprintf(proclock, "%s_%s", "LharcUUCP", systemname);
- LockFile(proclock);
- printf("%s: Processing queued jobs for >%s<.\n", "LharcUUCP", systemname);
-
-
- /*
- * Change into the appropiate directory, holding the spool jobs
- * for the specified system.
- */
-
- sprintf(currentdir, "%s%s", spooldir, systemname);
- if (chdir(currentdir)) {
- sprintf(currentdir, "%s%s", spooldir, TruncName(systemname));
- if (chdir(currentdir)) {
- fprintf(stderr, "LharcUUCP: System >%s< has no spool directory?\n", systemname);
- strcpy(currentdir, spooldir);
- if (chdir(currentdir)) {
- fprintf(stderr, "LharcUUCP: Cd to >%s< failed.\n", spooldir);
- UnLockFile(proclock);
- free(proclock);
- return;
- }
- }
- }
-
-
- /*
- * Determine the configured grades for our process.
- */
-
- {
- char *string, buf[80];
-
- string = GetConfig(NULL, "BatchOnlyGrade", NULL, NULL);
- grade = (string && isalnum(string[0])) ? string[0] : 'z';
-
- sprintf(buf, "%s_%s", "BatchOnlyGrade", systemname);
- string = GetConfig(NULL, buf, NULL, NULL);
- grade = (string && isalnum(string[0])) ? string[0] : grade;
-
- string = GetConfig(NULL, "LharcBatchGrade", NULL, NULL);
- owngrade = (string && isalnum(string[0])) ? string[0] : 'A';
- }
-
-
-
- /*
- * Build a pattern matching 'C.system*', with the systemname
- * truncated to 7 characters and scan the spooldirectory for
- * queued jobs we can pack.
- */
-
- {
- char pattern[64];
- sprintf(pattern, "C.%s#?", TruncName(systemname));
- success = InitDirScan(pattern);
- }
-
- if (success)
- while ((cfile = ScanNextFile()) && !break_detected)
- LharcJob(systemname, cfile, grade);
- else
- fprintf(stderr, "%s: Directory scan for >%s< failed!\n", "LharcUUCP", currentdir);
-
- EndDirScan();
-
- if (processed_flag)
- PostArchive(systemname, LHARCPOSTFIX, owngrade);
-
- UnLockFile(proclock);
- free(proclock);
- }
-
-
- void LharcJob(char *systemname, char *cfilename, char grade)
- {
- FILE *cfilehandle;
- char command[2], from[256], to[256], from2[256], to2[256];
- char cmdbuf[256], archivename[64];
- int success = 0;
-
- printf("LharcUUCP: processing <%s>\n", cfilename);
-
- if (IsFileLocked(cfilename))
- return; /* uucico is processing this job right now,
- * so we better leave it alone.
- */
- LockFile(cfilename);
-
- if(!IsValidJob(cfilename, systemname, grade)) {
- UnLockFile(cfilename);
- return;
- }
-
- if (cfilehandle = fopen(cfilename, "r")) {
- fscanf(cfilehandle, "%s%s%s", command, from, to);
- if (GoNextLine(cfilehandle) != EOF) {
- fscanf(cfilehandle, "%s%s%s", command, from2, to2);
-
- LockFile(from);
- LockFile(to);
- LockFile(from2);
- LockFile(to2);
- if (!(rename(from, to)))
- if (!(rename(from2, to2))) {
- success = 1;
- processed_flag = 1;
-
-
- /* Free unused files a soon as
- * possible.
- */
-
- fclose(cfilehandle);
- remove(cfilename);
- UnLockFile(from);
- UnLockFile(from2);
- UnLockFile(cfilename);
-
-
- /* Batch the processed files.
- */
-
- sprintf(cmdbuf, "%s %s %s %s", LHARC, systemname, to, to2);
- printf("%s\n", cmdbuf);
- sprintf(archivename, "%s%s", systemname, LHARCPOSTFIX);
- LockFile(archivename);
- system(cmdbuf);
- UnLockFile(archivename);
- }
- else
- rename(to, from);
-
-
- /* When one of the 'renames' failed, we still
- * have to close and unlock the files.
- */
-
- if (!success) {
- UnLockFile(from);
- UnLockFile(from2);
- fclose(cfilehandle);
- UnLockFile(cfilename);
- }
-
- UnLockFile(to); /* These must be freed */
- UnLockFile(to2); /* in any case. */
- }
- }
- }
-
-
- /*
- * IsValidJob() checks the provided C.* file and tests if this job may
- * may be processed by LharcUUCP. Currently, we only process mail and
- * news. UUCP- and lharc-jobs are left alone.
- */
-
- int IsValidJob(char *cfilename, char *systemname, char grade)
- {
-
- /*
- * Test if the grade of the commandfile is lower or the same as
- * our max. grade.
- */
-
- if (cfilename[strlen(TruncName(systemname)) + 2] > grade)
- return 0;
-
-
- /*
- * Check the number part of the filename. C.*0000 are the C.*
- * files for the processed archives and won't be touched.
- */
-
- if (!strncmp(&cfilename[strlen(cfilename) - 4], "0000", 4))
- return 0;
-
-
- /*
- * Check the contents of the file. Neither target nor source file
- * may contain paths and we can't batch read-requests of course.
- */
-
- {
- FILE *fh;
- int success = 0;
- char from[256], to[256];
-
- if (fh = fopen(cfilename, "r")) {
- while (1) {
- if (fgetc(fh) == 'S') {
- fscanf(fh, "%s%s", from, to);
- if (IsPath(from) || IsPath(to))
- break;
- GoNextLine(fh);
- if (feof(fh)) {
- success = 1;
- break;
- }
- }
- else
- break;
- }
- fclose(fh);
- }
- if (!success)
- return 0;
- }
-
- return 1;
- }
-
-