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

  1. /* UTIEXPRT.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 "post.h"
  8.  
  9. char *nulstr="";
  10. smb_t smb;
  11.  
  12. ulong loadmsgs(post_t huge **post, ulong ptr)
  13. {
  14.     int i;
  15.     long l=0;
  16.     idxrec_t idx;
  17.  
  18.  
  19. if((i=smb_locksmbhdr(&smb))!=0) {                  /* Be sure noone deletes or */
  20.     errormsg(WHERE,ERR_LOCK,smb.file,i);        /* adds while we're reading */
  21.     return(0L); }
  22.  
  23. fseek(smb.sid_fp,0L,SEEK_SET);
  24. while(!feof(smb.sid_fp)) {
  25.     if(!fread(&idx,sizeof(idxrec_t),1,smb.sid_fp))
  26.         break;
  27.  
  28.     if(idx.number<ptr || idx.attr&MSG_DELETE)
  29.         continue;
  30.  
  31.     if(idx.attr&MSG_MODERATED && !(idx.attr&MSG_VALIDATED))
  32.         break;
  33.  
  34.     if(((*post)=(post_t huge *)REALLOC((*post),sizeof(post_t)*(l+1)))
  35.         ==NULL) {
  36.         smb_unlocksmbhdr(&smb);
  37.         errormsg(WHERE,ERR_ALLOC,smb.file,sizeof(post_t)*(l+1));
  38.         return(l); }
  39.     (*post)[l].offset=idx.offset;
  40.     (*post)[l].number=idx.number;
  41.     l++; }
  42. smb_unlocksmbhdr(&smb);
  43. return(l);
  44. }
  45.  
  46.  
  47. int main(int argc, char **argv)
  48. {
  49.     char    str[512],*buf,ch,*outbuf;
  50.     ushort    xlat;
  51.     int     i,file,tear,cr,net=0,lzh;
  52.     uint    subnum;
  53.     long    l,m,length,buflen;
  54.     ulong    msgnum,posts,exported=0;
  55.     FILE    *stream;
  56.     post_t    huge *post;
  57.     smbmsg_t msg;
  58.     struct    date date;
  59.     struct    time curtime;
  60.  
  61. PREPSCREEN;
  62.  
  63. printf("Synchronet UTIEXPRT v%s\n",VER);
  64.  
  65. if(argc<3)
  66.     exit(1);
  67.  
  68. if(argc>4 && !stricmp(argv[4],"/NETWORK"))
  69.     net=1;
  70.  
  71. uti_init("UTIEXPRT",argc,argv);
  72.  
  73. if((file=nopen(argv[3],O_CREAT|O_TRUNC|O_WRONLY))==-1)
  74.     bail(2);
  75. if((stream=fdopen(file,"wb"))==NULL)
  76.     bail(2);
  77. setvbuf(stream,0,_IOFBF,4096);
  78.  
  79. subnum=getsubnum(argv[1]);
  80. if((int)subnum==-1)
  81.     bail(7);
  82. msgnum=atol(argv[2]);
  83.  
  84. sprintf(smb.file,"%s%s",sub[subnum]->data_dir,sub[subnum]->code);
  85. smb.retry_time=30;
  86. if((i=smb_open(&smb))!=0) {
  87.     errormsg(WHERE,ERR_OPEN,smb.file,i);
  88.     bail(5); }
  89.  
  90. post=NULL;
  91. posts=loadmsgs(&post,msgnum);
  92.  
  93. printf("\nExporting\n\n");
  94. for(l=0;l<posts;l++) {
  95.     printf("\rScanning: %lu of %lu  Exported: %lu",l+1,posts,exported);
  96.     msg.idx.offset=post[l].offset;
  97.     if((i=smb_lockmsghdr(&smb,&msg))!=0) {
  98.         errormsg(WHERE,ERR_LOCK,smb.file,i);
  99.         continue; }
  100.     i=smb_getmsghdr(&smb,&msg);
  101.     if(i || msg.hdr.number!=post[l].number) {
  102.         smb_unlockmsghdr(&smb,&msg);
  103.         smb_freemsgmem(&msg);
  104.  
  105.         msg.hdr.number=post[l].number;
  106.         if((i=smb_getmsgidx(&smb,&msg))!=0) {
  107.             errormsg(WHERE,ERR_READ,smb.file,i);
  108.             continue; }
  109.         if((i=smb_lockmsghdr(&smb,&msg))!=0) {
  110.             errormsg(WHERE,ERR_LOCK,smb.file,i);
  111.             continue; }
  112.         if((i=smb_getmsghdr(&smb,&msg))!=0) {
  113.             smb_unlockmsghdr(&smb,&msg);
  114.             errormsg(WHERE,ERR_READ,smb.file,i);
  115.             continue; } }
  116.  
  117.     if(net                                        /* Network */
  118.         && (!strncmpi(msg.subj,"NE:",3)         /* No Echo */
  119.         || msg.from_net.type==NET_POSTLINK)) {    /* from PostLink */
  120.         smb_unlockmsghdr(&smb,&msg);
  121.         smb_freemsgmem(&msg);
  122.         continue; }                     /* From a Fido node, ignore it */
  123.  
  124.     if(net && !(sub[subnum]->misc&SUB_GATE) && msg.from_net.type) {
  125.         smb_unlockmsghdr(&smb,&msg);
  126.         smb_freemsgmem(&msg);
  127.         continue; }
  128.  
  129.     exported++;
  130.  
  131.     fprintf(stream,"%s\r\n%s\r\n%s\r\n"
  132.         ,msg.to,msg.from,msg.subj);
  133.     unixtodos(msg.hdr.when_written.time,&date,&curtime);
  134.     fprintf(stream,"%lu\r\n%lu\r\n%02u/%02u/%02u\r\n%02u:%02u\r\n"
  135.         "%s\r\n%c\r\n%c\r\nTEXT:\r\n"
  136.         ,msg.hdr.number
  137.         ,msg.hdr.thread_orig
  138.         ,date.da_mon,date.da_day,date.da_year-1900
  139.         ,curtime.ti_hour,curtime.ti_min
  140.         ,msg.hdr.attr&MSG_PRIVATE ? "PRIVATE" : "PUBLIC"
  141.         ,msg.hdr.attr&MSG_READ ? 'Y':'N'
  142.         ,strncmpi(msg.subj,"NE:",3) ? 'Y':'N');
  143.  
  144.     for(i=0;i<msg.hdr.total_dfields;i++) {
  145.  
  146.         if(msg.dfield[i].type!=TEXT_BODY
  147.             && msg.dfield[i].type!=TEXT_TAIL)
  148.             continue;                    /* skip non-text data fields */
  149.  
  150.         if(msg.dfield[i].length<3)        /* need at least 3 bytes */
  151.             continue;
  152.  
  153.         fseek(smb.sdt_fp,msg.hdr.offset+msg.dfield[i].offset,SEEK_SET);
  154.  
  155.         lzh=0;
  156.         fread(&xlat,2,1,smb.sdt_fp);
  157.         if(xlat==XLAT_LZH) {
  158.             lzh=1;
  159.             fread(&xlat,2,1,smb.sdt_fp); }
  160.         if(xlat!=XLAT_NONE)             /* no translations supported */
  161.             continue;
  162.  
  163.         length=msg.dfield[i].length-2;
  164.         if(lzh)
  165.             length-=2;
  166.  
  167.         if((buf=MALLOC(length))==NULL) {
  168.             errormsg(WHERE,ERR_ALLOC,nulstr,length);
  169.             continue; }
  170.  
  171.         fread(buf,length,1,smb.sdt_fp);
  172.  
  173.         if(lzh) {
  174.             buflen=*(long *)buf;
  175.             if((outbuf=MALLOC(buflen))==NULL) {
  176.                 errormsg(WHERE,ERR_ALLOC,"lzh",buflen);
  177.                 FREE(buf);
  178.                 continue; }
  179.             length=lzh_decode(buf,length,outbuf);
  180.             FREE(buf);
  181.             buf=outbuf; }
  182.  
  183.         tear=0;
  184.         for(m=0,cr=1;m<length;m++) {
  185.             if(buf[m]==1) { /* Ctrl-A, so skip it and the next char */
  186.                 m++;
  187.                 continue; }
  188.             if(buf[m]==0)  /* Ignore line feeds */
  189.                 continue;
  190.  
  191.             if(m+3<length && cr && buf[m]=='-' && buf[m+1]=='-'
  192.                 && buf[m+2]=='-' && (buf[m+3]==CR || buf[m+3]==SP))
  193.                 tear=1;
  194.  
  195.             if(buf[m]==LF)
  196.                 cr=1;
  197.             else
  198.                 cr=0;
  199.  
  200.             if(sub[subnum]->misc&SUB_ASCII) {
  201.                 if(buf[m]<SP && buf[m]!=CR) /* Ctrl ascii */
  202.                     buf[m]='.';             /* converted to '.' */
  203.                 if((uchar)buf[m]>0x7f)        /* extended ASCII */
  204.                     buf[m]='*'; }           /* converted to '*' */
  205.             fputc(buf[m],stream); }
  206.         fprintf(stream,"\r\n");
  207.         FREE(buf); }
  208.  
  209.     if(!(sub[subnum]->misc&SUB_NOTAG)) {
  210.         if(!tear)    /* No previous tear line */
  211.             fprintf(stream,"---\r\n");            /* so add one */
  212.         if(sub[subnum]->misc&SUB_ASCII) ch='*';
  213.         else ch='■';
  214.         fprintf(stream," %c Synchronet UTI v%s\r\n",ch,VER); }
  215.  
  216.     fprintf(stream,"\xff\r\n");
  217.     smb_unlockmsghdr(&smb,&msg);
  218.     smb_freemsgmem(&msg); }
  219.  
  220. sprintf(str,"%20s Scanned %lu, Exported %lu\r\n"
  221.     ,"",posts,exported);
  222. write(logfile,str,strlen(str));
  223. printf("\nDone.\n");
  224. smb_close(&smb);
  225. FREE(post);
  226. bail(0);
  227. return(0);
  228. }
  229.