comparison libpurple/protocols/msn/userlist.c @ 20481:eb93710aec4d

Make buddy and group management actually work, add some SOAP templates, redesign some parts of code and separate some bigger functions into its smaller tasks which are completely unrelated to each other
author Carlos Silva <typ0@pidgin.im>
date Tue, 28 Aug 2007 03:54:18 +0000
parents 9a2a4a0c0003
children 1754155051a4
comparison
equal deleted inserted replaced
20480:7d3e53e3f623 20481:eb93710aec4d
40 static void 40 static void
41 msn_accept_add_cb(MsnPermitAdd *pa) 41 msn_accept_add_cb(MsnPermitAdd *pa)
42 { 42 {
43 MsnSession *session = pa->gc->proto_data; 43 MsnSession *session = pa->gc->proto_data;
44 MsnUserList *userlist = session->userlist; 44 MsnUserList *userlist = session->userlist;
45 45
46 msn_userlist_add_buddy(userlist, pa->who, MSN_LIST_AL, NULL); 46 msn_userlist_add_buddy_to_list(userlist, pa->who, MSN_LIST_AL);
47 msn_userlist_add_buddy(userlist, pa->who, NULL);
47 48
48 g_free(pa->who); 49 g_free(pa->who);
49 g_free(pa->friendly); 50 g_free(pa->friendly);
50 g_free(pa); 51 g_free(pa);
51 } 52 }
56 if (g_list_find(purple_connections_get_all(), pa->gc) != NULL) 57 if (g_list_find(purple_connections_get_all(), pa->gc) != NULL)
57 { 58 {
58 MsnSession *session = pa->gc->proto_data; 59 MsnSession *session = pa->gc->proto_data;
59 MsnUserList *userlist = session->userlist; 60 MsnUserList *userlist = session->userlist;
60 61
61 msn_userlist_add_buddy(userlist, pa->who, MSN_LIST_BL, NULL); 62 msn_userlist_add_buddy_to_list(userlist, pa->who, MSN_LIST_BL);
62 } 63 }
63 64
64 g_free(pa->who); 65 g_free(pa->who);
65 g_free(pa->friendly); 66 g_free(pa->friendly);
66 g_free(pa); 67 g_free(pa);
85 /************************************************************************** 86 /**************************************************************************
86 * Utility functions 87 * Utility functions
87 **************************************************************************/ 88 **************************************************************************/
88 89
89 static gboolean 90 static gboolean
90 user_is_in_group(MsnUser *user, const char * group_id) 91 msn_userlist_user_is_in_group(MsnUser *user, const char * group_id)
91 { 92 {
92 if (user == NULL) 93 if (user == NULL)
93 return FALSE; 94 return FALSE;
94 95
95 if (group_id == NULL) 96 if (group_id == NULL)
100 101
101 return FALSE; 102 return FALSE;
102 } 103 }
103 104
104 static gboolean 105 static gboolean
105 user_is_there(MsnUser *user, int list_id, const char * group_id) 106 msn_userlist_user_is_in_list(MsnUser *user, int list_id)
106 { 107 {
107 int list_op; 108 int list_op;
108 109
109 if (user == NULL) 110 if (user == NULL)
110 return FALSE; 111 return FALSE;
111 112
112 list_op = 1 << list_id; 113 list_op = 1 << list_id;
113 114
114 if (!(user->list_op & list_op)) 115 if (user->list_op & list_op)
116 return TRUE;
117 else
115 return FALSE; 118 return FALSE;
116
117 if (list_id == MSN_LIST_FL)
118 {
119 if (group_id != NULL)
120 return user_is_in_group(user, group_id);
121 }
122
123 return TRUE;
124 } 119 }
125 120
126 static const char* 121 static const char*
127 get_store_name(MsnUser *user) 122 get_store_name(MsnUser *user)
128 { 123 {
147 142
148 if (strlen(store_name) > BUDDY_ALIAS_MAXLEN) 143 if (strlen(store_name) > BUDDY_ALIAS_MAXLEN)
149 store_name = msn_user_get_passport(user); 144 store_name = msn_user_get_passport(user);
150 145
151 return store_name; 146 return store_name;
152 }
153
154 static void
155 msn_request_add_group(MsnUserList *userlist, const char *who,
156 const char *old_group_name, const char *new_group_name)
157 {
158 MsnSession *session;
159 MsnCmdProc *cmdproc;
160 MsnMoveBuddy *data;
161
162 session = userlist->session;
163 cmdproc = session->notification->cmdproc;
164 data = g_new0(MsnMoveBuddy, 1);
165
166 data->who = g_strdup(who);
167
168 if (old_group_name)
169 {
170 data->old_group_name = g_strdup(old_group_name);
171 /*delete the old group via SOAP action*/
172 msn_del_group(session,old_group_name);
173 }
174
175 /*add new group via SOAP action*/
176 msn_add_group(session, new_group_name);
177
178 } 147 }
179 148
180 /************************************************************************** 149 /**************************************************************************
181 * Server functions 150 * Server functions
182 **************************************************************************/ 151 **************************************************************************/
461 user = msn_userlist_find_user(userlist, passport); 430 user = msn_userlist_find_user(userlist, passport);
462 if (user == NULL) 431 if (user == NULL)
463 { 432 {
464 user = msn_user_new(userlist, passport, userName); 433 user = msn_user_new(userlist, passport, userName);
465 msn_userlist_add_user(userlist, user); 434 msn_userlist_add_user(userlist, user);
466 } 435 } else {
467 msn_user_set_store_name(user, userName); 436 msn_user_set_store_name(user, userName);
437 }
468 return user; 438 return user;
469 } 439 }
470 440
471 void 441 void
472 msn_userlist_add_user(MsnUserList *userlist, MsnUser *user) 442 msn_userlist_add_user(MsnUserList *userlist, MsnUser *user)
504 474
505 void 475 void
506 msn_userlist_add_group(MsnUserList *userlist, MsnGroup *group) 476 msn_userlist_add_group(MsnUserList *userlist, MsnGroup *group)
507 { 477 {
508 userlist->groups = g_list_append(userlist->groups, group); 478 userlist->groups = g_list_append(userlist->groups, group);
479
509 } 480 }
510 481
511 void 482 void
512 msn_userlist_remove_group(MsnUserList *userlist, MsnGroup *group) 483 msn_userlist_remove_group(MsnUserList *userlist, MsnGroup *group)
513 { 484 {
603 msn_group_destroy(group); 574 msn_group_destroy(group);
604 } 575 }
605 } 576 }
606 577
607 void 578 void
608 msn_userlist_rem_buddy(MsnUserList *userlist, 579 msn_userlist_rem_buddy(MsnUserList *userlist, const char *who)
609 const char *who, int list_id, const char *group_name) 580 {
581 MsnUser *user = NULL;
582
583 g_return_if_fail(userlist != NULL);
584 g_return_if_fail(userlist->session != NULL);
585 g_return_if_fail(userlist->session->contact != NULL);
586 g_return_if_fail(who != NULL);
587
588 user = msn_userlist_find_user(userlist, who);
589
590 msn_userlist_rem_buddy_from_list(userlist, who, MSN_LIST_FL);
591
592 /* delete the contact from address book via soap action */
593 if (user != NULL) {
594 msn_delete_contact(userlist->session->contact, user->uid);
595 }
596 }
597
598 void
599 msn_userlist_rem_buddy_from_list(MsnUserList *userlist, const char *who,
600 MsnListId list_id)
610 { 601 {
611 MsnUser *user; 602 MsnUser *user;
612 const char *group_id; 603 const gchar *list;
613 const char *list; 604
614
615 user = msn_userlist_find_user(userlist, who); 605 user = msn_userlist_find_user(userlist, who);
616 606
617 g_return_if_fail(user != NULL); 607 g_return_if_fail(user != NULL);
618 608
619 /*delete the contact from address book via soap action*/ 609 if ( !msn_userlist_user_is_in_list(user, list_id)) {
620 msn_delete_contact(userlist->session->contact,user->uid);
621
622 group_id = NULL;
623
624 if (group_name != NULL)
625 {
626 group_id = msn_userlist_find_group_id(userlist, group_name);
627
628 if (group_id == NULL)
629 {
630 /* Whoa, there is no such group. */
631 purple_debug_error("msn", "Group doesn't exist: %s\n", group_name);
632 return;
633 }
634 }
635
636 /* First we're going to check if not there. */
637 if (!(user_is_there(user, list_id, group_id)))
638 {
639 list = lists[list_id]; 610 list = lists[list_id];
640 purple_debug_error("msn", "User '%s' is not there: %s\n", 611 purple_debug_info("MSN Userlist", "User %s is not in list %s, not removing.\n", who, list);
641 who, list);
642 return; 612 return;
643 } 613 }
644 614
645 /* Then request the rem to the server. */ 615 msn_notification_rem_buddy_from_list(userlist->session->notification, list_id, who);
646 list = lists[list_id];
647
648 msn_notification_rem_buddy(userlist->session->notification, list, who, group_id);
649 } 616 }
650 617
651 /*add buddy*/ 618 /*add buddy*/
652 void 619 void
653 msn_userlist_add_buddy(MsnUserList *userlist, 620 msn_userlist_add_buddy(MsnUserList *userlist, const char *who,
654 const char *who, int list_id, 621 const char *group_name)
655 const char *group_name)
656 { 622 {
657 MsnUser *user; 623 MsnUser *user;
658 const char *group_id; 624 MsnCallbackState *state = NULL;
659 const char *list; 625 const char *group_id = NULL, *new_group_name;
660 const char *store_name; 626
661 627 new_group_name = group_name == NULL ? MSN_INDIVIDUALS_GROUP_NAME : group_name;
662 purple_debug_info("MSNP14", "userlist add buddy,name:{%s},group:{%s}\n",who ,group_name); 628
663 group_id = NULL; 629 g_return_if_fail(userlist != NULL);
630 g_return_if_fail(userlist->session != NULL);
631
632
633 purple_debug_info("MSN Userlist", "Add user: %s to group: %s\n", who, new_group_name);
634
635 state = msn_callback_state_new();
636 msn_callback_state_set_who(state, who);
637 msn_callback_state_set_new_group_name(state, new_group_name);
664 638
665 if (!purple_email_is_valid(who)) 639 if (!purple_email_is_valid(who))
666 { 640 {
667 /* only notify the user about problems adding to the friends list 641 /* only notify the user about problems adding to the friends list
668 * maybe we should do something else for other lists, but it probably 642 * maybe we should do something else for other lists, but it probably
669 * won't cause too many problems if we just ignore it */ 643 * won't cause too many problems if we just ignore it */
670 if (list_id == MSN_LIST_FL) 644
671 { 645 char *str = g_strdup_printf(_("Unable to add \"%s\"."), who);
672 char *str = g_strdup_printf(_("Unable to add \"%s\"."), who); 646
673 purple_notify_error(NULL, NULL, str, 647 purple_notify_error(NULL, NULL, str,
674 _("The screen name specified is invalid.")); 648 _("The screen name specified is invalid."));
675 g_free(str); 649 g_free(str);
676 }
677 650
678 return; 651 return;
679 } 652 }
680 653
681 if (group_name != NULL) 654 group_id = msn_userlist_find_group_id(userlist, new_group_name);
682 { 655
683 group_id = msn_userlist_find_group_id(userlist, group_name); 656 if (group_id == NULL)
684 657 {
685 if (group_id == NULL) 658 /* Whoa, we must add that group first. */
686 { 659 purple_debug_info("MSN Userlist", "Adding user %s to a new group, creating group %s first\n", who, new_group_name);
687 /* Whoa, we must add that group first. */ 660
688 msn_request_add_group(userlist, who, NULL, group_name); 661 msn_callback_state_set_action(state, MSN_ADD_BUDDY);
662
663 msn_add_group(userlist->session, state, new_group_name);
664 return;
665 } else {
666 msn_callback_state_set_guid(state, group_id);
667 }
668
669
670 user = msn_userlist_find_add_user(userlist, who, who);
671
672 if ( msn_userlist_user_is_in_list(user, MSN_LIST_FL) ) {
673
674 purple_debug_info("MSN Userlist", "User %s already exists\n", who);
675
676 msn_userlist_rem_buddy_from_list(userlist, who, MSN_LIST_BL);
677
678 if (msn_userlist_user_is_in_group(user, group_id)) {
679 purple_debug_info("MSN Userlist", "User %s is already in group %s, returning\n", who, new_group_name);
689 return; 680 return;
690 } 681 }
691 } 682 }
692 683
693 /* XXX: using _add_user here may not be correct (should add them in the 684 purple_debug_info("MSN Userlist", "Adding user: %s to group id: %s\n", who, group_id);
694 ACK to the ADL command, and we might also want to make sure the user's groups 685
695 are correct. but for now we need to make sure they exist early enough that 686 msn_callback_state_set_action(state, MSN_ADD_BUDDY);
696 the ILN command doesn't screw us up */ 687
688 /* Add contact in the Contact server with a SOAP request and if
689 successful, send ADL with MSN_LIST_AL and MSN_LIST_FL and a FQY */
690 msn_add_contact_to_group(userlist->session->contact, state, who, group_id);
691 }
692
693 void
694 msn_userlist_add_buddy_to_list(MsnUserList *userlist, const char *who,
695 MsnListId list_id)
696 {
697 MsnUser *user = NULL;
698 const gchar *list;
699 MsnListOp list_op = 1 << list_id;
700
701 g_return_if_fail(userlist != NULL);
702
697 user = msn_userlist_find_add_user(userlist, who, who); 703 user = msn_userlist_find_add_user(userlist, who, who);
698 704
699 /* First we're going to check if it's already there. */ 705 /* First we're going to check if it's already there. */
700 if (user_is_there(user, list_id, group_id)) 706 if (msn_userlist_user_is_in_list(user, list_id))
701 { 707 {
702 list = lists[list_id]; 708 list = lists[list_id];
703 purple_debug_error("msn", "User '%s' is already there: %s\n", who, list); 709 purple_debug_info("MSN Userlist", "User '%s' is already in list: %s\n", who, list);
704 return; 710 return;
705 } 711 }
706 712
707 store_name = (user != NULL) ? get_store_name(user) : who; 713 //store_name = (user != NULL) ? get_store_name(user) : who;
708 714
709 purple_debug_info("MSNCL", "store_name = %s\n",store_name); 715 //purple_debug_info("MSN Userlist", "store_name = %s\n", store_name);
710 716
711 /* XXX: see XXX above, this should really be done when we get the response from 717 /* XXX: see XXX above, this should really be done when we get the response from
712 the server */ 718 the server */
713 msn_user_set_op(user, list_id); 719
714 720 msn_user_set_op(user, list_op);
715 /* Then request the add to the server. */ 721
716 list = lists[list_id]; 722 msn_notification_add_buddy_to_list(userlist->session->notification, list_id, who);
717 723 }
718 purple_debug_info("MSNP14", "Add user: %s to group id: %s\n",store_name ,group_id); 724
719 msn_add_contact(userlist->session->contact,who,group_id); 725 gboolean
720 msn_notification_add_buddy(userlist->session->notification, list, who, 726 msn_userlist_add_buddy_to_group(MsnUserList *userlist, const char *who,
721 store_name, group_id); 727 const char *group_name)
728 {
729 MsnUser *user;
730 gchar * group_id;
731
732 g_return_val_if_fail(userlist != NULL, FALSE);
733 g_return_val_if_fail(group_name != NULL, FALSE);
734 g_return_val_if_fail(who != NULL, FALSE);
735
736 purple_debug_info("MSN Userlist","Adding buddy with passport %s to group %s\n", who, group_name);
737
738 if ( (group_id = (gchar *)msn_userlist_find_group_id(userlist, group_name)) == NULL) {
739 purple_debug_error("MSN Userlist", "Group %s has no guid!\n", group_name);
740 return FALSE;
741 }
742
743 if ( (user = msn_userlist_find_user(userlist, who)) == NULL) {
744 purple_debug_error("MSN Userlist", "User %s not found!", who);
745 return FALSE;
746 }
747
748 msn_user_add_group_id(user, group_id);
749
750 return TRUE;
751 }
752
753
754 gboolean
755 msn_userlist_rem_buddy_from_group(MsnUserList *userlist, const char *who,
756 const char *group_name)
757 {
758 const gchar * group_id;
759 MsnUser *user;
760
761 g_return_val_if_fail(userlist != NULL, FALSE);
762 g_return_val_if_fail(group_name != NULL, FALSE);
763 g_return_val_if_fail(who != NULL, FALSE);
764
765 purple_debug_info("MSN Userlist","Removing buddy with passport %s from group %s\n", who, group_name);
766
767 if ( (group_id = msn_userlist_find_group_id(userlist, group_name)) == NULL) {
768 purple_debug_error("MSN Userlist", "Group %s has no guid!\n", group_name);
769 return FALSE;
770 }
771
772 if ( (user = msn_userlist_find_user(userlist, who)) == NULL) {
773 purple_debug_error("MSN Userlist", "User %s not found!", who);
774 return FALSE;
775 }
776
777 msn_user_remove_group_id(user, group_id);
778
779 return TRUE;
722 } 780 }
723 781
724 void 782 void
725 msn_userlist_move_buddy(MsnUserList *userlist, const char *who, 783 msn_userlist_move_buddy(MsnUserList *userlist, const char *who,
726 const char *old_group_name, const char *new_group_name) 784 const char *old_group_name, const char *new_group_name)
727 { 785 {
728 const char *new_group_id; 786 const char *new_group_id;
787 MsnCallbackState *state = msn_callback_state_new();
788
789 g_return_if_fail(userlist != NULL);
790 g_return_if_fail(userlist->session != NULL);
791 g_return_if_fail(userlist->session->contact != NULL);
792
793 msn_callback_state_set_who(state, who);
794 msn_callback_state_set_action(state, MSN_MOVE_BUDDY);
795 msn_callback_state_set_old_group_name(state, old_group_name);
796 msn_callback_state_set_new_group_name(state, new_group_name);
729 797
730 new_group_id = msn_userlist_find_group_id(userlist, new_group_name); 798 new_group_id = msn_userlist_find_group_id(userlist, new_group_name);
731 799
732 if (new_group_id == NULL) 800 if (new_group_id == NULL)
733 { 801 {
734 msn_request_add_group(userlist, who, old_group_name, new_group_name); 802 msn_add_group(userlist->session, state, new_group_name);
735 return; 803 return;
736 } 804 }
737 805
738 msn_userlist_add_buddy(userlist, who, MSN_LIST_FL, new_group_name); 806 /* add the contact to the new group, and remove it from the old one in
739 msn_userlist_rem_buddy(userlist, who, MSN_LIST_FL, old_group_name); 807 * the callback
808 */
809 msn_add_contact_to_group(userlist->session->contact, state, who, new_group_id);
740 } 810 }
741 811
742 /*load userlist from the Blist file cache*/ 812 /*load userlist from the Blist file cache*/
743 void 813 void
744 msn_userlist_load(MsnSession *session) 814 msn_userlist_load(MsnSession *session)