home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************
- Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
-
- All Rights Reserved
-
- Permission to use, copy, modify, and distribute these examples for any
- purpose and without fee is hereby granted, provided that the above
- copyright notice appear in all copies and that both that copyright
- notice and this permission notice appear in supporting documentation,
- and that the name of Digital not be used in advertising or publicity
- pertaining to distribution of the software without specific, written
- prior permission.
-
- DIGITAL AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
- SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
- FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
- OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
- OR PERFORMANCE OF THIS SOFTWARE.
-
- ******************************************************************/
-
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- #include <X11/Shell.h>
- #include "Confirm.h"
-
- #include <stdio.h>
- #include <strings.h>
- #include <unistd.h>
- #include <pwd.h>
- #include <sys/stat.h>
-
- XtAppContext app;
- Widget toplevel, confirm = NULL;
- unsigned long fileSize;
-
- void GetMailFile(), Timer(), Popup(), CreatePopup(), Callback();
- Boolean CheckNewMail();
- String FindWhoFrom();
-
- typedef struct {
- Boolean quiet;
- String mail_file;
- int timeout;
- String default_message;
- String from_message;
- } OptionsRec;
-
- OptionsRec options;
-
- #define BUFSIZE 1000
-
- #define Offset(field) XtOffsetOf(OptionsRec, field)
-
- XtResource resources[] = {
- {"quiet", "Quiet", XtRBoolean, sizeof(Boolean),
- Offset(quiet), XtRImmediate, (XtPointer) FALSE},
- {"mailFile", "MailFile", XtRString, sizeof(String),
- Offset(mail_file), XtRString, (XtPointer) NULL},
- {"interval", "Interval", XtRInt, sizeof(int),
- Offset(timeout), XtRImmediate, (XtPointer) 60},
- {"defaultMessage", "DefaultMessage", XtRString, sizeof(String),
- Offset(default_message), XtRString, "New mail from someone"},
- {"fromMessage", "FromMessage", XtRString, sizeof(String),
- Offset(from_message), XtRString, "New mail from %s"}
- };
-
- #undef Offset
-
- XrmOptionDescRec optionDesc[] = {
- {"-quiet", "*quiet", XrmoptionNoArg, (XtPointer) "on"},
- {"-file", "*mailFile", XrmoptionSepArg, (XtPointer) NULL},
- {"-interval", "*interval", XrmoptionSepArg, (XtPointer) NULL}
- };
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- toplevel = XtAppInitialize(&app, "DemoBiff",
- optionDesc, XtNumber(optionDesc), &argc, argv,
- (String *) NULL, (ArgList) NULL, 0);
-
- XtGetApplicationResources(toplevel, (XtPointer) &options,
- resources, XtNumber(resources), (Arg *) NULL, 0);
-
- GetMailFile();
- (void) XtAppAddTimeOut(app, options.timeout * 1000,
- Timer, (XtPointer) NULL);
- XtAppMainLoop(app);
- }
-
- void GetMailFile()
- {
- extern int getuid();
- struct passwd *pw;
- extern struct passwd *getpwuid();
- struct stat status;
- char buffer[BUFSIZE];
-
- if (options.mail_file == NULL) {
- pw = getpwuid(getuid());
- if (pw == NULL) {
- XtAppErrorMsg(app, "noUser", "getMailFile",
- "DemoBiffError", "Could not get user name",
- (String *) NULL, (Cardinal *) NULL);
- }
-
- (void) sprintf(buffer, "/usr/spool/mail/%s", pw->pw_name);
- options.mail_file = XtNewString(buffer);
- }
-
- if (access(options.mail_file, R_OK) != 0 ||
- stat(options.mail_file, &status) != 0) {
- XtAppErrorMsg(app, "noAccess", "getMailFile", "DemoBiffError",
- "Could not read mail spool file",
- (String *) NULL, (Cardinal *) NULL);
- }
-
- fileSize = status.st_size;
- }
-
- void Timer(client_data, id)
- XtPointer client_data;
- XtIntervalId *id;
- {
- if (!CheckNewMail()) {
- (void) XtAppAddTimeOut(app, options.timeout * 1000,
- Timer, (XtPointer) NULL);
- }
- }
-
- Boolean CheckNewMail()
- {
- struct stat status;
-
- if (access(options.mail_file, R_OK) != 0 ||
- stat(options.mail_file, &status) != 0) {
- XtAppErrorMsg(app, "noAccess", "checkNewMail", "DemoBiffError",
- "Could not read mail spool file",
- (String *) NULL, (Cardinal *) NULL);
- }
-
- if (status.st_size <= fileSize) {
- fileSize = status.st_size;
- return FALSE;
- }
-
- Popup(FindWhoFrom());
- if (!options.quiet) XBell(XtDisplay(toplevel), 100);
- fileSize = status.st_size;
- return TRUE;
- }
-
- String FindWhoFrom()
- {
- char buffer[BUFSIZE];
- static char message[BUFSIZE];
- FILE *mail = fopen(options.mail_file, "r");
- int fromLen = strlen("From: ");
-
- if (mail == NULL) {
- return options.default_message;
- }
-
- if (fseek(mail, fileSize, 0) == -1) {
- fclose(mail);
- return options.default_message;
- }
-
- while (1) {
- if (fgets(buffer, BUFSIZE, mail) == NULL ||
- buffer[0] == '\n') {
- fclose(mail);
- return options.default_message;
- }
- if (strncmp(buffer, "From: ", fromLen) == 0) {
- buffer[strlen(buffer)-1] = '\0'; /* Replace \n */
- (void) sprintf(message, options.from_message,
- &buffer[fromLen]);
- fclose(mail);
- return message;
- }
- }
- }
-
- void Popup(string)
- String string;
- {
- Arg args[10];
- int i = 0;
-
- if (confirm == NULL) CreatePopup();
-
- XtSetArg(args[i], XtNlabel, string); i++;
- XtSetValues(confirm, args, i);
- XtPopup(toplevel, XtGrabNone);
- }
-
- void CreatePopup()
- {
- confirm = XtCreateManagedWidget("confirm", confirmWidgetClass,
- toplevel, (Arg *) NULL, 0);
- XtAddCallback(confirm, XtNcallback, Callback, (XtPointer) NULL);
- }
-
- void Callback(w, client_data, call_data)
- Widget w;
- XtPointer client_data, call_data;
- {
- (void) XtAppAddTimeOut(app, 0, Timer, (XtPointer) NULL);
- }
-