summaryrefslogtreecommitdiff
path: root/clients/zwrite/zwrite.c
blob: a88e346824ff6c9123c0cdd000700537679d0373 (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
/* 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 FUDGEFACTOR 10
#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 *getenv();

main(argc,argv)
	int argc;
	char *argv[];
{
	ZNotice_t notice, retnotice;
	ZPacket_t buffer;
	int retval, len, arg, nocheck, nchars, maxlen;
	long ourtime;
	char bfr[BUFSIZ], message[Z_MAXPKTLEN], *ptr, *signature;

	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));

	maxlen = Z_MAXPKTLEN-FUDGEFACTOR;
	auth = ZAUTH;
	verbose = quiet = msgarg = nrecips = nocheck = 0;

	if (!(class = ZGetVariable("zwrite-class")))
		class = DEFAULT_CLASS;
	if (!(inst = ZGetVariable("zwrite-inst")))
		inst = DEFAULT_INSTANCE;
	signature = ZGetVariable("zwrite-signature");
	if (signature)
		maxlen -= strlen(signature)+1;
	
	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 = "";

	if (!nocheck)
		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");

	notice.z_opcode = "";
	notice.z_recipient = "foobar12";
	if (signature)
		notice.z_default_format = "Message from $1 <$sender> at $time:\n\n$2";
	else
		notice.z_default_format = "Message from $sender at $time:\n\n$2";
	
	if ((retval = ZFormatNotice(&notice, buffer, sizeof buffer, &len,
				    auth)) != ZERR_NONE) {
		com_err(whoami, retval, "formatting notice");
		exit(1);
	} 

	maxlen -= len;
	
	if (signature) {
		strcpy(message, signature);
		ptr = message+strlen(message)+1;
	}
	else {
		message[0] = '\0';
		ptr = message+1;
	}
	
	if (msgarg) {
		for (arg=msgarg;arg<argc;arg++) {
			strcpy(ptr, argv[arg]);
			if (arg != argc-1)
				strcat(ptr, " ");
			ptr += strlen(ptr);
		}
		*ptr++ = '\n';
		*ptr++ = '\0';
	}
	else {
		if (isatty(0)) {
			for (;;) {
				if (!fgets(bfr, sizeof bfr, stdin))
					break;
				if (bfr[0] == '.' &&
				    (bfr[1] == '\n' || bfr[1] == '\0'))
					break;
				if (strlen(bfr)+(ptr-message) > maxlen) {
					printf("Your message is too long.  It will be truncated at this line.\n");
					break;
				}
				strcpy(ptr, bfr);
				ptr += strlen(ptr);
			}
			*ptr++ = '\0';
		}
		else { /* Use read so you can send binary messages... */
			nchars = read(fileno(stdin), ptr, maxlen+1);
			if (nchars == -1) {
				fprintf(stderr, "Read error from stdin!  Can't continue!\n");
				exit(1);
			}
			if (nchars > maxlen) {
				printf("Message too long.  Truncated.\n");
				nchars = maxlen;
			}
			ptr += nchars;
		} 
	}

	notice.z_message = message;
	notice.z_message_len = ptr-message;

	send_off(&notice, 1);
}

send_off(notice, real)
	ZNotice_t *notice;
	int real;
{
	int i, success, retval;
	char bfr[BUFSIZ];
	ZPacket_t 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(buffer, sizeof buffer, &retnotice,
					0, ZCompareUIDPred,
					(char *)&notice->z_uid)) !=
		    ZERR_NONE) {
			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);
			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);
			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");
		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);
}