[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / zephyr / ZAsyncLocate.c
blob1dab0f3166af18c5a2d1dea9536dab879f3be853
1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains source for asynchronous location functions.
4 * Created by: Marc Horowitz
6 * $Source$
7 * $Author: warmenhoven $
9 * Copyright (c) 1990,1991 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
11 * "mit-copyright.h".
13 /* $Header$ */
15 #include <internal.h>
17 #ifndef lint
18 static const char rcsid_ZAsyncLocate_c[] = "$Id: ZAsyncLocate.c 2096 2001-07-31 01:00:39Z warmenhoven $";
19 #endif
21 Code_t ZRequestLocations(user, zald, kind, auth)
22 char *user;
23 register ZAsyncLocateData_t *zald;
24 ZNotice_Kind_t kind; /* UNSAFE, UNACKED, or ACKED */
25 Z_AuthProc auth;
27 int retval;
28 ZNotice_t notice;
30 if (ZGetFD() < 0)
31 if ((retval = ZOpenPort((u_short *)0)) != ZERR_NONE)
32 return (retval);
34 (void) memset((char *)&notice, 0, sizeof(notice));
35 notice.z_kind = kind;
36 notice.z_port = __Zephyr_port;
37 notice.z_class = LOCATE_CLASS;
38 notice.z_class_inst = user;
39 notice.z_opcode = LOCATE_LOCATE;
40 notice.z_sender = 0;
41 notice.z_recipient = "";
42 notice.z_default_format = "";
43 notice.z_message_len = 0;
45 if ((retval = ZSendNotice(&notice, auth)) != ZERR_NONE)
46 return(retval);
48 if ((zald->user = (char *) malloc(strlen(user)+1)) == NULL) {
49 return(ENOMEM);
51 if ((zald->version = (char *) malloc(strlen(notice.z_version)+1)) == NULL) {
52 free(zald->user);
53 return(ENOMEM);
55 zald->uid = notice.z_multiuid;
56 strcpy(zald->user,user);
57 strcpy(zald->version,notice.z_version);
59 return(ZERR_NONE);
62 Code_t ZParseLocations(notice,zald,nlocs,user)
63 register ZNotice_t *notice;
64 register ZAsyncLocateData_t *zald;
65 int *nlocs;
66 char **user;
68 char *ptr, *end;
69 int i;
71 ZFlushLocations(); /* This never fails (this function is part of the
72 library, so it is allowed to know this). */
74 /* non-matching protocol version numbers means the
75 server is probably an older version--must punt */
77 if (zald && strcmp(notice->z_version, zald->version))
78 return(ZERR_VERS);
80 if (notice->z_kind == SERVNAK)
81 return (ZERR_SERVNAK);
83 /* flag ACKs as special */
84 if (notice->z_kind == SERVACK &&
85 !strcmp(notice->z_opcode, LOCATE_LOCATE)) {
86 *nlocs = -1;
87 return(ZERR_NONE);
90 if (notice->z_kind != ACKED)
91 return (ZERR_INTERNAL);
93 end = notice->z_message+notice->z_message_len;
95 __locate_num = 0;
97 for (ptr=notice->z_message;ptr<end;ptr++)
98 if (!*ptr)
99 __locate_num++;
101 __locate_num /= 3;
103 if (__locate_num)
105 __locate_list = (ZLocations_t *)malloc((unsigned)__locate_num*
106 sizeof(ZLocations_t));
107 if (!__locate_list)
108 return (ENOMEM);
109 } else {
110 __locate_list = 0;
113 for (ptr=notice->z_message, i=0; i<__locate_num; i++) {
114 unsigned int len;
116 len = strlen (ptr) + 1;
117 __locate_list[i].host = (char *) malloc(len);
118 if (!__locate_list[i].host)
119 return (ENOMEM);
120 (void) strcpy(__locate_list[i].host, ptr);
121 ptr += len;
123 len = strlen (ptr) + 1;
124 __locate_list[i].time = (char *) malloc(len);
125 if (!__locate_list[i].time)
126 return (ENOMEM);
127 (void) strcpy(__locate_list[i].time, ptr);
128 ptr += len;
130 len = strlen (ptr) + 1;
131 __locate_list[i].tty = (char *) malloc(len);
132 if (!__locate_list[i].tty)
133 return (ENOMEM);
134 (void) strcpy(__locate_list[i].tty, ptr);
135 ptr += len;
138 __locate_next = 0;
139 *nlocs = __locate_num;
140 if (user) {
141 if (zald) {
142 if ((*user = (char *) malloc(strlen(zald->user)+1)) == NULL)
143 return(ENOMEM);
144 strcpy(*user,zald->user);
145 } else {
146 if ((*user = (char *) malloc(strlen(notice->z_class_inst)+1)) == NULL)
147 return(ENOMEM);
148 strcpy(*user,notice->z_class_inst);
151 return (ZERR_NONE);
154 int ZCompareALDPred(notice, zald)
155 ZNotice_t *notice;
156 void *zald;
158 return(ZCompareUID(&(notice->z_multiuid),
159 &(((ZAsyncLocateData_t *) zald)->uid)));
162 void ZFreeALD(zald)
163 register ZAsyncLocateData_t *zald;
165 if (!zald) return;
167 if (zald->user) free(zald->user);
168 if (zald->version) free(zald->version);
169 (void) memset(zald, 0, sizeof(*zald));