diff options
Diffstat (limited to 'protocols/twitter/twitter_lib.c')
-rw-r--r-- | protocols/twitter/twitter_lib.c | 118 |
1 files changed, 105 insertions, 13 deletions
diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index 41d95db8..4ef22345 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -800,7 +800,7 @@ static char *twitter_msg_add_id(struct im_connection *ic, if (txs->reply_to) { int i; - for (i = 0; i < TWITTER_LOG_LENGTH; i++) { + for (i = 0; i < td->log_length; i++) { if (td->log[i].id == txs->reply_to) { reply_to = i; break; @@ -818,26 +818,39 @@ static char *twitter_msg_add_id(struct im_connection *ic, } } - td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; - td->log[td->log_id].id = txs->id; - td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); + if (txs->from_filter) { + td->filter_log_id = (td->filter_log_id + 1) % td->log_length; + td->filter_log[td->filter_log_id].id = txs->id; + td->filter_log[td->filter_log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); + } else { + td->log_id = (td->log_id + 1) % td->log_length; + td->log[td->log_id].id = txs->id; + td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); + } /* This is all getting hairy. :-( If we RT'ed something ourselves, remember OUR id instead so undo will work. In other cases, the original tweet's id should be remembered for deduplicating. */ if (g_strcasecmp(txs->user->screen_name, td->user) == 0) { - td->log[td->log_id].id = txs->rt_id; - /* More useful than NULL. */ - td->log[td->log_id].bu = &twitter_log_local_user; + if (txs->from_filter) { + td->filter_log[td->filter_log_id].id = txs->rt_id; + /* More useful than NULL. */ + td->filter_log[td->filter_log_id].bu = &twitter_log_local_user; + } else { + td->log[td->log_id].id = txs->rt_id; + /* More useful than NULL. */ + td->log[td->log_id].bu = &twitter_log_local_user; + } } if (set_getbool(&ic->acc->set, "show_ids")) { if (reply_to != -1) { - return g_strdup_printf("\002[\002%02x->%02x\002]\002 %s%s", - td->log_id, reply_to, prefix, txs->text); + return g_strdup_printf("\002[\002%0*x->%0*x\002]\002 %s%s", + td->id_length, td->log_id, td->id_length, + reply_to, prefix, txs->text); } else { - return g_strdup_printf("\002[\002%02x\002]\002 %s%s", - td->log_id, prefix, txs->text); + return g_strdup_printf("\002[\002%0*x\002]\002 %s%s", + td->id_length, td->log_id, prefix, txs->text); } } else { if (*prefix) { @@ -1019,8 +1032,23 @@ static void twitter_http_stream(struct http_request *req) if ((req->flags & HTTPC_EOF) || !req->reply_body) { if (req == td->stream) { td->stream = NULL; + + if (req->status_code == 200 && + td->stream_opentime + 3 < time(NULL)) { + debug("Reconnecting to twitter stream."); + twitter_open_stream(ic); + twitter_get_timeline(ic, -1); + return; + } } else if (req == td->filter_stream) { td->filter_stream = NULL; + + if (req->status_code == 200 && + td->filter_stream_opentime + 3 < time(NULL)) { + debug("Reconnecting to twitter filter stream."); + twitter_open_filter_stream(ic); + return; + } } imcb_error(ic, "Stream closed (%s)", req->status_string); @@ -1100,10 +1128,12 @@ static gboolean twitter_stream_handle_object(struct im_connection *ic, json_valu static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs) { struct twitter_data *td = ic->proto_data; + struct twitter_log_data *tl; int i; - for (i = 0; i < TWITTER_LOG_LENGTH; i++) { - if (td->log[i].id == txs->id) { + tl = txs->from_filter ? td->filter_log : td->log; + for (i = 0; i < td->log_length; i++) { + if (tl[i].id == txs->id) { /* Got a duplicate (RT, probably). Drop it. */ return TRUE; } @@ -1195,6 +1225,7 @@ gboolean twitter_open_stream(struct im_connection *ic) /* This flag must be enabled or we'll get no data until EOF (which err, kind of, defeats the purpose of a streaming API). */ td->stream->flags |= HTTPC_STREAMING; + td->stream_opentime = time(NULL); return TRUE; } @@ -1250,6 +1281,7 @@ static gboolean twitter_filter_stream(struct im_connection *ic) /* This flag must be enabled or we'll get no data until EOF (which err, kind of, defeats the purpose of a streaming API). */ td->filter_stream->flags |= HTTPC_STREAMING; + td->filter_stream_opentime = time(NULL); ret = TRUE; } @@ -1801,3 +1833,63 @@ void twitter_status_show_url(struct im_connection *ic, guint64 id) twitter_http(ic, url, twitter_http_status_show_url, ic, 0, NULL, 0); g_free(url); } + +struct twitter_http_msg { + struct im_connection *ic; + char *message; +}; + +static void twitter_http_status_quote_post(struct http_request *req) +{ + struct twitter_http_msg *thm = req->data; + struct im_connection *ic = thm->ic; + struct twitter_data *td = ic->proto_data; + char *message; + const char *name; + json_value *parsed, *id; + + if (!g_slist_find(twitter_connections, ic)) { + goto eof; + } + + if (!(parsed = twitter_parse_response(ic, req))) { + goto eof; + } + + name = json_o_str(json_o_get(parsed, "user"), "screen_name"); + id = json_o_get(parsed, "id"); + + if (name && id && id->type == json_integer) { + message = g_strdup_printf("%s https://twitter.com/%s/status/%" G_GUINT64_FORMAT, thm->message, name, id->u.integer); + + /*if (!twitter_length_check(ic, message)) { + goto eof; + }*/ + + td->last_status_id = 0; + twitter_post_status(ic, message, 0); + } else { + twitter_log(ic, "Error: could not fetch url for quoted tweet."); + } + + json_value_free(parsed); + +eof: + g_free(thm->message); + g_free(thm); +} + +void twitter_status_quote_post(struct im_connection *ic, guint64 id, char *message) +{ + struct twitter_http_msg *thm; + char *url; + + thm = g_new0(struct twitter_http_msg, 1); + + thm->ic = ic; + thm->message = g_strdup(message); + + url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", TWITTER_STATUS_SHOW_URL, id, ".json"); + twitter_http(ic, url, twitter_http_status_quote_post, thm, 0, NULL, 0); + g_free(url); +} |