home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / filesbbs / dos / sbbs_src.exe / SBBS / UTI / UTIIMPRT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-25  |  8.4 KB  |  333 lines

  1. /* UTIIMPRT.C */
  2.  
  3. /* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
  4.  
  5. #include "sbbs.h"
  6. #include "uti.h"
  7. #include "crc32.h"
  8.  
  9. smb_t smb;
  10.  
  11. /****************************************************************************/
  12. /* Updates 16-bit "rcrc" with character 'ch'                                */
  13. /****************************************************************************/
  14. void ucrc16(uchar ch, ushort *rcrc) {
  15.     ushort i, cy;
  16.     uchar nch=ch;
  17.  
  18. for (i=0; i<8; i++) {
  19.     cy=*rcrc & 0x8000;
  20.     *rcrc<<=1;
  21.     if (nch & 0x80) *rcrc |= 1;
  22.     nch<<=1;
  23.     if (cy) *rcrc ^= 0x1021; }
  24. }
  25.  
  26. /****************************************************************************/
  27. /* Returns 16-crc of string (not counting terminating NULL)                 */
  28. /****************************************************************************/
  29. ushort crc16(char *str)
  30. {
  31.     int     i=0;
  32.     ushort    crc=0;
  33.  
  34. ucrc16(0,&crc);
  35. while(str[i])
  36.     ucrc16(str[i++],&crc);
  37. ucrc16(0,&crc);
  38. ucrc16(0,&crc);
  39. return(crc);
  40. }
  41.  
  42.  
  43. #define PRIVATE 0
  44. #define PUBLIC    1
  45.  
  46. #define DEBUG 0
  47.  
  48.  
  49. char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  50. char *mon[]={"Jan","Feb","Mar","Apr","May","Jun"
  51.             ,"Jul","Aug","Sep","Oct","Nov","Dec"};
  52.  
  53. void remove_re(char *str)
  54. {
  55. while(!strnicmp(str,"RE:",3)) {
  56.     strcpy(str,str+3);
  57.     while(str[0]==SP)
  58.         strcpy(str,str+1); }
  59. }
  60.  
  61. /****************************************************************************/
  62. /* Converts a date string in format MM/DD/YY into unix time format            */
  63. /****************************************************************************/
  64. time_t dstrtounix(char *str)
  65. {
  66.     struct time curtime;
  67.     struct date date;
  68.  
  69. #if DEBUG
  70. printf("\rdstrtounix           ");
  71. #endif
  72.  
  73. curtime.ti_hour=curtime.ti_min=curtime.ti_sec=0;
  74. date.da_year=1900+((str[6]&0xf)*10)+(str[7]&0xf);
  75. date.da_mon=((str[0]&0xf)*10)+(str[1]&0xf);
  76. date.da_day=((str[3]&0xf)*10)+(str[4]&0xf);
  77. return(dostounix(&date,&curtime));
  78. }
  79.  
  80. /****************************************************************************/
  81. /* Checks the disk drive for the existence of a file. Returns 1 if it       */
  82. /* exists, 0 if it doesn't.                                                 */
  83. /* Called from upload                                                       */
  84. /****************************************************************************/
  85. char fexist(char *filespec)
  86. {
  87.     struct ffblk f;
  88.  
  89. if(findfirst(filespec,&f,0)==NULL)
  90.     return(1);
  91. return(0);
  92. }
  93.  
  94. /****************************************************************************/
  95. /* This function reads files that are potentially larger than 32k.          */
  96. /* Up to one megabyte of data can be read with each call.                   */
  97. /****************************************************************************/
  98. long lread(int file, char huge *buf,long bytes)
  99. {
  100.     long count;
  101.  
  102. for(count=bytes;count>32767;count-=32767,buf+=32767)
  103.     if(read(file,(char *)buf,32767)!=32767)
  104.         return(-1L);
  105. if(read(file,(char *)buf,(int)count)!=count)
  106.     return(-1L);
  107. return(bytes);
  108. }
  109.  
  110.  
  111. int main(int argc, char **argv)
  112. {
  113.     char    str[256],to[256],from[256],title[256],*p,*buf,*outbuf;
  114.     ushort    xlat,net=0;
  115.     int     i,j,file,lzh,storage;
  116.     uint    subnum,imported=0;
  117.     ulong    l,length,lzhlen,offset,crc;
  118.     FILE    *stream;
  119.     smbmsg_t    msg;
  120.     smbstatus_t status;
  121.  
  122. PREPSCREEN;
  123.  
  124. printf("Synchronet UTIIMPRT v%s\n",VER);
  125.  
  126. if(argc<3)
  127.     exit(1);
  128.  
  129. if((argc>3 && !stricmp(argv[3],"/NETWORK"))
  130.     || (argc>4 && !stricmp(argv[4],"/NETWORK")))
  131.     net=NET_POSTLINK;
  132.  
  133. uti_init("UTIIMPRT",argc,argv);
  134.  
  135. if((file=nopen(argv[2],O_RDONLY))==-1)
  136.     bail(2);
  137. if((stream=fdopen(file,"rb"))==NULL)
  138.     bail(2);
  139.  
  140. subnum=getsubnum(argv[1]);
  141. if((int)subnum==-1)
  142.     bail(7);
  143.  
  144. sprintf(smb.file,"%s%s",sub[subnum]->data_dir,sub[subnum]->code);
  145. smb.retry_time=30;
  146. if((i=smb_open(&smb))!=0) {
  147.     errormsg(WHERE,ERR_OPEN,smb.file,i);
  148.     bail(5); }
  149.  
  150. if(filelength(fileno(smb.shd_fp))<1) {     /* Create it if it doesn't exist */
  151.     smb.status.max_crcs=sub[subnum]->maxcrcs;
  152.     smb.status.max_msgs=sub[subnum]->maxmsgs;
  153.     smb.status.max_age=sub[subnum]->maxage;
  154.     smb.status.attr=sub[subnum]->misc&SUB_HYPER ? SMB_HYPERALLOC : 0;
  155.     if((i=smb_create(&smb))!=0) {
  156.         errormsg(WHERE,ERR_CREATE,smb.file,i);
  157.         bail(5); } }
  158.  
  159. printf("\r\nImporting ");
  160.  
  161. while(!feof(stream) && !ferror(stream)) {
  162.     memset(&msg,0,sizeof(smbmsg_t));
  163.     memcpy(msg.hdr.id,"SHD\x1a",4);
  164.     msg.hdr.version=SMB_VERSION;
  165.     msg.hdr.when_imported.time=time(NULL);
  166.     msg.hdr.when_imported.zone=sys_timezone;
  167.     if(sub[subnum]->misc&SUB_AONLY)
  168.         msg.hdr.attr|=MSG_ANONYMOUS;
  169.  
  170.     if(!fgets(to,250,stream))
  171.         break;
  172.     if(!fgets(from,250,stream))
  173.         break;
  174.     if(!fgets(title,250,stream))
  175.         break;
  176.     imported++;
  177.     printf("%-5u\b\b\b\b\b",imported);
  178.     truncsp(to);
  179.     truncsp(from);
  180.     truncsp(title);
  181.  
  182.     smb_hfield(&msg,RECIPIENT,strlen(to),to);
  183.     strlwr(to);
  184.     msg.idx.to=crc16(to);
  185.  
  186.     smb_hfield(&msg,SENDER,strlen(from),from);
  187.     strlwr(from);
  188.     msg.idx.from=crc16(from);
  189.  
  190.     if(net)
  191.         i=smb_hfield(&msg,SENDERNETTYPE,2,&net);
  192.  
  193.     i=smb_hfield(&msg,SUBJECT,strlen(title),title);
  194.     strlwr(title);
  195.     remove_re(title);
  196.     msg.idx.subj=crc16(title);
  197.  
  198.     fgets(str,128,stream);     /* skip msg # */
  199.     fgets(str,128,stream);     /* ref # */
  200.     msg.hdr.thread_orig=atol(str);
  201.     fgets(str,128,stream);     /* date */
  202.     msg.hdr.when_written.time=dstrtounix(str);
  203.     fgets(str,128,stream);     /* time */
  204.     msg.hdr.when_written.time+=atoi(str)*60*60;  /* hours */
  205.     p=strchr(str,':');
  206.     if(p)
  207.         msg.hdr.when_written.time+=atoi(p+1)*60; /* mins */
  208.     fgets(str,128,stream);     /* private/public */
  209.     if(!stricmp(str,"PRIVATE"))
  210.         msg.hdr.attr|=MSG_PRIVATE;
  211.     fgets(str,128,stream);     /* Read? Y/N */
  212.     if(toupper(str[0])=='Y')
  213.         msg.hdr.attr|=MSG_READ;
  214.     fgets(str,128,stream);     /* Net? Y/N - ignore */
  215.     if(toupper(str[0])=='Y')
  216.         msg.hdr.netattr|=MSG_TYPELOCAL;
  217.     while(!feof(stream) && !ferror(stream)) {
  218.         fgets(str,128,stream);
  219.         if(!strcmp(str,"TEXT:\r\n"))
  220.             break; }
  221.  
  222.     buf=NULL;
  223.     length=0;
  224.     crc=0xffffffff;
  225.     while(!feof(stream) && !ferror(stream)) {
  226.         fgets(str,128,stream);
  227.         if(!strcmp(str,"\xff\r\n"))     /* end of text */
  228.             break;
  229.         j=strlen(str);
  230.         if((buf=REALLOC(buf,length+j+1))==NULL) {
  231.             errormsg(WHERE,ERR_ALLOC,argv[1],length+j+1);
  232.             bail(3); }
  233.         if(sub[subnum]->maxcrcs) {
  234.             for(i=0;i<j;i++)
  235.                 crc=ucrc32(str[i],crc); }
  236.         strcpy(buf+length,str);
  237.         length+=strlen(str); }
  238.     crc=~crc;
  239.  
  240.     if((i=smb_locksmbhdr(&smb))!=0) {
  241.         errormsg(WHERE,ERR_LOCK,smb.file,i);
  242.         bail(11); }
  243.  
  244.     if((i=smb_getstatus(&smb))!=0) {
  245.         errormsg(WHERE,ERR_READ,smb.file,i);
  246.         bail(12); }
  247.  
  248.     if(sub[subnum]->maxcrcs) {
  249.         i=smb_addcrc(&smb,crc);
  250.         if(i) {
  251.             printf("\nDuplicate message!\n");
  252.             FREE(buf);
  253.             smb_unlocksmbhdr(&smb);
  254.             smb_freemsgmem(&msg);
  255.             continue; } }
  256.  
  257.     if(length>=2 && buf[length-1]==LF && buf[length-2]==CR)
  258.         length-=2;
  259.     if(length>=2 && buf[length-1]==LF && buf[length-2]==CR)
  260.         length-=2;
  261.  
  262.     lzh=0;
  263.     if(sub[subnum]->misc&SUB_LZH && length+2>=SDT_BLOCK_LEN) {
  264.         if((outbuf=(char *)MALLOC(length*2))==NULL) {
  265.             errormsg(WHERE,ERR_ALLOC,"lzh",length*2);
  266.             smb_unlocksmbhdr(&smb);
  267.             smb_freemsgmem(&msg);
  268.             bail(3); }
  269.         lzhlen=lzh_encode(buf,length,outbuf);
  270.         if(lzhlen>1
  271.             && smb_datblocks(lzhlen+4)
  272.                 <smb_datblocks(length+2)) { /* Compressable */
  273.             length=lzhlen+2;
  274.             FREE(buf);
  275.             lzh=1;
  276.             buf=outbuf; }
  277.         else                /* Uncompressable */
  278.             FREE(outbuf); }
  279.  
  280.     length+=2;                        /* for translation string */
  281.  
  282.     if(status.attr&SMB_HYPERALLOC) {
  283.         offset=smb_hallocdat(&smb);
  284.         storage=SMB_HYPERALLOC; }
  285.     else {
  286.         if((i=smb_open_da(&smb))!=0) {
  287.             errormsg(WHERE,ERR_OPEN,smb.file,i);
  288.             bail(5); }
  289.         if(sub[subnum]->misc&SUB_FAST) {
  290.             offset=smb_fallocdat(&smb,length,1);
  291.             storage=SMB_FASTALLOC; }
  292.         else {
  293.             offset=smb_allocdat(&smb,length,1);
  294.             storage=SMB_SELFPACK; }
  295.         fclose(smb.sda_fp); }
  296.  
  297.     msg.hdr.offset=offset;
  298.  
  299.     smb_dfield(&msg,TEXT_BODY,length);
  300.  
  301.     fseek(smb.sdt_fp,offset,SEEK_SET);
  302.     if(lzh) {
  303.         xlat=XLAT_LZH;
  304.         fwrite(&xlat,2,1,smb.sdt_fp); }
  305.     xlat=XLAT_NONE;
  306.     fwrite(&xlat,2,1,smb.sdt_fp);
  307.     j=SDT_BLOCK_LEN-2;                /* Don't read/write more than 255 */
  308.     if(lzh)
  309.         j-=2;
  310.     l=0;
  311.     length-=2;
  312.     if(lzh)
  313.         length-=2;
  314.     while(l<length) {
  315.         if(l+j>length)
  316.             j=length-l;
  317.         fwrite(buf+l,j,1,smb.sdt_fp);
  318.         l+=j;
  319.         j=SDT_BLOCK_LEN; }
  320.     fflush(smb.sdt_fp);
  321.     FREE(buf);
  322.     smb_unlocksmbhdr(&smb);
  323.     smb_addmsghdr(&smb,&msg,storage);
  324.     smb_freemsgmem(&msg); }
  325.  
  326. sprintf(str,"%20s Imported %u\r\n","",imported);
  327. write(logfile,str,strlen(str));
  328. printf("\nDone.\n");
  329. bail(0);
  330. return(0);
  331. }
  332.  
  333.