[1b221e0] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple module to facilitate twitter functionality. * |
---|
| 5 | * * |
---|
[199fea6] | 6 | * Copyright 2009-2010 Geert Mulders <g.c.w.m.mulders@gmail.com> * |
---|
[0e788f5] | 7 | * Copyright 2010-2013 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[1b221e0] | 8 | * * |
---|
| 9 | * This library is free software; you can redistribute it and/or * |
---|
| 10 | * modify it under the terms of the GNU Lesser General Public * |
---|
| 11 | * License as published by the Free Software Foundation, version * |
---|
| 12 | * 2.1. * |
---|
| 13 | * * |
---|
| 14 | * This library is distributed in the hope that it will be useful, * |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
| 17 | * Lesser General Public License for more details. * |
---|
| 18 | * * |
---|
| 19 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 20 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
| 22 | * * |
---|
| 23 | ****************************************************************************/ |
---|
| 24 | |
---|
[08579a1] | 25 | /* For strptime(): */ |
---|
[5ebff60] | 26 | #if (__sun) |
---|
[daae10f] | 27 | #else |
---|
[08579a1] | 28 | #define _XOPEN_SOURCE |
---|
[daae10f] | 29 | #endif |
---|
[08579a1] | 30 | |
---|
[1b221e0] | 31 | #include "twitter_http.h" |
---|
| 32 | #include "twitter.h" |
---|
| 33 | #include "bitlbee.h" |
---|
| 34 | #include "url.h" |
---|
| 35 | #include "misc.h" |
---|
| 36 | #include "base64.h" |
---|
| 37 | #include "twitter_lib.h" |
---|
[e08ae0c] | 38 | #include "json_util.h" |
---|
[1b221e0] | 39 | #include <ctype.h> |
---|
| 40 | #include <errno.h> |
---|
| 41 | |
---|
| 42 | #define TXL_STATUS 1 |
---|
[62d2cfb] | 43 | #define TXL_USER 2 |
---|
| 44 | #define TXL_ID 3 |
---|
| 45 | |
---|
[1b221e0] | 46 | struct twitter_xml_list { |
---|
[62d2cfb] | 47 | int type; |
---|
[8203da9] | 48 | gint64 next_cursor; |
---|
[1b221e0] | 49 | GSList *list; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | struct twitter_xml_user { |
---|
[73ee390] | 53 | guint64 uid; |
---|
[1b221e0] | 54 | char *name; |
---|
| 55 | char *screen_name; |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | struct twitter_xml_status { |
---|
[08579a1] | 59 | time_t created_at; |
---|
[1b221e0] | 60 | char *text; |
---|
| 61 | struct twitter_xml_user *user; |
---|
[67f6828] | 62 | guint64 id, rt_id; /* Usually equal, with RTs id == *original* id */ |
---|
| 63 | guint64 reply_to; |
---|
[73ee390] | 64 | gboolean from_filter; |
---|
[1b221e0] | 65 | }; |
---|
| 66 | |
---|
[62d2cfb] | 67 | /** |
---|
| 68 | * Frees a twitter_xml_user struct. |
---|
| 69 | */ |
---|
| 70 | static void txu_free(struct twitter_xml_user *txu) |
---|
| 71 | { |
---|
[5ebff60] | 72 | if (txu == NULL) { |
---|
[a26af5c] | 73 | return; |
---|
[5ebff60] | 74 | } |
---|
[2322a9f] | 75 | |
---|
[62d2cfb] | 76 | g_free(txu->name); |
---|
| 77 | g_free(txu->screen_name); |
---|
[2abceca] | 78 | g_free(txu); |
---|
[62d2cfb] | 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * Frees a twitter_xml_status struct. |
---|
| 83 | */ |
---|
| 84 | static void txs_free(struct twitter_xml_status *txs) |
---|
| 85 | { |
---|
[5ebff60] | 86 | if (txs == NULL) { |
---|
[2322a9f] | 87 | return; |
---|
[5ebff60] | 88 | } |
---|
[2322a9f] | 89 | |
---|
[62d2cfb] | 90 | g_free(txs->text); |
---|
| 91 | txu_free(txs->user); |
---|
[2abceca] | 92 | g_free(txs); |
---|
[62d2cfb] | 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * Free a twitter_xml_list struct. |
---|
| 97 | * type is the type of list the struct holds. |
---|
| 98 | */ |
---|
| 99 | static void txl_free(struct twitter_xml_list *txl) |
---|
| 100 | { |
---|
| 101 | GSList *l; |
---|
[5ebff60] | 102 | |
---|
| 103 | if (txl == NULL) { |
---|
[a26af5c] | 104 | return; |
---|
[5ebff60] | 105 | } |
---|
[2322a9f] | 106 | |
---|
| 107 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 108 | if (txl->type == TXL_STATUS) { |
---|
[5983eca] | 109 | txs_free((struct twitter_xml_status *) l->data); |
---|
[2322a9f] | 110 | } else if (txl->type == TXL_ID) { |
---|
[62d2cfb] | 111 | g_free(l->data); |
---|
[2322a9f] | 112 | } else if (txl->type == TXL_USER) { |
---|
[de923d5] | 113 | txu_free(l->data); |
---|
[2322a9f] | 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
[62d2cfb] | 117 | g_slist_free(txl->list); |
---|
[fd65edb] | 118 | g_free(txl); |
---|
[62d2cfb] | 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
[2322a9f] | 122 | * Compare status elements |
---|
| 123 | */ |
---|
| 124 | static gint twitter_compare_elements(gconstpointer a, gconstpointer b) |
---|
| 125 | { |
---|
| 126 | struct twitter_xml_status *a_status = (struct twitter_xml_status *) a; |
---|
| 127 | struct twitter_xml_status *b_status = (struct twitter_xml_status *) b; |
---|
| 128 | |
---|
| 129 | if (a_status->created_at < b_status->created_at) { |
---|
| 130 | return -1; |
---|
| 131 | } else if (a_status->created_at > b_status->created_at) { |
---|
| 132 | return 1; |
---|
| 133 | } else { |
---|
| 134 | return 0; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Add a buddy if it is not already added, set the status to logged in. |
---|
[62d2cfb] | 140 | */ |
---|
[3e69802] | 141 | static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname) |
---|
[62d2cfb] | 142 | { |
---|
[1014cab] | 143 | struct twitter_data *td = ic->proto_data; |
---|
| 144 | |
---|
[de923d5] | 145 | // Check if the buddy is already in the buddy list. |
---|
[5983eca] | 146 | if (!bee_user_by_handle(ic->bee, ic, name)) { |
---|
[62d2cfb] | 147 | // The buddy is not in the list, add the buddy and set the status to logged in. |
---|
[5983eca] | 148 | imcb_add_buddy(ic, name, NULL); |
---|
| 149 | imcb_rename_buddy(ic, name, fullname); |
---|
[631ec80] | 150 | if (td->flags & TWITTER_MODE_CHAT) { |
---|
[5c18a76] | 151 | /* Necessary so that nicks always get translated to the |
---|
| 152 | exact Twitter username. */ |
---|
[5983eca] | 153 | imcb_buddy_nick_hint(ic, name, name); |
---|
[5ebff60] | 154 | if (td->timeline_gc) { |
---|
[7f557d5] | 155 | imcb_chat_add_buddy(td->timeline_gc, name); |
---|
[5ebff60] | 156 | } |
---|
| 157 | } else if (td->flags & TWITTER_MODE_MANY) { |
---|
[5983eca] | 158 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
[5ebff60] | 159 | } |
---|
[62d2cfb] | 160 | } |
---|
| 161 | } |
---|
[1b221e0] | 162 | |
---|
[a7b9ec7] | 163 | /* Warning: May return a malloc()ed value, which will be free()d on the next |
---|
[de923d5] | 164 | call. Only for short-term use. NOT THREADSAFE! */ |
---|
[6eca2eb] | 165 | char *twitter_parse_error(struct http_request *req) |
---|
[a7b9ec7] | 166 | { |
---|
| 167 | static char *ret = NULL; |
---|
[5246133] | 168 | json_value *root, *err; |
---|
[5983eca] | 169 | |
---|
[a7b9ec7] | 170 | g_free(ret); |
---|
| 171 | ret = NULL; |
---|
[5983eca] | 172 | |
---|
| 173 | if (req->body_size > 0) { |
---|
[7a80925] | 174 | root = json_parse(req->reply_body, req->body_size); |
---|
[5246133] | 175 | err = json_o_get(root, "errors"); |
---|
[dff0e0b] | 176 | if (err && err->type == json_array && (err = err->u.array.values[0]) && |
---|
[5246133] | 177 | err->type == json_object) { |
---|
| 178 | const char *msg = json_o_str(err, "message"); |
---|
[5ebff60] | 179 | if (msg) { |
---|
[5246133] | 180 | ret = g_strdup_printf("%s (%s)", req->status_string, msg); |
---|
[5ebff60] | 181 | } |
---|
[5246133] | 182 | } |
---|
| 183 | json_value_free(root); |
---|
[a7b9ec7] | 184 | } |
---|
[5983eca] | 185 | |
---|
[6eca2eb] | 186 | return ret ? ret : req->status_string; |
---|
[a7b9ec7] | 187 | } |
---|
| 188 | |
---|
[fb351ce] | 189 | /* WATCH OUT: This function might or might not destroy your connection. |
---|
| 190 | Sub-optimal indeed, but just be careful when this returns NULL! */ |
---|
[e08ae0c] | 191 | static json_value *twitter_parse_response(struct im_connection *ic, struct http_request *req) |
---|
[2a6da96] | 192 | { |
---|
| 193 | gboolean logging_in = !(ic->flags & OPT_LOGGED_IN); |
---|
| 194 | gboolean periodic; |
---|
| 195 | struct twitter_data *td = ic->proto_data; |
---|
[e08ae0c] | 196 | json_value *ret; |
---|
[2a6da96] | 197 | char path[64] = "", *s; |
---|
[5ebff60] | 198 | |
---|
[2a6da96] | 199 | if ((s = strchr(req->request, ' '))) { |
---|
[5ebff60] | 200 | path[sizeof(path) - 1] = '\0'; |
---|
[2a6da96] | 201 | strncpy(path, s + 1, sizeof(path) - 1); |
---|
[5ebff60] | 202 | if ((s = strchr(path, '?')) || (s = strchr(path, ' '))) { |
---|
[2a6da96] | 203 | *s = '\0'; |
---|
[5ebff60] | 204 | } |
---|
[2a6da96] | 205 | } |
---|
[5ebff60] | 206 | |
---|
[2a6da96] | 207 | /* Kinda nasty. :-( Trying to suppress error messages, but only |
---|
| 208 | for periodic (i.e. mentions/timeline) queries. */ |
---|
| 209 | periodic = strstr(path, "timeline") || strstr(path, "mentions"); |
---|
[5ebff60] | 210 | |
---|
[2a6da96] | 211 | if (req->status_code == 401 && logging_in) { |
---|
| 212 | /* IIRC Twitter once had an outage where they were randomly |
---|
| 213 | throwing 401s so I'll keep treating this one as fatal |
---|
| 214 | only during login. */ |
---|
[5246133] | 215 | imcb_error(ic, "Authentication failure (%s)", |
---|
[5ebff60] | 216 | twitter_parse_error(req)); |
---|
[2a6da96] | 217 | imc_logout(ic, FALSE); |
---|
| 218 | return NULL; |
---|
| 219 | } else if (req->status_code != 200) { |
---|
| 220 | // It didn't go well, output the error and return. |
---|
[5ebff60] | 221 | if (!periodic || logging_in || ++td->http_fails >= 5) { |
---|
[96dd574] | 222 | twitter_log(ic, "Error: Could not retrieve %s: %s", |
---|
[5ebff60] | 223 | path, twitter_parse_error(req)); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | if (logging_in) { |
---|
[2a6da96] | 227 | imc_logout(ic, TRUE); |
---|
[5ebff60] | 228 | } |
---|
[2a6da96] | 229 | return NULL; |
---|
| 230 | } else { |
---|
| 231 | td->http_fails = 0; |
---|
| 232 | } |
---|
| 233 | |
---|
[7a80925] | 234 | if ((ret = json_parse(req->reply_body, req->body_size)) == NULL) { |
---|
[2a6da96] | 235 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
[5ebff60] | 236 | path, "XML parse error"); |
---|
[2a6da96] | 237 | } |
---|
| 238 | return ret; |
---|
| 239 | } |
---|
| 240 | |
---|
[1b221e0] | 241 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
[fb62f132] | 242 | static void twitter_http_get_mutes_ids(struct http_request *req); |
---|
| 243 | static void twitter_http_get_noretweets_ids(struct http_request *req); |
---|
[1b221e0] | 244 | |
---|
| 245 | /** |
---|
| 246 | * Get the friends ids. |
---|
| 247 | */ |
---|
[8203da9] | 248 | void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor) |
---|
[1b221e0] | 249 | { |
---|
[85c3004] | 250 | // Primitive, but hey! It works... |
---|
[5983eca] | 251 | char *args[2]; |
---|
[5ebff60] | 252 | |
---|
[1b221e0] | 253 | args[0] = "cursor"; |
---|
[85c3004] | 254 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
[bb5ce4d1] | 255 | twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); |
---|
[1b221e0] | 256 | |
---|
| 257 | g_free(args[1]); |
---|
| 258 | } |
---|
| 259 | |
---|
[fb62f132] | 260 | /** |
---|
| 261 | * Get the muted users ids. |
---|
| 262 | */ |
---|
| 263 | void twitter_get_mutes_ids(struct im_connection *ic, gint64 next_cursor) |
---|
| 264 | { |
---|
| 265 | char *args[2]; |
---|
| 266 | |
---|
| 267 | args[0] = "cursor"; |
---|
| 268 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
| 269 | twitter_http(ic, TWITTER_MUTES_IDS_URL, twitter_http_get_mutes_ids, ic, 0, args, 2); |
---|
| 270 | |
---|
| 271 | g_free(args[1]); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | /** |
---|
| 275 | * Get the ids for users from whom we should ignore retweets. |
---|
| 276 | */ |
---|
| 277 | void twitter_get_noretweets_ids(struct im_connection *ic, gint64 next_cursor) |
---|
| 278 | { |
---|
| 279 | char *args[2]; |
---|
| 280 | |
---|
| 281 | args[0] = "cursor"; |
---|
| 282 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
| 283 | twitter_http(ic, TWITTER_NORETWEETS_IDS_URL, twitter_http_get_noretweets_ids, ic, 0, args, 2); |
---|
| 284 | |
---|
| 285 | g_free(args[1]); |
---|
| 286 | } |
---|
| 287 | |
---|
[1b221e0] | 288 | /** |
---|
| 289 | * Fill a list of ids. |
---|
| 290 | */ |
---|
[9e8c945] | 291 | static gboolean twitter_xt_get_friends_id_list(json_value *node, struct twitter_xml_list *txl) |
---|
[1b221e0] | 292 | { |
---|
[e08ae0c] | 293 | json_value *c; |
---|
| 294 | int i; |
---|
[5983eca] | 295 | |
---|
[62d2cfb] | 296 | // Set the list type. |
---|
| 297 | txl->type = TXL_ID; |
---|
[1b221e0] | 298 | |
---|
[e08ae0c] | 299 | c = json_o_get(node, "ids"); |
---|
[5ebff60] | 300 | if (!c || c->type != json_array) { |
---|
[9e8c945] | 301 | return FALSE; |
---|
[5ebff60] | 302 | } |
---|
[1b221e0] | 303 | |
---|
[5ebff60] | 304 | for (i = 0; i < c->u.array.length; i++) { |
---|
| 305 | if (c->u.array.values[i]->type != json_integer) { |
---|
[0688e99] | 306 | continue; |
---|
[5ebff60] | 307 | } |
---|
| 308 | |
---|
[e08ae0c] | 309 | txl->list = g_slist_prepend(txl->list, |
---|
[5ebff60] | 310 | g_strdup_printf("%" PRIu64, c->u.array.values[i]->u.integer)); |
---|
[e08ae0c] | 311 | } |
---|
[5ebff60] | 312 | |
---|
[e08ae0c] | 313 | c = json_o_get(node, "next_cursor"); |
---|
[5ebff60] | 314 | if (c && c->type == json_integer) { |
---|
[e08ae0c] | 315 | txl->next_cursor = c->u.integer; |
---|
[5ebff60] | 316 | } else { |
---|
[e08ae0c] | 317 | txl->next_cursor = -1; |
---|
[5ebff60] | 318 | } |
---|
| 319 | |
---|
[9e8c945] | 320 | return TRUE; |
---|
[1b221e0] | 321 | } |
---|
| 322 | |
---|
[de923d5] | 323 | static void twitter_get_users_lookup(struct im_connection *ic); |
---|
| 324 | |
---|
[1b221e0] | 325 | /** |
---|
| 326 | * Callback for getting the friends ids. |
---|
| 327 | */ |
---|
| 328 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
| 329 | { |
---|
| 330 | struct im_connection *ic; |
---|
[e08ae0c] | 331 | json_value *parsed; |
---|
[1b221e0] | 332 | struct twitter_xml_list *txl; |
---|
[3bd4a93] | 333 | struct twitter_data *td; |
---|
[1b221e0] | 334 | |
---|
| 335 | ic = req->data; |
---|
| 336 | |
---|
[62d2cfb] | 337 | // Check if the connection is still active. |
---|
[5ebff60] | 338 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[62d2cfb] | 339 | return; |
---|
[5ebff60] | 340 | } |
---|
[5983eca] | 341 | |
---|
[37aa317] | 342 | td = ic->proto_data; |
---|
[62d2cfb] | 343 | |
---|
[1b221e0] | 344 | // Parse the data. |
---|
[5ebff60] | 345 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[2a6da96] | 346 | return; |
---|
[5ebff60] | 347 | } |
---|
| 348 | |
---|
[bbff22d] | 349 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 350 | txl->list = td->follow_ids; |
---|
| 351 | |
---|
[d0752e8] | 352 | twitter_xt_get_friends_id_list(parsed, txl); |
---|
[e08ae0c] | 353 | json_value_free(parsed); |
---|
[1b221e0] | 354 | |
---|
[de923d5] | 355 | td->follow_ids = txl->list; |
---|
[5ebff60] | 356 | if (txl->next_cursor) { |
---|
[de923d5] | 357 | /* These were just numbers. Up to 4000 in a response AFAIK so if we get here |
---|
| 358 | we may be using a spammer account. \o/ */ |
---|
[1b221e0] | 359 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
[5ebff60] | 360 | } else { |
---|
[de923d5] | 361 | /* Now to convert all those numbers into names.. */ |
---|
| 362 | twitter_get_users_lookup(ic); |
---|
[5ebff60] | 363 | } |
---|
[de923d5] | 364 | |
---|
| 365 | txl->list = NULL; |
---|
| 366 | txl_free(txl); |
---|
| 367 | } |
---|
| 368 | |
---|
[fb62f132] | 369 | /** |
---|
| 370 | * Callback for getting the mutes ids. |
---|
| 371 | */ |
---|
| 372 | static void twitter_http_get_mutes_ids(struct http_request *req) |
---|
| 373 | { |
---|
| 374 | struct im_connection *ic = req->data; |
---|
| 375 | json_value *parsed; |
---|
| 376 | struct twitter_xml_list *txl; |
---|
| 377 | struct twitter_data *td; |
---|
| 378 | |
---|
| 379 | // Check if the connection is stil active |
---|
| 380 | if (!g_slist_find(twitter_connections, ic)) { |
---|
| 381 | return; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | td = ic->proto_data; |
---|
| 385 | |
---|
| 386 | // Parse the data. |
---|
| 387 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
| 388 | return; |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 392 | txl->list = td->mutes_ids; |
---|
| 393 | |
---|
| 394 | /* mute ids API response is similar enough to friends response |
---|
| 395 | to reuse this method */ |
---|
| 396 | twitter_xt_get_friends_id_list(parsed, txl); |
---|
| 397 | json_value_free(parsed); |
---|
| 398 | |
---|
| 399 | td->mutes_ids = txl->list; |
---|
| 400 | if (txl->next_cursor) { |
---|
| 401 | /* Recurse while there are still more pages */ |
---|
| 402 | twitter_get_mutes_ids(ic, txl->next_cursor); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | txl->list = NULL; |
---|
| 406 | txl_free(txl); |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | /** |
---|
| 410 | * Callback for getting the no-retweets ids. |
---|
| 411 | */ |
---|
| 412 | static void twitter_http_get_noretweets_ids(struct http_request *req) |
---|
| 413 | { |
---|
| 414 | struct im_connection *ic = req->data; |
---|
| 415 | json_value *parsed; |
---|
| 416 | struct twitter_xml_list *txl; |
---|
| 417 | struct twitter_data *td; |
---|
| 418 | |
---|
| 419 | // Check if the connection is stil active |
---|
| 420 | if (!g_slist_find(twitter_connections, ic)) { |
---|
| 421 | return; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | td = ic->proto_data; |
---|
| 425 | |
---|
| 426 | // Parse the data. |
---|
| 427 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
| 428 | return; |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 432 | txl->list = td->noretweets_ids; |
---|
| 433 | |
---|
| 434 | // Process the retweet ids |
---|
| 435 | txl->type = TXL_ID; |
---|
| 436 | if (parsed->type == json_array) { |
---|
| 437 | unsigned int i; |
---|
| 438 | for (i = 0; i < parsed->u.array.length; i++) { |
---|
| 439 | json_value *c = parsed->u.array.values[i]; |
---|
| 440 | if (c->type != json_integer) { |
---|
| 441 | continue; |
---|
| 442 | } |
---|
| 443 | txl->list = g_slist_prepend(txl->list, |
---|
| 444 | g_strdup_printf("%"PRIu64, c->u.integer)); |
---|
| 445 | } |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | json_value_free(parsed); |
---|
| 449 | td->noretweets_ids = txl->list; |
---|
| 450 | |
---|
| 451 | txl->list = NULL; |
---|
| 452 | txl_free(txl); |
---|
| 453 | } |
---|
| 454 | |
---|
[9e8c945] | 455 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl); |
---|
[de923d5] | 456 | static void twitter_http_get_users_lookup(struct http_request *req); |
---|
| 457 | |
---|
| 458 | static void twitter_get_users_lookup(struct im_connection *ic) |
---|
| 459 | { |
---|
| 460 | struct twitter_data *td = ic->proto_data; |
---|
| 461 | char *args[2] = { |
---|
| 462 | "user_id", |
---|
| 463 | NULL, |
---|
| 464 | }; |
---|
| 465 | GString *ids = g_string_new(""); |
---|
| 466 | int i; |
---|
[5ebff60] | 467 | |
---|
[de923d5] | 468 | /* We can request up to 100 users at a time. */ |
---|
[5ebff60] | 469 | for (i = 0; i < 100 && td->follow_ids; i++) { |
---|
| 470 | g_string_append_printf(ids, ",%s", (char *) td->follow_ids->data); |
---|
[de923d5] | 471 | g_free(td->follow_ids->data); |
---|
| 472 | td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); |
---|
| 473 | } |
---|
| 474 | if (ids->len > 0) { |
---|
| 475 | args[1] = ids->str + 1; |
---|
| 476 | /* POST, because I think ids can be up to 1KB long. */ |
---|
| 477 | twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); |
---|
| 478 | } else { |
---|
| 479 | /* We have all users. Continue with login. (Get statuses.) */ |
---|
| 480 | td->flags |= TWITTER_HAVE_FRIENDS; |
---|
| 481 | twitter_login_finish(ic); |
---|
| 482 | } |
---|
| 483 | g_string_free(ids, TRUE); |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | /** |
---|
| 487 | * Callback for getting (twitter)friends... |
---|
| 488 | * |
---|
[5ebff60] | 489 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
| 490 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
[de923d5] | 491 | * BitlBee... Get a life and meet new people! |
---|
| 492 | */ |
---|
| 493 | static void twitter_http_get_users_lookup(struct http_request *req) |
---|
| 494 | { |
---|
| 495 | struct im_connection *ic = req->data; |
---|
[e08ae0c] | 496 | json_value *parsed; |
---|
[de923d5] | 497 | struct twitter_xml_list *txl; |
---|
| 498 | GSList *l = NULL; |
---|
| 499 | struct twitter_xml_user *user; |
---|
| 500 | |
---|
| 501 | // Check if the connection is still active. |
---|
[5ebff60] | 502 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[de923d5] | 503 | return; |
---|
[5ebff60] | 504 | } |
---|
[de923d5] | 505 | |
---|
| 506 | // Get the user list from the parsed xml feed. |
---|
[5ebff60] | 507 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[2a6da96] | 508 | return; |
---|
[5ebff60] | 509 | } |
---|
[bbff22d] | 510 | |
---|
| 511 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 512 | txl->list = NULL; |
---|
| 513 | |
---|
[d0752e8] | 514 | twitter_xt_get_users(parsed, txl); |
---|
[e08ae0c] | 515 | json_value_free(parsed); |
---|
[de923d5] | 516 | |
---|
| 517 | // Add the users as buddies. |
---|
| 518 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 519 | user = l->data; |
---|
| 520 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | // Free the structure. |
---|
[62d2cfb] | 524 | txl_free(txl); |
---|
[de923d5] | 525 | |
---|
| 526 | twitter_get_users_lookup(ic); |
---|
[1b221e0] | 527 | } |
---|
| 528 | |
---|
[8e3b7ac] | 529 | struct twitter_xml_user *twitter_xt_get_user(const json_value *node) |
---|
| 530 | { |
---|
| 531 | struct twitter_xml_user *txu; |
---|
[73ee390] | 532 | json_value *jv; |
---|
[5ebff60] | 533 | |
---|
[8e3b7ac] | 534 | txu = g_new0(struct twitter_xml_user, 1); |
---|
| 535 | txu->name = g_strdup(json_o_str(node, "name")); |
---|
| 536 | txu->screen_name = g_strdup(json_o_str(node, "screen_name")); |
---|
[5ebff60] | 537 | |
---|
[73ee390] | 538 | jv = json_o_get(node, "id"); |
---|
| 539 | txu->uid = jv->u.integer; |
---|
[5ebff60] | 540 | |
---|
[8e3b7ac] | 541 | return txu; |
---|
| 542 | } |
---|
| 543 | |
---|
[62d2cfb] | 544 | /** |
---|
| 545 | * Function to fill a twitter_xml_list struct. |
---|
| 546 | * It sets: |
---|
| 547 | * - all <user>s from the <users> element. |
---|
| 548 | */ |
---|
[9e8c945] | 549 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl) |
---|
[62d2cfb] | 550 | { |
---|
| 551 | struct twitter_xml_user *txu; |
---|
[e08ae0c] | 552 | int i; |
---|
[62d2cfb] | 553 | |
---|
| 554 | // Set the type of the list. |
---|
| 555 | txl->type = TXL_USER; |
---|
| 556 | |
---|
[5ebff60] | 557 | if (!node || node->type != json_array) { |
---|
[9e8c945] | 558 | return FALSE; |
---|
[5ebff60] | 559 | } |
---|
[e08ae0c] | 560 | |
---|
[62d2cfb] | 561 | // The root <users> node should hold the list of users <user> |
---|
| 562 | // Walk over the nodes children. |
---|
[5ebff60] | 563 | for (i = 0; i < node->u.array.length; i++) { |
---|
[8e3b7ac] | 564 | txu = twitter_xt_get_user(node->u.array.values[i]); |
---|
[5ebff60] | 565 | if (txu) { |
---|
[8e3b7ac] | 566 | txl->list = g_slist_prepend(txl->list, txu); |
---|
[5ebff60] | 567 | } |
---|
[62d2cfb] | 568 | } |
---|
| 569 | |
---|
[9e8c945] | 570 | return TRUE; |
---|
[62d2cfb] | 571 | } |
---|
| 572 | |
---|
[2b02617] | 573 | #ifdef __GLIBC__ |
---|
| 574 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y" |
---|
| 575 | #else |
---|
| 576 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y" |
---|
| 577 | #endif |
---|
[62d2cfb] | 578 | |
---|
[cd60710] | 579 | static void expand_entities(char **text, const json_value *node); |
---|
[d1356cb] | 580 | |
---|
[1b221e0] | 581 | /** |
---|
| 582 | * Function to fill a twitter_xml_status struct. |
---|
| 583 | * It sets: |
---|
| 584 | * - the status text and |
---|
| 585 | * - the created_at timestamp and |
---|
| 586 | * - the status id and |
---|
| 587 | * - the user in a twitter_xml_user struct. |
---|
| 588 | */ |
---|
[c9b5817] | 589 | static struct twitter_xml_status *twitter_xt_get_status(const json_value *node) |
---|
[1b221e0] | 590 | { |
---|
[c9b5817] | 591 | struct twitter_xml_status *txs; |
---|
[cd60710] | 592 | const json_value *rt = NULL; |
---|
[5ebff60] | 593 | |
---|
| 594 | if (node->type != json_object) { |
---|
[9e8c945] | 595 | return FALSE; |
---|
[5ebff60] | 596 | } |
---|
[c9b5817] | 597 | txs = g_new0(struct twitter_xml_status, 1); |
---|
[1b221e0] | 598 | |
---|
[5ebff60] | 599 | JSON_O_FOREACH(node, k, v) { |
---|
[8e3b7ac] | 600 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
[fb351ce] | 601 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
[29f72b7] | 602 | strip_html(txs->text); |
---|
[8e3b7ac] | 603 | } else if (strcmp("retweeted_status", k) == 0 && v->type == json_object) { |
---|
| 604 | rt = v; |
---|
| 605 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
[08579a1] | 606 | struct tm parsed; |
---|
[5983eca] | 607 | |
---|
[08579a1] | 608 | /* Very sensitive to changes to the formatting of |
---|
| 609 | this field. :-( Also assumes the timezone used |
---|
| 610 | is UTC since C time handling functions suck. */ |
---|
[5ebff60] | 611 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) { |
---|
[5983eca] | 612 | txs->created_at = mktime_utc(&parsed); |
---|
[5ebff60] | 613 | } |
---|
[8e3b7ac] | 614 | } else if (strcmp("user", k) == 0 && v->type == json_object) { |
---|
| 615 | txs->user = twitter_xt_get_user(v); |
---|
| 616 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
[573e274] | 617 | txs->rt_id = txs->id = v->u.integer; |
---|
[8e3b7ac] | 618 | } else if (strcmp("in_reply_to_status_id", k) == 0 && v->type == json_integer) { |
---|
| 619 | txs->reply_to = v->u.integer; |
---|
[ce81acd] | 620 | } |
---|
[1b221e0] | 621 | } |
---|
[5983eca] | 622 | |
---|
[0fff0b2] | 623 | /* If it's a (truncated) retweet, get the original. Even if the API claims it |
---|
| 624 | wasn't truncated because it may be lying. */ |
---|
| 625 | if (rt) { |
---|
[c9b5817] | 626 | struct twitter_xml_status *rtxs = twitter_xt_get_status(rt); |
---|
| 627 | if (rtxs) { |
---|
| 628 | g_free(txs->text); |
---|
[0ca1d79] | 629 | txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text); |
---|
[c9b5817] | 630 | txs->id = rtxs->id; |
---|
[0ca1d79] | 631 | txs_free(rtxs); |
---|
[e193aeb] | 632 | } |
---|
[cd60710] | 633 | } else { |
---|
| 634 | expand_entities(&txs->text, node); |
---|
[d1356cb] | 635 | } |
---|
| 636 | |
---|
[5ebff60] | 637 | if (txs->text && txs->user && txs->id) { |
---|
[c9b5817] | 638 | return txs; |
---|
[5ebff60] | 639 | } |
---|
| 640 | |
---|
[c9b5817] | 641 | txs_free(txs); |
---|
| 642 | return NULL; |
---|
[d1356cb] | 643 | } |
---|
| 644 | |
---|
| 645 | /** |
---|
| 646 | * Function to fill a twitter_xml_status struct (DM variant). |
---|
| 647 | */ |
---|
[2cd8540] | 648 | static struct twitter_xml_status *twitter_xt_get_dm(const json_value *node) |
---|
[d1356cb] | 649 | { |
---|
[2cd8540] | 650 | struct twitter_xml_status *txs; |
---|
[5ebff60] | 651 | |
---|
| 652 | if (node->type != json_object) { |
---|
[d1356cb] | 653 | return FALSE; |
---|
[5ebff60] | 654 | } |
---|
[2cd8540] | 655 | txs = g_new0(struct twitter_xml_status, 1); |
---|
[d1356cb] | 656 | |
---|
[5ebff60] | 657 | JSON_O_FOREACH(node, k, v) { |
---|
[d1356cb] | 658 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
| 659 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
[29f72b7] | 660 | strip_html(txs->text); |
---|
[d1356cb] | 661 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
| 662 | struct tm parsed; |
---|
| 663 | |
---|
| 664 | /* Very sensitive to changes to the formatting of |
---|
| 665 | this field. :-( Also assumes the timezone used |
---|
| 666 | is UTC since C time handling functions suck. */ |
---|
[5ebff60] | 667 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) { |
---|
[d1356cb] | 668 | txs->created_at = mktime_utc(&parsed); |
---|
[5ebff60] | 669 | } |
---|
[d1356cb] | 670 | } else if (strcmp("sender", k) == 0 && v->type == json_object) { |
---|
| 671 | txs->user = twitter_xt_get_user(v); |
---|
| 672 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
| 673 | txs->id = v->u.integer; |
---|
| 674 | } |
---|
| 675 | } |
---|
| 676 | |
---|
[cd60710] | 677 | expand_entities(&txs->text, node); |
---|
[d1356cb] | 678 | |
---|
[5ebff60] | 679 | if (txs->text && txs->user && txs->id) { |
---|
[2cd8540] | 680 | return txs; |
---|
[5ebff60] | 681 | } |
---|
| 682 | |
---|
[2cd8540] | 683 | txs_free(txs); |
---|
| 684 | return NULL; |
---|
[d1356cb] | 685 | } |
---|
| 686 | |
---|
[cd60710] | 687 | static void expand_entities(char **text, const json_value *node) |
---|
[5ebff60] | 688 | { |
---|
[cd60710] | 689 | json_value *entities, *quoted; |
---|
| 690 | char *quote_url = NULL, *quote_text = NULL; |
---|
| 691 | |
---|
| 692 | if (!((entities = json_o_get(node, "entities")) && entities->type == json_object)) |
---|
| 693 | return; |
---|
| 694 | if ((quoted = json_o_get(node, "quoted_status")) && quoted->type == json_object) { |
---|
| 695 | /* New "retweets with comments" feature. Note that this info |
---|
| 696 | * seems to be included in the streaming API only! Grab the |
---|
| 697 | * full message and try to insert it when we run into the |
---|
| 698 | * Tweet entity. */ |
---|
| 699 | struct twitter_xml_status *txs = twitter_xt_get_status(quoted); |
---|
| 700 | quote_text = g_strdup_printf("@%s: %s", txs->user->screen_name, txs->text); |
---|
| 701 | quote_url = g_strdup_printf("%s/status/%" G_GUINT64_FORMAT, txs->user->screen_name, txs->id); |
---|
| 702 | txs_free(txs); |
---|
| 703 | } else { |
---|
| 704 | quoted = NULL; |
---|
| 705 | } |
---|
| 706 | |
---|
[5ebff60] | 707 | JSON_O_FOREACH(entities, k, v) { |
---|
[d1356cb] | 708 | int i; |
---|
[5ebff60] | 709 | |
---|
| 710 | if (v->type != json_array) { |
---|
[d1356cb] | 711 | continue; |
---|
[5ebff60] | 712 | } |
---|
| 713 | if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0) { |
---|
[d1356cb] | 714 | continue; |
---|
[5ebff60] | 715 | } |
---|
| 716 | |
---|
| 717 | for (i = 0; i < v->u.array.length; i++) { |
---|
[cd60710] | 718 | const char *format = "%s%s <%s>%s"; |
---|
| 719 | |
---|
[5ebff60] | 720 | if (v->u.array.values[i]->type != json_object) { |
---|
[8e3b7ac] | 721 | continue; |
---|
[5ebff60] | 722 | } |
---|
| 723 | |
---|
[d1356cb] | 724 | const char *kort = json_o_str(v->u.array.values[i], "url"); |
---|
| 725 | const char *disp = json_o_str(v->u.array.values[i], "display_url"); |
---|
[cd60710] | 726 | const char *full = json_o_str(v->u.array.values[i], "expanded_url"); |
---|
[d1356cb] | 727 | char *pos, *new; |
---|
[5ebff60] | 728 | |
---|
[cd60710] | 729 | if (!kort || !disp || !(pos = strstr(*text, kort))) { |
---|
[429a9b1] | 730 | continue; |
---|
[5ebff60] | 731 | } |
---|
[cd60710] | 732 | if (quote_url && strstr(full, quote_url)) { |
---|
| 733 | format = "%s<%s> [%s]%s"; |
---|
| 734 | disp = quote_text; |
---|
| 735 | } |
---|
[5ebff60] | 736 | |
---|
[d1356cb] | 737 | *pos = '\0'; |
---|
[cd60710] | 738 | new = g_strdup_printf(format, *text, kort, |
---|
[d1356cb] | 739 | disp, pos + strlen(kort)); |
---|
[5ebff60] | 740 | |
---|
[cd60710] | 741 | g_free(*text); |
---|
| 742 | *text = new; |
---|
[429a9b1] | 743 | } |
---|
[e193aeb] | 744 | } |
---|
[cd60710] | 745 | g_free(quote_text); |
---|
| 746 | g_free(quote_url); |
---|
[1b221e0] | 747 | } |
---|
| 748 | |
---|
| 749 | /** |
---|
| 750 | * Function to fill a twitter_xml_list struct. |
---|
| 751 | * It sets: |
---|
| 752 | * - all <status>es within the <status> element and |
---|
| 753 | * - the next_cursor. |
---|
| 754 | */ |
---|
[9e8c945] | 755 | static gboolean twitter_xt_get_status_list(struct im_connection *ic, const json_value *node, |
---|
| 756 | struct twitter_xml_list *txl) |
---|
[1b221e0] | 757 | { |
---|
| 758 | struct twitter_xml_status *txs; |
---|
[8e3b7ac] | 759 | int i; |
---|
[1b221e0] | 760 | |
---|
[62d2cfb] | 761 | // Set the type of the list. |
---|
| 762 | txl->type = TXL_STATUS; |
---|
[5ebff60] | 763 | |
---|
| 764 | if (node->type != json_array) { |
---|
[9e8c945] | 765 | return FALSE; |
---|
[5ebff60] | 766 | } |
---|
[62d2cfb] | 767 | |
---|
[1b221e0] | 768 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 769 | // Walk over the nodes children. |
---|
[5ebff60] | 770 | for (i = 0; i < node->u.array.length; i++) { |
---|
[c9b5817] | 771 | txs = twitter_xt_get_status(node->u.array.values[i]); |
---|
[5ebff60] | 772 | if (!txs) { |
---|
[c9b5817] | 773 | continue; |
---|
[5ebff60] | 774 | } |
---|
| 775 | |
---|
[8e3b7ac] | 776 | txl->list = g_slist_prepend(txl->list, txs); |
---|
[1b221e0] | 777 | } |
---|
| 778 | |
---|
[9e8c945] | 779 | return TRUE; |
---|
[1b221e0] | 780 | } |
---|
| 781 | |
---|
[c9b5817] | 782 | /* Will log messages either way. Need to keep track of IDs for stream deduping. |
---|
| 783 | Plus, show_ids is on by default and I don't see why anyone would disable it. */ |
---|
[ce81acd] | 784 | static char *twitter_msg_add_id(struct im_connection *ic, |
---|
[5ebff60] | 785 | struct twitter_xml_status *txs, const char *prefix) |
---|
[ce81acd] | 786 | { |
---|
| 787 | struct twitter_data *td = ic->proto_data; |
---|
[c9b5817] | 788 | int reply_to = -1; |
---|
| 789 | bee_user_t *bu; |
---|
[5983eca] | 790 | |
---|
| 791 | if (txs->reply_to) { |
---|
[ce81acd] | 792 | int i; |
---|
[5ebff60] | 793 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
[5983eca] | 794 | if (td->log[i].id == txs->reply_to) { |
---|
[c9b5817] | 795 | reply_to = i; |
---|
[ce81acd] | 796 | break; |
---|
| 797 | } |
---|
[5ebff60] | 798 | } |
---|
[ce81acd] | 799 | } |
---|
[5983eca] | 800 | |
---|
[c9b5817] | 801 | if (txs->user && txs->user->screen_name && |
---|
| 802 | (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
| 803 | struct twitter_user_data *tud = bu->data; |
---|
| 804 | |
---|
| 805 | if (txs->id > tud->last_id) { |
---|
| 806 | tud->last_id = txs->id; |
---|
| 807 | tud->last_time = txs->created_at; |
---|
| 808 | } |
---|
| 809 | } |
---|
[5ebff60] | 810 | |
---|
[c9b5817] | 811 | td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; |
---|
| 812 | td->log[td->log_id].id = txs->id; |
---|
| 813 | td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); |
---|
[5ebff60] | 814 | |
---|
[67f6828] | 815 | /* This is all getting hairy. :-( If we RT'ed something ourselves, |
---|
| 816 | remember OUR id instead so undo will work. In other cases, the |
---|
| 817 | original tweet's id should be remembered for deduplicating. */ |
---|
[5ebff60] | 818 | if (g_strcasecmp(txs->user->screen_name, td->user) == 0) { |
---|
[67f6828] | 819 | td->log[td->log_id].id = txs->rt_id; |
---|
[c43146d] | 820 | /* More useful than NULL. */ |
---|
| 821 | td->log[td->log_id].bu = &twitter_log_local_user; |
---|
[5ebff60] | 822 | } |
---|
| 823 | |
---|
[0ca1d79] | 824 | if (set_getbool(&ic->acc->set, "show_ids")) { |
---|
[5ebff60] | 825 | if (reply_to != -1) { |
---|
[0ca1d79] | 826 | return g_strdup_printf("\002[\002%02x->%02x\002]\002 %s%s", |
---|
| 827 | td->log_id, reply_to, prefix, txs->text); |
---|
[5ebff60] | 828 | } else { |
---|
[0ca1d79] | 829 | return g_strdup_printf("\002[\002%02x\002]\002 %s%s", |
---|
| 830 | td->log_id, prefix, txs->text); |
---|
[5ebff60] | 831 | } |
---|
[0ca1d79] | 832 | } else { |
---|
[5ebff60] | 833 | if (*prefix) { |
---|
[0ca1d79] | 834 | return g_strconcat(prefix, txs->text, NULL); |
---|
[5ebff60] | 835 | } else { |
---|
[0ca1d79] | 836 | return NULL; |
---|
[5ebff60] | 837 | } |
---|
[0ca1d79] | 838 | } |
---|
[ce81acd] | 839 | } |
---|
| 840 | |
---|
[73ee390] | 841 | /** |
---|
| 842 | * Function that is called to see the filter statuses in groupchat windows. |
---|
| 843 | */ |
---|
| 844 | static void twitter_status_show_filter(struct im_connection *ic, struct twitter_xml_status *status) |
---|
| 845 | { |
---|
| 846 | struct twitter_data *td = ic->proto_data; |
---|
| 847 | char *msg = twitter_msg_add_id(ic, status, ""); |
---|
| 848 | struct twitter_filter *tf; |
---|
| 849 | GSList *f; |
---|
| 850 | GSList *l; |
---|
| 851 | |
---|
| 852 | for (f = td->filters; f; f = g_slist_next(f)) { |
---|
| 853 | tf = f->data; |
---|
| 854 | |
---|
| 855 | switch (tf->type) { |
---|
| 856 | case TWITTER_FILTER_TYPE_FOLLOW: |
---|
[5ebff60] | 857 | if (status->user->uid != tf->uid) { |
---|
[73ee390] | 858 | continue; |
---|
[5ebff60] | 859 | } |
---|
[73ee390] | 860 | break; |
---|
| 861 | |
---|
| 862 | case TWITTER_FILTER_TYPE_TRACK: |
---|
[5ebff60] | 863 | if (strcasestr(status->text, tf->text) == NULL) { |
---|
[73ee390] | 864 | continue; |
---|
[5ebff60] | 865 | } |
---|
[73ee390] | 866 | break; |
---|
| 867 | |
---|
| 868 | default: |
---|
| 869 | continue; |
---|
| 870 | } |
---|
| 871 | |
---|
| 872 | for (l = tf->groupchats; l; l = g_slist_next(l)) { |
---|
| 873 | imcb_chat_msg(l->data, status->user->screen_name, |
---|
[5ebff60] | 874 | msg ? msg : status->text, 0, 0); |
---|
[73ee390] | 875 | } |
---|
| 876 | } |
---|
| 877 | |
---|
| 878 | g_free(msg); |
---|
| 879 | } |
---|
| 880 | |
---|
[62d2cfb] | 881 | /** |
---|
| 882 | * Function that is called to see the statuses in a groupchat window. |
---|
| 883 | */ |
---|
[29f72b7] | 884 | static void twitter_status_show_chat(struct im_connection *ic, struct twitter_xml_status *status) |
---|
[62d2cfb] | 885 | { |
---|
| 886 | struct twitter_data *td = ic->proto_data; |
---|
| 887 | struct groupchat *gc; |
---|
[29f72b7] | 888 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
| 889 | char *msg; |
---|
[62d2cfb] | 890 | |
---|
| 891 | // Create a new groupchat if it does not exsist. |
---|
[631ec80] | 892 | gc = twitter_groupchat_init(ic); |
---|
[62d2cfb] | 893 | |
---|
[5ebff60] | 894 | if (!me) { |
---|
[29f72b7] | 895 | /* MUST be done before twitter_msg_add_id() to avoid #872. */ |
---|
| 896 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
[5ebff60] | 897 | } |
---|
[29f72b7] | 898 | msg = twitter_msg_add_id(ic, status, ""); |
---|
[5ebff60] | 899 | |
---|
[29f72b7] | 900 | // Say it! |
---|
| 901 | if (me) { |
---|
| 902 | imcb_chat_log(gc, "You: %s", msg ? msg : status->text); |
---|
| 903 | } else { |
---|
| 904 | imcb_chat_msg(gc, status->user->screen_name, |
---|
[5ebff60] | 905 | msg ? msg : status->text, 0, status->created_at); |
---|
[62d2cfb] | 906 | } |
---|
[29f72b7] | 907 | |
---|
| 908 | g_free(msg); |
---|
[62d2cfb] | 909 | } |
---|
| 910 | |
---|
| 911 | /** |
---|
| 912 | * Function that is called to see statuses as private messages. |
---|
| 913 | */ |
---|
[29f72b7] | 914 | static void twitter_status_show_msg(struct im_connection *ic, struct twitter_xml_status *status) |
---|
[62d2cfb] | 915 | { |
---|
| 916 | struct twitter_data *td = ic->proto_data; |
---|
[631ec80] | 917 | char from[MAX_STRING] = ""; |
---|
[29f72b7] | 918 | char *prefix = NULL, *text = NULL; |
---|
| 919 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
[62d2cfb] | 920 | |
---|
[631ec80] | 921 | if (td->flags & TWITTER_MODE_ONE) { |
---|
[5983eca] | 922 | g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user); |
---|
| 923 | from[MAX_STRING - 1] = '\0'; |
---|
[e88fbe27] | 924 | } |
---|
[5983eca] | 925 | |
---|
[5ebff60] | 926 | if (td->flags & TWITTER_MODE_ONE) { |
---|
[29f72b7] | 927 | prefix = g_strdup_printf("\002<\002%s\002>\002 ", |
---|
| 928 | status->user->screen_name); |
---|
[5ebff60] | 929 | } else if (!me) { |
---|
[29f72b7] | 930 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
[5ebff60] | 931 | } else { |
---|
[29f72b7] | 932 | prefix = g_strdup("You: "); |
---|
[5ebff60] | 933 | } |
---|
[5983eca] | 934 | |
---|
[29f72b7] | 935 | text = twitter_msg_add_id(ic, status, prefix ? prefix : ""); |
---|
[5983eca] | 936 | |
---|
[29f72b7] | 937 | imcb_buddy_msg(ic, |
---|
| 938 | *from ? from : status->user->screen_name, |
---|
| 939 | text ? text : status->text, 0, status->created_at); |
---|
[5983eca] | 940 | |
---|
[29f72b7] | 941 | g_free(text); |
---|
| 942 | g_free(prefix); |
---|
| 943 | } |
---|
[5983eca] | 944 | |
---|
[29f72b7] | 945 | static void twitter_status_show(struct im_connection *ic, struct twitter_xml_status *status) |
---|
| 946 | { |
---|
| 947 | struct twitter_data *td = ic->proto_data; |
---|
[7549d00] | 948 | char *last_id_str; |
---|
[fb62f132] | 949 | char *uid_str; |
---|
[5ebff60] | 950 | |
---|
| 951 | if (status->user == NULL || status->text == NULL) { |
---|
[29f72b7] | 952 | return; |
---|
[5ebff60] | 953 | } |
---|
[fb62f132] | 954 | |
---|
| 955 | /* Check this is not a tweet that should be muted */ |
---|
| 956 | uid_str = g_strdup_printf("%" PRIu64, status->user->uid); |
---|
| 957 | if (g_slist_find_custom(td->mutes_ids, uid_str, (GCompareFunc)strcmp)) { |
---|
| 958 | g_free(uid_str); |
---|
| 959 | return; |
---|
| 960 | } |
---|
| 961 | if (status->id != status->rt_id && g_slist_find_custom(td->noretweets_ids, uid_str, (GCompareFunc)strcmp)) { |
---|
| 962 | g_free(uid_str); |
---|
| 963 | return; |
---|
| 964 | } |
---|
[5ebff60] | 965 | |
---|
[29f72b7] | 966 | /* Grrrr. Would like to do this during parsing, but can't access |
---|
| 967 | settings from there. */ |
---|
[5ebff60] | 968 | if (set_getbool(&ic->acc->set, "strip_newlines")) { |
---|
[29f72b7] | 969 | strip_newlines(status->text); |
---|
[5ebff60] | 970 | } |
---|
| 971 | |
---|
| 972 | if (status->from_filter) { |
---|
[73ee390] | 973 | twitter_status_show_filter(ic, status); |
---|
[5ebff60] | 974 | } else if (td->flags & TWITTER_MODE_CHAT) { |
---|
[29f72b7] | 975 | twitter_status_show_chat(ic, status); |
---|
[5ebff60] | 976 | } else { |
---|
[29f72b7] | 977 | twitter_status_show_msg(ic, status); |
---|
[5ebff60] | 978 | } |
---|
[5983eca] | 979 | |
---|
[29f72b7] | 980 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
| 981 | // we won't pick up the updates already in the list. |
---|
[573e274] | 982 | td->timeline_id = MAX(td->timeline_id, status->rt_id); |
---|
[7549d00] | 983 | |
---|
| 984 | last_id_str = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
[fedc8f1] | 985 | set_setstr(&ic->acc->set, "_last_tweet", last_id_str); |
---|
[7549d00] | 986 | g_free(last_id_str); |
---|
[fb62f132] | 987 | g_free(uid_str); |
---|
[62d2cfb] | 988 | } |
---|
| 989 | |
---|
[73ee390] | 990 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o, gboolean from_filter); |
---|
[ddc2de5] | 991 | |
---|
| 992 | static void twitter_http_stream(struct http_request *req) |
---|
| 993 | { |
---|
[dff0e0b] | 994 | struct im_connection *ic = req->data; |
---|
[dd672e2] | 995 | struct twitter_data *td; |
---|
[dff0e0b] | 996 | json_value *parsed; |
---|
[62f6b45] | 997 | int len = 0; |
---|
| 998 | char c, *nl; |
---|
[73ee390] | 999 | gboolean from_filter; |
---|
[5ebff60] | 1000 | |
---|
| 1001 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[dff0e0b] | 1002 | return; |
---|
[5ebff60] | 1003 | } |
---|
| 1004 | |
---|
[e132b60] | 1005 | ic->flags |= OPT_PONGED; |
---|
[dd672e2] | 1006 | td = ic->proto_data; |
---|
[5ebff60] | 1007 | |
---|
[dd672e2] | 1008 | if ((req->flags & HTTPC_EOF) || !req->reply_body) { |
---|
[5ebff60] | 1009 | if (req == td->stream) { |
---|
[73ee390] | 1010 | td->stream = NULL; |
---|
[5ebff60] | 1011 | } else if (req == td->filter_stream) { |
---|
[73ee390] | 1012 | td->filter_stream = NULL; |
---|
[5ebff60] | 1013 | } |
---|
[73ee390] | 1014 | |
---|
[dd672e2] | 1015 | imcb_error(ic, "Stream closed (%s)", req->status_string); |
---|
[7320610] | 1016 | if (req->status_code == 401) { |
---|
| 1017 | imcb_error(ic, "Check your system clock."); |
---|
| 1018 | } |
---|
[dd672e2] | 1019 | imc_logout(ic, TRUE); |
---|
| 1020 | return; |
---|
| 1021 | } |
---|
[5ebff60] | 1022 | |
---|
[62f6b45] | 1023 | /* MUST search for CRLF, not just LF: |
---|
| 1024 | https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */ |
---|
[5ebff60] | 1025 | if (!(nl = strstr(req->reply_body, "\r\n"))) { |
---|
[ddc2de5] | 1026 | return; |
---|
[5ebff60] | 1027 | } |
---|
| 1028 | |
---|
[62f6b45] | 1029 | len = nl - req->reply_body; |
---|
| 1030 | if (len > 0) { |
---|
| 1031 | c = req->reply_body[len]; |
---|
| 1032 | req->reply_body[len] = '\0'; |
---|
[5ebff60] | 1033 | |
---|
[7a80925] | 1034 | if ((parsed = json_parse(req->reply_body, req->body_size))) { |
---|
[73ee390] | 1035 | from_filter = (req == td->filter_stream); |
---|
| 1036 | twitter_stream_handle_object(ic, parsed, from_filter); |
---|
[62f6b45] | 1037 | } |
---|
| 1038 | json_value_free(parsed); |
---|
| 1039 | req->reply_body[len] = c; |
---|
[dff0e0b] | 1040 | } |
---|
[5ebff60] | 1041 | |
---|
[62f6b45] | 1042 | http_flush_bytes(req, len + 2); |
---|
[5ebff60] | 1043 | |
---|
[dff0e0b] | 1044 | /* One notification might bring multiple events! */ |
---|
[5ebff60] | 1045 | if (req->body_size > 0) { |
---|
[dff0e0b] | 1046 | twitter_http_stream(req); |
---|
[5ebff60] | 1047 | } |
---|
[ddc2de5] | 1048 | } |
---|
[2322a9f] | 1049 | |
---|
[5aa96fc8] | 1050 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o); |
---|
[c9b5817] | 1051 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs); |
---|
[5aa96fc8] | 1052 | |
---|
[73ee390] | 1053 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o, gboolean from_filter) |
---|
[dff0e0b] | 1054 | { |
---|
[5aa96fc8] | 1055 | struct twitter_data *td = ic->proto_data; |
---|
[c9b5817] | 1056 | struct twitter_xml_status *txs; |
---|
[d1356cb] | 1057 | json_value *c; |
---|
[5ebff60] | 1058 | |
---|
[c9b5817] | 1059 | if ((txs = twitter_xt_get_status(o))) { |
---|
[73ee390] | 1060 | txs->from_filter = from_filter; |
---|
[2dcde94] | 1061 | gboolean ret = twitter_stream_handle_status(ic, txs); |
---|
| 1062 | txs_free(txs); |
---|
| 1063 | return ret; |
---|
[d1356cb] | 1064 | } else if ((c = json_o_get(o, "direct_message")) && |
---|
[2cd8540] | 1065 | (txs = twitter_xt_get_dm(c))) { |
---|
[5ebff60] | 1066 | if (g_strcasecmp(txs->user->screen_name, td->user) != 0) { |
---|
[2cd8540] | 1067 | imcb_buddy_msg(ic, txs->user->screen_name, |
---|
[5ebff60] | 1068 | txs->text, 0, txs->created_at); |
---|
| 1069 | } |
---|
[d1356cb] | 1070 | txs_free(txs); |
---|
| 1071 | return TRUE; |
---|
[5aa96fc8] | 1072 | } else if ((c = json_o_get(o, "event")) && c->type == json_string) { |
---|
| 1073 | twitter_stream_handle_event(ic, o); |
---|
| 1074 | return TRUE; |
---|
| 1075 | } else if ((c = json_o_get(o, "disconnect")) && c->type == json_object) { |
---|
| 1076 | /* HACK: Because we're inside an event handler, we can't just |
---|
| 1077 | disconnect here. Instead, just change the HTTP status string |
---|
| 1078 | into a Twitter status string. */ |
---|
| 1079 | char *reason = json_o_strdup(c, "reason"); |
---|
| 1080 | if (reason) { |
---|
| 1081 | g_free(td->stream->status_string); |
---|
| 1082 | td->stream->status_string = reason; |
---|
| 1083 | } |
---|
| 1084 | return TRUE; |
---|
[dff0e0b] | 1085 | } |
---|
| 1086 | return FALSE; |
---|
| 1087 | } |
---|
| 1088 | |
---|
[c9b5817] | 1089 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs) |
---|
| 1090 | { |
---|
| 1091 | struct twitter_data *td = ic->proto_data; |
---|
| 1092 | int i; |
---|
[5ebff60] | 1093 | |
---|
[c9b5817] | 1094 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
| 1095 | if (td->log[i].id == txs->id) { |
---|
[29f72b7] | 1096 | /* Got a duplicate (RT, probably). Drop it. */ |
---|
[c9b5817] | 1097 | return TRUE; |
---|
| 1098 | } |
---|
| 1099 | } |
---|
[5ebff60] | 1100 | |
---|
[9f8bb17] | 1101 | if (!(g_strcasecmp(txs->user->screen_name, td->user) == 0 || |
---|
[2dcde94] | 1102 | set_getbool(&ic->acc->set, "fetch_mentions") || |
---|
[c9b5817] | 1103 | bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
| 1104 | /* Tweet is from an unknown person and the user does not want |
---|
| 1105 | to see @mentions, so drop it. twitter_stream_handle_event() |
---|
| 1106 | picks up new follows so this simple filter should be safe. */ |
---|
| 1107 | /* TODO: The streaming API seems to do poor @mention matching. |
---|
| 1108 | I.e. I'm getting mentions for @WilmerSomething, not just for |
---|
| 1109 | @Wilmer. But meh. You want spam, you get spam. */ |
---|
| 1110 | return TRUE; |
---|
| 1111 | } |
---|
[5ebff60] | 1112 | |
---|
[29f72b7] | 1113 | twitter_status_show(ic, txs); |
---|
[5ebff60] | 1114 | |
---|
[c9b5817] | 1115 | return TRUE; |
---|
| 1116 | } |
---|
| 1117 | |
---|
[5aa96fc8] | 1118 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o) |
---|
| 1119 | { |
---|
| 1120 | struct twitter_data *td = ic->proto_data; |
---|
| 1121 | json_value *source = json_o_get(o, "source"); |
---|
| 1122 | json_value *target = json_o_get(o, "target"); |
---|
| 1123 | const char *type = json_o_str(o, "event"); |
---|
[5ebff60] | 1124 | |
---|
[5aa96fc8] | 1125 | if (!type || !source || source->type != json_object |
---|
[5ebff60] | 1126 | || !target || target->type != json_object) { |
---|
[5aa96fc8] | 1127 | return FALSE; |
---|
| 1128 | } |
---|
[5ebff60] | 1129 | |
---|
[5aa96fc8] | 1130 | if (strcmp(type, "follow") == 0) { |
---|
| 1131 | struct twitter_xml_user *us = twitter_xt_get_user(source); |
---|
| 1132 | struct twitter_xml_user *ut = twitter_xt_get_user(target); |
---|
[9f8bb17] | 1133 | if (g_strcasecmp(us->screen_name, td->user) == 0) { |
---|
[5aa96fc8] | 1134 | twitter_add_buddy(ic, ut->screen_name, ut->name); |
---|
| 1135 | } |
---|
| 1136 | txu_free(us); |
---|
| 1137 | txu_free(ut); |
---|
| 1138 | } |
---|
[5ebff60] | 1139 | |
---|
[5aa96fc8] | 1140 | return TRUE; |
---|
| 1141 | } |
---|
| 1142 | |
---|
[dff0e0b] | 1143 | gboolean twitter_open_stream(struct im_connection *ic) |
---|
| 1144 | { |
---|
| 1145 | struct twitter_data *td = ic->proto_data; |
---|
[5ebff60] | 1146 | char *args[2] = { "with", "followings" }; |
---|
| 1147 | |
---|
[dff0e0b] | 1148 | if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL, |
---|
[62f6b45] | 1149 | twitter_http_stream, ic, 0, args, 2))) { |
---|
[dff0e0b] | 1150 | /* This flag must be enabled or we'll get no data until EOF |
---|
| 1151 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
| 1152 | td->stream->flags |= HTTPC_STREAMING; |
---|
| 1153 | return TRUE; |
---|
| 1154 | } |
---|
[5ebff60] | 1155 | |
---|
[dff0e0b] | 1156 | return FALSE; |
---|
| 1157 | } |
---|
| 1158 | |
---|
[73ee390] | 1159 | static gboolean twitter_filter_stream(struct im_connection *ic) |
---|
| 1160 | { |
---|
| 1161 | struct twitter_data *td = ic->proto_data; |
---|
[5ebff60] | 1162 | char *args[4] = { "follow", NULL, "track", NULL }; |
---|
[73ee390] | 1163 | GString *followstr = g_string_new(""); |
---|
| 1164 | GString *trackstr = g_string_new(""); |
---|
| 1165 | gboolean ret = FALSE; |
---|
| 1166 | struct twitter_filter *tf; |
---|
| 1167 | GSList *l; |
---|
| 1168 | |
---|
| 1169 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
| 1170 | tf = l->data; |
---|
| 1171 | |
---|
| 1172 | switch (tf->type) { |
---|
| 1173 | case TWITTER_FILTER_TYPE_FOLLOW: |
---|
[5ebff60] | 1174 | if (followstr->len > 0) { |
---|
[73ee390] | 1175 | g_string_append_c(followstr, ','); |
---|
[5ebff60] | 1176 | } |
---|
[73ee390] | 1177 | |
---|
| 1178 | g_string_append_printf(followstr, "%" G_GUINT64_FORMAT, |
---|
[5ebff60] | 1179 | tf->uid); |
---|
[73ee390] | 1180 | break; |
---|
| 1181 | |
---|
| 1182 | case TWITTER_FILTER_TYPE_TRACK: |
---|
[5ebff60] | 1183 | if (trackstr->len > 0) { |
---|
[73ee390] | 1184 | g_string_append_c(trackstr, ','); |
---|
[5ebff60] | 1185 | } |
---|
[73ee390] | 1186 | |
---|
| 1187 | g_string_append(trackstr, tf->text); |
---|
| 1188 | break; |
---|
| 1189 | |
---|
| 1190 | default: |
---|
| 1191 | continue; |
---|
| 1192 | } |
---|
| 1193 | } |
---|
| 1194 | |
---|
| 1195 | args[1] = followstr->str; |
---|
| 1196 | args[3] = trackstr->str; |
---|
| 1197 | |
---|
[5ebff60] | 1198 | if (td->filter_stream) { |
---|
[73ee390] | 1199 | http_close(td->filter_stream); |
---|
[5ebff60] | 1200 | } |
---|
[73ee390] | 1201 | |
---|
| 1202 | if ((td->filter_stream = twitter_http(ic, TWITTER_FILTER_STREAM_URL, |
---|
| 1203 | twitter_http_stream, ic, 0, |
---|
[5ebff60] | 1204 | args, 4))) { |
---|
[73ee390] | 1205 | /* This flag must be enabled or we'll get no data until EOF |
---|
| 1206 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
| 1207 | td->filter_stream->flags |= HTTPC_STREAMING; |
---|
| 1208 | ret = TRUE; |
---|
| 1209 | } |
---|
| 1210 | |
---|
| 1211 | g_string_free(followstr, TRUE); |
---|
| 1212 | g_string_free(trackstr, TRUE); |
---|
| 1213 | |
---|
| 1214 | return ret; |
---|
| 1215 | } |
---|
| 1216 | |
---|
| 1217 | static void twitter_filter_users_post(struct http_request *req) |
---|
| 1218 | { |
---|
| 1219 | struct im_connection *ic = req->data; |
---|
| 1220 | struct twitter_data *td; |
---|
| 1221 | struct twitter_filter *tf; |
---|
| 1222 | GList *users = NULL; |
---|
| 1223 | json_value *parsed; |
---|
| 1224 | json_value *id; |
---|
| 1225 | const char *name; |
---|
| 1226 | GString *fstr; |
---|
| 1227 | GSList *l; |
---|
| 1228 | GList *u; |
---|
| 1229 | int i; |
---|
| 1230 | |
---|
| 1231 | // Check if the connection is still active. |
---|
[5ebff60] | 1232 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[73ee390] | 1233 | return; |
---|
[5ebff60] | 1234 | } |
---|
[73ee390] | 1235 | |
---|
| 1236 | td = ic->proto_data; |
---|
| 1237 | |
---|
[5ebff60] | 1238 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[73ee390] | 1239 | return; |
---|
[5ebff60] | 1240 | } |
---|
[73ee390] | 1241 | |
---|
| 1242 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
| 1243 | tf = l->data; |
---|
| 1244 | |
---|
[5ebff60] | 1245 | if (tf->type == TWITTER_FILTER_TYPE_FOLLOW) { |
---|
[73ee390] | 1246 | users = g_list_prepend(users, tf); |
---|
[5ebff60] | 1247 | } |
---|
[73ee390] | 1248 | } |
---|
| 1249 | |
---|
[5ebff60] | 1250 | if (parsed->type != json_array) { |
---|
[73ee390] | 1251 | goto finish; |
---|
[5ebff60] | 1252 | } |
---|
[73ee390] | 1253 | |
---|
| 1254 | for (i = 0; i < parsed->u.array.length; i++) { |
---|
| 1255 | id = json_o_get(parsed->u.array.values[i], "id"); |
---|
| 1256 | name = json_o_str(parsed->u.array.values[i], "screen_name"); |
---|
| 1257 | |
---|
[5ebff60] | 1258 | if (!name || !id || id->type != json_integer) { |
---|
[73ee390] | 1259 | continue; |
---|
[5ebff60] | 1260 | } |
---|
[73ee390] | 1261 | |
---|
| 1262 | for (u = users; u; u = g_list_next(u)) { |
---|
| 1263 | tf = u->data; |
---|
| 1264 | |
---|
| 1265 | if (g_strcasecmp(tf->text, name) == 0) { |
---|
| 1266 | tf->uid = id->u.integer; |
---|
| 1267 | users = g_list_delete_link(users, u); |
---|
| 1268 | break; |
---|
| 1269 | } |
---|
| 1270 | } |
---|
| 1271 | } |
---|
| 1272 | |
---|
| 1273 | finish: |
---|
| 1274 | json_value_free(parsed); |
---|
| 1275 | twitter_filter_stream(ic); |
---|
| 1276 | |
---|
[5ebff60] | 1277 | if (!users) { |
---|
[73ee390] | 1278 | return; |
---|
[5ebff60] | 1279 | } |
---|
[73ee390] | 1280 | |
---|
| 1281 | fstr = g_string_new(""); |
---|
| 1282 | |
---|
| 1283 | for (u = users; u; u = g_list_next(u)) { |
---|
[5ebff60] | 1284 | if (fstr->len > 0) { |
---|
[73ee390] | 1285 | g_string_append(fstr, ", "); |
---|
[5ebff60] | 1286 | } |
---|
[73ee390] | 1287 | |
---|
| 1288 | g_string_append(fstr, tf->text); |
---|
| 1289 | } |
---|
| 1290 | |
---|
| 1291 | imcb_error(ic, "Failed UID acquisitions: %s", fstr->str); |
---|
| 1292 | |
---|
| 1293 | g_string_free(fstr, TRUE); |
---|
| 1294 | g_list_free(users); |
---|
| 1295 | } |
---|
| 1296 | |
---|
| 1297 | gboolean twitter_open_filter_stream(struct im_connection *ic) |
---|
| 1298 | { |
---|
| 1299 | struct twitter_data *td = ic->proto_data; |
---|
[5ebff60] | 1300 | char *args[2] = { "screen_name", NULL }; |
---|
[73ee390] | 1301 | GString *ustr = g_string_new(""); |
---|
| 1302 | struct twitter_filter *tf; |
---|
| 1303 | struct http_request *req; |
---|
| 1304 | GSList *l; |
---|
| 1305 | |
---|
| 1306 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
| 1307 | tf = l->data; |
---|
| 1308 | |
---|
[5ebff60] | 1309 | if (tf->type != TWITTER_FILTER_TYPE_FOLLOW || tf->uid != 0) { |
---|
[73ee390] | 1310 | continue; |
---|
[5ebff60] | 1311 | } |
---|
[73ee390] | 1312 | |
---|
[5ebff60] | 1313 | if (ustr->len > 0) { |
---|
[73ee390] | 1314 | g_string_append_c(ustr, ','); |
---|
[5ebff60] | 1315 | } |
---|
[73ee390] | 1316 | |
---|
| 1317 | g_string_append(ustr, tf->text); |
---|
| 1318 | } |
---|
| 1319 | |
---|
| 1320 | if (ustr->len == 0) { |
---|
| 1321 | g_string_free(ustr, TRUE); |
---|
| 1322 | return twitter_filter_stream(ic); |
---|
| 1323 | } |
---|
| 1324 | |
---|
| 1325 | args[1] = ustr->str; |
---|
| 1326 | req = twitter_http(ic, TWITTER_USERS_LOOKUP_URL, |
---|
[5ebff60] | 1327 | twitter_filter_users_post, |
---|
| 1328 | ic, 0, args, 2); |
---|
[73ee390] | 1329 | |
---|
| 1330 | g_string_free(ustr, TRUE); |
---|
| 1331 | return req != NULL; |
---|
| 1332 | } |
---|
| 1333 | |
---|
[dff0e0b] | 1334 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor); |
---|
| 1335 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor); |
---|
| 1336 | |
---|
[2322a9f] | 1337 | /** |
---|
| 1338 | * Get the timeline with optionally mentions |
---|
| 1339 | */ |
---|
[2f9027c] | 1340 | gboolean twitter_get_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
[2322a9f] | 1341 | { |
---|
| 1342 | struct twitter_data *td = ic->proto_data; |
---|
| 1343 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
| 1344 | |
---|
| 1345 | if (td->flags & TWITTER_DOING_TIMELINE) { |
---|
[11ec078] | 1346 | if (++td->http_fails >= 5) { |
---|
| 1347 | imcb_error(ic, "Fetch timeout (%d)", td->flags); |
---|
| 1348 | imc_logout(ic, TRUE); |
---|
[2f9027c] | 1349 | return FALSE; |
---|
[11ec078] | 1350 | } |
---|
[2322a9f] | 1351 | } |
---|
| 1352 | |
---|
| 1353 | td->flags |= TWITTER_DOING_TIMELINE; |
---|
| 1354 | |
---|
| 1355 | twitter_get_home_timeline(ic, next_cursor); |
---|
| 1356 | |
---|
| 1357 | if (include_mentions) { |
---|
| 1358 | twitter_get_mentions(ic, next_cursor); |
---|
| 1359 | } |
---|
[5ebff60] | 1360 | |
---|
[2f9027c] | 1361 | return TRUE; |
---|
[2322a9f] | 1362 | } |
---|
| 1363 | |
---|
| 1364 | /** |
---|
| 1365 | * Call this one after receiving timeline/mentions. Show to user once we have |
---|
| 1366 | * both. |
---|
| 1367 | */ |
---|
| 1368 | void twitter_flush_timeline(struct im_connection *ic) |
---|
| 1369 | { |
---|
| 1370 | struct twitter_data *td = ic->proto_data; |
---|
| 1371 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
[b5fe39b] | 1372 | int show_old_mentions = set_getint(&ic->acc->set, "show_old_mentions"); |
---|
[2322a9f] | 1373 | struct twitter_xml_list *home_timeline = td->home_timeline_obj; |
---|
| 1374 | struct twitter_xml_list *mentions = td->mentions_obj; |
---|
[29f72b7] | 1375 | guint64 last_id = 0; |
---|
[2322a9f] | 1376 | GSList *output = NULL; |
---|
| 1377 | GSList *l; |
---|
| 1378 | |
---|
[29f72b7] | 1379 | imcb_connected(ic); |
---|
[5ebff60] | 1380 | |
---|
[2322a9f] | 1381 | if (!(td->flags & TWITTER_GOT_TIMELINE)) { |
---|
| 1382 | return; |
---|
| 1383 | } |
---|
| 1384 | |
---|
| 1385 | if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) { |
---|
| 1386 | return; |
---|
| 1387 | } |
---|
| 1388 | |
---|
| 1389 | if (home_timeline && home_timeline->list) { |
---|
| 1390 | for (l = home_timeline->list; l; l = g_slist_next(l)) { |
---|
| 1391 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
| 1392 | } |
---|
| 1393 | } |
---|
| 1394 | |
---|
| 1395 | if (include_mentions && mentions && mentions->list) { |
---|
| 1396 | for (l = mentions->list; l; l = g_slist_next(l)) { |
---|
[b5fe39b] | 1397 | if (show_old_mentions < 1 && output && twitter_compare_elements(l->data, output->data) < 0) { |
---|
[2322a9f] | 1398 | continue; |
---|
| 1399 | } |
---|
| 1400 | |
---|
| 1401 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
| 1402 | } |
---|
| 1403 | } |
---|
| 1404 | |
---|
| 1405 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
[29f72b7] | 1406 | while (output) { |
---|
| 1407 | struct twitter_xml_status *txs = output->data; |
---|
[5ebff60] | 1408 | if (txs->id != last_id) { |
---|
[29f72b7] | 1409 | twitter_status_show(ic, txs); |
---|
[5ebff60] | 1410 | } |
---|
[29f72b7] | 1411 | last_id = txs->id; |
---|
| 1412 | output = g_slist_remove(output, txs); |
---|
| 1413 | } |
---|
[2322a9f] | 1414 | |
---|
[199fea6] | 1415 | txl_free(home_timeline); |
---|
| 1416 | txl_free(mentions); |
---|
[2322a9f] | 1417 | |
---|
| 1418 | td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS); |
---|
[199fea6] | 1419 | td->home_timeline_obj = td->mentions_obj = NULL; |
---|
[2322a9f] | 1420 | } |
---|
| 1421 | |
---|
[ddc2de5] | 1422 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
| 1423 | static void twitter_http_get_mentions(struct http_request *req); |
---|
| 1424 | |
---|
[2322a9f] | 1425 | /** |
---|
| 1426 | * Get the timeline. |
---|
| 1427 | */ |
---|
[ddc2de5] | 1428 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
[2322a9f] | 1429 | { |
---|
| 1430 | struct twitter_data *td = ic->proto_data; |
---|
| 1431 | |
---|
[199fea6] | 1432 | txl_free(td->home_timeline_obj); |
---|
[2322a9f] | 1433 | td->home_timeline_obj = NULL; |
---|
| 1434 | td->flags &= ~TWITTER_GOT_TIMELINE; |
---|
| 1435 | |
---|
[429a9b1] | 1436 | char *args[6]; |
---|
[2322a9f] | 1437 | args[0] = "cursor"; |
---|
[85c3004] | 1438 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
[429a9b1] | 1439 | args[2] = "include_entities"; |
---|
| 1440 | args[3] = "true"; |
---|
[2322a9f] | 1441 | if (td->timeline_id) { |
---|
[429a9b1] | 1442 | args[4] = "since_id"; |
---|
[7549d00] | 1443 | args[5] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
[2322a9f] | 1444 | } |
---|
| 1445 | |
---|
[90fc864] | 1446 | if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, |
---|
[5ebff60] | 1447 | td->timeline_id ? 6 : 4) == NULL) { |
---|
| 1448 | if (++td->http_fails >= 5) { |
---|
[90fc864] | 1449 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 1450 | TWITTER_HOME_TIMELINE_URL, "connection failed"); |
---|
[5ebff60] | 1451 | } |
---|
[90fc864] | 1452 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
| 1453 | twitter_flush_timeline(ic); |
---|
| 1454 | } |
---|
[2322a9f] | 1455 | |
---|
| 1456 | g_free(args[1]); |
---|
| 1457 | if (td->timeline_id) { |
---|
[429a9b1] | 1458 | g_free(args[5]); |
---|
[2322a9f] | 1459 | } |
---|
| 1460 | } |
---|
| 1461 | |
---|
| 1462 | /** |
---|
| 1463 | * Get mentions. |
---|
| 1464 | */ |
---|
[ddc2de5] | 1465 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor) |
---|
[2322a9f] | 1466 | { |
---|
| 1467 | struct twitter_data *td = ic->proto_data; |
---|
| 1468 | |
---|
[199fea6] | 1469 | txl_free(td->mentions_obj); |
---|
[2322a9f] | 1470 | td->mentions_obj = NULL; |
---|
| 1471 | td->flags &= ~TWITTER_GOT_MENTIONS; |
---|
| 1472 | |
---|
[429a9b1] | 1473 | char *args[6]; |
---|
[2322a9f] | 1474 | args[0] = "cursor"; |
---|
[85c3004] | 1475 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
[429a9b1] | 1476 | args[2] = "include_entities"; |
---|
| 1477 | args[3] = "true"; |
---|
[2322a9f] | 1478 | if (td->timeline_id) { |
---|
[429a9b1] | 1479 | args[4] = "since_id"; |
---|
[85c3004] | 1480 | args[5] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
[b5fe39b] | 1481 | } else { |
---|
| 1482 | args[4] = "count"; |
---|
| 1483 | args[5] = g_strdup_printf("%d", set_getint(&ic->acc->set, "show_old_mentions")); |
---|
[2322a9f] | 1484 | } |
---|
| 1485 | |
---|
[b5fe39b] | 1486 | if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions, |
---|
| 1487 | ic, 0, args, 6) == NULL) { |
---|
[5ebff60] | 1488 | if (++td->http_fails >= 5) { |
---|
[90fc864] | 1489 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 1490 | TWITTER_MENTIONS_URL, "connection failed"); |
---|
[5ebff60] | 1491 | } |
---|
[90fc864] | 1492 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
| 1493 | twitter_flush_timeline(ic); |
---|
| 1494 | } |
---|
[2322a9f] | 1495 | |
---|
| 1496 | g_free(args[1]); |
---|
[2fb1262] | 1497 | g_free(args[5]); |
---|
[2322a9f] | 1498 | } |
---|
| 1499 | |
---|
[1b221e0] | 1500 | /** |
---|
| 1501 | * Callback for getting the home timeline. |
---|
| 1502 | */ |
---|
| 1503 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
| 1504 | { |
---|
[62d2cfb] | 1505 | struct im_connection *ic = req->data; |
---|
[37aa317] | 1506 | struct twitter_data *td; |
---|
[8e3b7ac] | 1507 | json_value *parsed; |
---|
[1b221e0] | 1508 | struct twitter_xml_list *txl; |
---|
[62d2cfb] | 1509 | |
---|
| 1510 | // Check if the connection is still active. |
---|
[5ebff60] | 1511 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[62d2cfb] | 1512 | return; |
---|
[5ebff60] | 1513 | } |
---|
[5983eca] | 1514 | |
---|
[37aa317] | 1515 | td = ic->proto_data; |
---|
[1b221e0] | 1516 | |
---|
[2322a9f] | 1517 | // The root <statuses> node should hold the list of statuses <status> |
---|
[5ebff60] | 1518 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[2a6da96] | 1519 | goto end; |
---|
[5ebff60] | 1520 | } |
---|
[bbff22d] | 1521 | |
---|
| 1522 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 1523 | txl->list = NULL; |
---|
| 1524 | |
---|
[d0752e8] | 1525 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
[2fb1262] | 1526 | json_value_free(parsed); |
---|
[2322a9f] | 1527 | |
---|
| 1528 | td->home_timeline_obj = txl; |
---|
| 1529 | |
---|
[5ebff60] | 1530 | end: |
---|
| 1531 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[fb351ce] | 1532 | return; |
---|
[5ebff60] | 1533 | } |
---|
[fb351ce] | 1534 | |
---|
[2322a9f] | 1535 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
| 1536 | |
---|
| 1537 | twitter_flush_timeline(ic); |
---|
| 1538 | } |
---|
| 1539 | |
---|
| 1540 | /** |
---|
| 1541 | * Callback for getting mentions. |
---|
| 1542 | */ |
---|
| 1543 | static void twitter_http_get_mentions(struct http_request *req) |
---|
| 1544 | { |
---|
| 1545 | struct im_connection *ic = req->data; |
---|
| 1546 | struct twitter_data *td; |
---|
[8e3b7ac] | 1547 | json_value *parsed; |
---|
[2322a9f] | 1548 | struct twitter_xml_list *txl; |
---|
| 1549 | |
---|
| 1550 | // Check if the connection is still active. |
---|
[5ebff60] | 1551 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[1b221e0] | 1552 | return; |
---|
[5ebff60] | 1553 | } |
---|
[2322a9f] | 1554 | |
---|
| 1555 | td = ic->proto_data; |
---|
| 1556 | |
---|
[1b221e0] | 1557 | // The root <statuses> node should hold the list of statuses <status> |
---|
[5ebff60] | 1558 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[2a6da96] | 1559 | goto end; |
---|
[5ebff60] | 1560 | } |
---|
[bbff22d] | 1561 | |
---|
| 1562 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 1563 | txl->list = NULL; |
---|
| 1564 | |
---|
[d0752e8] | 1565 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
[2fb1262] | 1566 | json_value_free(parsed); |
---|
[1b221e0] | 1567 | |
---|
[2322a9f] | 1568 | td->mentions_obj = txl; |
---|
[1b221e0] | 1569 | |
---|
[5ebff60] | 1570 | end: |
---|
| 1571 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[fb351ce] | 1572 | return; |
---|
[5ebff60] | 1573 | } |
---|
[fb351ce] | 1574 | |
---|
[2322a9f] | 1575 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
| 1576 | |
---|
| 1577 | twitter_flush_timeline(ic); |
---|
[1b221e0] | 1578 | } |
---|
| 1579 | |
---|
| 1580 | /** |
---|
[de923d5] | 1581 | * Callback to use after sending a POST request to twitter. |
---|
| 1582 | * (Generic, used for a few kinds of queries.) |
---|
[1b221e0] | 1583 | */ |
---|
[7d53efb] | 1584 | static void twitter_http_post(struct http_request *req) |
---|
[1b221e0] | 1585 | { |
---|
| 1586 | struct im_connection *ic = req->data; |
---|
[7b87539] | 1587 | struct twitter_data *td; |
---|
[24132ec] | 1588 | json_value *parsed, *id; |
---|
[1b221e0] | 1589 | |
---|
[62d2cfb] | 1590 | // Check if the connection is still active. |
---|
[5ebff60] | 1591 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[62d2cfb] | 1592 | return; |
---|
[5ebff60] | 1593 | } |
---|
[62d2cfb] | 1594 | |
---|
[7b87539] | 1595 | td = ic->proto_data; |
---|
| 1596 | td->last_status_id = 0; |
---|
[5983eca] | 1597 | |
---|
[5ebff60] | 1598 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[1b221e0] | 1599 | return; |
---|
[5ebff60] | 1600 | } |
---|
| 1601 | |
---|
[67f6828] | 1602 | if ((id = json_o_get(parsed, "id")) && id->type == json_integer) { |
---|
[24132ec] | 1603 | td->last_status_id = id->u.integer; |
---|
[67f6828] | 1604 | } |
---|
[5ebff60] | 1605 | |
---|
[c751e51] | 1606 | json_value_free(parsed); |
---|
[5ebff60] | 1607 | |
---|
| 1608 | if (req->flags & TWITTER_HTTP_USER_ACK) { |
---|
[b235228] | 1609 | twitter_log(ic, "Command processed successfully"); |
---|
[5ebff60] | 1610 | } |
---|
[1b221e0] | 1611 | } |
---|
| 1612 | |
---|
| 1613 | /** |
---|
| 1614 | * Function to POST a new status to twitter. |
---|
[5983eca] | 1615 | */ |
---|
[b890626] | 1616 | void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to) |
---|
[1b221e0] | 1617 | { |
---|
[5983eca] | 1618 | char *args[4] = { |
---|
[b890626] | 1619 | "status", msg, |
---|
| 1620 | "in_reply_to_status_id", |
---|
[85c3004] | 1621 | g_strdup_printf("%" G_GUINT64_FORMAT, in_reply_to) |
---|
[b890626] | 1622 | }; |
---|
[5ebff60] | 1623 | |
---|
[b890626] | 1624 | twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, |
---|
[5ebff60] | 1625 | args, in_reply_to ? 4 : 2); |
---|
[b890626] | 1626 | g_free(args[3]); |
---|
[1b221e0] | 1627 | } |
---|
| 1628 | |
---|
| 1629 | |
---|
[62d2cfb] | 1630 | /** |
---|
| 1631 | * Function to POST a new message to twitter. |
---|
| 1632 | */ |
---|
| 1633 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
| 1634 | { |
---|
[5983eca] | 1635 | char *args[4]; |
---|
[5ebff60] | 1636 | |
---|
[62d2cfb] | 1637 | args[0] = "screen_name"; |
---|
| 1638 | args[1] = who; |
---|
| 1639 | args[2] = "text"; |
---|
| 1640 | args[3] = msg; |
---|
| 1641 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
[ba3233c] | 1642 | twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4); |
---|
[62d2cfb] | 1643 | } |
---|
[7d53efb] | 1644 | |
---|
| 1645 | void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) |
---|
| 1646 | { |
---|
[5983eca] | 1647 | char *args[2]; |
---|
[5ebff60] | 1648 | |
---|
[7d53efb] | 1649 | args[0] = "screen_name"; |
---|
| 1650 | args[1] = who; |
---|
[5983eca] | 1651 | twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, |
---|
[5ebff60] | 1652 | twitter_http_post, ic, 1, args, 2); |
---|
[a26af5c] | 1653 | } |
---|
[7b87539] | 1654 | |
---|
[9cf63ac] | 1655 | /** |
---|
| 1656 | * Mute or unmute a user |
---|
| 1657 | */ |
---|
| 1658 | void twitter_mute_create_destroy(struct im_connection *ic, char *who, int create) |
---|
| 1659 | { |
---|
| 1660 | char *args[2]; |
---|
| 1661 | |
---|
| 1662 | args[0] = "screen_name"; |
---|
| 1663 | args[1] = who; |
---|
| 1664 | twitter_http(ic, create ? TWITTER_MUTES_CREATE_URL : TWITTER_MUTES_DESTROY_URL, |
---|
| 1665 | twitter_http_post, ic, 1, args, 2); |
---|
| 1666 | } |
---|
| 1667 | |
---|
[7b87539] | 1668 | void twitter_status_destroy(struct im_connection *ic, guint64 id) |
---|
| 1669 | { |
---|
| 1670 | char *url; |
---|
[5ebff60] | 1671 | |
---|
[85c3004] | 1672 | url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", |
---|
| 1673 | TWITTER_STATUS_DESTROY_URL, id, ".json"); |
---|
[b235228] | 1674 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
| 1675 | TWITTER_HTTP_USER_ACK); |
---|
[7b87539] | 1676 | g_free(url); |
---|
| 1677 | } |
---|
[b890626] | 1678 | |
---|
| 1679 | void twitter_status_retweet(struct im_connection *ic, guint64 id) |
---|
| 1680 | { |
---|
| 1681 | char *url; |
---|
[5ebff60] | 1682 | |
---|
[85c3004] | 1683 | url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", |
---|
| 1684 | TWITTER_STATUS_RETWEET_URL, id, ".json"); |
---|
[b235228] | 1685 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
| 1686 | TWITTER_HTTP_USER_ACK); |
---|
[b890626] | 1687 | g_free(url); |
---|
| 1688 | } |
---|
[d18dee42] | 1689 | |
---|
| 1690 | /** |
---|
| 1691 | * Report a user for sending spam. |
---|
| 1692 | */ |
---|
| 1693 | void twitter_report_spam(struct im_connection *ic, char *screen_name) |
---|
| 1694 | { |
---|
| 1695 | char *args[2] = { |
---|
| 1696 | "screen_name", |
---|
| 1697 | NULL, |
---|
| 1698 | }; |
---|
[5ebff60] | 1699 | |
---|
[d18dee42] | 1700 | args[1] = screen_name; |
---|
[b235228] | 1701 | twitter_http_f(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post, |
---|
| 1702 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
[d18dee42] | 1703 | } |
---|
[b61c74c] | 1704 | |
---|
| 1705 | /** |
---|
| 1706 | * Favourite a tweet. |
---|
| 1707 | */ |
---|
| 1708 | void twitter_favourite_tweet(struct im_connection *ic, guint64 id) |
---|
| 1709 | { |
---|
[75bda8b] | 1710 | char *args[2] = { |
---|
| 1711 | "id", |
---|
| 1712 | NULL, |
---|
| 1713 | }; |
---|
[5ebff60] | 1714 | |
---|
[85c3004] | 1715 | args[1] = g_strdup_printf("%" G_GUINT64_FORMAT, id); |
---|
[75bda8b] | 1716 | twitter_http_f(ic, TWITTER_FAVORITE_CREATE_URL, twitter_http_post, |
---|
| 1717 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
| 1718 | g_free(args[1]); |
---|
[b61c74c] | 1719 | } |
---|
[1201fcb] | 1720 | |
---|
| 1721 | static void twitter_http_status_show_url(struct http_request *req) |
---|
| 1722 | { |
---|
| 1723 | struct im_connection *ic = req->data; |
---|
| 1724 | json_value *parsed, *id; |
---|
| 1725 | const char *name; |
---|
| 1726 | |
---|
| 1727 | // Check if the connection is still active. |
---|
| 1728 | if (!g_slist_find(twitter_connections, ic)) { |
---|
| 1729 | return; |
---|
| 1730 | } |
---|
| 1731 | |
---|
| 1732 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
| 1733 | return; |
---|
| 1734 | } |
---|
| 1735 | |
---|
| 1736 | /* for the parson branch: |
---|
| 1737 | name = json_object_dotget_string(json_object(parsed), "user.screen_name"); |
---|
| 1738 | id = json_object_get_integer(json_object(parsed), "id"); |
---|
| 1739 | */ |
---|
| 1740 | |
---|
| 1741 | name = json_o_str(json_o_get(parsed, "user"), "screen_name"); |
---|
| 1742 | id = json_o_get(parsed, "id"); |
---|
| 1743 | |
---|
| 1744 | if (name && id && id->type == json_integer) { |
---|
| 1745 | twitter_log(ic, "https://twitter.com/%s/status/%" G_GUINT64_FORMAT, name, id->u.integer); |
---|
| 1746 | } else { |
---|
| 1747 | twitter_log(ic, "Error: could not fetch tweet url."); |
---|
| 1748 | } |
---|
| 1749 | |
---|
| 1750 | json_value_free(parsed); |
---|
| 1751 | } |
---|
| 1752 | |
---|
| 1753 | void twitter_status_show_url(struct im_connection *ic, guint64 id) |
---|
| 1754 | { |
---|
| 1755 | char *url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", TWITTER_STATUS_SHOW_URL, id, ".json"); |
---|
| 1756 | twitter_http(ic, url, twitter_http_status_show_url, ic, 0, NULL, 0); |
---|
| 1757 | g_free(url); |
---|
| 1758 | } |
---|