home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / qwksplit.zip / QWKSPLIT.C next >
C/C++ Source or Header  |  1994-07-15  |  10KB  |  365 lines

  1. /*****************************************************************************
  2.  
  3.  QWKSPLIT.C - Craig Morrison, 15 July 1994.
  4.  
  5.   I hereby release QWKSplit into the Public Domain on this day. Feel free to
  6.   use the code in any way you see fit, but if you use it for profit, give me
  7.   the credit in the documentation and make sure this notice appears in your
  8.   source code.
  9.  
  10.   This is a quick and dirty solution to a problem that I had with figuring
  11.   out how to send UUENCODED files through the Internet via a QWK mail door.
  12.  
  13.   The code may not be pretty nor the best in the world, but it does what I
  14.   needed it to do... If it don't work, fix it, that's why I released the
  15.   source code.
  16.  
  17.   The code is light and airy, shouldn't be too hard to follow. Just make
  18.   sure you have a copy of the QWK specs on hand if you want to know what
  19.   the different fields are for.
  20.  
  21.   QWKSPLIT takes a text file and splits it up into to one or more QWK style
  22.   messages based on a small configuration file. What it creates is a *.MSG
  23.   file that can be archived into a *.REP file and uploaded to a QWK mail
  24.   door.
  25.  
  26.   It was compiled using BC++ for OS/2 v1.00.
  27.  
  28. *****************************************************************************/
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include <time.h>
  34.  
  35. #pragma pack(1)
  36.  
  37. typedef struct _msgdat {
  38.     char msg_status;
  39.     char msg_number[7];
  40.     char msg_date[8];
  41.     char msg_time[5];
  42.     char msg_to[25];
  43.     char msg_from[25];
  44.     char msg_subj[25];
  45.     char msg_pass[12];
  46.     char msg_msgref[8];
  47.     char msg_blks[6];
  48.     char msg_flag;
  49.     unsigned short msg_conf;
  50.     unsigned short msg_lnum;
  51.     char msg_nettag;
  52. } MSGDAT, *PMSGDAT;
  53.  
  54. char BBSID[13];
  55. char ConfID[8];
  56. char tname[26];
  57. char fname[26];
  58. char subj[25];
  59. char password[13];
  60. char inetaddr[81];
  61. int lines;
  62. MSGDAT msg;
  63.  
  64. int GetQWKInfo(char *qwkcfg);
  65. int SplitMessage(char *filename);
  66. long GetLineCount(char *filename);
  67. long WriteMsgBlocks(FILE *f, FILE *bm);
  68.  
  69. void FillInMsgData(PMSGDAT pmsg)
  70. {
  71.     char *p;
  72.     char sdate[9], stime[9];
  73.  
  74.     memset(pmsg, ' ', sizeof(MSGDAT));
  75.     pmsg->msg_status = '*';
  76.     strncpy(pmsg->msg_number, ConfID, strlen(ConfID));
  77.     _strdate(sdate);
  78.     _strtime(stime);
  79.     p = strrchr(stime, ':');
  80.     if (p) *p = '\0';
  81.     p = strchr(sdate, '/');
  82.     if (p) *p = '-';
  83.     p = strchr(sdate, '/');
  84.     if (p) *p = '-';
  85.     strncpy(pmsg->msg_date, sdate, strlen(sdate));
  86.     strncpy(pmsg->msg_time, stime, strlen(stime));
  87.     strupr(tname);
  88.     strncpy(pmsg->msg_to, tname, strlen(tname));
  89.     strupr(fname);
  90.     strncpy(pmsg->msg_from, fname, strlen(fname));
  91.     strncpy(pmsg->msg_subj, subj, strlen(subj));
  92.     strncpy(pmsg->msg_pass, password, strlen(password));
  93.     pmsg->msg_conf = atoi(ConfID);
  94.     pmsg->msg_lnum = 0;
  95.     pmsg->msg_flag = 'ß';
  96. }
  97.  
  98. int main(int argc, char *argv[])
  99. {
  100.     int rc = 0;
  101.  
  102.     if (argc >= 3)
  103.     {
  104.         printf("Scanning configuration...");
  105.         rc = GetQWKInfo(argv[1]);
  106.         if (!rc)
  107.         {
  108.             printf("\n\nMessages will be prepared for: %s\n", BBSID);
  109.             printf("In conference %s.\n", ConfID);
  110.             printf("\nUsing the following parameters:\n\n");
  111.             printf("Addressee:         %s\n", tname);
  112.             if (strlen(inetaddr))
  113.                 printf("Internet address:  %s\n", inetaddr);
  114.             printf("Addressor:         %s\n", fname);
  115.             printf("Lines per message: %d\n", lines);
  116.             FillInMsgData(&msg);
  117.             rc = SplitMessage(argv[2]);
  118.         }
  119.     }
  120.     else
  121.         rc = 1;
  122.  
  123.     return(rc);
  124. }
  125.  
  126. int GetQWKInfo(char *qwkcfg)
  127. {
  128.     FILE *qi;
  129.     char text[128];
  130.     char *p;
  131.  
  132.     qi = fopen(qwkcfg, "r");
  133.     if (qi)
  134.     {
  135.         while (fgets(text, sizeof(text), qi)!=NULL)
  136.         {
  137.             p = strrchr(text, '\n');
  138.             if (p)
  139.                 *p = '\0';
  140.  
  141.             if (!strnicmp(text, "BBSID", 5))
  142.             {
  143.                 p = text;
  144.                 p += 5;
  145.                 while ((isspace(*p)) && (*p)) p++;
  146.                 if (strlen(p) >= 8)
  147.                     p[8] = '\0';
  148.                 strcpy(BBSID, p);
  149.             }
  150.             else if (!strnicmp(text, "CONFERENCE", 10))
  151.             {
  152.                 p = text;
  153.                 p += 10;
  154.                 while ((isspace(*p)) && (*p)) p++;
  155.                 if (strlen(p) >= 7)
  156.                     p[7] = '\0';
  157.                 strcpy(ConfID, p);
  158.             }
  159.             else if (!strnicmp(text, "TO", 2))
  160.             {
  161.                 p = text;
  162.                 p += 2;
  163.                 while ((isspace(*p)) && (*p)) p++;
  164.                 if (strlen(p) >= 25)
  165.                     p[25] = '\0';
  166.                 strcpy(tname, p);
  167.             }
  168.             else if (!strnicmp(text, "FROM", 4))
  169.             {
  170.                 p = text;
  171.                 p += 4;
  172.                 while ((isspace(*p)) && (*p)) p++;
  173.                 if (strlen(p) >= 25)
  174.                     p[25] = '\0';
  175.                 strcpy(fname, p);
  176.             }
  177.             else if (!strnicmp(text, "INTERNETTO", 10))
  178.             {
  179.                 p = text;
  180.                 p += 10;
  181.                 while ((isspace(*p)) && (*p)) p++;
  182.                 if (strlen(p) >= 25)
  183.                     p[25] = '\0';
  184.                 strcpy(inetaddr, p);
  185.             }
  186.             else if (!strnicmp(text, "PASSWORD", 8))
  187.             {
  188.                 p = text;
  189.                 p += 8;
  190.                 while ((isspace(*p)) && (*p)) p++;
  191.                 if (strlen(p) >= 12)
  192.                     p[12] = '\0';
  193.                 strcpy(password, p);
  194.             }
  195.             else if (!strnicmp(text, "LINES", 5))
  196.             {
  197.                 p = text;
  198.                 p += 5;
  199.                 while ((isspace(*p)) && (*p)) p++;
  200.                 lines = atoi(p);
  201.             }
  202.         }
  203.         fclose(qi);
  204.  
  205.         if (lines == 0)
  206.             lines = 55;
  207.         if (!strlen(tname))
  208.             strcpy(tname, "ALL");
  209.  
  210.         return(0);
  211.     }
  212.     else
  213.         return(2);
  214. }
  215.  
  216. long GetLineCount(char *filename)
  217. {
  218.     FILE *f;
  219.     char buf[256];
  220.     long i = 0;
  221.  
  222.     printf("\nScanning %s...", filename);
  223.  
  224.     f = fopen(filename, "r");
  225.     {
  226.         while(fgets(buf, sizeof(buf), f)!=NULL)
  227.             i++;
  228.  
  229.         fclose(f);
  230.     }
  231.  
  232.     return(i);
  233. }
  234.  
  235. int SplitMessage(char *filename)
  236. {
  237.     char mdat[128];
  238.     char *p;
  239.     FILE *bm;
  240.     FILE *f;
  241.     long lc;
  242.     long hdrpos;
  243.     long oldpos;
  244.     long msgs;
  245.     long i;
  246.     long blks;
  247.     int rc;
  248.  
  249.     rc = 0;
  250.     memset(mdat, ' ', sizeof(mdat));
  251.     strncpy(mdat, BBSID, strlen(BBSID));
  252.     strcat(BBSID, ".MSG");
  253.  
  254.     bm = fopen(BBSID, "wb");
  255.     if (bm)
  256.     {
  257.         lc = GetLineCount(filename);
  258.         msgs = lc / lines;
  259.         if (lc % lines)
  260.             msgs++;
  261.         printf("\n%ld lines, splitting %s into %ld messages...\n",
  262.                lc, filename, msgs);
  263.  
  264.         fwrite(mdat, sizeof(mdat), 1, bm);
  265.  
  266.         f = fopen(filename, "r");
  267.         if (f)
  268.         {
  269.             for(i = 0; i < msgs; i++)
  270.             {
  271.                 hdrpos = ftell(bm);
  272.                 fwrite(&msg, sizeof(MSGDAT), 1, bm);
  273.  
  274.                 blks = WriteMsgBlocks(f, bm);
  275.                 oldpos = ftell(bm);
  276.  
  277.                 memset(&(msg.msg_blks), ' ', 6);
  278.                 sprintf(mdat, "%ld", ++blks);
  279.                 strncpy(msg.msg_blks, mdat, strlen(mdat));
  280.  
  281.                 memset(&(msg.msg_subj), ' ', 25);
  282.                 sprintf(mdat, "Message %ld of %ld", i+1, msgs);
  283.                 strncpy(msg.msg_subj, mdat, strlen(mdat));
  284.  
  285.                 fseek(bm, hdrpos, SEEK_SET);
  286.                 fwrite(&msg, sizeof(MSGDAT), 1, bm);
  287.  
  288.                 fseek(bm, oldpos, SEEK_SET);
  289.                 printf("\nMessage #%ld complete...", i+1);
  290.             }
  291.             fclose(f);
  292.         }
  293.         else
  294.             rc = 4;
  295.         fclose(bm);
  296.     }
  297.     else
  298.         rc = 3;
  299.  
  300.     return(rc);
  301. }
  302.  
  303. long WriteMsgBlocks(FILE *f, FILE *bm)
  304. {
  305.     char buf[128];
  306.     char blk[128];
  307.     int blkpos;
  308.     int ppos;
  309.     char *p;
  310.     long lc;
  311.     long blocks;
  312.  
  313.     memset(blk, ' ', sizeof(blk));
  314.     if (strlen(inetaddr))
  315.     {
  316.         sprintf(buf, "To: %sππ", inetaddr);
  317.         strncpy(blk, buf, strlen(buf));
  318.         blkpos = strlen(buf);
  319.     }
  320.     else
  321.         blkpos = 0;
  322.  
  323.     blocks = 0;
  324.     lc = 0;
  325.     while (fgets(buf, sizeof(buf), f)!=NULL)
  326.     {
  327.         p = strrchr(buf, '\n');
  328.         if (p)
  329.             *p = 'π';
  330.         else
  331.             strcat(buf, "π");
  332.         p = blk;
  333.         p += blkpos;
  334.         ppos = sizeof(buf) - blkpos;
  335.         if (strlen(buf)>=ppos)
  336.         {
  337.             strncpy(p, buf, ppos);
  338.             fwrite(blk, sizeof(blk), 1, bm);
  339.             memset(blk, ' ', sizeof(blk));
  340.             if (strlen(buf)-ppos)
  341.             {
  342.                 strncpy(blk, &buf[ppos], strlen(buf) - ppos);
  343.                 blkpos = strlen(buf) - ppos;
  344.             }
  345.             else
  346.                 blkpos = 0;
  347.             blocks++;
  348.         }
  349.         else
  350.         {
  351.             strncpy(p, buf, strlen(buf));
  352.             blkpos += strlen(buf);
  353.         }
  354.         lc++;
  355.         if (lc >= lines)
  356.             break;
  357.     }
  358.     if (blkpos)
  359.     {
  360.         fwrite(blk, sizeof(blk), 1, bm);
  361.         blocks++;
  362.     }
  363.     return(blocks);
  364. }
  365.