home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume10 / xt-examples / part01 / xbiff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-04  |  5.7 KB  |  215 lines

  1. /***********************************************************
  2. Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
  3.  
  4.                         All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute these examples for any
  7. purpose and without fee is hereby granted, provided that the above
  8. copyright notice appear in all copies and that both that copyright
  9. notice and this permission notice appear in supporting documentation,
  10. and that the name of Digital not be used in advertising or publicity
  11. pertaining to distribution of the software without specific, written
  12. prior permission.
  13.  
  14. DIGITAL AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  15. SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
  17. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  18. OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  19. OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  20. OR PERFORMANCE OF THIS SOFTWARE.
  21.  
  22. ******************************************************************/
  23.  
  24. #include <X11/Intrinsic.h>
  25. #include <X11/StringDefs.h>
  26. #include <X11/Shell.h>
  27. #include "Confirm.h"
  28.  
  29. #include <stdio.h>
  30. #include <strings.h>
  31. #include <unistd.h>
  32. #include <pwd.h>
  33. #include <sys/stat.h>
  34.  
  35. XtAppContext app;
  36. Widget toplevel, confirm = NULL;
  37. unsigned long fileSize;
  38.  
  39. void GetMailFile(), Timer(), Popup(), CreatePopup(), Callback();
  40. Boolean CheckNewMail();
  41. String FindWhoFrom();
  42.  
  43. typedef struct {
  44.     Boolean quiet;
  45.     String mail_file;
  46.     int timeout;
  47.     String default_message;
  48.     String from_message;
  49. } OptionsRec;
  50.  
  51. OptionsRec options;
  52.  
  53. #define BUFSIZE 1000
  54.  
  55. #define Offset(field) XtOffsetOf(OptionsRec, field)
  56.  
  57. XtResource resources[] = {
  58.     {"quiet", "Quiet", XtRBoolean, sizeof(Boolean),
  59.     Offset(quiet), XtRImmediate, (XtPointer) FALSE},
  60.     {"mailFile", "MailFile", XtRString, sizeof(String),
  61.     Offset(mail_file), XtRString, (XtPointer) NULL},
  62.     {"interval", "Interval", XtRInt, sizeof(int),
  63.     Offset(timeout), XtRImmediate, (XtPointer) 60},
  64.     {"defaultMessage", "DefaultMessage", XtRString, sizeof(String),
  65.     Offset(default_message), XtRString, "New mail from someone"},
  66.     {"fromMessage", "FromMessage", XtRString, sizeof(String),
  67.     Offset(from_message), XtRString, "New mail from %s"}
  68. };
  69.  
  70. #undef Offset
  71.  
  72. XrmOptionDescRec optionDesc[] = {
  73.     {"-quiet", "*quiet", XrmoptionNoArg, (XtPointer) "on"},
  74.     {"-file", "*mailFile", XrmoptionSepArg, (XtPointer) NULL},
  75.     {"-interval", "*interval", XrmoptionSepArg, (XtPointer) NULL}
  76. };
  77.  
  78. main(argc, argv)
  79.     int argc;
  80.     char *argv[];
  81. {
  82.     toplevel = XtAppInitialize(&app, "DemoBiff",
  83.         optionDesc, XtNumber(optionDesc), &argc, argv,
  84.             (String *) NULL, (ArgList) NULL, 0);
  85.  
  86.     XtGetApplicationResources(toplevel, (XtPointer) &options,
  87.         resources, XtNumber(resources), (Arg *) NULL, 0);
  88.  
  89.     GetMailFile();
  90.     (void) XtAppAddTimeOut(app, options.timeout * 1000,
  91.         Timer, (XtPointer) NULL);
  92.     XtAppMainLoop(app);
  93. }
  94.  
  95. void GetMailFile()
  96. {
  97.     extern int getuid();
  98.     struct passwd *pw;
  99.     extern struct passwd *getpwuid();
  100.     struct stat status;
  101.     char buffer[BUFSIZE];
  102.  
  103.     if (options.mail_file == NULL) {
  104.     pw = getpwuid(getuid());
  105.     if (pw == NULL) {
  106.         XtAppErrorMsg(app, "noUser", "getMailFile",
  107.             "DemoBiffError", "Could not get user name",
  108.             (String *) NULL, (Cardinal *) NULL);
  109.     }
  110.     
  111.     (void) sprintf(buffer, "/usr/spool/mail/%s", pw->pw_name);
  112.     options.mail_file = XtNewString(buffer);
  113.     }
  114.  
  115.     if (access(options.mail_file, R_OK) != 0 ||
  116.         stat(options.mail_file, &status) != 0) {
  117.     XtAppErrorMsg(app, "noAccess", "getMailFile", "DemoBiffError",
  118.         "Could not read mail spool file",
  119.         (String *) NULL, (Cardinal *) NULL);
  120.     }
  121.  
  122.     fileSize = status.st_size;
  123. }
  124.  
  125. void Timer(client_data, id)
  126.     XtPointer client_data;
  127.     XtIntervalId *id;
  128. {
  129.     if (!CheckNewMail()) {
  130.     (void) XtAppAddTimeOut(app, options.timeout * 1000,
  131.         Timer, (XtPointer) NULL);
  132.     }
  133. }
  134.  
  135. Boolean CheckNewMail()
  136. {
  137.     struct stat status;
  138.  
  139.     if (access(options.mail_file, R_OK) != 0 ||
  140.         stat(options.mail_file, &status) != 0) {
  141.     XtAppErrorMsg(app, "noAccess", "checkNewMail", "DemoBiffError",
  142.         "Could not read mail spool file",
  143.         (String *) NULL, (Cardinal *) NULL);
  144.     }
  145.  
  146.     if (status.st_size <= fileSize) {
  147.     fileSize = status.st_size;
  148.     return FALSE;
  149.     }
  150.  
  151.     Popup(FindWhoFrom());
  152.     if (!options.quiet) XBell(XtDisplay(toplevel), 100);
  153.     fileSize = status.st_size;
  154.     return TRUE;
  155. }
  156.  
  157. String FindWhoFrom()
  158. {
  159.     char buffer[BUFSIZE];
  160.     static char message[BUFSIZE];
  161.     FILE *mail = fopen(options.mail_file, "r");
  162.     int fromLen = strlen("From: ");
  163.  
  164.     if (mail == NULL) {
  165.     return options.default_message;
  166.     }
  167.  
  168.     if (fseek(mail, fileSize, 0) == -1) {
  169.     fclose(mail);
  170.     return options.default_message;
  171.     }
  172.  
  173.     while (1) {
  174.     if (fgets(buffer, BUFSIZE, mail) == NULL ||
  175.         buffer[0] == '\n') {
  176.         fclose(mail);
  177.         return options.default_message;
  178.     }
  179.     if (strncmp(buffer, "From: ", fromLen) == 0) {
  180.         buffer[strlen(buffer)-1] = '\0';    /* Replace \n */
  181.         (void) sprintf(message, options.from_message,
  182.             &buffer[fromLen]);
  183.         fclose(mail);
  184.         return message;
  185.     }
  186.     }
  187. }    
  188.  
  189. void Popup(string)
  190.     String string;
  191. {
  192.     Arg args[10];
  193.     int i = 0;
  194.  
  195.     if (confirm == NULL) CreatePopup();
  196.  
  197.     XtSetArg(args[i], XtNlabel, string);    i++;
  198.     XtSetValues(confirm, args, i);
  199.     XtPopup(toplevel, XtGrabNone);
  200. }
  201.  
  202. void CreatePopup()
  203. {
  204.     confirm = XtCreateManagedWidget("confirm", confirmWidgetClass,
  205.         toplevel, (Arg *) NULL, 0);
  206.     XtAddCallback(confirm, XtNcallback, Callback, (XtPointer) NULL);
  207. }
  208.  
  209. void Callback(w, client_data, call_data)
  210.     Widget w;
  211.     XtPointer client_data, call_data;
  212. {
  213.     (void) XtAppAddTimeOut(app, 0, Timer, (XtPointer) NULL);
  214. }
  215.