Mercurial > pidgin
comparison libpurple/protocols/myspace/myspace.c @ 17316:2aff11bb5e72
Several small changes to work towards a working msim_remove_buddy():
(Note: I'm not there yet)
Change MsimSession.userid to an integer.
Change msim_postprocess_outgoing() to accept a format string in the _uid_field_name field, having "%d" replaced with the userid (instead of adding a new field with the userid). Added so that the "body" field can be postprocessed.
Enable msim_remove_buddy() to send the persistance buddy delete message,
but it does not yet work.
Add msim_new_reply_callback() for setting up a persistance reply callback,
instead of having to manually insert into the hash tables.
Add msim_msg_get_own_uid{,_cb}() to get our own userid.
Rename MG_YOUR_MYSPACE_INFO to MG_OWN_MYSPACE_INFO.
author | Jeffrey Connelly <jaconnel@calpoly.edu> |
---|---|
date | Wed, 13 Jun 2007 21:21:48 +0000 |
parents | d9aaccef8721 |
children | e9dfd3a5d4b6 |
comparison
equal
deleted
inserted
replaced
17315:d9aaccef8721 | 17316:2aff11bb5e72 |
---|---|
258 /* TODO: try other ports if in auto mode, then save | 258 /* TODO: try other ports if in auto mode, then save |
259 * working port and try that first next time. */ | 259 * working port and try that first next time. */ |
260 purple_connection_error(gc, _("Couldn't create socket")); | 260 purple_connection_error(gc, _("Couldn't create socket")); |
261 return; | 261 return; |
262 } | 262 } |
263 | |
264 } | 263 } |
265 /** | 264 /** |
266 * Process a login challenge, sending a response. | 265 * Process a login challenge, sending a response. |
267 * | 266 * |
268 * @param session | 267 * @param session |
941 return msim_login_challenge(session, msg); | 940 return msim_login_challenge(session, msg); |
942 } else if (msim_msg_get(msg, "sesskey")) { | 941 } else if (msim_msg_get(msg, "sesskey")) { |
943 | 942 |
944 purple_connection_update_progress(session->gc, _("Connected"), 3, 4); | 943 purple_connection_update_progress(session->gc, _("Connected"), 3, 4); |
945 | 944 |
946 /* Freed in msim_session_destroy */ | |
947 session->sesskey = msim_msg_get_integer(msg, "sesskey"); | 945 session->sesskey = msim_msg_get_integer(msg, "sesskey"); |
948 purple_debug_info("msim", "SESSKEY=<%d>\n", session->sesskey); | 946 purple_debug_info("msim", "SESSKEY=<%d>\n", session->sesskey); |
949 | 947 |
950 /* Comes with: proof,profileid,userid,uniquenick -- all same values | 948 /* Comes with: proof,profileid,userid,uniquenick -- all same values |
951 * (at least for me). */ | 949 * some of the time, but can vary. */ |
952 /* Freed in msim_session_destroy */ | 950 session->userid = msim_msg_get_integer(msg, "userid"); |
953 session->userid = msim_msg_get_string(msg, "userid"); | |
954 | 951 |
955 purple_connection_set_state(session->gc, PURPLE_CONNECTED); | 952 purple_connection_set_state(session->gc, PURPLE_CONNECTED); |
956 | 953 |
957 return TRUE; | 954 return TRUE; |
958 } else if (msim_msg_get(msg, "bm")) { | 955 } else if (msim_msg_get(msg, "bm")) { |
1274 return; | 1271 return; |
1275 } | 1272 } |
1276 #endif | 1273 #endif |
1277 } | 1274 } |
1278 | 1275 |
1279 /* Callback for msim_postprocess_outgoing() to add a uid field, after resolving username/email. */ | 1276 /** Callback for msim_postprocess_outgoing() to add a userid to a message, and send it (once receiving userid). |
1277 * | |
1278 * @param session | |
1279 * @param userinfo The user information reply message, containing the user ID | |
1280 * @param data The message to postprocess and send. | |
1281 * | |
1282 * The data message should contain these fields: | |
1283 * | |
1284 * _uid_field_name: string, name of field to add with userid from userinfo message | |
1285 * _uid_before: string, name of field before field to insert, or NULL for end | |
1286 * | |
1287 * | |
1288 * If the field named by _uid_field_name already exists, then its string contents will | |
1289 * be treated as a format string, containing a "%d", to be replaced by the userid. | |
1290 * | |
1291 * If the field named by _uid_field_name does not exist, it will be added before the | |
1292 * field named by _uid_before, as an integer, with the userid. | |
1293 */ | |
1280 void msim_postprocess_outgoing_cb(MsimSession *session, MsimMessage *userinfo, gpointer data) | 1294 void msim_postprocess_outgoing_cb(MsimSession *session, MsimMessage *userinfo, gpointer data) |
1281 { | 1295 { |
1282 gchar *body_str; | 1296 gchar *body_str; |
1283 GHashTable *body; | 1297 GHashTable *body; |
1284 gchar *uid, *uid_field_name, *uid_before; | 1298 gchar *uid, *uid_field_name, *uid_before; |
1295 g_free(body_str); | 1309 g_free(body_str); |
1296 | 1310 |
1297 uid = g_strdup(g_hash_table_lookup(body, "UserID")); | 1311 uid = g_strdup(g_hash_table_lookup(body, "UserID")); |
1298 g_hash_table_destroy(body); | 1312 g_hash_table_destroy(body); |
1299 | 1313 |
1300 /* Insert into outgoing message. */ | |
1301 uid_field_name = msim_msg_get_string(msg, "_uid_field_name"); | 1314 uid_field_name = msim_msg_get_string(msg, "_uid_field_name"); |
1302 uid_before = msim_msg_get_string(msg, "_uid_before"); | 1315 uid_before = msim_msg_get_string(msg, "_uid_before"); |
1303 | 1316 |
1304 msg = msim_msg_insert_before(msg, uid_before, uid_field_name, MSIM_TYPE_STRING, uid); | 1317 /* First, check - if the field already exists, treat it as a format string. */ |
1318 if (msim_msg_get(msg, uid_field_name)) | |
1319 { | |
1320 MsimMessageElement *elem; | |
1321 gchar *fmt_string; | |
1322 | |
1323 elem = msim_msg_get(msg, uid_field_name); | |
1324 g_return_if_fail(elem->type == MSIM_TYPE_STRING); | |
1325 | |
1326 /* Get the raw string, not with msim_msg_get_string() since that copies it. | |
1327 * Want the original string so can free it. */ | |
1328 fmt_string = (gchar *)(elem->data); | |
1329 | |
1330 /* Format string: ....%d... */ | |
1331 elem->data = g_strdup_printf(fmt_string, uid); | |
1332 | |
1333 g_free(fmt_string); | |
1334 } else { | |
1335 /* Otherwise, insert new field into outgoing message. */ | |
1336 msg = msim_msg_insert_before(msg, uid_before, uid_field_name, MSIM_TYPE_STRING, uid); | |
1337 } | |
1305 | 1338 |
1306 /* Send */ | 1339 /* Send */ |
1307 if (!msim_msg_send(session, msg)) | 1340 if (!msim_msg_send(session, msg)) |
1308 { | 1341 { |
1309 purple_debug_info("msim", "msim_postprocess_outgoing_cb: sending failed for message: %s\n", msg); | 1342 purple_debug_info("msim", "msim_postprocess_outgoing_cb: sending failed for message: %s\n", msg); |
1383 /** Remove a buddy from the user's buddy list. */ | 1416 /** Remove a buddy from the user's buddy list. */ |
1384 void msim_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) | 1417 void msim_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) |
1385 { | 1418 { |
1386 MsimSession *session; | 1419 MsimSession *session; |
1387 MsimMessage *msg; | 1420 MsimMessage *msg; |
1421 MsimMessage *persist_msg; | |
1388 | 1422 |
1389 session = (MsimSession *)gc->proto_data; | 1423 session = (MsimSession *)gc->proto_data; |
1390 | 1424 |
1391 msg = msim_msg_new(FALSE, | 1425 msg = msim_msg_new(FALSE, |
1392 "delbuddy", MSIM_TYPE_BOOLEAN, TRUE, | 1426 "delbuddy", MSIM_TYPE_BOOLEAN, TRUE, |
1397 if (!msim_postprocess_outgoing(session, msg, buddy->name, "delprofileid", NULL)) | 1431 if (!msim_postprocess_outgoing(session, msg, buddy->name, "delprofileid", NULL)) |
1398 { | 1432 { |
1399 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("'delbuddy' command failed")); | 1433 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("'delbuddy' command failed")); |
1400 return; | 1434 return; |
1401 } | 1435 } |
1402 | 1436 |
1403 | 1437 persist_msg = msim_msg_new(FALSE, |
1404 /* TODO */ | |
1405 #if 0 | |
1406 if (!msim_send(session, | |
1407 "persist", MSIM_TYPE_INTEGER, 1, | 1438 "persist", MSIM_TYPE_INTEGER, 1, |
1408 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, | 1439 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, |
1409 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_DELETE, | 1440 "cmd", MSIM_TYPE_INTEGER, MSIM_CMD_BIT_ACTION | MSIM_CMD_DELETE, |
1410 "dsn", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_DSN, | 1441 "dsn", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_DSN, |
1411 "lid", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_LID, | 1442 "lid", MSIM_TYPE_INTEGER, MD_DELETE_BUDDY_LID, |
1412 "uid", MSIM_TYPE_INTEGER, 42, /* TODO: put YOUR userid here */ | 1443 "uid", MSIM_TYPE_INTEGER, 42, /* TODO: put YOUR userid here */ |
1413 "rid", MSIM_TYPE_INTEGER, session->next_rid++, | 1444 "rid", MSIM_TYPE_INTEGER, session->next_rid++, |
1414 "body", MSIM_TYPE_STRING, g_strdup_printf("ContactID=%s", buddy->name), | 1445 "body", MSIM_TYPE_STRING, g_strdup("ContactID=%d"), |
1415 NULL)) | 1446 NULL); |
1447 | |
1448 /* TODO: free msg */ | |
1449 if (!msim_postprocess_outgoing(session, msg, buddy->name, "body", NULL)) | |
1416 { | 1450 { |
1417 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("persist command failed")); | 1451 purple_notify_error(NULL, NULL, _("Failed to remove buddy"), _("persist command failed")); |
1418 return; | 1452 return; |
1419 } | 1453 } |
1420 #endif | |
1421 | 1454 |
1422 /* TODO: update blocklist */ | 1455 /* TODO: update blocklist */ |
1423 } | 1456 } |
1424 | 1457 |
1425 | 1458 |
1551 /* Clear end of buffer */ | 1584 /* Clear end of buffer */ |
1552 //memset(end, 0, MSIM_READ_BUF_SIZE - (end - session->rxbuf)); | 1585 //memset(end, 0, MSIM_READ_BUF_SIZE - (end - session->rxbuf)); |
1553 } | 1586 } |
1554 } | 1587 } |
1555 | 1588 |
1589 /* Setup a callback, to be called when a reply is received with the returned rid. | |
1590 * | |
1591 * @param cb The callback, an MSIM_USER_LOOKUP_CB. | |
1592 * @param data Arbitrary user data to be passed to callback (probably an MsimMessage *). | |
1593 * | |
1594 * @return The request/reply ID, used to link replies with requests. Put the rid in your request. | |
1595 * | |
1596 * TODO: Make more generic and more specific: | |
1597 * 1) MSIM_USER_LOOKUP_CB - make it for PERSIST_REPLY, not just user lookup | |
1598 * 2) data - make it an MsimMessage? | |
1599 */ | |
1600 guint msim_new_reply_callback(MsimSession *session, MSIM_USER_LOOKUP_CB cb, gpointer data) | |
1601 { | |
1602 guint rid; | |
1603 | |
1604 rid = session->next_rid++; | |
1605 | |
1606 g_hash_table_insert(session->user_lookup_cb, GUINT_TO_POINTER(rid), cb); | |
1607 g_hash_table_insert(session->user_lookup_cb_data, GUINT_TO_POINTER(rid), data); | |
1608 | |
1609 return rid; | |
1610 } | |
1611 | |
1612 /** Process reply to get our own userid. */ | |
1613 void msim_get_own_uid_cb(MsimSession *session, MsimMessage *userinfo, gpointer data) | |
1614 { | |
1615 /* TODO */ | |
1616 msim_msg_dump("msim_get_own_uid_cb: %s\n", userinfo); | |
1617 } | |
1618 | |
1619 /** Request our own userid. */ | |
1620 void msim_get_own_uid(MsimSession *session) | |
1621 { | |
1622 guint rid; | |
1623 | |
1624 rid = msim_new_reply_callback(session, msim_get_own_uid_cb, NULL); | |
1625 | |
1626 g_return_if_fail(msim_send(session, | |
1627 "persist", MSIM_TYPE_INTEGER, 1, | |
1628 "sesskey", MSIM_TYPE_INTEGER, session->sesskey, | |
1629 "cmd", MSIM_TYPE_INTEGER, 1, | |
1630 "dsn", MSIM_TYPE_INTEGER, MG_OWN_MYSPACE_INFO_DSN, | |
1631 "lid", MSIM_TYPE_INTEGER, MG_OWN_MYSPACE_INFO_LID, | |
1632 "rid", MSIM_TYPE_INTEGER, rid, | |
1633 "body", MSIM_TYPE_STRING, g_strdup(""), | |
1634 NULL)); | |
1635 } | |
1636 | |
1637 | |
1556 /** | 1638 /** |
1557 * Callback when connected. Sets up input handlers. | 1639 * Callback when connected. Sets up input handlers. |
1558 * | 1640 * |
1559 * @param data A PurpleConnection pointer. | 1641 * @param data A PurpleConnection pointer. |
1560 * @param source File descriptor. | 1642 * @param source File descriptor. |
1566 MsimSession *session; | 1648 MsimSession *session; |
1567 | 1649 |
1568 g_return_if_fail(data != NULL); | 1650 g_return_if_fail(data != NULL); |
1569 | 1651 |
1570 gc = (PurpleConnection *)data; | 1652 gc = (PurpleConnection *)data; |
1571 session = gc->proto_data; | 1653 session = (MsimSession *)gc->proto_data; |
1572 | 1654 |
1573 if (source < 0) | 1655 if (source < 0) |
1574 { | 1656 { |
1575 purple_connection_error(gc, _("Couldn't connect to host")); | 1657 purple_connection_error(gc, _("Couldn't connect to host")); |
1576 purple_connection_error(gc, g_strdup_printf(_("Couldn't connect to host: %s (%d)"), | 1658 purple_connection_error(gc, g_strdup_printf(_("Couldn't connect to host: %s (%d)"), |
1736 cb(cached_userid, NULL, NULL, data); | 1818 cb(cached_userid, NULL, NULL, data); |
1737 return; | 1819 return; |
1738 } | 1820 } |
1739 #endif | 1821 #endif |
1740 | 1822 |
1741 rid = session->next_rid; | |
1742 ++session->next_rid; | |
1743 | |
1744 /* Setup callback. Response will be associated with request using 'rid'. */ | 1823 /* Setup callback. Response will be associated with request using 'rid'. */ |
1745 g_hash_table_insert(session->user_lookup_cb, GUINT_TO_POINTER(rid), cb); | 1824 rid = msim_new_reply_callback(session, cb, data); |
1746 g_hash_table_insert(session->user_lookup_cb_data, GUINT_TO_POINTER(rid), data); | |
1747 | 1825 |
1748 /* Send request */ | 1826 /* Send request */ |
1749 | 1827 |
1750 cmd = MSIM_CMD_GET; | 1828 cmd = MSIM_CMD_GET; |
1751 | 1829 |