home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / pcmail / part04 / email.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  5.3 KB  |  200 lines

  1. /*++
  2. /* NAME
  3. /*    email
  4. /* SUMMARY
  5. /*    manipulate work files (mail in preparation)
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    mailsh
  10. /* SYNOPSIS
  11. /*    #include "email.h"
  12. /*
  13. /*    int mail()
  14. /* DESCRIPTION
  15. /*      The functions in this module are responsible for manipulations 
  16. /*    on work files and other mail messages in preparation.
  17. /*
  18. /*      mail() can be called either when the user has selected a work file in
  19. /*    the mail box display or when the user wants to create a new letter.
  20. /*
  21. /*    The message file is displayed on the screen and the can choose
  22. /*    to print, mail, edit or delete etc. the message.
  23. /*
  24. /*    The code in this module is a little tricky, to avoid that a work
  25. /*    file exists without a metafile (for the mail box display). 
  26. /* COMMANDS
  27. /*    the program specified in the EDITOR environment variable,
  28. /*    or a system-dependent default.
  29. /* FILES
  30. /*      temporary edit file in current directory
  31. /*    work file and meta file in spool directory
  32. /* SEE ALSO
  33. /*      pager(3), pager(5), kbdinp(3), edit(3)
  34. /* AUTHOR(S)
  35. /*      W.Z. Venema
  36. /*      Eindhoven University of Technology
  37. /*      Department of Mathematics and Computer Science
  38. /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  39. /* CREATION DATE
  40. /*    Tue May 12 15:35:20 GMT+1:00 1987
  41. /* LAST MODIFICATION
  42. /*    Mon Apr  4 23:39:13 MET 1988
  43. /* VERSION/RELEASE
  44. /*    1.3
  45. /*--*/
  46.  
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #include <errno.h>
  50. #include "defs.h"
  51. #include "path.h"
  52. #include "dir.h"
  53. #include "pager.h"
  54. #include "screen.h"
  55. #include "mailsh.h"
  56. #include "status.h"
  57.  
  58. /* forward declarations */
  59.  
  60. hidden void junk_work();
  61. hidden int queue_work();
  62. hidden int edit_work();
  63. hidden int show_work();
  64. hidden int send_work();
  65. hidden int hold_work();
  66. hidden int label_work();
  67.  
  68. hidden File *workfile = 0;            /* pager file */
  69.  
  70. /* mail - user has made a choice. show message at cursor */
  71.  
  72. public int mail()
  73. {
  74.     static Screen screen[] = {
  75.     'C',    "Close",    hold_work,"Send message later, return to main menu",
  76.     'D',    "Delete",       delete,    delcurr,
  77.     'E',    "Edit",        edit_work,"Edit this message",
  78.     'M',    "Mail",        send_work,"Send this message to destination",
  79.     'P',    "Print",    print,    printcurr,
  80.     PGUP,    PgUp,        pu_pager,pageup,
  81.     PGDN,    PgDn,        pd_pager,pagedn,
  82.     UP,    "Up",           up_pager,csrup,
  83.     DOWN,    "Down",         dn_pager,csrdn,
  84.     0,    0,              show_work,
  85.     "(Reading a message in preparation)",
  86.     };
  87.     struct stat s;
  88.  
  89.     if (stat(message,&s) && errno == ENOENT)    /* if new message file */
  90.     edit_work();                /* invoke editor first */
  91.     kbdinp(screen);                /* ask disposition */
  92.     junk_work();                /* destroy mail pager file */
  93.     return(S_REDRAW);                /* say screen was changed */
  94. }
  95.  
  96. /* show_work - show work file or error message in middle window */
  97.  
  98. hidden int show_work()
  99. {
  100.     if (workfile) {                /* check pager file exists */
  101.     set_pager(workfile);            /* select existent display */
  102.     } else if (rd_pager(workfile = open_pager(),message)) {/* create display */
  103.     mesg_pager(workfile,m_msgread);        /* cannot display message */
  104.     }
  105.     ds_pager();                    /* (re)draw display */
  106.     return(0);                    /* screen is up-to-date */
  107. }
  108.  
  109. /* junk_work - destroy work file display */
  110.  
  111. hidden void junk_work()
  112. {
  113.     if (workfile) {                /* no-op if no display */
  114.     close_pager(workfile);            /* release memory */
  115.     workfile = 0;                /* say it is gone */
  116.     }
  117. }
  118.  
  119. /* edit_work - edit or create a work file */
  120.  
  121. hidden int edit_work()
  122. {
  123.     register int stat;
  124.  
  125.     if (stat = edit(message,MAILFILE)) 
  126.     errdisp(stat);                /* edit() had a problem */
  127.     junk_work();                /* force new message display */
  128.     return(S_REDRAW);                /* say screen has changed */
  129. }
  130.  
  131. /* hold_work - stop editing but do not yet mail a work file */
  132.  
  133. hidden int hold_work()
  134. {
  135.     static Screen screen[] = {
  136.     STRING,    0,              label_work,int_error,
  137.     0,    0,              0,
  138.     getsummary,
  139.     };
  140.     struct stat s;
  141.  
  142.     /*
  143.     * The user has terminated an editor session, but does not yet want
  144.     * to send the message off. The purpose of the following code is to
  145.     * ask for a one-line summary, but only if such a comment does not yet
  146.     * exist. The summary is used to identify the work file in the main
  147.     * mail box display.
  148.     */
  149.  
  150.     if (stat(message,&s) || !stat(comment,&s)) {/* no msg, or comment exists */
  151.     return(S_BREAK);            /* we are done here */
  152.     } else {
  153.     return(kbdinp(screen)|S_REDRAW);    /* ask for a summary */
  154.     }
  155. }
  156.  
  157. /* label_work - save summary line to meta file */
  158.  
  159. hidden label_work(string)
  160. char *string;
  161. {
  162.     register int stat;
  163.  
  164.     if (stat = metafile(string,comment)) {    /* try to create meta file */
  165.     errdisp(stat);                /* oops, notify the user */
  166.     return(S_REDRAW);            /* say screen has changed */
  167.     } else {
  168.     chmod(comment,0444);            /* make comments read-only */
  169.     junk_desk();                /* say mail box has changed */
  170.     return(S_BREAK);            /* say no more work */
  171.     }
  172. }
  173.  
  174. /* send_work - user wants to send work file, ask for destination */
  175.  
  176. hidden int send_work()
  177. {
  178.     static Screen screen[] = {
  179.     STRING,    0,              queue_work,int_error,
  180.     0,    0,              when,
  181.     "Press ESC to cancel. Send message to:",
  182.     };
  183.     return(kbdinp(screen)|S_REDRAW);
  184. }
  185.  
  186. /* queue_work - spool mail, delete work file and meta file */
  187.  
  188. hidden int queue_work(to)
  189. char *to;
  190. {
  191.     register int stat;
  192.  
  193.     if (stat = submit(message,to)) {
  194.     errdisp(stat);                /* cannot queue message */
  195.     return(S_REDRAW);            /* say screen has changed */
  196.     } else {
  197.     return(unspool()|S_BREAK);        /* remove work and meta file */
  198.     }
  199. }
  200.