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
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$";
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 > optind) {
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 if (retval != ETIMEDOUT)
140 com_err(argv[0],retval,"while receiving notice");
144 if (strcmp(notice.z_sender,ZGetSender()) == 0 ||
145 strcmp(notice.z_opcode,"PING") == 0 ||
146 strcmp(notice.z_opcode,"AUTO") == 0 ||
147 strcmp(notice.z_message,"Automated reply:") == 0) {
148 ZFreeNotice(¬ice);
152 if (watch_location) {
153 if ((retval = ZLocateUser(ZGetSender(), &nlocs, ZNOAUTH))
155 com_err(argv[0],retval,"while locating self");
160 /* User is logged in. Don't send an autoreply. */
168 ptr = malloc(strlen(cmdline_msg)+1);
170 com_err(argv[0],ENOMEM,"while getting cmdline message");
173 (void) strcpy(ptr,cmdline_msg);
176 if (!(ptr = find_message(¬ice,fp))) {
177 ZFreeNotice(¬ice);
182 ptr = malloc(sizeof(DEFAULT_MSG)+1);
184 com_err(argv[0],ENOMEM,"while getting default message");
187 (void) strcpy(ptr,DEFAULT_MSG);
189 notice.z_recipient = notice.z_sender;
191 notice.z_default_format = "";
192 notice.z_opcode = RESPONSE_OPCODE;
194 msg[0] = "Automated reply:";
197 notice.z_message_len = strlen(notice.z_message)+1;
198 if ((retval = ZSendList(¬ice,msg,2,ZNOAUTH)) != ZERR_NONE) {
199 com_err(argv[0],retval,"while sending notice");
202 ZFreeNotice(¬ice);
206 char *find_message(notice,fp)
210 register char *ptr,*ptr2;
211 char bfr[BUFSIZ],sender[BUFSIZ];
212 int gotone,lastwasnt;
216 (void) strcpy(sender,notice->z_sender);
217 ptr2 = strchr(sender,'@');
225 while (fgets(bfr,sizeof bfr,fp) != (char *)0) {
229 bfr[strlen(bfr)-1] = '\0';
230 ptr2 = strchr(bfr,'@');
233 if (!strcmp(bfr+1,sender) ||
234 !strcmp(bfr+1,"*") ||
235 (!strcmp(bfr+1,"%") && !ptr))
242 ptr = malloc((unsigned)(strlen(bfr)+1));
246 ptr = realloc(ptr,(unsigned)(strlen(bfr)+strlen(ptr)+1));
247 (void) strcat(ptr,bfr);
258 ZCancelSubscriptions(port);