Mercurial > pidgin
annotate src/protocols/novell/nmuserrecord.c @ 8723:07f670358f6e
[gaim-migrate @ 9478]
update
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Tue, 20 Apr 2004 05:34:41 +0000 |
parents | 046dd8ef2920 |
children | 6663ad2386d9 |
rev | line source |
---|---|
8675 | 1 /* |
2 * nmuserrecord.c | |
3 * | |
4 * Copyright © 2004 Unpublished Work of Novell, Inc. All Rights Reserved. | |
5 * | |
6 * THIS WORK IS AN UNPUBLISHED WORK OF NOVELL, INC. NO PART OF THIS WORK MAY BE | |
7 * USED, PRACTICED, PERFORMED, COPIED, DISTRIBUTED, REVISED, MODIFIED, | |
8 * TRANSLATED, ABRIDGED, CONDENSED, EXPANDED, COLLECTED, COMPILED, LINKED, | |
9 * RECAST, TRANSFORMED OR ADAPTED WITHOUT THE PRIOR WRITTEN CONSENT OF NOVELL, | |
10 * INC. ANY USE OR EXPLOITATION OF THIS WORK WITHOUT AUTHORIZATION COULD SUBJECT | |
11 * THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY. | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
12 * |
8675 | 13 * AS BETWEEN [GAIM] AND NOVELL, NOVELL GRANTS [GAIM] THE RIGHT TO REPUBLISH |
14 * THIS WORK UNDER THE GPL (GNU GENERAL PUBLIC LICENSE) WITH ALL RIGHTS AND | |
15 * LICENSES THEREUNDER. IF YOU HAVE RECEIVED THIS WORK DIRECTLY OR INDIRECTLY | |
16 * FROM [GAIM] AS PART OF SUCH A REPUBLICATION, YOU HAVE ALL RIGHTS AND LICENSES | |
17 * GRANTED BY [GAIM] UNDER THE GPL. IN CONNECTION WITH SUCH A REPUBLICATION, IF | |
18 * ANYTHING IN THIS NOTICE CONFLICTS WITH THE TERMS OF THE GPL, SUCH TERMS | |
19 * PREVAIL. | |
20 * | |
21 */ | |
22 | |
23 #include <glib.h> | |
24 #include <string.h> | |
25 #include "nmuserrecord.h" | |
26 #include "nmfield.h" | |
27 #include "nmuser.h" | |
28 | |
29 struct _NMUserRecord | |
30 { | |
31 NMSTATUS_T status; | |
32 char *status_text; | |
33 char *dn; | |
34 char *cn; | |
35 char *display_id; | |
36 char *fname; | |
37 char *lname; | |
38 char *full_name; | |
39 NMField *fields; | |
40 gboolean auth_attr; | |
41 gpointer data; | |
42 int ref_count; | |
43 }; | |
44 | |
45 struct _NMProperty | |
46 { | |
47 char *tag; | |
48 char *value; | |
49 }; | |
50 | |
51 static int count = 0; | |
52 | |
53 /* API functions */ | |
54 | |
55 NMUserRecord * | |
56 nm_create_user_record() | |
57 { | |
58 NMUserRecord *user_record = g_new0(NMUserRecord, 1); | |
59 | |
60 user_record->ref_count = 1; | |
61 | |
62 gaim_debug(GAIM_DEBUG_INFO, "novell", "Creating user_record, total=%d\n", | |
63 count++); | |
64 | |
65 return user_record; | |
66 } | |
67 | |
68 static char * | |
69 _get_attribute_value(NMField *field) | |
70 { | |
71 char *value = NULL; | |
72 | |
73 if (field->value == 0) | |
74 return NULL; | |
75 | |
76 if (field->type == NMFIELD_TYPE_UTF8 || field->type == NMFIELD_TYPE_DN) { | |
77 | |
78 value = (char *)field->value; | |
79 | |
80 } else if (field->type == NMFIELD_TYPE_MV) { | |
81 | |
82 /* Need to handle multi-valued returns, for now | |
83 * just pick the first value and return it | |
84 */ | |
85 NMField *tmp = (NMField *)field->value; | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
86 if ((tmp != NULL) && |
8675 | 87 ((tmp->type == NMFIELD_TYPE_UTF8) || |
88 (tmp->type == NMFIELD_TYPE_DN))) { | |
89 | |
90 value = (char *)tmp->value; | |
91 | |
92 } else { | |
93 return NULL; | |
94 } | |
95 | |
96 } else { | |
97 return NULL; | |
98 } | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
99 |
8675 | 100 return g_strdup(value); |
101 } | |
102 /* | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
103 * This creates a user_record for the reference list the |
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
104 * field array that is passed in should be a |
8675 | 105 * NM_A_FA_USER_DETAILS array. |
106 */ | |
107 NMUserRecord * | |
108 nm_create_user_record_from_fields(NMField * details) | |
109 { | |
110 NMUserRecord *user_record; | |
111 NMField *field, *fields = details; | |
112 | |
113 if (details == NULL) { | |
114 return NULL; | |
115 } | |
116 | |
117 if (details->type == NMFIELD_TYPE_ARRAY) { | |
118 if (details->value == 0) | |
119 return NULL; | |
120 fields = (NMField *) details->value; | |
121 } | |
122 | |
123 user_record = nm_create_user_record(); | |
124 | |
125 if ((field = nm_locate_field(NM_A_SZ_AUTH_ATTRIBUTE, fields))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
126 |
8675 | 127 if (field->value) { |
128 user_record->display_id = _get_attribute_value(field); | |
129 user_record->auth_attr = TRUE; | |
130 } | |
131 } | |
132 | |
133 if ((field = nm_locate_field(NM_A_SZ_DN, fields))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
134 |
8675 | 135 if (field->value) { |
136 user_record->dn = _get_attribute_value(field); | |
137 } | |
138 } | |
139 | |
140 if ((field = nm_locate_field("CN", fields))) { | |
141 | |
142 if (field->value) { | |
143 user_record->cn = _get_attribute_value(field); | |
144 } | |
145 } | |
146 | |
147 if ((field = nm_locate_field("Given Name", fields))) { | |
148 | |
149 if (field->value) { | |
150 user_record->fname = _get_attribute_value(field); | |
151 } | |
152 } | |
153 | |
154 if ((field = nm_locate_field("Surname", fields))) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
155 |
8675 | 156 if (field->value) { |
157 user_record->lname = _get_attribute_value(field); | |
158 } | |
159 } | |
160 | |
161 if ((field = nm_locate_field("Full Name", fields))) { | |
162 | |
163 if (field->value) { | |
164 user_record->full_name = _get_attribute_value(field); | |
165 } | |
166 } | |
167 | |
168 if ((field = nm_locate_field(NM_A_SZ_STATUS, fields))) { | |
169 | |
170 if (field->value) | |
171 user_record->status = atoi((char *) field->value); | |
172 | |
173 } | |
174 | |
175 if ((field = nm_locate_field(NM_A_SZ_MESSAGE_BODY, fields))) { | |
176 | |
177 if (field->value) | |
178 user_record->status_text = g_strdup((char *) field->value); | |
179 | |
180 } | |
181 | |
182 user_record->fields = nm_copy_field_array(fields); | |
183 | |
184 return user_record; | |
185 } | |
186 | |
187 void | |
188 nm_user_record_copy(NMUserRecord * dest, NMUserRecord * src) | |
189 { | |
190 if (dest == NULL || src == NULL) | |
191 return; | |
192 | |
193 dest->status = src->status; | |
194 | |
195 /* Copy status text */ | |
196 if (dest->status_text) { | |
197 g_free(dest->status_text); | |
198 dest->status_text = NULL; | |
199 } | |
200 | |
201 if (src->status_text) | |
202 dest->status_text = g_strdup(src->status_text); | |
203 | |
204 /* Copy DN */ | |
205 if (dest->dn) { | |
206 g_free(dest->dn); | |
207 dest->dn = NULL; | |
208 } | |
209 | |
210 if (src->dn) | |
211 dest->dn = g_strdup(src->dn); | |
212 | |
213 /* Copy CN */ | |
214 if (dest->cn) { | |
215 g_free(dest->cn); | |
216 dest->cn = NULL; | |
217 } | |
218 | |
219 if (src->cn) | |
220 dest->cn = g_strdup(src->cn); | |
221 | |
222 /* Copy display id */ | |
223 if (dest->display_id) { | |
224 g_free(dest->display_id); | |
225 dest->display_id = NULL; | |
226 } | |
227 | |
228 if (src->display_id) | |
229 dest->display_id = g_strdup(src->display_id); | |
230 | |
231 /* Copy first name */ | |
232 if (dest->fname) { | |
233 g_free(dest->fname); | |
234 dest->fname = NULL; | |
235 } | |
236 | |
237 if (src->fname) | |
238 dest->fname = g_strdup(src->fname); | |
239 | |
240 /* Copy last name */ | |
241 if (dest->lname) { | |
242 g_free(dest->lname); | |
243 dest->lname = NULL; | |
244 } | |
245 | |
246 if (src->lname) | |
247 dest->lname = g_strdup(src->lname); | |
248 | |
249 /* Copy full name */ | |
250 if (dest->full_name) { | |
251 g_free(dest->full_name); | |
252 dest->full_name = NULL; | |
253 } | |
254 | |
255 if (src->full_name) | |
256 dest->full_name = g_strdup(src->full_name); | |
257 | |
258 /* Copy fields */ | |
259 if (src->fields) { | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
260 |
8675 | 261 if (dest->fields) { |
262 nm_free_fields(&dest->fields); | |
263 } | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
264 |
8675 | 265 dest->fields = nm_copy_field_array(src->fields); |
266 } | |
267 | |
268 /* Copy data */ | |
269 dest->data = src->data; | |
270 } | |
271 | |
272 void | |
273 nm_user_record_add_ref(NMUserRecord * user_record) | |
274 { | |
275 if (user_record) | |
276 user_record->ref_count++; | |
277 } | |
278 | |
279 void | |
280 nm_release_user_record(NMUserRecord * user_record) | |
281 { | |
282 if (--(user_record->ref_count) == 0) { | |
283 | |
284 gaim_debug(GAIM_DEBUG_INFO, "novell", | |
285 "Releasing user_record, total=%d\n", --count); | |
286 | |
287 if (user_record->dn) { | |
288 g_free(user_record->dn); | |
289 } | |
290 | |
291 if (user_record->cn) { | |
292 g_free(user_record->cn); | |
293 } | |
294 | |
295 if (user_record->display_id) { | |
296 g_free(user_record->display_id); | |
297 } | |
298 | |
299 if (user_record->fname) { | |
300 g_free(user_record->fname); | |
301 } | |
302 | |
303 if (user_record->lname) { | |
304 g_free(user_record->lname); | |
305 } | |
306 | |
307 if (user_record->full_name) { | |
308 g_free(user_record->full_name); | |
309 } | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
310 |
8675 | 311 if (user_record->status_text) { |
312 g_free(user_record->status_text); | |
313 } | |
314 | |
315 nm_free_fields(&user_record->fields); | |
316 | |
317 g_free(user_record); | |
318 } | |
319 } | |
320 | |
321 /* UserRecord API */ | |
322 | |
323 NMSTATUS_T | |
324 nm_user_record_get_status(NMUserRecord * user_record) | |
325 { | |
326 if (user_record == NULL) | |
327 return (NMSTATUS_T) - 1; | |
328 | |
329 return user_record->status; | |
330 | |
331 } | |
332 | |
333 const char * | |
334 nm_user_record_get_status_text(NMUserRecord * user_record) | |
335 { | |
336 if (user_record == NULL) | |
337 return NULL; | |
338 | |
339 return user_record->status_text; | |
340 } | |
341 | |
342 void | |
343 nm_user_record_set_dn(NMUserRecord * user_record, const char *dn) | |
344 { | |
345 if (user_record != NULL && dn != NULL) { | |
346 if (user_record->dn) | |
347 g_free(user_record->dn); | |
348 | |
349 user_record->dn = g_strdup(dn); | |
350 } | |
351 } | |
352 | |
353 const char * | |
354 nm_user_record_get_dn(NMUserRecord * user_record) | |
355 { | |
356 if (user_record == NULL) | |
357 return NULL; | |
358 | |
359 return user_record->dn; | |
360 } | |
361 | |
362 void | |
363 nm_user_record_set_userid(NMUserRecord * user_record, const char *userid) | |
364 { | |
365 if (user_record != NULL && userid != NULL) { | |
366 if (user_record->cn) | |
367 g_free(user_record->cn); | |
368 | |
369 user_record->cn = g_strdup(userid); | |
370 } | |
371 } | |
372 | |
373 const char * | |
374 nm_user_record_get_userid(NMUserRecord * user_record) | |
375 { | |
376 if (user_record == NULL) | |
377 return NULL; | |
378 | |
379 return user_record->cn; | |
380 } | |
381 | |
382 void | |
383 nm_user_record_set_display_id(NMUserRecord * user_record, const char *display_id) | |
384 { | |
385 if (user_record != NULL && display_id != NULL) { | |
386 if (user_record->display_id) | |
387 g_free(user_record->display_id); | |
388 | |
389 user_record->display_id = g_strdup(display_id); | |
390 } | |
391 } | |
392 | |
393 const char * | |
394 nm_user_record_get_display_id(NMUserRecord * user_record) | |
395 { | |
396 if (user_record == NULL) | |
397 return NULL; | |
398 | |
399 if (user_record->display_id == NULL) { | |
400 user_record->display_id = nm_typed_to_dotted(user_record->dn); | |
401 } | |
402 | |
403 return user_record->display_id; | |
404 } | |
405 | |
406 const char * | |
407 nm_user_record_get_full_name(NMUserRecord * user_record) | |
408 { | |
409 if (user_record == NULL) | |
410 return NULL; | |
411 | |
412 if (user_record->full_name == NULL) { | |
413 if (user_record->fname && user_record->lname) { | |
414 user_record->full_name = g_strdup_printf("%s %s", | |
415 user_record->fname, | |
416 user_record->lname); | |
417 | |
418 } | |
419 } | |
420 | |
421 return user_record->full_name; | |
422 } | |
423 | |
424 const char * | |
425 nm_user_record_get_first_name(NMUserRecord * user_record) | |
426 { | |
427 if (user_record == NULL) | |
428 return NULL; | |
429 | |
430 return user_record->fname; | |
431 | |
432 } | |
433 | |
434 const char * | |
435 nm_user_record_get_last_name(NMUserRecord * user_record) | |
436 { | |
437 if (user_record == NULL) | |
438 return NULL; | |
439 | |
440 return user_record->lname; | |
441 } | |
442 | |
443 gpointer | |
444 nm_user_record_get_data(NMUserRecord * user_record) | |
445 { | |
446 if (user_record == NULL) | |
447 return NULL; | |
448 | |
449 return user_record->data; | |
450 } | |
451 | |
452 void | |
453 nm_user_record_set_data(NMUserRecord * user_record, gpointer data) | |
454 { | |
455 if (user_record == NULL) | |
456 return; | |
457 | |
458 user_record->data = data; | |
459 } | |
460 | |
461 void | |
462 nm_user_record_set_status(NMUserRecord * user_record, | |
463 int status, const char *text) | |
464 { | |
465 if (user_record == NULL) | |
466 return; | |
467 | |
468 user_record->status = status; | |
469 | |
470 if (user_record->status_text) { | |
471 g_free(user_record->status_text); | |
472 user_record->status_text = NULL; | |
473 } | |
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
474 |
8675 | 475 if (text) |
476 user_record->status_text = g_strdup(text); | |
477 } | |
478 | |
479 gboolean | |
480 nm_user_record_get_auth_attr(NMUserRecord *user_record) | |
481 { | |
482 if (user_record == NULL) | |
483 return FALSE; | |
484 | |
485 return user_record->auth_attr; | |
486 } | |
487 | |
488 int | |
489 nm_user_record_get_property_count(NMUserRecord * user_record) | |
490 { | |
491 NMField *locate, *fields; | |
492 | |
493 int count = 0; | |
494 | |
495 if (user_record && user_record->fields) { | |
496 locate = nm_locate_field(NM_A_FA_INFO_DISPLAY_ARRAY, | |
497 (NMField *) user_record->fields); | |
498 if (locate && (fields = (NMField *) (locate->value))) { | |
499 count = (int) nm_count_fields(fields); | |
500 } | |
501 } | |
502 return count; | |
503 } | |
504 | |
505 NMProperty * | |
506 nm_user_record_get_property(NMUserRecord * user_record, int index) | |
507 { | |
508 NMProperty *property = NULL; | |
509 NMField *field = NULL, *fields, *locate; | |
510 | |
511 if (user_record && user_record->fields) { | |
512 locate = nm_locate_field(NM_A_FA_INFO_DISPLAY_ARRAY, | |
513 (NMField *) user_record->fields); | |
514 if (locate && (fields = (NMField *) (locate->value))) { | |
515 int max = nm_count_fields(fields); | |
516 | |
517 if (index < max) { | |
518 if (user_record) { | |
519 field = &fields[index]; | |
520 if (field && field->tag && field->value) { | |
521 property = g_new0(NMProperty, 1); | |
522 property->tag = g_strdup(field->tag); | |
523 property->value = _get_attribute_value(field); | |
524 } | |
525 } | |
526 } | |
527 } | |
528 } | |
529 | |
530 return property; | |
531 } | |
532 | |
533 void | |
534 nm_release_property(NMProperty * property) | |
535 { | |
536 if (property) { | |
537 if (property->tag) | |
538 g_free(property->tag); | |
539 | |
540 if (property->value) | |
541 g_free(property->value); | |
542 | |
543 g_free(property); | |
544 } | |
545 } | |
546 | |
547 const char * | |
548 nm_property_get_tag(NMProperty * property) | |
549 { | |
550 if (property) | |
551 return property->tag; | |
552 else | |
553 return NULL; | |
554 } | |
555 | |
556 const char * | |
557 nm_property_get_value(NMProperty * property) | |
558 { | |
559 if (property) | |
560 return property->value; | |
561 else | |
562 return NULL; | |
563 } |