diff options
author | Geert Mulders <g.c.w.m.mulders@gmail.com> | 2010-04-06 19:25:51 +0200 |
---|---|---|
committer | Geert Mulders <g.c.w.m.mulders@gmail.com> | 2010-04-06 19:25:51 +0200 |
commit | 2abceca711403e8e3308213954b4477ceecd4282 (patch) | |
tree | b4b86d6e1b199afd3ac96d2c6c5c8d8211202c80 | |
parent | 62d2cfb0b7b5e7f3eda9ca13b1877d3ad74fcd5e (diff) |
Updates made as a result to the comments on the review.
-rw-r--r-- | protocols/twitter/twitter.c | 20 | ||||
-rw-r--r-- | protocols/twitter/twitter.h | 1 | ||||
-rw-r--r-- | protocols/twitter/twitter_http.c | 49 | ||||
-rw-r--r-- | protocols/twitter/twitter_lib.c | 18 |
4 files changed, 31 insertions, 57 deletions
diff --git a/protocols/twitter/twitter.c b/protocols/twitter/twitter.c index b6b23fa5..fb7acc12 100644 --- a/protocols/twitter/twitter.c +++ b/protocols/twitter/twitter.c @@ -34,7 +34,9 @@ gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond) { struct im_connection *ic = data; // Check if we are still logged in... - if ((ic->flags & OPT_LOGGED_IN) != OPT_LOGGED_IN) + // We are logged in if the flag says so and the connection is still in the connections list. + if ((ic->flags & OPT_LOGGED_IN) != OPT_LOGGED_IN + && !g_slist_find( twitter_connections, ic )) return 0; // If the user uses multiple private message windows we need to get the @@ -54,6 +56,7 @@ static void twitter_init( account_t *acc ) { set_t *s; s = set_add( &acc->set, "use_groupchat", "false", set_eval_bool, acc ); + s->flags |= ACC_SET_OFFLINE_ONLY; } /** @@ -71,17 +74,15 @@ static void twitter_login( account_t *acc ) ic->proto_data = td; - // Set the status to logged in. - ic->flags = OPT_LOGGED_IN; + imcb_log( ic, "Connecting to twitter" ); + imcb_connected(ic); // Run this once. After this queue the main loop function. twitter_main_loop(ic, -1, 0); // Queue the main_loop - b_timeout_add(60000, twitter_main_loop, ic); - - imcb_log( ic, "Connecting to twitter" ); - imcb_connected(ic); + // Save the return value, so we can remove the timeout on logout. + td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic); twitter_connections = g_slist_append( twitter_connections, ic ); } @@ -96,6 +97,9 @@ static void twitter_logout( struct im_connection *ic ) // Set the status to logged out. ic->flags = 0; + // Remove the main_loop function from the function queue. + b_event_remove(td->main_loop_id); + if( td ) { g_free( td ); @@ -148,6 +152,8 @@ static void twitter_remove_buddy( struct im_connection *ic, char *who, char *gro static void twitter_chat_msg( struct groupchat *c, char *message, int flags ) { + if( c && message ) + twitter_post_status(c->ic, message); } static void twitter_chat_invite( struct groupchat *c, char *who, char *message ) diff --git a/protocols/twitter/twitter.h b/protocols/twitter/twitter.h index 05a861bb..e13deddb 100644 --- a/protocols/twitter/twitter.h +++ b/protocols/twitter/twitter.h @@ -37,6 +37,7 @@ struct twitter_data char* user; char* pass; guint64 home_timeline_id; + gint main_loop_id; struct groupchat *home_timeline_gc; }; diff --git a/protocols/twitter/twitter_http.c b/protocols/twitter/twitter_http.c index 4385475c..34b9408d 100644 --- a/protocols/twitter/twitter_http.c +++ b/protocols/twitter/twitter_http.c @@ -38,9 +38,7 @@ #include <errno.h> -char *twitter_urlencode(const char *instr); char *twitter_url_append(char *url, char *key, char* value); -static int isurlchar(unsigned char c); /** * Do a request. @@ -106,9 +104,9 @@ void *twitter_http(char *url_string, http_input_function func, gpointer data, in // Make the request. request = g_strdup_printf( "%s %s HTTP/1.0\r\n" - "Host: %s\r\n" - "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n", - is_post ? "POST" : "GET", url->file, url->host ); + "Host: %s\r\n" + "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n", + is_post ? "POST" : "GET", url->file, url->host ); // If a pass and user are given we append them to the request. if (userpass_base64) @@ -145,8 +143,11 @@ void *twitter_http(char *url_string, http_input_function func, gpointer data, in char *twitter_url_append(char *url, char *key, char* value) { - char *key_encoded = twitter_urlencode(key); - char *value_encoded = twitter_urlencode(value); + char *key_encoded = g_strndup(key, 3 * strlen(key)); + http_encode(key_encoded); + char *value_encoded = g_strndup(value, 3 * strlen(value)); + http_encode(value_encoded); + char *retval; if (strlen(url) != 0) retval = g_strdup_printf("%s&%s=%s", url, key_encoded, value_encoded); @@ -159,35 +160,6 @@ char *twitter_url_append(char *url, char *key, char* value) return retval; } -char *twitter_urlencode(const char *instr) -{ - int ipos=0, bpos=0; - char *str = NULL; - int len = strlen(instr); - - if(!(str = g_new(char, 3*len + 1) )) - return ""; - - while(instr[ipos]) { - while(isurlchar(instr[ipos])) - str[bpos++] = instr[ipos++]; - if(!instr[ipos]) - break; - - g_snprintf(&str[bpos], 4, "%%%.2x", instr[ipos]); - bpos+=3; - ipos++; - } - str[bpos]='\0'; - - /* free extra alloc'ed mem. */ - len = strlen(str); - str = g_renew(char, str, len+1); - - return (str); -} - - char *twitter_urldecode(const char *instr) { int ipos=0, bpos=0; @@ -228,8 +200,3 @@ char *twitter_urldecode(const char *instr) return (str); } -static int isurlchar(unsigned char c) -{ - return (isalnum(c) || '-' == c || '_' == c); -} - diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index f07897ed..d4e07c42 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -64,6 +64,7 @@ static void txu_free(struct twitter_xml_user *txu) { g_free(txu->name); g_free(txu->screen_name); + g_free(txu); } @@ -75,6 +76,7 @@ static void txs_free(struct twitter_xml_status *txs) g_free(txs->created_at); g_free(txs->text); txu_free(txs->user); + g_free(txs); } /** @@ -130,7 +132,7 @@ void twitter_get_friends_ids(struct im_connection *ic, int next_cursor) static xt_status twitter_xt_next_cursor( struct xt_node *node, struct twitter_xml_list *txl ) { // Do something with the cursor. - txl->next_cursor = atoi(node->text); + txl->next_cursor = node->text != NULL ? atoi(node->text) : -1; return XT_HANDLED; } @@ -152,7 +154,7 @@ static xt_status twitter_xt_get_friends_id_list( struct xt_node *node, struct tw if ( g_strcasecmp( "id", child->name ) == 0) { // Add the item to the list. - txl->list = g_slist_append (txl->list, g_memdup( node->text, node->text_len + 1 )); + txl->list = g_slist_append (txl->list, g_memdup( child->text, child->text_len + 1 )); } else if ( g_strcasecmp( "next_cursor", child->name ) == 0) { @@ -186,7 +188,6 @@ static void twitter_http_get_friends_ids(struct http_request *req) } txl = g_new0(struct twitter_xml_list, 1); - txl->list = NULL; // Parse the data. parser = xt_new( NULL, txl ); @@ -450,7 +451,7 @@ static void twitter_http_get_home_timeline(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 retrieve home/timeline. HTTP STATUS: %d", req->status_code); + imcb_error(ic, "Could not retrieve " TWITTER_HOME_TIMELINE_URL ". HTTP STATUS: %d", req->status_code); return; } @@ -487,6 +488,8 @@ static void twitter_http_get_statuses_friends(struct http_request *req) struct im_connection *ic = req->data; struct xt_parser *parser; struct twitter_xml_list *txl; + GSList *l = NULL; + struct twitter_xml_user *user; // Check if the connection is still active. if( !g_slist_find( twitter_connections, ic ) ) @@ -495,7 +498,7 @@ static void twitter_http_get_statuses_friends(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 retrieve home/timeline. HTTP STATUS: %d", req->status_code); + imcb_error(ic, "Could not retrieve " TWITTER_SHOW_FRIENDS_URL " HTTP STATUS: %d", req->status_code); return; } @@ -510,8 +513,6 @@ static void twitter_http_get_statuses_friends(struct http_request *req) twitter_xt_get_user_list(parser->root, txl); xt_free( parser ); - GSList *l = NULL; - struct twitter_xml_user *user; // Add the users as buddies. for ( l = txl->list; l ; l = g_slist_next(l) ) { @@ -558,8 +559,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 tweed... HTTP STATUS: %d", req->status_code); - imcb_error(ic, req->reply_body); + imcb_error(ic, "Could not post tweet... HTTP STATUS: %d", req->status_code); return; } } |