home *** CD-ROM | disk | FTP | other *** search
-
- /***************************************************************************/
- /* This program, DU (DELETE USER) takes one simple commandline param: */
- /* the user number to delete. An errorlevel of 1 is returned if there is a */
- /* major problem, otherwise 0 is returned. */
- /* */
- /* Jon R. Rickher */
- /* September 10, 1992 */
- /* Tolkien 1 @3456 */
- /* */
- /***************************************************************************/
-
- #include <sys\stat.h>
- #include <stdio.h>
- #include <io.h>
- #include <fcntl.h>
- #include <dos.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dir.h>
- #include "vardec.h"
- #include "vars.h"
-
- #pragma hdrstop
-
- /***************************************************************************/
-
- int statusfile;
- int configfile;
- int userfile;
- int whichuser;
- int numfiles;
- int numk;
- char s[81], gatfn[81];
- userrec tu;
- statusrec status;
- smalrec *smallist;
- configrec syscfg;
- int questused[20];
- int gat_section;
- short *gat;
-
- /***************************************************************************/
-
- void far *mallocx(unsigned long l, char *s)
- {
- void *x;
-
- x=farmalloc(l);
- if (!x) {
- exit(1);
- }
- return(x);
- }
-
- /***************************************************************************/
-
- void read_user(unsigned int un, userrec *u)
- {
- long pos;
- char s[80];
- int i;
-
- open_user();
- if ((userfile<0) || (un>number_userrecs())) {
- u->inact=inact_deleted;
- fix_user_rec(u);
- return;
- }
-
- pos=((long) syscfg.userreclen) * ((long) un);
- lseek(userfile,pos,SEEK_SET);
- i=read(userfile, (void *)u, syscfg.userreclen);
- if (i==-1) {
- open_user();
- if ((userfile<0) || (un>number_userrecs())) {
- u->inact=inact_deleted;
- fix_user_rec(u);
- return;
- }
- pos=((long) syscfg.userreclen) * ((long) un);
- lseek(userfile,pos,SEEK_SET);
- i=read(userfile, (void *)u, syscfg.userreclen);
- close_user();
- }
- fix_user_rec(u);
- }
-
- /***************************************************************************/
-
- void write_user(unsigned int un, userrec *u)
- {
- long pos;
- char s[80];
- unsigned char oldsl;
- int i;
-
- if (userfile==-1) {
- sprintf(s,"%sUSER.LST",syscfg.datadir);
- userfile=open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
- }
- pos=((long) syscfg.userreclen) * ((long) un);
- lseek(userfile,pos,SEEK_SET);
- i=write(userfile, (void *)u, syscfg.userreclen);
- if (i==-1) {
- sprintf(s,"%sUSER.LST",syscfg.datadir);
- userfile=open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
- pos=((long) syscfg.userreclen) * ((long) un);
- lseek(userfile,pos,SEEK_SET);
- i=write(userfile, (void *)u, syscfg.userreclen);
- close_user();
- }
- }
-
- /***************************************************************************/
-
- void save_status(void)
- {
- char s[80];
-
- sprintf(s,"%sSTATUS.DAT",syscfg.datadir);
- statusfile=open(s,O_RDWR | O_BINARY);
- write(statusfile, (void *)(&status), sizeof(statusrec));
- close(statusfile);
- statusfile=-1;
- }
-
- /***************************************************************************/
-
- void dsr(char *name)
- {
- int cp,i;
- char s[81];
- smalrec sr;
-
- cp=0;
- while ((cp<status.users) && (strcmp(name,(smallist[cp].name))!=0))
- ++cp;
- if (strcmp(name,(smallist[cp].name))) {
- return;
- }
- memmove(&(smallist[cp]),&(smallist[cp+1]),sizeof(smalrec)*(status.users-cp));
- sprintf(s,"%sNAMES.LST",syscfg.datadir);
- i=open(s,O_RDWR | O_BINARY | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
- if (i<0)
- return;
- --status.users;
- save_status();
- write(i,(void *) (smallist), (sizeof(smalrec) * status.users));
- close(i);
- }
-
- /***************************************************************************/
-
- void fix_user_rec(userrec *u)
- {
- u->name[30]=0;
- u->realname[20]=0;
- u->callsign[6]=0;
- u->phone[12]=0;
- u->pw[8]=0;
- u->laston[8]=0;
- u->note[40]=0;
- u->macros[0][80]=0;
- u->macros[1][80]=0;
- u->macros[2][80]=0;
- }
-
- /***************************************************************************/
-
- void close_user()
- {
- if (userfile!=-1) {
- close(userfile);
- userfile=-1;
- }
- }
-
- /***************************************************************************/
-
- void open_user()
- {
- char s[81];
-
- if (userfile==-1) {
- sprintf(s,"%sUSER.LST",syscfg.datadir);
- userfile=open(s,O_RDWR | O_BINARY);
- if (userfile<0) {
- userfile=-1;
- }
- }
- }
-
- /***************************************************************************/
-
- int number_userrecs()
- {
- open_user();
- return((int) (filelength(userfile)/syscfg.userreclen)-1);
- }
-
- /***************************************************************************/
-
- void rsm(int un, userrec *u)
- {
- shortmsgrec sm;
- int i,i1,f;
- char s1[81];
-
- if ((u->sysstatus) & sysstatus_smw) {
- sprintf(s1,"%sSMW.DAT",syscfg.datadir);
- f=open(s1,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
- i=(int) (filelength(f) / sizeof(shortmsgrec));
- for (i1=0; i1<i; i1++) {
- lseek(f,((long) (i1)) * sizeof(shortmsgrec),SEEK_SET);
- read(f,(void *)&sm,sizeof(shortmsgrec));
- if ((sm.touser==un) && (sm.tosys==0)) {
- sm.touser=0;
- sm.tosys=0;
- sm.message[0]=0;
- lseek(f,((long) (i1)) * sizeof(shortmsgrec),SEEK_SET);
- write(f,(void *)&sm,sizeof(shortmsgrec));
- }
- }
- close(f);
- u->sysstatus ^= sysstatus_smw;
- }
- }
-
- /***************************************************************************/
-
- int open_file(char *fn)
- {
- int f,i;
- char s[81];
-
- sprintf(s,"%s%s.DAT",syscfg.msgsdir,fn);
- f=open(s,O_RDWR | O_BINARY);
- if (f<0) {
- f=open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
- for (i=0; i<2048; i++)
- gat[i]=0;
- write(f,(void *)gat,4096);
- strcpy(gatfn,fn);
- gat_section=0;
- }
- if (strcmp(gatfn,fn)) {
- lseek(f,0L,SEEK_SET);
- read(f,(void *)gat,4096);
- strcpy(gatfn,fn);
- gat_section=0;
- }
- return(f);
- }
-
- /***************************************************************************/
-
- #define GATSECLEN (4096L+2048L*512L)
- #define MSG_STARTING (((long)gat_section)*GATSECLEN + 4096)
-
- /***************************************************************************/
-
- void set_gat_section(int f, int section)
- {
- long l,l1;
- int i;
-
- if (gat_section!=section) {
- l=filelength(f);
- l1=((long)section)*GATSECLEN;
- if (l<l1) {
- chsize(f,l1);
- l=l1;
- }
- lseek(f,l1,SEEK_SET);
- if (l<(l1+4096)) {
- for (i=0; i<2048; i++)
- gat[i]=0;
- write(f,(void *)gat, 4096);
- } else {
- read(f,(void *)gat, 4096);
- }
- gat_section=section;
- }
- }
-
- /***************************************************************************/
-
- void save_gat(int f)
- {
- long l;
-
- l=((long)gat_section)*GATSECLEN;
- lseek(f,l,SEEK_SET);
- write(f,(void *)gat,4096);
- }
-
- /***************************************************************************/
-
- void remove_link(messagerec *m1, char *aux)
- {
- messagerec m;
- char s[81],s1[81];
- int f;
- long csec,nsec;
-
- m=*m1;
- strcpy(s,syscfg.msgsdir);
- switch(m.storage_type) {
- case 0:
- case 1:
- ltoa(m.stored_as,s1,16);
- if (m.storage_type==1) {
- strcat(s,aux);
- strcat(s,"\\");
- }
- strcat(s,s1);
- unlink(s);
- break;
- case 2:
- f=open_file(aux);
- set_gat_section(f,m.stored_as/2048);
- csec=m.stored_as % 2048;
- while ((csec>0) && (csec<2048)) {
- nsec=(long) gat[csec];
- gat[csec]=0;
- csec=nsec;
- }
- save_gat(f);
- close(f);
- break;
- default:
- /* illegal storage type */
- break;
- }
- }
-
- /***************************************************************************/
-
- void delmail(int f, int loc)
- {
- mailrec m,m1;
- userrec tu;
- int rm,i,t,otf;
-
- lseek(f,((long) loc) * ((long) sizeof(mailrec)), SEEK_SET);
- read(f,(void *)&m,sizeof(mailrec));
-
- rm=1;
- if (m.status & status_multimail) {
- t=filelength(f)/sizeof(mailrec);
- otf=0;
- for (i=0; i<t; i++)
- if (i!=loc) {
- lseek(f,((long)i)*((long)sizeof(mailrec)),SEEK_SET);
- read(f,(void *)&m1,sizeof(mailrec));
- if ((m.msg.stored_as==m1.msg.stored_as) && (m.msg.storage_type==m1.msg.storage_type) && (m1.daten!=0xffffffff))
- otf=1;
- }
- if (otf)
- rm=0;
- }
-
- if (rm)
- remove_link(&m.msg,"EMAIL");
-
- if (m.tosys==0) {
- read_user(m.touser,&tu);
- if (tu.waiting) {
- --tu.waiting;
- write_user(m.touser,&tu);
- close_user();
- }
- }
-
- lseek(f,((long) loc) * ((long) sizeof(mailrec)), SEEK_SET);
- m.touser=0;
- m.tosys=0;
- m.daten=0xffffffff;
- m.msg.storage_type=0;
- m.msg.stored_as=0xffffffff;
- write(f,(void *)&m,sizeof(mailrec));
- }
-
- /***************************************************************************/
-
- void deluser(int un)
- {
- userrec u;
- int i,i1,f,n;
- mailrec m;
- char fn[81];
- votingrec v;
- voting_response vr;
-
- read_user(un,&u);
- rsm(un,&u);
- dsr(u.name);
- u.inact |= inact_deleted;
- u.waiting=0;
- write_user(un,&u);
- sprintf(fn,"%sEMAIL.DAT",syscfg.datadir);
- f=open(fn,O_RDWR | O_BINARY, S_IREAD | S_IWRITE);
- if (f>0) {
- i1=filelength(f)/sizeof(mailrec);
- for (i=0; i<i1; i++) {
- lseek(f,((long) i) * sizeof(mailrec), SEEK_SET);
- read(f,(void *)(&m),sizeof(mailrec));
- if (((m.tosys==0) && (m.touser==un)) ||
- ((m.fromsys==0) && (m.fromuser==un))) {
- delmail(f,i);
- }
- }
- }
- close(f);
- sprintf(fn,"%sVOTING.DAT",syscfg.datadir);
- f=open(fn,O_RDWR | O_BINARY, S_IREAD | S_IWRITE);
- n=(int) (filelength(f) / sizeof(votingrec)) -1;
- for (i=0; i<20; i++)
- if (u.votes[i]) {
- if (i<=n) {
- lseek(f,((long) i) * sizeof(votingrec), SEEK_SET);
- read(f,(void *)&v,sizeof(votingrec));
- vr=v.responses[u.votes[i]-1];
- vr.numresponses--;
- v.responses[u.votes[i]-1]=vr;
- lseek(f,((long) i) * sizeof(votingrec), SEEK_SET);
- write(f,(void *)&v,sizeof(votingrec));
- }
- u.votes[i]=0;
- }
- write_user(un,&u);
- close(f);
- }
-
- /***************************************************************************/
-
- int xinit(void)
- {
- int i,n,f;
- votingrec v;
-
- statusfile=-1;
- configfile=-1;
- userfile=-1;
- gatfn[0]=0;
- gat_section=0;
-
- configfile=open("CONFIG.DAT",O_RDWR | O_BINARY);
- if (configfile<0) {
- return(0);
- }
- read(configfile,(void *) (&syscfg), sizeof(configrec));
- close(configfile);
-
- /* update user info data */
- if (!syscfg.userreclen)
- syscfg.userreclen=1024;
- syscfg.waitingoffset=FP_OFF(&(tu.waiting))-FP_OFF(&tu);
- syscfg.inactoffset=FP_OFF(&(tu.inact))-FP_OFF(&tu);
- #ifdef WWIV421A
- syscfg.sysstatusoffset=FP_OFF(&(tu.sysstatus))-FP_OFF(&tu);
- syscfg.fuoffset=FP_OFF(&(tu.forwardusr))-FP_OFF(&tu);
- syscfg.fsoffset=FP_OFF(&(tu.forwardsys))-FP_OFF(&tu);
- syscfg.fnoffset=FP_OFF(&(tu.net_num))-FP_OFF(&tu);
- #endif
-
- /* check for changes in userrec - hack code */
- #pragma warn -rch
- if (sizeof(userrec)!=1024)
- syscfg.userreclen=sizeof(userrec);
- #pragma warn +rch
-
- /* store the new config.dat file */
- configfile=open("CONFIG.DAT",O_RDWR | O_BINARY);
- write(configfile,(void *) (&syscfg), sizeof(configrec));
- close(configfile);
-
- sprintf(s,"%sSTATUS.DAT",syscfg.datadir);
- statusfile=open(s,O_RDWR | O_BINARY);
- if (statusfile<0) {
- return(0);
- }
- read(statusfile,(void *)(&status), sizeof(statusrec));
- close(statusfile);
-
- smallist=(smalrec *) mallocx((long)syscfg.maxusers * (long)sizeof(smalrec),"");
- gat=(short *) mallocx(2048 * sizeof(short),"");
-
- sprintf(s,"%sVOTING.DAT",syscfg.datadir);
- f=open(s,O_RDWR | O_BINARY);
- if (f>0) {
- n=(int) (filelength(f) / sizeof(votingrec)) -1;
- for (i=0; i<n; i++) {
- lseek(f,(long) i * sizeof(votingrec),SEEK_SET);
- read(f,(void *)&v,sizeof(votingrec));
- if (v.numanswers)
- questused[i]=1;
- }
- close(f);
- }
-
- return(1);
- }
-
- /***************************************************************************/
-
- void main(int argc, char *argv[])
- {
- int i;
- int okay;
-
- if (!xinit())
- exit(1);
-
- okay=1;
-
- if (argc != 2) {
- printf("\r\nSyntax: DU <usernum>\r\n");
- exit(1);
- }
-
- strcpy(s,argv[1]);
- if (atoi(s))
- whichuser=atoi(s);
- else
- okay=0;
-
- if (!okay)
- exit(1);
-
- read_user(1,&tu);
-
- deluser(whichuser);
- }
-
- /***************************************************************************/
-