home *** CD-ROM | disk | FTP | other *** search
/ The Arcade BBS / arcadebbs.zip / arcadebbs / bbstools / MODS / 1AD_001.ZIP / 1AD.C next >
Encoding:
C/C++ Source or Header  |  1994-10-17  |  10.1 KB  |  449 lines

  1. #include <stdio.h>
  2. #include <process.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dos.h>
  6. #include <dir.h>
  7. #include <time.h>
  8. #include "1ad.h"
  9. #include "1adini.h"
  10.  
  11.  
  12.  
  13.  
  14. /* global variables */
  15. net_networks_rec thisnet;                // net struct for current network
  16. net_system_list_rec thisboard;    // sys info struc for this board
  17. char netpath[85];                                // path to network files
  18.  
  19. FILE *log;                                            // the tron.log file handle
  20. FILE *mail;                                            // file for outgoing mail
  21. FILE *incomming;                                // incomming file handle
  22. time_t now;
  23.  
  24. char LOGFILE[201];
  25. char text_file_dir[201];
  26. char from[201];
  27. char title[201];
  28. char **pings;
  29. int amount_pings;
  30.  
  31. char **ok_systems;
  32. int amount_ok_systems;
  33.  
  34. ini_inforec ini_info;
  35.  
  36.  
  37. void main(int argc, char *argv[])
  38. {
  39.   struct tm *time_now;
  40.     int i, netnum;
  41.   char s[101];
  42.  
  43.   read_ini();
  44.   if(!amount_pings)
  45.     return;
  46.  
  47.   if(argc < 2)
  48.     return;
  49.  
  50.  
  51.   log = fopen(LOGFILE, "at");         // open the log file
  52.   if (!log)                           // serious problem...
  53.   {
  54.     printf("Couldn't open logfile\n");
  55.     return;
  56.   }
  57.  
  58.   now = time(NULL);                   // time at start of processing
  59.   time_now = localtime(&now);
  60.   strftime(s, 100, "Start entry at %X on %x\n", time_now);
  61.   fprintf(log, s);
  62.  
  63.  
  64.   // get info for current network from networks.dat, store in "thisnet"
  65.     netnum = atoi(&argv[1][1]);        // skip the decimal point
  66.     if (!get_net_info(netnum, &thisnet))
  67.     {
  68.         fprintf(log, "\nUnable to open network data file !\n");
  69.     printf("\nUnable to open network data file !\n");
  70.  
  71.     fprintf(log, "Done\n\n");
  72.     fclose(log);
  73.         return;
  74.     }
  75.     // make a valid path string from path in networks.dat - store to "netpath"
  76.     // this is the path where all incomming & outgoing files for this net are
  77.  
  78.     strcpy(netpath, thisnet.dir);
  79. //  for (i = 0; i < strlen(netpath); i++) // change "\" to "/"
  80. //    if (netpath[i] == 0x5c)
  81. //      netpath[i] = 0x2f;
  82. //  netpath[i] = '/';
  83. //  netpath[++i] = '\0';
  84.  
  85.  
  86.   sprintf(s, "%sP9.NET", netpath);
  87.   mail = fopen(s, "ab");
  88.   if(!mail)
  89.   {
  90.     printf("Couldn't open P9.NET");
  91.     fclose(log);
  92.     return;
  93.   }
  94.  
  95.   get_board_info(&thisboard, thisnet.sysnum, 1); // get info for this board
  96.     checklocal();                                    // process the incomming "local.net"
  97.  
  98.   fprintf(log, "Done\n\n");
  99.     fclose(log);
  100.   fclose(mail);
  101. }
  102.  
  103. // process incomming "local.net" file
  104. void checklocal(void)
  105. {
  106.     char tmppath[80];                            // scratch path buffer
  107.     FILE *in;                                            // handle for incomming local.net
  108.     net_header_rec temp;                    // current net packet header
  109.   long this_pos, next_pos;
  110.  
  111.  
  112.   sprintf(tmppath, "%slocal.net", netpath);
  113.   in = fopen(tmppath, "rb+wb");
  114.     if (!in)
  115.         return;                                            // no local.net - nothing to do !
  116.  
  117.   fprintf(log, "\nProcessing %s at %s", tmppath, ctime(&now));
  118.   printf("\nProcessing %s at %s", tmppath, ctime(&now));
  119.  
  120.  
  121. #ifdef DEBUG
  122.     fprintf(log, "   TO\t  FROM\t\t  DATE\t\t\tTYPE\t\tSIZE\n");
  123. #endif
  124.  
  125.  
  126.  
  127.     // read a net header rec, store in temp
  128.   while(1)
  129.     {
  130.     this_pos = ftell(in);
  131.     if(fread((void *)&temp, sizeof(net_header_rec), 1, in) == NULL)
  132.       break;
  133.  
  134.         fseek(in, temp.list_len * 2, SEEK_CUR);    // seek past list
  135.     next_pos = ftell(in);
  136.  
  137.  
  138. #ifdef DEBUG
  139.     fprintf(log, "%3d@%-4d %3d@%-4d\t%s", temp.touser, temp.tosys, temp.fromuser, temp.fromsys, daten_to_date(temp.daten));
  140.     if (temp.main_type < 0x01c)
  141.       fprintf(log, "\t%x/%u %s", temp.main_type, temp.minor_type, types[temp.main_type]);
  142.         else
  143.       fprintf(log, "\t%x/%d UNKNOWN !", temp.main_type, temp.minor_type);
  144.         fprintf(log, "\t%ld (%d)\n", temp.length, temp.method);
  145. #endif
  146.  
  147.  
  148.     if(sub_type_allowed(temp.main_type, temp.minor_type, temp.fromsys, temp.fromuser) == 0)
  149.     {
  150.       fseek(in, this_pos, SEEK_SET);
  151.       temp.main_type = 0xffff;       /* set to deleted (65535) */
  152.       fwrite((void *)&temp, sizeof(net_header_rec), 1, in);
  153.       fseek(in, next_pos, SEEK_SET);
  154.     }
  155.  
  156.  
  157.     fseek(in, temp.length, SEEK_CUR); /* dunno - skip it */
  158.     }
  159.  
  160.   fclose(in);
  161.     fprintf(log, "** End of %s \n", tmppath);
  162.   printf("** End of %s \n", tmppath);
  163.     return;
  164. }
  165.  
  166.  
  167. int sub_type_allowed(unsigned main_type, unsigned minor_type, unsigned sys, unsigned user)
  168. {
  169.   int x = 0;
  170.   char main[201], minor[201], tfile[201];
  171.  
  172.  
  173.   while(x < amount_pings)
  174.   {
  175.     parse_unwanted_pings(pings[x], main, minor, tfile);
  176.  
  177.     if(atoi(main) == main_type && (atoi(minor) == minor_type || strcmpi(minor, "ALL") == 0))
  178.     {
  179.       if(is_sys_wanted(sys) == 0)
  180.       {
  181.         email(sys, tfile, sys, user);
  182.  
  183.         sprintf(main, "Killing %u/%u from %u, sent file %s", main_type, minor_type, sys, tfile);
  184.         ssm(0, main);
  185.         fprintf(log, "%s\n", main);
  186.  
  187.         return 0;    /* delete it, it isn't allowed */
  188.       }
  189.       else
  190.       {
  191.         sprintf(main, "Allowed %u/%u from %u to pass through", main_type, minor_type, sys);
  192.         ssm(0, main);
  193.         fprintf(log, "%s\n", main);
  194.         return 1;    /* if system is wanted, return 1 */
  195.       }
  196.     }
  197.     ++x;
  198.   }
  199.   return 1;    /* it is an allowed subtype */
  200. }
  201.  
  202. int is_sys_wanted(unsigned sys)
  203. {
  204.   int x=0;
  205.  
  206.   while(x < amount_ok_systems)
  207.   {
  208.     if(atoi(ok_systems[x])==sys)
  209.       return 1;
  210.  
  211.     ++x;
  212.   }
  213.   return 0;      /* not in the list of systems that are allowed to ping */
  214. }
  215.  
  216.  
  217. void parse_unwanted_pings(char *unwanted, char *main, char *minor, char *tfile)
  218. {
  219.   char *tmp1, *tmp2;
  220.  
  221.   tmp1 = strchr(unwanted, '/');
  222.  
  223.   if(!tmp1)
  224.   {
  225.     strcpy(main, unwanted);
  226.     minor[0] = 0;
  227.     tfile[0] = 0;
  228.     return;
  229.   }
  230.  
  231.   tmp1[0] = 0;
  232.   ++tmp1;
  233.  
  234.   strcpy(main, unwanted);
  235.  
  236.   tmp2 = strchr(tmp1, '/');
  237.   if(!tmp2)
  238.   {
  239.     strcpy(minor, tmp1);
  240.     tfile[0] = 0;
  241.     return;
  242.   }
  243.  
  244.   tmp2[0] = 0;
  245.   ++tmp2;
  246.  
  247.   strcpy(minor, tmp1);
  248.   strcpy(tfile, tmp2);
  249. }
  250.  
  251.  
  252.  
  253.  
  254. /*** following function ripped off from uedit.c ***/
  255. unsigned char *daten_to_date(long dt)
  256. {
  257.     struct date d;
  258.     struct time t;
  259.     static unsigned char s[9];
  260.  
  261.     unixtodos(dt, &d, &t);
  262.     sprintf(s, "%02d/%02d/%02d", d.da_mon, d.da_day, (d.da_year - 1900));
  263.     return (s);
  264. }
  265.  
  266.  
  267. int get_net_info(int num, net_networks_rec * rec)
  268. {
  269.     FILE *in;
  270.     in = fopen("data/networks.dat", "rb");
  271.     if (!in)
  272.         return 0;
  273.     fseek(in, (long) (num * sizeof(net_networks_rec)), SEEK_SET);
  274.     fread(rec, sizeof(net_networks_rec), 1, in);
  275.     fclose(in);
  276.     return 1;
  277. }
  278.  
  279. void ssm(unsigned sys, char *msg)
  280. {
  281.   net_header_rec h;
  282.   FILE *locmail;
  283.   char locpath[90];
  284.  
  285.   h.touser = 1;
  286.   if (sys)                      // if sys == 0, it's a local ssm
  287.     h.tosys = sys;
  288.   else
  289.     h.tosys = thisboard.sysnum;
  290.   h.fromsys = thisboard.sysnum;
  291.   h.fromuser = 1;
  292.   h.length = (unsigned long) strlen(msg) + 1;
  293.   h.main_type = 15;
  294.   h.daten = now;
  295.   h.method = 0;
  296.   h.list_len = 0;
  297.  
  298.   if(mail)
  299.   {
  300.     fwrite(&h, sizeof(net_header_rec), 1, mail);
  301.     fwrite(msg, strlen(msg) + 1, 1, mail);
  302.   }
  303.  
  304.   return;
  305. }
  306.  
  307.  
  308. /*
  309.   Need to add code to put the From name as well as the date in the email
  310. */
  311. void email(unsigned sys, char *fname, unsigned from_sys, unsigned from_user)
  312. {
  313.   net_header_rec h;
  314.   FILE *locmail, *textfile;
  315.   char locpath[90], tf_path[101];
  316.   char *msg, the_time[101], from_line[101];
  317.   long ll;
  318.   int x;
  319.  
  320.  
  321.  
  322.   sprintf(from_line, "%s\r\n", from);
  323.  
  324.   time(&ll);
  325.   strcpy(the_time,ctime(&ll));
  326.   the_time[strlen(the_time)-1]=0;      /* remove /n added with ctime */
  327.   strcat(the_time, "\r\n");
  328.  
  329.  
  330.   sprintf(tf_path, "%s%s", text_file_dir, fname);
  331.   textfile=fopen(tf_path, "rb");
  332.   if(!textfile)
  333.   {
  334.     sprintf(locpath, "Couldn't email system %d file '%s', file didn't exist", sys, fname);
  335.     ssm(thisboard.sysnum, locpath);
  336.     return;
  337.   }
  338.   fseek(textfile, 0, SEEK_END);
  339.   h.length = ftell(textfile);
  340.   fseek(textfile, 0, SEEK_SET);
  341.  
  342.  
  343.   msg=(char *) malloc(h.length+3);
  344.   if(!msg)
  345.   {
  346.     sprintf(locpath, "Couldn't email system %d file '%s', no mem to read file", sys, fname);
  347.     ssm(thisboard.sysnum, locpath);
  348.     fclose(textfile);
  349.     return;
  350.   }
  351.  
  352.   fread(msg, h.length, 1, textfile);
  353.   msg[h.length]=0;
  354.   ++h.length;
  355.   fclose(textfile);
  356.  
  357.  
  358.   h.length += strlen(title) + 1;
  359.   h.length += strlen(from_line);
  360.   h.length += strlen(the_time);
  361.  
  362.  
  363.  
  364.   h.touser = 1;
  365.   if (sys)                      // if sys == 0, it's a local ssm
  366.     h.tosys = sys;
  367.   else
  368.     h.tosys = thisboard.sysnum;
  369.   h.fromsys = from_sys;
  370.   h.fromuser = from_user;
  371.   h.main_type = 2;
  372.   h.daten = now;
  373.   h.method = 0;
  374.   h.list_len = 0;
  375.  
  376.   if(mail)
  377.   {
  378.     fwrite(&h, sizeof(net_header_rec), 1, mail);
  379.  
  380.     fwrite(title, strlen(title) + 1, 1, mail);
  381.     fwrite(from_line, strlen(from_line), 1, mail);
  382.     fwrite(the_time, strlen(the_time), 1, mail);
  383.     fwrite(msg, strlen(msg) + 1, 1, mail);
  384.   }
  385.  
  386.   free(msg);
  387.   return;
  388. }
  389.  
  390. void read_ini(void)
  391. {
  392.   ini_inforec ini_info;
  393.   int x;
  394.  
  395.   if(!open_ini_file("1AD.INI", &ini_info))
  396.   {
  397.     amount_pings=0;
  398.     return;
  399.   }
  400.  
  401.   strcpy(text_file_dir, ".\\");
  402.   read_ini_string(&ini_info, "Main", "Text file directory", text_file_dir, 100);
  403.   x=strlen(text_file_dir);
  404.   if(text_file_dir[x] != '\\' && text_file_dir[x] != '/')
  405.     { text_file_dir[x] = '\\'; text_file_dir[x+1] = 0; }
  406.  
  407.  
  408.   strcpy(LOGFILE, "1AD.LOG");
  409.   read_ini_string(&ini_info, "Main", "Logfile", LOGFILE, 100);
  410.  
  411.  
  412.   strcpy(from, "Sysop");
  413.   read_ini_string(&ini_info, "Main", "From", from, 100);
  414.  
  415.   strcpy(title, "Your recent ping");
  416.   read_ini_string(&ini_info, "Main", "Title", title, 100);
  417.  
  418.  
  419.   pings = read_ini_indents(&ini_info, "Pings", "Unwanted");
  420.   amount_pings = count_ini_indents(&ini_info, "Pings", "Unwanted");
  421.  
  422.   ok_systems = read_ini_indents(&ini_info, "Ok systems", "Wanted");
  423.   amount_ok_systems = count_ini_indents(&ini_info, "Ok systems", "Wanted");
  424.  
  425.   if(ini_info.changes_made)
  426.     write_ini_file(&ini_info);
  427.  
  428.   free_ini_info(&ini_info);
  429. }
  430.  
  431.  
  432. int get_board_info(net_system_list_rec *node,unsigned short sys)
  433. {
  434.   FILE *net;
  435.   char fn[80];
  436.   strcpy(fn,netpath);
  437.   strcat(fn,"bbsdata.net");
  438.   net=fopen(fn,"rb");
  439.   while(fread(node,sizeof(net_system_list_rec),1,net))
  440.     if(node->sysnum == sys)
  441.     {
  442.       fclose(net);
  443.       return 1;
  444.     }
  445.   fclose(net);
  446.   return 0;
  447. }
  448.  
  449.