home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / comms / comprgs / osrc_149.lzh / ommmmake.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-08  |  14.2 KB  |  526 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.  
  55. #include    "stat.h" 
  56.  
  57. #    include    <dos.h>
  58.  
  59. /*--------------------------------------------------------------------------*/
  60. /* Static function declarations                                             */
  61. /*--------------------------------------------------------------------------*/
  62.  
  63.  /* ... NONE ...  */
  64.  
  65. /*--------------------------------------------------------------------------*/
  66. /* Static variable definitions                                              */
  67. /*--------------------------------------------------------------------------*/
  68.  
  69. static FILE    *                fh;
  70. static FILE    *                flo = NULL;
  71. static FILE    *                req = NULL;
  72. static struct _msg            msg;
  73. static struct _msghdr        msghdr;
  74. static struct _msglist *    numbers;
  75.  
  76. /*--------------------------------------------------------------------------*/
  77. /* External variable declarations                                           */
  78. /*--------------------------------------------------------------------------*/
  79.  
  80.  /* ... NONE ...  */
  81.  
  82. /*--------------------------------------------------------------------------*/
  83. /* Local Definitions                                                        */
  84. /*--------------------------------------------------------------------------*/
  85.  
  86. #ifdef    ZTC
  87. #    define    O_BINARY    0
  88. #endif
  89.  
  90. /****************************************************************************/
  91.  
  92. /*--------------------------------------------------------------------------*/
  93. /* MAKE BUNDLE                                                              */
  94. /*--------------------------------------------------------------------------*/
  95.  
  96.  void
  97. make_bundle()
  98. {
  99.     char    flagchar = 'O';
  100.     char    oldflag;
  101.     char    messagefile[64];
  102.     char    file_attached[64];
  103.     char    file_path[64];
  104.     char    flist[64];
  105.     char    file_name[64];
  106.  
  107.  
  108.     /*--------------------------------------------------------------------*/
  109.     /* Build OUT files                                                    */
  110.     /*--------------------------------------------------------------------*/
  111.  
  112. #    if defined(MSC)
  113.         tzset();        /* not supported currently by the zortech library    */
  114. #    endif
  115.  
  116.     buffer = malloc(BUFFER_SIZE + 1);
  117.     if (!buffer) {
  118.         printf("MEM");
  119.         return;
  120.     }
  121.     outfile = NULL;
  122.  
  123.     while (root) {
  124.  
  125.         /*--------------------------------------------------------------*/
  126.         /* Copy messages to the OUT file                                */
  127.         /*--------------------------------------------------------------*/
  128.  
  129.         numbers = root->firstmsg;
  130.         while (numbers) {
  131.  
  132.             printf("\r%-4d ", numbers->number);
  133.             fflush(stdout);
  134.  
  135.             /*--------------------------------------------------------*/
  136.             /* Open the message file and read its header              */
  137.             /*--------------------------------------------------------*/
  138.  
  139.             sprintf(template, "%s%d.MSG", message_path, numbers->number);
  140.             strcpy(messagefile, template);
  141.  
  142.             errno = 0;
  143.             if ((fh = fopen(template, "r+b")) == NULL) {
  144.                 cputs("\nCan't open ");
  145.                 perror(template);
  146.                 continue;
  147.             }
  148.  
  149.             if (fread(&msg, 1, sizeof(struct _msg),fh) != sizeof(struct _msg)) {
  150.                 cputs("\nHeader corrupted ");
  151.                 perror(template);
  152.                 continue;
  153.             }
  154.  
  155.             /* swap_msg(&msg); amigaspec */ 
  156.             if (msg.attr & MSGFILE)
  157.                 flagchar = 'D';
  158.  
  159.             if (msg.attr & MSGSENT) {
  160.                 fclose(fh);
  161.                 fh = NULL;
  162.                 continue;
  163.             }
  164.  
  165.             /*--------------------------------------------------------*/
  166.             /* Create/Append REQ file for file requests               */
  167.             /*--------------------------------------------------------*/
  168.  
  169.             else if ((msg.attr & MSGFRQ) || (msg.attr & MSGURQ)) {
  170.                 if (!req) {
  171.                     sprintf(template, "%s%04x%04x.REQ",
  172.                         adjust_packet_path(root->id3), root->id1, root->id2);
  173.                     req = fopen(template, "at");
  174.                 }
  175.  
  176.                 j = 0;
  177.                 flist[0] = 0;
  178.  
  179.                 {    /* file requests */
  180.                     long    timediff;
  181.                     FILE   *fp = NULL;
  182.                     time_t  reqtime = time(NULL);
  183.                     time_t  updtime = 0;
  184.                     time_t  filetime = 0;
  185.                     char   *path = NULL;
  186.                     char   *name = NULL;
  187.                     char   *password = NULL;
  188.                     char    buffer[128];
  189.                     char   *s = NULL, *t = NULL, *p = NULL;
  190.                     struct _dta fileinfo;
  191.                     struct    stat buf;
  192.                     long    filesize = 0;
  193.                     int    status;
  194.  
  195.                     fileinfo.fib=NULL;
  196.  
  197.                     if (msg.attr & MSGURQ) {
  198.                         strcpy(template,adjust_packet_path(root->id3));
  199.                         fp = fopen(strcat(template, "requests.upd"), "at");
  200.                     }
  201.  
  202.                     strncpy(buffer, msg.subj, 72);
  203.                     s = strtok(buffer, " ,");
  204.  
  205. #                    if defined(MSC)
  206.                         timediff = timezone;
  207. #                    else
  208.                         timediff = 0;
  209. #                    endif
  210.  
  211.                     while (s != NULL) {
  212.  
  213.                         /* get the first file name */
  214.                         filetime = 0;
  215.  
  216.                         if ((t = strchr(s,'!')) != NULL)
  217.                             *t = '\0';
  218.  
  219.                         status = dir_findfirst(s, 0, &fileinfo);
  220.  
  221.                         if (t)
  222.                             *t = '!';
  223.  
  224.                         /* strip out the pathname */
  225.  
  226.                         if ((t = strrchr(s, '/')) == NULL)
  227.                             t = strrchr(s, '/');
  228.                         if (t == NULL) {
  229.                             path = getcwd(NULL, 64);
  230.                             name = s;
  231.                         } else {
  232.                             *t = NULL;
  233.                             path = strdup(s);
  234.                             *t++ = '/';
  235.                             name = t;
  236.                         }
  237.  
  238.                         /*
  239.                          * get the next token and
  240.                          * check for a password 
  241.                          */
  242.  
  243.                         s = strtok(NULL, " ,");
  244.  
  245.                         if ((s != NULL) && (*s == '!')) {
  246.                             password = strdup(s + 1);
  247.                             s = strtok(NULL, " ,");
  248.                         }
  249.  
  250.                         if ((t = strchr(name, '!')) != NULL) {
  251.                             if (password != NULL)
  252.                                 free(password);
  253.                             password = strdup(t + 1);
  254.                             *t = '\0';
  255.                         }
  256.  
  257.                         /*
  258.                          * process each file matching
  259.                          * the filespec 
  260.                          */
  261.  
  262.                         p = malloc(strlen(path) + 14 + 2);
  263.  
  264.                         while ((status == 0) && (msg.attr & MSGURQ)) {
  265.                             filesize = max(fileinfo.size,filesize);
  266.                             strcpy(p,path);
  267.                             strcat(p,"/");
  268.                             strncat(p,fileinfo.name,14);
  269.                             stat(p,&buf);
  270.                             filetime = max(buf.st_atime,filetime) - timediff;
  271.                             status = dir_findnext(&fileinfo);
  272.                         }
  273.  
  274.                         free(p);
  275.                         updtime = filetime;
  276.  
  277.                         if (name)
  278.                             fputs(name,req);
  279.  
  280.                         if (password)
  281.                             fprintf(req," !%s",password);
  282.  
  283.                         if (msg.attr & MSGURQ)
  284.                             fprintf(req," +%ld",filetime);
  285.  
  286.                         fprintf(req,"\n");
  287.  
  288.                         if (msg.attr & MSGURQ)
  289.                             fprintf(fp, "%s %s %+ld %d:%d/%d %ld %ld %ld\n", name, path, updtime, root->id3, root->id1, root->id2, filetime, filesize, reqtime);
  290.  
  291.                         free(path);
  292.                         path = NULL;
  293.                         if (password != NULL) {
  294.                             free(password);
  295.                             password = NULL;
  296.                         }
  297.                     }
  298.  
  299.                     if (fp)
  300.                         fclose(fp);
  301.                     fp = NULL;
  302.                 }
  303.  
  304.                 fclose(fh);
  305.                 fh = NULL;
  306.  
  307.                 if (!keepmsgs)
  308.                     unlink(messagefile);
  309.                 else {
  310.                     if (!pms) {
  311.                         sprintf(template, "%s%04x%04x.PMS", adjust_packet_path(root->id3), root->id1, root->id2);
  312.                         pms = fopen(template, "at");
  313.                     }
  314.                     fprintf(pms, "%d.MSG\n", numbers->number);
  315.                 }
  316.  
  317.  
  318.                 if (msg.attr & MSGFRQ) {
  319.                     printf(" ... To: %d:%d/%d (REQ)", root->id3,root->id1, root->id2);
  320.                     fflush(stdout);
  321.                 }
  322.  
  323.                 if (msg.attr & MSGURQ) {
  324.                     printf(" ... To: %d:%d/%d (UPDATE)", root->id3,root->id1, root->id2);
  325.                     fflush(stdout);
  326.                 }
  327.  
  328.                 numbers = numbers->next;
  329.                 continue;
  330.             }
  331.  
  332.             oldflag = flagchar;
  333.  
  334.             switch (flagchar) {
  335.  
  336.             case 'O':
  337.                 if ((msg.attr & MSGCRASH) || (msg.attr & MSGHOLD)) {
  338.                     flagchar = (char) ((msg.attr & MSGHOLD) ? 'H' : 'C');
  339.                     if (outfile)
  340.                         fwrite("\0\0", 1,2,outfile);
  341.                     open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  342.                 }
  343.                 break;
  344.  
  345.             case 'D':
  346.                 if ((msg.attr & MSGCRASH) || (msg.attr & MSGHOLD)) {
  347.                     flagchar = (char) ((msg.attr & MSGHOLD) ? 'H' : 'C');
  348.                     if (outfile)
  349.                         fwrite("\0\0", 1,2,outfile);
  350.                     open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  351.                 }
  352.                 break;
  353.  
  354.             case 'C':
  355.                 if (!(msg.attr & MSGCRASH)) {
  356.                     flagchar = (char) ((msg.attr & MSGHOLD) ? 'H' : 'O');
  357.                     if (outfile)
  358.                         fwrite("\0\0", 1,2,outfile);
  359.                     open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  360.                 }
  361.                 break;
  362.  
  363.             case 'H':
  364.                 if (!(msg.attr & MSGHOLD)) {
  365.                     flagchar = (char) ((msg.attr & MSGCRASH) ? 'C' : 'O');
  366.                     if (outfile)
  367.                         fwrite("\0\0", 1, 2, outfile);
  368.                     open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  369.                 }
  370.                 break;
  371.  
  372.             }
  373.  
  374.             if (outfile == NULL) {
  375.                 open_outfile(flagchar, root->id1, root->id2, root->id3, 1);
  376.                 if (outfile == NULL) {
  377.                     printf("OPEN");
  378.                     free(buffer);
  379.                     return;
  380.                 }
  381.             }
  382.  
  383.             /*--------------------------------------------------------*/
  384.             /* Copy message header                                    */
  385.             /*--------------------------------------------------------*/
  386.  
  387.             msghdr.ver = BUNDLE_VERSION;
  388.             msghdr.orignode = msg.orig;
  389.             msghdr.destnode = msg.dest;
  390.             msghdr.orignet = msg.orig_net;
  391.             msghdr.destnet = msg.dest_net;
  392.             msghdr.attr = (msg.attr & RETAIN);
  393.             msghdr.cost = 0;
  394.             errno = 0;
  395.             swap_msghdr(&msghdr);
  396.             fwrite((char *) &msghdr, sizeof(struct _msghdr),1,outfile);
  397.             swap_msghdr(&msghdr);
  398.             if (errno) {
  399.                 perror("\nError writing header");
  400.                 continue;
  401.             }
  402.  
  403.             if (date_conv)
  404.                 convert_date(msg.date);
  405.             fwrite(msg.date, 1,1 + strlen(msg.date),outfile);
  406.             fwrite(msg.to, 1,1 + strlen(msg.to),outfile);
  407.             fwrite(msg.from, 1,1 + strlen(msg.from),outfile);
  408.  
  409.             /*--------------------------------------------------------*/
  410.             /* Create/Append FLO file for file attaches               */
  411.             /*--------------------------------------------------------*/
  412.  
  413.             if (((msg.attr & (MSGLOCAL | MSGFILE)) == (MSGLOCAL | MSGFILE)) || ((msg.attr & (MSGFWD | MSGFILE)) == (MSGFWD | MSGFILE))) {
  414.                 if (!flo) {
  415.                     if (flagchar == 'O')
  416.                         flagchar = 'F';
  417.                     sprintf(template, "%s%04x%04x.%cLO", adjust_packet_path(root->id3), root->id1, root->id2, flagchar);
  418.                     flo = fopen(template, "at");
  419.                 }
  420.                 j = 0;
  421.                 flist[0] = 0;
  422.                 while (msg.subj[j]) {
  423.                     if (msg.subj[j] == ' ') {
  424.                         ++j;
  425.                         continue;
  426.                     }
  427.                     for (i = j; msg.subj[i]; i++) {
  428.                         if (msg.subj[i] == ' ') {
  429.                             break;
  430.                         } else
  431.                             file_attached[i - j] = msg.subj[i];
  432.                     }
  433.                     file_attached[i - j] = 0;
  434.                     j = i;
  435.                     if (!dir_findfirst(file_attached, NORMAL, &dta)) {
  436.                         extract_parts(file_path, file_name, flist, file_attached);
  437.                         fputs(file_path, flo);
  438.                         fputs(dta.name, flo);
  439.                         while (!dir_findnext(&dta)) {
  440.                             fputc('\n', flo);
  441.                             fputs(file_path, flo);
  442.                             fputs(dta.name, flo);
  443.                         }
  444.                         fputc('\n', flo);
  445.                     } else {
  446.                         printf(" can't find file '%s' - not sent\n", file_attached);
  447.                         fflush(stdout);
  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.                 /* swap_msg(&msg); amigaspec */
  485.                 fwrite((char *) &msg, sizeof(struct _msg),1,fh);
  486.                 /* swap_msg(&msg); */
  487.                 fclose(fh);
  488.                 fh = NULL;
  489.             }
  490.  
  491.             numbers = numbers->next;
  492.             flagchar = oldflag;
  493.         }
  494.  
  495.         putc('\n', stdout);
  496.         if (outfile != NULL) {
  497.             fwrite("\0\0", 1, 2, outfile);
  498.             fclose(outfile);
  499.             outfile = NULL;
  500.         }
  501.         flagchar = 'O';
  502.  
  503.         if (flo) {
  504.             fclose(flo);
  505.             flo = NULL;
  506.         }
  507.  
  508.         if (req) {
  509.             fclose(req);
  510.             req = NULL;
  511.         }
  512.  
  513.         if (pms && keepmsgs) {
  514.             fclose(pms);
  515.             pms = NULL;
  516.         }
  517.  
  518.         root = root->next;
  519.     }
  520.     free(buffer);
  521. }
  522.  
  523.  
  524. /*-------------------------------------------------------------------------*/
  525. /* ID: 474.17@14469  Last Changed: 08 Apr 1990 15:51:48 by max */
  526.