home *** CD-ROM | disk | FTP | other *** search
/ World of Ham Radio 1994 January / AMSOFT_1994.iso / packet / pbbs / cbbs / prog / mblog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-31  |  6.3 KB  |  331 lines

  1.  
  2. /*
  3.  *  MBLOG.C - 4/15/87 - Logging and Monitoring.
  4.  */
  5.  
  6. #include "mb.h"
  7.  
  8. #define ln_jstr (ln_callp + ln_time)
  9.  
  10. char *lgfile;
  11. FILE *lfl;
  12.  
  13. char *monfile;
  14.  
  15. char *hrdfile, *hrd;
  16. int  hrdmax, hrdn;
  17.  
  18. /*
  19.  *  Clean the log file.
  20.  *  Force drain of buffers, update of directory item.
  21.  */
  22.  
  23. clnlog()
  24. {
  25.   if (s_param & s_log_on)
  26.   {
  27.     fclose(lfl);
  28.     lfl = fopen(lgfile, "a+");
  29.   }
  30. }
  31.  
  32. clslog()
  33. {
  34.   if (s_param & s_log_on) fclose (lfl);
  35. }
  36.  
  37. /*
  38.  *  Open the log file.
  39.  */
  40.  
  41. opnlog()
  42. {
  43.   if (s_param & s_log_on)
  44.     if ((lfl = fopen(lgfile, "a+")) is NULL) { nofile(lgfile); exit(1); }
  45. }
  46.  
  47. /*
  48.  * Get the log date.
  49.  */
  50.  
  51. logdat()
  52. {
  53.   if (s_param & s_log_on)
  54.   {
  55.     fseek(lfl, 0L, 0);
  56.     fgets(port->line, linelen, lfl);
  57.     log_mon = 10 * (port->line[3] - '0') + (port->line[4] - '0');
  58.     fseek (lfl, 0L, 2);
  59.   }
  60. }
  61.  
  62. /*
  63.  * Change the log file at end of month.
  64.  */
  65.  
  66. chglog()
  67. {
  68.   register char *logp;
  69.   char logs[80], nlgfile[80];
  70.  
  71.   log ('X', 'Q', ' ', nullstr);
  72.   clslog();
  73.   logp = strchr(lgfile, '.');
  74.   strncpy( logs, lgfile, (int)(logp-lgfile));
  75.   logs[(int)(logp-lgfile)] = '\0';
  76.   sprintf( nlgfile, "%s%02u%s", logs, log_mon, logp);
  77.   printf( "%s renamed to %s\n", lgfile, nlgfile);
  78.   rename(lgfile, nlgfile);
  79.   opnlog();
  80.   log ('C', 'I', ' ', nullstr);
  81.   logdat();
  82. }
  83.  
  84. /*
  85.  *  Log an event.
  86.  */
  87.  
  88. log(event, fn, sfn, text)
  89. char event, fn, sfn, *text;
  90. {
  91.  
  92. /*
  93.  *  Do we log this type of event?
  94.  */
  95.  
  96.   if (!(s_param & s_log_on)) return;
  97.  
  98.   if (port->dev is p_console) if (!(s_param & s_log_loc))
  99.   switch(event)
  100.   {
  101.     case 'M' : if(!((fn is 'M') or (fn is 'S'))) return; break;
  102.     case 'X' : if(fn isnt 'Q') return; break;
  103.     case 'C' : if(fn isnt 'I') return; break;
  104.     default  : return;
  105.   }
  106.  
  107.   switch(event)
  108.   {
  109.     case 'G' : if (!(s_param & s_log_gate)) return; break;
  110.     case 'M' : if (!(s_param & s_log_msg))  return; break;
  111.     case 'F' : if (!(s_param & s_log_file)) return; break;
  112.   }
  113.  
  114. /*
  115.  *  Yes, so log it.
  116.  */
  117.  
  118.   curtim();
  119.   fprintf(lfl, "%c%s%s%c%c %s\n", event, l_date, l_time, fn, sfn, text);
  120. }
  121.  
  122. /*
  123.  *  Monitoring ...
  124.  */
  125.  
  126. clsmon()
  127. {
  128.   register short  i;
  129.   register char  *c;
  130.   register PORTS *p;
  131.  
  132. /*
  133.  *  Write out the heard log.
  134.  */
  135.  
  136.   if ((port->fl = fopen(hrdfile, "w")) isnt NULL)
  137.   {
  138.     sort(hrd, hrdn, ln_call, tmp->scr);
  139.     for (i = 0, c = hrd; i < hrdn; i++, c += ln_call)
  140.       fprintf(port->fl, "%6.6s\n", c);
  141.     fclose(port->fl);
  142.   }
  143.  
  144. /*
  145.  *  Write out the J list for each port.
  146.  */
  147.  
  148.   if ((port->fl = fopen(monfile, "w")) isnt NULL)
  149.   {
  150.     for (p = porthd; p isnt NULL; p = p->next)
  151.     {
  152.       for (i = 0, c = p->heard; i < p->nhrd; i++, c += ln_jstr)
  153.       fprintf (port->fl, "%13.13s\n", c);
  154.       fprintf (port->fl, "*** EOF\n");
  155.     }
  156.     fprintf (port->fl, "%s%s\n", l_date, l_time);
  157.     fclose (port->fl);
  158.   }
  159. }
  160.  
  161. /*
  162.  *  Open "heard log" and "monitor log".
  163.  */
  164.  
  165. opnmon()
  166. {
  167.   register char *i, *j, *st;
  168.   register PORTS *p;
  169.  
  170. /*
  171.  *  Open the "heard list" file.
  172.  */
  173.  
  174.   if ((hrd = (char *)malloc(ln_call * hrdmax)) is NULL) errall();
  175.  
  176.   hrdn = 0;
  177.  
  178.   if ((port->fl = fopen(hrdfile, "r")) isnt NULL)
  179.   {
  180.     i = hrd;
  181.     while((hrdn < hrdmax) and (fgets (port->line, linelen, port->fl) isnt NULL))
  182.     {
  183.       pcall(i, port->line);
  184.       hrdn++; i += ln_call;
  185.     }
  186.     fclose(port->fl);
  187.     sprintf(port->line, "%d calls in %s", hrdn, hrdfile); outstr(port->line);
  188.     if (hrdn is hrdmax) outstr(" (File is full.)");
  189.     outchar('\n');
  190.   }
  191.  
  192. /*
  193.  *  Open the monitor file and read the J list for each port.
  194.  */
  195.  
  196.   if ((port->fl = fopen(monfile, "r")) isnt NULL)
  197.   {
  198.     for (p = porthd; p isnt NULL; p = p->next)
  199.     {
  200.       st = fgets (port->line, linelen, port->fl);
  201.       j = p->heard;
  202.       while (!iseof(port->line) and (st isnt NULL))
  203.       {
  204.         if (p->nhrd < p->maxhrd)
  205.         {
  206.           for (i = port->line; i < port->line + ln_jstr;) *j++ = *i++;
  207.           p->nhrd++;
  208.         }
  209.         st = fgets (port->line, linelen, port->fl);
  210.       }
  211.     }
  212.     fclose(port->fl);
  213.   }
  214. }
  215.  
  216. /*
  217.  *  Add call in monitored packet to J list of current port.
  218.  */
  219.  
  220. monitor()
  221. {
  222.   register short l1, l2;
  223.  
  224.   if (port->heard is NULL) return;
  225.  
  226.   for (l1 = 0; l1 < ln_callp; l1++) if (port->line[l1] is '>')
  227.   {
  228.     if (l1 < 4) return;
  229.  
  230.     port->line[l1] = '\0';
  231.  
  232.     for (l2 = 1; l2 < 10; l2++)
  233.     if ((port->line[l1 + l2] is ':') or
  234.         (port->line[l1 + l2] is ',') or
  235.         (port->line[l1 + l2] is ' ') or
  236.         (port->line[l1 + l2] is '\0'))
  237.     {
  238.       if (l2 > 4) { addcall(port->line, port); addhrd(); }
  239.       return;
  240.     }
  241.     return;
  242.   }
  243. }
  244.  
  245. /*
  246.  *  Add call to heard list.
  247.  */
  248.  
  249. addhrd()
  250. {
  251.   register int i;
  252.   register char *p;
  253.  
  254.   if (hrdn >= hrdmax) return;
  255.   pcall(tmp->scr, port->line);
  256.  
  257.   for (i = 0, p = hrd; i < hrdn; i++, p += ln_call)
  258.   if (matchn(tmp->scr, p, ln_call)) return;
  259.  
  260.   strncpy(hrd + (ln_call * hrdn++), tmp->scr, ln_call);
  261. }
  262.  
  263. /*
  264.  *  Add call to J list.
  265.  */
  266.  
  267. addcall(cp, p)
  268. char *cp;
  269. PORTS *p;
  270. {
  271.   register char  *k;
  272.   register short i;
  273.   register char  *sk;
  274.   register short si;
  275.  
  276.   ljsf(tmp->scr, cp, ln_callp);
  277.  
  278. /*
  279.  *  Check if this call already in list for this port.
  280.  */
  281.  
  282.   si = -1;
  283.   for (i = 0, k = p->heard; i < p->nhrd; i++, k += ln_jstr)
  284.   if (matchn(tmp->scr, k, ln_callp)) { si = i; sk = k; }
  285.  
  286. /*
  287.  *  If not already in the list ...
  288.  */
  289.  
  290.   if (si is -1)
  291.   {
  292.     si = p->nhrd; sk = p->heard + ln_jstr * p->nhrd;
  293.     if (p->nhrd < p->maxhrd) p->nhrd++; else { si--; sk -= ln_jstr; }
  294.   }
  295.  
  296. /*
  297.  *  Put call at top of list for this port.
  298.  */
  299.  
  300.   for (; si; si--, sk -= ln_jstr) strncpy (sk, sk - ln_jstr, ln_jstr);
  301.   curtim();
  302.   strncpy(p->heard + ln_callp, l_time, ln_time);
  303.   strncpy(p->heard, tmp->scr, ln_callp);
  304. }
  305.  
  306. /*
  307.  *  J command, print list of calls heard.
  308.  */
  309.  
  310. prthrd()
  311. {
  312.   register short i;
  313.   register char *j;
  314.   register PORTS *p;
  315.  
  316.   if (port->opt2 is ' ') { shports(); return; }
  317.  
  318.   if ((p = findport(port->opt2)) is NULL) { prtx(mnport); return; }
  319.  
  320.   sprintf(tmp->scr, "Port %c, %s\n", p->id, p->name);
  321.   outstr(tmp->scr);
  322.  
  323.   if (!(p->nhrd)) { prtx(mfind); return; }
  324.  
  325.   for (i = 0, j = p->heard; i < p->nhrd; i++, j += ln_jstr)
  326.   {
  327.     sprintf(tmp->scr, "%9.9s %4.4s\n", j, j + ln_callp);
  328.     outstr(tmp->scr);
  329.   }
  330. }
  331.