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
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 const char rcsid_zlocate_c[] = "$Id$";
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.
56 char *reminder_message = NULL;
58 int use_zephyr=1, oldpid;
62 void bother(long, char *);
64 int gethm(char *, int *, int*);
79 if (ZInitialize() != ZERR_NONE) {
80 fprintf(stderr,"No Zephyr! Will write directly to terminal.\n");
83 (void) sprintf(tempfile, "/tmp/zleave.%d", (int) getuid());
86 if ((port = ZGetWGPort()) == -1) {
88 "Can't find WindowGram subscription port.\n");
89 fprintf(stderr,"Will write directly to terminal.\n");
92 sub.zsub_class = MESSAGE_CLASS;
93 sub.zsub_classinst = INSTANCE;
94 sub.zsub_recipient = ZGetSender();
95 if (ZSubscribeToSansDefaults(&sub,1,(u_short)port)
98 "Subscription error! Writing to your terminal...\n");
104 if ((cp = getlogin()) == NULL) {
105 fputs("leave: You are not logged in.\n", stderr);
108 (void) strcpy(origlogin, cp);
112 while ((c<argc) && (! reminder_message))
113 if (!strcmp(argv[c++],"-m")) {
115 reminder_message = argv[c];
120 if (!reminder_message)
121 reminder_message = "";
124 printf("When do you have to leave? ");
125 (void) fflush(stdout);
126 buff[read(0, buff, sizeof buff)] = 0;
134 if (!gethm(cp, &hours, &minutes))
136 if (minutes < 0 || minutes > 59)
138 diff = 60*hours+minutes;
142 if (!strcmp(cp, "cancel") || !strcmp(cp, "can")) {
143 if (!(fp = fopen(tempfile,"r"))) {
144 printf("No zleave is currently running.\n");
147 if (fscanf(fp, "%d", &oldpid) != 1) {
148 printf("The zleave pid file is corrupted.\n");
154 printf("No zleave is currently running.\n");
155 (void) unlink(tempfile);
158 if (!gethm(cp, &hours, &minutes))
165 if (hours < 0 || hours > 12 || minutes < 0 || minutes > 59)
169 nv = localtime(&now);
170 when = 60*hours+minutes;
171 if (nv->tm_hour > 12)
172 nv->tm_hour -= 12; /* do am/pm bit */
173 now = 60 * nv->tm_hour + nv->tm_min;
178 fprintf(stderr, "That time has already passed!\n");
189 fprintf(stderr, "usage: zleave [[+]hhmm [-m \"Reminder Message\"]]\n\
190 \tor: zleave can[cel]\n");
203 while ((c = *cp++) != '\0') {
206 tod = tod * 10 + (c - '0');
217 char *msg1, *msg2, *msg3, *msg4;
219 long slp1, slp2, slp3, slp4;
220 long seconds, gseconds;
222 #ifdef _POSIX_VERSION
226 seconds = 60 * nmins;
231 msg1 = "You have to leave in 5 minutes";
232 if (seconds <= 60*5) {
235 slp1 = seconds - 60*5;
239 msg2 = "Just one more minute!";
247 msg3 = "Time to leave!";
250 msg4 = "You're going to be late!";
253 (void) time(&daytime);
255 whenleave = ctime(&daytime);
257 fp = fopen(tempfile,"r");
259 if (fscanf(fp, "%d", &oldpid) == 1)
261 printf("Old zleave process killed.\n");
264 printf("Alarm set for %s", whenleave);
266 /* Subscribe to MESSAGE.LEAVE here */
279 if (!(fp = fopen(tempfile, "w")))
280 fprintf(stderr, "Cannot open pid file.\n");
282 fprintf(fp, "%d\n", getpid());
283 if (fclose(fp) == EOF)
284 (void) perror("fclose on pid file");
287 #ifdef _POSIX_VERSION
288 sigemptyset(&sa.sa_mask);
290 sa.sa_handler = SIG_IGN;
291 sigaction(SIGINT, &sa, (struct sigaction *)0);
292 sigaction(SIGQUIT, &sa, (struct sigaction *)0);
293 sigaction(SIGTERM, &sa, (struct sigaction *)0);
294 sigaction(SIGTTOU, &sa, (struct sigaction *)0);
296 (void) signal(SIGINT, SIG_IGN);
297 (void) signal(SIGQUIT, SIG_IGN);
298 (void) signal(SIGTERM, SIG_IGN);
299 (void) signal(SIGTTOU, SIG_IGN);
307 for (i = 0; i < 10; i++)
310 bother(0L, "That was the last time I'll tell you. Bye.");
311 (void) unlink(tempfile);
327 real_message = (char *) malloc(strlen(msg) +
328 strlen(reminder_message) + 3);
329 if (real_message == NULL) {
330 fprintf (stderr, "zleave: out of memory\n");
333 sprintf(real_message,"%c%s\n%s",'\0',msg,reminder_message);
335 (void) memset((char *)¬ice, 0, sizeof(notice));
336 notice.z_kind = ACKED;
338 notice.z_class = MESSAGE_CLASS;
339 notice.z_class_inst = INSTANCE;
340 notice.z_recipient = ZGetSender();
341 notice.z_opcode = "";
342 notice.z_sender = (char *) 0;
343 notice.z_default_format = "\n$2";
344 notice.z_message = real_message;
345 /* +3: initial null, newline, final null */
346 notice.z_message_len = strlen(msg)+strlen(reminder_message)+3;
348 if (ZSendNotice(¬ice, ZAUTH) != ZERR_NONE) {
349 printf("\7\7\7%s\n%s", msg, reminder_message);
352 if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *) 0,
354 (char *)¬ice.z_uid)) != ZERR_NONE) {
356 "zleave: %s while waiting for acknowledgement\n",
357 error_message(retval));
360 if (retnotice.z_kind == SERVNAK) {
362 "zleave: authorization failure while sending\n");
365 if (retnotice.z_kind != SERVACK || !retnotice.z_message_len) {
366 fprintf(stderr, "zleave: Detected server failure while receiving acknowledgement\n");
369 if (strcmp(retnotice.z_message, ZSRVACK_SENT)) {
375 ZFreeNotice(&retnotice);
379 printf("\a\a\a%s\n%s", msg, reminder_message);
381 printf("\7\7\7%s\n%s", msg, reminder_message);
386 * delay is like sleep but does it in 100 sec pieces and
387 * knows what zero means.
406 if (strcmp(origlogin, l) != 0)
412 #ifndef HAVE_GETLOGIN
416 static struct utmp ubuf;
419 ufd = open("/etc/utmp",0);
420 seek(ufd, ttyn(0)*sizeof(ubuf), 0);
421 read(ufd, &ubuf, sizeof(ubuf));
422 ubuf.ut_name[sizeof(ubuf.ut_name)] = 0;
423 return(&ubuf.ut_name);