1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains source for the ZRetrieveSubscriptions and
3 * ZRetrieveDefaultSubscriptions functions.
5 * Created by: Robert French
9 * Copyright (c) 1987,1988,1991 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
17 static const char rcsid_ZRetrieveSubscriptions_c[] =
21 static Code_t Z_RetSubs ();
23 /* Need STDC definition when possible for unsigned short argument. */
25 Code_t ZRetrieveSubscriptions(unsigned short port, int *nsubs)
27 Code_t ZRetrieveSubscriptions(port,nsubs)
36 if (!port) /* use default port */
39 retval = ZMakeAscii16(asciiport, sizeof(asciiport), ntohs(port));
40 if (retval != ZERR_NONE)
43 (void) memset((char *)¬ice, 0, sizeof(notice));
44 notice.z_message = asciiport;
45 notice.z_message_len = strlen(asciiport)+1;
46 notice.z_opcode = CLIENT_GIMMESUBS;
48 return(Z_RetSubs(¬ice, nsubs, ZAUTH));
51 Code_t ZRetrieveDefaultSubscriptions(nsubs)
56 (void) memset((char *)¬ice, 0, sizeof(notice));
57 notice.z_message = (char *) 0;
58 notice.z_message_len = 0;
59 notice.z_opcode = CLIENT_GIMMEDEFS;
61 return(Z_RetSubs(¬ice, nsubs, ZNOAUTH));
65 static Code_t Z_RetSubs(notice, nsubs, auth_routine)
66 register ZNotice_t *notice;
68 Z_AuthProc auth_routine;
71 int retval,nrecv,gimmeack;
74 ZSubscription_t *list = __subscriptions_list;
76 retval = ZFlushSubscriptions();
78 if (retval != ZERR_NONE && retval != ZERR_NOSUBSCRIPTIONS)
82 if ((retval = ZOpenPort((u_short *)0)) != ZERR_NONE)
85 notice->z_kind = ACKED;
86 notice->z_port = __Zephyr_port;
87 notice->z_class = ZEPHYR_CTL_CLASS;
88 notice->z_class_inst = ZEPHYR_CTL_CLIENT;
90 notice->z_recipient = "";
91 notice->z_default_format = "";
93 if ((retval = ZSendNotice(notice,auth_routine)) != ZERR_NONE)
98 list = (ZSubscription_t *) 0;
100 while (!nrecv || !gimmeack) {
101 retval = Z_WaitForNotice (&retnotice, ZCompareMultiUIDPred,
102 ¬ice->z_multiuid, SRV_TIMEOUT);
103 if (retval == ZERR_NONOTICE)
105 else if (retval != ZERR_NONE)
108 if (retnotice.z_kind == SERVNAK) {
109 ZFreeNotice(&retnotice);
110 return (ZERR_SERVNAK);
112 /* non-matching protocol version numbers means the
113 server is probably an older version--must punt */
114 if (strcmp(notice->z_version,retnotice.z_version)) {
115 ZFreeNotice(&retnotice);
118 if (retnotice.z_kind == SERVACK &&
119 !strcmp(retnotice.z_opcode,notice->z_opcode)) {
120 ZFreeNotice(&retnotice);
125 if (retnotice.z_kind != ACKED) {
126 ZFreeNotice(&retnotice);
127 return (ZERR_INTERNAL);
132 end = retnotice.z_message+retnotice.z_message_len;
134 __subscriptions_num = 0;
135 for (ptr=retnotice.z_message;ptr<end;ptr++)
137 __subscriptions_num++;
139 __subscriptions_num = __subscriptions_num / 3;
141 list = (ZSubscription_t *)
142 malloc(__subscriptions_num * sizeof(ZSubscription_t));
143 if (__subscriptions_num && !list) {
144 ZFreeNotice(&retnotice);
148 ptr = retnotice.z_message;
149 for (i = 0; i < __subscriptions_num; i++) {
150 list[i].zsub_class = (char *)
151 malloc(strlen(ptr) + 1);
152 if (!list[i].zsub_class) {
153 ZFreeNotice(&retnotice);
156 strcpy(list[i].zsub_class, ptr);
157 ptr += strlen(ptr)+1;
158 list[i].zsub_classinst = (char *)
159 malloc(strlen(ptr) + 1);
160 if (!list[i].zsub_classinst) {
161 ZFreeNotice(&retnotice);
164 strcpy(list[i].zsub_classinst, ptr);
165 ptr += strlen(ptr)+1;
167 list[i].zsub_recipient = (char *)
168 malloc(strlen(ptr2) + 2);
169 if (!list[i].zsub_recipient) {
170 ZFreeNotice(&retnotice);
173 if (*ptr2 == '@' || *ptr2 == 0) {
174 *list[i].zsub_recipient = '*';
175 strcpy(list[i].zsub_recipient + 1, ptr2);
177 strcpy(list[i].zsub_recipient, ptr2);
179 ptr += strlen(ptr)+1;
181 ZFreeNotice(&retnotice);
184 __subscriptions_list = list;
185 __subscriptions_next = 0;
186 *nsubs = __subscriptions_num;