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
|
/* This file is part of the Project Athena Zephyr Notification System.
* It is one of the source files comprising zwgc, the Zephyr WindowGram
* client.
*
* Created by: Marc Horowitz <marc@athena.mit.edu>
*
* $Id$
*
* Copyright (c) 1989 by the Massachusetts Institute of Technology.
* For copying and distribution information, see the file
* "mit-copyright.h".
*/
#if (!defined(lint) && !defined(SABER))
static const char rcsid_subscriptions_c[] = "$Id$";
#endif
/****************************************************************************/
/* */
/* Subscriptions.c: code to deal with subscriptions & punting: */
/* */
/****************************************************************************/
#include <sysdep.h>
#include <zephyr/zephyr.h>
#include <netdb.h>
#include "new_memory.h"
#include "new_string.h"
#include "int_dictionary.h"
#include "zwgc.h"
#include "subscriptions.h"
#include "error.h"
#include "file.h"
#include "main.h"
/****************************************************************************/
/* */
/* Code to implement punting of notices: */
/* */
/****************************************************************************/
/*
*
*/
#ifndef CMU_ZCTL_PUNT
static
#endif
int_dictionary puntable_addresses_dict = 0;
static void
init_puntable_dict(void)
{
puntable_addresses_dict = int_dictionary_Create(33);
}
static string
address_to_string(string class,
string instance,
string recipient)
{
string result;
/*
* Treat a recipient of "" as "*":
*/
if (string_Eq(recipient,""))
recipient = "*";
else if (recipient[0] == '@') {
recipient = string_Concat("*", recipient);
}
/*
* The following is a hack for now only. It should be replaced with
* several calls to escape_code... <<<>>>
*/
result = string_Concat(class, "\001");
result = string_Concat2(result, instance);
result = string_Concat2(result, "\001");
result = string_Concat2(result, recipient);
string_Downcase(result);
return(result);
}
int puntable_address_p(string class,
string instance,
string recipient)
{
string temp;
int ret;
if (!puntable_addresses_dict)
init_puntable_dict();
temp = address_to_string(class, instance, recipient);
ret = (int)int_dictionary_Lookup(puntable_addresses_dict, temp);
free(temp);
if (ret)
return 1;;
/* This kludge is to allow punts of wildcard instance to work */
temp = address_to_string(class, "*", recipient);
if (int_dictionary_Lookup(puntable_addresses_dict, temp)) {
free(temp);
return(1);
}
free(temp);
return(0);
}
void punt(string class,
string instance,
string recipient)
{
string temp;
if (!puntable_addresses_dict)
init_puntable_dict();
temp = address_to_string(class, instance, recipient);
(void)int_dictionary_Define(puntable_addresses_dict, temp, 0);
free(temp);
}
void unpunt(string class,
string instance,
string recipient)
{
string temp;
int_dictionary_binding *binding;
if (!puntable_addresses_dict)
init_puntable_dict();
temp = address_to_string(class, instance, recipient);
binding = int_dictionary_Define(puntable_addresses_dict, temp, 0);
free(temp);
if (binding)
int_dictionary_Delete(puntable_addresses_dict, binding);
}
/****************************************************************************/
/* */
/* Code to implement batching [un]subscription requests: */
/* */
/****************************************************************************/
/*
* <<<>>> these routines require zwgc_active to be false (0)
*/
#define BATCH_SIZE 20
static int subscription_list_size = 0;
static ZSubscription_t subscription_list[BATCH_SIZE];
static int unsubscription_list_size = 0;
static ZSubscription_t unsubscription_list[BATCH_SIZE];
static void
free_subscription_list(ZSubscription_t *list,
int number_of_elements)
{
int i;
for (i=0; i<number_of_elements; i++) {
free(list[i].zsub_class);
free(list[i].zsub_classinst);
free(list[i].zsub_recipient);
}
}
static void
flush_subscriptions(void)
{
TRAP(ZSubscribeTo(subscription_list,subscription_list_size, 0),
"while subscribing");
free_subscription_list(subscription_list, subscription_list_size);
subscription_list_size = 0;
}
static void
flush_unsubscriptions(void)
{
if (unsubscription_list_size)
TRAP(ZUnsubscribeTo(unsubscription_list, unsubscription_list_size, 0),
"while unsubscribing");
free_subscription_list(unsubscription_list, unsubscription_list_size);
unsubscription_list_size = 0;
}
static void
subscribe(string class,
string instance,
string recipient)
{
subscription_list[subscription_list_size].zsub_class = string_Copy(class);
subscription_list[subscription_list_size].zsub_classinst= string_Copy(instance);
subscription_list[subscription_list_size].zsub_recipient=string_Copy(recipient);
if (++subscription_list_size == BATCH_SIZE)
flush_subscriptions();
}
static void
unsubscribe(string class,
string instance,
string recipient)
{
unsubscription_list[unsubscription_list_size].zsub_class = string_Copy(class);
unsubscription_list[unsubscription_list_size].zsub_classinst
= string_Copy(instance);
unsubscription_list[unsubscription_list_size].zsub_recipient
= string_Copy(recipient);
if (++unsubscription_list_size == BATCH_SIZE)
flush_unsubscriptions();
}
/****************************************************************************/
/* */
/* Code to implement reading [un]subscriptions from a file: */
/* */
/****************************************************************************/
#define TOKEN_HOSTNAME "%host%"
#define TOKEN_CANONNAME "%canon%"
#define TOKEN_ME "%me%"
#define TOKEN_WILD "*"
char ourhost[MAXHOSTNAMELEN],ourhostcanon[MAXHOSTNAMELEN];
static void
inithosts(void)
{
struct hostent *hent;
if (gethostname(ourhost, sizeof(ourhost)-1) == -1) {
ERROR3("unable to retrieve hostname, %s and %s will be wrong in subscriptions.\n", TOKEN_HOSTNAME, TOKEN_CANONNAME);
return;
}
if (!(hent = gethostbyname(ourhost))) {
ERROR2("unable to resolve hostname, %s will be wrong in subscriptions.\n", TOKEN_CANONNAME);
strncpy(ourhostcanon, ourhost, sizeof(ourhostcanon)-1);
return;
}
strncpy(ourhostcanon, hent->h_name, sizeof(ourhostcanon)-1);
return;
}
static void
macro_sub(char *str)
{
static int initedhosts = 0;
if (!initedhosts) {
inithosts();
initedhosts = 1;
}
if (string_Eq(str, TOKEN_ME))
strcpy(str, ZGetSender());
else if (string_Eq(str, TOKEN_HOSTNAME))
strcpy(str, ourhost);
else if (string_Eq(str, TOKEN_CANONNAME))
strcpy(str, ourhostcanon);
}
#define UNSUBSCRIBE_CHARACTER '!'
#define PUNT_CHARACTER '-'
static void
load_subscriptions_from_file(FILE *file)
{
char line[BUFSIZ];
char class_buffer[BUFSIZ], instance[BUFSIZ], recipient[BUFSIZ];
char *class, *temp;
char c;
while ((!feof(file)) && (!ferror(file))) {
if (fgets(line, BUFSIZ, file)) {
class = class_buffer;
/* Parse line */
/* <<<>>>
* The below does NOT work is the recipient field is "":
*/
temp = strchr(line, '#');
if (temp)
*temp = '\0';
for (temp=line; *temp && *temp==' '; temp++) ;
if (!*temp || *temp=='\n')
continue;
sscanf(temp,"%[^,],%[^,],%s", class, instance, recipient);
/* skip type indicator if any: */
c = class[0];
if (c==UNSUBSCRIBE_CHARACTER || c==PUNT_CHARACTER)
class++;
/* perform macro substitutions */
macro_sub(class);
macro_sub(instance);
macro_sub(recipient);
/* do the right thing with it */
switch (c) {
case UNSUBSCRIBE_CHARACTER:
unsubscribe(class, instance, recipient);
break;
case PUNT_CHARACTER:
punt(class, instance, recipient);
break;
default:
subscribe(class, instance, recipient);
break;
}
} else {
break;
}
}
if (ferror(file)) {
com_err("zwgc", errno, "while reading from subscription file");
exit(1);
}
flush_subscriptions();
flush_unsubscriptions();
fclose(file);
}
#define DEFSUBS "/dev/null"
static void
load_subscriptions(void)
{
FILE *subscriptions_file;
/* no system default sub file on client--they live on the server */
/* BUT...we need to use something to call load_subscriptions_from_file,
so we use /dev/null */
subscriptions_file = locate_file(subscriptions_filename_override,
USRSUBS, DEFSUBS);
if (subscriptions_file)
load_subscriptions_from_file(subscriptions_file);
}
/****************************************************************************/
/* */
/* Code to implement shutdown and startup: */
/* */
/****************************************************************************/
int zwgc_active = 0;
static ZSubscription_t *saved_subscriptions = NULL;
static int number_of_saved_subscriptions;
void zwgc_shutdown(void)
{
if (!zwgc_active)
return;
TRAP(ZRetrieveSubscriptions(0, &number_of_saved_subscriptions),
"while retrieving zephyr subscription list");
if (error_code)
return;
saved_subscriptions = (ZSubscription_t *)
malloc(number_of_saved_subscriptions*sizeof(ZSubscription_t));
if (number_of_saved_subscriptions)
TRAP(ZGetSubscriptions(saved_subscriptions,
&number_of_saved_subscriptions),
"while getting subscriptions");
if (error_code) {
free(saved_subscriptions);
saved_subscriptions = NULL;
}
TRAP(ZCancelSubscriptions(0), "while canceling subscriptions") ;
zwgc_active = 0;
}
void zwgc_startup(void)
{
if (zwgc_active)
return;
if (saved_subscriptions) {
TRAP(ZSubscribeToSansDefaults(saved_subscriptions,number_of_saved_subscriptions,0),
"while resubscribing to zephyr messages");
free(saved_subscriptions);
saved_subscriptions = NULL;
} else
load_subscriptions();
zwgc_active = 1;
}
|