home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume13 / xmail / part03 / mail.c < prev    next >
C/C++ Source or Header  |  1991-06-15  |  12KB  |  343 lines

  1. /*
  2.  * xmail - X window system interface to the mail program
  3.  *
  4.  * Copyright 1989 The University of Texas at Austin
  5.  *
  6.  * Author:    Po Cheung
  7.  * Date:    March 10, 1989
  8.  *
  9.  * Permission to use, copy, modify, and distribute this software and
  10.  * its documentation for any purpose and without fee is hereby granted,
  11.  * provided that the above copyright notice appear in all copies and that
  12.  * both that copyright notice and this permission notice appear in
  13.  * supporting documentation.  The University of Texas at Austin makes no 
  14.  * representations about the suitability of this software for any purpose.  
  15.  * It is provided "as is" without express or implied warranty.
  16.  *
  17.  * Copyright 1990 by National Semiconductor Corporation
  18.  *
  19.  * Permission to use, copy, modify, and distribute this software and its
  20.  * documentation for any purpose is hereby granted without fee, provided that
  21.  * the above copyright notice appear in all copies and that both that
  22.  * copyright notice and this permission notice appear in supporting
  23.  * documentation, and that the name of National Semiconductor Corporation not
  24.  * be used in advertising or publicity pertaining to distribution of the
  25.  * software without specific, written prior permission.
  26.  *
  27.  * NATIONAL SEMICONDUCTOR CORPORATION MAKES NO REPRESENTATIONS ABOUT THE
  28.  * SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS"
  29.  * WITHOUT EXPRESS OR IMPLIED WARRANTY.  NATIONAL SEMICONDUCTOR CORPORATION
  30.  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
  31.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO
  32.  * EVENT SHALL NATIONAL SEMICONDUCTOR CORPORATION BE LIABLE FOR ANY SPECIAL,
  33.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  34.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  35.  * OR OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  36.  * PERFORMANCE OF THIS SOFTWARE.
  37.  *
  38.  * The following software modules were created and are Copyrighted by National
  39.  * Semiconductor Corporation:
  40.  *
  41.  * 1. editMail: 
  42.  * 2. sendMail: 
  43.  *
  44.  * Author:  Michael C. Wagnitz - National Semiconductor Corporation
  45.  *
  46.  */
  47. #include "global.h"
  48. #include <sys/wait.h>
  49. #include <sys/stat.h>
  50.  
  51. #ifndef    DEFAULT_VISUAL
  52. #define    DEFAULT_VISUAL    "/usr/ucb/vi"
  53. #endif
  54. /*
  55. ** @(#)editMail() - edit a mail message using the preferred editor
  56. **
  57. ** Support is now provided for declaring the editor command as an xmail
  58. ** resource, ala xrn.  If the resource ``xmail.editorCommand'' is defined,
  59. ** it must contain an `sprintf'-able format string that provides for the
  60. ** inclusion of both a display name and the name of the file to be edited.
  61. ** If the resource declaration is not included, or does not contain the two
  62. ** required percent-s (%s) formatting strings, xmail will use the VISUAL
  63. ** resource.  If VISUAL is used, try to accommodate those editors (emacs,
  64. ** xedit...) which start their own window in X11.  We know for a fact that
  65. ** vi and ed variants do not invoke windows.  We assume any other editor
  66. ** specification does.
  67. */
  68. void
  69. editMail()
  70. {
  71.  Display    *ad;
  72.  char        *edit, *cp;
  73.  char        *Argv[50];
  74.  char        cmd[BUFSIZ];
  75.  int        i, editMail_pid;
  76.  int        status;
  77.  static XEvent    event;
  78.  
  79.  
  80.  bzero(cmd, BUFSIZ);
  81.  
  82.  ad  = XtDisplay(XtNameToWidget(toplevel, "topBox.statusWindow"));
  83. /*
  84. ** If editorCommand resource exists, use it (format validated during initialize)
  85. */
  86.  if (XMail.editorCommand)
  87.     sprintf(cmd, XMail.editorCommand, ad->display_name, tmpName);
  88.  else {
  89.     /*
  90.     ** Otherwise, default to the VISUAL method of starting things
  91.     */
  92.     if ((edit = GetMailEnv("VISUAL")) == NULL)
  93.          edit = XtNewString(DEFAULT_VISUAL);
  94.  
  95.     if ((cp = strrchr(edit, '/')) == NULL) cp = edit;
  96.     else cp++;
  97.  
  98.     if (strcmp(cp, "ed") == 0 ||
  99.         strcmp(cp,"red") == 0 ||
  100.         strcmp(cp, "ex") == 0 ||
  101.         strcmp(cp, "vi") == 0) {
  102.        sprintf(cmd,
  103.    "/usr/bin/X11/xterm -display %s -name XMail -title 'Message entry' -e %s %s",
  104.                      ad->display_name, edit, tmpName);
  105.       } else sprintf(cmd, "%s -display %s %s", edit, ad->display_name, tmpName);
  106.     XtFree(edit);
  107.    }
  108.  
  109.  editMail_pid = fork();
  110.  
  111.  switch (editMail_pid) {
  112.     case -1:            /* fork failed ... revert to a system call */
  113.          system(cmd);
  114.          break;
  115.     case 0:            /* child starts the message entry session */
  116.          for (i = 0, cp = cmd; i < 49 && *cp; i++) {
  117.              Argv[i] = cp++;        /* pull out each separate argument */
  118.              if (*Argv[i] == '"') {    /* if this is a "quoted string"... */
  119.                 Argv[i] = cp++;
  120.                 while (*cp && *cp != '"') cp++;
  121.                 *cp++ = '\0';
  122.                } else
  123.              if (*Argv[i] == "'"[0]) {    /* or if it's a 'quoted string'... */
  124.                 Argv[i] = cp++;
  125.                 while (*cp && *cp != "'"[0]) cp++;
  126.                 *cp++ = '\0';
  127.                }
  128.              else while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n') cp++;
  129.              if (*cp)            /* truncate each argument as needed */
  130.                 *cp++ = '\0';
  131.             }
  132.          Argv[i] = NULL;
  133.  
  134.          SetCursor(0);            /* restore cursor just before editing */
  135.  
  136.          execvp(Argv[0], Argv);
  137.          perror("editMail: Failed to start the text editor");
  138.          _exit();
  139.          break;
  140.     default:            /* wait for child to finish before continuing */
  141.          while (wait3(&status, WNOHANG, NULL) != editMail_pid)
  142.                if (XPending(XtDisplay(toplevel)) > 0) {
  143.                   XNextEvent(XtDisplay(toplevel), &event);
  144.                   XtDispatchEvent(&event);
  145.                  }
  146.          break;
  147.    }
  148. } /* editMail */
  149.  
  150.  
  151. /*
  152. ** @(#)readMail() - callback invoked every time input is pending on mail fd
  153. **
  154. ** Calls QueryMail() to read all available data from mail file descriptor,
  155. ** and passes output to parse() for analysis and appropriate action.
  156. */
  157. XtInputCallbackProc
  158. readMail(client_data, source, id)
  159. caddr_t   client_data;
  160. int       *source;
  161. XtInputId *id;
  162. {
  163.  parse(QueryMail(""));
  164. } /* readMail */
  165.  
  166.  
  167. /*
  168. ** @(#)sendMail() - send a mail message to the indicated recipient(s)
  169. */
  170. /* ARGSUSED */
  171. void
  172. sendMail(parent)
  173. Widget    parent;
  174. {
  175.  Arg        args[11];
  176.  Widget        Popup, Layout, Box;
  177.  Widget        lab1, lab2, lab3, lab4;
  178.  Widget        To, Subject, Cclist, Bcc, Last;
  179.  
  180.  
  181.  editMail();
  182.  
  183.  Popup = XtNameToWidget(toplevel, "topBox.commandPanel.Send.popup");
  184.  
  185.  if (! Popup) {
  186.     XtSetArg(args[0], XtNinput, True);
  187.     XtSetArg(args[1], XtNwidth, XMail.shellWidth - 2);
  188.     XtSetArg(args[2], XtNheight,
  189.            XMail.borderWidth*3 + XMail.buttonHeight*5 + 44);
  190.     Popup = XtCreatePopupShell("popup",transientShellWidgetClass,parent,args,3);
  191.  
  192.     XtSetArg(args[0], XtNdefaultDistance, 2);
  193.     Layout = XtCreateManagedWidget("SubjCc", formWidgetClass, Popup, args, 1);
  194.  
  195.     XtSetArg(args[0], XtNfromVert, NULL);
  196.     XtSetArg(args[1], XtNfromHoriz, NULL);
  197.     XtSetArg(args[2], XtNlabel, "To:");
  198.     XtSetArg(args[3], XtNborderWidth, 0);
  199.     XtSetArg(args[4], XtNfont, XMail.buttonFont);
  200.     XtSetArg(args[5], XtNheight, XMail.buttonHeight + XMail.borderWidth + 7);
  201.     XtSetArg(args[6], XtNwidth, XMail.buttonWidth);
  202.     XtSetArg(args[7], XtNjustify, XtJustifyLeft);
  203.     XtSetArg(args[8], XtNinternalHeight, 0);
  204.     XtSetArg(args[9], XtNinternalWidth, 1);
  205.     lab1 = XtCreateManagedWidget("SubjCc", labelWidgetClass, Layout, args, 10);
  206.  
  207.     To = CreateInputWindow(Layout, "To", Recipient, BUFSIZ);
  208.  
  209.     AddInfoHandler(To, SendMail_Info[0]);
  210.  
  211.     XtSetArg(args[0], XtNfromVert, NULL);
  212.     XtSetArg(args[1], XtNfromHoriz, lab1);
  213.     XtSetValues(To, args, 2);
  214.  
  215.     AddHelpText(To, To_Help);
  216.  
  217.     XtSetArg(args[0], XtNfromVert, lab1);
  218.     XtSetArg(args[1], XtNfromHoriz, NULL);
  219.     XtSetArg(args[2], XtNlabel, "Subject:");
  220.     lab2 = XtCreateManagedWidget("SubjCc", labelWidgetClass, Layout, args, 10);
  221.  
  222.     Subject = CreateInputWindow(Layout, "Subject", SubjBuf, BUFSIZ);
  223.  
  224.     AddInfoHandler(Subject, SendMail_Info[1]);
  225.  
  226.     XtSetArg(args[0], XtNfromVert, To);
  227.     XtSetArg(args[1], XtNfromHoriz, lab2);
  228.     XtSetValues(Subject, args, 2);
  229.  
  230.     AddHelpText(Subject, Subject_Help);
  231.  
  232.     XtSetArg(args[0], XtNfromVert, lab2);
  233.     XtSetArg(args[1], XtNfromHoriz, NULL);
  234.     XtSetArg(args[2], XtNlabel, "Cc:");
  235.     lab3 = XtCreateManagedWidget("SubjCc", labelWidgetClass, Layout, args, 10);
  236.  
  237.     Cclist = CreateInputWindow(Layout, "Cc", CcBuf, BUFSIZ);
  238.  
  239.     AddInfoHandler(Cclist, SendMail_Info[2]);
  240.  
  241.     XtSetArg(args[0], XtNfromVert, Subject);
  242.     XtSetArg(args[1], XtNfromHoriz, lab3);
  243.     XtSetValues(Cclist, args, 2);
  244.  
  245.     AddHelpText(Cclist, Cc_Help);
  246.  
  247.     XtSetArg(args[0], XtNfromVert, lab3);
  248.     XtSetArg(args[1], XtNfromHoriz, NULL);
  249.     XtSetArg(args[2], XtNlabel, "Bcc:");
  250.     lab4 = XtCreateManagedWidget("SubjCc", labelWidgetClass, Layout, args, 10);
  251.  
  252.     Bcc = CreateInputWindow(Layout, "Bcc", BccBuf, BUFSIZ);
  253.  
  254.     AddInfoHandler(Bcc, SendMail_Info[3]);
  255.  
  256.     XtSetArg(args[0], XtNfromVert, Cclist);
  257.     XtSetArg(args[1], XtNfromHoriz, lab4);
  258.     XtSetValues(Bcc, args, 2);
  259.  
  260.     AddHelpText(Bcc, Bcc_Help);
  261.  
  262.     XtSetArg(args[0], XtNfont, XMail.buttonFont);
  263.     XtSetArg(args[1], XtNheight, XMail.buttonHeight + XMail.borderWidth*2 + 4);
  264.     XtSetArg(args[2], XtNwidth, XMail.shellWidth - 2);
  265.     XtSetArg(args[3], XtNfromVert, lab4);
  266.     XtSetArg(args[4], XtNfromHoriz, NULL);
  267.     XtSetArg(args[5], XtNborderWidth, 0);
  268.     XtSetArg(args[6], XtNresize, False);
  269.     XtSetArg(args[7], XtNmin, args[1].value);
  270.     XtSetArg(args[8], XtNmax, args[1].value);
  271.     XtSetArg(args[9], XtNhSpace, 2);
  272.     XtSetArg(args[10],XtNvSpace, 2);
  273.     Box = XtCreateManagedWidget("Box", boxWidgetClass, Layout, args, 11);
  274.  
  275.     XtSetArg(args[1], XtNheight, XMail.buttonHeight);
  276.     XtSetArg(args[2], XtNwidth,
  277.     (((XMail.shellWidth - 2) / 6) - XMail.borderWidth * 2) - 4);
  278.  
  279.     lab1 = XtCreateManagedWidget("Autograph", commandWidgetClass, Box, args, 3);
  280.     XtAddCallback(lab1, XtNcallback, (XtCallbackProc) Autograph, (caddr_t) "A");
  281.     AddInfoHandler(lab1, Autograph_Info[0]);
  282.     AddHelpText(lab1, Sign_Help);
  283.  
  284.     lab2 = XtCreateManagedWidget("autograph", commandWidgetClass, Box, args, 3);
  285.     XtAddCallback(lab2, XtNcallback, (XtCallbackProc) Autograph, (caddr_t) "a");
  286.     AddInfoHandler(lab2, Autograph_Info[1]);
  287.     AddHelpText(lab2, sign_Help);
  288.  
  289.     lab1 = XtCreateManagedWidget("ReEdit", commandWidgetClass, Box, args, 3);
  290.     XtAddCallback(lab1,XtNcallback,(XtCallbackProc) ReEdit, (caddr_t)"ReEdit");
  291.     AddInfoHandler(lab1, Deliver_Info[1]);
  292.     AddHelpText(lab1, ReEdit_Help);
  293.  
  294.     lab2 = XtCreateManagedWidget("Cancel", commandWidgetClass, Box, args, 3);
  295.     XtAddCallback(lab2, XtNcallback, (XtCallbackProc) Done, (caddr_t) "cancel");
  296.     AddInfoHandler(lab2, Deliver_Info[2]);
  297.     AddHelpText(lab2, Cancel_Help);
  298.  
  299.     lab3 = XtCreateManagedWidget("Abort", commandWidgetClass, Box, args, 3);
  300.     XtAddCallback(lab3, XtNcallback, (XtCallbackProc) Done, (caddr_t) "Cancel");
  301.     AddInfoHandler(lab3, Deliver_Info[3]);
  302.     AddHelpText(lab3, Abort_Help);
  303.  
  304.     Last = XtCreateManagedWidget("Deliver", commandWidgetClass, Box, args, 3);
  305.     XtAddCallback(Last, XtNcallback, (XtCallbackProc) Done, (caddr_t)"Deliver");
  306.     AddInfoHandler(Last, Deliver_Info[0]);
  307.     AddHelpText(Last, Deliver_Help);
  308.    }
  309.  
  310.  To = XtNameToWidget(Popup, "*To");
  311.  writeText(To, Recipient, 0);
  312.  XawTextSetInsertionPoint(To, TextGetLastPos(To));
  313.  
  314.  Subject = XtNameToWidget(Popup, "*Subject");
  315.  writeText(Subject, SubjBuf, 0);
  316.  XawTextSetInsertionPoint(Subject, TextGetLastPos(Subject));
  317.  
  318.  Cclist = XtNameToWidget(Popup, "*Cc");
  319.  writeText(Cclist, CcBuf, 0);
  320.  XawTextSetInsertionPoint(Cclist, TextGetLastPos(Cclist));
  321.  
  322.  Bcc = XtNameToWidget(Popup, "*Bcc");
  323.  writeText(Bcc, BccBuf, 0);
  324.  XawTextSetInsertionPoint(Bcc, TextGetLastPos(Bcc));
  325.  
  326.  SetXY(Popup, XtNameToWidget(toplevel, "topBox.commandPanel"), 0, 0);
  327.  
  328.  XtPopup(Popup, XtGrabNone);
  329.  
  330.  XWarpPointer(XtDisplay(toplevel), None, XtWindow(To), 0, 0, 0, 0, 10, 5);
  331. } /* sendMail */
  332.  
  333.  
  334. /*
  335. ** @(#)writeMail() - Write s to mail, and flush the output.
  336. */
  337. void
  338. writeMail(s) 
  339. char *s;
  340. {
  341.  write(mail_fd, s, strlen(s));
  342. } /* writeMail */
  343.