home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / GIGO0209.ZIP / FIXMAP.ZIP / FIXMAP.CPP next >
C/C++ Source or Header  |  1993-12-20  |  7KB  |  233 lines

  1. /*
  2.  *  FIXMAP
  3.  *
  4.  *  Sample program that takes a GIGO function request file (FUNCTION.REQ),
  5.  *  checks it, and then modifies your mapping.cfg file with the new
  6.  *  request.  It assumes that the message body was written to look like
  7.  *  a FidoNet areafix request.
  8.  *
  9.  *  Due to the lack of standards for asking your host to change your settings,
  10.  *  this source code makes no attempts at doing so.  You are free to modify
  11.  *  it for your needs.
  12.  *
  13.  *  Jason Fesler  December, 1990    jfesler@wmeonlin.sacbbx.com
  14.  */
  15.  
  16.  
  17. /*  For simplicity sake, I include everything, so that I don't have
  18.  *  any problems with the compiler knowing what I am talking about.
  19.  */
  20. #include <io.h>
  21. #include <fcntl.h>
  22. #include <sys\stat.h>
  23. #include <process.h>
  24. #include <share.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <malloc.h>
  29. #include <conio.h>
  30. #include <ctype.h>
  31. #include <direct.h>
  32. #include <dos.h>
  33. #include <stdarg.h>
  34.  
  35.  
  36. char            mapfile[256] = "MAPPING.CFG";
  37.  
  38. extern unsigned int _stklen = 20000;    // Borland style stack declaration,
  39.                                         // ymmv
  40.  
  41. struct headers {
  42.     char            Apparently_To[256];
  43.     char            To[256];
  44.     char            From[256];
  45.     char            Subject[256];
  46. };
  47.  
  48.  
  49. /*
  50.  * cmpcopy will compare the string at SOURCE, and see if it contains the 
  51.  * TOKEN.  If so, it will copy the value of that token to DEST.
  52.  /
  53.  
  54. void            cmpcopy(char *token, char *source, char *dest)
  55. {
  56.     if (!strncmp(source, token, strlen(token))) {
  57.         source += strlen(token);// get past the token itself
  58.         while (*source == ' ')
  59.             source++;           // get past the spaces
  60.         strcpy(dest, source);
  61.     }
  62. }
  63.  
  64. /*
  65.  *  readheaders will read lines until it finds a blank line or 
  66.  *  end of file.  Known tokens will be copied to the structure
  67.  *  called headers.
  68.  */
  69.  
  70. void            readheaders(FILE * file, headers * header)
  71. {
  72.     char            line[1024] = " ";
  73.     memset(header, 0, sizeof(headers));
  74.     while ((*line) && (!feof(file))) {
  75.  
  76.         /* remember, GIGO stands for garbage in, garbage out. */
  77.         memset(line, 0, sizeof(line));
  78.         fgets(line, sizeof(line) - 1, file);
  79.  
  80.         /* fgets includes the line terminator; we need to remove it. */
  81.         if (*line)
  82.             if (line[strlen(line) - 1] == '\n')
  83.                 line[strlen(line) - 1] = 0;
  84.         if (*line)
  85.             if (line[strlen(line) - 1] == '\r')
  86.                 line[strlen(line) - 1] = 0;
  87.         if (!*line)
  88.             continue;           /* We got a blank line, or an eof */
  89.         cmpcopy("Apparently-To:", line, header->Apparently_To);
  90.         cmpcopy("To:", line, header->To);
  91.         cmpcopy("From:", line, header->From);
  92.         cmpcopy("Subject:", line, header->Subject);
  93.     }
  94. }
  95.  
  96.  
  97. /*
  98.  *  readlines will read the remainder of the file, and decide if it's
  99.  *  going to ADD or DROP the area in question.
  100.  * 
  101.  *  It will append the + newsgroupname\n to the appropriate global variable
  102.  *  buffer.
  103.  /
  104.  
  105.  
  106. void            readlines(FILE * file, char *adds, char *drops)
  107. {
  108.     char            line[1024] = " ";
  109.     strcpy(adds, "\n");
  110.     strcpy(drops, "\n");
  111.  
  112.     while (!feof(file)) {
  113.  
  114.         memset(line, 0, sizeof(line));
  115.         fgets(line, sizeof(line) - 1, file);
  116.  
  117.         /* fgets includes the line terminator; we need to remove it. */
  118.         if (*line)
  119.             if (line[strlen(line) - 1] == '\n')
  120.                 line[strlen(line) - 1] = 0;
  121.         if (*line)
  122.             if (line[strlen(line) - 1] == '\r')
  123.                 line[strlen(line) - 1] = 0;
  124.         if (!*line)
  125.             continue;           /* We got a blank line, or an eof */
  126.  
  127.         if (line[0] == '-')
  128.             sprintf(drops + strlen(drops), "+ %s\n", line + 1);
  129.         else
  130.         if (line[0] == '+')
  131.             sprintf(adds + strlen(adds), "+ %s\n", line + 1);
  132.         else
  133.             sprintf(adds + strlen(adds), "+ %s\n", line);
  134.     }
  135.  
  136.     strlwr(adds);
  137.     strlwr(drops);
  138. }
  139.  
  140. /*
  141.  * Generate the new map file.  ADDS are written at the top.
  142.  * The file is then scanned line by line; any line that exists in the buffer
  143.  * pointed to by DROPS or ADDS will not be output (either because they
  144.  * are being dropped, or they were just added, and should not be printed
  145.  * a second time around!)
  146.  */
  147.  
  148.  
  149. void            update_map(char *mappingcfg, char *adds, char *drops)
  150. {
  151.     FILE           *oldfile;
  152.     FILE           *newfile;
  153.     char            line[1024];
  154.     unlink("MAPPING.BAK");
  155.     rename(mappingcfg, "MAPPING.BAK");
  156.  
  157.     oldfile = fopen("MAPPING.BAK", "rt");
  158.     if (!oldfile) {
  159.         fprintf(stderr, "Damn! Could not open MAPPING.BAK for input\n");
  160.         exit(1);
  161.     }
  162.     newfile = fopen(mappingcfg, "wt");
  163.     if (!newfile) {
  164.         fprintf(stderr, "Damn! Could not open %s for output\n", mappingcfg);
  165.         exit(1);
  166.     }
  167.     fprintf(newfile, adds + 1, strlen(adds) - 1);
  168.  
  169.     while (!feof(oldfile)) {
  170.  
  171.         memset(line, 0, sizeof(line));
  172.         line[0] = '\n';
  173.         fgets(line + 1, sizeof(line) - 2, oldfile);
  174.         if (!line[1])
  175.             continue;           /* We got a blank line, or an eof */
  176.         if (strstr(drops, line))
  177.             continue;
  178.         if (strstr(adds, line))
  179.             continue;
  180.         fprintf(newfile, "%s", line + 1);
  181.     }
  182.  
  183.     fclose(newfile);
  184.     fclose(oldfile);
  185. }
  186.  
  187. /*
  188.  *  Find out what the message is all about, and handle it.
  189.  *
  190.  */
  191.  
  192. void            process_function(char *function_req, char *mappingcfg)
  193. {
  194.     FILE           *file;
  195.     char           *adds;
  196.     char           *drops;
  197.     char           *mapbuf;
  198.     headers         header;
  199.     adds = (char *) calloc(1, 64000);
  200.     drops = (char *) calloc(1, 64000);
  201.     if ((!adds) || (!drops)) {
  202.         fprintf(stderr, "Could not allocate buffers.\n");
  203.         exit(1);
  204.     }
  205.     file = fopen(function_req, "rt");
  206.     if (!file) {
  207.         fprintf(stderr, "Could not open %s for input.\n", function_req);
  208.         exit(1);
  209.     }
  210.     readheaders(file, &header);
  211.     readlines(file, adds, drops);
  212.     fclose(file);
  213.     printf("From: %s\n"
  214.            "To  : %s\n"
  215.            "Subj: %s\n", header.From, header.To, header.Subject);
  216.  
  217.     update_map(mappingcfg, adds, drops);
  218. }
  219.  
  220. void            main(int argc, char *argv[])
  221. {
  222.     if (argc > 1)
  223.         strcpy(mapfile, argv[1]);
  224.     printf("FIXMAP Function Request Server for GIGO.\n"
  225.       "Reads the areafix style message body and modifies your mapping file.\n"
  226.            "(C) Copyright 1993 by Jason Fesler.  All rights reserved.\n\n"
  227.         "Source code for this utility is available; it can provide a decent\n"
  228.            "template for other function requests.\n\n"
  229.            "Input file  :  FUNCTION.REQ\n"
  230.            "Mapping File:  %s\n", mapfile);
  231.     process_function("FUNCTION.REQ", mapfile);
  232. }
  233.