home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 December / PCO1297.ISO / FilesBBS / DOS / SQRESET.EXE / SOURCE.ZIP / SQRESET.C next >
Encoding:
C/C++ Source or Header  |  1997-09-03  |  7.3 KB  |  349 lines

  1. /*
  2.             SqReset by John Gardeniers (3:632/360.70)
  3.                     This code is public domain.
  4.  
  5.             Resets the maximum messages, frozen messages
  6.             and maximum age of messages to that specified
  7.             in Squish.cfg.
  8.  
  9.             Will search for Squish.cfg by using the Squish
  10.             environement variable, followed by a search of
  11.             the current directory, then the path.
  12.  
  13.             Tested only with Turbo C++ v3.0
  14.  
  15.             This version is fully self contained, requiring
  16.             no external programs to function but requires
  17.             the MSGAPI headers and library to be available
  18.             at compile time. To use the library as it
  19.             arrives in the developer's kit the program must
  20.             be compiled with the large memory model.
  21.  
  22.             To skip over passthrough areas use "SqReset -P".
  23.             to report only the present settings use "SqReset -R".
  24.  
  25.             If a log if defined in Squish.cfg SqReset will
  26.             use that log as well.
  27. */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <process.h>
  33. #include <dir.h>
  34. #include <dos.h>
  35. #include <io.h>
  36. #include <msgapi.h>
  37.  
  38. #define BRAG "SQRESET v4.4 - public domain by John Gardeniers (3:632/360.70)"
  39.  
  40. /* A nice, generous buffer size */
  41. #define BUF 1025
  42.  
  43. FILE *log;
  44.  
  45. /* Used by MSGAPI */
  46. struct _minf minf;
  47.  
  48. /* TC complained about needing this */
  49. char *strupr(char *);
  50.  
  51. /* Output to the log file (if open) */
  52. void log_it(char *msg)
  53. {
  54.     printf("%s", msg);
  55.  
  56.     if(log)
  57.         fputs(msg, log);
  58. }
  59.  
  60. /* Close the logfile */
  61. void do_close(void)
  62. {
  63.     if(log)
  64.     {
  65.         fputs("SqReset end\n", log);
  66.         fclose(log);
  67.     }
  68. }
  69.  
  70. /* Error message and abort */
  71. void abandon(char *text, char code)
  72. {
  73.     log_it(text);
  74.     log_it("... Exiting.\nBetter run SqFix.\n");
  75.     do_close();
  76.     exit(code);
  77. }
  78.  
  79. /* Convert a set of bytes to a long */
  80. dword make_long(char *bytes)
  81. {
  82. union {
  83.     char Bytes[4];
  84.     dword Long;
  85. } conv;
  86.  
  87.     strncpy(conv.Bytes, bytes, 4);
  88.     return conv.Long;
  89. }
  90.  
  91. /* Strip leading white space from the string */
  92. char *kill_white(char *string)
  93. {
  94. char *ptr;
  95.  
  96.     ptr=string;
  97.  
  98.     while(*ptr==' ' || *ptr=='\t')
  99.         ptr++;
  100.  
  101.     return ptr;
  102. }
  103.  
  104. /* Isolate the next "word", eliminating leading white space */
  105. char *nextword(char *text)
  106. {
  107. char *chr;
  108.  
  109.     chr=text;
  110.  
  111.     /* Search for the first space or tab character, or end of string */
  112.     for(; chr; chr++)
  113.     {
  114.         if(*chr==' ' || *chr=='\t' || *chr==0)
  115.             break;
  116.     }
  117.  
  118.     if(*chr)
  119.     {
  120.         *chr++=0;
  121.         chr=kill_white(chr);
  122.     }
  123.  
  124.     return chr;
  125. }
  126.  
  127. void syntax(void)
  128. {
  129.     puts(BRAG);
  130.     puts("\nA utility to set Squish message area settings");
  131.     puts("according to those specified in Squish.cfg\n");
  132.     puts("Syntax: SqReset [-P] [-R]");
  133.     puts("        -P will skip passthrough areas");
  134.     puts("        -R will report the current settings only");
  135.     exit(0);
  136. }
  137.  
  138. /* Main code */
  139. int main(int argc, char **argv)
  140. {
  141. /* These arrays are a bit generous. Better safe than sorry */
  142. char buffer[BUF], *ptr, *string;
  143. char msg_area[80], days[80], msgs[80], skip[80];
  144. FILE *filenum, *squish_data;
  145. int squish, passthru, no_pass=0, report=0;
  146. HAREA handle;
  147. char sq_buff[256], text[128];
  148. dword skip_, msgs_, days_;
  149. struct date dd;
  150. struct time tt;
  151.  
  152.     if(argc>1 && strchr(argv[1], '?')!=NULL)
  153.         syntax();
  154.  
  155.     memset(&log, '\0', sizeof log);
  156.     memset(&minf, '\0', sizeof minf);
  157.     minf.req_version=1;
  158.     puts(BRAG);
  159.  
  160.     strcpy(buffer, getenv("SQUISH"));
  161.  
  162.     if(*buffer)
  163.         if(buffer[strlen(buffer)-1]=='\\')
  164.             strcat(buffer, "\\squish.cfg");
  165.  
  166.     else
  167.         strcpy(buffer, searchpath("squish.cfg"));
  168.  
  169.     /* Locate the Squish configuration file */
  170.     if((filenum=fopen(buffer, "rt"))==NULL)
  171.         abandon("Squish configuration file not found", 1);
  172.  
  173.     /* See if we want to exclude passthroughs */
  174.     if(argc>1)
  175.     {
  176.         strupr(argv[1]);
  177.  
  178.         if(!strcmp(argv[1], "-P"))
  179.             no_pass++;
  180.  
  181.         if(!strcmp(argv[1], "-R"))
  182.             report++;
  183.     }
  184.  
  185.     /* Initialise MSGAPI */
  186.     if(MsgOpenApi(&minf))
  187.         abandon("Error initialising MSGAPI", 2);
  188.  
  189.     /* Read in the configuration file line by line */
  190.     while(fgets(buffer, BUF, filenum)!=NULL)
  191.     {
  192.         /* Get rid of any trailing CR */
  193.         if((ptr=strchr(buffer, '\n'))!=NULL)
  194.             *ptr=0;
  195.  
  196.         /* Convert to uppercase and lose leading white space */
  197.         ptr=kill_white(strupr(buffer));
  198.  
  199.         /* Skip past comment and blank lines */
  200.         if(*ptr==';' || !*ptr)
  201.             continue;
  202.  
  203.         /* Check for the logfile entry */
  204.         if(!log && !strncmp(ptr, "LOGFILE", 7))
  205.         {
  206.             if((log=fopen(nextword(ptr), "a"))!=NULL)
  207.             {
  208.                 getdate(&dd);
  209.                 gettime(&tt);
  210.                 fprintf(log, "\nSqReset: %d/%d/%d %2d:%02d:%02d\n",
  211.                     dd.da_day, dd.da_mon, dd.da_year,
  212.                     tt.ti_hour, tt.ti_min, tt.ti_sec);
  213.             }
  214.         }
  215.  
  216.         /* We're looking for the message base details only */
  217.         if(!strncmp(ptr, "NETAREA", 7)
  218.             || !strncmp(ptr, "ECHOAREA", 8)
  219.             || !strncmp(ptr, "LOCALAREA", 9)
  220.             || !strncmp(ptr, "DUPEAREA", 8)
  221.             || !strncmp(ptr, "BADAREA", 7))
  222.         {
  223.             ptr=nextword(ptr);
  224.  
  225.             /* Ignore the area tag */
  226.             if(*ptr)
  227.                 ptr=nextword(ptr);
  228.  
  229.             /* Get the path and root name */
  230.             if(*ptr)
  231.             {
  232.                 string=ptr;
  233.                 ptr=nextword(ptr);
  234.                 strcpy(msg_area, string);
  235.             }
  236.  
  237.             /* Set the defaults */
  238.             strcpy(msgs, "0");
  239.             strcpy(skip, "0");
  240.             strcpy(days, "0");
  241.             squish=passthru=0;
  242.  
  243.             /* Search for the area's flags */
  244.             while(*ptr++=='-')
  245.             {
  246.                 /* If this one isn't found it's not Squish */
  247.                 if(*ptr=='$')
  248.                 {
  249.                     squish++;
  250.                     ptr++;
  251.                 }
  252.  
  253.                 /* Isolate this parameter */
  254.                 string=ptr;
  255.                 ptr=nextword(ptr);
  256.  
  257.                 /* Act only on the correct flags */
  258.                 switch(*string++)
  259.                 {
  260.                     case '0': /* Passthrough area */
  261.                         if(no_pass)
  262.                             passthru++;
  263.  
  264.                         break;
  265.  
  266.                     case 'M': /* Maximum messages */
  267.                         strcpy(msgs, string);
  268.                         break;
  269.  
  270.                     case 'S': /* Messages to skip (freeze)*/
  271.                         strcpy(skip, string);
  272.                         break;
  273.  
  274.                     case 'D': /* Number of days */
  275.                         strcpy(days, string);
  276.                         break;
  277.                 }
  278.             }
  279.  
  280.             /* Only do this for Squish messages */
  281.             if(squish && !passthru)
  282.             {
  283.                 /* Tell us what was found */
  284.                 sprintf(text, "   %s\n", msg_area);
  285.                 log_it(text);
  286.                 strcpy(sq_buff, msg_area);
  287.                 strcat(sq_buff, ".sqd");
  288.                 squish_data=fopen(sq_buff, "rb");
  289.  
  290.                 if(squish_data==NULL)
  291.                 {
  292.                     strcpy(text, "      Area missing\n");
  293.  
  294.                     if(report)
  295.                         log_it(text);
  296.  
  297.                     else
  298.                         fprintf(log, text);
  299.                 }
  300.  
  301.                 else
  302.                 {
  303.                     fread(sq_buff, 1, 256, squish_data);
  304.                     skip_=make_long(&sq_buff[12]);
  305.                     msgs_=make_long(&sq_buff[124]);
  306.                     days_=make_long(&sq_buff[128]);
  307.                     fclose(squish_data);
  308.  
  309.                     if(report)
  310.                     {
  311.                         sprintf(text, "      Skipmsg = %ld, maxmsg = %ld, days = %ld\n",
  312.                                 skip_, msgs_, days_);
  313.                         log_it(text);
  314.                     }
  315.  
  316.                     else
  317.                     {
  318.                         fprintf(log, "      Was: skipmsg = %ld, maxmsg = %ld, days = %ld\n",
  319.                             skip_, msgs_, days_);
  320.                         skip_=atol(skip);
  321.                         msgs_=atol(msgs);
  322.                         days_=atol(days);
  323.                         fprintf(log, "      Now: skipmsg = %ld, maxmsg = %ld, days = %ld\n",
  324.                             skip_, msgs_, days_);
  325.  
  326.                         /* Open the message area and fiddle with it */
  327.                         if((handle=MsgOpenArea(msg_area, MSGAREA_CRIFNEC, MSGTYPE_SQUISH))==NULL)
  328.                             abandon("Error opening the message area", 3);
  329.  
  330.                         SquishSetMaxMsg(handle, msgs_, skip_, days_);
  331.  
  332.                         if(MsgCloseArea(handle))
  333.                             abandon("Error closing the message area", 4);
  334.                     }
  335.                 }
  336.             }
  337.         }
  338.     }
  339.  
  340.     /* Close and exit */
  341.     fclose(filenum);
  342.  
  343.     if(MsgCloseApi())
  344.         abandon("Error closing MSGAPI", 5);
  345.  
  346.     do_close();
  347.     return 0;
  348. }
  349.