comparison libpurple/protocols/myspace/myspace.c @ 16730:a2e9890a57e0

Implement msim_pack(), msim_sendh(), msim_send(). msim_pack() converts a hash table to a protocol message string. msim_sendh() sends a protocol message using msim_send_raw, after converting the hash table to a string using msim_pack(). msim_send() accepts variable arguments and converts them to a hash table, then sends using msim_sendh(). These are part of the TODO item "Generic protocol message sending function, that handles packing of messages", but functions haven't been updated to use msim_send() instead of msim_send_raw() yet.
author Jeffrey Connelly <jaconnel@calpoly.edu>
date Sun, 20 May 2007 07:08:57 +0000
parents 37e3d6378b08
children 2e0bd3e6f2c7
comparison
equal deleted inserted replaced
16729:37e3d6378b08 16730:a2e9890a57e0
33 33
34 #define PURPLE_PLUGIN 34 #define PURPLE_PLUGIN
35 35
36 #include <string.h> 36 #include <string.h>
37 #include <errno.h> /* for EAGAIN */ 37 #include <errno.h> /* for EAGAIN */
38 #include <stdarg.h>
38 39
39 #include <glib.h> 40 #include <glib.h>
40 41
41 #ifdef _WIN32 42 #ifdef _WIN32
42 #include "win32dep.h" 43 #include "win32dep.h"
379 * 380 *
380 */ 381 */
381 static gboolean msim_send_raw(MsimSession *session, const gchar *msg) 382 static gboolean msim_send_raw(MsimSession *session, const gchar *msg)
382 { 383 {
383 int total_bytes_sent, total_bytes; 384 int total_bytes_sent, total_bytes;
385
386 purple_debug_info("msim", "msim_send: writing <%s>\n", msg);
384 387
385 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE); 388 g_return_val_if_fail(MSIM_SESSION_VALID(session), FALSE);
386 g_return_val_if_fail(msg != NULL, FALSE); 389 g_return_val_if_fail(msg != NULL, FALSE);
387 390
388 purple_debug_info("msim", "msim_send: writing <%s>\n", msg);
389 391
390 /* Loop until all data is sent, or a failure occurs. */ 392 /* Loop until all data is sent, or a failure occurs. */
391 total_bytes_sent = 0; 393 total_bytes_sent = 0;
392 total_bytes = strlen(msg); 394 total_bytes = strlen(msg);
393 do 395 do
408 } while(total_bytes_sent < total_bytes); 410 } while(total_bytes_sent < total_bytes);
409 return TRUE; 411 return TRUE;
410 } 412 }
411 413
412 /** 414 /**
413 * Send a message to the server. 415 * Pack a key=value item into a part of a protocol messge.
416 */
417 static void msim_pack_each(gpointer key, gpointer value, gpointer user_data)
418 {
419 gchar ***items; /* wow, a pointer to a pointer to a pointer */
420 gchar *item;
421 gchar *escaped_key, *escaped_value;
422
423 items = user_data;
424
425 escaped_key = msim_escape((gchar *)key);
426 escaped_value = msim_escape((gchar *)value);
427
428 item = g_strdup_printf("%s\\%s", escaped_key, escaped_value);
429
430 g_free(escaped_key);
431 g_free(escaped_value);
432
433 **items = item;
434 ++(*items);
435 }
436
437 /**
438 * Pack a hash table of a protocol of a message into a string suitable
439 * for sending.
440 *
441 * @return The packed string; caller must g_free() when no longer needed.
442 */
443 static gchar *msim_pack(GHashTable *table)
444 {
445 gchar **items, **items_tmp;
446 gchar *raw;
447 guint i;
448
449 items = (gchar **)g_new0(gchar *, g_hash_table_size(table));
450
451 items_tmp = items;
452 g_hash_table_foreach(table, (GHFunc)msim_pack_each, &items_tmp);
453
454 /* Join each item of the message. */
455 raw = g_strjoinv("\\", items);
456
457 /* Free each item, then the array itself. */
458 for (i = 0; i < g_hash_table_size(table); ++i)
459 {
460 g_free(items[i]);
461 }
462 g_free(items);
463
464 return raw;
465 }
466
467 /**
468 * Send a message to the server, by a hash table.
414 * 469 *
415 * @param session 470 * @param session
416 * @param ... A sequence of gchar* key/value pairs, terminated with NULL 471 * @param table A hash table of the message to send.
417 */ 472 */
473 static gboolean msim_sendh(MsimSession *session, GHashTable *table)
474 {
475 gchar *raw_msg;
476 gboolean success;
477
478 raw_msg = msim_pack(table);
479 success = msim_send_raw(session, raw_msg);
480
481 g_free(raw_msg);
482
483 return success;
484 }
485
486 /**
487 *
488 * Send a message to the server, whose contents is specified using
489 * variable arguments.
490 *
491 * @param session
492 * @param ... A sequence of gchar* key/value pairs, terminated with NULL. The strings will be copied.
493 *
494 * This function exists for coding convenience: a message can be created
495 * and sent in one line of code. Internally it calls msim_sendh().
496 */
497
418 static gboolean msim_send(MsimSession *session, ...) 498 static gboolean msim_send(MsimSession *session, ...)
419 { 499 {
420 /* TODO: implement this */ 500 va_list argp;
501 GHashTable *table;
502 gchar *key, *value;
503 gboolean success;
504
505 table = g_hash_table_new_full((GHashFunc)g_str_hash,
506 (GEqualFunc)g_str_equal, g_free, g_free);
507
508 /* Parse key, value pairs until NULL. */
509 va_start(argp, session);
510 do
511 {
512 key = va_arg(argp, gchar *);
513 if (!key)
514 {
515 break;
516 }
517
518 value = va_arg(argp, gchar *);
519 if (!value)
520 {
521 purple_debug_info("msim", "msim_send: no value for key '%s', ignoring\n", key);
522 break;
523 }
524
525 g_hash_table_insert(table, g_strdup(key), g_strdup(value));
526 } while(key && value);
527
528 /* Actually send the message. */
529 success = msim_sendh(session, table);
530
531 /* Cleanup. */
532 va_end(argp);
533 g_hash_table_destroy(table);
534
535 return success;
421 } 536 }
422 537
423 /** 538 /**
424 * Start logging in to the MSIM servers. 539 * Start logging in to the MSIM servers.
425 * 540 *