comparison libpurple/protocols/myspace/myspace.c @ 17656:61f0b36b9891

Retrieve information for profile ("Get Info") from server, instead of from cached buddy list information. Allows getting info for not only online users, but also offline users, and users not on buddy list (by username or uid).
author Jeffrey Connelly <jaconnel@calpoly.edu>
date Thu, 21 Jun 2007 05:50:41 +0000
parents 60f614824207
children f903418147f6
comparison
equal deleted inserted replaced
17655:60f614824207 17656:61f0b36b9891
775 purple_debug_info("msim", "msim_send_typing(%s): %d (%s)\n", name, state, typing_str); 775 purple_debug_info("msim", "msim_send_typing(%s): %d (%s)\n", name, state, typing_str);
776 msim_send_bm(session, name, typing_str, MSIM_BM_ACTION); 776 msim_send_bm(session, name, typing_str, MSIM_BM_ACTION);
777 return 0; 777 return 0;
778 } 778 }
779 779
780 /** Retrieve a user's profile. */ 780 /** Callback for msim_get_info(), for when user info is received. */
781 void msim_get_info(PurpleConnection *gc, const gchar *name) 781 void msim_get_info_cb(MsimSession *session, MsimMessage *user_info_msg, gpointer data)
782 { 782 {
783 GHashTable *body;
784 gchar *body_str;
785 MsimMessage *msg;
786 gchar *user;
783 PurpleNotifyUserInfo *user_info; 787 PurpleNotifyUserInfo *user_info;
784 PurpleBuddy *buddy; 788 PurpleBuddy *buddy;
785 MsimSession *session; 789 gchar *song;
786 790
787 session = (MsimSession *)gc->proto_data; 791
792 /* Get user{name,id} from msim_get_info, passed as an MsimMessage for orthogonality. */
793 msg = (MsimMessage *)data;
794 user = g_strdup(msim_msg_get_string(msg, "user"));
795 purple_debug_info("msim", "msim_get_info_cb: got for user: %s\n", user);
796 msim_msg_free(msg);
797
798
799 body_str = msim_msg_get_string(user_info_msg, "body");
800 body = msim_parse_body(body_str);
801 g_free(body_str);
802
803 buddy = purple_find_buddy(session->account, user);
804 /* Note: don't assume buddy is non-NULL; will be if lookup random user not on blist. */
788 805
789 user_info = purple_notify_user_info_new(); 806 user_info = purple_notify_user_info_new();
790 807
791 buddy = purple_find_buddy(session->account, name);
792 if (!buddy)
793 {
794 /* TODO: profile of buddies not on blist! Wouldn't be too hard,
795 * just have to schedule a lookup, and when receive reply,
796 * call purple_notify_userinfo() etc.
797 */
798 purple_notify_user_info_add_pair(user_info, NULL,
799 "Sorry, currently user information can only be retrieved from users on your buddy list.");
800 purple_notify_userinfo(gc, name, user_info, NULL, NULL);
801 purple_notify_user_info_destroy(user_info);
802 return;
803 }
804
805
806 /* Identification */ 808 /* Identification */
807 purple_notify_user_info_add_pair(user_info, "User Name", 809 purple_notify_user_info_add_pair(user_info, _("User"), user);
808 purple_blist_node_get_string(&buddy->node, "UserName")); 810
809 811 /* note: g_hash_table_lookup does not create a new string! */
810 812 purple_notify_user_info_add_pair(user_info, _("User ID"),
811 purple_notify_user_info_add_pair(user_info, "User ID", 813 g_strdup(g_hash_table_lookup(body, "UserID")));
812 g_strdup_printf("%d", purple_blist_node_get_int(&buddy->node, "UserID")));
813
814 purple_notify_user_info_add_pair(user_info, "Display Name",
815 purple_blist_node_get_string(&buddy->node, "DisplayName"));
816
817 814
818 /* a/s/l...the vitals */ 815 /* a/s/l...the vitals */
819 purple_notify_user_info_add_pair(user_info, "Age", 816 purple_notify_user_info_add_pair(user_info, _("Age"),
820 g_strdup_printf("%d", purple_blist_node_get_int(&buddy->node, "Age"))); 817 g_strdup(g_hash_table_lookup(body, "Age")));
821 818
822 purple_notify_user_info_add_pair(user_info, "Gender", 819 purple_notify_user_info_add_pair(user_info, _("Gender"),
823 purple_blist_node_get_string(&buddy->node, "Gender")); 820 g_strdup(g_hash_table_lookup(body, "Gender")));
824 821
825 purple_notify_user_info_add_pair(user_info, "Location", 822 purple_notify_user_info_add_pair(user_info, _("Location"),
826 purple_blist_node_get_string(&buddy->node, "Location")); 823 g_strdup(g_hash_table_lookup(body, "Location")));
827 824
828 /* Other information */ 825 /* Other information */
829 if (purple_blist_node_get_string(&buddy->node, "Headline")) 826
827 /* Headline comes from buddy status messages */
828 if (buddy && purple_blist_node_get_string(&buddy->node, "Headline"))
830 purple_notify_user_info_add_pair(user_info, "Headline", 829 purple_notify_user_info_add_pair(user_info, "Headline",
831 purple_blist_node_get_string(&buddy->node, "Headline")); 830 purple_blist_node_get_string(&buddy->node, "Headline"));
832 831
833 purple_notify_user_info_add_pair(user_info, "Song", 832 song = g_strdup_printf("%s - %s",
834 g_strdup_printf("%s - %s", 833 (gchar *)g_hash_table_lookup(body, "BandName"),
835 purple_blist_node_get_string(&buddy->node, "BandName"), 834 (gchar *)g_hash_table_lookup(body, "SongName"));
836 purple_blist_node_get_string(&buddy->node, "SongName"))); 835
837 836
838 purple_notify_user_info_add_pair(user_info, "Total Friends", 837 purple_notify_user_info_add_pair(user_info, _("Song"), song);
839 g_strdup_printf("%d", purple_blist_node_get_int(&buddy->node, "TotalFriends"))); 838 /* Do not free song - used by user_info. */
840 839
841 840 /* Total friends only available if looked up by uid, not username. */
842 purple_notify_userinfo(gc, name, user_info, NULL, NULL); 841 if (g_hash_table_lookup(body, "TotalFriends"))
843 purple_notify_user_info_destroy(user_info); 842 purple_notify_user_info_add_pair(user_info, _("Total Friends"),
843 g_strdup(g_hash_table_lookup(body, "TotalFriends")));
844
845 purple_notify_userinfo(session->gc, user, user_info, NULL, NULL);
846 purple_debug_info("msim", "msim_get_info_cb: username=%s\n", user);
847 //purple_notify_user_info_destroy(user_info);
848 /* Do not free username, since it will be used by user_info. */
849
850 //g_hash_table_destroy(body);
851 }
852
853 /** Retrieve a user's profile. */
854 void msim_get_info(PurpleConnection *gc, const gchar *user)
855 {
856 PurpleBuddy *buddy;
857 MsimSession *session;
858 guint uid;
859 gchar *user_to_lookup;
860 MsimMessage *user_msg;
861
862 session = (MsimSession *)gc->proto_data;
863
864 /* Obtain uid of buddy. */
865 buddy = purple_find_buddy(session->account, user);
866 if (buddy)
867 {
868 uid = purple_blist_node_get_int(&buddy->node, "UserID");
869 if (!uid)
870 {
871 PurpleNotifyUserInfo *user_info;
872
873 user_info = purple_notify_user_info_new();
874 purple_notify_user_info_add_pair(user_info, NULL,
875 _("This buddy appears to not have a userid stored in the buddy list, can't look up. Is the user really on the buddy list?"));
876
877 purple_notify_userinfo(session->gc, user, user_info, NULL, NULL);
878 purple_notify_user_info_destroy(user_info);
879 return;
880 }
881
882 user_to_lookup = g_strdup_printf("%d", uid);
883 } else {
884
885 /* Looking up buddy not on blist. Lookup by whatever user entered. */
886 user_to_lookup = g_strdup(user);
887 }
888
889 /* Pass the username to msim_get_info_cb(), because since we lookup
890 * by userid, the userinfo message will only contain the uid (not
891 * the username).
892 */
893 user_msg = msim_msg_new(TRUE,
894 "user", MSIM_TYPE_STRING, g_strdup(user),
895 NULL);
896 purple_debug_info("msim", "msim_get_info, setting up lookup, user=%s\n", user);
897
898 msim_lookup_user(session, user_to_lookup, msim_get_info_cb, user_msg);
899
900 g_free(user_to_lookup);
844 } 901 }
845 902
846 /** After a uid is resolved to username, tag it with the username and submit for processing. 903 /** After a uid is resolved to username, tag it with the username and submit for processing.
847 * 904 *
848 * @param session 905 * @param session
999 username); 1056 username);
1000 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, g_strdup(username)); 1057 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, g_strdup(username));
1001 return msim_process(session, msg); 1058 return msim_process(session, msg);
1002 1059
1003 } else { 1060 } else {
1061 gchar *from;
1062
1004 /* Send lookup request. */ 1063 /* Send lookup request. */
1005 /* XXX: where is msim_msg_get_string() freed? make _strdup and _nonstrdup. */ 1064 /* XXX: where is msim_msg_get_string() freed? make _strdup and _nonstrdup. */
1006 purple_debug_info("msim", "msim_incoming: sending lookup, setting up callback\n"); 1065 purple_debug_info("msim", "msim_incoming: sending lookup, setting up callback\n");
1007 msim_lookup_user(session, msim_msg_get_string(msg, "f"), msim_incoming_resolved, msim_msg_clone(msg)); 1066 from = msim_msg_get_string(msg, "f");
1067 msim_lookup_user(session, from, msim_incoming_resolved, msim_msg_clone(msg));
1068 g_free(from);
1008 1069
1009 /* indeterminate */ 1070 /* indeterminate */
1010 return TRUE; 1071 return TRUE;
1011 } 1072 }
1012 } else { 1073 } else {
1950 2011
1951 /** 2012 /**
1952 * Asynchronously lookup user information, calling callback when receive result. 2013 * Asynchronously lookup user information, calling callback when receive result.
1953 * 2014 *
1954 * @param session 2015 * @param session
1955 * @param user The user id, email address, or username. 2016 * @param user The user id, email address, or username. Not freed.
1956 * @param cb Callback, called with user information when available. 2017 * @param cb Callback, called with user information when available.
1957 * @param data An arbitray data pointer passed to the callback. 2018 * @param data An arbitray data pointer passed to the callback.
1958 */ 2019 */
1959 /* TODO: change to not use callbacks */ 2020 /* TODO: change to not use callbacks */
1960 void msim_lookup_user(MsimSession *session, const gchar *user, MSIM_USER_LOOKUP_CB cb, gpointer data) 2021 void msim_lookup_user(MsimSession *session, const gchar *user, MSIM_USER_LOOKUP_CB cb, gpointer data)
2095 /* Already shown in status text. 2156 /* Already shown in status text.
2096 purple_notify_user_info_add_pair(user_info, "Display Name", 2157 purple_notify_user_info_add_pair(user_info, "Display Name",
2097 purple_blist_node_get_string(&buddy->node, "DisplayName")); */ 2158 purple_blist_node_get_string(&buddy->node, "DisplayName")); */
2098 2159
2099 /* Useful to identify the account the tooltip refers to. Other prpls show this. */ 2160 /* Useful to identify the account the tooltip refers to. Other prpls show this. */
2100 purple_notify_user_info_add_pair(user_info, "Account", 2161 purple_notify_user_info_add_pair(user_info, _("User Name"),
2101 purple_blist_node_get_string(&buddy->node, "UserName")); 2162 (purple_blist_node_get_string(&buddy->node, "UserName")));
2102
2103 2163
2104 /* a/s/l...the vitals */ 2164 /* a/s/l...the vitals */
2105 purple_notify_user_info_add_pair(user_info, "Age", 2165 purple_notify_user_info_add_pair(user_info, _("Age"),
2106 g_strdup_printf("%d", purple_blist_node_get_int(&buddy->node, "Age"))); 2166 g_strdup_printf("%d", purple_blist_node_get_int(&buddy->node, "Age")));
2107 2167
2108 purple_notify_user_info_add_pair(user_info, "Gender", 2168 purple_notify_user_info_add_pair(user_info, _("Gender"),
2109 purple_blist_node_get_string(&buddy->node, "Gender")); 2169 (purple_blist_node_get_string(&buddy->node, "Gender")));
2110 2170
2111 purple_notify_user_info_add_pair(user_info, "Location", 2171 purple_notify_user_info_add_pair(user_info, _("Location"),
2112 purple_blist_node_get_string(&buddy->node, "Location")); 2172 (purple_blist_node_get_string(&buddy->node, "Location")));
2113 2173
2114 /* Other information */ 2174 /* Other information */
2115 if (purple_blist_node_get_string(&buddy->node, "Headline")) 2175 if (purple_blist_node_get_string(&buddy->node, "Headline"))
2116 purple_notify_user_info_add_pair(user_info, "Headline", 2176 purple_notify_user_info_add_pair(user_info, _("Headline"),
2117 purple_blist_node_get_string(&buddy->node, "Headline")); 2177 (purple_blist_node_get_string(&buddy->node, "Headline")));
2118 2178
2119 purple_notify_user_info_add_pair(user_info, "Song", 2179 purple_notify_user_info_add_pair(user_info, _("Song"),
2120 g_strdup_printf("%s - %s", 2180 g_strdup_printf("%s - %s",
2121 purple_blist_node_get_string(&buddy->node, "BandName"), 2181 purple_blist_node_get_string(&buddy->node, "BandName"),
2122 purple_blist_node_get_string(&buddy->node, "SongName"))); 2182 purple_blist_node_get_string(&buddy->node, "SongName")));
2123 2183
2124 purple_notify_user_info_add_pair(user_info, "Total Friends", 2184 purple_notify_user_info_add_pair(user_info, _("Total Friends"),
2125 g_strdup_printf("%d", purple_blist_node_get_int(&buddy->node, "TotalFriends"))); 2185 g_strdup_printf("%d", purple_blist_node_get_int(&buddy->node, "TotalFriends")));
2126 2186
2127 } 2187 }
2128 } 2188 }
2129 2189