grim/purple-spasm

3c2a35b27899
Parents 511b1f3fb898
Children 0b0058df4b6b
Add some basic send message support and replies for pings
  • +65 -42
    spasm-chat.c
  • --- a/spasm-chat.c Thu May 18 19:43:51 2017 -0500
    +++ b/spasm-chat.c Thu May 18 22:18:39 2017 -0500
    @@ -20,7 +20,10 @@
    #include "spasm-chat.h"
    #include "spasm-const.h"
    +#include <stdarg.h>
    +
    #include "debug.h"
    +#include "util.h"
    /******************************************************************************
    * Structs
    @@ -35,8 +38,64 @@
    };
    /******************************************************************************
    + * sending
    + *****************************************************************************/
    +static void
    +_purple_spasm_chat_service_send(PurpleSpasmChatService *chat,
    + const gchar *format, ...)
    +{
    + GCancellable *cancellable = NULL;
    + GError *error = NULL;
    + gchar *buffer = NULL;
    + gboolean success;
    + va_list vargs;
    +
    + cancellable = purple_spasm_account_get_cancellable(chat->sa);
    +
    + va_start(vargs, format);
    + buffer = g_strdup_vprintf(format, vargs);
    + va_end(vargs);
    +
    + success = g_output_stream_printf(
    + chat->output_stream,
    + NULL,
    + cancellable,
    + &error,
    + buffer
    + );
    +
    + g_free(buffer);
    +
    + if(!success) {
    + PurpleConnection *purple_connection = purple_spasm_account_get_connection(chat->sa);
    +
    + if(error) {
    + purple_connection_error(purple_connection, error->message);
    +
    + g_error_free(error);
    + } else {
    + purple_connection_error(purple_connection, "unknown error");
    + }
    +
    + return;
    + }
    + g_output_stream_flush(chat->output_stream, NULL, NULL);
    +}
    +
    +/******************************************************************************
    * read loop
    *****************************************************************************/
    +static void
    +_purple_spasm_chat_service_parse(PurpleSpasmChatService *chat,
    + const gchar *buffer)
    +{
    + if(purple_str_has_prefix(buffer, "PING ")) {
    + purple_debug_misc("spasm", "PING? PONG!\n");
    +
    + _purple_spasm_chat_service_send(chat, "PONG %s\r\n", buffer + 5);
    + }
    +}
    +
    static void _purple_spasm_chat_read(PurpleSpasmChatService *chat);
    static void
    @@ -81,6 +140,8 @@
    purple_debug_info("spasm", "chat buffer: %s\n", buffer);
    + _purple_spasm_chat_service_parse(chat, buffer);
    +
    g_free(buffer);
    _purple_spasm_chat_read(chat);
    @@ -102,13 +163,10 @@
    *****************************************************************************/
    static void
    _purple_spasm_chat_login_cb(GObject *obj, GAsyncResult *res, gpointer data) {
    - GCancellable *cancellable = NULL;
    GError *error = NULL;
    PurpleConnection *purple_connection = NULL;
    PurpleSpasmChatService *chat = (PurpleSpasmChatService *)data;
    - gboolean success = FALSE;
    - cancellable = purple_spasm_account_get_cancellable(chat->sa);
    purple_connection = purple_spasm_account_get_connection(chat->sa);
    chat->socket_connection = g_socket_client_connect_to_host_finish(
    @@ -134,53 +192,18 @@
    chat->output_stream = g_io_stream_get_output_stream(G_IO_STREAM(chat->socket_connection));
    /* now do the login */
    - success = g_output_stream_printf(
    - chat->output_stream,
    - NULL,
    - cancellable,
    - &error,
    + _purple_spasm_chat_service_send(
    + chat,
    "PASS oauth:%s\r\n",
    purple_spasm_account_get_access_token(chat->sa)
    );
    - if(!success) {
    - if(error) {
    - g_prefix_error(&error, "failed to set password: ");
    -
    - purple_connection_error(purple_connection, error->message);
    -
    - g_error_free(error);
    - } else {
    - purple_connection_error(purple_connection, "unknown error");
    - }
    -
    - return;
    - }
    - g_output_stream_flush(chat->output_stream, NULL, NULL);
    /* now try to use our nick */
    - success = g_output_stream_printf(
    - chat->output_stream,
    - NULL,
    - cancellable,
    - &error,
    + _purple_spasm_chat_service_send(
    + chat,
    "NICK %s\r\n",
    purple_spasm_account_get_name(chat->sa)
    );
    - if(!success) {
    - if(error) {
    - g_prefix_error(&error, "failed to set nick : ");
    -
    - purple_connection_error(purple_connection, error->message);
    -
    - g_error_free(error);
    - } else {
    - purple_connection_error(purple_connection, "unknown error");
    - }
    -
    - return;
    - }
    -
    - g_output_stream_flush(chat->output_stream, NULL, NULL);
    chat->input_stream = g_data_input_stream_new(
    g_io_stream_get_input_stream(G_IO_STREAM(chat->socket_connection))