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

  1. #line 1 "QWKTOMSG.C"
  2.  
  3. /* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
  4.  
  5. #include "sbbs.h"
  6. #include "qwk.h"
  7.  
  8. /****************************************************************************/
  9. /* Converts a QWK message packet into a message. The 'qwkbuf' pointer       */
  10. /* points to the message header and 'str' is the file to output to.         */
  11. /* Returns the 1 on success or 0 if failure.                                */
  12. /****************************************************************************/
  13. char qwktomsg(FILE *qwk_fp, uchar *hdrblk, char fromhub, uint subnum
  14.     , uint touser)
  15. {
  16.     uchar    str[256],*body,*tail,col=0,lastch=0,*p,*lzhbuf,qwkbuf[128];
  17.     int     file,i,j,k,lzh=0,storage,skip=0;
  18.     ushort    xlat;
  19.     long    l,bodylen,taillen,length;
  20.     ulong    crc,block,blocks;
  21.     smbmsg_t    msg;
  22.  
  23. memset(&msg,0,sizeof(smbmsg_t));        /* Initialize message header */
  24. memcpy(msg.hdr.id,"SHD\x1a",4);
  25. msg.hdr.version=SMB_VERSION;
  26.  
  27. blocks=atol(hdrblk+116);
  28. if(blocks<2)
  29.     return(0);
  30.  
  31. if(subnum!=INVALID_SUB
  32.     && (hdrblk[0]=='*' || hdrblk[0]=='+' || sub[subnum]->misc&SUB_PONLY))
  33.     msg.idx.attr|=MSG_PRIVATE;
  34. if(subnum!=INVALID_SUB && sub[subnum]->misc&SUB_AONLY)
  35.     msg.idx.attr|=MSG_ANONYMOUS;
  36. if(subnum==INVALID_SUB && sys_misc&SM_DELREADM)
  37.     msg.idx.attr|=MSG_KILLREAD;
  38. if((fromhub || useron.rest&FLAG('Q')) &&
  39.     (hdrblk[0]=='*' || hdrblk[0]=='-' || hdrblk[0]=='`'))
  40.     msg.idx.attr|=MSG_READ;
  41.  
  42. if(subnum!=INVALID_SUB && !fromhub && sub[subnum]->mod_ar[0]
  43.     && chk_ar(sub[subnum]->mod_ar,useron))
  44.     msg.idx.attr|=MSG_MODERATED;
  45. if(subnum!=INVALID_SUB && !fromhub && sub[subnum]->misc&SUB_SYSPERM
  46.     && sub_op(subnum))
  47.     msg.idx.attr|=MSG_PERMANENT;
  48.  
  49. msg.hdr.attr=msg.idx.attr;
  50.  
  51. date.da_mon=((hdrblk[8]&0xf)*10)+(hdrblk[9]&0xf);
  52. date.da_day=((hdrblk[11]&0xf)*10)+(hdrblk[12]&0xf);
  53. date.da_year=((hdrblk[14]&0xf)*10)+(hdrblk[15]&0xf)+1900;
  54. curtime.ti_hour=((hdrblk[16]&0xf)*10)+(hdrblk[17]&0xf);
  55. curtime.ti_min=((hdrblk[19]&0xf)*10)+(hdrblk[20]&0xf);
  56. curtime.ti_sec=0;
  57. msg.hdr.when_written.time=dostounix(&date,&curtime);
  58. if(!(useron.rest&FLAG('Q')) && !fromhub)
  59.     msg.hdr.when_written.zone=sys_timezone;
  60. msg.hdr.when_imported.time=time(NULL);
  61. msg.hdr.when_imported.zone=sys_timezone;
  62.  
  63. msg.hdr.thread_orig=atol(hdrblk+108);
  64.  
  65. if((uint)subnum==INVALID_SUB) {         /* E-mail */
  66.     msg.idx.to=touser;
  67.  
  68. /***                    Unnecessary code in v2.10a
  69.     if(!touser) {
  70.         sprintf(str,"%25.25s",hdrblk+21);
  71.         truncsp(str);
  72.         for(i=0;i<total_qhubs;i++)
  73.             if(!stricmp(qhub[i]->id,str))
  74.                 break;
  75.         if(i==total_qhubs) {
  76.             errormsg(WHERE,ERR_CHK,str,0);
  77.             return(0); }
  78.         smb_hfield(&msg,RECIPIENT,strlen(str),str); }
  79.     else {
  80. ***/
  81.         username(touser,str);
  82.         smb_hfield(&msg,RECIPIENT,strlen(str),str);
  83.         sprintf(str,"%u",touser);
  84.         smb_hfield(&msg,RECIPIENTEXT,strlen(str),str); }
  85.  
  86. else {
  87.     sprintf(str,"%25.25s",hdrblk+21);     /* To user */
  88.     truncsp(str);
  89.     smb_hfield(&msg,RECIPIENT,strlen(str),str);
  90.     strlwr(str);
  91.     msg.idx.to=crc16(str); }
  92.  
  93. fread(qwkbuf,1,128,qwk_fp);
  94.  
  95. if(useron.rest&FLAG('Q') || fromhub) {      /* QWK Net */
  96.     if(!strnicmp(qwkbuf,"@VIA:",5)) {
  97.         p=strchr(qwkbuf,'\xe3');
  98.         if(p) {
  99.             *p=0;
  100.             skip=strlen(qwkbuf)+1; }
  101.         truncsp(qwkbuf);
  102.         p=qwkbuf+5;                     /* Skip "@VIA:" */
  103.         while(*p && *p<=SP) p++;        /* Skip any spaces */
  104.         if(route_circ(p,sys_id)) {
  105.             smb_freemsgmem(&msg);
  106.             bprintf("\r\nCircular message path: %s\r\n",p);
  107.             sprintf(str,"Circular message path: %s from %s"
  108.                 ,p,fromhub ? qhub[fromhub-1]->id:useron.alias);
  109.             errorlog(str);
  110.             return(0); }
  111.         sprintf(str,"%s/%s"
  112.             ,fromhub ? qhub[fromhub-1]->id : useron.alias,p);
  113.         strupr(str);
  114.         update_qwkroute(str); }
  115.     else {
  116.         if(fromhub)
  117.             strcpy(str,qhub[fromhub-1]->id);
  118.         else
  119.             strcpy(str,useron.alias); }
  120.     strupr(str);
  121.     j=NET_QWK;
  122.     smb_hfield(&msg,SENDERNETTYPE,2,&j);
  123.     smb_hfield(&msg,SENDERNETADDR,strlen(str),str);
  124.     sprintf(str,"%25.25s",hdrblk+46);  /* From user */
  125.     truncsp(str);
  126.     if(!strnicmp(qwkbuf+skip,"@TZ:",4)) {
  127.         p=strchr(qwkbuf+skip,'\xe3');
  128.         i=skip;
  129.         if(p) {
  130.             *p=0;
  131.             skip+=strlen(qwkbuf+i)+1; }
  132.         p=qwkbuf+i+4;                    /* Skip "@TZ:" */
  133.         while(*p && *p<=SP) p++;        /* Skip any spaces */
  134.         msg.hdr.when_written.zone=(short)ahtoul(p); }
  135.     }
  136. else {
  137.     sprintf(str,"%u",useron.number);
  138.     smb_hfield(&msg,SENDEREXT,strlen(str),str);
  139.     if((uint)subnum!=INVALID_SUB && sub[subnum]->misc&SUB_NAME)
  140.         strcpy(str,useron.name);
  141.     else
  142.         strcpy(str,useron.alias); }
  143.  
  144. smb_hfield(&msg,SENDER,strlen(str),str);
  145. if((uint)subnum==INVALID_SUB) {
  146.     if(useron.rest&FLAG('Q') || fromhub)
  147.         msg.idx.from=0;
  148.     else
  149.         msg.idx.from=useron.number; }
  150. else {
  151.     strlwr(str);
  152.     msg.idx.from=crc16(str); }
  153.  
  154. sprintf(str,"%25.25s",hdrblk+71);   /* Subject */
  155. truncsp(str);
  156. remove_re(str);
  157. smb_hfield(&msg,SUBJECT,strlen(str),str);
  158. strlwr(str);
  159. msg.idx.subj=crc16(str);
  160.  
  161. /********************************/
  162. /* Convert the QWK message text */
  163. /********************************/
  164.  
  165. bodylen=0;
  166. if((body=LMALLOC((blocks-1L)*128L*2L))==NULL) {
  167.     smb_freemsgmem(&msg);
  168.     errormsg(WHERE,ERR_ALLOC,"QWK msg",(blocks-1L)*128L*2L);
  169.     return(0); }
  170.  
  171. taillen=0;
  172. if((tail=LMALLOC((blocks-1L)*128L))==NULL) {
  173.     LFREE(body);
  174.     smb_freemsgmem(&msg);
  175.     errormsg(WHERE,ERR_ALLOC,"QWK msg",(blocks-1L)*128L);
  176.     return(0); }
  177.  
  178. for(block=1;block<blocks;block++) {
  179.     if(block>1)
  180.         if(!fread(qwkbuf,1,128,qwk_fp))
  181.             break;
  182.     for(k=skip;k<128;k++,skip=0) {
  183.         if(qwkbuf[k]==0)
  184.             continue;
  185.         if(!taillen && qwkbuf[k]==SP && col==3 && bodylen>=3
  186.             && body[bodylen-3]=='-' && body[bodylen-2]=='-'
  187.             && body[bodylen-1]=='-') {
  188.             bodylen-=3;
  189.             strcpy(tail,"--- ");
  190.             taillen=4;
  191.             col++;
  192.             continue; }
  193.         if(qwkbuf[k]==0xE3) {        /* expand 0xe3 to crlf */
  194.             if(!taillen && col==3 && bodylen>=3 && body[bodylen-3]=='-'
  195.                 && body[bodylen-2]=='-' && body[bodylen-1]=='-') {
  196.                 bodylen-=3;
  197.                 strcpy(tail,"---");
  198.                 taillen=3; }
  199.             col=0;
  200.             if(taillen) {
  201.                 tail[taillen++]=CR;
  202.                 tail[taillen++]=LF; }
  203.             else {
  204.                 body[bodylen++]=CR;
  205.                 body[bodylen++]=LF; }
  206.             continue; }
  207.         if(!fromhub && qwkbuf[k]==7 && useron.rest&FLAG('B'))   /* beep res */
  208.             continue;
  209.         if(!fromhub && (qwkbuf[k]==1 || qwkbuf[k]==ESC) /* ANSI restriction */
  210.             && useron.rest&FLAG('A'))
  211.             continue;
  212.         if(qwkbuf[k]!=1 && lastch!=1)
  213.             col++;
  214.         if(lastch==1 && !validattr(qwkbuf[k])) {
  215.             if(taillen) taillen--;
  216.             else        bodylen--;
  217.             lastch=0;
  218.             continue; }
  219.         lastch=qwkbuf[k];
  220.         if(taillen)
  221.             tail[taillen++]=qwkbuf[k];
  222.         else
  223.             body[bodylen++]=qwkbuf[k]; } }
  224.  
  225. while(bodylen && body[bodylen-1]==SP) bodylen--; /* remove trailing spaces */
  226. if(bodylen>=2 && body[bodylen-2]==CR && body[bodylen-1]==LF)
  227.     bodylen-=2;
  228.  
  229. while(taillen && tail[taillen-1]<=SP) taillen--; /* remove trailing garbage */
  230.  
  231. /*****************/
  232. /* Calculate CRC */
  233. /*****************/
  234.  
  235. if(smb.status.max_crcs) {
  236.     crc=0xffffffffUL;
  237.     for(l=0;l<bodylen;l++)
  238.         crc=ucrc32(body[l],crc);
  239.     crc=~crc;
  240.  
  241.     /*******************/
  242.     /* Check for dupes */
  243.     /*******************/
  244.  
  245.     j=smb_addcrc(&smb,crc);
  246.     if(j) {
  247.         if(j==1) {
  248.             bprintf("\r\nDuplicate message\r\n");
  249.             logline(subnum==INVALID_SUB ? "E!":"P!","Duplicate message"); }
  250.         else
  251.             errormsg(WHERE,ERR_CHK,smb.file,j);
  252.  
  253.         smb_freemsgmem(&msg);
  254.         LFREE(body);
  255.         LFREE(tail);
  256.         return(0); } }
  257.  
  258. bputs(text[WritingIndx]);
  259.  
  260. /*************************************/
  261. /* Write SMB message header and text */
  262. /*************************************/
  263.  
  264. if(subnum!=INVALID_SUB && sub[subnum]->misc&SUB_LZH && bodylen
  265.     && bodylen+2+taillen+2>=SDT_BLOCK_LEN
  266.     && (lzhbuf=(char *)LMALLOC(bodylen*2))!=NULL) {
  267.     length=lzh_encode((uchar *)body,bodylen,(uchar *)lzhbuf);
  268.     if(length>1L
  269.         && smb_datblocks(length+4L+taillen+2L)
  270.             <smb_datblocks(bodylen+2L+taillen+2L)) {
  271.         bodylen=length;     /* Compressable */
  272.         length+=4L;
  273.         lzh=1;
  274.         LFREE(body);
  275.         body=lzhbuf; }
  276.     else {                    /* Non-compressable */
  277.         length=bodylen+2L;
  278.         LFREE(lzhbuf); } }
  279. else
  280.     length=bodylen+2L;                     /* +2 for translation string */
  281.  
  282. if(taillen)
  283.     length+=taillen+2L;
  284.  
  285. if(length&0xfff00000UL) {
  286.     errormsg(WHERE,ERR_LEN,"REP msg",length);
  287.     smb_freemsgmem(&msg);
  288.     LFREE(body);
  289.     LFREE(tail);
  290.     return(0); }
  291.  
  292. if((i=smb_locksmbhdr(&smb))!=0) {
  293.     errormsg(WHERE,ERR_LOCK,smb.file,i);
  294.     FREE(body);
  295.     FREE(tail);
  296.     return(0); }
  297.  
  298. if(smb.status.attr&SMB_HYPERALLOC) {
  299.     msg.hdr.offset=smb_hallocdat(&smb);
  300.     storage=SMB_HYPERALLOC; }
  301. else {
  302.     if((i=smb_open_da(&smb))!=0) {
  303.         errormsg(WHERE,ERR_OPEN,smb.file,i);
  304.         FREE(body);
  305.         FREE(tail);
  306.         return(0); }
  307.     if((subnum==INVALID_SUB && sys_misc&SM_FASTMAIL)
  308.         || (subnum!=INVALID_SUB && sub[subnum]->misc&SUB_FAST)) {
  309.         msg.hdr.offset=smb_fallocdat(&smb,length,1);
  310.         storage=SMB_FASTALLOC; }
  311.     else {
  312.         msg.hdr.offset=smb_allocdat(&smb,length,1);
  313.         storage=SMB_SELFPACK; }
  314.     smb_close_da(&smb); }
  315.  
  316. if(msg.hdr.offset && msg.hdr.offset<1L) {
  317.     smb_unlocksmbhdr(&smb);
  318.     errormsg(WHERE,ERR_READ,smb.file,msg.hdr.offset);
  319.     smb_freemsgmem(&msg);
  320.     FREE(body);
  321.     FREE(tail); }
  322. fseek(smb.sdt_fp,msg.hdr.offset,SEEK_SET);
  323. if(lzh) {
  324.     xlat=XLAT_LZH;
  325.     fwrite(&xlat,2,1,smb.sdt_fp); }
  326. xlat=XLAT_NONE;
  327. fwrite(&xlat,2,1,smb.sdt_fp);
  328. fwrite(body,bodylen,1,smb.sdt_fp);
  329. smb_dfield(&msg,TEXT_BODY,bodylen+2+(lzh ? 2:0));
  330. if(taillen) {
  331.     fwrite(&xlat,2,1,smb.sdt_fp);
  332.     fwrite(tail,taillen,1,smb.sdt_fp);
  333.     smb_dfield(&msg,TEXT_TAIL,taillen+2); }
  334. fflush(smb.sdt_fp);
  335. smb_unlocksmbhdr(&smb);
  336.  
  337. if((i=smb_addmsghdr(&smb,&msg,storage))!=0)
  338.     errormsg(WHERE,ERR_WRITE,smb.file,i);
  339.  
  340. smb_freemsgmem(&msg);
  341.  
  342. LFREE(body);
  343. LFREE(tail);
  344.  
  345. return(1);
  346. }
  347.