From 0b3ffb13172c211eed561288d0fd04d76506bb02 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Wed, 19 May 2010 20:46:43 +0100 Subject: Setting the HTML flag on a connection has a nasty side effect of escaping a lot of "special" characters, and these HTML entities are not counted as one character. :-( So just strip HTML of incoming stuff and don't do anything with what goes out. It's not required. The story may actually be more complicated this, let's find out. --- protocols/twitter/twitter_lib.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'protocols/twitter/twitter_lib.c') diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index ee6e39fe..3becc67e 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -433,6 +433,8 @@ static void twitter_groupchat(struct im_connection *ic, GSList *list) status = l->data; twitter_add_buddy(ic, status->user->screen_name, status->user->name); + strip_html(status->text); + // Say it! if (g_strcasecmp(td->user, status->user->screen_name) == 0) imcb_chat_log (gc, "Your Tweet: %s", status->text); @@ -470,6 +472,7 @@ static void twitter_private_message_chat(struct im_connection *ic, GSList *list) status = l->data; + strip_html( status->text ); if( mode_one ) text = g_strdup_printf( "\002<\002%s\002>\002 %s", status->user->screen_name, status->text ); -- cgit v1.2.3 From bb5ce4d1ad2aa6cbcf1042ccf87cf280d9645d4c Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 23 May 2010 13:50:51 +0100 Subject: Added base_url settting to Twitter module so other services using the Twitter API can be used. Only with Basic authentication though. --- protocols/twitter/twitter_lib.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'protocols/twitter/twitter_lib.c') diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index 3becc67e..d3f0d718 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -129,7 +129,7 @@ void twitter_get_friends_ids(struct im_connection *ic, int next_cursor) char* args[2]; args[0] = "cursor"; args[1] = g_strdup_printf ("%d", next_cursor); - twitter_http(TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, td->user, td->pass, td->oauth_info, args, 2); + twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); g_free(args[1]); } @@ -395,7 +395,7 @@ void twitter_get_home_timeline(struct im_connection *ic, int next_cursor) args[3] = g_strdup_printf ("%llu", (long long unsigned int) td->home_timeline_id); } - twitter_http(TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, td->user, td->pass, td->oauth_info, args, td->home_timeline_id ? 4 : 2); + twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, td->home_timeline_id ? 4 : 2); g_free(args[1]); if (td->home_timeline_id) { @@ -622,7 +622,7 @@ void twitter_get_statuses_friends(struct im_connection *ic, int next_cursor) args[0] = "cursor"; args[1] = g_strdup_printf ("%d", next_cursor); - twitter_http(TWITTER_SHOW_FRIENDS_URL, twitter_http_get_statuses_friends, ic, 0, td->user, td->pass, td->oauth_info, args, 2); + twitter_http(ic, TWITTER_SHOW_FRIENDS_URL, twitter_http_get_statuses_friends, ic, 0, args, 2); g_free(args[1]); } @@ -656,7 +656,7 @@ void twitter_post_status(struct im_connection *ic, char* msg) char* args[2]; args[0] = "status"; args[1] = msg; - twitter_http(TWITTER_STATUS_UPDATE_URL, twitter_http_post_status, ic, 1, td->user, td->pass, td->oauth_info, args, 2); + twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post_status, ic, 1, args, 2); // g_free(args[1]); } @@ -674,7 +674,7 @@ void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) args[2] = "text"; args[3] = msg; // Use the same callback as for twitter_post_status, since it does basically the same. - twitter_http(TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post_status, ic, 1, td->user, td->pass, td->oauth_info, args, 4); + twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post_status, ic, 1, args, 4); // g_free(args[1]); // g_free(args[3]); } -- cgit v1.2.3 From a7b9ec7011cb649be3c34ec681ab98e103dfc686 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 23 May 2010 14:49:54 +0100 Subject: Improved error reporting (get textual HTTP error message and error message from Twitter API response if possible). --- protocols/twitter/twitter_lib.c | 48 ++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'protocols/twitter/twitter_lib.c') diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index d3f0d718..ad09bbc5 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -116,6 +116,38 @@ static void twitter_add_buddy(struct im_connection *ic, char *name, const char * } } +/* Warning: May return a malloc()ed value, which will be free()d on the next + call. Only for short-term use. */ +static char *twitter_parse_error(struct http_request *req) +{ + static char *ret = NULL; + struct xt_parser *xp; + struct xt_node *node; + char *err_s = NULL; + + g_free(ret); + ret = NULL; + + xp = xt_new(NULL, NULL); + xt_feed(xp, req->reply_body, req->body_size); + + if ((node = xt_find_node(xp->root, "hash")) && + (node = xt_find_node(node->children, "error")) && + node->text_len > 0) + err_s = node->text; + + if (err_s) + { + ret = g_strdup_printf("%s (%s)", req->status_string, err_s); + xt_free(xp); + return ret; + } + else + { + return req->status_string; + } +} + static void twitter_http_get_friends_ids(struct http_request *req); /** @@ -123,8 +155,6 @@ static void twitter_http_get_friends_ids(struct http_request *req); */ void twitter_get_friends_ids(struct im_connection *ic, int next_cursor) { - struct twitter_data *td = ic->proto_data; - // Primitive, but hey! It works... char* args[2]; args[0] = "cursor"; @@ -195,7 +225,7 @@ static void twitter_http_get_friends_ids(struct http_request *req) if (req->status_code != 200) { // It didn't go well, output the error and return. if (++td->http_fails >= 5) - imcb_error(ic, "Could not retrieve friends. HTTP STATUS: %d", req->status_code); + imcb_error(ic, "Could not retrieve friends: %s", twitter_parse_error(req)); return; } else { @@ -525,7 +555,7 @@ static void twitter_http_get_home_timeline(struct http_request *req) { // It didn't go well, output the error and return. if (++td->http_fails >= 5) - imcb_error(ic, "Could not retrieve " TWITTER_HOME_TIMELINE_URL ". HTTP STATUS: %d", req->status_code); + imcb_error(ic, "Could not retrieve " TWITTER_HOME_TIMELINE_URL ": %s", twitter_parse_error(req)); return; } @@ -577,7 +607,7 @@ static void twitter_http_get_statuses_friends(struct http_request *req) if (req->status_code != 200) { // It didn't go well, output the error and return. if (++td->http_fails >= 5) - imcb_error(ic, "Could not retrieve " TWITTER_SHOW_FRIENDS_URL " HTTP STATUS: %d", req->status_code); + imcb_error(ic, "Could not retrieve " TWITTER_SHOW_FRIENDS_URL ": %s", twitter_parse_error(req)); return; } else { @@ -616,8 +646,6 @@ static void twitter_http_get_statuses_friends(struct http_request *req) */ void twitter_get_statuses_friends(struct im_connection *ic, int next_cursor) { - struct twitter_data *td = ic->proto_data; - char* args[2]; args[0] = "cursor"; args[1] = g_strdup_printf ("%d", next_cursor); @@ -641,7 +669,7 @@ static void twitter_http_post_status(struct http_request *req) // Check if the HTTP request went well. if (req->status_code != 200) { // It didn't go well, output the error and return. - imcb_error(ic, "Could not post message... HTTP STATUS: %d", req->status_code); + imcb_error(ic, "Could not post message: %s", twitter_parse_error(req)); return; } } @@ -651,8 +679,6 @@ static void twitter_http_post_status(struct http_request *req) */ void twitter_post_status(struct im_connection *ic, char* msg) { - struct twitter_data *td = ic->proto_data; - char* args[2]; args[0] = "status"; args[1] = msg; @@ -666,8 +692,6 @@ void twitter_post_status(struct im_connection *ic, char* msg) */ void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) { - struct twitter_data *td = ic->proto_data; - char* args[4]; args[0] = "screen_name"; args[1] = who; -- cgit v1.2.3 From 3d93aed32d49bae6c608a71858f00127dccd28e9 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 23 May 2010 15:52:41 +0100 Subject: Restructure Twitter error parser a bit, it fed a NULL pointer to the XML parser sometimes (which fails safely but is a bad idea anyway). --- protocols/twitter/twitter_lib.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'protocols/twitter/twitter_lib.c') diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index ad09bbc5..e4dfc595 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -121,31 +121,30 @@ static void twitter_add_buddy(struct im_connection *ic, char *name, const char * static char *twitter_parse_error(struct http_request *req) { static char *ret = NULL; - struct xt_parser *xp; + struct xt_parser *xp = NULL; struct xt_node *node; - char *err_s = NULL; g_free(ret); ret = NULL; - xp = xt_new(NULL, NULL); - xt_feed(xp, req->reply_body, req->body_size); - - if ((node = xt_find_node(xp->root, "hash")) && - (node = xt_find_node(node->children, "error")) && - node->text_len > 0) - err_s = node->text; - - if (err_s) + if (req->body_size > 0) { - ret = g_strdup_printf("%s (%s)", req->status_string, err_s); + xp = xt_new(NULL, NULL); + xt_feed(xp, req->reply_body, req->body_size); + + if ((node = xt_find_node(xp->root, "hash")) && + (node = xt_find_node(node->children, "error")) && + node->text_len > 0) + { + ret = g_strdup_printf("%s (%s)", req->status_string, node->text); + xt_free(xp); + return ret; + } + xt_free(xp); - return ret; - } - else - { - return req->status_string; } + + return req->status_string; } static void twitter_http_get_friends_ids(struct http_request *req); -- cgit v1.2.3 From 7d53efb7ec5310b2710757cef03d7f53c94a7797 Mon Sep 17 00:00:00 2001 From: Geert Mulders Date: Sat, 29 May 2010 14:40:17 +0200 Subject: Added functionality to add and remove friendships. --- protocols/twitter/twitter_lib.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'protocols/twitter/twitter_lib.c') diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index ee6e39fe..3ea694e6 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -625,9 +625,9 @@ void twitter_get_statuses_friends(struct im_connection *ic, int next_cursor) } /** - * Callback after sending a new update to twitter. + * Callback to use after sending a post request to twitter. */ -static void twitter_http_post_status(struct http_request *req) +static void twitter_http_post(struct http_request *req) { struct im_connection *ic = req->data; @@ -638,7 +638,7 @@ static void twitter_http_post_status(struct http_request *req) // Check if the HTTP request went well. if (req->status_code != 200) { // It didn't go well, output the error and return. - imcb_error(ic, "Could not post message... HTTP STATUS: %d", req->status_code); + imcb_error(ic, "HTTP Error... STATUS: %d", req->status_code); return; } } @@ -653,7 +653,7 @@ void twitter_post_status(struct im_connection *ic, char* msg) char* args[2]; args[0] = "status"; args[1] = msg; - twitter_http(TWITTER_STATUS_UPDATE_URL, twitter_http_post_status, ic, 1, td->user, td->pass, td->oauth_info, args, 2); + twitter_http(TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, td->user, td->pass, td->oauth_info, args, 2); // g_free(args[1]); } @@ -671,7 +671,20 @@ void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) args[2] = "text"; args[3] = msg; // Use the same callback as for twitter_post_status, since it does basically the same. - twitter_http(TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post_status, ic, 1, td->user, td->pass, td->oauth_info, args, 4); + twitter_http(TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, td->user, td->pass, td->oauth_info, args, 4); // g_free(args[1]); // g_free(args[3]); } + +void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) +{ + struct twitter_data *td = ic->proto_data; + + char* args[2]; + args[0] = "screen_name"; + args[1] = who; + twitter_http(create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, twitter_http_post, ic, 1, td->user, td->pass, td->oauth_info, args, 2); +} + + + -- cgit v1.2.3