home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / gigop806.zip / FIXMAP.CPP < prev    next >
Text File  |  1994-07-05  |  8KB  |  239 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, June 1994    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 <ctype.h>
  30. #include <direct.h>
  31. #include <dos.h>
  32. #include <stdarg.h>
  33.  
  34.  
  35. char            mapfile[256] = "MAPPING.CFG";
  36.  
  37. struct headers {
  38.     char            Apparently_To[256];
  39.     char            To[256];
  40.     char            From[256];
  41.     char            Subject[256];
  42. };
  43.  
  44. /*
  45.  * cmpcopy will compare the string at SOURCE, and see if it contains the
  46.  * TOKEN.  If so, it will copy the value of that token to DEST.
  47.  */
  48.  
  49. void            cmpcopy(char *token, char *source, char *dest)
  50. {
  51.     if (!strncmp(source, token, strlen(token))) {
  52.         source += strlen(token);// get past the token itself
  53.         while (*source == ' ')
  54.             source++;           // get past the spaces
  55.         strcpy(dest, source);
  56.     }
  57. }
  58.  
  59. /*
  60.  *  readheaders will read lines until it finds a blank line or 
  61.  *  end of file.  Known tokens will be copied to the structure
  62.  *  called headers.
  63.  */
  64.  
  65.  void            cmpcopy(char *token, char *source, char *dest);
  66.  
  67. void            readheaders(FILE * file, headers * header)
  68. {
  69.     char            line[1024] = " ";
  70.     memset(header, 0, sizeof(headers));
  71.     while ((*line) && (!feof(file))) {
  72.  
  73.         /* remember, GIGO stands for garbage in, garbage out. */
  74.         memset(line, 0, sizeof(line));
  75.         fgets(line, sizeof(line) - 1, file);
  76.  
  77.         /* fgets includes the line terminator; we need to remove it. */
  78.         if (*line)
  79.             if (line[strlen(line) - 1] == '\n')
  80.                 line[strlen(line) - 1] = 0;
  81.         if (*line)
  82.             if (line[strlen(line) - 1] == '\r')
  83.                 line[strlen(line) - 1] = 0;
  84.         if (!*line)
  85.             continue;           /* We got a blank line, or an eof */
  86.         cmpcopy("Apparently-To:", line, header->Apparently_To);
  87.         cmpcopy("To:", line, header->To);
  88.         cmpcopy("From:", line, header->From);
  89.         cmpcopy("Subject:", line, header->Subject);
  90.     }
  91. }
  92.  
  93.  
  94. /*
  95.  *  readlines will read the remainder of the file, and decide if it's
  96.  *  going to ADD or DROP the area in question.
  97.  * 
  98.  *  It will append the + newsgroupname\n to the appropriate global variable
  99.  *  buffer.
  100.  */
  101.  
  102.  
  103. void            readlines(FILE * file, char *adds, char *drops)
  104. {
  105.     char            line[1024] = " ";
  106.     char            line2[1024];
  107.     char             *p;
  108.  
  109.     strcpy(adds, "\n");
  110.     strcpy(drops, "\n");
  111.  
  112.     while (!feof(file)) {
  113.         memset(line, 0, sizeof(line));
  114.         fgets(line, sizeof(line) - 1, file);
  115.  
  116.         if ((p=strpbrk(line,"\r\n"))!=NULL) *p=0;  /* we don't want the newline info on this */
  117.         if (!line[0])       /* No line? No problem, go to the next line. */
  118.             continue;           /* We got a blank line, or an eof */
  119.  
  120.         if (strnicmp(line,"--",2)==NULL) break;   /* Tearline */
  121.         if (strnicmp(line,"|",2)==NULL) break;  /* Common start of a sig */
  122.  
  123.         if (!isalnum(line[1])) break; /* Huh? What the hell is THIS doing here? */
  124.                                       /* Supposedly, newsgroups start with alpha or num */
  125.  
  126.         strcpy(line2,line);    /* this will be the uppercase equivalent */
  127.         strupr(line2); strlwr(line);
  128.  
  129.         if (line[0] == '') continue;
  130.         if (line[0] == '%') continue;
  131.         if (line[0] == '-')
  132.             sprintf(drops + strlen(drops), ";= %s %s\n", line + 1,line2+1);
  133.         else
  134.         if (line[0] == '+')
  135.             sprintf(adds + strlen(adds), "= %s %s\n", line + 1,line2+1);
  136.         else
  137.             sprintf(adds + strlen(adds), "= %s %s\n", line,line2);
  138.     }
  139. }
  140.  
  141. /*
  142.  * Generate the new map file.  ADDS are written at the top.
  143.  * The file is then scanned line by line; any line that exists in the buffer
  144.  * pointed to by DROPS or ADDS will not be output (either because they
  145.  * are being dropped, or they were just added, and should not be printed
  146.  * a second time around!)
  147.  */
  148.  
  149.  
  150. void            update_map(char *mappingcfg, char *adds, char *drops)
  151. {
  152.     FILE           *oldfile;
  153.     FILE           *newfile;
  154.     char            line[1024];
  155.     char            line2[1024];
  156.     unlink("MAPPING.BAK");
  157.     rename(mappingcfg, "MAPPING.BAK");
  158.  
  159.     oldfile = fopen("MAPPING.BAK", "rt");
  160.     if (!oldfile) {
  161.         fprintf(stderr, "Damn! Could not open MAPPING.BAK for input\n");
  162.         exit(1);
  163.     }
  164.     newfile = fopen(mappingcfg, "wt");
  165.     if (!newfile) {
  166.         fprintf(stderr, "Damn! Could not open %s for output\n", mappingcfg);
  167.         exit(1);
  168.     }
  169.     fprintf(newfile, adds + 1, strlen(adds) - 1);
  170.  
  171.     while (!feof(oldfile)) {
  172.  
  173.         memset(line, 0, sizeof(line));line[0]='\n';
  174.         fgets(line + 1, sizeof(line) - 2, oldfile);
  175.         strcpy(line2,line);
  176.         if (line2[1]==';') strcpy(line2+1,line+2);
  177.         if (!line[1])
  178.             continue;           /* We got a blank line, or an eof */
  179.         if (strstr(drops, line))
  180.             continue;
  181.         if (strstr(adds, line))
  182.             continue;
  183.         if (strstr(drops, line2))
  184.             continue;
  185.         if (strstr(adds, line2))
  186.             continue;
  187.         fprintf(newfile, "%s", line + 1);
  188.     }
  189.     fprintf(newfile,drops);
  190.     fclose(newfile);
  191.     fclose(oldfile);
  192. }
  193.  
  194. /*
  195.  *  Find out what the message is all about, and handle it.
  196.  *
  197.  */
  198.  
  199. void            process_function(char *function_req, char *mappingcfg)
  200. {
  201.     FILE           *file;
  202.     char           *adds;
  203.     char           *drops;
  204.     headers         header;
  205.     adds = (char *) calloc(1, 64000);
  206.     drops = (char *) calloc(1, 64000);
  207.     if ((!adds) || (!drops)) {
  208.         fprintf(stderr, "Could not allocate buffers.\n");
  209.         exit(1);
  210.     }
  211.     file = fopen(function_req, "rt");
  212.     if (!file) {
  213.         fprintf(stderr, "Could not open %s for input.\n", function_req);
  214.         exit(1);
  215.     }
  216.     readheaders(file, &header);
  217.     readlines(file, adds, drops);
  218.     fclose(file);
  219.     printf("From: %s\n"
  220.            "To  : %s\n"
  221.            "Subj: %s\n", header.From, header.To, header.Subject);
  222.  
  223.     update_map(mappingcfg, adds, drops);
  224. }
  225.  
  226. void            main(int argc, char *argv[])
  227. {
  228.     if (argc > 1)
  229.         strcpy(mapfile, argv[1]);
  230.     printf("FIXMAP Function Request Server for GIGO.\n"
  231.       "Reads the areafix style message body and modifies your mapping file.\n"
  232.            "(C) Copyright 1994 by Jason Fesler.  All rights reserved.\n\n"
  233.         "Source code for this utility is available; it can provide a decent\n"
  234.            "template for other function requests.\n\n"
  235.            "Input file  :  FUNCTION.REQ\n"
  236.            "Mapping File:  %s\n", mapfile);
  237.     process_function("FUNCTION.REQ", mapfile);
  238. }
  239.