1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains code for the "zleave" command.
4 * Created by: David Jedlinsky
6 * $Id: zleave.c,v 1.25 1999/08/13 00:19:39 danw Exp $
8 * Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
9 * For copying and distribution information, see the file
14 #include <zephyr/mit-copyright.h>
15 #include <zephyr/zephyr.h>
20 static char rcsid_zlocate_c[] = "$Id: zleave.c,v 1.25 1999/08/13 00:19:39 danw Exp $";
24 * Copyright (c) 1980 Regents of the University of California.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms are permitted
28 * provided that this notice is preserved and that due credit is given
29 * to the University of California at Berkeley. The name of the University
30 * may not be used to endorse or promote products derived from this
31 * software without specific written prior permission. This software
32 * is provided ``as is'' without express or implied warranty.
37 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
38 All rights reserved.\n";
41 #define MESSAGE_CLASS "MESSAGE"
42 #define INSTANCE "LEAVE"
44 * zleave [[+]hhmm [ -m "Reminder Message" ]]
48 * Reminds you when you have to leave.
49 * Leave prompts for input and goes away if you hit return.
50 * Messages are sent through Zephyr. Subscriptions are handled automagically.
51 * It nags you like a mother hen.
57 char *reminder_message = NULL;
59 int use_zephyr=1, oldpid;
61 void usage(), doalarm(), bother(), delay();
67 long when, diff, hours, minutes;
75 if (ZInitialize() != ZERR_NONE) {
76 fprintf(stderr,"No Zephyr! Will write directly to terminal.\n");
79 (void) sprintf(tempfile, "/tmp/zleave.%d", (int) getuid());
82 if ((port = ZGetWGPort()) == -1) {
84 "Can't find WindowGram subscription port.\n");
85 fprintf(stderr,"Will write directly to terminal.\n");
88 sub.zsub_class = MESSAGE_CLASS;
89 sub.zsub_classinst = INSTANCE;
90 sub.zsub_recipient = ZGetSender();
91 if (ZSubscribeToSansDefaults(&sub,1,(u_short)port)
94 "Subscription error! Writing to your terminal...\n");
100 if ((cp = getlogin()) == NULL) {
101 fputs("leave: You are not logged in.\n", stderr);
104 (void) strcpy(origlogin, cp);
108 while ((c<argc) && (! reminder_message))
109 if (!strcmp(argv[c++],"-m")) {
111 reminder_message = argv[c];
116 if (!reminder_message)
117 reminder_message = "";
120 printf("When do you have to leave? ");
121 (void) fflush(stdout);
122 buff[read(0, buff, sizeof buff)] = 0;
130 if (!gethm(cp, &hours, &minutes))
132 if (minutes < 0 || minutes > 59)
134 diff = 60*hours+minutes;
138 if (!strcmp(cp, "cancel") || !strcmp(cp, "can")) {
139 if (!(fp = fopen(tempfile,"r"))) {
140 printf("No zleave is currently running.\n");
143 if (fscanf(fp, "%d", &oldpid) != 1) {
144 printf("The zleave pid file is corrupted.\n");
150 printf("No zleave is currently running.\n");
151 (void) unlink(tempfile);
154 if (!gethm(cp, &hours, &minutes))
161 if (hours < 0 || hours > 12 || minutes < 0 || minutes > 59)
165 nv = localtime(&now);
166 when = 60*hours+minutes;
167 if (nv->tm_hour > 12)
168 nv->tm_hour -= 12; /* do am/pm bit */
169 now = 60 * nv->tm_hour + nv->tm_min;
174 fprintf(stderr, "That time has already passed!\n");
185 fprintf(stderr, "usage: zleave [[+]hhmm [-m \"Reminder Message\"]]\n\
186 \tor: zleave can[cel]\n");
199 while ((c = *cp++) != '\0') {
202 tod = tod * 10 + (c - '0');
214 char *msg1, *msg2, *msg3, *msg4;
216 long slp1, slp2, slp3, slp4;
217 long seconds, gseconds;
219 #ifdef _POSIX_VERSION
223 seconds = 60 * nmins;
228 msg1 = "You have to leave in 5 minutes";
229 if (seconds <= 60*5) {
232 slp1 = seconds - 60*5;
236 msg2 = "Just one more minute!";
244 msg3 = "Time to leave!";
247 msg4 = "You're going to be late!";
250 (void) time(&daytime);
252 whenleave = ctime(&daytime);
254 if (fp = fopen(tempfile,"r")) {
255 if (fscanf(fp, "%d", &oldpid) == 1)
257 printf("Old zleave process killed.\n");
260 printf("Alarm set for %s", whenleave);
262 /* Subscribe to MESSAGE.LEAVE here */
275 if (!(fp = fopen(tempfile, "w")))
276 fprintf(stderr, "Cannot open pid file.\n");
278 fprintf(fp, "%d\n", getpid());
279 if (fclose(fp) == EOF)
280 (void) perror("fclose on pid file");
283 #ifdef _POSIX_VERSION
284 sigemptyset(&sa.sa_mask);
286 sa.sa_handler = SIG_IGN;
287 sigaction(SIGINT, &sa, (struct sigaction *)0);
288 sigaction(SIGQUIT, &sa, (struct sigaction *)0);
289 sigaction(SIGTERM, &sa, (struct sigaction *)0);
290 sigaction(SIGTTOU, &sa, (struct sigaction *)0);
292 (void) signal(SIGINT, SIG_IGN);
293 (void) signal(SIGQUIT, SIG_IGN);
294 (void) signal(SIGTERM, SIG_IGN);
295 (void) signal(SIGTTOU, SIG_IGN);
303 for (i = 0; i < 10; i++)
306 bother(0L, "That was the last time I'll tell you. Bye.");
307 (void) unlink(tempfile);
324 real_message = (char *) malloc(strlen(msg) +
325 strlen(reminder_message) + 3);
326 if (real_message == NULL) {
327 fprintf (stderr, "zleave: out of memory\n");
330 sprintf(real_message,"%c%s\n%s",'\0',msg,reminder_message);
332 (void) memset((char *)¬ice, 0, sizeof(notice));
333 notice.z_kind = ACKED;
335 notice.z_class = MESSAGE_CLASS;
336 notice.z_class_inst = INSTANCE;
337 notice.z_recipient = ZGetSender();
338 notice.z_opcode = "";
339 notice.z_sender = (char *) 0;
340 notice.z_default_format = "\n$2";
341 notice.z_message = real_message;
342 /* +3: initial null, newline, final null */
343 notice.z_message_len = strlen(msg)+strlen(reminder_message)+3;
345 if (ZSendNotice(¬ice, ZAUTH) != ZERR_NONE) {
346 printf("\7\7\7%s\n%s", msg, reminder_message);
349 if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *) 0,
351 (char *)¬ice.z_uid)) != ZERR_NONE) {
353 "zleave: %s while waiting for acknowledgement\n",
354 error_message(retval));
357 if (retnotice.z_kind == SERVNAK) {
359 "zleave: authorization failure while sending\n");
362 if (retnotice.z_kind != SERVACK || !retnotice.z_message_len) {
363 fprintf(stderr, "zleave: Detected server failure while receiving acknowledgement\n");
366 if (strcmp(retnotice.z_message, ZSRVACK_SENT)) {
372 ZFreeNotice(&retnotice);
376 printf("\a\a\a%s\n%s", msg, reminder_message);
378 printf("\7\7\7%s\n%s", msg, reminder_message);
383 * delay is like sleep but does it in 100 sec pieces and
384 * knows what zero means.
404 if (strcmp(origlogin, l) != 0)
410 #ifndef HAVE_GETLOGIN
414 static struct utmp ubuf;
417 ufd = open("/etc/utmp",0);
418 seek(ufd, ttyn(0)*sizeof(ubuf), 0);
419 read(ufd, &ubuf, sizeof(ubuf));
420 ubuf.ut_name[sizeof(ubuf.ut_name)] = 0;
421 return(&ubuf.ut_name);