home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / showmsg.c < prev    next >
C/C++ Source or Header  |  1992-03-29  |  16KB  |  493 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: showmsg.c,v 4.1.1.1 90/07/12 23:04:26 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    showmsg.c,v $
  17.  * Revision 4.1.1.1  90/07/12  23:04:26  syd
  18.  * Fix MMDF case, where MSG_SEPERATOR has newline, buffer check
  19.  * didnt, thus it didnt detect the MSG_SEPERATOR.
  20.  * From: jbwaters@bsu-cs.bsu.edu (J. Brian Waters)
  21.  *
  22.  * Revision 4.1  90/04/28  22:44:06  syd
  23.  * checkin of Elm 2.3 as of Release PL0
  24.  *
  25.  *
  26.  ******************************************************************************/
  27.  
  28. /** This file contains all the routines needed to display the specified
  29.     message.
  30. **/
  31.  
  32. #include "headers.h"
  33. #include <ctype.h>
  34. #include <errno.h>
  35.  
  36. #ifdef BSD
  37. # include <sys/wait.h>
  38. # undef       tolower
  39. #endif
  40.  
  41. #ifndef OS2
  42. extern int errno;
  43. #endif
  44.  
  45. char *error_name(), *strcat(), *strcpy();
  46. void   _exit();
  47.  
  48. int    memory_lock = FALSE;    /* is it available?? */
  49. int    pipe_abort  = FALSE;    /* did we receive a SIGNAL(SIGPIPE)? */
  50.  
  51. FILE *pipe_wr_fp;        /* file pointer to write to external pager */
  52. extern int lines_displayed,    /* defined in "builtin" */
  53.        lines_put_on_screen;    /*    ditto too!        */
  54.  
  55. int
  56. show_msg(number)
  57. int number;
  58. {
  59.     /*** Display number'th message.  Get starting and ending lines
  60.          of message from headers data structure, then fly through
  61.          the file, displaying only those lines that are between the
  62.          two!
  63.  
  64.          Return 0 to return to the index screen or a character entered
  65.          by the user to initiate a command without returning to
  66.          the index screen (to be processed via process_showmsg_cmd()).
  67.     ***/
  68.  
  69.     char title1[SLEN], title2[SLEN], title3[SLEN], titlebuf[SLEN];
  70.     char who[LONG_STRING], buffer[VERY_LONG_STRING];
  71. #if defined(BSD) && !defined(WEXITSTATUS)
  72.     union wait wait_stat;
  73. #else
  74.     int wait_stat;
  75. #endif
  76.  
  77.     int crypted = 0;            /* encryption */
  78.     int weed_header, weeding_out = 0;    /* weeding    */
  79.     int using_to,                /* misc use   */
  80.         pipe_fd[2],                /* pipe file descriptors */
  81.         new_pipe_fd,            /* dup'ed pipe fil des */
  82.         lines,                /* num lines in msg */
  83.         fork_ret,                /* fork return value */
  84.         wait_ret,                /* wait return value */
  85.         form_letter = FALSE,        /* Form ltr?  */
  86.         form_letter_section = 0,        /* section    */
  87.         padding = 0,            /*   counter  */
  88.         builtin = FALSE,            /* our pager? */
  89.         val = 0,                /* return val */
  90.         buf_len;                /* line length */
  91.     struct header_rec *current_header = headers[number-1];
  92.  
  93.  
  94.     lines = current_header->lines;
  95.  
  96.     dprint(4, (debugfile,"displaying %d lines from message %d using %s\n",
  97.         lines, number, pager));
  98.  
  99.     if (number > message_count || number < 1)
  100.       return(val);
  101.  
  102.     if(ison(current_header->status, NEW)) {
  103.       clearit(current_header->status, NEW);   /* it's been read now! */
  104.       current_header->status_chgd = TRUE;
  105.     }
  106.     if(ison(current_header->status, UNREAD)) {
  107.       clearit(current_header->status, UNREAD);   /* it's been read now! */
  108.       current_header->status_chgd = TRUE;
  109.     }
  110.  
  111.     memory_lock = FALSE;
  112.  
  113.     /* some explanation for that last one - We COULD use memory locking
  114.        to speed up the paging, but the action of "ClearScreen" on a screen
  115.        with memory lock turned on seems to vary considerably (amazingly so)
  116.        so it's safer to only allow memory lock to be a viable bit of
  117.        trickery when dumping text to the screen in scroll mode.
  118.        Philosophical arguments should be forwarded to Bruce at the
  119.        University of Walamazoo, Australia, via ACSNet  *wry chuckle* */
  120.  
  121.     if (fseek(mailfile, current_header->offset, 0) == -1) {
  122.       dprint(1, (debugfile,
  123.           "Error: seek %d bytes into file, errno %s (show_message)\n",
  124.           current_header->offset, error_name(errno)));
  125.       error2("ELM failed seeking %d bytes into file (%s).",
  126.           current_header->offset, error_name(errno));
  127.       return(val);
  128.     }
  129.     if(current_header->encrypted)
  130.       getkey(OFF);
  131.  
  132.     if(builtin=(first_word(pager,"builtin")||first_word(pager,"internal")))
  133.  
  134.       start_builtin(lines);
  135.  
  136.     else {
  137.  
  138.       /* put terminal out of raw mode so external pager has normal env */
  139.       Raw(OFF);
  140.  
  141. #ifdef OS2
  142.           if ( (pipe_wr_fp = popen(pager, "w")) == NULL )
  143.             return -1;
  144. #else
  145.       /* create pipe for external pager and fork */
  146.  
  147.       if(pipe(pipe_fd) == -1) {
  148.         dprint(1, (debugfile, "Error: pipe failed, errno %s (show_msg)\n",
  149.           error_name(errno)));
  150.         error1("Could not prepare for external pager(pipe()-%s).",
  151.           error_name(errno));
  152.         Raw(ON);
  153.         return(val);
  154.       }
  155.  
  156.       if((fork_ret = fork()) == -1) {
  157.  
  158.         dprint(1, (debugfile, "Error: fork failed, errno %s (show_msg)\n",
  159.           error_name(errno)));
  160.         error1("Could not prepare for external pager(fork()-%s).",
  161.           error_name(errno));
  162.         Raw(ON);
  163.         return(val);
  164.  
  165.       } else if (fork_ret == 0) {
  166.  
  167.         /* child fork */
  168.  
  169.         /* close write-only pipe fd and fit read-only pipe fd to stdin */
  170.  
  171.         close(pipe_fd[1]);
  172.         close(fileno(stdin));
  173.         if((new_pipe_fd = dup(pipe_fd[0])) == -1) {
  174.           dprint(1, (debugfile, "Error: dup failed, errno %s (show_msg)\n",
  175.         error_name(errno)));
  176.           error1("Could not prepare for external pager(dup()-%s).",
  177.         error_name(errno));
  178.           _exit(errno);
  179.         }
  180.         close(pipe_fd[0]);    /* original pipe fd no longer needed */
  181.  
  182.         /* use stdio on new pipe fd */
  183.         if(fdopen(new_pipe_fd, "r") == NULL) {
  184.           dprint(1,
  185.         (debugfile, "Error: child fdopen failed, errno %s (show_msg)\n",
  186.         error_name(errno)));
  187.           error1("Could not prepare for external pager(child fdopen()-%s).",
  188.         error_name(errno));
  189.           _exit(errno);
  190.         }
  191.  
  192.         /* now execute pager and exit */
  193.  
  194.         /* system_call() will return user to user's normal permissions.
  195.          * This is what makes this pipe secure - user won't have elm's
  196.          * special setgid permissions (if so configured) and will only
  197.          * be able to execute a pager that user normally has permission
  198.          * to execute */
  199.  
  200.         _exit(system_call(pager, SH, TRUE, TRUE));
  201.  
  202.       } /* else this is the parent fork */
  203.  
  204.       /* close read-only pipe fd and do write-only with stdio */
  205.       close(pipe_fd[0]);
  206.  
  207.       if((pipe_wr_fp = fdopen(pipe_fd[1], "w")) == NULL) {
  208.         dprint(1,
  209.           (debugfile, "Error: parent fdopen failed, errno %s (show_msg)\n",
  210.           error_name(errno)));
  211.         error1("Could not prepare for external pager(parent fdopen()-%s).",
  212.           error_name(errno));
  213.  
  214.         /* Failure - must close pipe and wait for child */
  215.         close(pipe_fd[1]);
  216.         while ((wait_ret = wait(&wait_stat)) != fork_ret && wait_ret!= -1)
  217.           ;
  218.  
  219.         Raw(OFF);
  220.         return(val);    /* pager may have already touched the screen */
  221.       }
  222. #endif
  223.  
  224.       /* and that's it! */
  225.       lines_displayed = 0;
  226.     }
  227.  
  228.     ClearScreen();
  229.  
  230.     if (cursor_control) transmit_functions(OFF);
  231.  
  232.     pipe_abort = FALSE;
  233.  
  234.     if (form_letter = (current_header->status&FORM_LETTER)) {
  235.       if (filter)
  236.         form_letter_section = 1;    /* initialize to section 1 */
  237.     }
  238.  
  239.     if (title_messages && filter) {
  240.  
  241.       using_to =
  242.         tail_of(current_header->from, who, current_header->to);
  243.  
  244.       sprintf(title1, "%s %d/%d  ",
  245.             headers[current-1]->status & DELETED ? "[deleted]" :
  246.             form_letter ? "Form": "Message",
  247.             number, message_count);
  248.       sprintf(title2, "%s %s", using_to? "To" : "From", who);
  249.       sprintf(title3, "  %s %s '%d at %s %s",
  250.                 current_header->month, current_header->day,
  251.                atoi(current_header->year), current_header->time,
  252.            current_header->time_zone);
  253.  
  254.       /* truncate or pad title2 portion on the right
  255.        * so that line fits exactly */
  256.       padding =
  257.         COLUMNS - 1 -
  258.         (strlen(title1) + (buf_len=strlen(title2)) + strlen(title3));
  259.  
  260.       sprintf(titlebuf, "%s%-*.*s%s\n", title1, buf_len+padding,
  261.           buf_len+padding, title2, title3);
  262.  
  263.       if (builtin)
  264.         display_line(titlebuf);
  265.       else
  266.         fprintf(pipe_wr_fp, "%s", titlebuf);
  267.  
  268.       /** if there's a subject, let's output it next,
  269.           centered if it fits on a single line.  **/
  270.  
  271.       if ((buf_len = strlen(current_header->subject)) > 0 &&
  272.         matches_weedlist("Subject:")) {
  273.         padding = (buf_len < COLUMNS ? COLUMNS - buf_len : 0);
  274.         sprintf(buffer, "%*s%s\n", padding/2, "", current_header->subject);
  275.       } else
  276.         strcpy(buffer, "\n");
  277.  
  278.       if (builtin)
  279.         display_line(buffer);
  280.       else
  281.         fprintf(pipe_wr_fp, "%s", buffer);
  282.  
  283.       /** was this message address to us?  if not, then to whom? **/
  284.  
  285.       if (! using_to && matches_weedlist("To:") && filter &&
  286.           strcmp(current_header->to,username) != 0 &&
  287.           strlen(current_header->to) > 0) {
  288.         if (strlen(current_header->to) > 60)
  289.           sprintf(buffer, "%s(message addressed to %.60s)\n",
  290.                 strlen(current_header->subject) > 0 ? "\n" : "",
  291.             current_header->to);
  292.         else
  293.           sprintf(buffer, "%s(message addressed to %s)\n",
  294.                 strlen(current_header->subject) > 0 ? "\n" : "",
  295.             current_header->to);
  296.         if (builtin)
  297.           display_line(buffer);
  298.         else
  299.           fprintf(pipe_wr_fp, "%s", buffer);
  300.       }
  301.  
  302.       /** The test above is: if we didn't originally send the mail
  303.           (e.g. we're not reading "mail.sent") AND the user is currently
  304.           weeding out the "To:" line (otherwise they'll get it twice!)
  305.           AND the user is actually weeding out headers AND the message
  306.           wasn't addressed to the user AND the 'to' address is non-zero
  307.           (consider what happens when the message doesn't HAVE a "To:"
  308.           line...the value is NULL but it doesn't match the username
  309.           either.  We don't want to display something ugly like
  310.           "(message addressed to )" which will just clutter up the
  311.           screen!).
  312.  
  313.           And you thought programming was EASY!!!!
  314.       **/
  315.  
  316.       /** one more friendly thing - output a line indicating what sort
  317.           of status the message has (e.g. Urgent etc).  Mostly added
  318.           for X.400 support, this is nonetheless generally useful to
  319.           include...
  320.       **/
  321.  
  322.       buffer[0] = '\0';
  323.  
  324.       /* we want to flag Urgent, Confidential, Private and Expired tags */
  325.  
  326.       if (current_header->status & PRIVATE)
  327.         strcpy(buffer, "\n(** This message is tagged Private");
  328.       else if (current_header->status & CONFIDENTIAL)
  329.         strcpy(buffer, "\n(** This message is tagged Company Confidential");
  330.  
  331.       if (current_header->status & URGENT) {
  332.         if (buffer[0] == '\0')
  333.           strcpy(buffer, "\n(** This message is tagged Urgent");
  334.         else if (current_header->status & EXPIRED)
  335.           strcat(buffer, ", Urgent");
  336.         else
  337.           strcat(buffer, " and Urgent");
  338.       }
  339.  
  340.       if (current_header->status & EXPIRED) {
  341.         if (buffer[0] == '\0')
  342.           strcpy(buffer, "\n(** This message has Expired");
  343.         else
  344.           strcat(buffer, ", and has Expired");
  345.       }
  346.  
  347.       if (buffer[0] != '\0') {
  348.         strcat(buffer, " **)\n");
  349.         if (builtin)
  350.           display_line(buffer);
  351.         else
  352.           fprintf(pipe_wr_fp, buffer);
  353.       }
  354.  
  355.       if (builtin)            /* this is for a one-line blank    */
  356.         display_line("\n");        /*   separator between the title   */
  357.       else                /*   stuff and the actual message  */
  358.         fprintf(pipe_wr_fp, "\n");    /*   we're trying to display       */
  359.  
  360.     }
  361.  
  362.     weed_header = filter;    /* allow us to change it after header */
  363.  
  364.     while (lines > 0 && pipe_abort == FALSE) {
  365.  
  366.         if (fgets(buffer, VERY_LONG_STRING, mailfile) == NULL) {
  367.  
  368.           dprint(1, (debugfile,
  369.         "Premature end of file! Lines left = %d msg = %s (show_msg)\n",
  370.         lines, number));
  371.  
  372.           error("Premature end of file!");
  373.           sleep(2);
  374.           break;
  375.         }
  376.         if ((buf_len=strlen(buffer)) > 0)  {
  377.           if(buffer[buf_len - 1] == '\n') {
  378.             lines--;
  379.             lines_displayed++;
  380.         }
  381.               no_ret(buffer);
  382.         }
  383.  
  384.           if (strlen(buffer) == 0) {
  385.           weed_header = 0;        /* past header! */
  386.           weeding_out = 0;
  387.         }
  388.  
  389.         if (form_letter && weed_header)
  390.         /* skip it.  NEVER display random headers in forms! */;
  391.         else if (weed_header && matches_weedlist(buffer))
  392.           weeding_out = 1;     /* aha!  We don't want to see this! */
  393.         else if (buffer[0] == '[') {
  394.           if (strcmp(buffer, START_ENCODE)==0)
  395.             crypted = ON;
  396.           else if (strcmp(buffer, END_ENCODE)==0)
  397.             crypted = OFF;
  398.           else if (crypted) {
  399.                 encode(buffer);
  400.             val = show_line(buffer, builtin);
  401.           }
  402.           else
  403.             val = show_line(buffer, builtin);
  404.         }
  405.         else if (crypted) {
  406.           encode(buffer);
  407.           val = show_line(buffer, builtin);
  408.         }
  409.         else if (weeding_out) {
  410.           weeding_out = (whitespace(buffer[0]));    /* 'n' line weed */
  411.           if (! weeding_out)     /* just turned on! */
  412.             val = show_line(buffer, builtin);
  413.         }
  414.         else if (form_letter && first_word(buffer,"***") && filter) {
  415.           strcpy(buffer,
  416. "\n------------------------------------------------------------------------------\n");
  417.           val = show_line(buffer, builtin);    /* hide '***' */
  418.           form_letter_section++;
  419.         }
  420.         else if (form_letter_section == 1 || form_letter_section == 3)
  421.           /** skip this stuff - we can't deal with it... **/;
  422.         else
  423.           val = show_line(buffer, builtin);
  424.  
  425.         if (val != 0)    /* discontinue the display */
  426.           break;
  427.     }
  428.  
  429.         if (cursor_control) transmit_functions(ON);
  430.  
  431.     if (!builtin) {
  432. #ifdef OS2
  433.       pclose(pipe_wr_fp);
  434. #else
  435.       fclose(pipe_wr_fp);
  436.       while ((wait_ret = wait(&wait_stat)) != fork_ret
  437.           && wait_ret!= -1)
  438.         ;
  439. #endif
  440.       /* turn raw on **after** child terminates in case child
  441.        * doesn't put us back to cooked mode after we return ourselves
  442.        * to raw.
  443.        */
  444.       Raw(ON);
  445.       EndBold();
  446.       ClearScreen();
  447.     }
  448.  
  449.     /* If we are to prompt for a user input command and we don't
  450.      * already have one */
  451.     if ((prompt_after_pager || builtin) && val == 0) {
  452.       MoveCursor(LINES,0);
  453.       StartBold();
  454.       Write_to_screen(" Command ('i' to return to index): ", 0);
  455.       EndBold();
  456.       fflush(stdout);
  457.       val = ReadCh();
  458.     }
  459.  
  460.     if (memory_lock) EndMemlock();    /* turn it off!! */
  461.  
  462.     /* 'q' means quit current operation and pop back up to previous level -
  463.      * in this case it therefore means return to index screen.
  464.      */
  465.     return(val == 'i' || val == 'q' ? 0 : val);
  466. }
  467.  
  468. int
  469. show_line(buffer, builtin)
  470. char *buffer;
  471. int  builtin;
  472. {
  473.     /** Hands the given line to the output pipe.  'builtin' is true if
  474.         we're using the builtin pager.
  475.         Return the character entered by the user to indicate
  476.         a command other than continuing with the display (only possible
  477.         with the builtin pager), otherwise 0. **/
  478.  
  479.     strcat(buffer, "\n");
  480. #ifdef MMDF
  481.     if (strcmp(buffer, MSG_SEPERATOR) == 0)
  482.       return(0);    /* no reason to show the ending terminator */
  483. #endif /* MMDF */
  484.     if (builtin) {
  485.       return(display_line(buffer));
  486.     }
  487.     errno = 0;
  488.     fprintf(pipe_wr_fp, "%s", buffer);
  489.     if (errno != 0)
  490.       dprint(1, (debugfile, "\terror %s hit!\n", error_name(errno)));
  491.     return(0);
  492. }
  493.