1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains code for the "zaway" command.
4 * Created by: Robert French
6 * $Id: zaway.c,v 1.14 1999/07/21 12:34:22 ghudson Exp $
8 * Copyright (c) 1987, 1993 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_zaway_c[] = "$Id: zaway.c,v 1.14 1999/07/21 12:34:22 ghudson Exp $";
23 #define MESSAGE_CLASS "MESSAGE"
24 #define DEFAULT_MSG "I'm sorry, but I am currently away from the terminal and am\nnot able to receive your message.\n"
25 #define RESPONSE_OPCODE ""
33 printf("Usage: %s [OPTIONS] [FILE]\n"
35 " -m STRING use STRING as the body of the reply message\n"
36 " -w watch your location and only reply if you aren't locatable\n"
37 " -h display this help and exit\n",
51 char awayfile[BUFSIZ],*msg[2],*envptr;
52 int optchar, watch_location;
60 if ((retval = ZInitialize()) != ZERR_NONE) {
61 com_err(argv[0],retval,"while initializing");
66 if ((retval = ZOpenPort(&port)) != ZERR_NONE) {
67 com_err(argv[0],retval,"while opening port");
71 sub.zsub_class = MESSAGE_CLASS;
72 sub.zsub_classinst = "*";
73 sub.zsub_recipient = ZGetSender();
77 while ((optchar = getopt(argc, argv, "m:wh")) != EOF) {
93 "Unrecognized option '-%c'.\n"
94 "Try '%s -h' for more information.\n",
101 (void) strcpy(awayfile,argv[optind]);
103 envptr = getenv("HOME");
105 (void) sprintf(awayfile,"%s/.away",envptr);
107 if (!(pw = getpwuid((int) getuid()))) {
108 fprintf(stderr,"Who are you?\n");
111 (void) sprintf(awayfile,"%s/.away",pw->pw_dir);
115 fp = fopen(awayfile,"r");
116 if (!fp && argc > 1) {
117 fprintf(stderr,"File %s not found!\n",awayfile);
120 #ifdef _POSIX_VERSION
121 (void) sigemptyset(&sa.sa_mask);
123 sa.sa_handler = cleanup;
124 (void) sigaction(SIGINT, &sa, (struct sigaction *)0);
125 (void) sigaction(SIGTERM, &sa, (struct sigaction *)0);
126 (void) sigaction(SIGHUP, &sa, (struct sigaction *)0);
128 (void) signal(SIGINT, cleanup);
129 (void) signal(SIGTERM, cleanup);
130 (void) signal(SIGHUP, cleanup);
132 if ((retval = ZSubscribeToSansDefaults(&sub,1,port)) != ZERR_NONE) {
133 com_err(argv[0],retval,"while subscribing");
138 if ((retval = ZReceiveNotice(¬ice, (struct sockaddr_in *)0)) != ZERR_NONE) {
139 com_err(argv[0],retval,"while receiving notice");
143 if (strcmp(notice.z_sender,ZGetSender()) == 0 ||
144 strcmp(notice.z_opcode,"PING") == 0 ||
145 strcmp(notice.z_message,"Automated reply:") == 0) {
146 ZFreeNotice(¬ice);
150 if (watch_location) {
151 if ((retval = ZLocateUser(ZGetSender(), &nlocs, ZNOAUTH))
153 com_err(argv[0],retval,"while locating self");
158 /* User is logged in. Don't send an autoreply. */
166 ptr = malloc(strlen(cmdline_msg));
168 com_err(argv[0],ENOMEM,"while getting cmdline message");
171 (void) strcpy(ptr,cmdline_msg);
174 if (!(ptr = find_message(¬ice,fp))) {
175 ZFreeNotice(¬ice);
180 ptr = malloc(sizeof(DEFAULT_MSG)+1);
182 com_err(argv[0],ENOMEM,"while getting default message");
185 (void) strcpy(ptr,DEFAULT_MSG);
187 notice.z_recipient = notice.z_sender;
189 notice.z_default_format = "";
190 notice.z_opcode = RESPONSE_OPCODE;
192 msg[0] = "Automated reply:";
195 notice.z_message_len = strlen(notice.z_message)+1;
196 if ((retval = ZSendList(¬ice,msg,2,ZNOAUTH)) != ZERR_NONE) {
197 com_err(argv[0],retval,"while sending notice");
200 ZFreeNotice(¬ice);
204 char *find_message(notice,fp)
208 register char *ptr,*ptr2;
209 char bfr[BUFSIZ],sender[BUFSIZ];
210 int gotone,lastwasnt;
214 (void) strcpy(sender,notice->z_sender);
215 ptr2 = strchr(sender,'@');
223 while (fgets(bfr,sizeof bfr,fp) != (char *)0) {
227 bfr[strlen(bfr)-1] = '\0';
228 ptr2 = strchr(bfr,'@');
231 if (!strcmp(bfr+1,sender) ||
232 !strcmp(bfr+1,"*") ||
233 (!strcmp(bfr+1,"%") && !ptr))
240 ptr = malloc((unsigned)(strlen(bfr)+1));
244 ptr = realloc(ptr,(unsigned)(strlen(bfr)+strlen(ptr)+1));
245 (void) strcat(ptr,bfr);
256 ZCancelSubscriptions(port);