home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / RAUSM120.ZIP / RAUSM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-09  |  15.5 KB  |  460 lines

  1. #include "rausm.h"
  2. #include "ra20.h"
  3. #include <ctype.h>
  4. #include <dos.h>
  5. #include <process.h>
  6. #include <share.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <time.h>
  11.  
  12. FILE         *logfp;
  13. char         UserBbsPath[_MAX_PATH];
  14. char         ListName[_MAX_PATH];
  15. int          ReportOnly;
  16. long int     UploadCredit;
  17. long int     UploadKCredit;
  18. long int     DownloadCredit;
  19. long int     DownloadKCredit;
  20. long int     MessageCredit;
  21. long int     CallOnCredit;
  22. long int     SysOpCredit;
  23. long int     LoginDayCredit;
  24. long int     BaseCredit;
  25. char         UserFlag[4];
  26. unsigned int MaxSecurity;
  27. unsigned int MinSecurity;
  28. int          SecCount;
  29. SecData      Security[200];
  30. Hidden       *HiddenHead;
  31. SortList     *SortHead;
  32.  
  33. char *strp2c(char *p)
  34. {
  35.     static char str[100];
  36.  
  37.     memcpy(str, p+1, *p);
  38.     str[*p] = '\0';
  39.  
  40.     return &str[0];
  41. }
  42.  
  43. unsigned int change_security(long int count)
  44. {
  45.     int i;
  46.     unsigned sec;
  47.  
  48.     sec = MinSecurity;
  49.     for (i = 0; i < SecCount; i++)
  50.         if (count > Security[i].Credit)
  51.             sec = Security[i].Security > sec ? Security[i].Security : sec;
  52.     return sec;
  53. }
  54.  
  55. int CheckFlag(char *s)
  56. {
  57.     int i;
  58.  
  59.     for (i = 0; i < 4; i++)
  60.         if (UserFlag[i] & s[i]) return 0;
  61.     return 1;
  62. }
  63.  
  64. long int CountDay(char *last)
  65. {
  66.     static struct dosdate_t tm;
  67.     static int x = 0;
  68.     char str[3];
  69.     int mon, day, year;
  70.     long int count1, count2;
  71.  
  72.     str[0] = last[1];
  73.     str[1] = last[2];
  74.     str[2] = '\0';
  75.     mon = atoi(str);
  76.     str[0] = last[4];
  77.     str[1] = last[5];
  78.     str[2] = '\0';
  79.     day = atoi(str);
  80.     str[0] = last[7];
  81.     str[1] = last[8];
  82.     str[2] = '\0';
  83.     year = atoi(str);
  84.     if (x == 0) _dos_getdate(&tm);
  85.     count1 = (long)(day-1) + 30L * (mon-1) + 365L * (year-90);
  86.     count2 = (long)(tm.day-1) + 30L * (tm.month-1) + 365L * (tm.year-1990);
  87.  
  88.     return count2-count1;
  89. }
  90.  
  91. int isHidden(char *p)
  92. {
  93.     Hidden *temp;
  94.  
  95.     temp = HiddenHead;
  96.     while (temp != NULL) {
  97.         if (stricmp(temp->name, p) == 0) return 1;
  98.         temp = temp->next;
  99.     }
  100.     return 0;
  101. }
  102.  
  103. int usort_func(const void *a, const void *b)
  104. {
  105.     USort *pa, *pb;
  106.  
  107.     pa = (USort *)a;
  108.     pb = (USort *)b;
  109.     return (pb->no - pa->no);
  110. }
  111.  
  112. int lsort_func(const void *a, const void *b)
  113. {
  114.     LSort *pa, *pb;
  115.  
  116.     pa = (LSort *)a;
  117.     pb = (LSort *)b;
  118.     return (int)(pb->no - pa->no);
  119. }
  120.  
  121.  
  122. int main(int argc, char *argv[])
  123. {
  124.     int len, i, userno, intable;
  125.     char    *name;
  126.     FILE    *fp, *listfp;
  127.     RA2T_users  userdata;
  128.     fpos_t  pos, prepos;
  129.     long int count;
  130.     USort        *USortData[1];
  131.     LSort        *LSortData[6];
  132.     SortList     *temp;
  133.     struct tm *time_now;
  134.     time_t secs_now;
  135.  
  136.     printf("RAUSM %s, RemoteAccess User Security Manager\n"
  137.            "          Copyright (c) 1992 by Tommy Wu. All rights reserved.\n", Version);
  138.  
  139.     ReadCTL(argv[0]);
  140.     len = strlen(UserBbsPath);
  141.     if (UserBbsPath[len-1] != '\\') {
  142.         UserBbsPath[len] = '\\';
  143.         UserBbsPath[len+1] = '\0';
  144.     }
  145.     strcat(UserBbsPath, "USERS.BBS");
  146.     printf(" ■ Open RemoteAccess database file [%s].\n", UserBbsPath);
  147.     if (logfp != NULL) fprintf(logfp, " ■ Open RemoteAccess database file [%s].\n", UserBbsPath);
  148.     if ((fp = _fsopen(UserBbsPath, "r+b", SH_DENYNO)) == NULL) {
  149.         fprintf(stderr, "Failed to open %s.\n", UserBbsPath);
  150.         if (logfp != NULL) fprintf(logfp, "Failed to open %s.\n", UserBbsPath);
  151.         ERR_EXIT(1);
  152.     }
  153.     USortData[0] = (USort *)calloc(sizeof(USort), 101);
  154.     if (USortData[0] == NULL) {
  155.         fprintf(stderr, "Memory not enough.\n");
  156.         if (logfp != NULL) fprintf(logfp, "Memory not enough.\n");
  157.         ERR_EXIT(2);
  158.     }
  159.     for (i = 0; i < 6; i++) {
  160.         LSortData[i] = (LSort *)calloc(sizeof(LSort), 101);
  161.         if (LSortData[i] == NULL) {
  162.             fprintf(stderr, "Memory not enough.\n");
  163.             if (logfp != NULL) fprintf(logfp, "Memory not enough.\n");
  164.             ERR_EXIT(2);
  165.         }
  166.     }
  167.  
  168.     listfp = NULL;
  169.     if (ListName[0] != '\0') {
  170.         char str[100];
  171.         struct tm *time_now;
  172.         time_t secs_now;
  173.  
  174.         if ((listfp = fopen(ListName, "wb")) == NULL) {
  175.             fprintf(stderr, "Failed to open %s.\n", ListName);
  176.             if (logfp != NULL) fprintf(logfp, "Failed to open %s.\n", ListName);
  177.             ERR_EXIT(1);
  178.         }
  179.         tzset();
  180.         time(&secs_now);
  181.         time_now = localtime(&secs_now);
  182.         strftime(str, 80, "%b %d %Y %H:%M:%S", time_now);
  183.         //               0        1         2         3         4         5         6         7
  184.         //               1234567890123456789012345678901234567890123456789012345678901234567890123456789
  185.         fprintf(listfp, " ówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówów\r\n");
  186.         fprintf(listfp, "  RemoteAccess User Security Manager ▓╬¡p╕Ω«╞¬φ\r\n");
  187.         fprintf(listfp, "  ╗s¬φñΘ┤┴ : %s\r\n", str);
  188.         fprintf(listfp, "  │╠░¬╡Ñ»┼ : %5u         │╠ºC╡Ñ»┼ : %5u\r\n", MaxSecurity, MinSecurity);
  189.         fprintf(listfp, "  Generated by RAUSM %s, written by Tommy Wu.\r\n", Version);
  190.         fprintf(listfp, " ówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówów\r\n");
  191.     }
  192.  
  193.     printf(" ■ Processing....\n");
  194.     if (logfp != NULL) fprintf(logfp, " ■ Processing....\n");
  195.  
  196.     userno = 0;
  197.     intable = 0;
  198.     while (1) {
  199.         fgetpos(fp, &pos);
  200.         fread(&userdata, sizeof(RA2T_users), 1, fp);
  201.         if (feof(fp)) break;
  202.         name = strp2c(userdata.Name);
  203.         count = 0UL;
  204.         count += MessageCredit * userdata.MsgsPosted;
  205.         count += CallOnCredit * userdata.NoCalls;
  206.         count += UploadCredit * userdata.Uploads;
  207.         count += DownloadCredit * userdata.Downloads;
  208.         count += UploadKCredit * userdata.UploadsK;
  209.         count += DownloadKCredit * userdata.DownloadsK;
  210.         count += LoginDayCredit * CountDay(userdata.LastDate);
  211.         count += SysOpCredit * (userdata.Credit-BaseCredit);
  212.  
  213.         if (isHidden(name) == 0) {
  214.             if (intable < 100) {
  215.                 USortData[0][intable].userno = userno;
  216.                 USortData[0][intable].no = userdata.MsgsPosted;
  217.                 for (i = 0; i < 6; i++) LSortData[i][intable].userno = userno;
  218.                 LSortData[0][intable].no = userdata.NoCalls;
  219.                 LSortData[1][intable].no = userdata.Uploads;
  220.                 LSortData[2][intable].no = userdata.Downloads;
  221.                 LSortData[3][intable].no = userdata.UploadsK;
  222.                 LSortData[4][intable].no = userdata.DownloadsK;
  223.                 LSortData[5][intable].no = userdata.Credit;
  224.                 intable++;
  225.             }
  226.             else {
  227.                 USortData[0][100].userno = userno;
  228.                 USortData[0][100].no = userdata.MsgsPosted;
  229.                 for (i = 0; i < 6; i++) LSortData[i][100].userno = userno;
  230.                 LSortData[0][100].no = userdata.NoCalls;
  231.                 LSortData[1][100].no = userdata.Uploads;
  232.                 LSortData[2][100].no = userdata.Downloads;
  233.                 LSortData[3][100].no = userdata.UploadsK;
  234.                 LSortData[4][100].no = userdata.DownloadsK;
  235.                 LSortData[5][100].no = userdata.Credit;
  236.                 qsort((void *)USortData[0], 101, sizeof(USort), usort_func);
  237.                 for (i = 0; i < 6; i++)
  238.                     qsort((void *)LSortData[i], 101, sizeof(LSort), lsort_func);
  239.             }
  240.         }
  241.  
  242.         userno++;
  243.  
  244.         if (userdata.Security <= MaxSecurity && userdata.Security >= MinSecurity
  245.                  && CheckFlag(userdata.Flags)) {
  246.             unsigned int sec;
  247.  
  248.             sec = change_security(count);
  249.             if (sec != userdata.Security) {
  250.                 if (logfp != NULL)
  251.                     fprintf(logfp, "    User : %-35s security %u %sto %u.\n", name, userdata.Security,
  252.                                           (sec > userdata.Security) ? "up" : "down", sec);
  253.                 printf("    User : %-35s security %u %sto %u.\n", name, userdata.Security,
  254.                                               (sec > userdata.Security) ? "up" : "down", sec);
  255.                 userdata.Security = sec;
  256.             }
  257.         }
  258.         if (!ReportOnly) {
  259.             fgetpos(fp, &prepos);
  260.             fsetpos(fp, &pos);
  261.             fwrite(&userdata, sizeof(RA2T_users), 1, fp);
  262.             fsetpos(fp, &prepos);
  263.         }
  264.         if (listfp != NULL) {
  265.             fprintf(listfp, " ¿╧Ñ╬¬╠⌐mªW : %-40s    ╡Ñ»┼ : %5u\r\n", name, userdata.Security);
  266.             fprintf(listfp, " ╝g½Hª╕╝╞ : %10u      ñW╜uª╕╝╞ : %10ld     ñW╢╟ª╕╝╞ : %10ld\r\n",
  267.                       userdata.MsgsPosted, userdata.NoCalls, userdata.Uploads);
  268.             fprintf(listfp, " ñU╢╟ª╕╝╞ : %10ld      ñW╢╟ó┘╝╞ : %10ld     ñU╢╟ó┘╝╞ : %10ld\r\n",
  269.                       userdata.Downloads, userdata.UploadsK, userdata.DownloadsK);
  270.             fprintf(listfp, " »╕¬°Ñ[┼v : %10ld      ┴`Ñ[┼v╝╞ : %10ld\r\n",
  271.                       userdata.Credit-BaseCredit, count);
  272.             fprintf(listfp, " ówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówówów\r\n");
  273.         }
  274.     }
  275.     if (listfp != NULL) fclose(listfp);
  276.  
  277.     if (intable < 100) {
  278.         qsort((void *)USortData[0], intable, sizeof(USort), usort_func);
  279.         for (i = 0; i < 6; i++)
  280.             qsort((void *)LSortData[i], intable, sizeof(LSort), lsort_func);
  281.     }
  282.     temp = SortHead;
  283.     while (temp != NULL) {
  284.         FILE *in, *out;
  285.         char ch;
  286.         char tagno, itemno;
  287.         char str[80];
  288.         int no;
  289.  
  290.         if ((in = _fsopen(temp->SourceName, "rb", SH_DENYNO)) == NULL) {
  291.             fprintf(stderr, "Failed to open %s.\n", temp->SourceName);
  292.             if (logfp != NULL) fprintf(logfp, "Failed to open %s.\n", temp->SourceName);
  293.             ERR_EXIT(1);
  294.         }
  295.  
  296.         if ((out = _fsopen(temp->TargetName, "wb", SH_DENYNO)) == NULL) {
  297.             fprintf(stderr, "Failed to open %s.\n", temp->TargetName);
  298.             if (logfp != NULL) fprintf(logfp, "Failed to open %s.\n", temp->TargetName);
  299.             ERR_EXIT(1);
  300.         }
  301.  
  302.         printf(" ■ Generate %s from %s.\n", temp->TargetName, temp->SourceName);
  303.         if (logfp != NULL)
  304.             fprintf(logfp, " ■ Generate %s from %s.\n", temp->TargetName, temp->SourceName);
  305.  
  306.         temp = temp->Next;
  307.  
  308.         ch = fgetc(in);
  309.         while (!feof(in)) {
  310.             if (ch != '%') {
  311.                 fputc(ch, out);
  312.                 ch = fgetc(in);
  313.                 continue;
  314.             }
  315.  
  316.             ch = fgetc(in);
  317.             if (ch == '\0' || strchr("ABCDEFG012", toupper(ch)) == NULL) {
  318.                 fputc('%', out);
  319.                 fputc(ch, out);
  320.                 ch = fgetc(in);
  321.                 continue;
  322.             }
  323.  
  324.             if (ch == '0') {
  325.                 tzset();
  326.                 time(&secs_now);
  327.                 time_now = localtime(&secs_now);
  328.                 strftime(str, 80, "%b %d %Y", time_now);
  329.                 len = strlen(str);
  330.                 fprintf(out, "%s", str);
  331.                 len -= 2;
  332.                 for (i = 0; i < len; i++) ch = fgetc(in);
  333.                 ch = fgetc(in);
  334.                 continue;
  335.             }
  336.  
  337.             if (ch == '1') {
  338.                 tzset();
  339.                 time(&secs_now);
  340.                 time_now = localtime(&secs_now);
  341.                 strftime(str, 80, "%H:%M:%S", time_now);
  342.                 len = strlen(str);
  343.                 fprintf(out, "%s", str);
  344.                 len -= 2;
  345.                 for (i = 0; i < len; i++) ch = fgetc(in);
  346.                 ch = fgetc(in);
  347.                 continue;
  348.             }
  349.  
  350.             if (ch == '2') {
  351.                 sprintf(str, "■ Generated by RAUSM %s, written by Tommy Wu.", Version);
  352.                 len = strlen(str);
  353.                 fprintf(out, "%s", str);
  354.                 len -= 2;
  355.                 for (i = 0; i < len; i++) ch = fgetc(in);
  356.                 ch = fgetc(in);
  357.                 continue;
  358.             }
  359.  
  360.             tagno = ch;
  361.             ch = fgetc(in);
  362.             if (!isdigit(ch)) {
  363.                 fputc('%', out);
  364.                 fputc(tagno, out);
  365.                 fputc(ch, out);
  366.                 ch = fgetc(in);
  367.                 continue;
  368.             }
  369.  
  370.             str[0] = ch;
  371.             ch = fgetc(in);
  372.             if (!isdigit(ch)) {
  373.                 fputc('%', out);
  374.                 fputc(tagno, out);
  375.                 fputc(str[0], out);
  376.                 fputc(ch, out);
  377.                 ch = fgetc(in);
  378.                 continue;
  379.             }
  380.  
  381.             str[1] = ch;
  382.             str[2] = '\0';
  383.             no = atoi(str)-1;
  384.             ch = fgetc(in);
  385.             if (ch == '\0' || strchr("NSABCDEFG", toupper(ch)) == NULL) {
  386.                 fputc('%', out);
  387.                 fputc(tagno, out);
  388.                 fputc(str[0], out);
  389.                 fputc(str[1], out);
  390.                 fputc(ch, out);
  391.                 ch = fgetc(in);
  392.                 continue;
  393.             }
  394.  
  395.             itemno = ch;
  396.             if (toupper(tagno) == 'A')
  397.                 fseek(fp, (long)sizeof(RA2T_users) * USortData[0][no].userno, SEEK_SET);
  398.             else
  399.                 fseek(fp, (long)sizeof(RA2T_users) * LSortData[toupper(tagno)-'B'][no].userno, SEEK_SET);
  400.             fread(&userdata, sizeof(RA2T_users), 1, fp);
  401.             switch (toupper(itemno)) {
  402.                 case 'N':   // Name
  403.                     strcpy(str, strp2c(userdata.Name));
  404.                     str[24] = '\0';
  405.                     fprintf(out, "%-24s", str);
  406.                     for (i = 0; i < 19; i++) fgetc(in);
  407.                     break;
  408.                 case 'S':
  409.                     sprintf(str, "%5u", userdata.Security);
  410.                     fprintf(out, "%s", str);
  411.                     break;
  412.                 case 'A':
  413.                     sprintf(str, "%5u", userdata.MsgsPosted);
  414.                     fprintf(out, "%s", str);
  415.                     break;
  416.                 case 'B':
  417.                     sprintf(str, "%10ld", userdata.NoCalls);
  418.                     fprintf(out, "%s", str);
  419.                     for (i = 0; i < 5; i++) fgetc(in);
  420.                     break;
  421.                 case 'C':
  422.                     sprintf(str, "%10ld", userdata.Uploads);
  423.                     fprintf(out, "%s", str);
  424.                     for (i = 0; i < 5; i++) fgetc(in);
  425.                     break;
  426.                 case 'D':
  427.                     sprintf(str, "%10ld", userdata.Downloads);
  428.                     fprintf(out, "%s", str);
  429.                     for (i = 0; i < 5; i++) fgetc(in);
  430.                     break;
  431.                 case 'E':
  432.                     sprintf(str, "%10ld", userdata.UploadsK);
  433.                     fprintf(out, "%s", str);
  434.                     for (i = 0; i < 5; i++) fgetc(in);
  435.                     break;
  436.                 case 'F':
  437.                     sprintf(str, "%10ld", userdata.DownloadsK);
  438.                     fprintf(out, "%s", str);
  439.                     for (i = 0; i < 5; i++) fgetc(in);
  440.                     break;
  441.                 case 'G':
  442.                     sprintf(str, "%10ld", userdata.Credit);
  443.                     fprintf(out, "%s", str);
  444.                     for (i = 0; i < 5; i++) fgetc(in);
  445.                     break;
  446.             }
  447.             ch = fgetc(in);
  448.         }
  449.         fclose(in);
  450.         fclose(out);
  451.     }
  452.  
  453.     fclose(fp);
  454.     if (logfp != NULL) {
  455.         fprintf(logfp, " ■ RAUSM %s shutdown.\n\n", Version);
  456.         fclose(logfp);
  457.     }
  458.     return 0;
  459. }
  460.