summaryrefslogtreecommitdiff
path: root/clients/zwrite/zwrite.c
blob: 5140031ca00e77d23402b37906c3c8c615192db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains code for the "zwrite" command.
 *
 *	Created by:	Robert French
 *
 *	$Source$
 *	$Author$
 *
 *	Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
 *	For copying and distribution information, see the file
 *	"mit-copyright.h". 
 */

#include <zephyr/mit-copyright.h>

#include <zephyr/zephyr.h>
#include <string.h>
#include <netdb.h>
#include <pwd.h>
#include <ctype.h>

#ifndef lint
static char rcsid_zwrite_c[] = "$Id$";
#endif lint

#define DEFAULT_CLASS "MESSAGE"
#define DEFAULT_INSTANCE "PERSONAL"
#define URGENT_INSTANCE "URGENT"
#define DEFAULT_OPCODE "VERBATIM"
#define PRETTY_OPCODE "PRETTY"
#define FILSRV_CLASS "FILSRV"

#define MAXRECIPS 100

int nrecips, msgarg, verbose, quiet, nodot;
char *whoami, *inst, *class, *opcode, *recips[MAXRECIPS];
int (*auth)();
void un_tabify();

extern char *malloc(), *realloc();
char *fix_filsrv_inst();

main(argc, argv)
    int argc;
    char *argv[];
{
    ZNotice_t notice;
    int retval, arg, nocheck, nchars, msgsize, filsys, tabexpand, pretty;
    char bfr[BUFSIZ], *message, *signature = NULL;
    char classbfr[BUFSIZ], instbfr[BUFSIZ], sigbfr[BUFSIZ], opbfr[BUFSIZ];
	
    whoami = argv[0];

    if ((retval = ZInitialize()) != ZERR_NONE) {
	com_err(whoami, retval, "while initializing");
	exit(1);
    }

    if (argc < 2)
	usage(whoami);

    bzero((char *) &notice, sizeof(notice));

    auth = ZAUTH;
    verbose = quiet = msgarg = nrecips = nocheck = filsys = nodot = pretty = 0;
    tabexpand = 1;

    if (class = ZGetVariable("zwrite-class")) {
	(void) strcpy(classbfr, class);
	class = classbfr;
    }
    else
	class = DEFAULT_CLASS;
    if (inst = ZGetVariable("zwrite-inst")) {
	(void) strcpy(instbfr, inst);
	inst = instbfr;
    }
    else
	inst = DEFAULT_INSTANCE;
    if (opcode = ZGetVariable("zwrite-opcode")) {
	(void) strcpy(opbfr, opcode);
	opcode = opbfr;
    }
    else
	opcode = DEFAULT_OPCODE;
    signature = ZGetVariable("zwrite-signature");
    if (signature) {
	(void) strcpy(sigbfr, signature);
	signature = sigbfr;
    }
	
    arg = 1;
	
    for (;arg<argc&&!msgarg;arg++) {
	if (*argv[arg] != '-') {
	    recips[nrecips++] = argv[arg];
	    continue;
	}
	if (strlen(argv[arg]) > 2)
	    usage(whoami);
	switch (argv[arg][1]) {
	case 'a':		/* Backwards compatibility */
	    break;
	case 'p':
	    if (pretty == -1)
	        usage(whoami);
	    pretty = 1;
	    opcode = PRETTY_OPCODE;
	    break;
	case 'o':
	    class = DEFAULT_CLASS;
	    inst = DEFAULT_INSTANCE;
	    opcode = DEFAULT_OPCODE;
	    break;
	case 'd':
	    auth = ZNOAUTH;
	    break;
	case 'v':
	    verbose = 1;
	    break;
	case 'q':
	    quiet = 1;
	    break;
	case 'n':
	    nocheck = 1;
	    break;
	case 't':
	    tabexpand = 0;
	    break;
	case 'u':
	    inst = URGENT_INSTANCE;
	    break;
	case 'O':
	    if (arg == argc-1 || pretty == 1)
	        usage(whoami);
	    arg++;
	    opcode = argv[arg];
	    pretty = -1;
	    break;
	case 'i':
	    if (arg == argc-1 || filsys == 1)
		usage(whoami);
	    arg++;
	    inst = argv[arg];
	    filsys = -1;
	    break;
	case 'c':
	    if (arg == argc-1 || filsys == 1)
		usage(whoami);
	    arg++;
	    class = argv[arg];
	    filsys = -1;
	    break;
	case 'f':
	    if (arg == argc-1 || filsys == -1)
		usage(whoami);
	    arg++;
	    class = FILSRV_CLASS;
	    inst = fix_filsrv_inst(argv[arg]);
	    filsys = 1;
	    break;
	case 's':
	    if (arg == argc-1)
		usage(whoami);
	    arg++;
	    signature = argv[arg];
	    break;
	case 'm':
	    if (arg == argc-1)
		usage(whoami);
	    msgarg = arg+1;
	    break;
	case 'l':			/* literal */
	    nodot = 1;
	    break;
	default:
	    usage(whoami);
	}
    }

    if (!nrecips && !(strcmp(class, DEFAULT_CLASS) ||
		      (strcmp(inst, DEFAULT_INSTANCE) &&
		       strcmp(inst, URGENT_INSTANCE)))) {
	/* must specify recipient if using default class and
	   (default instance or urgent instance) */
	fprintf(stderr, "No recipients specified.\n");
	usage(whoami);
    }

    if (!signature) {
	/* try to find name in the password file */
	register struct passwd *pwd;
	register char *cp = sigbfr;
	register char *cp2, *pp;

	pwd = getpwuid(getuid());
	if (pwd) {
	    cp2 = pwd->pw_gecos;
	    for (; *cp2 && *cp2 != ',' ; cp2++) {
		if (*cp2 == '&') {
		    pp = pwd->pw_name;
		    *cp++ = islower(*pp) ? toupper(*pp) : *pp;
		    pp++;
		    while (*pp)
			*cp++ = *pp++;
		} else
		    *cp++ = *cp2;
	    }
	    signature = sigbfr;
	}
    }	

    notice.z_kind = ACKED;
    notice.z_port = 0;
    notice.z_class = class;
    notice.z_class_inst = inst;
    notice.z_opcode = "PING";
    notice.z_sender = 0;
    notice.z_message_len = 0;
    notice.z_recipient = "";
    if (filsys == 1)
	    notice.z_default_format = "@bold(Filesystem Operation Message for $instance:)\nFrom: @bold($sender)\n$message";
    else if (auth == ZAUTH) {
	if (signature)
	    notice.z_default_format = "Class $class, Instance $instance:\nTo: @bold($recipient)\n@bold($1) <$sender>\n\n$2";
	else
	    notice.z_default_format = "Class $class, Instance $instance:\nTo: @bold($recipient)\n$message";
    } else {
	if (signature)
	    notice.z_default_format = "@bold(UNAUTHENTIC) Class $class, Instance $instance:\n@bold($1) <$sender>\n\n$2";
	else
	    notice.z_default_format = "@bold(UNAUTHENTIC) Class $class, Instance $instance:\n$message";
    }
    if (!nocheck && !msgarg && filsys != 1)
	send_off(&notice, 0);
	
    if (quiet)
	notice.z_kind = UNACKED;	/* change for real sending */

    if (!msgarg && isatty(0))
	if (nodot)
	    printf("Type your message now.  End with the end-of-file character.\n");
	else
	    printf("Type your message now.  End with control-D or a dot on a line by itself.\n");
	
    message = NULL;
    msgsize = 0;
    if (signature) {
	message = malloc((unsigned)(strlen(signature)+2));
	(void) strcpy(message, signature);
	msgsize = strlen(message);
	message[msgsize++] = '\n';
	message[msgsize++] = '\0';
    } else {
	message = malloc(1);
	message[msgsize++] = '\0';
    }

    if (msgarg) {
	int size = msgsize;
	for (arg=msgarg;arg<argc;arg++)
		size += (strlen(argv[arg]) + 1);
	size++;				/* for the newline */
	message = realloc(message, (unsigned) size);
	for (arg=msgarg;arg<argc;arg++) {
	    (void) strcpy(message+msgsize, argv[arg]);
	    msgsize += strlen(argv[arg]);
	    if (arg != argc-1) {
		message[msgsize] = ' ';
		msgsize++;
	    } 
	}
	message[msgsize] = '\n';
	message[msgsize+1] = '\0';
	msgsize += 2;
    } else {
	if (isatty(0)) {
	    for (;;) {
		if (!fgets(bfr, sizeof bfr, stdin))
		    break;
		if (!nodot && bfr[0] == '.' &&
		    (bfr[1] == '\n' || bfr[1] == '\0'))
		    break;
		message = realloc(message, (unsigned)(msgsize+strlen(bfr)));
		(void) strcpy(message+msgsize, bfr);
		msgsize += strlen(bfr);
	    }
	    message = realloc(message, (unsigned)(msgsize+1));
	    message[msgsize++] = '\0';
	}
	else {	/* Use read so you can send binary messages... */
	    while (nchars = read(fileno(stdin), bfr, sizeof bfr)) {
		if (nchars == -1) {
		    fprintf(stderr, "Read error from stdin!  Can't continue!\n");
		    exit(1);
		}
		message = realloc(message, (unsigned)(msgsize+nchars));
		bcopy(bfr, message+msgsize, nchars);
		msgsize += nchars;
	    }
	    /* end of msg */
	    message = realloc(message, (unsigned)(msgsize+1));
	    message[msgsize++] = '\0';	/* null-terminate */
	} 
    }

    notice.z_opcode = opcode;
    if (tabexpand)
	un_tabify(&message, &msgsize);
    notice.z_message = message;
    notice.z_message_len = msgsize;

    send_off(&notice, 1);
    exit(0);
}

send_off(notice, real)
    ZNotice_t *notice;
    int real;
{
    int i, success, retval;
    char bfr[BUFSIZ];
    ZNotice_t retnotice;

    success = 0;
	
    for (i=0;i<nrecips || !nrecips;i++) {
	notice->z_recipient = nrecips?recips[i]:"";
	if (verbose && real)
	    printf("Sending %smessage, class %s, instance %s, to %s\n", 
		   auth?"authenticated ":"", 
		   class, inst, 
		   nrecips?notice->z_recipient:"everyone");
	if ((retval = ZSendNotice(notice, auth)) != ZERR_NONE) {
	    (void) sprintf(bfr, "while sending notice to %s", 
		    nrecips?notice->z_recipient:inst);
	    com_err(whoami, retval, bfr);
	    break;
	}
	if (quiet && real) {
	    if (nrecips)
		continue;		/* next! */
	    else
		break;			/* no more */
	}
	if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *) 0,
				ZCompareUIDPred, 
				(char *)&notice->z_uid)) !=
	    ZERR_NONE) {
	    ZFreeNotice(&retnotice);
	    (void) sprintf(bfr, "while waiting for acknowledgement for %s", 
		    nrecips?notice->z_recipient:inst);
	    com_err(whoami, retval, bfr);
	    continue;
	}
	if (retnotice.z_kind == SERVNAK) {
	    printf("Received authorization failure while sending to %s\n", 
		   nrecips?notice->z_recipient:inst);
	    ZFreeNotice(&retnotice);
	    break;			/* if auth fails, punt */
	} 
	if (retnotice.z_kind != SERVACK || !retnotice.z_message_len) {
	    printf("Detected server failure while receiving acknowledgement for %s\n", 
		   nrecips?notice->z_recipient:inst);
	    ZFreeNotice(&retnotice);
	    continue;
	}
	if (!real || (!quiet && real))
	    if (!strcmp(retnotice.z_message, ZSRVACK_SENT)) {
		if (real) {
		    if (verbose)
			printf("Successful\n");
		    else
			printf("%s: Message sent\n", 
			       nrecips?notice->z_recipient:inst);
		}
		else
		    success = 1;
	    } 
	    else
		if (!strcmp(retnotice.z_message, 
			    ZSRVACK_NOTSENT)) {
		    if (verbose && real) {
			if (strcmp(class, DEFAULT_CLASS))
			    printf("Not logged in or not subscribing to class %s, instance %s\n", 
				   class, inst);
			else
			    printf("Not logged in or not subscribing to messages\n");
		    } 
		    else
			if (!nrecips)
			    printf("No one subscribing to class %s, instance %s\n", 
				   class, inst);
			else {
			    if (strcmp(class, DEFAULT_CLASS))
				printf("%s: Not logged in or not subscribing to class %s, instance %s\n", 
				       notice->z_recipient, class, inst);
			    else
				printf("%s: Not logged in or not subscribing to messages\n", 
				       notice->z_recipient);
			} 
		} 
		else
		    printf("Internal failure - illegal message field in server response\n");
	ZFreeNotice(&retnotice);
	if (!nrecips)
	    break;
    }
    if (!real && !success)
	exit(1);
} 

usage(s)
    char *s;
{
    fprintf(stderr,
	    "Usage: %s [-a] [-o] [-d] [-v] [-q] [-n] [-t] [-u] [-l] [-p]\n\
\t[-c class] [-i inst] [-f fsname] [-O opcode] [-s signature]\n\
\t[user ...] [-m message]\n", s);
    fprintf(stderr,"\t-f and -c are mutually exclusive\n\
\t-f and -i are mutually exclusive\n\
\t-p and -O are mutually exclusive\n\
\trecipients must be specified unless -c or -f specifies a class\n\
\tother than the default class or -i or -f specifies an instance\n\
\tother than the default or urgent instance\n");
    exit(1);
} 

/*
  if the -f option is specified, this routine is called to canonicalize
  an instance of the form hostname[:pack].  It turns the hostname into the
  name returned by gethostbyname(hostname)
 */

char *fix_filsrv_inst(str)
char *str;
{
	static char fsinst[BUFSIZ];
	char *ptr;
	struct hostent *hp;

	ptr = index(str,':');
	if (ptr)
		*ptr = '\0';
	
	hp = gethostbyname(str);
	if (!hp) {
		if (ptr)
			*ptr = ':';
		return(str);
	}
	(void) strcpy(fsinst, hp->h_name);
	if (ptr) {
		(void) strcat(fsinst, ":");
		ptr++;
		(void) strcat(fsinst, ptr);
	}
	return(fsinst);
}

/* convert tabs in the buffer into appropriate # of spaces.
   slightly tricky since the buffer can have NUL's in it. */

#ifndef TABSTOP
#define	TABSTOP	8			/* #chars between tabstops */
#endif /* ! TABSTOP */

void
un_tabify(bufp, sizep)
char **bufp;
register int *sizep;
{
    register char *cp, *cp2;
    char *cp3;
    register int i;
    register int column;		/* column of next character */
    register int size = *sizep;

    for (cp = *bufp, i = 0; size; size--, cp++)
	if (*cp == '\t')
	    i++;			/* count tabs in buffer */

    if (!i)
	return;				/* no tabs == no work */

    /* To avoid allocation churning, allocate enough extra space to convert
       every tab into TABSTOP spaces */
    /* only add (TABSTOP-1)x because we re-use the cell holding the
       tab itself */
    cp = malloc((unsigned)(*sizep + (i * (TABSTOP-1))));
    if (!cp)				/* XXX */
	return;				/* punt expanding if memory fails */
    cp3 = cp;
    /* Copy buffer, converting tabs to spaces as we go */
    for (cp2 = *bufp, column = 1, size = *sizep; size; cp2++, size--) {
	switch (*cp2) {
	case '\n':
	case '\0':
	    /* newline or null: reset column */
	    column = 1;
	    *cp++ = *cp2;		/* copy the newline */
	    break;
	default:
	    /* copy the character */
	    *cp = *cp2;
	    cp++;
	    column++;
	    break;
	case '\t':
	    /* it's a tab, compute how many spaces to expand into. */
	    i = TABSTOP - ((column - 1) % TABSTOP);
	    for (; i > 0; i--) {
		*cp++ = ' ';		/* fill in the spaces */
		column++;
		(*sizep)++;		/* increment the size */
	    }
	    (*sizep)--;			/* remove one (we replaced the tab) */
	    break;
	}
    }
    free(*bufp);			/* free the old buf */
    *bufp = cp3;
    return;
}