home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lan / g0bsx / files.c < prev    next >
Text File  |  1989-05-26  |  9KB  |  425 lines

  1. /*
  2.  *  FILES.C - Server for the W0RLI MailBox V10.xx
  3.  *
  4.  *  Copyright (C) 1989
  5.  *  Peter Meiring
  6.  *  13 Venn Crescent,
  7.  *  Hartley, Plymouth.
  8.  *  PL3 5PJ
  9.  *
  10.  *  This code may be freely used and copied for non-commercial uses.
  11.  *
  12.  *  s files files.exe files.out H8 files.in H8
  13.  *
  14.  *  See SERVER.DOC for further details.
  15.  *
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <dir.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24.  
  25. #define BUFSIZE 256
  26.  
  27. /*
  28.  *  Copyright storage:
  29.  */
  30.  
  31. char *copyright = "FILES 1.03  Copyright (c) Peter Meiring, G0BSX, 1989.\n";
  32.  
  33. /*
  34.  *  Some buffer space.
  35.  */
  36.  
  37. #define ln_buf 512
  38.  
  39. char buf[ln_buf];
  40.  
  41. /*
  42.  *  End-of-message mark.
  43.  */
  44.  
  45. char *mark    = "/EX\n";
  46.  
  47. /*
  48.  *  File used by this program to read initialisation Data
  49.  */
  50.  
  51. char *datfile = "/bbs/config.mb";
  52. /*
  53.  *  File the MailBox will create, and this server will read.
  54.  */
  55.  
  56. char *infile  = "files.out";
  57.  
  58. /*
  59.  *  File this server will create, and the MailBox will read.
  60.  */
  61.  
  62. char *outfile = "files.in";
  63.  
  64. /*
  65.  *  Data structures for the W0RLI filing system.
  66.  */
  67.  
  68. static struct { char letter;
  69.          char path[80] ;
  70.          char name[80] ;
  71.          int  ok    ; }  root[32];
  72. static int ne;
  73.  
  74. /* Copy file */
  75.  
  76. copyfile(filename,fp)
  77. FILE *fp;
  78. char *filename;
  79. {
  80.     int i, dn;
  81.     char *st, *n1, *n2;
  82.     FILE *datafile;
  83.     char tmp[128];
  84.     long nb, nl, ln;
  85.  
  86.     for(dn=0;((root[dn].letter!=toupper(*filename))&&(dn < ne));dn++)
  87.  
  88.     if( (dn == ne) || (root[dn].ok == 0) ) {
  89.       fprintf(fp,"Directory Area %c: not found\n",*filename);
  90.       return 1;
  91.     }
  92.  
  93.     for(i=1;(ispunct(filename[i])||isspace(filename[i]));i++);
  94.  
  95.     for(n1 = filename+i;((*n1!=' ')&&(*n1!='\0'));n1++);
  96.     if(*n1==' ') {
  97.       *n1++ = '\0';
  98.       while(*n1==' ') n1++ ; }
  99.     for(n2 = n1;((*n2!=' ')&&(*n2!='\0'));n2++);
  100.     if(*n2==' ') {
  101.       *n2++ = '\0';
  102.       while( *n2==' ' ) n2++; }
  103.  
  104.     nb = atol(n1);
  105.     nl = atol(n2);
  106.  
  107.    if(nl==0) {
  108.       if(nb==0) {
  109.         nb = 1L;
  110.         nl = 65535L; }
  111.       else {
  112.         nl = nb ;
  113.         nb = 1 ; } }
  114.  
  115.     strcpy(tmp, root[dn].path);
  116.     strcat(tmp, filename+i);
  117.  
  118.     fprintf(fp, "Full path: %s\n",tmp);
  119.  
  120.     if ((datafile = fopen(tmp, "r")) == NULL) {
  121.        fprintf(fp,"File: %s not found \n", filename);
  122.        return 1;
  123.     }
  124.     fprintf(fp, "Directory: %c: %s\n     File: %s",root[dn].letter,
  125.             root[dn].name, filename +i);
  126.  
  127.     if ((atol(n1)!=0) && (atol(n2)==0))
  128.       fprintf(fp, "   Lines 1 to %ld", nl);
  129.     else if((atol(n2)!=0))
  130.       fprintf(fp, "   Lines %ld to %ld",nb,nl);
  131.     fprintf(fp, "\n\n");
  132.  
  133.     st = fgets(buf, ln_buf, datafile) ;
  134.     ln = 1L;
  135.     while (st != NULL)
  136.     {
  137.        if((nb<=ln)&&(ln<=nl)) fputs(buf, fp);
  138.        st = fgets(buf, ln_buf, datafile);
  139.        ln++;
  140.     }
  141.     fclose(datafile);
  142.     return 0;
  143. }
  144.  
  145. stripcr( line )
  146. char *line;
  147. {
  148.   int i;
  149.   for(i=1;i<strlen(line);i++)
  150.     if(line[i]=='\n')
  151.         line[i]='\0' ;
  152. }
  153. char *
  154. skip(buf, fp)
  155. char *buf;
  156. FILE *fp;
  157. {
  158.   char *sts;
  159.   sts = fgets( buf, ln_buf, fp);
  160.   while( ((*buf == ';') || (*buf == '\n')) && (sts != NULL))
  161.     sts = fgets(buf, ln_buf, fp) ;
  162.   return sts;
  163. }
  164.  
  165. int getdirs()
  166. {
  167.   FILE *fp;
  168.   int n;
  169.   char *st;
  170.  
  171.   if((fp = fopen(datfile, "r")) == NULL) return -1;
  172.  
  173. /*
  174.  *  Skip the first section of the config file.
  175.  */
  176.  
  177.   st = fgets(buf, ln_buf, fp);
  178.  
  179.   while ( (strncmp(buf, "***", 3)) && (st != NULL))
  180.      st = fgets(buf, ln_buf, fp);
  181. /*
  182.  *  Read the files section (three lines per entry)
  183.  */
  184.  
  185.   st = skip(buf, fp);
  186.   n=0;
  187.   while( (strncmp(buf, "***", 3)) && (st != NULL)) {
  188.  
  189. /*
  190.  *  Extract parameters
  191.  */
  192.     root[n].letter = toupper(*buf);
  193. /*    if( index(buf, "D") > -1 || index(buf, "d") > -1 ) 
  194. */      root[n].ok = 1;
  195. /*    else
  196.       root[n].ok = 0; 
  197. */      st = skip(buf, fp);
  198.       if (!strncmp(buf, "***",3) || (st == NULL)) return -1;
  199.       strcpy(root[n].path, buf);
  200.     stripcr(root[n].path);
  201.  
  202.     st = skip(buf, fp);
  203.     if ((!strncmp(buf, "***", 3)) || (st == NULL)) return -1;
  204.     strcpy(root[n].name, buf);
  205.     stripcr(root[n].name);
  206.  
  207.     st = skip(buf, fp);
  208.     n++;
  209.   }
  210.   fclose(datfile);
  211.   return n;
  212. }
  213.  
  214. void
  215. doroot(fp)
  216. FILE *fp;
  217. {
  218.   int dn;
  219.  
  220.   fputs("Directories Available:\n",fp);
  221.   if( ne == 0 )
  222.     fputs("None!\n",fp);
  223.   else
  224.     for(dn=0;dn<ne;dn++)
  225.       fprintf(fp, "%c: %s\n",root[dn].letter, root[dn].name);
  226. }
  227.  
  228. getdir( fp, name )
  229. char *name;
  230. FILE *fp;
  231. {
  232.   struct ffblk *fbuf;
  233.  
  234.   if( findfirst( name, fbuf, 0xFFFF ) == 0 )
  235.     printf("%12s %8ld\n", fbuf->ff_name, fbuf->ff_fsize);
  236.   while( findnext( fbuf ) == 0)
  237.     printf("%12s %8ld\n", fbuf->ff_name, fbuf->ff_fsize);
  238. }
  239.  
  240. dodir(path,fp)
  241. FILE *fp;
  242. char *path;
  243. {
  244.   char tmp[128];
  245.   int dn;
  246.   char dc;
  247.  
  248.   dc = toupper(*path++);
  249. /*  while (*path && !isalpha(*path)) path++ ;
  250. */
  251.   for(dn=0;((root[dn].letter!=dc)&&(dn < ne));dn++) ;
  252.  
  253.   if(dn>=ne) {
  254.     if (isalpha(dc))
  255.       fprintf(fp, "Directory Area: %c not found\n",dc);
  256.     else
  257.       doroot(fp);
  258.     return 0; }
  259.  
  260.   fprintf(fp, "Directory Listing of  %c: %s\n\n",dc, root[dn].name);
  261.   strcpy(tmp, root[dn].path);
  262. /*  if (*path) strcat(tmp, path) ;
  263.   else */
  264.   strcat(tmp,"*.*");
  265.  
  266.   if (getdir(tmp,fp))
  267.     fprintf(fp,"Path: %s not found",tmp);
  268.   fprintf(fp,"\n");
  269.   return 0;
  270. }
  271.  
  272. void
  273. rfc822(from, to, subject, fp)
  274. char *from, *to, *subject;
  275. FILE *fp;
  276. {
  277.   fprintf(fp, "From: %s", from);
  278.   fprintf(fp, "To: %s", to);
  279.   fprintf(fp, "Subject: Files Cmd: %s", subject);
  280.   fputs("\n", fp);
  281. }
  282.  
  283. main(argc, argv)
  284. int argc;
  285. char *argv[];
  286. {
  287.   FILE *in, *out;
  288.   char *st, *tail, *fname, *ptr, from[32], to[32], subject[64] ;
  289.   int n, ok;
  290.  
  291. /*
  292.  *   Open and read the initialisation file: reqxxx.dat in the root directory
  293.  */
  294.   ok = 0;
  295.   puts(copyright);
  296.  
  297. /*
  298.  *  Initialise the Filing Database
  299.  */
  300.  
  301.   if ((ne = getdirs()) == -1) {
  302.     printf("FILES: Unexpected end of file in: %s\n", datfile);
  303.     exit(1);
  304.   }
  305.  
  306. /*
  307.  *  Open the input file.
  308.  *  If not possible, exit with return code 1 to signal
  309.  *  the MailBox that the server failed.
  310.  */
  311.  
  312.   if ((in = fopen(infile, "r")) == NULL) exit(1);
  313.  
  314. /*
  315.  *  Open the output file.
  316.  *  If not possible, exit with return code 1 to signal
  317.  *  the MailBox that the server failed.
  318.  */
  319.  
  320.   if ((out = fopen(outfile, "w")) == NULL) exit(1);
  321.  
  322.   st = fgets(buf, ln_buf, in);
  323.   while (st != NULL)
  324.   {
  325.  
  326. /*
  327.  *  Read the rfc-822 header placed into the message by the MailBox.
  328.  *  The end of header is marked by a blank line.
  329.  */
  330.  
  331.     while(*buf != '\n')
  332.     {
  333.  
  334. /*
  335.  *  Find end of keyword.
  336.  *  Terminate string at end of keyword.
  337.  */
  338.  
  339.       tail = strchr(buf, ' ');
  340.       *tail++ = '\0';
  341.  
  342. /*
  343.  *  Make the new rfc-822 header.
  344.  */
  345.  
  346.       if      (!strcmp(buf, "To:"))      strcpy(from, tail);
  347.       else if (!strcmp(buf, "From:"))    strcpy(to, tail);
  348.       else if (!strcmp(buf, "Subject:")) {
  349.                 strcpy(subject, tail);
  350.         strcpy( fname, tail); }
  351.       st = fgets(buf, ln_buf, in);
  352.     }
  353.  
  354. /*
  355.  *  Now that the rfc-822 header has been read, and a new one created,
  356.  *  copy the text of the message from the input to the output.
  357.  */
  358.     stripcr(fname) ;
  359.  
  360.     if( toupper(*fname) == 'W' ) {
  361.       rfc822(from, to, subject, out);
  362.       dodir(fname+1, out);
  363.       fputs(mark, out);
  364.       ok = 1; }
  365.     else if (toupper(*fname) == 'D' ) {
  366.       rfc822(from, to, subject, out);
  367.       copyfile(fname+1,out) ;
  368.       fputs(mark, out);
  369.       ok = 1; }
  370.  
  371.     while ((st != NULL) && stricmp(buf, mark))
  372.     {
  373.       st = fgets(buf, ln_buf, in);
  374.       if( toupper(*buf) == 'W' ) {
  375.         strcpy(subject, buf);
  376.         stripcr(buf);
  377.         rfc822(from, to, subject, out);
  378.         dodir(buf+1, out);
  379.         fputs(mark, out); 
  380.         ok = 1; }
  381.       else if (toupper(*buf) == 'D' ) {
  382.         strcpy(subject, buf);
  383.         stripcr(buf);
  384.         rfc822(from, to, subject, out);
  385.         copyfile(buf+1,out) ;
  386.         fputs(mark, out);
  387.         ok = 1; }
  388.     }
  389.     if( !ok ) {
  390.       rfc822(from, to, "NONE\n", out);
  391.       fputs("FILES: No valid command found in Subject or Message\n", out);
  392.       fputs(mark, out); }
  393.  
  394. /*
  395.  *  Go on to the next message.
  396.  */
  397.  
  398.     if (st != NULL) st = fgets(buf, ln_buf, in);
  399.   }
  400.  
  401. /*
  402.  *  All the incoming messages have been processed,
  403.  *  and an outgoing message created for each one.
  404.  */
  405.  
  406.   fclose(in);
  407.   fclose(out);
  408.  
  409. /*
  410.  *  Delete the input file.
  411.  *  The MailBox appends messages to this file, this server must delete it.
  412.  */
  413.  
  414.   unlink(infile);
  415.  
  416. /*
  417.  *  Exit with return code 0 to signal the MailBox that
  418.  *  the server completed it's processing normally.
  419.  *  The MailBox will then read our output file,
  420.  *  and create messages from it.
  421.  */
  422.  
  423.   exit(0);
  424. }
  425.