[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / zephyr / ZSubs.c
blob51284ed3def80b68b8cbf2be91904e6c55af0bf1
1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains source for the ZSubscribeTo, ZUnsubscribeTo, and
3 * ZCancelSubscriptions functions.
5 * Created by: Robert French
7 * $Source$
8 * $Author: warmenhoven $
10 * Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
11 * For copying and distribution information, see the file
12 * "mit-copyright.h".
14 /* $Header$ */
16 #include <internal.h>
18 #ifndef lint
19 static const char rcsid_ZSubscriptions_c[] = "$Id: ZSubs.c 2432 2001-10-03 19:38:28Z warmenhoven $";
20 #endif
22 static Code_t Z_Subscriptions __P((register ZSubscription_t *sublist,
23 int nitems, unsigned int port,
24 char *opcode, int authit));
25 static Code_t subscr_sendoff __P((ZNotice_t *notice, char **lyst, int num,
26 int authit));
28 Code_t ZSubscribeTo(sublist, nitems, port)
29 ZSubscription_t *sublist;
30 int nitems;
31 unsigned int port;
33 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE, 1));
36 Code_t ZSubscribeToSansDefaults(sublist, nitems, port)
37 ZSubscription_t *sublist;
38 int nitems;
39 unsigned int port;
41 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE_NODEFS,
42 1));
45 Code_t ZUnsubscribeTo(sublist, nitems, port)
46 ZSubscription_t *sublist;
47 int nitems;
48 unsigned int port;
50 return (Z_Subscriptions(sublist, nitems, port, CLIENT_UNSUBSCRIBE, 1));
53 Code_t ZCancelSubscriptions(port)
54 unsigned int port;
56 return (Z_Subscriptions((ZSubscription_t *)0, 0, port,
57 CLIENT_CANCELSUB, 0));
61 * This routine must do its own fragmentation. Subscriptions must
62 * not be broken across packet boundaries, or else the server will
63 * mis-interpret them.
66 static Code_t
67 Z_Subscriptions(sublist, nitems, port, opcode, authit)
68 register ZSubscription_t *sublist;
69 int nitems;
70 unsigned int port;
71 char *opcode;
72 int authit;
74 register int i, j;
75 int retval;
76 ZNotice_t notice;
77 char header[Z_MAXHEADERLEN];
78 char **list;
79 int hdrlen;
80 int size_avail = Z_MAXPKTLEN-Z_FRAGFUDGE; /* space avail for data,
81 adjusted below */
82 int size, start, numok;
84 /* nitems = 0 means cancel all subscriptions; still need to allocate a */
85 /* array for one item so we can cancel, however. */
87 list = (char **)malloc((unsigned)((nitems==0)?1:nitems)*3*sizeof(char *));
88 if (!list)
89 return (ENOMEM);
91 (void) memset((char *)&notice, 0, sizeof(notice));
92 notice.z_kind = ACKED;
93 notice.z_port = port;
94 notice.z_class = ZEPHYR_CTL_CLASS;
95 notice.z_class_inst = ZEPHYR_CTL_CLIENT;
96 notice.z_opcode = opcode;
97 notice.z_sender = 0;
98 notice.z_recipient = "";
99 notice.z_default_format = "";
100 notice.z_message_len = 0;
102 /* format the header to figure out how long it is */
103 retval = Z_FormatHeader(&notice, header, sizeof(header), &hdrlen, ZAUTH);
104 if (retval != ZERR_NONE && !authit)
105 retval = Z_FormatHeader(&notice, header, sizeof(header),
106 &hdrlen, ZNOAUTH);
107 if (retval != ZERR_NONE) {
108 free((char *)list);
109 return(retval);
112 /* compute amount of room left */
113 size_avail -= hdrlen;
114 size = size_avail;
116 /* assemble subs into an array of pointers */
117 for (i=0;i<nitems;i++) {
118 list[i*3] = sublist[i].zsub_class;
119 list[i*3+1] = sublist[i].zsub_classinst;
120 if (sublist[i].zsub_recipient && *sublist[i].zsub_recipient &&
121 *sublist[i].zsub_recipient != '*')
122 list[i*3+2] = ZGetSender();
123 else
124 list[i*3+2] = "";
127 start = -1;
128 i = 0;
129 numok = 0;
130 if (!nitems) {
131 /* there aren't really any, but we need to xmit anyway */
132 retval = subscr_sendoff(&notice, list, 0, authit);
133 free((char *)list);
134 return(retval);
136 while(i < nitems) {
137 if (start == -1) {
138 size = size_avail;
139 start = i;
140 numok = 0;
142 if ((j = strlen(list[i*3])
143 + strlen(list[i*3+1])
144 + strlen(list[i*3+2]) + 3) <= size) {
145 /* it will fit in this packet */
146 size -= j;
147 numok++;
148 i++;
149 continue;
151 if (!numok) { /* a single subscription won't
152 fit into one packet */
153 free((char *)list);
154 return(ZERR_FIELDLEN);
156 retval = subscr_sendoff(&notice, &list[start*3], numok, authit);
157 if (retval) {
158 free((char *)list);
159 return(retval);
161 start = -1;
163 if (numok)
164 retval = subscr_sendoff(&notice, &list[start*3], numok, authit);
165 free((char *)list);
166 return(retval);
169 static Code_t
170 subscr_sendoff(notice, lyst, num, authit)
171 ZNotice_t *notice;
172 char **lyst;
173 int num;
174 int authit;
176 register Code_t retval;
177 ZNotice_t retnotice;
179 retval = ZSendList(notice, lyst, num*3, ZAUTH);
180 if (retval != ZERR_NONE && !authit)
181 retval = ZSendList(notice, lyst, num*3, ZNOAUTH);
183 if (retval != ZERR_NONE)
184 return (retval);
185 if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *)0,
186 ZCompareUIDPred, (char *)&notice->z_uid)) !=
187 ZERR_NONE)
188 return (retval);
189 if (retnotice.z_kind == SERVNAK) {
190 ZFreeNotice(&retnotice);
191 return (ZERR_SERVNAK);
193 if (retnotice.z_kind != SERVACK) {
194 ZFreeNotice(&retnotice);
195 return (ZERR_INTERNAL);
197 ZFreeNotice(&retnotice);
198 return (ZERR_NONE);