home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / ftp.vapor.com / microdot-1 / md1_src_02.lzx / misc.c < prev    next >
C/C++ Source or Header  |  2014-05-19  |  13KB  |  510 lines

  1. /*=============================================================================
  2.  
  3.                               HydraCom Version 1.00
  4.  
  5.                          A sample implementation of the
  6.                    HYDRA Bi-Directional File Transfer Protocol
  7.  
  8.                              HydraCom was written by
  9.                    Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT
  10.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  11.  
  12.                        The HYDRA protocol was designed by
  13.                  Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT and
  14.                              Joaquim H. Homrighausen
  15.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  16.  
  17.  
  18.   Revision history:
  19.   06 Sep 1991 - (AGL) First tryout
  20.   .. ... .... - Internal development
  21.   11 Jan 1993 - HydraCom version 1.00, Hydra revision 001 (01 Dec 1992)
  22.  
  23.  
  24.   For complete details of the Hydra and HydraCom licensing restrictions,
  25.   please refer to the license agreements which are published in their entirety
  26.   in HYDRACOM.C and LICENSE.DOC, and also contained in the documentation file
  27.   HYDRACOM.DOC
  28.  
  29.   Use of this file is subject to the restrictions contained in the Hydra and
  30.   HydraCom licensing agreements. If you do not find the text of this agreement
  31.   in any of the aforementioned files, or if you do not have these files, you
  32.   should immediately contact LENTZ SOFTWARE-DEVELOPMENT and/or Joaquim
  33.   Homrighausen at one of the addresses listed below. In no event should you
  34.   proceed to use this file without having accepted the terms of the Hydra and
  35.   HydraCom licensing agreements, or such other agreement as you are able to
  36.   reach with LENTZ SOFTWARE-DEVELOMENT and Joaquim Homrighausen.
  37.  
  38.  
  39.   Hydra protocol design and HydraCom driver:         Hydra protocol design:
  40.   Arjen G. Lentz                                     Joaquim H. Homrighausen
  41.   LENTZ SOFTWARE-DEVELOPMENT                         389, route d'Arlon
  42.   Langegracht 7B                                     L-8011 Strassen
  43.   3811 BT  Amersfoort                                Luxembourg
  44.   The Netherlands
  45.   FidoNet 2:283/512, AINEX-BBS +31-33-633916         FidoNet 2:270/17
  46.   arjen_lentz@f512.n283.z2.fidonet.org               joho@ae.lu
  47.  
  48.   Please feel free to contact us at any time to share your comments about our
  49.   software and/or licensing policies.
  50.  
  51. =============================================================================*/
  52.  
  53. #include "hydracom.h"
  54.  
  55. #ifdef AMIGA
  56. #include <dos/dosextens.h>
  57. #include <clib/dos_protos.h>
  58. #endif    /* AMIGA */
  59.  
  60. static char *chatstart = "\007\007 * Chat mode start\r\n";
  61. static char *chatend   = "\007\007\r\n * Chat mode end\r\n";
  62. static char *chattime  = "\007\007\r\n * Chat mode end -- timeout\r\n";
  63.  
  64. //#include <libraries/mui.h>
  65.  
  66. #include "microdot.h"
  67.  
  68. #include <constructor.h>
  69.  
  70. extern void hyd_addchatin( char *);
  71.  
  72. void loc_puts (char *s)
  73. {
  74.     int l;
  75.     char newline[ 256 ], *p;
  76.     int ch;
  77.  
  78.     while( *s )
  79.     {
  80.         p = newline;
  81.         while( *s && *s != '\n' )
  82.         {
  83.             ch = *s++;
  84.             if( ch == '\r' )
  85.             {
  86.                 s++;
  87.                 continue;
  88.             }
  89.             if( ch == 0x7 )
  90.                 DisplayBeep( NULL );
  91.             else
  92.                 *p++ = ch;
  93.         }
  94.         *p = 0;
  95.         if( *s )
  96.             s++;
  97.  
  98.         if( newline[á0 ]á)
  99.             hyd_addchatin( newline );
  100.     }
  101. }/*loc_puts()*/
  102.  
  103. extern void enablechat( int );
  104.  
  105. static char cb1[ 256 ], cb2[á256 ];
  106. char *chatbuffer = cb1;
  107.  
  108. /*static char *cb1, *cb2;
  109. char *chatbuffer;
  110.  
  111. CONSTRUCTOR_P(initcb,200)
  112. {
  113.     cb1 = AllocMem( 512, MEMF_CLEAR );
  114.     if( !cb1 )
  115.     {
  116.         outofmem();
  117.         return( 1 );
  118.     }
  119.     cb2 = cb1 + 256;
  120.     chatbuffer = cb1;
  121.     return( 0 );
  122. }
  123.  
  124. DESTRUCTOR_P(initcb,200)
  125. {
  126.     if( cb1 )
  127.         FreeMem( cb1, 512 );
  128. }
  129. */
  130.  
  131. extern int warned;
  132.  
  133. int keyabort (void)
  134. {
  135. #define CHATLEN 256
  136.         boolean         esc = false;
  137.         char           *p;
  138.         word            c;
  139.  
  140.         if (chattimer > 0L) {
  141.            if (time(NULL) > chattimer) {
  142.               chattimer = lasttimer = 0L;
  143.               hydra_devsend("CON",(byte *) chattime,strlen(chattime));
  144.               loc_puts(&chattime[2]);
  145.            }
  146.            else if ((time(NULL) + 10L) > chattimer && !warned) {
  147.               loc_puts("\007\r\n * Warning: chat mode timeout in 10 seconds\r\n");
  148.               warned = true;
  149.            }
  150.         }
  151.         else if (chattimer != lasttimer) {
  152.            if (chattimer ==  0L) {
  153.               if (nobell) p = " * Remote has chat facility with bell disabled\n";
  154.               else        p = " * Remote has chat facility with bell enabled\n";
  155.  
  156.               hydra_devsend("CON",(byte *) p,(int) strlen(p));
  157.               loc_puts(" * Hydra session in progress, chat facility now available\r\n");
  158.               if( prefs.hydra_autochat )
  159.               {
  160.                      hydra_devsend("CON",(byte *) chatstart,strlen(chatstart));
  161.                      loc_puts(&chatstart[2]);
  162.                      enablechat( TRUE );
  163.                      if( prefs.hydra_nochattimeout )
  164.                         chattimer = lasttimer = MAXINT;
  165.                      else
  166.                         chattimer = lasttimer = time(NULL) + CHAT_TIMEOUT;
  167.               }
  168.            }
  169.            else if (chattimer == -1L)
  170.               loc_puts(" * Hydra session in init state, can't chat yet\r\n");
  171.            else if (chattimer == -2L)
  172.               loc_puts(" * Remote has no chat facility available\r\n");
  173.            else if (chattimer == -3L) {
  174.               if (lasttimer > 0L) loc_puts("\r\n");
  175.               loc_puts(" * Hydra session in exit state, can't chat anymore\r\n");
  176.            }
  177.            lasttimer = chattimer;
  178.         }
  179.  
  180. #if WIN_AGL
  181.         while (win_keyscan()) {
  182. #else
  183. #ifdef AMIGA
  184.         while (ConScanKey()) {
  185. #else
  186.         while (kbhit()) {
  187. #endif
  188. #endif
  189.               switch (c = get_key()) {
  190.                      case Esc:
  191.                           esc = true;
  192.                           break;
  193.  
  194.                      case Alt_C:
  195.                           if (chattimer == 0L) {
  196.                              hydra_devsend("CON",(byte *) chatstart,strlen(chatstart));
  197.                              loc_puts(&chatstart[2]);
  198.                              enablechat( TRUE );
  199.                              if( prefs.hydra_nochattimeout )
  200.                                 chattimer = lasttimer = MAXINT;
  201.                              else
  202.                                  chattimer = lasttimer = time(NULL) + CHAT_TIMEOUT;
  203.                           }
  204.                           else if (chattimer > 0L) {
  205.                              chattimer = lasttimer = 0L;
  206.                              hydra_devsend("CON",(byte *) chatend,strlen(chatend));
  207.                              enablechat( FALSE );
  208.                              loc_puts(&chatend[2]);
  209.                           }
  210.                           else
  211.                              loc_puts("\007");
  212.                           break;
  213.  
  214.               }
  215.         }
  216.  
  217.         while( chatbuffer[á0 ]á&& hydra_devsend("CON",chatbuffer,strlen( chatbuffer )))
  218.         {
  219.             chatbuffer = ( chatbuffer == cb2 ) ? cb1 : cb2;
  220.             chatbuffer[á0 ]á= 0;
  221.         }
  222.  
  223.         return (esc);
  224. }/*keyabort()*/
  225.  
  226. extern void hyd_addchatin( char *);
  227.  
  228. /*static char *tchatbuffer;
  229.  
  230. ALLOCSTATIC( tchatbuffer, 512, MEMF_CLEAR )*/
  231. static char tchatbuffer[á512 ];
  232.  
  233. void rem_chat (byte *data, word len)
  234. {
  235.     //static UBYTE buffer[ 512 ];
  236.     char *p, *p2;
  237.  
  238.     p = strchr( tchatbuffer, 0 );
  239.     memcpy( p, data, len );
  240.     p[álen ]á= 0;
  241.     sibm2iso( p );
  242.  
  243.     while( p = stpbrk( tchatbuffer, "\r\n" ) )
  244.     {
  245.         *p++ = 0;
  246.  
  247.         p2 = tchatbuffer;
  248.         while( *p2 )
  249.         {
  250.             if( *p2 == 8 || *p2 == 127 )
  251.             {
  252.                 if( p2 > tchatbuffer )
  253.                 {
  254.                     p2--;
  255.                     strcpy( p2, &p2[á2 ]á);
  256.                 }
  257.                 else
  258.                     strcpy( p2, &p2[á1 ]á);
  259.             }
  260.             else if( *p2 == 7 )
  261.             {
  262.                 strcpy( p2, &p2[á1 ]á);
  263.             }
  264.             else
  265.                 p2++;
  266.         }
  267.         if( tchatbuffer[á0 ]á)
  268.             hyd_addchatin( tchatbuffer );
  269.         strcpy( tchatbuffer, p );
  270.     }
  271. }
  272.  
  273. #if 0
  274. int parse(char *string)
  275. {
  276.         int ac = 0;
  277.         char *p;
  278.  
  279.         p = strchr(string,';');
  280.         if (p) *p = '\0';
  281. #ifndef AMIGA
  282.         strupr(string);
  283. #endif    /* AMIGA */
  284.         av[ac] = strtok(string," \t\r\n\032");
  285.  
  286.         while (av[ac]) {
  287.               if (++ac > MAXARGS) {
  288.                  message(6,"!Too many arguments!");
  289.                  endprog(2);
  290.               }
  291.               av[ac]=strtok(NULL," \t\r\n\032");
  292.         }
  293.  
  294.         return (ac);
  295. }/*parse()*/
  296. #endif
  297.  
  298. void splitpath(char *filepath,char *path,char *file)
  299. {
  300. #ifdef AMIGA
  301.     STRPTR path_name,file_name;
  302.     LONG len;
  303.  
  304.     path_name = PathPart(filepath);
  305.     file_name = FilePart(filepath);
  306.  
  307.     strcpy(file,file_name);
  308.  
  309.     len = path_name - filepath;
  310.  
  311.     memcpy(path,filepath,len);
  312.  
  313.     path[len] = 0;
  314. #else
  315.         char *p,*q;
  316.  
  317.         for (p=filepath;*p;p++) ;
  318.         while (p!=filepath && *p!=':' && *p!='\\' && *p!='/') --p;
  319.         if (*p==':' || *p=='\\' || *p=='/') ++p;        /* begin     */
  320.         q=filepath;
  321.         while (q!=p) *path++=*q++;                      /* copy path */
  322.         *path='\0';
  323.         strcpy(file,p);
  324. #endif    /* AMIGA */
  325. }/*splitpath()*/
  326.  
  327.  
  328. void mergepath(char *filepath,char *path,char *file)
  329. {
  330. #ifdef AMIGA
  331.     strcpy(filepath,path);
  332.     AddPart(filepath,file,256);
  333. #else
  334.         strcpy(filepath,path);
  335.         strcat(filepath,file);
  336. #endif    /* AMIGA */
  337. }/*mergepath()*/
  338.  
  339.  
  340. int fexist (char *filename)
  341. {
  342. #ifdef AMIGA
  343.     BPTR file_lock;
  344.  
  345.     if(file_lock = Lock(filename,SHARED_LOCK))
  346.     {
  347.         UnLock(file_lock);
  348.  
  349.         return(1);
  350.     }
  351.     else
  352.         return(0);
  353. #else
  354.         struct stat f;
  355.  
  356.         return ((stat(filename,&f) != -1) ? 1 : 0);
  357. #endif    /* AMIGA */
  358. }/*fexist()*/
  359.  
  360.  
  361. int get_key (void)
  362. {
  363.         return(ConGetKey());
  364. }/*get_key()*/
  365.  
  366. void resultlog (boolean xmit, char *fname, long bytes, long xfertime)
  367. {          /* Omen's DSZ compatible logfile - for RBBS-PC XFER-?.DEF reports */
  368.         FILE *fp;
  369.  
  370.         // Generate clxfer_recfile-node
  371.         if( !xmit )
  372.             addrecfile( fname, xfertime );
  373.  
  374.         if (opuslog) {
  375.            if ((fp = sfopen(opuslog,"at",DENY_WRITE)) != NULL) {
  376.               if (fname) {
  377.                  fprintf(fp, "%s %s%s %ld", xmit ? "Sent" : "Got",
  378.                              xmit ? "" : download, fname, bytes);
  379.                  if (mailer)
  380.                     fprintf(fp," %ld",xfertime);
  381.                  fprintf(fp,"\n");
  382.               }
  383.               fclose(fp);
  384.            }
  385.            else
  386.               message(3,"-Couldn't append opus log-file %s",opuslog);
  387.         }
  388.  
  389.         if (result) {
  390.            if ((fp = sfopen(result,"at",DENY_WRITE)) != NULL) {
  391.               if (fname) {
  392.                  fprintf(fp, "%c %6ld %5lu bps %4ld cps 0 errors     0 1024 %s -1\n",
  393.                              xmit ? 'H' : 'R',
  394.                              bytes, cur_speed,
  395.                              xfertime ? (bytes / xfertime) : 9999L,
  396.                              fname);
  397.               }
  398.               fclose(fp);
  399.            }
  400.            else
  401.               message(3,"-Couldn't append result-file %s",result);
  402.         }
  403. }/*resultlog()*/
  404.  
  405.  
  406. static char *mon[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  407.                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  408.  
  409. char *h_revdate (long revstamp)
  410. {
  411.         static char  buf[12];
  412.         struct tm   *t;
  413.  
  414.         t = localtime(&revstamp);
  415.         sprintf(buf, "%02ld %s %ld",
  416.                      t->tm_mday, mon[t->tm_mon], t->tm_year + 1900);
  417.  
  418.         return (buf);
  419. }/*h_revdate()*/
  420.  
  421.  
  422. extern void hyd_addlog( char* );
  423. void message (int level, char *fmt,...)
  424. {
  425.         char       buf[255];
  426.         long       tim;
  427.         struct tm *t;
  428.         va_list    arg_ptr;
  429.  
  430.         tim = time(NULL);
  431.         t = localtime(&tim);
  432.  
  433.         va_start(arg_ptr,fmt);
  434.         sprintf(buf, "%lc %02ld %03s %02ld:%02ld:%02ld %-4s ",
  435.                      *fmt, t->tm_mday, mon[t->tm_mon],
  436.                      t->tm_hour, t->tm_min, t->tm_sec, LOGID);
  437.         vsprintf(&buf[23], &fmt[1], arg_ptr);
  438.         va_end(arg_ptr);
  439.  
  440.         if (level >= loglevel && logfp)
  441.            fprintf(logfp, "%s\n", buf);
  442.  
  443. #if WIN_AGL
  444.         if (!file_win)
  445.            cprint("\r%s\n",buf);
  446.         else {
  447.            if (!log_first)
  448.               log_first = true;
  449.            else
  450.               win_putc(log_win,'\n');
  451.            win_puts(log_win,buf);
  452.         }
  453. #else
  454. #ifdef AMIGA
  455.         hyd_addlog( buf );
  456. /*        DoMethod( lv_log, MUIM_List_InsertSingle, buf, MUIV_List_Insert_Bottom );
  457.         get( lv_log, MUIA_List_Entries, &tim );
  458.         DoMethod( lv_log, MUIM_List_Jump, tim - 1 );*/
  459.  
  460. /*        if (!LogRequest)
  461.            cprint("%s\n",buf);
  462.         else
  463.            ConPrintf(LogRequest,"%s\n",buf);*/
  464. #else
  465.         if (!file_x)
  466.            cprint("\r%s\n",buf);
  467.         else {
  468.            local_x = wherex();
  469.            local_y = wherey();
  470.            window(1,2,80,6);
  471.            if (log_y) {
  472.               gotoxy(1,log_y);
  473.               putch('\n');
  474.            }
  475.            cputs(buf);
  476.            log_y = wherey();
  477.            window(1,19,80,25);
  478.            gotoxy(local_x,local_y);
  479.         }
  480. #endif
  481. #endif
  482. }/*message()*/
  483.  
  484.  
  485. void cprint (char *fmt, ...)
  486. {
  487.         char    buf[255];
  488.         va_list arg_ptr;
  489.  
  490.         va_start(arg_ptr,fmt);
  491.         vsprintf(buf, fmt, arg_ptr);
  492.         va_end(arg_ptr);
  493.  
  494. #if WIN_AGL
  495.         if (didsome)
  496.            win_puts(0,buf);
  497.         else
  498. #else
  499. #ifdef AMIGA
  500.         hyd_addlog( buf );
  501. //        DoMethod( lv_chatout, MUIM_List_InsertSingle, buf, MUIV_List_Insert_Bottom );
  502. /*        if (LocalRequest)
  503.            ConPrintf(LocalRequest,buf);
  504.         else*/
  505. #endif
  506. #endif
  507. /*           fputs(buf,stdout);*/
  508. }/*cprint()*/
  509.  
  510.