Use GSList functions instead of manual iterations
[pidgin-git.git] / libpurple / protocols / novell / nmuser.c
blobe6de078b50a3f670ae6f56f014b36bc941c70b19
1 /*
2 * nmuser.c
4 * Copyright (c) 2004 Novell, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include <string.h>
23 #include "nmfield.h"
24 #include "nmuser.h"
25 #include "nmconn.h"
26 #include "nmcontact.h"
27 #include "nmuserrecord.h"
28 #include "util.h"
30 /* This is the template that we wrap outgoing messages in, since the other
31 * GW Messenger clients expect messages to be in RTF.
33 #define RTF_TEMPLATE "{\\rtf1\\ansi\n"\
34 "{\\fonttbl{\\f0\\fnil Unknown;}}\n"\
35 "{\\colortbl ;\\red0\\green0\\blue0;}\n"\
36 "\\uc1\\cf1\\f0\\fs24 %s\\par\n}"
37 #define NM_MAX_MESSAGE_SIZE 2048
39 static NMERR_T nm_process_response(NMUser * user);
40 static void _update_contact_list(NMUser * user, NMField * fields);
41 static void _handle_multiple_get_details_login_cb(NMUser * user, NMERR_T ret_code,
42 gpointer resp_data, gpointer user_data);
43 static char * nm_rtfize_text(char *text);
45 /**
46 * See header for comments on on "public" functions
49 NMUser *
50 nm_initialize_user(const char *name, const char *server_addr,
51 int port, gpointer data, nm_event_cb event_callback)
53 NMUser *user;
54 if (name == NULL || server_addr == NULL || event_callback == NULL)
55 return NULL;
57 user = g_new0(NMUser, 1);
61 user->contacts =
62 g_hash_table_new_full(g_str_hash, nm_utf8_str_equal,
63 g_free, (GDestroyNotify) nm_release_contact);
65 user->user_records =
66 g_hash_table_new_full(g_str_hash, nm_utf8_str_equal, g_free,
67 (GDestroyNotify) nm_release_user_record);
69 user->display_id_to_dn = g_hash_table_new_full(g_str_hash, nm_utf8_str_equal,
70 g_free, g_free);
72 user->name = g_strdup(name);
73 user->conn = nm_create_conn(server_addr, port);
74 user->conn->addr = g_strdup(server_addr);
75 user->conn->port = port;
76 user->evt_callback = event_callback;
77 user->client_data = data;
79 return user;
83 void
84 nm_deinitialize_user(NMUser * user)
86 nm_release_conn(user->conn);
88 if (user->contacts) {
89 g_hash_table_destroy(user->contacts);
92 if (user->user_records) {
93 g_hash_table_destroy(user->user_records);
96 if (user->display_id_to_dn) {
97 g_hash_table_destroy(user->display_id_to_dn);
100 g_free(user->name);
102 if (user->user_record) {
103 nm_release_user_record(user->user_record);
106 nm_conference_list_free(user);
107 nm_destroy_contact_list(user);
109 g_free(user);
112 NMERR_T
113 nm_send_login(NMUser * user, const char *pwd, const char *my_addr,
114 const char *user_agent, nm_response_cb callback, gpointer data)
116 NMERR_T rc = NM_OK;
117 NMField *fields = NULL;
119 if (user == NULL || pwd == NULL || user_agent == NULL) {
120 return NMERR_BAD_PARM;
123 fields = nm_field_add_pointer(fields, NM_A_SZ_USERID, 0, NMFIELD_METHOD_VALID, 0,
124 g_strdup(user->name), NMFIELD_TYPE_UTF8);
126 fields = nm_field_add_pointer(fields, NM_A_SZ_CREDENTIALS, 0, NMFIELD_METHOD_VALID, 0,
127 g_strdup(pwd), NMFIELD_TYPE_UTF8);
129 fields = nm_field_add_pointer(fields, NM_A_SZ_USER_AGENT, 0, NMFIELD_METHOD_VALID, 0,
130 g_strdup(user_agent), NMFIELD_TYPE_UTF8);
132 fields = nm_field_add_number(fields, NM_A_UD_BUILD, 0, NMFIELD_METHOD_VALID, 0,
133 NM_PROTOCOL_VERSION, NMFIELD_TYPE_UDWORD);
134 if (my_addr) {
135 fields = nm_field_add_pointer(fields, NM_A_IP_ADDRESS, 0, NMFIELD_METHOD_VALID, 0,
136 g_strdup(my_addr), NMFIELD_TYPE_UTF8);
139 /* Send the login */
140 rc = nm_send_request(user->conn, "login", fields, callback, data, NULL);
142 nm_free_fields(&fields);
143 return rc;
146 NMERR_T
147 nm_send_set_status(NMUser * user, int status, const char *text,
148 const char *auto_resp, nm_response_cb callback, gpointer data)
150 NMERR_T rc = NM_OK;
151 NMField *fields = NULL;
153 if (user == NULL)
154 return NMERR_BAD_PARM;
156 /* Add the status */
157 fields = nm_field_add_pointer(fields, NM_A_SZ_STATUS, 0, NMFIELD_METHOD_VALID, 0,
158 g_strdup_printf("%d", status), NMFIELD_TYPE_UTF8);
160 /* Add the status text and auto reply text if there is any */
161 if (text) {
162 fields = nm_field_add_pointer(fields, NM_A_SZ_STATUS_TEXT, 0,
163 NMFIELD_METHOD_VALID, 0, g_strdup(text),
164 NMFIELD_TYPE_UTF8);
167 if (auto_resp) {
168 fields = nm_field_add_pointer(fields, NM_A_SZ_MESSAGE_BODY, 0,
169 NMFIELD_METHOD_VALID, 0, g_strdup(auto_resp),
170 NMFIELD_TYPE_UTF8);
173 rc = nm_send_request(user->conn, "setstatus", fields, callback, data, NULL);
175 nm_free_fields(&fields);
176 return rc;
179 NMERR_T
180 nm_send_multiple_get_details(NMUser * user, GSList *names,
181 nm_response_cb callback, gpointer data)
183 NMERR_T rc = NM_OK;
184 NMField *fields = NULL;
185 GSList *node;
187 if (user == NULL || names == NULL)
188 return NMERR_BAD_PARM;
190 /* Add in DN or display id */
191 for (node = names; node; node = node->next) {
192 fields = nm_field_add_pointer(fields, NM_A_SZ_USERID, 0, NMFIELD_METHOD_VALID, 0,
193 g_strdup(node->data), NMFIELD_TYPE_UTF8);
196 rc = nm_send_request(user->conn, "getdetails", fields, callback, data, NULL);
198 nm_free_fields(&fields);
199 return rc;
202 NMERR_T
203 nm_send_get_details(NMUser * user, const char *name,
204 nm_response_cb callback, gpointer data)
206 NMERR_T rc = NM_OK;
207 NMField *fields = NULL;
209 if (user == NULL || name == NULL)
210 return NMERR_BAD_PARM;
212 /* Add in DN or display id */
213 if (strstr("=", name)) {
214 fields = nm_field_add_pointer(fields, NM_A_SZ_DN, 0, NMFIELD_METHOD_VALID, 0,
215 g_strdup(name), NMFIELD_TYPE_DN);
216 } else {
218 const char *dn = nm_lookup_dn(user, name);
219 if (dn) {
220 fields = nm_field_add_pointer(fields, NM_A_SZ_DN, 0, NMFIELD_METHOD_VALID, 0,
221 g_strdup(name), NMFIELD_TYPE_DN);
222 } else {
223 fields =
224 nm_field_add_pointer(fields, NM_A_SZ_USERID, 0, NMFIELD_METHOD_VALID, 0,
225 g_strdup(name), NMFIELD_TYPE_UTF8);
230 rc = nm_send_request(user->conn, "getdetails", fields, callback, data, NULL);
232 nm_free_fields(&fields);
233 return rc;
236 NMERR_T
237 nm_send_create_conference(NMUser * user, NMConference * conference,
238 nm_response_cb callback, gpointer data)
240 NMERR_T rc = NM_OK;
241 NMField *fields = NULL;
242 NMField *tmp = NULL;
243 NMField *field = NULL;
244 NMRequest *req = NULL;
245 int count, i;
247 if (user == NULL || conference == NULL)
248 return NMERR_BAD_PARM;
250 /* Add in a blank guid */
251 tmp = nm_field_add_pointer(tmp, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0,
252 g_strdup(BLANK_GUID), NMFIELD_TYPE_UTF8);
254 fields = nm_field_add_pointer(fields, NM_A_FA_CONVERSATION, 0,
255 NMFIELD_METHOD_VALID, 0, tmp,
256 NMFIELD_TYPE_ARRAY);
257 tmp = NULL;
260 /* Add participants in */
261 count = nm_conference_get_participant_count(conference);
262 for (i = 0; i < count; i++) {
263 NMUserRecord *user_record = nm_conference_get_participant(conference, i);
265 if (user_record) {
266 fields = nm_field_add_pointer(fields, NM_A_SZ_DN,
267 0, NMFIELD_METHOD_VALID, 0,
268 g_strdup(nm_user_record_get_dn(user_record)),
269 NMFIELD_TYPE_DN);
273 /* Add our user in */
274 field = nm_locate_field(NM_A_SZ_DN, user->fields);
275 if (field) {
276 fields = nm_field_add_pointer(fields, NM_A_SZ_DN,
277 0, NMFIELD_METHOD_VALID, 0,
278 g_strdup((char *) field->ptr_value),
279 NMFIELD_TYPE_DN);
282 rc = nm_send_request(user->conn, "createconf", fields, callback, data, &req);
283 if (rc == NM_OK && req) {
284 nm_conference_add_ref(conference);
285 nm_request_set_data(req, conference);
288 if (req)
289 nm_release_request(req);
291 nm_free_fields(&fields);
292 return rc;
295 NMERR_T
296 nm_send_leave_conference(NMUser * user, NMConference * conference,
297 nm_response_cb callback, gpointer data)
299 NMERR_T rc = NM_OK;
300 NMField *fields = NULL;
301 NMField *tmp = NULL;
302 NMRequest *req = NULL;
304 if (user == NULL || conference == NULL)
305 return NMERR_BAD_PARM;
307 /* Add in the conference guid */
308 tmp = nm_field_add_pointer(tmp, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0,
309 g_strdup(nm_conference_get_guid(conference)),
310 NMFIELD_TYPE_UTF8);
312 fields = nm_field_add_pointer(fields, NM_A_FA_CONVERSATION, 0,
313 NMFIELD_METHOD_VALID, 0, tmp,
314 NMFIELD_TYPE_ARRAY);
315 tmp = NULL;
317 /* Send the request to the server */
318 rc = nm_send_request(user->conn, "leaveconf", fields, callback, data, &req);
319 if (rc == NM_OK && req)
320 nm_request_set_data(req, conference);
322 if (req)
323 nm_release_request(req);
325 nm_free_fields(&fields);
326 return rc;
329 NMERR_T
330 nm_send_join_conference(NMUser * user, NMConference * conference,
331 nm_response_cb callback, gpointer data)
333 NMERR_T rc = NM_OK;
334 NMField *fields = NULL, *tmp = NULL;
335 NMRequest *req = NULL;
337 if (user == NULL || conference == NULL)
338 return NMERR_BAD_PARM;
340 /* Add in the conference guid */
341 tmp = nm_field_add_pointer(tmp, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0,
342 g_strdup(nm_conference_get_guid(conference)),
343 NMFIELD_TYPE_UTF8);
345 fields = nm_field_add_pointer(fields, NM_A_FA_CONVERSATION, 0,
346 NMFIELD_METHOD_VALID, 0, tmp,
347 NMFIELD_TYPE_ARRAY);
348 tmp = NULL;
350 /* Send the request to the server */
351 rc = nm_send_request(user->conn, "joinconf", fields, callback, data, &req);
352 if (rc == NM_OK && req)
353 nm_request_set_data(req, conference);
355 if (req)
356 nm_release_request(req);
358 nm_free_fields(&fields);
359 return rc;
362 NMERR_T
363 nm_send_reject_conference(NMUser * user, NMConference * conference,
364 nm_response_cb callback, gpointer data)
366 NMERR_T rc = NM_OK;
367 NMField *fields = NULL;
368 NMField *tmp = NULL;
369 NMRequest *req = NULL;
371 if (user == NULL || conference == NULL)
372 return NMERR_BAD_PARM;
374 /* Add in the conference guid */
375 tmp = nm_field_add_pointer(tmp, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0,
376 g_strdup(nm_conference_get_guid(conference)),
377 NMFIELD_TYPE_UTF8);
379 fields = nm_field_add_pointer(fields, NM_A_FA_CONVERSATION, 0,
380 NMFIELD_METHOD_VALID, 0, tmp,
381 NMFIELD_TYPE_ARRAY);
382 tmp = NULL;
384 /* Send the request to the server */
385 rc = nm_send_request(user->conn, "rejectconf", fields, callback, data, &req);
386 if (rc == NM_OK && req)
387 nm_request_set_data(req, conference);
389 if (req)
390 nm_release_request(req);
392 nm_free_fields(&fields);
393 return rc;
396 NMERR_T
397 nm_send_conference_invite(NMUser *user, NMConference *conference, NMUserRecord *user_record,
398 const char *message, nm_response_cb callback, gpointer data)
400 NMERR_T rc = NM_OK;
401 NMField *fields = NULL;
402 NMField *tmp = NULL;
403 NMRequest *req = NULL;
405 if (user == NULL || conference == NULL || user_record == NULL)
406 return NMERR_BAD_PARM;
408 /* Add in the conference guid */
409 tmp = nm_field_add_pointer(tmp, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0,
410 g_strdup(nm_conference_get_guid(conference)),
411 NMFIELD_TYPE_UTF8);
413 fields = nm_field_add_pointer(fields, NM_A_FA_CONVERSATION, 0,
414 NMFIELD_METHOD_VALID, 0, tmp,
415 NMFIELD_TYPE_ARRAY);
416 tmp = NULL;
418 /* Add in DN of user to invite */
419 fields = nm_field_add_pointer(fields, NM_A_SZ_DN, 0, NMFIELD_METHOD_VALID, 0,
420 g_strdup(nm_user_record_get_dn(user_record)),
421 NMFIELD_TYPE_DN);
423 /* Add the invite message if there is one */
424 if (message)
425 fields = nm_field_add_pointer(fields, NM_A_SZ_MESSAGE_BODY, 0, NMFIELD_METHOD_VALID, 0,
426 g_strdup(message), NMFIELD_TYPE_UTF8);
428 /* Send the request to the server */
429 rc = nm_send_request(user->conn, "sendinvite", fields, callback, data, &req);
430 if (rc == NM_OK && req)
431 nm_request_set_data(req, conference);
433 if (req)
434 nm_release_request(req);
436 nm_free_fields(&fields);
437 return rc;
440 NMERR_T
441 nm_send_message(NMUser * user, NMMessage * message, nm_response_cb callback)
443 NMERR_T rc = NM_OK;
444 char *text, *rtfized;
445 NMField *fields = NULL, *tmp = NULL;
446 NMConference *conf;
447 NMUserRecord *user_record;
448 int count, i;
450 if (user == NULL || message == NULL) {
451 return NMERR_BAD_PARM;
454 conf = nm_message_get_conference(message);
455 if (!nm_conference_is_instantiated(conf)) {
456 rc = NMERR_CONFERENCE_NOT_INSTANTIATED;
457 } else {
459 tmp = nm_field_add_pointer(tmp, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0,
460 g_strdup(nm_conference_get_guid(conf)),
461 NMFIELD_TYPE_UTF8);
463 fields =
464 nm_field_add_pointer(fields, NM_A_FA_CONVERSATION, 0, NMFIELD_METHOD_VALID, 0,
465 tmp, NMFIELD_TYPE_ARRAY);
466 tmp = NULL;
468 /* Add RTF and plain text versions of the message */
469 text = g_strdup(nm_message_get_text(message));
471 /* Truncate if necessary */
472 if (strlen(text) > NM_MAX_MESSAGE_SIZE)
473 text[NM_MAX_MESSAGE_SIZE] = 0;
475 rtfized = nm_rtfize_text(text);
477 purple_debug_info("novell", "message text is: %s\n", text);
478 purple_debug_info("novell", "message rtf is: %s\n", rtfized);
480 tmp = nm_field_add_pointer(tmp, NM_A_SZ_MESSAGE_BODY, 0, NMFIELD_METHOD_VALID, 0,
481 rtfized, NMFIELD_TYPE_UTF8);
483 tmp = nm_field_add_number(tmp, NM_A_UD_MESSAGE_TYPE, 0, NMFIELD_METHOD_VALID, 0,
484 0, NMFIELD_TYPE_UDWORD);
486 tmp = nm_field_add_pointer(tmp, NM_A_SZ_MESSAGE_TEXT, 0, NMFIELD_METHOD_VALID, 0,
487 text, NMFIELD_TYPE_UTF8);
489 fields = nm_field_add_pointer(fields, NM_A_FA_MESSAGE, 0, NMFIELD_METHOD_VALID, 0,
490 tmp, NMFIELD_TYPE_ARRAY);
491 tmp = NULL;
493 /* Add participants */
494 count = nm_conference_get_participant_count(conf);
495 for (i = 0; i < count; i++) {
496 user_record = nm_conference_get_participant(conf, i);
497 if (user_record) {
498 fields =
499 nm_field_add_pointer(fields, NM_A_SZ_DN, 0, NMFIELD_METHOD_VALID, 0,
500 g_strdup(nm_user_record_get_dn(user_record)),
501 NMFIELD_TYPE_DN);
505 /* Send the request */
506 rc = nm_send_request(user->conn, "sendmessage", fields, callback, NULL, NULL);
509 nm_free_fields(&fields);
510 return rc;
513 NMERR_T
514 nm_send_typing(NMUser * user, NMConference * conf,
515 gboolean typing, nm_response_cb callback)
517 NMERR_T rc = NM_OK;
518 char *str = NULL;
519 NMField *fields = NULL, *tmp = NULL;
521 if (user == NULL || conf == NULL) {
522 return NMERR_BAD_PARM;
525 if (!nm_conference_is_instantiated(conf)) {
526 rc = NMERR_CONFERENCE_NOT_INSTANTIATED;
527 } else {
528 /* Add the conference GUID */
529 tmp = nm_field_add_pointer(tmp, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0,
530 g_strdup(nm_conference_get_guid(conf)),
531 NMFIELD_TYPE_UTF8);
533 /* Add typing type */
534 str = g_strdup_printf("%d",
535 (typing ? NMEVT_USER_TYPING :
536 NMEVT_USER_NOT_TYPING));
538 tmp = nm_field_add_pointer(tmp, NM_A_SZ_TYPE, 0, NMFIELD_METHOD_VALID, 0,
539 str, NMFIELD_TYPE_UTF8);
541 fields =
542 nm_field_add_pointer(fields, NM_A_FA_CONVERSATION, 0, NMFIELD_METHOD_VALID, 0,
543 tmp, NMFIELD_TYPE_ARRAY);
544 tmp = NULL;
546 rc = nm_send_request(user->conn, "sendtyping", fields, callback, NULL, NULL);
549 nm_free_fields(&fields);
550 return rc;
553 NMERR_T
554 nm_send_create_contact(NMUser * user, NMFolder * folder,
555 NMContact * contact, nm_response_cb callback,
556 gpointer data)
558 NMERR_T rc = NM_OK;
559 NMField *fields = NULL;
560 NMRequest *req = NULL;
561 const char *name = NULL;
562 const char *display_name = NULL;
564 if (user == NULL || folder == NULL || contact == NULL) {
565 return NMERR_BAD_PARM;
568 /* Add parent ID */
569 fields = nm_field_add_pointer(fields, NM_A_SZ_PARENT_ID, 0, NMFIELD_METHOD_VALID, 0,
570 g_strdup_printf("%d", nm_folder_get_id(folder)),
571 NMFIELD_TYPE_UTF8);
573 /* Check to see if userid is current user and return an error? */
575 /* Check to see if contact already exists and return an error? */
577 /* Add userid or dn */
578 name = nm_contact_get_dn(contact);
579 if (name == NULL)
580 return NMERR_BAD_PARM;
582 if (strstr("=", name)) {
583 fields = nm_field_add_pointer(fields, NM_A_SZ_DN, 0, NMFIELD_METHOD_VALID, 0,
584 g_strdup(name), NMFIELD_TYPE_DN);
585 } else {
586 fields = nm_field_add_pointer(fields, NM_A_SZ_USERID, 0, NMFIELD_METHOD_VALID, 0,
587 g_strdup(name), NMFIELD_TYPE_UTF8);
590 /* Add display name */
591 display_name = nm_contact_get_display_name(contact);
592 if (display_name)
593 fields = nm_field_add_pointer(fields, NM_A_SZ_DISPLAY_NAME, 0, NMFIELD_METHOD_VALID, 0,
594 g_strdup(display_name), NMFIELD_TYPE_UTF8);
596 /* Dispatch the request */
597 rc = nm_send_request(user->conn, "createcontact", fields, callback, data, &req);
598 if (rc == NM_OK && req)
599 nm_request_set_data(req, contact);
601 if (req)
602 nm_release_request(req);
604 nm_free_fields(&fields);
605 return rc;
608 NMERR_T
609 nm_send_remove_contact(NMUser * user, NMFolder * folder,
610 NMContact * contact, nm_response_cb callback,
611 gpointer data)
613 NMERR_T rc = NM_OK;
614 NMField *fields = NULL;
615 NMRequest *req = NULL;
617 if (user == NULL || folder == NULL || contact == NULL) {
618 return NMERR_BAD_PARM;
621 /* Add parent id */
622 fields = nm_field_add_pointer(fields, NM_A_SZ_PARENT_ID, 0, NMFIELD_METHOD_VALID, 0,
623 g_strdup_printf("%d", nm_folder_get_id(folder)),
624 NMFIELD_TYPE_UTF8);
626 /* Add object id */
627 fields = nm_field_add_pointer(fields, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0,
628 g_strdup_printf("%d", nm_contact_get_id(contact)),
629 NMFIELD_TYPE_UTF8);
631 /* Dispatch the request */
632 rc = nm_send_request(user->conn, "deletecontact", fields, callback, data, &req);
633 if (rc == NM_OK && req)
634 nm_request_set_data(req, contact);
636 if (req)
637 nm_release_request(req);
639 nm_free_fields(&fields);
640 return rc;
643 NMERR_T
644 nm_send_create_folder(NMUser * user, const char *name,
645 nm_response_cb callback, gpointer data)
647 NMERR_T rc = NM_OK;
648 NMField *fields = NULL;
649 NMRequest *req = NULL;
651 if (user == NULL || name == NULL) {
652 return NMERR_BAD_PARM;
655 /* Add parent ID */
656 fields = nm_field_add_pointer(fields, NM_A_SZ_PARENT_ID, 0, NMFIELD_METHOD_VALID, 0,
657 g_strdup("0"), NMFIELD_TYPE_UTF8);
659 /* Add name of the folder to add */
660 fields =
661 nm_field_add_pointer(fields, NM_A_SZ_DISPLAY_NAME, 0, NMFIELD_METHOD_VALID, 0,
662 g_strdup(name), NMFIELD_TYPE_UTF8);
664 /* Add sequence, for now just put it at the bottom */
665 fields =
666 nm_field_add_pointer(fields, NM_A_SZ_SEQUENCE_NUMBER, 0, NMFIELD_METHOD_VALID, 0,
667 g_strdup("-1"), NMFIELD_TYPE_UTF8);
669 /* Dispatch the request */
670 rc = nm_send_request(user->conn, "createfolder", fields, callback, data, &req);
671 if (rc == NM_OK && req)
672 nm_request_set_data(req, g_strdup(name));
674 if (req)
675 nm_release_request(req);
677 nm_free_fields(&fields);
678 return rc;
681 NMERR_T
682 nm_send_remove_folder(NMUser * user, NMFolder * folder,
683 nm_response_cb callback, gpointer data)
685 NMERR_T rc = NM_OK;
686 NMField *fields = NULL;
687 NMRequest *req = NULL;
689 if (user == NULL || folder == NULL) {
690 return NMERR_BAD_PARM;
693 /* Add the object id */
694 fields = nm_field_add_pointer(fields, NM_A_SZ_OBJECT_ID, 0, NMFIELD_METHOD_VALID, 0,
695 g_strdup_printf("%d", nm_folder_get_id(folder)),
696 NMFIELD_TYPE_UTF8);
698 /* Dispatch the request */
699 rc = nm_send_request(user->conn, "deletecontact", fields, callback, data, &req);
700 if (rc == NM_OK && req)
701 nm_request_set_data(req, folder);
703 if (req)
704 nm_release_request(req);
706 nm_free_fields(&fields);
707 return rc;
710 NMERR_T
711 nm_send_get_status(NMUser * user, NMUserRecord * user_record,
712 nm_response_cb callback, gpointer data)
714 NMERR_T rc = NM_OK;
715 NMField *fields = NULL;
716 NMRequest *req = NULL;
717 const char *dn;
719 if (user == NULL || user_record == NULL)
720 return NMERR_BAD_PARM;
722 /* Add DN to field list */
723 dn = nm_user_record_get_dn(user_record);
724 if (dn == NULL)
725 return (NMERR_T) -1;
727 fields = nm_field_add_pointer(fields, NM_A_SZ_DN, 0, NMFIELD_METHOD_VALID, 0,
728 g_strdup(dn), NMFIELD_TYPE_UTF8);
730 /* Dispatch the request */
731 rc = nm_send_request(user->conn, "getstatus", fields, callback, data, &req);
732 if (rc == NM_OK && req)
733 nm_request_set_data(req, user_record);
735 if (req)
736 nm_release_request(req);
738 nm_free_fields(&fields);
739 return rc;
742 NMERR_T
743 nm_send_rename_contact(NMUser * user, NMContact * contact,
744 const char *new_name, nm_response_cb callback,
745 gpointer data)
747 NMERR_T rc = NM_OK;
748 NMField *field = NULL, *fields = NULL, *list = NULL;
749 NMRequest *req = NULL;
751 if (user == NULL || contact == NULL || new_name == NULL)
752 return NMERR_BAD_PARM;
754 /* Create field list for current contact */
755 field = nm_contact_to_fields(contact);
756 if (field) {
758 fields =
759 nm_field_add_pointer(fields, NM_A_FA_CONTACT, 0, NMFIELD_METHOD_DELETE, 0,
760 field, NMFIELD_TYPE_ARRAY);
761 field = NULL;
763 /* Update the contacts display name locally */
764 nm_contact_set_display_name(contact, new_name);
766 /* Create field list for updated contact */
767 field = nm_contact_to_fields(contact);
768 if (field) {
769 fields =
770 nm_field_add_pointer(fields, NM_A_FA_CONTACT, 0, NMFIELD_METHOD_ADD, 0,
771 field, NMFIELD_TYPE_ARRAY);
772 field = NULL;
774 /* Package it up */
775 list =
776 nm_field_add_pointer(list, NM_A_FA_CONTACT_LIST, 0, NMFIELD_METHOD_VALID,
777 0, fields, NMFIELD_TYPE_ARRAY);
778 fields = NULL;
780 rc = nm_send_request(user->conn, "updateitem", list, callback, data, &req);
781 if (rc == NM_OK && req)
782 nm_request_set_data(req, contact);
786 if (req)
787 nm_release_request(req);
789 if (list)
790 nm_free_fields(&list);
792 return rc;
795 NMERR_T
796 nm_send_rename_folder(NMUser * user, NMFolder * folder, const char *new_name,
797 nm_response_cb callback, gpointer data)
799 NMERR_T rc = NM_OK;
800 NMField *field = NULL, *fields = NULL, *list = NULL;
801 NMRequest *req = NULL;
803 if (user == NULL || folder == NULL || new_name == NULL)
804 return NMERR_BAD_PARM;
806 /* Make sure folder does not already exist!? */
807 if (nm_find_folder(user, new_name))
808 return NMERR_FOLDER_EXISTS;
810 /* Create field list for current folder */
811 field = nm_folder_to_fields(folder);
812 if (field) {
814 fields = nm_field_add_pointer(fields, NM_A_FA_FOLDER, 0, NMFIELD_METHOD_DELETE, 0,
815 field, NMFIELD_TYPE_ARRAY);
816 field = NULL;
818 /* Update the folders display name locally */
819 nm_folder_set_name(folder, new_name);
821 /* Create field list for updated folder */
822 field = nm_folder_to_fields(folder);
823 if (field) {
824 fields = nm_field_add_pointer(fields, NM_A_FA_FOLDER, 0, NMFIELD_METHOD_ADD, 0,
825 field, NMFIELD_TYPE_ARRAY);
826 field = NULL;
828 /* Package it up */
829 list = nm_field_add_pointer(list, NM_A_FA_CONTACT_LIST, 0, NMFIELD_METHOD_VALID,
830 0, fields, NMFIELD_TYPE_ARRAY);
831 fields = NULL;
833 rc = nm_send_request(user->conn, "updateitem", list, callback, data, &req);
834 if (rc == NM_OK && req)
835 nm_request_set_data(req, folder);
839 if (req)
840 nm_release_request(req);
842 if (list)
843 nm_free_fields(&list);
845 return rc;
848 NMERR_T
849 nm_send_move_contact(NMUser * user, NMContact * contact, NMFolder * folder,
850 nm_response_cb callback, gpointer data)
852 NMERR_T rc = NM_OK;
853 NMField *field = NULL, *fields = NULL, *list = NULL;
854 NMRequest *req = NULL;
856 if (user == NULL || contact == NULL || folder == NULL)
857 return NMERR_BAD_PARM;
859 /* Create field list for the contact */
860 field = nm_contact_to_fields(contact);
861 if (field) {
863 fields = nm_field_add_pointer(fields, NM_A_FA_CONTACT, 0, NMFIELD_METHOD_DELETE, 0,
864 field, NMFIELD_TYPE_ARRAY);
865 field = NULL;
867 /* Wrap the contact up and add it to the request field list */
868 list = nm_field_add_pointer(list, NM_A_FA_CONTACT_LIST, 0, NMFIELD_METHOD_VALID, 0,
869 fields, NMFIELD_TYPE_ARRAY);
870 fields = NULL;
872 /* Add sequence number */
873 list = nm_field_add_pointer(list, NM_A_SZ_SEQUENCE_NUMBER, 0, NMFIELD_METHOD_VALID,
874 0, g_strdup("-1"), NMFIELD_TYPE_UTF8);
876 /* Add parent ID */
877 list = nm_field_add_pointer(list, NM_A_SZ_PARENT_ID, 0, NMFIELD_METHOD_VALID, 0,
878 g_strdup_printf("%d", nm_folder_get_id(folder)),
879 NMFIELD_TYPE_UTF8);
881 /* Dispatch the request */
882 rc = nm_send_request(user->conn, "movecontact", list, callback, data, &req);
883 if (rc == NM_OK && req)
884 nm_request_set_data(req, contact);
888 if (req)
889 nm_release_request(req);
891 if (list)
892 nm_free_fields(&list);
894 return rc;
898 NMERR_T
899 nm_send_create_privacy_item(NMUser *user, const char *who, gboolean allow_list,
900 nm_response_cb callback, gpointer data)
902 NMERR_T rc = NM_OK;
903 NMField *fields = NULL;
904 const char *tag;
906 if (user == NULL || who == NULL)
907 return NMERR_BAD_PARM;
909 if (allow_list)
910 tag = NM_A_SZ_BLOCKING_ALLOW_ITEM;
911 else
912 tag = NM_A_SZ_BLOCKING_DENY_ITEM;
914 fields = nm_field_add_pointer(fields, tag, 0, NMFIELD_METHOD_ADD, 0,
915 g_strdup(who), NMFIELD_TYPE_UTF8);
917 rc = nm_send_request(user->conn, "createblock", fields, callback, data, NULL);
919 nm_free_fields(&fields);
920 return rc;
923 NMERR_T
924 nm_send_remove_privacy_item(NMUser *user, const char *dn, gboolean allow_list,
925 nm_response_cb callback, gpointer data)
927 NMERR_T rc = NM_OK;
928 NMField *fields = NULL;
929 const char *tag;
930 GSList **list_ptr, *node;
932 if (user == NULL || dn == NULL)
933 return NMERR_BAD_PARM;
935 if (allow_list) {
936 tag = NM_A_BLOCKING_ALLOW_LIST;
937 list_ptr = &user->allow_list;
938 } else {
939 tag = NM_A_BLOCKING_DENY_LIST;
940 list_ptr = &user->deny_list;
943 /* Remove item from the cached list */
944 if ((node = g_slist_find_custom(*list_ptr, dn, (GCompareFunc)purple_utf8_strcasecmp))) {
945 *list_ptr = g_slist_delete_link(*list_ptr, node);
948 fields = nm_field_add_pointer(fields, tag, 0, NMFIELD_METHOD_DELETE, 0,
949 g_strdup(dn), NMFIELD_TYPE_DN);
951 rc = nm_send_request(user->conn, "updateblocks", fields, callback, data, NULL);
953 nm_free_fields(&fields);
954 return rc;
958 NMERR_T
959 nm_send_set_privacy_default(NMUser *user, gboolean default_deny,
960 nm_response_cb callback, gpointer data)
962 NMERR_T rc = NM_OK;
963 NMField *fields = NULL;
965 if (user == NULL)
966 return NMERR_BAD_PARM;
968 fields = nm_field_add_pointer(fields, NM_A_BLOCKING, 0, NMFIELD_METHOD_UPDATE, 0,
969 (default_deny ? g_strdup("1") : g_strdup("0")),
970 NMFIELD_TYPE_UTF8);
972 rc = nm_send_request(user->conn, "updateblocks", fields, callback, data, NULL);
974 nm_free_fields(&fields);
975 return rc;
978 NMERR_T
979 nm_send_keepalive(NMUser *user, nm_response_cb callback, gpointer data)
981 NMERR_T rc = NM_OK;
983 if (user == NULL)
984 return NMERR_BAD_PARM;
986 rc = nm_send_request(user->conn, "ping", NULL, callback, data, NULL);
988 return rc;
991 NMERR_T
992 nm_process_new_data(NMUser * user)
994 NMConn *conn;
995 NMERR_T rc = NM_OK;
996 guint32 val;
998 if (user == NULL)
999 return NMERR_BAD_PARM;
1001 conn = user->conn;
1003 /* Check to see if this is an event or a response */
1004 rc = nm_read_all(conn, (char *) &val, sizeof(val));
1005 if (rc == NM_OK) {
1006 if (strncmp((char *) &val, "HTTP", strlen("HTTP")) == 0)
1007 rc = nm_process_response(user);
1008 else
1009 rc = nm_process_event(user, GUINT32_FROM_LE(val));
1011 } else {
1012 if (errno == EAGAIN)
1013 rc = NM_OK;
1014 else
1015 rc = NMERR_PROTOCOL;
1018 return rc;
1021 NMConference *
1022 nm_find_conversation(NMUser * user, const char *who)
1024 NMConference *conference = NULL;
1025 NMConference *tmp;
1026 GSList *cnode;
1028 if (user && user->conferences) {
1029 for (cnode = user->conferences; cnode; cnode = cnode->next) {
1030 tmp = cnode->data;
1031 if (nm_conference_get_participant_count(tmp) == 1) {
1032 NMUserRecord *ur = nm_conference_get_participant(tmp, 0);
1034 if (ur) {
1035 if (nm_utf8_str_equal(nm_user_record_get_dn(ur), who)) {
1036 conference = tmp;
1037 break;
1044 return conference;
1047 void
1048 nm_conference_list_add(NMUser * user, NMConference * conf)
1050 if (user == NULL || conf == NULL)
1051 return;
1053 nm_conference_add_ref(conf);
1054 user->conferences = g_slist_append(user->conferences, conf);
1057 void
1058 nm_conference_list_remove(NMUser * user, NMConference * conf)
1060 if (user == NULL || conf == NULL)
1061 return;
1063 if (g_slist_find(user->conferences, conf)) {
1064 user->conferences = g_slist_remove(user->conferences, conf);
1065 nm_release_conference(conf);
1069 void
1070 nm_conference_list_free(NMUser * user)
1072 if (user == NULL)
1073 return;
1075 g_slist_free_full(user->conferences, (GDestroyNotify)nm_release_conference);
1076 user->conferences = NULL;
1079 NMConference *
1080 nm_conference_list_find(NMUser * user, const char *guid)
1082 GSList *cnode;
1083 NMConference *conference = NULL, *tmp;
1085 if (user == NULL || guid == NULL)
1086 return NULL;
1088 if (user->conferences) {
1089 for (cnode = user->conferences; cnode; cnode = cnode->next) {
1090 tmp = cnode->data;
1091 if (nm_are_guids_equal(nm_conference_get_guid(tmp), guid)) {
1092 conference = tmp;
1093 break;
1098 return conference;
1101 gboolean
1102 nm_are_guids_equal(const char *guid1, const char *guid2)
1104 if (guid1 == NULL || guid2 == NULL)
1105 return FALSE;
1107 return (strncmp(guid1, guid2, CONF_GUID_END) == 0);
1110 void
1111 nm_user_add_contact(NMUser * user, NMContact * contact)
1113 if (user == NULL || contact == NULL)
1114 return;
1116 nm_contact_add_ref(contact);
1118 g_hash_table_insert(user->contacts,
1119 g_utf8_strdown(nm_contact_get_dn(contact), -1), contact);
1122 void
1123 nm_user_add_user_record(NMUser * user, NMUserRecord * user_record)
1125 const char *display_id;
1126 const char *dn;
1128 if (!user || !user_record)
1129 return;
1131 display_id = nm_user_record_get_display_id(user_record);
1132 dn = nm_user_record_get_dn(user_record);
1134 if (!dn || !display_id)
1135 return;
1137 nm_user_record_add_ref(user_record);
1139 g_hash_table_insert(user->user_records,
1140 g_utf8_strdown(dn, -1),
1141 user_record);
1143 g_hash_table_insert(user->display_id_to_dn,
1144 g_utf8_strdown(display_id, -1),
1145 g_utf8_strdown(dn, -1));
1148 nm_event_cb
1149 nm_user_get_event_callback(NMUser * user)
1151 if (user == NULL)
1152 return NULL;
1154 return user->evt_callback;
1157 NMConn *
1158 nm_user_get_conn(NMUser * user)
1160 if (user == NULL)
1161 return NULL;
1163 return user->conn;
1166 NMERR_T
1167 nm_create_contact_list(NMUser * user)
1169 NMERR_T rc = NM_OK;
1170 NMField *locate = NULL;
1172 if (user == NULL || user->fields == NULL) {
1173 return NMERR_BAD_PARM;
1176 /* Create the root folder */
1177 user->root_folder = nm_create_folder("");
1179 /* Find the contact list in the login fields */
1180 locate = nm_locate_field(NM_A_FA_CONTACT_LIST, user->fields);
1181 if (locate != NULL) {
1183 /* Add the folders and then the contacts */
1184 nm_folder_add_contacts_and_folders(user, user->root_folder,
1185 (NMField *) (locate->ptr_value));
1189 return rc;
1192 gboolean nm_user_is_privacy_locked(NMUser *user)
1194 if (user) {
1195 return user->privacy_locked;
1198 return FALSE;
1201 static gboolean
1202 _create_privacy_list(NMUser * user, NMRequest *request)
1204 NMField *locate = NULL;
1205 GSList *need_details = NULL;
1207 /* Are the privacy settings locked */
1208 locate = nm_locate_field(NM_A_LOCKED_ATTR_LIST, user->fields);
1209 if (locate && locate->ptr_value) {
1210 if (locate->type == NMFIELD_TYPE_UTF8 &&
1211 (purple_utf8_strcasecmp(locate->ptr_value, NM_A_BLOCKING) == 0)) {
1212 user->privacy_locked = TRUE;
1213 } else if (locate->type == NMFIELD_TYPE_MV ||
1214 locate->type == NMFIELD_TYPE_ARRAY) {
1215 NMField *tmp = (NMField *)locate->ptr_value;
1216 while (tmp && tmp->tag) {
1217 if (purple_utf8_strcasecmp(tmp->ptr_value, NM_A_BLOCKING) == 0) {
1218 user->privacy_locked = TRUE;
1219 break;
1221 tmp++;
1226 /* Set default deny flag */
1227 locate = nm_locate_field(NM_A_BLOCKING, user->fields);
1228 if (locate && locate->ptr_value) {
1229 user->default_deny = atoi((char *)locate->ptr_value);
1232 /* Read internal blocking allow list */
1233 locate = nm_locate_field(NM_A_BLOCKING_ALLOW_LIST, user->fields);
1234 if (locate && locate->ptr_value) {
1236 if (locate->type == NMFIELD_TYPE_MV) {
1237 locate = (NMField *)locate->ptr_value;
1238 for (; locate->tag != NULL; locate++) {
1239 if (locate->ptr_value) {
1241 user->allow_list = g_slist_append(user->allow_list, (char *)locate->ptr_value);
1243 if (nm_find_user_record(user, (char *)locate->ptr_value) == NULL)
1244 need_details = g_slist_append(need_details, (char *)locate->ptr_value);
1248 } else {
1250 user->allow_list = g_slist_append(user->allow_list, (char *)locate->ptr_value);
1252 if (nm_find_user_record(user, (char *)locate->ptr_value) == NULL)
1253 need_details = g_slist_append(need_details, (char *)locate->ptr_value);
1258 /* Read internal blocking deny list */
1259 locate = nm_locate_field(NM_A_BLOCKING_DENY_LIST, user->fields);
1260 if (locate && locate->ptr_value) {
1262 if (locate->type == NMFIELD_TYPE_MV) {
1263 locate = (NMField *)locate->ptr_value;
1264 for (; locate->tag != NULL; locate++) {
1265 if (locate->ptr_value) {
1267 user->deny_list = g_slist_append(user->deny_list, (char *)locate->ptr_value);
1269 if (nm_find_user_record(user, (char *)locate->ptr_value) == NULL)
1270 need_details = g_slist_append(need_details, (char *)locate->ptr_value);
1274 } else {
1276 user->deny_list = g_slist_append(user->deny_list, (char *)locate->ptr_value);
1278 if (nm_find_user_record(user, (char *)locate->ptr_value) == NULL)
1279 need_details = g_slist_append(need_details, (char *)locate->ptr_value);
1284 if (need_details) {
1286 nm_request_add_ref(request);
1287 nm_send_multiple_get_details(user, need_details,
1288 _handle_multiple_get_details_login_cb, request);
1290 return FALSE;
1293 return TRUE;
1296 void
1297 nm_destroy_contact_list(NMUser * user)
1299 if (user == NULL)
1300 return;
1302 if (user->root_folder) {
1303 nm_release_folder(user->root_folder);
1304 user->root_folder = NULL;
1308 NMFolder *
1309 nm_get_root_folder(NMUser * user)
1311 if (user == NULL)
1312 return NULL;
1314 if (user->root_folder == NULL)
1315 nm_create_contact_list(user);
1317 return user->root_folder;
1320 NMContact *
1321 nm_find_contact(NMUser * user, const char *name)
1323 char *str;
1324 const char *dn = NULL;
1325 NMContact *contact = NULL;
1327 if (user == NULL || name == NULL)
1328 return NULL;
1330 str = g_utf8_strdown(name, -1);
1331 if (strstr(str, "=")) {
1332 dn = str;
1333 } else {
1334 /* Assume that we have a display id instead of a dn */
1335 dn = (const char *) g_hash_table_lookup(user->display_id_to_dn, str);
1338 /* Find contact object in reference table */
1339 if (dn) {
1340 contact = (NMContact *) g_hash_table_lookup(user->contacts, dn);
1343 g_free(str);
1344 return contact;
1347 GList *
1348 nm_find_contacts(NMUser * user, const char *dn)
1350 guint32 i, cnt;
1351 NMFolder *folder;
1352 NMContact *contact;
1353 GList *contacts = NULL;
1355 if (user == NULL || dn == NULL)
1356 return NULL;
1358 /* Check for contact at the root */
1359 contact = nm_folder_find_contact(user->root_folder, dn);
1360 if (contact) {
1361 contacts = g_list_append(contacts, contact);
1362 contact = NULL;
1365 /* Check for contact in each subfolder */
1366 cnt = nm_folder_get_subfolder_count(user->root_folder);
1367 for (i = 0; i < cnt; i++) {
1368 folder = nm_folder_get_subfolder(user->root_folder, i);
1369 contact = nm_folder_find_contact(folder, dn);
1370 if (contact) {
1371 contacts = g_list_append(contacts, contact);
1372 contact = NULL;
1376 return contacts;
1379 NMUserRecord *
1380 nm_find_user_record(NMUser * user, const char *name)
1382 char *str = NULL;
1383 const char *dn = NULL;
1384 NMUserRecord *user_record = NULL;
1386 if (user == NULL || name == NULL)
1387 return NULL;
1389 str = g_utf8_strdown(name, -1);
1390 if (strstr(str, "=")) {
1391 dn = str;
1392 } else {
1393 /* Assume that we have a display id instead of a dn */
1394 dn = (const char *) g_hash_table_lookup(user->display_id_to_dn, str);
1397 /* Find user record in reference table */
1398 if (dn) {
1399 user_record =
1400 (NMUserRecord *) g_hash_table_lookup(user->user_records, dn);
1403 g_free(str);
1404 return user_record;
1407 const char *
1408 nm_lookup_dn(NMUser * user, const char *display_id)
1410 const char *dn;
1411 char *lower;
1413 if (user == NULL || display_id == NULL)
1414 return NULL;
1416 lower = g_utf8_strdown(display_id, -1);
1417 dn = g_hash_table_lookup(user->display_id_to_dn, lower);
1418 g_free(lower);
1420 return dn;
1423 NMFolder *
1424 nm_find_folder(NMUser * user, const char *name)
1426 NMFolder *folder = NULL, *temp;
1427 int i, num_folders;
1428 const char *tname = NULL;
1430 if (user == NULL || name == NULL)
1431 return NULL;
1433 if (*name == '\0')
1434 return user->root_folder;
1436 num_folders = nm_folder_get_subfolder_count(user->root_folder);
1437 for (i = 0; i < num_folders; i++) {
1438 temp = nm_folder_get_subfolder(user->root_folder, i);
1439 tname = nm_folder_get_name(temp);
1440 if (tname && purple_strequal(tname, name)) {
1441 folder = temp;
1442 break;
1446 return folder;
1449 NMFolder *
1450 nm_find_folder_by_id(NMUser * user, int object_id)
1452 NMFolder *folder = NULL, *temp;
1453 int i, num_folders;
1455 if (user == NULL)
1456 return NULL;
1458 if (object_id == 0)
1459 return user->root_folder;
1461 num_folders = nm_folder_get_subfolder_count(user->root_folder);
1462 for (i = 0; i < num_folders; i++) {
1463 temp = nm_folder_get_subfolder(user->root_folder, i);
1464 if (nm_folder_get_id(temp) == object_id) {
1465 folder = temp;
1466 break;
1470 return folder;
1473 static void
1474 _handle_multiple_get_details_login_cb(NMUser * user, NMERR_T ret_code,
1475 gpointer resp_data, gpointer user_data)
1477 nm_response_cb cb;
1478 NMRequest *request = user_data;
1480 if (user == NULL || request == NULL)
1481 return;
1483 if ((cb = nm_request_get_callback(request))) {
1484 cb(user, ret_code, nm_request_get_data(request),
1485 nm_request_get_user_define(request));
1486 nm_release_request(request);
1490 static void
1491 _handle_multiple_get_details_joinconf_cb(NMUser * user, NMERR_T ret_code,
1492 gpointer resp_data, gpointer user_data)
1494 NMRequest *request = user_data;
1495 NMUserRecord *user_record = resp_data;
1496 NMConference *conference;
1497 GSList *list, *node;
1499 if (user == NULL || resp_data == NULL || user_data == NULL)
1500 return;
1502 conference = nm_request_get_data(request);
1503 list = nm_request_get_user_define(request);
1505 if (ret_code == 0 && conference && list) {
1507 /* Add the user to the conference */
1508 nm_conference_add_participant(conference, user_record);
1510 /* Find the user in the list and remove it */
1511 node = g_slist_find_custom(list, nm_user_record_get_dn(user_record), (GCompareFunc)purple_utf8_strcasecmp);
1512 if (node) {
1513 g_free(node->data);
1514 list = g_slist_delete_link(list, node);
1515 nm_request_set_user_define(request, list);
1518 /* Time to callback? */
1519 if (list == NULL) {
1520 nm_response_cb cb = nm_request_get_callback(request);
1522 if (cb) {
1523 cb(user, 0, conference, conference);
1525 nm_release_request(request);
1530 static NMERR_T
1531 nm_call_handler(NMUser * user, NMRequest * request, NMField * fields)
1533 NMERR_T rc = NM_OK, ret_code = NM_OK;
1534 NMConference *conf = NULL;
1535 NMUserRecord *user_record = NULL;
1536 NMField *locate = NULL;
1537 NMField *field = NULL;
1538 const char *cmd;
1539 nm_response_cb cb;
1540 gboolean done = TRUE;
1542 if (user == NULL || request == NULL || fields == NULL)
1543 return NMERR_BAD_PARM;
1545 /* Get the return code */
1546 field = nm_locate_field(NM_A_SZ_RESULT_CODE, fields);
1547 if (field) {
1548 ret_code = atoi((char *) field->ptr_value);
1549 } else {
1550 ret_code = NMERR_PROTOCOL;
1553 cmd = nm_request_get_cmd(request);
1554 if (ret_code == NM_OK && cmd != NULL) {
1556 if (purple_strequal("login", cmd)) {
1558 user->user_record = nm_create_user_record_from_fields(fields);
1560 /* Save the users fields */
1561 user->fields = nm_copy_field_array(fields);
1563 nm_create_contact_list(user);
1564 done = _create_privacy_list(user, request);
1566 } else if (purple_strequal("setstatus", cmd)) {
1568 /* Nothing to do */
1570 } else if (purple_strequal("createconf", cmd)) {
1572 conf = (NMConference *) nm_request_get_data(request);
1574 /* get the convo guid */
1575 locate = nm_locate_field(NM_A_FA_CONVERSATION, fields);
1576 if (locate) {
1577 field =
1578 nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->ptr_value);
1579 if (field) {
1580 nm_conference_set_guid(conf, (char *) field->ptr_value);
1584 nm_conference_list_add(user, conf);
1585 nm_release_conference(conf);
1587 } else if (purple_strequal("leaveconf", cmd)) {
1589 conf = (NMConference *) nm_request_get_data(request);
1590 nm_conference_list_remove(user, conf);
1592 } else if (purple_strequal("joinconf", cmd)) {
1593 GSList *list = NULL, *node;
1595 conf = nm_request_get_data(request);
1597 locate = nm_locate_field(NM_A_FA_CONTACT_LIST, fields);
1598 if (locate && locate->ptr_value != 0) {
1600 field = (NMField *) locate->ptr_value;
1601 while ((field = nm_locate_field(NM_A_SZ_DN, field))) {
1602 if (field->ptr_value != 0) {
1604 if (nm_utf8_str_equal
1605 (nm_user_record_get_dn(user->user_record),
1606 (const char *) field->ptr_value)) {
1607 field++;
1608 continue;
1611 user_record =
1612 nm_find_user_record(user,
1613 (const char *) field->ptr_value);
1614 if (user_record == NULL) {
1615 list =
1616 g_slist_append(list,
1617 g_strdup((char *) field->ptr_value));
1618 } else {
1619 nm_conference_add_participant(conf, user_record);
1622 field++;
1625 if (list != NULL) {
1627 done = FALSE;
1628 nm_request_set_user_define(request, list);
1629 nm_request_add_ref(request);
1630 for (node = list; node; node = node->next) {
1632 nm_send_get_details(user, (const char *) node->data,
1633 _handle_multiple_get_details_joinconf_cb,
1634 request);
1639 } else if (purple_strequal("getdetails", cmd)) {
1641 locate = nm_locate_field(NM_A_FA_RESULTS, fields);
1642 while (locate && locate->ptr_value != 0) {
1644 user_record = nm_create_user_record_from_fields(locate);
1645 if (user_record) {
1646 NMUserRecord *tmp;
1648 tmp =
1649 nm_find_user_record(user,
1650 nm_user_record_get_dn(user_record));
1651 if (tmp) {
1653 /* Update the existing user record */
1654 nm_user_record_copy(tmp, user_record);
1655 nm_release_user_record(user_record);
1656 user_record = tmp;
1658 } else {
1659 nm_user_add_user_record(user, user_record);
1660 nm_release_user_record(user_record);
1663 /* Response data is new user record */
1664 nm_request_set_data(request, (gpointer) user_record);
1667 locate = nm_locate_field(NM_A_FA_RESULTS, locate+1);
1670 } else if (purple_strequal("createfolder", cmd)) {
1672 _update_contact_list(user, fields);
1674 } else if (purple_strequal("createcontact", cmd)) {
1676 _update_contact_list(user, fields);
1678 locate =
1679 nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) fields->ptr_value);
1680 if (locate) {
1682 NMContact *new_contact =
1683 nm_folder_find_item_by_object_id(user->root_folder,
1684 atoi((char *)locate->ptr_value));
1686 if (new_contact) {
1688 /* Add the contact to our cache */
1689 nm_user_add_contact(user, new_contact);
1691 /* Set the contact as the response data */
1692 nm_request_set_data(request, (gpointer) new_contact);
1698 } else if (purple_strequal("deletecontact", cmd)) {
1700 _update_contact_list(user, fields);
1702 } else if (purple_strequal("movecontact", cmd)) {
1704 _update_contact_list(user, fields);
1706 } else if (purple_strequal("getstatus", cmd)) {
1708 locate = nm_locate_field(NM_A_SZ_STATUS, fields);
1709 if (locate) {
1710 nm_user_record_set_status((NMUserRecord *)
1711 nm_request_get_data(request),
1712 atoi((char *) locate->ptr_value), NULL);
1715 } else if (purple_strequal("updateitem", cmd)) {
1717 /* Nothing extra to do here */
1719 } else if (purple_strequal("createblock", cmd)) {
1720 if ((locate = nm_locate_field(NM_A_BLOCKING_DENY_LIST, fields))) {
1721 if (locate->ptr_value) {
1722 user->deny_list = g_slist_append(user->deny_list, g_strdup((char *)locate->ptr_value));
1724 } else if ((locate = nm_locate_field(NM_A_BLOCKING_ALLOW_LIST, fields))) {
1725 if (locate->ptr_value) {
1726 user->allow_list = g_slist_append(user->allow_list, g_strdup((char *)locate->ptr_value));
1729 } else if (purple_strequal("updateblocks", cmd)) {
1730 /* nothing to do here */
1731 } else {
1733 /* Nothing to do, just print debug message */
1734 purple_debug(PURPLE_DEBUG_INFO, "novell",
1735 "nm_call_handler(): Unknown request command, %s\n", cmd);
1740 if (done && (cb = nm_request_get_callback(request))) {
1742 cb(user, ret_code, nm_request_get_data(request),
1743 nm_request_get_user_define(request));
1746 return rc;
1749 static NMERR_T
1750 nm_process_response(NMUser * user)
1752 NMERR_T rc = NM_OK;
1753 NMField *fields = NULL;
1754 NMField *field = NULL;
1755 NMConn *conn = user->conn;
1756 NMRequest *req = NULL;
1758 rc = nm_read_header(conn);
1759 if (rc == NM_OK) {
1760 rc = nm_read_fields(conn, -1, &fields);
1763 if (rc == NM_OK) {
1764 field = nm_locate_field(NM_A_SZ_TRANSACTION_ID, fields);
1765 if (field != NULL && field->ptr_value != 0) {
1766 req = nm_conn_find_request(conn, atoi((char *) field->ptr_value));
1767 if (req != NULL) {
1768 rc = nm_call_handler(user, req, fields);
1769 nm_conn_remove_request_item(conn, req);
1775 if (fields)
1776 nm_free_fields(&fields);
1778 return rc;
1782 * Some utility functions...haven't figured out where
1783 * they belong yet.
1786 gboolean
1787 nm_utf8_str_equal(gconstpointer str1, gconstpointer str2)
1789 return (purple_utf8_strcasecmp(str1, str2) == 0);
1792 char *
1793 nm_typed_to_dotted(const char *typed)
1795 unsigned i = 0, j = 0;
1796 char *dotted;
1798 if (typed == NULL)
1799 return NULL;
1801 dotted = g_new0(char, strlen(typed) + 1);
1803 do {
1805 /* replace comma with a dot */
1806 if (j != 0) {
1807 dotted[j] = '.';
1808 j++;
1811 /* skip the type */
1812 while (typed[i] != '\0' && typed[i] != '=')
1813 i++;
1815 /* verify that we aren't running off the end */
1816 if (typed[i] == '\0') {
1817 dotted[j] = '\0';
1818 break;
1821 i++;
1823 /* copy the object name to context */
1824 while (typed[i] != '\0' && typed[i] != ',') {
1825 dotted[j] = typed[i];
1826 j++;
1827 i++;
1830 } while (typed[i] != '\0');
1832 return dotted;
1835 const char *
1836 nm_error_to_string(NMERR_T err)
1838 static char *unknown_msg = NULL;
1840 g_free(unknown_msg);
1841 unknown_msg = NULL;
1843 switch (err) {
1845 case NMERR_BAD_PARM:
1846 return _("Required parameters not passed in");
1848 case NMERR_TCP_WRITE:
1849 return _("Unable to write to network");
1851 case NMERR_TCP_READ:
1852 return _("Unable to read from network");
1854 case NMERR_PROTOCOL:
1855 return _("Error communicating with server");
1857 case NMERR_CONFERENCE_NOT_FOUND:
1858 case NMERR_CONFERENCE_NOT_FOUND_2:
1859 return _("Conference not found");
1861 case NMERR_CONFERENCE_NOT_INSTANTIATED:
1862 return _("Conference does not exist");
1864 case NMERR_DUPLICATE_FOLDER:
1865 case NMERR_FOLDER_EXISTS:
1866 return _("A folder with that name already exists");
1868 case NMERR_NOT_SUPPORTED:
1869 return _("Not supported");
1871 case NMERR_PASSWORD_EXPIRED:
1872 case NMERR_PASSWORD_EXPIRED_2:
1873 return _("Password has expired");
1875 case NMERR_PASSWORD_INVALID:
1876 return _("Incorrect password");
1878 case NMERR_USER_NOT_FOUND:
1879 return _("User not found");
1881 case NMERR_USER_DISABLED:
1882 return _("Account has been disabled");
1884 case NMERR_DIRECTORY_FAILURE:
1885 return _("The server could not access the directory");
1887 case NMERR_ADMIN_LOCKED:
1888 return _("Your system administrator has disabled this operation");
1890 case NMERR_SERVER_BUSY:
1891 return _("The server is unavailable; try again later");
1893 case NMERR_DUPLICATE_CONTACT:
1894 return _("Cannot add a contact to the same folder twice");
1896 case NMERR_USER_NOT_ALLOWED:
1897 return _("Cannot add yourself");
1899 case NMERR_MASTER_ARCHIVE_MISSING:
1900 return _("Master archive is misconfigured");
1902 case NMERR_AUTHENTICATION_FAILED:
1903 case NMERR_CREDENTIALS_MISSING:
1904 return _("Incorrect username or password");
1906 case NMERR_HOST_NOT_FOUND:
1907 return _("Could not recognize the host of the username you entered");
1909 case NMERR_ACCESS_DENIED:
1910 return _("Your account has been disabled because too many incorrect passwords were entered");
1912 case NMERR_DUPLICATE_PARTICIPANT:
1913 return _("You cannot add the same person twice to a conversation");
1915 case NMERR_TOO_MANY_CONTACTS:
1916 case NMERR_TOO_MANY_FOLDERS:
1917 return _("You have reached your limit for the number of contacts allowed");
1919 case NMERR_OBJECT_NOT_FOUND:
1920 return _("You have entered an incorrect username");
1922 case NMERR_DIRECTORY_UPDATE:
1923 return _("An error occurred while updating the directory");
1925 case NMERR_SERVER_PROTOCOL:
1926 return _("Incompatible protocol version");
1928 case NMERR_USER_BLOCKED:
1929 return _("The user has blocked you");
1931 case NMERR_EVAL_CONNECTION_LIMIT:
1932 return _("This evaluation version does not allow more than ten users to log in at one time");
1934 case NMERR_CONVERSATION_INVITE:
1935 return _("The user is either offline or you are blocked");
1937 default:
1938 unknown_msg = g_strdup_printf (_("Unknown error: 0x%X"), err);
1940 return unknown_msg;
1944 static void
1945 _update_contact_list(NMUser * user, NMField * fields)
1947 NMField *list, *cursor, *locate;
1948 gint objid1;
1949 NMContact *contact;
1950 NMFolder *folder;
1951 gpointer item;
1953 if (user == NULL || fields == NULL)
1954 return;
1956 /* Is it wrapped in a RESULTS array? */
1957 if (purple_strequal(fields->tag, NM_A_FA_RESULTS)) {
1958 list = (NMField *) fields->ptr_value;
1959 } else {
1960 list = fields;
1963 /* Update the cached contact list */
1964 cursor = (NMField *) list->ptr_value;
1965 while (cursor->tag != NULL) {
1966 if ((g_ascii_strcasecmp(cursor->tag, NM_A_FA_CONTACT) == 0) ||
1967 (g_ascii_strcasecmp(cursor->tag, NM_A_FA_FOLDER) == 0)) {
1969 locate =
1970 nm_locate_field(NM_A_SZ_OBJECT_ID, (NMField *) cursor->ptr_value);
1971 if (locate != NULL && locate->ptr_value != 0) {
1972 objid1 = atoi((char *) locate->ptr_value);
1973 item =
1974 nm_folder_find_item_by_object_id(user->root_folder, objid1);
1975 if (item != NULL) {
1976 if (cursor->method == NMFIELD_METHOD_ADD) {
1977 if (g_ascii_strcasecmp(cursor->tag, NM_A_FA_CONTACT) == 0) {
1978 contact = (NMContact *) item;
1979 nm_contact_update_list_properties(contact, cursor);
1980 } else if (g_ascii_strcasecmp(cursor->tag, NM_A_FA_FOLDER)
1981 == 0) {
1982 folder = (NMFolder *) item;
1983 nm_folder_update_list_properties(folder, cursor);
1985 } else if (cursor->method == NMFIELD_METHOD_DELETE) {
1986 if (g_ascii_strcasecmp(cursor->tag, NM_A_FA_CONTACT) == 0) {
1987 contact = (NMContact *) item;
1988 folder =
1989 nm_find_folder_by_id(user,
1990 nm_contact_get_parent_id
1991 (contact));
1992 if (folder) {
1993 nm_folder_remove_contact(folder, contact);
1995 } else if (g_ascii_strcasecmp(cursor->tag, NM_A_FA_FOLDER)
1996 == 0) {
1997 /* TODO: write nm_folder_remove_folder */
1998 /* ignoring for now, should not be a big deal */
1999 /* folder = (NMFolder *) item;*/
2000 /* nm_folder_remove_folder(user->root_folder, folder);*/
2003 } else {
2005 if (cursor->method == NMFIELD_METHOD_ADD) {
2007 /* Not found, so we need to add it */
2008 if (g_ascii_strcasecmp(cursor->tag, NM_A_FA_CONTACT) == 0) {
2010 locate =
2011 nm_locate_field(NM_A_SZ_DN,
2012 (NMField *) cursor->ptr_value);
2013 if (locate != NULL && locate->ptr_value != NULL) {
2014 contact = nm_create_contact_from_fields(cursor);
2015 if (contact) {
2016 nm_folder_add_contact_to_list(
2017 user->root_folder, contact);
2018 nm_release_contact(contact);
2021 } else if (g_ascii_strcasecmp(cursor->tag, NM_A_FA_FOLDER)
2022 == 0) {
2023 folder = nm_create_folder_from_fields(cursor);
2024 nm_folder_add_folder_to_list(user->root_folder,
2025 folder);
2026 nm_release_folder(folder);
2032 cursor++;
2036 static char *
2037 nm_rtfize_text(char *text)
2039 GString *gstr = NULL;
2040 unsigned char *pch;
2041 char *uni_str = NULL, *rtf = NULL;
2042 int bytes;
2043 gunichar uc;
2045 gstr = g_string_sized_new(strlen(text)*2);
2046 pch = (unsigned char *)text;
2047 while (*pch) {
2048 if ((*pch) <= 0x7F) {
2049 switch (*pch) {
2050 case '{':
2051 case '}':
2052 case '\\':
2053 gstr = g_string_append_c(gstr, '\\');
2054 gstr = g_string_append_c(gstr, *pch);
2055 break;
2056 case '\n':
2057 gstr = g_string_append(gstr, "\\par ");
2058 break;
2059 default:
2060 gstr = g_string_append_c(gstr, *pch);
2061 break;
2063 pch++;
2064 } else {
2065 /* convert the utf-8 character to ucs-4 for rtf encoding */
2066 if(*pch <= 0xDF) {
2067 uc = ((((gunichar)pch[0]) & 0x001F) << 6) |
2068 (((gunichar)pch[1]) & 0x003F);
2069 bytes = 2;
2070 } else if(*pch <= 0xEF) {
2071 uc = ((((gunichar)pch[0]) & 0x000F) << 12) |
2072 ((((gunichar)pch[1]) & 0x003F) << 6) |
2073 (((gunichar)pch[2]) & 0x003F);
2074 bytes = 3;
2075 } else if (*pch <= 0xF7) {
2076 uc = ((((gunichar)pch[0]) & 0x0007) << 18) |
2077 ((((gunichar)pch[1]) & 0x003F) << 12) |
2078 ((((gunichar)pch[2]) & 0x003F) << 6) |
2079 (((gunichar)pch[3]) & 0x003F);
2080 bytes = 4;
2081 } else if (*pch <= 0xFB) {
2082 uc = ((((gunichar)pch[0]) & 0x0003) << 24) |
2083 ((((gunichar)pch[1]) & 0x003F) << 18) |
2084 ((((gunichar)pch[2]) & 0x003F) << 12) |
2085 ((((gunichar)pch[3]) & 0x003F) << 6) |
2086 (((gunichar)pch[4]) & 0x003F);
2087 bytes = 5;
2088 } else if (*pch <= 0xFD) {
2089 uc = ((((gunichar)pch[0]) & 0x0001) << 30) |
2090 ((((gunichar)pch[1]) & 0x003F) << 24) |
2091 ((((gunichar)pch[2]) & 0x003F) << 18) |
2092 ((((gunichar)pch[3]) & 0x003F) << 12) |
2093 ((((gunichar)pch[4]) & 0x003F) << 6) |
2094 (((gunichar)pch[5]) & 0x003F);
2095 bytes = 6;
2096 } else {
2097 /* should never happen ... bogus utf-8! */
2098 purple_debug_info("novell", "bogus utf-8 lead byte: 0x%X\n", pch[0]);
2099 uc = 0x003F;
2100 bytes = 1;
2102 uni_str = g_strdup_printf("\\u%d?", uc);
2103 purple_debug_info("novell", "unicode escaped char %s\n", uni_str);
2104 gstr = g_string_append(gstr, uni_str);
2105 pch += bytes;
2106 g_free(uni_str);
2110 rtf = g_strdup_printf(RTF_TEMPLATE, gstr->str);
2111 g_string_free(gstr, TRUE);
2112 return rtf;