home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UEDIT204.ZIP / DU.C next >
Encoding:
C/C++ Source or Header  |  1993-01-22  |  12.8 KB  |  538 lines

  1.  
  2. /***************************************************************************/
  3. /*  This program, DU (DELETE USER) takes one simple commandline param:     */
  4. /* the user number to delete. An errorlevel of 1 is returned if there is a */
  5. /* major problem, otherwise 0 is returned.                                 */
  6. /*                                                                         */
  7. /* Jon R. Rickher                                                          */
  8. /* September 10, 1992                                                      */
  9. /* Tolkien 1 @3456                                                         */
  10. /*                                                                         */
  11. /***************************************************************************/
  12.  
  13. #include <sys\stat.h>
  14. #include <stdio.h>
  15. #include <io.h>
  16. #include <fcntl.h>
  17. #include <dos.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <dir.h>
  21. #include "vardec.h"
  22. #include "vars.h"
  23.  
  24. #pragma hdrstop
  25.  
  26. /***************************************************************************/
  27.  
  28.   int statusfile;
  29.   int configfile;
  30.   int userfile;
  31.   int whichuser;
  32.   int numfiles;
  33.   int numk;
  34.   char s[81], gatfn[81];
  35.   userrec tu;
  36.   statusrec status;
  37.   smalrec *smallist;
  38.   configrec syscfg;
  39.   int questused[20];
  40.   int gat_section;
  41.   short *gat;
  42.  
  43. /***************************************************************************/
  44.  
  45. void far *mallocx(unsigned long l, char *s)
  46. {
  47.   void *x;
  48.  
  49.   x=farmalloc(l);
  50.   if (!x) {
  51.     exit(1);
  52.   }
  53.   return(x);
  54. }
  55.  
  56. /***************************************************************************/
  57.  
  58. void read_user(unsigned int un, userrec *u)
  59. {
  60.   long pos;
  61.   char s[80];
  62.   int i;
  63.  
  64.   open_user();
  65.   if ((userfile<0) || (un>number_userrecs())) {
  66.     u->inact=inact_deleted;
  67.     fix_user_rec(u);
  68.     return;
  69.   }
  70.  
  71.   pos=((long) syscfg.userreclen) * ((long) un);
  72.   lseek(userfile,pos,SEEK_SET);
  73.   i=read(userfile, (void *)u, syscfg.userreclen);
  74.   if (i==-1) {
  75.     open_user();
  76.     if ((userfile<0) || (un>number_userrecs())) {
  77.       u->inact=inact_deleted;
  78.       fix_user_rec(u);
  79.       return;
  80.     }
  81.     pos=((long) syscfg.userreclen) * ((long) un);
  82.     lseek(userfile,pos,SEEK_SET);
  83.     i=read(userfile, (void *)u, syscfg.userreclen);
  84.     close_user();
  85.   }
  86.   fix_user_rec(u);
  87. }
  88.  
  89. /***************************************************************************/
  90.  
  91. void write_user(unsigned int un, userrec *u)
  92. {
  93.   long pos;
  94.   char s[80];
  95.   unsigned char oldsl;
  96.   int i;
  97.  
  98.   if (userfile==-1) {
  99.     sprintf(s,"%sUSER.LST",syscfg.datadir);
  100.     userfile=open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  101.   }
  102.   pos=((long) syscfg.userreclen) * ((long) un);
  103.   lseek(userfile,pos,SEEK_SET);
  104.   i=write(userfile, (void *)u, syscfg.userreclen);
  105.   if (i==-1) {
  106.     sprintf(s,"%sUSER.LST",syscfg.datadir);
  107.     userfile=open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  108.     pos=((long) syscfg.userreclen) * ((long) un);
  109.     lseek(userfile,pos,SEEK_SET);
  110.     i=write(userfile, (void *)u, syscfg.userreclen);
  111.     close_user();
  112.   }
  113. }
  114.  
  115. /***************************************************************************/
  116.  
  117. void save_status(void)
  118. {
  119.   char s[80];
  120.  
  121.   sprintf(s,"%sSTATUS.DAT",syscfg.datadir);
  122.   statusfile=open(s,O_RDWR | O_BINARY);
  123.   write(statusfile, (void *)(&status), sizeof(statusrec));
  124.   close(statusfile);
  125.   statusfile=-1;
  126. }
  127.  
  128. /***************************************************************************/
  129.  
  130. void dsr(char *name)
  131. {
  132.   int cp,i;
  133.   char s[81];
  134.   smalrec sr;
  135.  
  136.   cp=0;
  137.   while ((cp<status.users) && (strcmp(name,(smallist[cp].name))!=0))
  138.     ++cp;
  139.   if (strcmp(name,(smallist[cp].name))) {
  140.     return;
  141.   }
  142.   memmove(&(smallist[cp]),&(smallist[cp+1]),sizeof(smalrec)*(status.users-cp));
  143.   sprintf(s,"%sNAMES.LST",syscfg.datadir);
  144.   i=open(s,O_RDWR | O_BINARY | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
  145.   if (i<0)
  146.     return;
  147.   --status.users;
  148.   save_status();
  149.   write(i,(void *) (smallist), (sizeof(smalrec) * status.users));
  150.   close(i);
  151. }
  152.  
  153. /***************************************************************************/
  154.  
  155. void fix_user_rec(userrec *u)
  156. {
  157.   u->name[30]=0;
  158.   u->realname[20]=0;
  159.   u->callsign[6]=0;
  160.   u->phone[12]=0;
  161.   u->pw[8]=0;
  162.   u->laston[8]=0;
  163.   u->note[40]=0;
  164.   u->macros[0][80]=0;
  165.   u->macros[1][80]=0;
  166.   u->macros[2][80]=0;
  167. }
  168.  
  169. /***************************************************************************/
  170.  
  171. void close_user()
  172. {
  173.   if (userfile!=-1) {
  174.     close(userfile);
  175.     userfile=-1;
  176.   }
  177. }
  178.  
  179. /***************************************************************************/
  180.  
  181. void open_user()
  182. {
  183.   char s[81];
  184.  
  185.   if (userfile==-1) {
  186.     sprintf(s,"%sUSER.LST",syscfg.datadir);
  187.     userfile=open(s,O_RDWR | O_BINARY);
  188.     if (userfile<0) {
  189.       userfile=-1;
  190.     }
  191.   }
  192. }
  193.  
  194. /***************************************************************************/
  195.  
  196. int number_userrecs()
  197. {
  198.   open_user();
  199.   return((int) (filelength(userfile)/syscfg.userreclen)-1);
  200. }
  201.  
  202. /***************************************************************************/
  203.  
  204. void rsm(int un, userrec *u)
  205. {
  206.   shortmsgrec sm;
  207.   int i,i1,f;
  208.   char s1[81];
  209.  
  210.   if ((u->sysstatus) & sysstatus_smw) {
  211.     sprintf(s1,"%sSMW.DAT",syscfg.datadir);
  212.     f=open(s1,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  213.     i=(int) (filelength(f) / sizeof(shortmsgrec));
  214.     for (i1=0; i1<i; i1++) {
  215.       lseek(f,((long) (i1)) * sizeof(shortmsgrec),SEEK_SET);
  216.       read(f,(void *)&sm,sizeof(shortmsgrec));
  217.       if ((sm.touser==un) && (sm.tosys==0)) {
  218.         sm.touser=0;
  219.         sm.tosys=0;
  220.         sm.message[0]=0;
  221.         lseek(f,((long) (i1)) * sizeof(shortmsgrec),SEEK_SET);
  222.         write(f,(void *)&sm,sizeof(shortmsgrec));
  223.       }
  224.     }
  225.     close(f);
  226.     u->sysstatus ^= sysstatus_smw;
  227.   }
  228. }
  229.  
  230. /***************************************************************************/
  231.  
  232. int open_file(char *fn)
  233. {
  234.   int f,i;
  235.   char s[81];
  236.  
  237.   sprintf(s,"%s%s.DAT",syscfg.msgsdir,fn);
  238.   f=open(s,O_RDWR | O_BINARY);
  239.   if (f<0) {
  240.     f=open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  241.     for (i=0; i<2048; i++)
  242.       gat[i]=0;
  243.     write(f,(void *)gat,4096);
  244.     strcpy(gatfn,fn);
  245.     gat_section=0;
  246.   }
  247.   if (strcmp(gatfn,fn)) {
  248.     lseek(f,0L,SEEK_SET);
  249.     read(f,(void *)gat,4096);
  250.     strcpy(gatfn,fn);
  251.     gat_section=0;
  252.   }
  253.   return(f);
  254. }
  255.  
  256. /***************************************************************************/
  257.  
  258. #define GATSECLEN (4096L+2048L*512L)
  259. #define MSG_STARTING (((long)gat_section)*GATSECLEN + 4096)
  260.  
  261. /***************************************************************************/
  262.  
  263. void set_gat_section(int f, int section)
  264. {
  265.   long l,l1;
  266.   int i;
  267.  
  268.   if (gat_section!=section) {
  269.     l=filelength(f);
  270.     l1=((long)section)*GATSECLEN;
  271.     if (l<l1) {
  272.       chsize(f,l1);
  273.       l=l1;
  274.     }
  275.     lseek(f,l1,SEEK_SET);
  276.     if (l<(l1+4096)) {
  277.       for (i=0; i<2048; i++)
  278.         gat[i]=0;
  279.       write(f,(void *)gat, 4096);
  280.     } else {
  281.       read(f,(void *)gat, 4096);
  282.     }
  283.     gat_section=section;
  284.   }
  285. }
  286.  
  287. /***************************************************************************/
  288.  
  289. void save_gat(int f)
  290. {
  291.   long l;
  292.  
  293.   l=((long)gat_section)*GATSECLEN;
  294.   lseek(f,l,SEEK_SET);
  295.   write(f,(void *)gat,4096);
  296. }
  297.  
  298. /***************************************************************************/
  299.  
  300. void remove_link(messagerec *m1, char *aux)
  301. {
  302.   messagerec m;
  303.   char s[81],s1[81];
  304.   int f;
  305.   long csec,nsec;
  306.  
  307.   m=*m1;
  308.   strcpy(s,syscfg.msgsdir);
  309.   switch(m.storage_type) {
  310.     case 0:
  311.     case 1:
  312.       ltoa(m.stored_as,s1,16);
  313.       if (m.storage_type==1) {
  314.         strcat(s,aux);
  315.         strcat(s,"\\");
  316.       }
  317.       strcat(s,s1);
  318.       unlink(s);
  319.       break;
  320.     case 2:
  321.       f=open_file(aux);
  322.       set_gat_section(f,m.stored_as/2048);
  323.       csec=m.stored_as % 2048;
  324.       while ((csec>0) && (csec<2048)) {
  325.         nsec=(long) gat[csec];
  326.           gat[csec]=0;
  327.           csec=nsec;
  328.       }
  329.       save_gat(f);
  330.       close(f);
  331.       break;
  332.     default:
  333.       /* illegal storage type */
  334.       break;
  335.   }
  336. }
  337.  
  338. /***************************************************************************/
  339.  
  340. void delmail(int f, int loc)
  341. {
  342.   mailrec m,m1;
  343.   userrec tu;
  344.   int rm,i,t,otf;
  345.  
  346.   lseek(f,((long) loc) * ((long) sizeof(mailrec)), SEEK_SET);
  347.   read(f,(void *)&m,sizeof(mailrec));
  348.  
  349.   rm=1;
  350.   if (m.status & status_multimail) {
  351.     t=filelength(f)/sizeof(mailrec);
  352.     otf=0;
  353.     for (i=0; i<t; i++)
  354.       if (i!=loc) {
  355.         lseek(f,((long)i)*((long)sizeof(mailrec)),SEEK_SET);
  356.         read(f,(void *)&m1,sizeof(mailrec));
  357.         if ((m.msg.stored_as==m1.msg.stored_as) && (m.msg.storage_type==m1.msg.storage_type) && (m1.daten!=0xffffffff))
  358.           otf=1;
  359.       }
  360.     if (otf)
  361.       rm=0;
  362.   }
  363.  
  364.   if (rm)
  365.     remove_link(&m.msg,"EMAIL");
  366.  
  367.   if (m.tosys==0) {
  368.     read_user(m.touser,&tu);
  369.     if (tu.waiting) {
  370.       --tu.waiting;
  371.       write_user(m.touser,&tu);
  372.       close_user();
  373.     }
  374.   }
  375.  
  376.   lseek(f,((long) loc) * ((long) sizeof(mailrec)), SEEK_SET);
  377.   m.touser=0;
  378.   m.tosys=0;
  379.   m.daten=0xffffffff;
  380.   m.msg.storage_type=0;
  381.   m.msg.stored_as=0xffffffff;
  382.   write(f,(void *)&m,sizeof(mailrec));
  383. }
  384.  
  385. /***************************************************************************/
  386.  
  387. void deluser(int un)
  388. {
  389.   userrec u;
  390.   int i,i1,f,n;
  391.   mailrec m;
  392.   char fn[81];
  393.   votingrec v;
  394.   voting_response vr;
  395.  
  396.   read_user(un,&u);
  397.   rsm(un,&u);
  398.   dsr(u.name);
  399.   u.inact |= inact_deleted;
  400.   u.waiting=0;
  401.   write_user(un,&u);
  402.   sprintf(fn,"%sEMAIL.DAT",syscfg.datadir);
  403.   f=open(fn,O_RDWR | O_BINARY, S_IREAD | S_IWRITE);
  404.   if (f>0) {
  405.     i1=filelength(f)/sizeof(mailrec);
  406.     for (i=0; i<i1; i++) {
  407.       lseek(f,((long) i) * sizeof(mailrec), SEEK_SET);
  408.       read(f,(void *)(&m),sizeof(mailrec));
  409.       if (((m.tosys==0) && (m.touser==un)) ||
  410.           ((m.fromsys==0) && (m.fromuser==un))) {
  411.         delmail(f,i);
  412.       }
  413.     }
  414.   }
  415.   close(f);
  416.   sprintf(fn,"%sVOTING.DAT",syscfg.datadir);
  417.   f=open(fn,O_RDWR | O_BINARY, S_IREAD | S_IWRITE);
  418.   n=(int) (filelength(f) / sizeof(votingrec)) -1;
  419.   for (i=0; i<20; i++)
  420.     if (u.votes[i]) {
  421.       if (i<=n) {
  422.         lseek(f,((long) i) * sizeof(votingrec), SEEK_SET);
  423.         read(f,(void *)&v,sizeof(votingrec));
  424.         vr=v.responses[u.votes[i]-1];
  425.         vr.numresponses--;
  426.         v.responses[u.votes[i]-1]=vr;
  427.         lseek(f,((long) i) * sizeof(votingrec), SEEK_SET);
  428.         write(f,(void *)&v,sizeof(votingrec));
  429.       }
  430.       u.votes[i]=0;
  431.     }
  432.   write_user(un,&u);
  433.   close(f);
  434. }
  435.  
  436. /***************************************************************************/
  437.  
  438. int xinit(void)
  439. {
  440.   int i,n,f;
  441.   votingrec v;
  442.  
  443.   statusfile=-1;
  444.   configfile=-1;
  445.   userfile=-1;
  446.   gatfn[0]=0;
  447.   gat_section=0;
  448.  
  449.   configfile=open("CONFIG.DAT",O_RDWR | O_BINARY);
  450.   if (configfile<0) {
  451.     return(0);
  452.   }
  453.   read(configfile,(void *) (&syscfg), sizeof(configrec));
  454.   close(configfile);
  455.  
  456.   /* update user info data */
  457.   if (!syscfg.userreclen)
  458.     syscfg.userreclen=1024;
  459.   syscfg.waitingoffset=FP_OFF(&(tu.waiting))-FP_OFF(&tu);
  460.   syscfg.inactoffset=FP_OFF(&(tu.inact))-FP_OFF(&tu);
  461. #ifdef WWIV421A
  462.   syscfg.sysstatusoffset=FP_OFF(&(tu.sysstatus))-FP_OFF(&tu);
  463.   syscfg.fuoffset=FP_OFF(&(tu.forwardusr))-FP_OFF(&tu);
  464.   syscfg.fsoffset=FP_OFF(&(tu.forwardsys))-FP_OFF(&tu);
  465.   syscfg.fnoffset=FP_OFF(&(tu.net_num))-FP_OFF(&tu);
  466. #endif
  467.  
  468.   /* check for changes in userrec - hack code */
  469. #pragma warn -rch
  470.   if (sizeof(userrec)!=1024)
  471.     syscfg.userreclen=sizeof(userrec);
  472. #pragma warn +rch
  473.  
  474.   /* store the new config.dat file */
  475.   configfile=open("CONFIG.DAT",O_RDWR | O_BINARY);
  476.   write(configfile,(void *) (&syscfg), sizeof(configrec));
  477.   close(configfile);
  478.  
  479.   sprintf(s,"%sSTATUS.DAT",syscfg.datadir);
  480.   statusfile=open(s,O_RDWR | O_BINARY);
  481.   if (statusfile<0) {
  482.     return(0);
  483.   }
  484.   read(statusfile,(void *)(&status), sizeof(statusrec));
  485.   close(statusfile);
  486.  
  487.   smallist=(smalrec *) mallocx((long)syscfg.maxusers * (long)sizeof(smalrec),"");
  488.   gat=(short *) mallocx(2048 * sizeof(short),"");
  489.  
  490.   sprintf(s,"%sVOTING.DAT",syscfg.datadir);
  491.   f=open(s,O_RDWR | O_BINARY);
  492.   if (f>0) {
  493.     n=(int) (filelength(f) / sizeof(votingrec)) -1;
  494.     for (i=0; i<n; i++) {
  495.       lseek(f,(long) i * sizeof(votingrec),SEEK_SET);
  496.       read(f,(void *)&v,sizeof(votingrec));
  497.       if (v.numanswers)
  498.         questused[i]=1;
  499.     }
  500.     close(f);
  501.   }
  502.  
  503.   return(1);
  504. }
  505.  
  506. /***************************************************************************/
  507.  
  508. void main(int argc, char *argv[])
  509. {
  510.   int i;
  511.   int okay;
  512.  
  513.   if (!xinit())
  514.     exit(1);
  515.  
  516.   okay=1;
  517.  
  518.   if (argc != 2) {
  519.     printf("\r\nSyntax: DU <usernum>\r\n");
  520.     exit(1);
  521.   }
  522.  
  523.   strcpy(s,argv[1]);
  524.   if (atoi(s))
  525.     whichuser=atoi(s);
  526.   else
  527.     okay=0;
  528.  
  529.   if (!okay)
  530.     exit(1);
  531.  
  532.   read_user(1,&tu);
  533.  
  534.   deluser(whichuser);
  535. }
  536.  
  537. /***************************************************************************/
  538.