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