1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains code for the "zwrite" command.
4 * Created by: Robert French
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_zwrite_c[] = "$Id$";
23 #define DEFAULT_CLASS "MESSAGE"
24 #define DEFAULT_INSTANCE "PERSONAL"
25 #define URGENT_INSTANCE "URGENT"
26 #define DEFAULT_OPCODE ""
27 #define FILSRV_CLASS "FILSRV"
31 int nrecips, msgarg, verbose, quiet, nodot, cc;
32 char *whoami, *inst, *class, *opcode, *realm, *recips[MAXRECIPS];
34 void un_tabify(char **, int *);
36 char *fix_filsrv_inst(char *);
38 void send_off(ZNotice_t *, int);
44 int retval, arg, nocheck, nchars, msgsize, filsys, tabexpand;
45 char *message, *signature = NULL, *format = NULL;
46 static char bfr[BUFSIZ], classbfr[BUFSIZ], instbfr[BUFSIZ], sigbfr[BUFSIZ];
47 static char opbfr[BUFSIZ];
48 static ZNotice_t notice;
52 if ((retval = ZInitialize()) != ZERR_NONE) {
53 com_err(whoami, retval, "while initializing");
61 verbose = quiet = msgarg = nrecips = nocheck = filsys = nodot = 0;
64 class = ZGetVariable("zwrite-class");
66 (void) strcpy(classbfr, class);
70 class = DEFAULT_CLASS;
71 inst = ZGetVariable("zwrite-inst");
73 (void) strcpy(instbfr, inst);
77 inst = DEFAULT_INSTANCE;
79 opcode = ZGetVariable("zwrite-opcode");
81 opcode = strcpy(opbfr, opcode);
83 opcode = DEFAULT_OPCODE;
85 signature = ZGetVariable("zwrite-signature");
87 (void) strcpy(sigbfr, signature);
93 for (;arg<argc&&!msgarg;arg++) {
94 if (*argv[arg] != '-') {
95 recips[nrecips++] = argv[arg];
98 if (strlen(argv[arg]) > 2)
100 switch (argv[arg][1]) {
101 case 'a': /* Backwards compatibility */
105 class = DEFAULT_CLASS;
106 inst = DEFAULT_INSTANCE;
107 opcode = DEFAULT_OPCODE;
125 inst = URGENT_INSTANCE;
134 if (arg == argc-1 || filsys == 1)
141 if (arg == argc-1 || filsys == 1)
148 if (arg == argc-1 || filsys == -1)
151 class = FILSRV_CLASS;
152 inst = fix_filsrv_inst(argv[arg]);
154 nocheck = 1; /* implied -n (no ping) */
160 signature = argv[arg];
165 nocheck = 1; /* implied -n (no ping) */
168 case 'l': /* literal */
191 if (!nrecips && !(strcmp(class, DEFAULT_CLASS) ||
192 (strcmp(inst, DEFAULT_INSTANCE) &&
193 strcmp(inst, URGENT_INSTANCE)))) {
194 /* must specify recipient if using default class and
195 (default instance or urgent instance) */
196 fprintf(stderr, "No recipients specified.\n");
201 /* try to find name in the password file */
202 register struct passwd *pwd;
203 register char *cp = sigbfr;
204 register char *cp2, *pp;
206 pwd = getpwuid(getuid());
209 for (; *cp2 && *cp2 != ',' ; cp2++) {
212 *cp++ = islower(*pp) ? toupper(*pp) : *pp;
223 notice.z_kind = ACKED;
225 notice.z_class = class;
226 notice.z_class_inst = inst;
227 notice.z_opcode = "PING";
229 notice.z_message_len = 0;
230 notice.z_recipient = "";
232 notice.z_default_format = format;
233 else if (filsys == 1)
234 notice.z_default_format = "@bold(Filesystem Operation Message for $instance:)\nFrom: @bold($sender) at $time $date\n$message";
235 else if (auth == ZAUTH) {
237 notice.z_default_format = "Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold($1) <$sender>\n\n$2";
239 notice.z_default_format = "Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\n$message";
242 notice.z_default_format = "@bold(UNAUTHENTIC) Class $class, Instance $instance at $time $date:\nFrom: @bold($1) <$sender>\n\n$2";
244 notice.z_default_format = "@bold(UNAUTHENTIC) Class $class, Instance $instance at $time $date:\n$message";
246 if (!nocheck && nrecips)
247 send_off(¬ice, 0);
249 if (!msgarg && isatty(0)) {
251 printf("Type your message now. End with the end-of-file character.\n");
253 printf("Type your message now. End with control-D or a dot on a line by itself.\n");
259 message = malloc((unsigned)(strlen(signature)+2));
260 (void) strcpy(message, signature);
261 msgsize = strlen(message);
262 message[msgsize++] = '\0';
265 message[msgsize++] = '\0';
268 if (cc && nrecips > 1) {
270 for (arg=0;arg<nrecips;arg++)
271 size += (strlen(recips[arg]) + 2);
272 size += 6; /* for the newlines and "cc: " */
273 message = realloc(message, (unsigned) size);
274 (void) strcpy(message+msgsize, "CC: ");
276 for (arg=0;arg<nrecips;arg++) {
277 (void) strcpy(message+msgsize, recips[arg]);
278 msgsize += strlen(recips[arg]);
279 if (arg != nrecips-1) {
280 message[msgsize] = ' ';
284 message[msgsize] = '\n';
290 for (arg=msgarg;arg<argc;arg++)
291 size += (strlen(argv[arg]) + 1);
292 size++; /* for the newline */
293 message = realloc(message, (unsigned) size);
294 for (arg=msgarg;arg<argc;arg++) {
295 (void) strcpy(message+msgsize, argv[arg]);
296 msgsize += strlen(argv[arg]);
298 message[msgsize] = ' ';
302 message[msgsize] = '\n';
308 if (!fgets(bfr, sizeof bfr, stdin))
310 if (!nodot && bfr[0] == '.' &&
311 (bfr[1] == '\n' || bfr[1] == '\0'))
314 message = realloc(message, msgsize+l+1);
315 (void) strcpy(message+msgsize, bfr);
318 message = realloc(message, (unsigned)(msgsize+1));
320 else { /* Use read so you can send binary messages... */
321 while ((nchars = read(fileno(stdin), bfr, sizeof bfr))) {
323 fprintf(stderr, "Read error from stdin! Can't continue!\n");
326 message = realloc(message, (unsigned)(msgsize+nchars));
327 (void) memcpy(message+msgsize, bfr, nchars);
331 message = realloc(message, (unsigned)(msgsize+1));
335 notice.z_opcode = opcode;
337 un_tabify(&message, &msgsize);
338 notice.z_message = message;
339 notice.z_message_len = msgsize;
341 send_off(¬ice, 1);
346 send_off(ZNotice_t *notice,
349 int i, success, retval;
350 char bfr[BUFSIZ], realm_recip[BUFSIZ], dest[3 * BUFSIZ];
355 for (i=0;i<nrecips || i==0;i++) {
357 sprintf(realm_recip, "%s@%s", (nrecips) ? recips[i] : "", realm);
358 notice->z_recipient = realm_recip;
360 notice->z_recipient = (nrecips) ? recips[i] : "";
363 strcpy(dest, recips[i]);
364 else if (!strcmp(class, DEFAULT_CLASS))
365 sprintf(dest, "instance \"%s\"", inst);
366 else if (!strcmp(inst, DEFAULT_INSTANCE))
367 sprintf(dest, "class \"%s\"", class);
369 sprintf(dest, "class \"%s\", instance \"%s\"", class, inst);
371 printf("Sending %smessage, class %s, instance %s, to %s\n",
372 auth?"authenticated ":"",
374 nrecips?notice->z_recipient:"everyone");
375 if ((retval = ZSendNotice(notice, auth)) != ZERR_NONE) {
376 (void) sprintf(bfr, "while sending notice to %s", dest);
377 com_err(whoami, retval, bfr);
380 if (real && !quiet) {
382 printf("Queued... ");
384 printf("Message queued for %s... ", dest);
387 if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *) 0,
389 (char *)¬ice->z_uid)) !=
391 ZFreeNotice(&retnotice);
392 (void) sprintf(bfr, "while waiting for acknowledgement for %s",
394 com_err(whoami, retval, bfr);
397 if (retnotice.z_kind == SERVNAK) {
399 printf("Received authorization failure while sending to %s\n",
402 ZFreeNotice(&retnotice);
403 break; /* if auth fails, punt */
405 if (retnotice.z_kind != SERVACK || !retnotice.z_message_len) {
407 printf("Detected server failure while receiving acknowledgement for %s\n",
410 ZFreeNotice(&retnotice);
413 if (!strcmp(retnotice.z_message, ZSRVACK_SENT)) {
417 } else if (!strcmp(retnotice.z_message, ZSRVACK_NOTSENT)) {
418 if (verbose && real && !quiet) {
419 if (strcmp(class, DEFAULT_CLASS)) {
420 fprintf(stderr, "Not logged in or not subscribing to class %s, instance %s\n",
424 "Not logged in or not subscribing to messages\n");
430 "No one subscribing to class %s, instance %s\n",
433 if (strcmp(class, DEFAULT_CLASS)) {
434 fprintf(stderr, "%s: Not logged in or not subscribing to class %s, instance %s\n",
435 notice->z_recipient, class, inst);
437 fprintf(stderr, "%s: Not logged in or not subscribing to messages\n",
438 notice->z_recipient);
444 printf("Internal failure - illegal message field in server response\n");
445 ZFreeNotice(&retnotice);
455 "Usage: %s [-a] [-o] [-d] [-v] [-q] [-n] [-t] [-u] [-l]\n\
456 \t[-c class] [-i inst] [-O opcode] [-f fsname] [-s signature] [-C]\n\
457 \t[user ...] [-F format] [-r realm] [-m message]\n", s);
458 fprintf(stderr,"\t-f and -c are mutually exclusive\n\
459 \t-f and -i are mutually exclusive\n\
460 \trecipients must be specified unless -c or -f specifies a class\n\
461 \tother than the default class or -i or -f specifies an instance\n\
462 \tother than the default or urgent instance\n");
467 if the -f option is specified, this routine is called to canonicalize
468 an instance of the form hostname[:pack]. It turns the hostname into the
469 name returned by gethostbyname(hostname)
473 fix_filsrv_inst(char *str)
475 static char fsinst[BUFSIZ];
479 ptr = strchr(str,':');
483 hp = gethostbyname(str);
489 (void) strcpy(fsinst, hp->h_name);
491 (void) strcat(fsinst, ":");
493 (void) strcat(fsinst, ptr);
498 /* convert tabs in the buffer into appropriate # of spaces.
499 slightly tricky since the buffer can have NUL's in it. */
502 #define TABSTOP 8 /* #chars between tabstops */
503 #endif /* ! TABSTOP */
506 un_tabify(char **bufp,
509 register char *cp, *cp2;
512 register int column; /* column of next character */
513 register int size = *sizep;
515 for (cp = *bufp, i = 0; size; size--, cp++)
517 i++; /* count tabs in buffer */
520 return; /* no tabs == no work */
522 /* To avoid allocation churning, allocate enough extra space to convert
523 every tab into TABSTOP spaces */
524 /* only add (TABSTOP-1)x because we re-use the cell holding the
526 cp = malloc((unsigned)(*sizep + (i * (TABSTOP-1))));
528 return; /* punt expanding if memory fails */
530 /* Copy buffer, converting tabs to spaces as we go */
531 for (cp2 = *bufp, column = 1, size = *sizep; size; cp2++, size--) {
535 /* newline or null: reset column */
537 *cp++ = *cp2; /* copy the newline */
540 /* copy the character */
546 /* it's a tab, compute how many spaces to expand into. */
547 i = TABSTOP - ((column - 1) % TABSTOP);
549 *cp++ = ' '; /* fill in the spaces */
551 (*sizep)++; /* increment the size */
553 (*sizep)--; /* remove one (we replaced the tab) */
557 free(*bufp); /* free the old buf */