summaryrefslogtreecommitdiff
path: root/clients/zwrite/zwrite.c
blob: 0eebf920fc922ab3ea33d593733e8d1e6dfc5822 (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
/* 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 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>

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

#define DEFAULT_CLASS "MESSAGE"
#define DEFAULT_INSTANCE "PERSONAL"
#define URGENT_INSTANCE "URGENT"

#define MAXRECIPS 100

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

char *malloc(), *realloc();

main(argc, argv)
    int argc;
    char *argv[];
{
    ZNotice_t notice, retnotice;
    char *buffer;
    int retval, len, arg, nocheck, nchars, msgsize;
    long ourtime;
    char bfr[BUFSIZ], *message, *ptr, *signature;
    char classbfr[BUFSIZ], instbfr[BUFSIZ], sigbfr[BUFSIZ];
	
    whoami = argv[0];

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

    if (argc < 2)
	usage(whoami);

    bzero(&notice, sizeof(ZNotice_t));

    auth = ZAUTH;
    verbose = quiet = msgarg = nrecips = nocheck = 0;

    if (class = ZGetVariable("zwrite-class")) {
	strcpy(classbfr, class);
	class = classbfr;
    }
    else
	class = DEFAULT_CLASS;
    if (inst = ZGetVariable("zwrite-inst")) {
	strcpy(instbfr, inst);
	inst = instbfr;
    }
    else
	inst = DEFAULT_INSTANCE;
    signature = ZGetVariable("zwrite-signature");
    if (signature) {
	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 'o':
	    class = DEFAULT_CLASS;
	    inst = DEFAULT_INSTANCE;
	    break;
	case 'd':
	    auth = ZNOAUTH;
	    break;
	case 'v':
	    verbose = 1;
	    break;
	case 'q':
	    quiet = 1;
	    break;
	case 'n':
	    nocheck = 1;
	    break;
	case 'u':
	    inst = URGENT_INSTANCE;
	    break;
	case 'i':
	    if (arg == argc-1)
		usage(whoami);
	    arg++;
	    inst = argv[arg];
	    break;
	case 'c':
	    if (arg == argc-1)
		usage(whoami);
	    arg++;
	    class = argv[arg];
	    break;
	case 'm':
	    if (arg == argc-1)
		usage(whoami);
	    msgarg = arg+1;
	    break;
	default:
	    usage(whoami);
	}
    }

    if (!nrecips && !(strcmp(class, DEFAULT_CLASS) ||
		      strcmp(inst, DEFAULT_INSTANCE))) {
	fprintf(stderr, "No recipients specified.\n");
	exit (1);
    }

    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 = "";
    notice.z_default_format = "";
    notice.z_num_other_fields = 0;

    if (!nocheck && !msgarg)
	send_off(&notice, 0);
	
    if (!msgarg && isatty(0))
	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(strlen(signature)+1);
	strcpy(message, signature);
	msgsize = strlen(message)+1;
    }
	
    if (msgarg) {
	for (arg=msgarg;arg<argc;arg++) {
	    if (message)
		    message = realloc(message, msgsize+strlen(argv[arg])+
				      (arg == argc-1)?2:1);
	    else
		    message = malloc(strlen(argv[arg])+ ((arg == argc-1)?2:1));
	    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 (bfr[0] == '.' &&
		    (bfr[1] == '\n' || bfr[1] == '\0'))
		    break;
		if (message)
			message = realloc(message, msgsize+
					  strlen(bfr));
		else
			message = malloc(msgsize+strlen(bfr));
		strcpy(message+msgsize, bfr);
		msgsize += strlen(bfr);
	    }
	    message = realloc(message, 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, msgsize+nchars);
		bcopy(bfr, message+msgsize, nchars);
		msgsize += nchars;
	    }
	} 
    }

    notice.z_opcode = "";
    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];
    char *buffer;
    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, real?auth:ZNOAUTH)) != ZERR_NONE) {
	    sprintf(bfr, "while sending notice to %s", 
		    nrecips?notice->z_recipient:inst);
	    com_err(whoami, retval, bfr);
	    continue;
	}
	if ((retval = ZIfNotice(&retnotice, 
				0, ZCompareUIDPred, 
				(char *)&notice->z_uid)) !=
	    ZERR_NONE) {
	    ZFreeNotice(&retnotice);
	    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 authentication failure while sending to %s\n", 
		   nrecips?notice->z_recipient:inst);
	    ZFreeNotice(&retnotice);
	    continue;
	} 
	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;
{
    printf("Usage: %s [-a] [-d] [-v] [-q] [-u] [-o] [-c class] [-i inst] [user ...]\n       [-m message]\n", s);
    exit(1);
}