home *** CD-ROM | disk | FTP | other *** search
- /*
- SqReset by John Gardeniers (3:632/360.70)
- This code is public domain.
-
- Resets the maximum messages, frozen messages
- and maximum age of messages to that specified
- in Squish.cfg.
-
- Will search for Squish.cfg by using the Squish
- environement variable, followed by a search of
- the current directory, then the path.
-
- Tested only with Turbo C++ v3.0
-
- This version is fully self contained, requiring
- no external programs to function but requires
- the MSGAPI headers and library to be available
- at compile time. To use the library as it
- arrives in the developer's kit the program must
- be compiled with the large memory model.
-
- To skip over passthrough areas use "SqReset -P".
- to report only the present settings use "SqReset -R".
-
- If a log if defined in Squish.cfg SqReset will
- use that log as well.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <process.h>
- #include <dir.h>
- #include <dos.h>
- #include <io.h>
- #include <msgapi.h>
-
- #define BRAG "SQRESET v4.4 - public domain by John Gardeniers (3:632/360.70)"
-
- /* A nice, generous buffer size */
- #define BUF 1025
-
- FILE *log;
-
- /* Used by MSGAPI */
- struct _minf minf;
-
- /* TC complained about needing this */
- char *strupr(char *);
-
- /* Output to the log file (if open) */
- void log_it(char *msg)
- {
- printf("%s", msg);
-
- if(log)
- fputs(msg, log);
- }
-
- /* Close the logfile */
- void do_close(void)
- {
- if(log)
- {
- fputs("SqReset end\n", log);
- fclose(log);
- }
- }
-
- /* Error message and abort */
- void abandon(char *text, char code)
- {
- log_it(text);
- log_it("... Exiting.\nBetter run SqFix.\n");
- do_close();
- exit(code);
- }
-
- /* Convert a set of bytes to a long */
- dword make_long(char *bytes)
- {
- union {
- char Bytes[4];
- dword Long;
- } conv;
-
- strncpy(conv.Bytes, bytes, 4);
- return conv.Long;
- }
-
- /* Strip leading white space from the string */
- char *kill_white(char *string)
- {
- char *ptr;
-
- ptr=string;
-
- while(*ptr==' ' || *ptr=='\t')
- ptr++;
-
- return ptr;
- }
-
- /* Isolate the next "word", eliminating leading white space */
- char *nextword(char *text)
- {
- char *chr;
-
- chr=text;
-
- /* Search for the first space or tab character, or end of string */
- for(; chr; chr++)
- {
- if(*chr==' ' || *chr=='\t' || *chr==0)
- break;
- }
-
- if(*chr)
- {
- *chr++=0;
- chr=kill_white(chr);
- }
-
- return chr;
- }
-
- void syntax(void)
- {
- puts(BRAG);
- puts("\nA utility to set Squish message area settings");
- puts("according to those specified in Squish.cfg\n");
- puts("Syntax: SqReset [-P] [-R]");
- puts(" -P will skip passthrough areas");
- puts(" -R will report the current settings only");
- exit(0);
- }
-
- /* Main code */
- int main(int argc, char **argv)
- {
- /* These arrays are a bit generous. Better safe than sorry */
- char buffer[BUF], *ptr, *string;
- char msg_area[80], days[80], msgs[80], skip[80];
- FILE *filenum, *squish_data;
- int squish, passthru, no_pass=0, report=0;
- HAREA handle;
- char sq_buff[256], text[128];
- dword skip_, msgs_, days_;
- struct date dd;
- struct time tt;
-
- if(argc>1 && strchr(argv[1], '?')!=NULL)
- syntax();
-
- memset(&log, '\0', sizeof log);
- memset(&minf, '\0', sizeof minf);
- minf.req_version=1;
- puts(BRAG);
-
- strcpy(buffer, getenv("SQUISH"));
-
- if(*buffer)
- if(buffer[strlen(buffer)-1]=='\\')
- strcat(buffer, "\\squish.cfg");
-
- else
- strcpy(buffer, searchpath("squish.cfg"));
-
- /* Locate the Squish configuration file */
- if((filenum=fopen(buffer, "rt"))==NULL)
- abandon("Squish configuration file not found", 1);
-
- /* See if we want to exclude passthroughs */
- if(argc>1)
- {
- strupr(argv[1]);
-
- if(!strcmp(argv[1], "-P"))
- no_pass++;
-
- if(!strcmp(argv[1], "-R"))
- report++;
- }
-
- /* Initialise MSGAPI */
- if(MsgOpenApi(&minf))
- abandon("Error initialising MSGAPI", 2);
-
- /* Read in the configuration file line by line */
- while(fgets(buffer, BUF, filenum)!=NULL)
- {
- /* Get rid of any trailing CR */
- if((ptr=strchr(buffer, '\n'))!=NULL)
- *ptr=0;
-
- /* Convert to uppercase and lose leading white space */
- ptr=kill_white(strupr(buffer));
-
- /* Skip past comment and blank lines */
- if(*ptr==';' || !*ptr)
- continue;
-
- /* Check for the logfile entry */
- if(!log && !strncmp(ptr, "LOGFILE", 7))
- {
- if((log=fopen(nextword(ptr), "a"))!=NULL)
- {
- getdate(&dd);
- gettime(&tt);
- fprintf(log, "\nSqReset: %d/%d/%d %2d:%02d:%02d\n",
- dd.da_day, dd.da_mon, dd.da_year,
- tt.ti_hour, tt.ti_min, tt.ti_sec);
- }
- }
-
- /* We're looking for the message base details only */
- if(!strncmp(ptr, "NETAREA", 7)
- || !strncmp(ptr, "ECHOAREA", 8)
- || !strncmp(ptr, "LOCALAREA", 9)
- || !strncmp(ptr, "DUPEAREA", 8)
- || !strncmp(ptr, "BADAREA", 7))
- {
- ptr=nextword(ptr);
-
- /* Ignore the area tag */
- if(*ptr)
- ptr=nextword(ptr);
-
- /* Get the path and root name */
- if(*ptr)
- {
- string=ptr;
- ptr=nextword(ptr);
- strcpy(msg_area, string);
- }
-
- /* Set the defaults */
- strcpy(msgs, "0");
- strcpy(skip, "0");
- strcpy(days, "0");
- squish=passthru=0;
-
- /* Search for the area's flags */
- while(*ptr++=='-')
- {
- /* If this one isn't found it's not Squish */
- if(*ptr=='$')
- {
- squish++;
- ptr++;
- }
-
- /* Isolate this parameter */
- string=ptr;
- ptr=nextword(ptr);
-
- /* Act only on the correct flags */
- switch(*string++)
- {
- case '0': /* Passthrough area */
- if(no_pass)
- passthru++;
-
- break;
-
- case 'M': /* Maximum messages */
- strcpy(msgs, string);
- break;
-
- case 'S': /* Messages to skip (freeze)*/
- strcpy(skip, string);
- break;
-
- case 'D': /* Number of days */
- strcpy(days, string);
- break;
- }
- }
-
- /* Only do this for Squish messages */
- if(squish && !passthru)
- {
- /* Tell us what was found */
- sprintf(text, " %s\n", msg_area);
- log_it(text);
- strcpy(sq_buff, msg_area);
- strcat(sq_buff, ".sqd");
- squish_data=fopen(sq_buff, "rb");
-
- if(squish_data==NULL)
- {
- strcpy(text, " Area missing\n");
-
- if(report)
- log_it(text);
-
- else
- fprintf(log, text);
- }
-
- else
- {
- fread(sq_buff, 1, 256, squish_data);
- skip_=make_long(&sq_buff[12]);
- msgs_=make_long(&sq_buff[124]);
- days_=make_long(&sq_buff[128]);
- fclose(squish_data);
-
- if(report)
- {
- sprintf(text, " Skipmsg = %ld, maxmsg = %ld, days = %ld\n",
- skip_, msgs_, days_);
- log_it(text);
- }
-
- else
- {
- fprintf(log, " Was: skipmsg = %ld, maxmsg = %ld, days = %ld\n",
- skip_, msgs_, days_);
- skip_=atol(skip);
- msgs_=atol(msgs);
- days_=atol(days);
- fprintf(log, " Now: skipmsg = %ld, maxmsg = %ld, days = %ld\n",
- skip_, msgs_, days_);
-
- /* Open the message area and fiddle with it */
- if((handle=MsgOpenArea(msg_area, MSGAREA_CRIFNEC, MSGTYPE_SQUISH))==NULL)
- abandon("Error opening the message area", 3);
-
- SquishSetMaxMsg(handle, msgs_, skip_, days_);
-
- if(MsgCloseArea(handle))
- abandon("Error closing the message area", 4);
- }
- }
- }
- }
- }
-
- /* Close and exit */
- fclose(filenum);
-
- if(MsgCloseApi())
- abandon("Error closing MSGAPI", 5);
-
- do_close();
- return 0;
- }
-