home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / bos2_xr5.zip / developr / gethist / maximus3 / getmax3.c
C/C++ Source or Header  |  1997-06-09  |  8KB  |  262 lines

  1. /* This small program imports Maximus CBCS 3.0x CALLINFO.BBS/LASTUSxx.BBS
  2.    into BT-XE's history file */
  3. /* by Alex Woick, 2:244/1351 970531 */
  4. /* Compiler used: Watcom 10.0a, tested to compile DOS16 and OS/2 v2 versions */
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <io.h>
  11. #include <ctype.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <share.h>
  15.  
  16. /* this is max.h from Maximus 3.01 "structs.zip" package */
  17. #include "max.h"
  18.  
  19. typedef struct _ADDRESS
  20. {
  21.     word Zone;
  22.     word Net;
  23.     word Node;
  24.     word Point;
  25.     char *Domain;
  26. } ADDR, *ADDRP;
  27.  
  28. #include "..\..\include\history.h"
  29.  
  30. #define YEAR(t)   (((t & 0xFE00) >> 9) + 1980) 
  31. #define MONTH(t)  ((t & 0x01E0) >> 5) 
  32. #define DAY(t)    (t & 0x001F) 
  33.  
  34. #define HOUR(t)   ((t & 0xF800) >> 11) 
  35. #define MINUTE(t) ((t & 0x07E0) >> 5) 
  36. #define SECOND(t) ((t & 0x001F) << 1) 
  37.  
  38. struct m_pointers prm;
  39. char *offsets;
  40.  
  41. int ReadMaxPrm(const char *maxprmname)
  42. {
  43.   int prmfile;
  44.   long heaplen;
  45.   long numread;
  46.  
  47.   prmfile = shopen(maxprmname, O_RDONLY|O_BINARY);
  48.   if (prmfile != -1) {
  49.     numread = read(prmfile, &prm, sizeof(struct m_pointers));
  50.     if (numread!=sizeof(struct m_pointers)) {
  51.       close(prmfile);
  52.       return -1;
  53.     }
  54.     heaplen = filelength(prmfile)-prm.heap_offset;
  55.     offsets = (char *)malloc(heaplen);
  56.     if (offsets) {
  57.       lseek(prmfile, prm.heap_offset, SEEK_SET);
  58.       read(prmfile, offsets, heaplen);
  59.       close(prmfile);
  60.       return 0;
  61.     }
  62.   }
  63.   return -1;
  64. }
  65.  
  66. char *nicepath(char *s) {
  67. /* writes a backslash to the end of the string */
  68.  
  69.   int i;
  70.  
  71.   if (s==NULL) return(s);
  72.  
  73.   i = strlen(s);
  74.   if (i > 0) {
  75.     if (s[i-1] != '\\') {
  76.       strcat(s,"\\");
  77.     }
  78.   }
  79.   return(s);
  80. }
  81.  
  82.  
  83. int GetMax3Lastcall(CALLHIST *phist, struct callinfo *pcallinfo, struct _usr *plastuser, word task)
  84. {
  85.   struct tm st;
  86.  
  87.   memset(phist, 0, sizeof(CALLHIST));
  88.  
  89.   strncpy(phist->h.b2.name,  plastuser->name,  sizeof(phist->h.b2.name));
  90.   strncpy(phist->h.b2.handle, plastuser->alias, sizeof(phist->h.b2.handle));
  91.   strncpy(phist->h.b2.city,  pcallinfo->city,  sizeof(phist->h.b2.city));      
  92.   phist->h.b2.filesup = pcallinfo->filesup;
  93.   phist->h.b2.filesdn = pcallinfo->filesdn;
  94.   phist->h.b2.kbup    = pcallinfo->kbup;
  95.   phist->h.b2.kbdn    = pcallinfo->kbdn;
  96.   phist->h.b2.calls   = pcallinfo->calls;
  97.   phist->h.b2.read    = pcallinfo->read;
  98.   phist->h.b2.posted  = pcallinfo->posted;
  99.   phist->h.b2.baud    = plastuser->delflag;
  100.  
  101.   phist->task = task;
  102.   phist->outgoing=0;
  103.   phist->calltype = CT_BBS;
  104.   phist->subclass = SC_CALLINFO;
  105.  
  106.   st.tm_sec = SECOND(pcallinfo->login.dos_st.time);
  107.   st.tm_min = MINUTE(pcallinfo->login.dos_st.time);
  108.   st.tm_hour = HOUR(pcallinfo->login.dos_st.time);
  109.   st.tm_mday = DAY(pcallinfo->login.dos_st.date);
  110.   st.tm_mon = MONTH(pcallinfo->login.dos_st.date) - 1;
  111.   st.tm_year = YEAR(pcallinfo->login.dos_st.date) - 1900;
  112.   st.tm_isdst = -1;
  113.   phist->starttime = mktime(&st);
  114.  
  115.   printf("Logon:  %02d.%02d.%02d %02d:%02d:%02d (%ul)\n", st.tm_mday, st.tm_mon, st.tm_year, st.tm_hour, st.tm_min, st.tm_sec, phist->starttime);
  116.  
  117.   st.tm_sec = SECOND(pcallinfo->logoff.dos_st.time);
  118.   st.tm_min = MINUTE(pcallinfo->logoff.dos_st.time);
  119.   st.tm_hour = HOUR(pcallinfo->logoff.dos_st.time);
  120.   st.tm_mday = DAY(pcallinfo->logoff.dos_st.date);
  121.   st.tm_mon = MONTH(pcallinfo->logoff.dos_st.date) - 1;
  122.   st.tm_year = YEAR(pcallinfo->logoff.dos_st.date) - 1900;
  123.   st.tm_isdst = -1;
  124.  
  125.   printf("Logoff: %02d.%02d.%02d %02d:%02d:%02d (%ul)\n", st.tm_mday, st.tm_mon, st.tm_year, st.tm_hour, st.tm_min, st.tm_sec, mktime(&st));
  126.  
  127.   phist->length = mktime(&st) - phist->starttime;
  128.  
  129.   printf("Length: %d seconds\n", phist->length);
  130.  
  131.   return 0;
  132. }
  133.  
  134.  
  135. void help()
  136. /* show short usage help */
  137. {
  138.   printf ("Usage:   GETMAX3 [/h:<btxe_history_file> [/m:<max.prm>] [/t:<task_number>]\n");
  139.   printf ("Example: GETMAX3 /h:P:\\BTXE\\TASK\\ALLTASKS.HST /m:L:\\MAX\\MAX.PRM /t:2\n\n");
  140.   printf ("Defaults: btexe_history_file: %%BINKLEY%%\\TASK\\CALLHIST.ALL\n");
  141.   printf ("          max.prm:            %%MAXIMUS%%\n");
  142.   printf ("          task_number:        %%TASK%%, if not defined then from max.prm\n");
  143.   exit (1);
  144. }
  145.  
  146. int main (int argc,char **argv)
  147. {
  148.     CALLHIST rec;
  149.  
  150.     char tmp[255], tmp2[255];
  151.     char *s, *opt;
  152.     char shistfname[255];
  153.     char *smaxprm;
  154.     char *stasknr;
  155.  
  156.     unsigned tasknr=0;
  157.     FILE *callerbbs;
  158.     FILE *lastuserbbs;
  159.     FILE *callhist;
  160.  
  161.     struct _usr     lastuserrec;
  162.     struct callinfo callinforec;
  163.  
  164.     int i;
  165.  
  166.     putenv("TZ=GMT0"); /* make it compatible to BT's misbehaviour        */
  167.     tzset();           /* TJW: Read TZ variable and adapt time functions */
  168.     
  169.     printf ("Maximus CBCS 3.0x to BT-XE 1.0, by Alex Woick, 2:244/1351\n");
  170.     printf ("-------------------------------------------------------------------------------\n");
  171.  
  172.     /* set the defaults */
  173.  
  174.     strcpy(shistfname, "CALLHIST.ALL");
  175.     s = getenv("BINKLEY");
  176.     if (s) {
  177.       strcpy(shistfname, s);
  178.       nicepath(shistfname);
  179.       strcat(shistfname, "TASK\\CALLHIST.ALL");
  180.     }
  181.     smaxprm = getenv("MAXIMUS");
  182.     stasknr = getenv("TASK");
  183.  
  184.     for (i=1; i<argc; i++) {
  185.       opt = argv[i];
  186.       if (strlen(opt)>3 && (opt[0]=='/' || opt[0]=='-') && opt[2]==':') {
  187.         switch (toupper(opt[1])) {
  188.         case 'H': strcpy(shistfname, opt+3);
  189.            break;
  190.         case 'M': smaxprm = opt+3;
  191.            break;
  192.         case 'T': stasknr = opt+3;
  193.            break;
  194.         default: help();
  195.           break;
  196.         }
  197.       } else
  198.         help();
  199.     }
  200.     if (stasknr) {
  201.       tasknr=atoi(stasknr);
  202.     }
  203.     printf("tasknr=%u\n", tasknr);
  204.     if (tasknr==0) {
  205.       printf("task number not given or not >=1: '%s'\n", stasknr);
  206.       exit(-1);
  207.     }
  208.  
  209.     if (ReadMaxPrm(smaxprm)) {
  210.       printf("cannot read Maximus .PRM file '%s'\n", smaxprm);
  211.       exit(-1);
  212.      }
  213.  
  214.     strcpy(tmp, PRM(sys_path));
  215.     nicepath(tmp);
  216.  
  217.     sprintf(tmp2, "%slastus%02x.bbs", &tmp, tasknr);
  218.     printf("lastuser.bbs='%s'\n", tmp2);
  219.     lastuserbbs = fopen(tmp2, "rb");
  220.     if (!lastuserbbs) {
  221.       printf("cannot open '%s'\n", tmp2);
  222.       free(offsets);
  223.       exit(-1);
  224.     }
  225.     fread(&lastuserrec, 1, sizeof(struct _usr), lastuserbbs);
  226.     fclose(lastuserbbs);
  227.  
  228.     sprintf(tmp2, "%s.bbs", PRM(caller_log));
  229.     printf("callers logfile='%s'\n", tmp2);
  230.     callerbbs = fopen(tmp2, "rb");
  231.     if (!callerbbs) {
  232.       printf("cannot open callers logfile '%s'\n", tmp2);
  233.       free(offsets);
  234.       exit(-1);
  235.     }
  236.     printf("BT XE history file='%s'\n", shistfname);
  237.     for (i=1; i<=255; i++) { /* search the last 255 entries */
  238.       fseek(callerbbs, -(long)i * (long) sizeof(struct callinfo), SEEK_END);
  239.       fread(&callinforec, 1, sizeof(struct callinfo), callerbbs);
  240.       if (callinforec.task==tasknr) { /* found the last entry in caller.bbs for this task */
  241.         printf("Found last user: %s (task=%u)\n", callinforec.name, callinforec.task);
  242.         GetMax3Lastcall(&rec, &callinforec, &lastuserrec, tasknr);
  243.         callhist = fopen (shistfname,"a+b");
  244.         if (!callhist) {
  245.           printf ("Unable to open output file '%s'\n", shistfname);
  246.           exit (-1);
  247.         }
  248.         fwrite (&rec,1, sizeof(CALLHIST), callhist);
  249.         fclose (callhist);
  250.         printf ("Done.\n");
  251.         break;
  252.       }
  253.     }
  254.     if (i==256) {
  255.       printf("Cannot find entry for last user in '%s'\n", tmp2);
  256.     }
  257.     fclose(callerbbs);
  258.     free(offsets);
  259.  
  260.     return 0;
  261. }
  262.