home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / BBS / SMB_111.ZIP / INET2SMB.C < prev    next >
C/C++ Source or Header  |  1994-03-28  |  8KB  |  312 lines

  1. /* Converts RFC #822 Internet Text Messages to SMB format */
  2.  
  3. #include <dos.h>
  4. #include "smblib.h"
  5.  
  6. /******************************************************************************
  7.  Chops off the ":" and all spaces and tabs after it for the message headers
  8. ******************************************************************************/
  9. char *header(char *instr)
  10. {
  11.     char str[256],*p;
  12.     int i=0;
  13.  
  14.     p=strstr(instr,":");
  15.     ++p;
  16.     while(*p==SP || *p==TAB)
  17.         ++p;
  18.     while(i<strlen(p)) {
  19.         if(p[i]==LF || p[i]==CR) {
  20.             p[i]=0;
  21.             break; }
  22.         ++i; }
  23.     return p;
  24. }
  25. /****************************************************************************/
  26. /* Updates 16-bit "rcrc" with character 'ch'                                */
  27. /****************************************************************************/
  28. void ucrc16(uchar ch, ushort *rcrc) {
  29.     ushort i, cy;
  30.     uchar nch=ch;
  31.  
  32. for (i=0; i<8; i++) {
  33.     cy=*rcrc & 0x8000;
  34.     *rcrc<<=1;
  35.     if (nch & 0x80) *rcrc |= 1;
  36.     nch<<=1;
  37.     if (cy) *rcrc ^= 0x1021; }
  38. }
  39.  
  40. /****************************************************************************/
  41. /* Returns 16-crc of string (not counting terminating NULL)                 */
  42. /****************************************************************************/
  43. ushort crc16(char *str)
  44. {
  45.     int     i=0;
  46.     ushort    crc=0;
  47.  
  48. ucrc16(0,&crc);
  49. while(str[i])
  50.     ucrc16(str[i++],&crc);
  51. ucrc16(0,&crc);
  52. ucrc16(0,&crc);
  53. return(crc);
  54. }
  55.  
  56. /******************************************************************************
  57.  Converts ASCII time in RFC 822 or RFC 1036 format to SMB when_t format
  58. ******************************************************************************/
  59. when_t imsgtime(char *str)
  60. {
  61.     char month[25],zone[25],*p;
  62.     struct date date;
  63.     struct time t;
  64.     when_t when;
  65.  
  66. when.zone=0;    /* Default to UT */
  67.  
  68. if(isdigit(str[1])) {    /* Regular format: "01 Jan 86 0234 GMT" */
  69.     date.da_day=atoi(str);
  70.     sprintf(month,"%3.3s",str+3);
  71.     if(!stricmp(month,"jan"))
  72.         date.da_mon=1;
  73.     else if(!stricmp(month,"feb"))
  74.         date.da_mon=2;
  75.     else if(!stricmp(month,"mar"))
  76.         date.da_mon=3;
  77.     else if(!stricmp(month,"apr"))
  78.         date.da_mon=4;
  79.     else if(!stricmp(month,"may"))
  80.         date.da_mon=5;
  81.     else if(!stricmp(month,"jun"))
  82.         date.da_mon=6;
  83.     else if(!stricmp(month,"jul"))
  84.         date.da_mon=7;
  85.     else if(!stricmp(month,"aug"))
  86.         date.da_mon=8;
  87.     else if(!stricmp(month,"sep"))
  88.         date.da_mon=9;
  89.     else if(!stricmp(month,"oct"))
  90.         date.da_mon=10;
  91.     else if(!stricmp(month,"nov"))
  92.         date.da_mon=11;
  93.     else
  94.         date.da_mon=12;
  95.     date.da_year=1900+atoi(str+7);
  96.     t.ti_hour=atoi(str+10);
  97.     t.ti_min=atoi(str+12);
  98.     t.ti_sec=0;
  99.     p=str+13; }
  100.  
  101. else {                    /* USENET format: "Mon,  1 Jan 86 02:34:00 GMT" */
  102.     date.da_day=atoi(str+5);
  103.     sprintf(month,"%3.3s",str+8);
  104.     if(!stricmp(month,"jan"))
  105.         date.da_mon=1;
  106.     else if(!stricmp(month,"feb"))
  107.         date.da_mon=2;
  108.     else if(!stricmp(month,"mar"))
  109.         date.da_mon=3;
  110.     else if(!stricmp(month,"apr"))
  111.         date.da_mon=4;
  112.     else if(!stricmp(month,"may"))
  113.         date.da_mon=5;
  114.     else if(!stricmp(month,"jun"))
  115.         date.da_mon=6;
  116.     else if(!stricmp(month,"jul"))
  117.         date.da_mon=7;
  118.     else if(!stricmp(month,"aug"))
  119.         date.da_mon=8;
  120.     else if(!stricmp(month,"sep"))
  121.         date.da_mon=9;
  122.     else if(!stricmp(month,"oct"))
  123.         date.da_mon=10;
  124.     else if(!stricmp(month,"nov"))
  125.         date.da_mon=11;
  126.     else
  127.         date.da_mon=12;
  128.     date.da_year=atoi(str+12);
  129.     if(date.da_year<100)
  130.         date.da_year+=1900;
  131.     p=str+12;
  132.     while(*p!=SP) p++;    /* skip the year */
  133.     while(*p==SP) p++;    /* and white space */
  134.     t.ti_hour=atoi(p);
  135.     t.ti_min=atoi(p+3);
  136.     t.ti_sec=atoi(p+6);
  137.     p=str+22; }
  138.  
  139. when.time=dostounix(&date,&t);
  140.  
  141. while(*p!=SP) p++;    /* skip the time */
  142. while(*p==SP) p++;    /* and white space */
  143.  
  144. sprintf(zone,"%-.5s",p);
  145.  
  146. /* Get the zone */
  147. if(!strcmpi(zone,"GMT") || !strcmpi(zone,"UT"))
  148.     when.zone=0;
  149. else if(!strcmpi(zone,"EST"))
  150.     when.zone=EST;
  151. else if(!strcmpi(zone,"EDT"))
  152.     when.zone=EDT;
  153. else if(!strcmpi(zone,"MST"))
  154.     when.zone=MST;
  155. else if(!strcmpi(zone,"MDT"))
  156.     when.zone=MDT;
  157. else if(!strcmpi(zone,"CST"))
  158.     when.zone=CST;
  159. else if(!strcmpi(zone,"CDT"))
  160.     when.zone=CDT;
  161. else if(!strcmpi(zone,"PST"))
  162.     when.zone=PST;
  163. else if(!strcmpi(zone,"PDT"))
  164.     when.zone=PDT;
  165.  
  166. else if(isalpha(zone[0]) && !zone[1]) { /* Military single character zone */
  167.     zone[0]&=0xdf;    /* convert to upper case */
  168.     if(zone[0]>='A' && zone[0]<='I')
  169.         when.zone='@'-zone[0];
  170.     else if(zone[0]>='K' && zone[0]<='M')   /* J is not used */
  171.         when.zone='A'-zone[0];
  172.     else if(zone[0]>='N' && zone[0]<='Y')
  173.         when.zone=zone[0]-'M'; }
  174.  
  175. else if((zone[0]=='+' || zone[0]=='-') && isdigit(zone[1])) { /* Literal */
  176.     when.zone=(((zone[1]&0xf)*10)+(zone[2]&0xf))*60;    /* Hours */
  177.     when.zone+=atoi(zone+3);                            /* Minutes */
  178.     if(zone[0]=='-')                                    /* Negative? */
  179.         when.zone=-when.zone; }
  180.  
  181. return(when);
  182. }
  183.  
  184. void main(int argc, char **argv)
  185. {
  186.     FILE *stream;
  187.     char str[256]={NULL},filespec[256],file[256],*p,*abuf;
  188.     ushort i,xlat,net;
  189.     ulong l,length,datlen;
  190.     struct ffblk ff;
  191.     smbmsg_t    msg;
  192.     smbstatus_t status;
  193.  
  194.     if(argc<2) {
  195.         printf("Usage: INET2SMB <file_spec> <smb_name>\r\n");
  196.         exit(1); }
  197.  
  198.     strcpy(filespec,argv[1]);
  199.     strcpy(smb_file,argv[2]);
  200.     strupr(smb_file);
  201.  
  202.     smb_open(10);
  203.     if(!filelength(fileno(shd_fp)))
  204.         smb_create(2000,2000,0,10);
  205.  
  206.     i=findfirst(filespec,&ff,0);
  207.     while(!i) {
  208.         sprintf(file,"%s",ff.ff_name);
  209.         if((stream=fopen(file,"rb"))==NULL) {
  210.             printf("Error opening %s\r\n",file);
  211.             break; }
  212.  
  213.         memset(&msg,0,sizeof(smbmsg_t));
  214.         memcpy(msg.hdr.id,"SHD\x1a",4);
  215.         msg.hdr.version=SMB_VERSION;
  216.         msg.hdr.when_imported.time=time(NULL);
  217.         msg.hdr.when_imported.zone=PST;  /* set to local time zone */
  218.  
  219.         while(str[0]!=CR && str[0]!=LF) {
  220.             fgets(str,81,stream);
  221.             if(!strnicmp(str,"Resent-",6)) {
  222.                 p=strstr(str,"Resent-");
  223.                 sprintf(str,"%s",p); }
  224.             if(!strnicmp(str,"Return-Path",11)
  225.                 || !strnicmp(str,"Path",4)) {
  226.                 strcpy(str,header(str));
  227.                 smb_hfield(&msg,REPLYTO,strlen(str),str); }
  228.             else if(!strnicmp(str,"Date",4)) {
  229.                 strcpy(str,header(str));
  230.                 msg.hdr.when_written=imsgtime(str); }
  231.             else if(!strnicmp(str,"From",4) || !strnicmp(str,"Sender",6)) {
  232.                 strcpy(str,header(str));
  233.                 p=strstr(str," (");
  234.                 if(p) {
  235.                     *p=0;
  236.                     *(p+1)=0;
  237.                     p+=2;
  238.                     smb_hfield(&msg,SENDERNETADDR,strlen(str),str);
  239.                     sprintf(str,"%s",p);
  240.                     p=strstr(str,")");
  241.                     *p=0; }
  242.                 smb_hfield(&msg,SENDER,strlen(str),str);
  243.                 strlwr(str);
  244.                 msg.idx.from=crc16(str); }
  245.             else if(!strnicmp(str,"Subject",7)) {
  246.                 strcpy(str,header(str));
  247.                 smb_hfield(&msg,SUBJECT,strlen(str),str);
  248.                 strlwr(str);
  249.                 msg.idx.subj=crc16(str); }
  250.             else if(!strnicmp(str,"To",2)) {
  251.                 strcpy(str,header(str));
  252.                 smb_hfield(&msg,RECIPIENT,strlen(str),str);
  253.                 strlwr(str);
  254.                 msg.idx.to=crc16(str); }
  255.  
  256.             /* Following are optional fields */
  257.  
  258.             else if(!strnicmp(str,"Message-ID",10)) {
  259.                 strcpy(str,header(str));
  260.                 smb_hfield(&msg,RFC822MSGID,strlen(str),str); }
  261.             else if(!strnicmp(str,"In-Reply-To",11)) {
  262.                 strcpy(str,header(str));
  263.                 smb_hfield(&msg,RFC822REPLYID,strlen(str),str); }
  264.  
  265.             /* User defined extension field */
  266.  
  267.             else if(!strnicmp(str,"X-",2)) {
  268.                 if(strstr(str,"To")) {
  269.                     strcpy(str,header(str));
  270.                     smb_hfield(&msg,RECIPIENT,strlen(str),str);
  271.                     strlwr(str);
  272.                     msg.idx.to=crc16(str); }
  273.                 else
  274.                     smb_hfield(&msg,RFC822HEADER,strlen(str),str); }
  275.             else
  276.                 smb_hfield(&msg,RFC822HEADER,strlen(str),str); }
  277.  
  278.         l=ftell(stream);
  279.         fseek(stream,0L,SEEK_END);
  280.         length=ftell(stream)-l;
  281.         fseek(stream,l,SEEK_SET);
  282.  
  283.         if((abuf=(char *)MALLOC(length))==NULL) {
  284.             printf("alloc error\n");
  285.             exit(1); }
  286.         fread(abuf,length,1,stream);
  287.         fclose(stream);
  288.  
  289.         net=NET_INTERNET;
  290.         smb_hfield(&msg,SENDERNETTYPE,sizeof(ushort),&net);
  291.  
  292.         if(smb_open_da(10)) {
  293.             printf("error opening %s.SDA\n",smb_file);
  294.             exit(1); }
  295.         msg.hdr.offset=smb_fallocdat(length+2,1);
  296.         fclose(sda_fp);
  297.         if(msg.hdr.offset && msg.hdr.offset<1L) {
  298.             printf("error %ld allocating records\r\n",msg.hdr.offset);
  299.             exit(1); }
  300.         fseek(sdt_fp,msg.hdr.offset,SEEK_SET);
  301.         xlat=XLAT_NONE;
  302.         fwrite(&xlat,2,1,sdt_fp);
  303.         fwrite(abuf,SDT_BLOCK_LEN,smb_datblocks(length),sdt_fp);
  304.         FREE(abuf);
  305.  
  306.         smb_dfield(&msg,TEXT_BODY,length+2);
  307.  
  308.         smb_addmsghdr(&msg,&status,1,10);
  309.         smb_freemsgmem(msg);
  310.         i=findnext(&ff); }
  311. }
  312.