home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / OS2OMMM.SRC / OMMMMAKE.C < prev    next >
Text File  |  1989-02-12  |  15KB  |  524 lines

  1. /***************************************************************************/
  2. /***                                                                     ***/
  3. /***               oMMM - The Outbound Matrix Message Masher             ***/
  4. /***                      Copyright 1989 BS Software                     ***/
  5. /***                                                                     ***/
  6. /***                         FILENAME: OMMMMAKE.C                        ***/
  7. /***                                                                     ***/
  8. /***                             Bundle maker                            ***/
  9. /***                                                                     ***/
  10. /***                 Based on the original oMMM, a portion of            ***/
  11. /***               the Opus Computer-Based Conversation System           ***/
  12. /***                     Copyright 1986, Wynn Wagner III                 ***/
  13. /***                                                                     ***/
  14. /***************************************************************************/
  15. /***                                                                     ***/
  16. /***                    Tabs set at every 4th column                     ***/
  17. /***                                                                     ***/
  18. /***************************************************************************/
  19.  
  20. /*
  21.     Polytron Version Control System Comments:
  22.  
  23.     The revision of this file is *** $Revision:   1.40  $ ***
  24.  
  25.     History of changes from 1.30 release version
  26.  
  27.     $Log:   C:/OMMM/PROJFILE/OMMMMAKE.C_V  $
  28.  * 
  29.  *    Rev 1.40   12 Feb 1989  4:56:00   Marshall Presnell
  30.  * Public Release Version 1.40
  31.  * 
  32.  *    Rev 1.32   31 Jan 1989  0:58:08   Marshall Presnell
  33.  * oMMM 1.35 Beta Release Version
  34.  * 
  35.  *    Rev 1.31   30 Jan 1989 21:29:54   Marshall Presnell
  36.  * Multiple file requests from single messages now insert CR/LF in the .REQ file
  37.  * 
  38.  *    Rev 1.30   23 Jan 1989 17:53:56   Marshall Presnell
  39.  * Public Source Code Release - Version 1.30
  40.  
  41. */
  42.  
  43. /*--------------------------------------------------------------------------*/
  44. /* Include files                                                            */
  45. /*--------------------------------------------------------------------------*/
  46.  
  47. #include    "ommm.h"
  48. #include    <stdlib.h>
  49. #include    <string.h>
  50. #include    <io.h>
  51. #include    <fcntl.h>
  52. #include    <time.h>
  53.  
  54. #ifdef    TURBO_C
  55. #    include    <dir.h>
  56. #else
  57. #    include    <direct.h>
  58. #endif
  59.  
  60. #ifdef    MSC
  61. #    include    <sys\types.h>
  62. #endif
  63.  
  64. #include    <sys\stat.h>
  65.  
  66. #ifdef    ZTC
  67. #    include    <dos.h>
  68. #endif
  69.  
  70. /*--------------------------------------------------------------------------*/
  71. /* Static function declarations                                             */
  72. /*--------------------------------------------------------------------------*/
  73.  
  74.  /* ... NONE ...  */
  75.  
  76. /*--------------------------------------------------------------------------*/
  77. /* Static variable definitions                                              */
  78. /*--------------------------------------------------------------------------*/
  79.  
  80. static FILE    *                fh;
  81. static FILE    *                flo = NULL;
  82. static FILE    *                req = NULL;
  83. static struct _msg            msg;
  84. static struct _msghdr        msghdr;
  85. static struct _msglist *    numbers;
  86.  
  87. /*--------------------------------------------------------------------------*/
  88. /* External variable declarations                                           */
  89. /*--------------------------------------------------------------------------*/
  90.  
  91.  /* ... NONE ...  */
  92.  
  93. /*--------------------------------------------------------------------------*/
  94. /* Local Definitions                                                        */
  95. /*--------------------------------------------------------------------------*/
  96.  
  97. #ifdef    ZTC
  98. #    define    O_BINARY    0
  99. #endif
  100.  
  101. /****************************************************************************/
  102.  
  103. /*--------------------------------------------------------------------------*/
  104. /* MAKE BUNDLE                                                              */
  105. /*--------------------------------------------------------------------------*/
  106.  
  107.  void
  108. make_bundle()
  109. {
  110.     char    flagchar = 'O';
  111.     char    oldflag;
  112.     char    messagefile[64];
  113.     char    file_attached[64];
  114.     char    file_path[64];
  115.     char    flist[64];
  116.     char    file_name[64];
  117.  
  118.  
  119.     /*--------------------------------------------------------------------*/
  120.     /* Build OUT files                                                    */
  121.     /*--------------------------------------------------------------------*/
  122.  
  123. #    if defined(MSC)
  124.         tzset();        /* not supported currently by the zortech library    */
  125. #    endif
  126.  
  127.     buffer = malloc(BUFFER_SIZE + 1);
  128.     if (!buffer) {
  129.         printf("MEM");
  130.         return;
  131.     }
  132.     outfile = NULL;
  133.  
  134.     while (root) {
  135.  
  136.         /*--------------------------------------------------------------*/
  137.         /* Copy messages to the OUT file                                */
  138.         /*--------------------------------------------------------------*/
  139.  
  140.         numbers = root->firstmsg;
  141.         while (numbers) {
  142.  
  143.             printf("\r%-4d ", numbers->number);
  144.  
  145.             /*--------------------------------------------------------*/
  146.             /* Open the message file and read its header              */
  147.             /*--------------------------------------------------------*/
  148.  
  149.             sprintf(template, "%s%d.MSG", message_path, numbers->number);
  150.             strcpy(messagefile, template);
  151.  
  152.             errno = 0;
  153.             if ((fh = fopen(template, "r+b")) == NULL) {
  154.                 cputs("\nCan't open ");
  155.                 perror(template);
  156.                 continue;
  157.             }
  158.  
  159.             if (fread(&msg, 1, sizeof(struct _msg),fh) != sizeof(struct _msg)) {
  160.                 cputs("\nHeader corrupted ");
  161.                 perror(template);
  162.                 continue;
  163.             }
  164.  
  165.             if (msg.attr & MSGFILE)
  166.                 flagchar = 'D';
  167.  
  168.             if (msg.attr & MSGSENT) {
  169.                 fclose(fh);
  170.                 fh = NULL;
  171.                 continue;
  172.             }
  173.  
  174.             /*--------------------------------------------------------*/
  175.             /* Create/Append REQ file for file requests               */
  176.             /*--------------------------------------------------------*/
  177.  
  178.             else if ((msg.attr & MSGFRQ) || (msg.attr & MSGURQ)) {
  179.                 if (!req) {
  180.                     sprintf(template, "%s%04x%04x.REQ",
  181.                         adjust_packet_path(root->id3), root->id1, root->id2);
  182.                     req = fopen(template, "at");
  183.                 }
  184.  
  185.                 j = 0;
  186.                 flist[0] = 0;
  187.  
  188.                 {    /* file requests */
  189.                     long    timediff;
  190.                     FILE   *fp = NULL;
  191.                     time_t  reqtime = time(NULL);
  192.                     time_t  updtime = 0;
  193.                     time_t  filetime = 0;
  194.                     char   *path = NULL;
  195.                     char   *name = NULL;
  196.                     char   *password = NULL;
  197.                     char    buffer[128];
  198.                     char   *s = NULL, *t = NULL, *p = NULL;
  199.                     struct _dta fileinfo;
  200.                     struct    stat buf;
  201.                     long    filesize = 0;
  202.                     int    status;
  203.  
  204.                     if (msg.attr & MSGURQ) {
  205.                         strcpy(template,adjust_packet_path(root->id3));
  206.                         fp = fopen(strcat(template, "requests.upd"), "at");
  207.                     }
  208.  
  209.                     strncpy(buffer, msg.subj, 72);
  210.                     s = strtok(buffer, " ,");
  211.  
  212. #                    if defined(MSC)
  213.                         timediff = timezone;
  214. #                    else
  215.                         timediff = 0;
  216. #                    endif
  217.  
  218.                     while (s != NULL) {
  219.  
  220.                         /* get the first file name */
  221.                         filetime = 0;
  222.  
  223.                         if ((t = strchr(s,'!')) != NULL)
  224.                             *t = '\0';
  225.  
  226.                         status = dir_findfirst(s, 0, &fileinfo);
  227.  
  228.                         if (t)
  229.                             *t = '!';
  230.  
  231.                         /* strip out the pathname */
  232.  
  233.                         if ((t = strrchr(s, '\\')) == NULL)
  234.                             t = strrchr(s, '/');
  235.                         if (t == NULL) {
  236.                             path = getcwd(NULL, 64);
  237.                             name = s;
  238.                         } else {
  239.                             *t = NULL;
  240.                             path = strdup(s);
  241.                             *t++ = '\\';
  242.                             name = t;
  243.                         }
  244.  
  245.                         /*
  246.                          * get the next token and
  247.                          * check for a password 
  248.                          */
  249.  
  250.                         s = strtok(NULL, " ,");
  251.  
  252.                         if ((s != NULL) && (*s == '!')) {
  253.                             password = strdup(s + 1);
  254.                             s = strtok(NULL, " ,");
  255.                         }
  256.  
  257.                         if ((t = strchr(name, '!')) != NULL) {
  258.                             if (password != NULL)
  259.                                 free(password);
  260.                             password = strdup(t + 1);
  261.                             *t = '\0';
  262.                         }
  263.  
  264.                         /*
  265.                          * process each file matching
  266.                          * the filespec 
  267.                          */
  268.  
  269.                         p = malloc(strlen(path) + 14 + 2);
  270.  
  271.                         while ((status == 0) && (msg.attr & MSGURQ)) {
  272.                             filesize = max(fileinfo.size,filesize);
  273.                             strcpy(p,path);
  274.                             strcat(p,"\\");
  275.                             strncat(p,fileinfo.name,14);
  276.                             stat(p,&buf);
  277.                             filetime = max(buf.st_atime,filetime) - timediff;
  278.                             status = dir_findnext(&fileinfo);
  279.                         }
  280.  
  281.                         free(p);
  282.                         updtime = filetime;
  283.  
  284.                         if (name)
  285.                             fputs(name,req);
  286.  
  287.                         if (password)
  288.                             fprintf(req," !%s",password);
  289.  
  290.                         if (msg.attr & MSGURQ)
  291.                             fprintf(req," +%ld",filetime);
  292.  
  293.                         fprintf(req,"\n");
  294.  
  295.                         if (msg.attr & MSGURQ)
  296.                             fprintf(fp, "%s %s %+ld %d:%d/%d %ld %ld %ld\n", name, path, updtime, root->id3, root->id1, root->id2, filetime, filesize, reqtime);
  297.  
  298.                         free(path);
  299.                         path = NULL;
  300.                         if (password != NULL) {
  301.                             free(password);
  302.                             password = NULL;
  303.                         }
  304.                     }
  305.  
  306.                     if (fp)
  307.                         fclose(fp);
  308.                     fp = NULL;
  309.                 }
  310.  
  311.                 fclose(fh);
  312.                 fh = NULL;
  313.  
  314.                 if (!keepmsgs)
  315.                     unlink(messagefile);
  316.                 else {
  317.                     if (!pms) {
  318.                         sprintf(template, "%s%04x%04x.PMS", adjust_packet_path(root->id3), root->id1, root->id2);
  319.                         pms = fopen(template, "at");
  320.                     }
  321.                     fprintf(pms, "%d.MSG\n", numbers->number);
  322.                 }
  323.  
  324.  
  325.                 if (msg.attr & MSGFRQ)
  326.                     printf(" ... To: %d:%d/%d (REQ)", root->id3,root->id1, root->id2);
  327.  
  328.                 if (msg.attr & MSGURQ)
  329.                     printf(" ... To: %d:%d/%d (UPDATE)", root->id3,root->id1, root->id2);
  330.  
  331.                 numbers = numbers->next;
  332.                 continue;
  333.             }
  334.  
  335.             oldflag = flagchar;
  336.  
  337.             switch (flagchar) {
  338.  
  339.             case 'O':
  340.                 if ((msg.attr & MSGCRASH) || (msg.attr & MSGHOLD)) {
  341.                     flagchar = (char) ((msg.attr & MSGHOLD) ? 'H' : 'C');
  342.                     if (outfile)
  343.                         fwrite("\0\0", 1,2,outfile);
  344.                     open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  345.                 }
  346.                 break;
  347.  
  348.             case 'D':
  349.                 if ((msg.attr & MSGCRASH) || (msg.attr & MSGHOLD)) {
  350.                     flagchar = (char) ((msg.attr & MSGHOLD) ? 'H' : 'C');
  351.                     if (outfile)
  352.                         fwrite("\0\0", 1,2,outfile);
  353.                     open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  354.                 }
  355.                 break;
  356.  
  357.             case 'C':
  358.                 if (!(msg.attr & MSGCRASH)) {
  359.                     flagchar = (char) ((msg.attr & MSGHOLD) ? 'H' : 'O');
  360.                     if (outfile)
  361.                         fwrite("\0\0", 1,2,outfile);
  362.                     open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  363.                 }
  364.                 break;
  365.  
  366.             case 'H':
  367.                 if (!(msg.attr & MSGHOLD)) {
  368.                     flagchar = (char) ((msg.attr & MSGCRASH) ? 'C' : 'O');
  369.                     if (outfile)
  370.                         fwrite("\0\0", 1, 2, outfile);
  371.                     open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  372.                 }
  373.                 break;
  374.  
  375.             }
  376.  
  377.             if (outfile == NULL) {
  378.                 open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  379.                 if (outfile == NULL) {
  380.                     printf("OPEN");
  381.                     free(buffer);
  382.                     return;
  383.                 }
  384.             }
  385.  
  386.             /*--------------------------------------------------------*/
  387.             /* Copy message header                                    */
  388.             /*--------------------------------------------------------*/
  389.  
  390.             msghdr.ver = BUNDLE_VERSION;
  391.             msghdr.orignode = msg.orig;
  392.             msghdr.destnode = msg.dest;
  393.             msghdr.orignet = msg.orig_net;
  394.             msghdr.destnet = msg.dest_net;
  395.             msghdr.attr = (msg.attr & RETAIN);
  396.             msghdr.cost = 0;
  397.             errno = 0;
  398.             fwrite((char *) &msghdr, sizeof(struct _msghdr),1,outfile);
  399.             if (errno) {
  400.                 perror("\nError writing header");
  401.                 continue;
  402.             }
  403.  
  404.             if (date_conv)
  405.                 convert_date(msg.date);
  406.             fwrite(msg.date, 1,1 + strlen(msg.date),outfile);
  407.             fwrite(msg.to, 1,1 + strlen(msg.to),outfile);
  408.             fwrite(msg.from, 1,1 + strlen(msg.from),outfile);
  409.  
  410.             /*--------------------------------------------------------*/
  411.             /* Create/Append FLO file for file attaches               */
  412.             /*--------------------------------------------------------*/
  413.  
  414.             if (((msg.attr & (MSGLOCAL | MSGFILE)) == (MSGLOCAL | MSGFILE)) || ((msg.attr & (MSGFWD | MSGFILE)) == (MSGFWD | MSGFILE))) {
  415.                 if (!flo) {
  416.                     if (flagchar == 'O')
  417.                         flagchar = 'F';
  418.                     sprintf(template, "%s%04x%04x.%cLO", adjust_packet_path(root->id3), root->id1, root->id2, flagchar);
  419.                     flo = fopen(template, "at");
  420.                 }
  421.                 j = 0;
  422.                 flist[0] = 0;
  423.                 while (msg.subj[j]) {
  424.                     if (msg.subj[j] == ' ') {
  425.                         ++j;
  426.                         continue;
  427.                     }
  428.                     for (i = j; msg.subj[i]; i++) {
  429.                         if (msg.subj[i] == ' ') {
  430.                             break;
  431.                         } else
  432.                             file_attached[i - j] = msg.subj[i];
  433.                     }
  434.                     file_attached[i - j] = 0;
  435.                     j = i;
  436.                     if (!dir_findfirst(file_attached, NORMAL, &dta)) {
  437.                         extract_parts(file_path, file_name, flist, file_attached);
  438.                         fputs(file_path, flo);
  439.                         fputs(dta.name, flo);
  440.                         while (!dir_findnext(&dta)) {
  441.                             fputc('\n', flo);
  442.                             fputs(file_path, flo);
  443.                             fputs(dta.name, flo);
  444.                         }
  445.                         fputc('\n', flo);
  446.                     } else {
  447.                         printf(" can't find file '%s' - not sent\n", file_attached);
  448.                     }
  449.                 }
  450.                 fwrite(flist, 1,1 + strlen(flist),outfile);
  451.             } else
  452.                 fwrite(msg.subj, 1,1 + strlen(msg.subj),outfile);
  453.  
  454.             /*--------------------------------------------------------*/
  455.             /* Copy message body                                      */
  456.             /*--------------------------------------------------------*/
  457.  
  458.             copy_msg(fh, outfile, buffer, BUFFER_SIZE);
  459.  
  460.             /*--------------------------------------------------------*/
  461.             /* Handle KILL/SENT messages                              */
  462.             /*--------------------------------------------------------*/
  463.  
  464.             if (msg.attr & (MSGKILL | MSGFWD | MSGCPT | MSGFRQ | MSGURQ)) {
  465.                 fclose(fh);
  466.                 fh = NULL;
  467.                 if (!keepmsgs)
  468.                     unlink(messagefile);
  469.                 else {
  470.                     if (!pms) {
  471.                         sprintf(template, "%s%04x%04x.PMS", adjust_packet_path(root->id3), root->id1, root->id2);
  472.                         pms = fopen(template, "at");
  473.                     }
  474.                     fprintf(pms, "%d.MSG\n", numbers->number);
  475.                 }
  476.  
  477.                 /*--------------------------------------------------------*/
  478.                 /* Mark as SENT for non-Kill/Sent messages                */
  479.                 /*--------------------------------------------------------*/
  480.  
  481.             } else {
  482.                 fseek(fh, 0L, SEEK_SET);
  483.                 msg.attr |= MSGSENT;
  484.                 fwrite((char *) &msg, sizeof(struct _msg),1,fh);
  485.                 fclose(fh);
  486.                 fh = NULL;
  487.             }
  488.  
  489.             numbers = numbers->next;
  490.             flagchar = oldflag;
  491.         }
  492.  
  493.         putc('\n', stdout);
  494.         if (outfile != NULL) {
  495.             fwrite("\0\0", 1, 2, outfile);
  496.             fclose(outfile);
  497.             outfile = NULL;
  498.         }
  499.         flagchar = 'O';
  500.  
  501.         if (flo) {
  502.             fclose(flo);
  503.             flo = NULL;
  504.         }
  505.  
  506.         if (req) {
  507.             fclose(req);
  508.             req = NULL;
  509.         }
  510.  
  511.         if (pms && keepmsgs) {
  512.             fclose(pms);
  513.             pms = NULL;
  514.         }
  515.  
  516.         root = root->next;
  517.     }
  518.     free(buffer);
  519. }
  520.  
  521. /*--------------------------------------------------------------------------*/
  522. /*                                END OF FILE                               */
  523. /*--------------------------------------------------------------------------*/
  524.