comparison libpurple/protocols/myspace/myspace.c @ 17312:962edb051146

Use msim_postprocess_outgoing() to resolve uid for adding and deleting buddies. Adding or deleting buddies still results in an "invalid session key" message from server, but at least the username is correctly resolved to userid now.
author Jeffrey Connelly <jaconnel@calpoly.edu>
date Wed, 06 Jun 2007 04:38:03 +0000
parents 3720176bdac6
children 7a0061f4845d
comparison
equal deleted inserted replaced
17311:3720176bdac6 17312:962edb051146
573 const char *message, PurpleMessageFlags flags) 573 const char *message, PurpleMessageFlags flags)
574 { 574 {
575 MsimSession *session; 575 MsimSession *session;
576 MsimMessage *msg; 576 MsimMessage *msg;
577 const char *from_username = gc->account->username; 577 const char *from_username = gc->account->username;
578 int rc;
578 579
579 g_return_val_if_fail(gc != NULL, 0); 580 g_return_val_if_fail(gc != NULL, 0);
580 g_return_val_if_fail(who != NULL, 0); 581 g_return_val_if_fail(who != NULL, 0);
581 g_return_val_if_fail(message != NULL, 0); 582 g_return_val_if_fail(message != NULL, 0);
582 583
601 /* Return 1 to have Purple show this IM as being sent, 0 to not. I always 602 /* Return 1 to have Purple show this IM as being sent, 0 to not. I always
602 * return 1 even if the message could not be sent, since I don't know if 603 * return 1 even if the message could not be sent, since I don't know if
603 * it has failed yet--because the IM is only sent after the userid is 604 * it has failed yet--because the IM is only sent after the userid is
604 * retrieved from the server (which happens after this function returns). 605 * retrieved from the server (which happens after this function returns).
605 */ 606 */
606 return 1; 607 rc = 1;
607 } else { 608 } else {
608 return -1; 609 rc = -1;
609 } 610 }
611 msim_msg_free(msg);
612
613 return rc;
610 614
611 /* 615 /*
612 * TODO: In MySpace, you login with your email address, but don't talk to other 616 * TODO: In MySpace, you login with your email address, but don't talk to other
613 * users using their email address. So there is currently an asymmetry in the 617 * users using their email address. So there is currently an asymmetry in the
614 * IM windows when using this plugin: 618 * IM windows when using this plugin:
618 * you@example.com: just coding a prpl 622 * you@example.com: just coding a prpl
619 * 623 *
620 * TODO: Make the sent IM's appear as from the user's username, instead of 624 * TODO: Make the sent IM's appear as from the user's username, instead of
621 * their email address. Purple uses the login (in MSIM, the email)--change this. 625 * their email address. Purple uses the login (in MSIM, the email)--change this.
622 */ 626 */
623 return 1;
624 } 627 }
625 628
626 /** 629 /**
627 * Handle an incoming instant message. 630 * Handle an incoming instant message.
628 * 631 *
1076 1079
1077 /** Add a buddy to user's buddy list. TODO: make work. Should receive statuses from added buddy. */ 1080 /** Add a buddy to user's buddy list. TODO: make work. Should receive statuses from added buddy. */
1078 void msim_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) 1081 void msim_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group)
1079 { 1082 {
1080 MsimSession *session; 1083 MsimSession *session;
1084 MsimMessage *msg;
1081 1085
1082 session = (MsimSession *)gc->proto_data; 1086 session = (MsimSession *)gc->proto_data;
1083 purple_debug_info("msim", "msim_add_buddy: want to add %s to %s\n", buddy->name, 1087 purple_debug_info("msim", "msim_add_buddy: want to add %s to %s\n", buddy->name,
1084 group ? group->name : "(no group)"); 1088 group ? group->name : "(no group)");
1085 1089
1086 if (!msim_send(session, 1090 msg = msim_msg_new(TRUE,
1087 "addbuddy", MSIM_TYPE_BOOLEAN, TRUE, 1091 "addbuddy", MSIM_TYPE_BOOLEAN, TRUE,
1088 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, 1092 "sesskey", MSIM_TYPE_INTEGER, session->sesskey,
1089 /* Currently only allow numeric ID. TODO: Lookup username/email to uid. */ 1093 /* "newprofileid" will be inserted here with uid. */
1090 "newprofileid", MSIM_TYPE_STRING, g_strdup(buddy->name),
1091 "reason", MSIM_TYPE_STRING, g_strdup(""), 1094 "reason", MSIM_TYPE_STRING, g_strdup(""),
1092 NULL)) 1095 NULL);
1096
1097 if (!msim_postprocess_outgoing(session, msg, buddy->name, "newprofileid", "reason"))
1093 { 1098 {
1094 purple_notify_error(NULL, NULL, _("Failed to add buddy"), _("'addbuddy' command failed.")); 1099 purple_notify_error(NULL, NULL, _("Failed to add buddy"), _("'addbuddy' command failed."));
1100 msim_msg_free(msg);
1095 return; 1101 return;
1096 } 1102 }
1103 msim_msg_free(msg);
1097 1104
1098 /* TODO: update blocklist */ 1105 /* TODO: update blocklist */
1099 1106
1107 #if 0
1108 /* TODO */
1100 if (!msim_send(session, 1109 if (!msim_send(session,
1101 "persist", MSIM_TYPE_INTEGER, 1, 1110 "persist", MSIM_TYPE_INTEGER, 1,
1102 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, 1111 "sesskey", MSIM_TYPE_INTEGER, session->sesskey,
1103 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_PUT, 1112 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_PUT,
1104 "dsn", MSIM_TYPE_INTEGER, MC_CONTACT_INFO_DSN, 1113 "dsn", MSIM_TYPE_INTEGER, MC_CONTACT_INFO_DSN,
1116 NULL)) 1125 NULL))
1117 { 1126 {
1118 purple_notify_error(NULL, NULL, _("Failed to add buddy"), _("persist command failed")); 1127 purple_notify_error(NULL, NULL, _("Failed to add buddy"), _("persist command failed"));
1119 return; 1128 return;
1120 } 1129 }
1130 #endif
1121 } 1131 }
1122 1132
1123 /* Callback for msim_postprocess_outgoing() to add a uid field, after resolving username/email. */ 1133 /* Callback for msim_postprocess_outgoing() to add a uid field, after resolving username/email. */
1124 void msim_postprocess_outgoing_cb(MsimSession *session, MsimMessage *userinfo, gpointer data) 1134 void msim_postprocess_outgoing_cb(MsimSession *session, MsimMessage *userinfo, gpointer data)
1125 { 1135 {
1143 1153
1144 /* Insert into outgoing message. */ 1154 /* Insert into outgoing message. */
1145 uid_field_name = msim_msg_get_string(msg, "_uid_field_name"); 1155 uid_field_name = msim_msg_get_string(msg, "_uid_field_name");
1146 uid_before = msim_msg_get_string(msg, "_uid_before"); 1156 uid_before = msim_msg_get_string(msg, "_uid_before");
1147 1157
1148 msg = msim_msg_insert_before(msg, uid_field_name, uid_before, MSIM_TYPE_STRING, uid); 1158 msg = msim_msg_insert_before(msg, uid_before, uid_field_name, MSIM_TYPE_STRING, uid);
1149 1159
1160 /* Send */
1161 if (!msim_msg_send(session, msg))
1162 {
1163 purple_debug_info("msim", "msim_postprocess_outgoing_cb: sending failed for message: %s\n", msg);
1164 }
1165
1166 /* Free field names AFTER sending message, because MsimMessage does NOT copy
1167 * field names - instead, treats them as static strings (which they usually are).
1168 */
1150 g_free(uid_field_name); 1169 g_free(uid_field_name);
1151 g_free(uid_before); 1170 g_free(uid_before);
1152 1171
1153 /* Send */ 1172 //msim_msg_free(msg);
1154 g_return_if_fail(msim_msg_send(session, msg));
1155 } 1173 }
1156 1174
1157 /** Postprocess and send a message. 1175 /** Postprocess and send a message.
1158 * 1176 *
1159 * @param session 1177 * @param session
1160 * @param msg Message to postprocess. 1178 * @param msg Message to postprocess. Will NOT be freed.
1161 * @param username Username to resolve. Assumed to be a static string (will not be freed or copied). 1179 * @param username Username to resolve. Assumed to be a static string (will not be freed or copied).
1162 * @param uid_field_name Name of new field to add, containing uid of username. Static string. 1180 * @param uid_field_name Name of new field to add, containing uid of username. Static string.
1163 * @param uid_before Name of existing field to insert username field before. Static string. 1181 * @param uid_before Name of existing field to insert username field before. Static string.
1164 * 1182 *
1165 * @return Postprocessed message. 1183 * @return Postprocessed message.
1167 gboolean msim_postprocess_outgoing(MsimSession *session, MsimMessage *msg, gchar *username, 1185 gboolean msim_postprocess_outgoing(MsimSession *session, MsimMessage *msg, gchar *username,
1168 gchar *uid_field_name, gchar *uid_before) 1186 gchar *uid_field_name, gchar *uid_before)
1169 { 1187 {
1170 PurpleBuddy *buddy; 1188 PurpleBuddy *buddy;
1171 guint uid; 1189 guint uid;
1190 gboolean rc;
1172 1191
1173 /* Store information for msim_postprocess_outgoing_cb(). */ 1192 /* Store information for msim_postprocess_outgoing_cb(). */
1174 purple_debug_info("msim", "msim_postprocess_outgoing(u=%s,ufn=%s,ub=%s)\n", 1193 purple_debug_info("msim", "msim_postprocess_outgoing(u=%s,ufn=%s,ub=%s)\n",
1175 username, uid_field_name, uid_before); 1194 username, uid_field_name, uid_before);
1176 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, g_strdup(username)); 1195 msg = msim_msg_append(msg, "_username", MSIM_TYPE_STRING, g_strdup(username));
1193 1212
1194 if (!buddy || !uid) 1213 if (!buddy || !uid)
1195 { 1214 {
1196 purple_debug_info("msim", ">>> msim_postprocess_outgoing: couldn't find username %s in blist\n", 1215 purple_debug_info("msim", ">>> msim_postprocess_outgoing: couldn't find username %s in blist\n",
1197 username); 1216 username);
1217 msim_msg_dump("msim_postprocess_outgoing - scheduling lookup, msg=%s\n", msg);
1218 /* TODO: where is cloned message freed? Should be in _cb. */
1198 msim_lookup_user(session, username, msim_postprocess_outgoing_cb, msim_msg_clone(msg)); 1219 msim_lookup_user(session, username, msim_postprocess_outgoing_cb, msim_msg_clone(msg));
1199 return TRUE; /* not sure of status yet - haven't sent! */ 1220 return TRUE; /* not sure of status yet - haven't sent! */
1200 } 1221 }
1201 } 1222 }
1202 1223
1203 /* Already have uid, insert it and send msg. */ 1224 /* Already have uid, insert it and send msg immediately. */
1204 purple_debug_info("msim", "msim_postprocess_outgoing: found username %s has uid %d\n", 1225 purple_debug_info("msim", "msim_postprocess_outgoing: found username %s has uid %d\n",
1205 username, uid); 1226 username, uid);
1206 1227
1207 msg = msim_msg_insert_before(msg, uid_before, uid_field_name, MSIM_TYPE_INTEGER, GUINT_TO_POINTER(uid)); 1228 msg = msim_msg_insert_before(msg, uid_before, uid_field_name, MSIM_TYPE_INTEGER, GUINT_TO_POINTER(uid));
1208 1229
1209 return msim_msg_send(session, msg); 1230 rc = msim_msg_send(session, msg);
1231
1232 //msim_msg_free(msg);
1233
1234 return rc;
1210 } 1235 }
1211 1236
1212 /** Remove a buddy from the user's buddy list. */ 1237 /** Remove a buddy from the user's buddy list. */
1213 void msim_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) 1238 void msim_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group)
1214 { 1239 {
1215 MsimSession *session; 1240 MsimSession *session;
1241 MsimMessage *msg;
1216 1242
1217 session = (MsimSession *)gc->proto_data; 1243 session = (MsimSession *)gc->proto_data;
1218 1244
1219 if (!msim_send(session, 1245 msg = msim_msg_new(FALSE,
1220 "delbuddy", MSIM_TYPE_BOOLEAN, TRUE, 1246 "delbuddy", MSIM_TYPE_BOOLEAN, TRUE,
1221 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, 1247 "sesskey", MSIM_TYPE_INTEGER, session->sesskey,
1222 /* TODO: Lookup username/email to uid, currently on userid. */ 1248 /* 'delprofileid' with uid will be inserted here. */
1223 "delprofileid", MSIM_TYPE_STRING, g_strdup(buddy->name), 1249 NULL);
1224 NULL)) 1250 /* TODO: free msg */
1251 if (!msim_postprocess_outgoing(session, msg, buddy->name, "delprofileid", NULL))
1225 { 1252 {
1226 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("'delbuddy' command failed")); 1253 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("'delbuddy' command failed"));
1227 return; 1254 return;
1228 } 1255 }
1229 1256
1257
1258 /* TODO */
1259 #if 0
1230 if (!msim_send(session, 1260 if (!msim_send(session,
1231 "persist", MSIM_TYPE_INTEGER, 1, 1261 "persist", MSIM_TYPE_INTEGER, 1,
1232 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, 1262 "sesskey", MSIM_TYPE_INTEGER, session->sesskey,
1233 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_DELETE, 1263 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_DELETE,
1234 "dsn", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_DSN, 1264 "dsn", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_DSN,
1239 NULL)) 1269 NULL))
1240 { 1270 {
1241 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("persist command failed")); 1271 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("persist command failed"));
1242 return; 1272 return;
1243 } 1273 }
1274 #endif
1244 1275
1245 /* TODO: update blocklist */ 1276 /* TODO: update blocklist */
1246 } 1277 }
1247 1278
1248 1279
1528 g_return_if_fail(user != NULL); 1559 g_return_if_fail(user != NULL);
1529 g_return_if_fail(cb != NULL); 1560 g_return_if_fail(cb != NULL);
1530 1561
1531 purple_debug_info("msim", "msim_lookup_userid: " 1562 purple_debug_info("msim", "msim_lookup_userid: "
1532 "asynchronously looking up <%s>\n", user); 1563 "asynchronously looking up <%s>\n", user);
1564
1565 msim_msg_dump("msim_lookup_user: data=%s\n", (MsimMessage *)data);
1533 1566
1534 /* TODO: check if this user's info was cached and fresh; if so return immediately */ 1567 /* TODO: check if this user's info was cached and fresh; if so return immediately */
1535 #if 0 1568 #if 0
1536 /* If already know userid, then call callback immediately */ 1569 /* If already know userid, then call callback immediately */
1537 cached_userid = g_hash_table_lookup(session->userid_cache, who); 1570 cached_userid = g_hash_table_lookup(session->userid_cache, who);