summaryrefslogtreecommitdiff
path: root/lib/ZRetSubs.c
blob: 7ef9a7453d4305387162ce3d2c5e01588747979d (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
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains source for the ZRetrieveSubscriptions function.
 *
 *	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". 
 */
/* $Header$ */

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

#include <zephyr/mit-copyright.h>

#include <zephyr/zephyr_internal.h>

#define MAXSUBPACKETS 200 /* If a person has more than 1000
			   *  subscriptions, he loses! */

#ifdef notdef
Code_t ZRetrieveSubscriptions(port,nsubs)
	u_short port;
	int *nsubs;
{
	int subscription_pred();
	
	int i,retval,nrecv,totalrecv,npacket,thispacket,returncode,gimmeack;
	ZNotice_t notice,retnotice;
	ZPacket_t buffer;
	char *ptr,*end,*ptr2;
	char subs_recvd[MAXSUBPACKETS],asciiport[50];
	
	retval = ZFlushSubscriptions();

	if (retval != ZERR_NONE && retval != ZERR_NOSUBSCRIPTIONS)
		return (retval);
	
	notice.z_kind = ACKED;
	notice.z_port = 0;
	notice.z_class = ZEPHYR_CTL_CLASS;
	notice.z_class_inst = ZEPHYR_CTL_CLIENT;
	notice.z_opcode = CLIENT_GIMMESUBS;
	notice.z_sender = 0;
	notice.z_recipient = "";
	notice.z_default_format = 0;
	notice.z_message = asciiport;
	if ((retval = ZMakeAscii(asciiport,sizeof(asciiport),
				 (unsigned char *)&port,
				 sizeof(u_short))) != ZERR_NONE)
		return (retval);
	notice.z_message_len = strlen(asciiport)+1;

	if ((retval = ZSendNotice(&notice,ZAUTH)) != ZERR_NONE)
		return (retval);

	for (i=0;i<MAXSUBPACKETS;i++)
		subs_recvd[i] = 'n';
	
	nrecv = 0;
	totalrecv = -1;
	gimmeack = 0;
	__subscriptions_num = 0;
	__subscriptions_list = (ZSubscription_t *) 0;

	returncode = ZERR_NONE;

	while (nrecv < totalrecv || totalrecv == -1 || !gimmeack) {
		if ((retval = ZIfNotice(buffer,sizeof buffer,&retnotice,NULL,
					ZCompareUIDPred,
					(char *)&notice.z_uid)) !=
		    ZERR_NONE)
			return (retval);

		if (retnotice.z_kind == SERVNAK)
			return (ZERR_SERVNAK);
	
		if (retnotice.z_kind != SERVACK)
			return (ZERR_INTERNAL);

		if (!strcmp(retnotice.z_opcode,CLIENT_GIMMESUBS)) {
			gimmeack = 1;
			continue;
		} 
		ptr = index(retnotice.z_opcode,'/');
		if (!ptr)
			return (ZERR_INTERNAL);
		*ptr = '\0';
		totalrecv = atoi(ptr+1);
		if (totalrecv > MAXSUBPACKETS) {
			returncode = ZERR_TOOMANYSUBS;
			totalrecv = MAXSUBPACKETS;
		} 
		thispacket = atoi(retnotice.z_opcode);
		if (thispacket > MAXSUBPACKETS)
			continue;
		
		if (subs_recvd[thispacket] == 'y')
			continue;
		subs_recvd[thispacket] = 'y';
		nrecv++;

		end = retnotice.z_message+retnotice.z_message_len;

		npacket = 0;
		for (ptr=retnotice.z_message;ptr<end;ptr++)
			if (!*ptr)
				npacket++;

		npacket /= 3;

		__subscriptions_list = (ZSubscription_t *)
			realloc(__subscriptions_list,
				(unsigned)(__subscriptions_num+npacket)*
			       sizeof(ZSubscription_t));
		if (!__subscriptions_list)
			return (ENOMEM);
	
		for (ptr=retnotice.z_message,i=__subscriptions_num;
		     i<__subscriptions_num+npacket;i++) {
			__subscriptions_list[i].class = (char *)
				malloc((unsigned)strlen(ptr)+1);
			if (!__subscriptions_list[i].class)
				return (ENOMEM);
			(void) strcpy(__subscriptions_list[i].class,ptr);
			ptr += strlen(ptr)+1;
			__subscriptions_list[i].classinst = (char *)
				malloc((unsigned)strlen(ptr)+1);
			if (!__subscriptions_list[i].classinst)
				return (ENOMEM);
			(void) strcpy(__subscriptions_list[i].classinst,ptr);
			ptr += strlen(ptr)+1;
			ptr2 = ptr;
			if (!*ptr2)
				ptr2 = "*";
			__subscriptions_list[i].recipient = (char *)
				malloc((unsigned)strlen(ptr2)+1);
			if (!__subscriptions_list[i].recipient)
				return (ENOMEM);
			(void) strcpy(__subscriptions_list[i].recipient,ptr2);
			ptr += strlen(ptr)+1;
		}

		__subscriptions_num += npacket;
	} 

	__subscriptions_next = 0;
	*nsubs = __subscriptions_num;

	return (returncode);
}
#endif