[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 | |
---|
[dc45a85] | 386 | if (req->status_code != 200) { |
---|
| 387 | /* Fail silently */ |
---|
| 388 | return; |
---|
| 389 | } |
---|
| 390 | |
---|
[fb62f132] | 391 | // Parse the data. |
---|
| 392 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
| 393 | return; |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 397 | txl->list = td->mutes_ids; |
---|
| 398 | |
---|
| 399 | /* mute ids API response is similar enough to friends response |
---|
| 400 | to reuse this method */ |
---|
| 401 | twitter_xt_get_friends_id_list(parsed, txl); |
---|
| 402 | json_value_free(parsed); |
---|
| 403 | |
---|
| 404 | td->mutes_ids = txl->list; |
---|
| 405 | if (txl->next_cursor) { |
---|
| 406 | /* Recurse while there are still more pages */ |
---|
| 407 | twitter_get_mutes_ids(ic, txl->next_cursor); |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | txl->list = NULL; |
---|
| 411 | txl_free(txl); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | /** |
---|
| 415 | * Callback for getting the no-retweets ids. |
---|
| 416 | */ |
---|
| 417 | static void twitter_http_get_noretweets_ids(struct http_request *req) |
---|
| 418 | { |
---|
| 419 | struct im_connection *ic = req->data; |
---|
| 420 | json_value *parsed; |
---|
| 421 | struct twitter_xml_list *txl; |
---|
| 422 | struct twitter_data *td; |
---|
| 423 | |
---|
| 424 | // Check if the connection is stil active |
---|
| 425 | if (!g_slist_find(twitter_connections, ic)) { |
---|
| 426 | return; |
---|
| 427 | } |
---|
| 428 | |
---|
[dc45a85] | 429 | if (req->status_code != 200) { |
---|
| 430 | /* Fail silently */ |
---|
| 431 | return; |
---|
| 432 | } |
---|
| 433 | |
---|
[fb62f132] | 434 | td = ic->proto_data; |
---|
| 435 | |
---|
| 436 | // Parse the data. |
---|
| 437 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
| 438 | return; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 442 | txl->list = td->noretweets_ids; |
---|
| 443 | |
---|
| 444 | // Process the retweet ids |
---|
| 445 | txl->type = TXL_ID; |
---|
| 446 | if (parsed->type == json_array) { |
---|
| 447 | unsigned int i; |
---|
| 448 | for (i = 0; i < parsed->u.array.length; i++) { |
---|
| 449 | json_value *c = parsed->u.array.values[i]; |
---|
| 450 | if (c->type != json_integer) { |
---|
| 451 | continue; |
---|
| 452 | } |
---|
| 453 | txl->list = g_slist_prepend(txl->list, |
---|
| 454 | g_strdup_printf("%"PRIu64, c->u.integer)); |
---|
| 455 | } |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | json_value_free(parsed); |
---|
| 459 | td->noretweets_ids = txl->list; |
---|
| 460 | |
---|
| 461 | txl->list = NULL; |
---|
| 462 | txl_free(txl); |
---|
| 463 | } |
---|
| 464 | |
---|
[9e8c945] | 465 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl); |
---|
[de923d5] | 466 | static void twitter_http_get_users_lookup(struct http_request *req); |
---|
| 467 | |
---|
| 468 | static void twitter_get_users_lookup(struct im_connection *ic) |
---|
| 469 | { |
---|
| 470 | struct twitter_data *td = ic->proto_data; |
---|
| 471 | char *args[2] = { |
---|
| 472 | "user_id", |
---|
| 473 | NULL, |
---|
| 474 | }; |
---|
| 475 | GString *ids = g_string_new(""); |
---|
| 476 | int i; |
---|
[5ebff60] | 477 | |
---|
[de923d5] | 478 | /* We can request up to 100 users at a time. */ |
---|
[5ebff60] | 479 | for (i = 0; i < 100 && td->follow_ids; i++) { |
---|
| 480 | g_string_append_printf(ids, ",%s", (char *) td->follow_ids->data); |
---|
[de923d5] | 481 | g_free(td->follow_ids->data); |
---|
| 482 | td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); |
---|
| 483 | } |
---|
| 484 | if (ids->len > 0) { |
---|
| 485 | args[1] = ids->str + 1; |
---|
| 486 | /* POST, because I think ids can be up to 1KB long. */ |
---|
| 487 | twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); |
---|
| 488 | } else { |
---|
| 489 | /* We have all users. Continue with login. (Get statuses.) */ |
---|
| 490 | td->flags |= TWITTER_HAVE_FRIENDS; |
---|
| 491 | twitter_login_finish(ic); |
---|
| 492 | } |
---|
| 493 | g_string_free(ids, TRUE); |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | /** |
---|
| 497 | * Callback for getting (twitter)friends... |
---|
| 498 | * |
---|
[5ebff60] | 499 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
| 500 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
[de923d5] | 501 | * BitlBee... Get a life and meet new people! |
---|
| 502 | */ |
---|
| 503 | static void twitter_http_get_users_lookup(struct http_request *req) |
---|
| 504 | { |
---|
| 505 | struct im_connection *ic = req->data; |
---|
[e08ae0c] | 506 | json_value *parsed; |
---|
[de923d5] | 507 | struct twitter_xml_list *txl; |
---|
| 508 | GSList *l = NULL; |
---|
| 509 | struct twitter_xml_user *user; |
---|
| 510 | |
---|
| 511 | // Check if the connection is still active. |
---|
[5ebff60] | 512 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[de923d5] | 513 | return; |
---|
[5ebff60] | 514 | } |
---|
[de923d5] | 515 | |
---|
| 516 | // Get the user list from the parsed xml feed. |
---|
[5ebff60] | 517 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[2a6da96] | 518 | return; |
---|
[5ebff60] | 519 | } |
---|
[bbff22d] | 520 | |
---|
| 521 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 522 | txl->list = NULL; |
---|
| 523 | |
---|
[d0752e8] | 524 | twitter_xt_get_users(parsed, txl); |
---|
[e08ae0c] | 525 | json_value_free(parsed); |
---|
[de923d5] | 526 | |
---|
| 527 | // Add the users as buddies. |
---|
| 528 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 529 | user = l->data; |
---|
| 530 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | // Free the structure. |
---|
[62d2cfb] | 534 | txl_free(txl); |
---|
[de923d5] | 535 | |
---|
| 536 | twitter_get_users_lookup(ic); |
---|
[1b221e0] | 537 | } |
---|
| 538 | |
---|
[8e3b7ac] | 539 | struct twitter_xml_user *twitter_xt_get_user(const json_value *node) |
---|
| 540 | { |
---|
| 541 | struct twitter_xml_user *txu; |
---|
[73ee390] | 542 | json_value *jv; |
---|
[5ebff60] | 543 | |
---|
[8e3b7ac] | 544 | txu = g_new0(struct twitter_xml_user, 1); |
---|
| 545 | txu->name = g_strdup(json_o_str(node, "name")); |
---|
| 546 | txu->screen_name = g_strdup(json_o_str(node, "screen_name")); |
---|
[5ebff60] | 547 | |
---|
[73ee390] | 548 | jv = json_o_get(node, "id"); |
---|
| 549 | txu->uid = jv->u.integer; |
---|
[5ebff60] | 550 | |
---|
[8e3b7ac] | 551 | return txu; |
---|
| 552 | } |
---|
| 553 | |
---|
[62d2cfb] | 554 | /** |
---|
| 555 | * Function to fill a twitter_xml_list struct. |
---|
| 556 | * It sets: |
---|
| 557 | * - all <user>s from the <users> element. |
---|
| 558 | */ |
---|
[9e8c945] | 559 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl) |
---|
[62d2cfb] | 560 | { |
---|
| 561 | struct twitter_xml_user *txu; |
---|
[e08ae0c] | 562 | int i; |
---|
[62d2cfb] | 563 | |
---|
| 564 | // Set the type of the list. |
---|
| 565 | txl->type = TXL_USER; |
---|
| 566 | |
---|
[5ebff60] | 567 | if (!node || node->type != json_array) { |
---|
[9e8c945] | 568 | return FALSE; |
---|
[5ebff60] | 569 | } |
---|
[e08ae0c] | 570 | |
---|
[62d2cfb] | 571 | // The root <users> node should hold the list of users <user> |
---|
| 572 | // Walk over the nodes children. |
---|
[5ebff60] | 573 | for (i = 0; i < node->u.array.length; i++) { |
---|
[8e3b7ac] | 574 | txu = twitter_xt_get_user(node->u.array.values[i]); |
---|
[5ebff60] | 575 | if (txu) { |
---|
[8e3b7ac] | 576 | txl->list = g_slist_prepend(txl->list, txu); |
---|
[5ebff60] | 577 | } |
---|
[62d2cfb] | 578 | } |
---|
| 579 | |
---|
[9e8c945] | 580 | return TRUE; |
---|
[62d2cfb] | 581 | } |
---|
| 582 | |
---|
[2b02617] | 583 | #ifdef __GLIBC__ |
---|
| 584 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y" |
---|
| 585 | #else |
---|
| 586 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y" |
---|
| 587 | #endif |
---|
[62d2cfb] | 588 | |
---|
[cd60710] | 589 | static void expand_entities(char **text, const json_value *node); |
---|
[d1356cb] | 590 | |
---|
[1b221e0] | 591 | /** |
---|
| 592 | * Function to fill a twitter_xml_status struct. |
---|
| 593 | * It sets: |
---|
| 594 | * - the status text and |
---|
| 595 | * - the created_at timestamp and |
---|
| 596 | * - the status id and |
---|
| 597 | * - the user in a twitter_xml_user struct. |
---|
| 598 | */ |
---|
[c9b5817] | 599 | static struct twitter_xml_status *twitter_xt_get_status(const json_value *node) |
---|
[1b221e0] | 600 | { |
---|
[15c4cd4] | 601 | struct twitter_xml_status *txs = {0}; |
---|
[cd60710] | 602 | const json_value *rt = NULL; |
---|
[15c4cd4] | 603 | const json_value *text_value = NULL; |
---|
[5ebff60] | 604 | |
---|
| 605 | if (node->type != json_object) { |
---|
[9e8c945] | 606 | return FALSE; |
---|
[5ebff60] | 607 | } |
---|
[c9b5817] | 608 | txs = g_new0(struct twitter_xml_status, 1); |
---|
[1b221e0] | 609 | |
---|
[5ebff60] | 610 | JSON_O_FOREACH(node, k, v) { |
---|
[15c4cd4] | 611 | if (strcmp("text", k) == 0 && v->type == json_string && text_value == NULL) { |
---|
| 612 | text_value = v; |
---|
| 613 | } else if (strcmp("full_text", k) == 0 && v->type == json_string) { |
---|
| 614 | text_value = v; |
---|
| 615 | } else if (strcmp("extended_tweet", k) == 0 && v->type == json_object) { |
---|
| 616 | text_value = json_o_get(v, "full_text"); |
---|
[8e3b7ac] | 617 | } else if (strcmp("retweeted_status", k) == 0 && v->type == json_object) { |
---|
| 618 | rt = v; |
---|
| 619 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
[08579a1] | 620 | struct tm parsed; |
---|
[5983eca] | 621 | |
---|
[08579a1] | 622 | /* Very sensitive to changes to the formatting of |
---|
| 623 | this field. :-( Also assumes the timezone used |
---|
| 624 | is UTC since C time handling functions suck. */ |
---|
[5ebff60] | 625 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) { |
---|
[5983eca] | 626 | txs->created_at = mktime_utc(&parsed); |
---|
[5ebff60] | 627 | } |
---|
[8e3b7ac] | 628 | } else if (strcmp("user", k) == 0 && v->type == json_object) { |
---|
| 629 | txs->user = twitter_xt_get_user(v); |
---|
| 630 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
[573e274] | 631 | txs->rt_id = txs->id = v->u.integer; |
---|
[8e3b7ac] | 632 | } else if (strcmp("in_reply_to_status_id", k) == 0 && v->type == json_integer) { |
---|
| 633 | txs->reply_to = v->u.integer; |
---|
[ce81acd] | 634 | } |
---|
[1b221e0] | 635 | } |
---|
[5983eca] | 636 | |
---|
[0fff0b2] | 637 | /* If it's a (truncated) retweet, get the original. Even if the API claims it |
---|
| 638 | wasn't truncated because it may be lying. */ |
---|
| 639 | if (rt) { |
---|
[c9b5817] | 640 | struct twitter_xml_status *rtxs = twitter_xt_get_status(rt); |
---|
| 641 | if (rtxs) { |
---|
[0ca1d79] | 642 | txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text); |
---|
[c9b5817] | 643 | txs->id = rtxs->id; |
---|
[0ca1d79] | 644 | txs_free(rtxs); |
---|
[e193aeb] | 645 | } |
---|
[15c4cd4] | 646 | } else if (text_value && text_value->type == json_string) { |
---|
| 647 | txs->text = g_memdup(text_value->u.string.ptr, text_value->u.string.length + 1); |
---|
| 648 | strip_html(txs->text); |
---|
[cd60710] | 649 | expand_entities(&txs->text, node); |
---|
[d1356cb] | 650 | } |
---|
| 651 | |
---|
[5ebff60] | 652 | if (txs->text && txs->user && txs->id) { |
---|
[c9b5817] | 653 | return txs; |
---|
[5ebff60] | 654 | } |
---|
| 655 | |
---|
[c9b5817] | 656 | txs_free(txs); |
---|
| 657 | return NULL; |
---|
[d1356cb] | 658 | } |
---|
| 659 | |
---|
| 660 | /** |
---|
| 661 | * Function to fill a twitter_xml_status struct (DM variant). |
---|
| 662 | */ |
---|
[2cd8540] | 663 | static struct twitter_xml_status *twitter_xt_get_dm(const json_value *node) |
---|
[d1356cb] | 664 | { |
---|
[2cd8540] | 665 | struct twitter_xml_status *txs; |
---|
[5ebff60] | 666 | |
---|
| 667 | if (node->type != json_object) { |
---|
[d1356cb] | 668 | return FALSE; |
---|
[5ebff60] | 669 | } |
---|
[2cd8540] | 670 | txs = g_new0(struct twitter_xml_status, 1); |
---|
[d1356cb] | 671 | |
---|
[5ebff60] | 672 | JSON_O_FOREACH(node, k, v) { |
---|
[d1356cb] | 673 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
| 674 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
[29f72b7] | 675 | strip_html(txs->text); |
---|
[d1356cb] | 676 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
| 677 | struct tm parsed; |
---|
| 678 | |
---|
| 679 | /* Very sensitive to changes to the formatting of |
---|
| 680 | this field. :-( Also assumes the timezone used |
---|
| 681 | is UTC since C time handling functions suck. */ |
---|
[5ebff60] | 682 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) { |
---|
[d1356cb] | 683 | txs->created_at = mktime_utc(&parsed); |
---|
[5ebff60] | 684 | } |
---|
[d1356cb] | 685 | } else if (strcmp("sender", k) == 0 && v->type == json_object) { |
---|
| 686 | txs->user = twitter_xt_get_user(v); |
---|
| 687 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
| 688 | txs->id = v->u.integer; |
---|
| 689 | } |
---|
| 690 | } |
---|
| 691 | |
---|
[cd60710] | 692 | expand_entities(&txs->text, node); |
---|
[d1356cb] | 693 | |
---|
[5ebff60] | 694 | if (txs->text && txs->user && txs->id) { |
---|
[2cd8540] | 695 | return txs; |
---|
[5ebff60] | 696 | } |
---|
| 697 | |
---|
[2cd8540] | 698 | txs_free(txs); |
---|
| 699 | return NULL; |
---|
[d1356cb] | 700 | } |
---|
| 701 | |
---|
[cd60710] | 702 | static void expand_entities(char **text, const json_value *node) |
---|
[5ebff60] | 703 | { |
---|
[cd60710] | 704 | json_value *entities, *quoted; |
---|
| 705 | char *quote_url = NULL, *quote_text = NULL; |
---|
| 706 | |
---|
| 707 | if (!((entities = json_o_get(node, "entities")) && entities->type == json_object)) |
---|
| 708 | return; |
---|
| 709 | if ((quoted = json_o_get(node, "quoted_status")) && quoted->type == json_object) { |
---|
| 710 | /* New "retweets with comments" feature. Note that this info |
---|
| 711 | * seems to be included in the streaming API only! Grab the |
---|
| 712 | * full message and try to insert it when we run into the |
---|
| 713 | * Tweet entity. */ |
---|
| 714 | struct twitter_xml_status *txs = twitter_xt_get_status(quoted); |
---|
| 715 | quote_text = g_strdup_printf("@%s: %s", txs->user->screen_name, txs->text); |
---|
| 716 | quote_url = g_strdup_printf("%s/status/%" G_GUINT64_FORMAT, txs->user->screen_name, txs->id); |
---|
| 717 | txs_free(txs); |
---|
| 718 | } else { |
---|
| 719 | quoted = NULL; |
---|
| 720 | } |
---|
| 721 | |
---|
[5ebff60] | 722 | JSON_O_FOREACH(entities, k, v) { |
---|
[d1356cb] | 723 | int i; |
---|
[5ebff60] | 724 | |
---|
| 725 | if (v->type != json_array) { |
---|
[d1356cb] | 726 | continue; |
---|
[5ebff60] | 727 | } |
---|
| 728 | if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0) { |
---|
[d1356cb] | 729 | continue; |
---|
[5ebff60] | 730 | } |
---|
| 731 | |
---|
| 732 | for (i = 0; i < v->u.array.length; i++) { |
---|
[cd60710] | 733 | const char *format = "%s%s <%s>%s"; |
---|
| 734 | |
---|
[5ebff60] | 735 | if (v->u.array.values[i]->type != json_object) { |
---|
[8e3b7ac] | 736 | continue; |
---|
[5ebff60] | 737 | } |
---|
| 738 | |
---|
[d1356cb] | 739 | const char *kort = json_o_str(v->u.array.values[i], "url"); |
---|
| 740 | const char *disp = json_o_str(v->u.array.values[i], "display_url"); |
---|
[cd60710] | 741 | const char *full = json_o_str(v->u.array.values[i], "expanded_url"); |
---|
[d1356cb] | 742 | char *pos, *new; |
---|
[5ebff60] | 743 | |
---|
[cd60710] | 744 | if (!kort || !disp || !(pos = strstr(*text, kort))) { |
---|
[429a9b1] | 745 | continue; |
---|
[5ebff60] | 746 | } |
---|
[cd60710] | 747 | if (quote_url && strstr(full, quote_url)) { |
---|
| 748 | format = "%s<%s> [%s]%s"; |
---|
| 749 | disp = quote_text; |
---|
| 750 | } |
---|
[5ebff60] | 751 | |
---|
[d1356cb] | 752 | *pos = '\0'; |
---|
[cd60710] | 753 | new = g_strdup_printf(format, *text, kort, |
---|
[d1356cb] | 754 | disp, pos + strlen(kort)); |
---|
[5ebff60] | 755 | |
---|
[cd60710] | 756 | g_free(*text); |
---|
| 757 | *text = new; |
---|
[429a9b1] | 758 | } |
---|
[e193aeb] | 759 | } |
---|
[cd60710] | 760 | g_free(quote_text); |
---|
| 761 | g_free(quote_url); |
---|
[1b221e0] | 762 | } |
---|
| 763 | |
---|
| 764 | /** |
---|
| 765 | * Function to fill a twitter_xml_list struct. |
---|
| 766 | * It sets: |
---|
| 767 | * - all <status>es within the <status> element and |
---|
| 768 | * - the next_cursor. |
---|
| 769 | */ |
---|
[9e8c945] | 770 | static gboolean twitter_xt_get_status_list(struct im_connection *ic, const json_value *node, |
---|
| 771 | struct twitter_xml_list *txl) |
---|
[1b221e0] | 772 | { |
---|
| 773 | struct twitter_xml_status *txs; |
---|
[8e3b7ac] | 774 | int i; |
---|
[1b221e0] | 775 | |
---|
[62d2cfb] | 776 | // Set the type of the list. |
---|
| 777 | txl->type = TXL_STATUS; |
---|
[5ebff60] | 778 | |
---|
| 779 | if (node->type != json_array) { |
---|
[9e8c945] | 780 | return FALSE; |
---|
[5ebff60] | 781 | } |
---|
[62d2cfb] | 782 | |
---|
[1b221e0] | 783 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 784 | // Walk over the nodes children. |
---|
[5ebff60] | 785 | for (i = 0; i < node->u.array.length; i++) { |
---|
[c9b5817] | 786 | txs = twitter_xt_get_status(node->u.array.values[i]); |
---|
[5ebff60] | 787 | if (!txs) { |
---|
[c9b5817] | 788 | continue; |
---|
[5ebff60] | 789 | } |
---|
| 790 | |
---|
[8e3b7ac] | 791 | txl->list = g_slist_prepend(txl->list, txs); |
---|
[1b221e0] | 792 | } |
---|
| 793 | |
---|
[9e8c945] | 794 | return TRUE; |
---|
[1b221e0] | 795 | } |
---|
| 796 | |
---|
[c9b5817] | 797 | /* Will log messages either way. Need to keep track of IDs for stream deduping. |
---|
| 798 | Plus, show_ids is on by default and I don't see why anyone would disable it. */ |
---|
[ce81acd] | 799 | static char *twitter_msg_add_id(struct im_connection *ic, |
---|
[5ebff60] | 800 | struct twitter_xml_status *txs, const char *prefix) |
---|
[ce81acd] | 801 | { |
---|
| 802 | struct twitter_data *td = ic->proto_data; |
---|
[c9b5817] | 803 | int reply_to = -1; |
---|
| 804 | bee_user_t *bu; |
---|
[5983eca] | 805 | |
---|
| 806 | if (txs->reply_to) { |
---|
[ce81acd] | 807 | int i; |
---|
[5ebff60] | 808 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
[5983eca] | 809 | if (td->log[i].id == txs->reply_to) { |
---|
[c9b5817] | 810 | reply_to = i; |
---|
[ce81acd] | 811 | break; |
---|
| 812 | } |
---|
[5ebff60] | 813 | } |
---|
[ce81acd] | 814 | } |
---|
[5983eca] | 815 | |
---|
[c9b5817] | 816 | if (txs->user && txs->user->screen_name && |
---|
| 817 | (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
| 818 | struct twitter_user_data *tud = bu->data; |
---|
| 819 | |
---|
| 820 | if (txs->id > tud->last_id) { |
---|
| 821 | tud->last_id = txs->id; |
---|
| 822 | tud->last_time = txs->created_at; |
---|
| 823 | } |
---|
| 824 | } |
---|
[5ebff60] | 825 | |
---|
[c9b5817] | 826 | td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; |
---|
| 827 | td->log[td->log_id].id = txs->id; |
---|
| 828 | td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); |
---|
[5ebff60] | 829 | |
---|
[67f6828] | 830 | /* This is all getting hairy. :-( If we RT'ed something ourselves, |
---|
| 831 | remember OUR id instead so undo will work. In other cases, the |
---|
| 832 | original tweet's id should be remembered for deduplicating. */ |
---|
[5ebff60] | 833 | if (g_strcasecmp(txs->user->screen_name, td->user) == 0) { |
---|
[67f6828] | 834 | td->log[td->log_id].id = txs->rt_id; |
---|
[c43146d] | 835 | /* More useful than NULL. */ |
---|
| 836 | td->log[td->log_id].bu = &twitter_log_local_user; |
---|
[5ebff60] | 837 | } |
---|
| 838 | |
---|
[0ca1d79] | 839 | if (set_getbool(&ic->acc->set, "show_ids")) { |
---|
[5ebff60] | 840 | if (reply_to != -1) { |
---|
[0ca1d79] | 841 | return g_strdup_printf("\002[\002%02x->%02x\002]\002 %s%s", |
---|
| 842 | td->log_id, reply_to, prefix, txs->text); |
---|
[5ebff60] | 843 | } else { |
---|
[0ca1d79] | 844 | return g_strdup_printf("\002[\002%02x\002]\002 %s%s", |
---|
| 845 | td->log_id, prefix, txs->text); |
---|
[5ebff60] | 846 | } |
---|
[0ca1d79] | 847 | } else { |
---|
[5ebff60] | 848 | if (*prefix) { |
---|
[0ca1d79] | 849 | return g_strconcat(prefix, txs->text, NULL); |
---|
[5ebff60] | 850 | } else { |
---|
[0ca1d79] | 851 | return NULL; |
---|
[5ebff60] | 852 | } |
---|
[0ca1d79] | 853 | } |
---|
[ce81acd] | 854 | } |
---|
| 855 | |
---|
[73ee390] | 856 | /** |
---|
| 857 | * Function that is called to see the filter statuses in groupchat windows. |
---|
| 858 | */ |
---|
| 859 | static void twitter_status_show_filter(struct im_connection *ic, struct twitter_xml_status *status) |
---|
| 860 | { |
---|
| 861 | struct twitter_data *td = ic->proto_data; |
---|
| 862 | char *msg = twitter_msg_add_id(ic, status, ""); |
---|
| 863 | struct twitter_filter *tf; |
---|
| 864 | GSList *f; |
---|
| 865 | GSList *l; |
---|
| 866 | |
---|
| 867 | for (f = td->filters; f; f = g_slist_next(f)) { |
---|
| 868 | tf = f->data; |
---|
| 869 | |
---|
| 870 | switch (tf->type) { |
---|
| 871 | case TWITTER_FILTER_TYPE_FOLLOW: |
---|
[5ebff60] | 872 | if (status->user->uid != tf->uid) { |
---|
[73ee390] | 873 | continue; |
---|
[5ebff60] | 874 | } |
---|
[73ee390] | 875 | break; |
---|
| 876 | |
---|
| 877 | case TWITTER_FILTER_TYPE_TRACK: |
---|
[5ebff60] | 878 | if (strcasestr(status->text, tf->text) == NULL) { |
---|
[73ee390] | 879 | continue; |
---|
[5ebff60] | 880 | } |
---|
[73ee390] | 881 | break; |
---|
| 882 | |
---|
| 883 | default: |
---|
| 884 | continue; |
---|
| 885 | } |
---|
| 886 | |
---|
| 887 | for (l = tf->groupchats; l; l = g_slist_next(l)) { |
---|
| 888 | imcb_chat_msg(l->data, status->user->screen_name, |
---|
[5ebff60] | 889 | msg ? msg : status->text, 0, 0); |
---|
[73ee390] | 890 | } |
---|
| 891 | } |
---|
| 892 | |
---|
| 893 | g_free(msg); |
---|
| 894 | } |
---|
| 895 | |
---|
[62d2cfb] | 896 | /** |
---|
| 897 | * Function that is called to see the statuses in a groupchat window. |
---|
| 898 | */ |
---|
[29f72b7] | 899 | static void twitter_status_show_chat(struct im_connection *ic, struct twitter_xml_status *status) |
---|
[62d2cfb] | 900 | { |
---|
| 901 | struct twitter_data *td = ic->proto_data; |
---|
| 902 | struct groupchat *gc; |
---|
[29f72b7] | 903 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
| 904 | char *msg; |
---|
[62d2cfb] | 905 | |
---|
| 906 | // Create a new groupchat if it does not exsist. |
---|
[631ec80] | 907 | gc = twitter_groupchat_init(ic); |
---|
[62d2cfb] | 908 | |
---|
[5ebff60] | 909 | if (!me) { |
---|
[29f72b7] | 910 | /* MUST be done before twitter_msg_add_id() to avoid #872. */ |
---|
| 911 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
[5ebff60] | 912 | } |
---|
[29f72b7] | 913 | msg = twitter_msg_add_id(ic, status, ""); |
---|
[5ebff60] | 914 | |
---|
[29f72b7] | 915 | // Say it! |
---|
| 916 | if (me) { |
---|
| 917 | imcb_chat_log(gc, "You: %s", msg ? msg : status->text); |
---|
| 918 | } else { |
---|
| 919 | imcb_chat_msg(gc, status->user->screen_name, |
---|
[5ebff60] | 920 | msg ? msg : status->text, 0, status->created_at); |
---|
[62d2cfb] | 921 | } |
---|
[29f72b7] | 922 | |
---|
| 923 | g_free(msg); |
---|
[62d2cfb] | 924 | } |
---|
| 925 | |
---|
| 926 | /** |
---|
| 927 | * Function that is called to see statuses as private messages. |
---|
| 928 | */ |
---|
[29f72b7] | 929 | static void twitter_status_show_msg(struct im_connection *ic, struct twitter_xml_status *status) |
---|
[62d2cfb] | 930 | { |
---|
| 931 | struct twitter_data *td = ic->proto_data; |
---|
[631ec80] | 932 | char from[MAX_STRING] = ""; |
---|
[29f72b7] | 933 | char *prefix = NULL, *text = NULL; |
---|
| 934 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
[62d2cfb] | 935 | |
---|
[631ec80] | 936 | if (td->flags & TWITTER_MODE_ONE) { |
---|
[5983eca] | 937 | g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user); |
---|
| 938 | from[MAX_STRING - 1] = '\0'; |
---|
[e88fbe27] | 939 | } |
---|
[5983eca] | 940 | |
---|
[5ebff60] | 941 | if (td->flags & TWITTER_MODE_ONE) { |
---|
[29f72b7] | 942 | prefix = g_strdup_printf("\002<\002%s\002>\002 ", |
---|
| 943 | status->user->screen_name); |
---|
[5ebff60] | 944 | } else if (!me) { |
---|
[29f72b7] | 945 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
[5ebff60] | 946 | } else { |
---|
[29f72b7] | 947 | prefix = g_strdup("You: "); |
---|
[5ebff60] | 948 | } |
---|
[5983eca] | 949 | |
---|
[29f72b7] | 950 | text = twitter_msg_add_id(ic, status, prefix ? prefix : ""); |
---|
[5983eca] | 951 | |
---|
[29f72b7] | 952 | imcb_buddy_msg(ic, |
---|
| 953 | *from ? from : status->user->screen_name, |
---|
| 954 | text ? text : status->text, 0, status->created_at); |
---|
[5983eca] | 955 | |
---|
[29f72b7] | 956 | g_free(text); |
---|
| 957 | g_free(prefix); |
---|
| 958 | } |
---|
[5983eca] | 959 | |
---|
[29f72b7] | 960 | static void twitter_status_show(struct im_connection *ic, struct twitter_xml_status *status) |
---|
| 961 | { |
---|
| 962 | struct twitter_data *td = ic->proto_data; |
---|
[7549d00] | 963 | char *last_id_str; |
---|
[fb62f132] | 964 | char *uid_str; |
---|
[5ebff60] | 965 | |
---|
| 966 | if (status->user == NULL || status->text == NULL) { |
---|
[29f72b7] | 967 | return; |
---|
[5ebff60] | 968 | } |
---|
[fb62f132] | 969 | |
---|
| 970 | /* Check this is not a tweet that should be muted */ |
---|
[166a571] | 971 | uid_str = g_strdup_printf("%" PRIu64, status->user->uid); |
---|
[a244877] | 972 | |
---|
[fb62f132] | 973 | if (g_slist_find_custom(td->mutes_ids, uid_str, (GCompareFunc)strcmp)) { |
---|
| 974 | g_free(uid_str); |
---|
| 975 | return; |
---|
| 976 | } |
---|
| 977 | if (status->id != status->rt_id && g_slist_find_custom(td->noretweets_ids, uid_str, (GCompareFunc)strcmp)) { |
---|
| 978 | g_free(uid_str); |
---|
| 979 | return; |
---|
| 980 | } |
---|
[5ebff60] | 981 | |
---|
[29f72b7] | 982 | /* Grrrr. Would like to do this during parsing, but can't access |
---|
| 983 | settings from there. */ |
---|
[5ebff60] | 984 | if (set_getbool(&ic->acc->set, "strip_newlines")) { |
---|
[29f72b7] | 985 | strip_newlines(status->text); |
---|
[5ebff60] | 986 | } |
---|
| 987 | |
---|
| 988 | if (status->from_filter) { |
---|
[73ee390] | 989 | twitter_status_show_filter(ic, status); |
---|
[5ebff60] | 990 | } else if (td->flags & TWITTER_MODE_CHAT) { |
---|
[29f72b7] | 991 | twitter_status_show_chat(ic, status); |
---|
[5ebff60] | 992 | } else { |
---|
[29f72b7] | 993 | twitter_status_show_msg(ic, status); |
---|
[5ebff60] | 994 | } |
---|
[5983eca] | 995 | |
---|
[29f72b7] | 996 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
| 997 | // we won't pick up the updates already in the list. |
---|
[573e274] | 998 | td->timeline_id = MAX(td->timeline_id, status->rt_id); |
---|
[7549d00] | 999 | |
---|
| 1000 | last_id_str = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
[fedc8f1] | 1001 | set_setstr(&ic->acc->set, "_last_tweet", last_id_str); |
---|
[7549d00] | 1002 | g_free(last_id_str); |
---|
[fb62f132] | 1003 | g_free(uid_str); |
---|
[62d2cfb] | 1004 | } |
---|
| 1005 | |
---|
[73ee390] | 1006 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o, gboolean from_filter); |
---|
[ddc2de5] | 1007 | |
---|
| 1008 | static void twitter_http_stream(struct http_request *req) |
---|
| 1009 | { |
---|
[dff0e0b] | 1010 | struct im_connection *ic = req->data; |
---|
[dd672e2] | 1011 | struct twitter_data *td; |
---|
[dff0e0b] | 1012 | json_value *parsed; |
---|
[62f6b45] | 1013 | int len = 0; |
---|
| 1014 | char c, *nl; |
---|
[73ee390] | 1015 | gboolean from_filter; |
---|
[5ebff60] | 1016 | |
---|
| 1017 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[dff0e0b] | 1018 | return; |
---|
[5ebff60] | 1019 | } |
---|
| 1020 | |
---|
[e132b60] | 1021 | ic->flags |= OPT_PONGED; |
---|
[dd672e2] | 1022 | td = ic->proto_data; |
---|
[5ebff60] | 1023 | |
---|
[dd672e2] | 1024 | if ((req->flags & HTTPC_EOF) || !req->reply_body) { |
---|
[5ebff60] | 1025 | if (req == td->stream) { |
---|
[73ee390] | 1026 | td->stream = NULL; |
---|
[5ebff60] | 1027 | } else if (req == td->filter_stream) { |
---|
[73ee390] | 1028 | td->filter_stream = NULL; |
---|
[5ebff60] | 1029 | } |
---|
[73ee390] | 1030 | |
---|
[dd672e2] | 1031 | imcb_error(ic, "Stream closed (%s)", req->status_string); |
---|
[7320610] | 1032 | if (req->status_code == 401) { |
---|
| 1033 | imcb_error(ic, "Check your system clock."); |
---|
| 1034 | } |
---|
[dd672e2] | 1035 | imc_logout(ic, TRUE); |
---|
| 1036 | return; |
---|
| 1037 | } |
---|
[5ebff60] | 1038 | |
---|
[62f6b45] | 1039 | /* MUST search for CRLF, not just LF: |
---|
| 1040 | https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */ |
---|
[5ebff60] | 1041 | if (!(nl = strstr(req->reply_body, "\r\n"))) { |
---|
[ddc2de5] | 1042 | return; |
---|
[5ebff60] | 1043 | } |
---|
| 1044 | |
---|
[62f6b45] | 1045 | len = nl - req->reply_body; |
---|
| 1046 | if (len > 0) { |
---|
| 1047 | c = req->reply_body[len]; |
---|
| 1048 | req->reply_body[len] = '\0'; |
---|
[5ebff60] | 1049 | |
---|
[7a80925] | 1050 | if ((parsed = json_parse(req->reply_body, req->body_size))) { |
---|
[73ee390] | 1051 | from_filter = (req == td->filter_stream); |
---|
| 1052 | twitter_stream_handle_object(ic, parsed, from_filter); |
---|
[62f6b45] | 1053 | } |
---|
| 1054 | json_value_free(parsed); |
---|
| 1055 | req->reply_body[len] = c; |
---|
[dff0e0b] | 1056 | } |
---|
[5ebff60] | 1057 | |
---|
[62f6b45] | 1058 | http_flush_bytes(req, len + 2); |
---|
[5ebff60] | 1059 | |
---|
[dff0e0b] | 1060 | /* One notification might bring multiple events! */ |
---|
[5ebff60] | 1061 | if (req->body_size > 0) { |
---|
[dff0e0b] | 1062 | twitter_http_stream(req); |
---|
[5ebff60] | 1063 | } |
---|
[ddc2de5] | 1064 | } |
---|
[2322a9f] | 1065 | |
---|
[5aa96fc8] | 1066 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o); |
---|
[c9b5817] | 1067 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs); |
---|
[5aa96fc8] | 1068 | |
---|
[73ee390] | 1069 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o, gboolean from_filter) |
---|
[dff0e0b] | 1070 | { |
---|
[5aa96fc8] | 1071 | struct twitter_data *td = ic->proto_data; |
---|
[c9b5817] | 1072 | struct twitter_xml_status *txs; |
---|
[d1356cb] | 1073 | json_value *c; |
---|
[5ebff60] | 1074 | |
---|
[c9b5817] | 1075 | if ((txs = twitter_xt_get_status(o))) { |
---|
[73ee390] | 1076 | txs->from_filter = from_filter; |
---|
[2dcde94] | 1077 | gboolean ret = twitter_stream_handle_status(ic, txs); |
---|
| 1078 | txs_free(txs); |
---|
| 1079 | return ret; |
---|
[d1356cb] | 1080 | } else if ((c = json_o_get(o, "direct_message")) && |
---|
[2cd8540] | 1081 | (txs = twitter_xt_get_dm(c))) { |
---|
[5ebff60] | 1082 | if (g_strcasecmp(txs->user->screen_name, td->user) != 0) { |
---|
[2cd8540] | 1083 | imcb_buddy_msg(ic, txs->user->screen_name, |
---|
[5ebff60] | 1084 | txs->text, 0, txs->created_at); |
---|
| 1085 | } |
---|
[d1356cb] | 1086 | txs_free(txs); |
---|
| 1087 | return TRUE; |
---|
[5aa96fc8] | 1088 | } else if ((c = json_o_get(o, "event")) && c->type == json_string) { |
---|
| 1089 | twitter_stream_handle_event(ic, o); |
---|
| 1090 | return TRUE; |
---|
| 1091 | } else if ((c = json_o_get(o, "disconnect")) && c->type == json_object) { |
---|
| 1092 | /* HACK: Because we're inside an event handler, we can't just |
---|
| 1093 | disconnect here. Instead, just change the HTTP status string |
---|
| 1094 | into a Twitter status string. */ |
---|
| 1095 | char *reason = json_o_strdup(c, "reason"); |
---|
| 1096 | if (reason) { |
---|
| 1097 | g_free(td->stream->status_string); |
---|
| 1098 | td->stream->status_string = reason; |
---|
| 1099 | } |
---|
| 1100 | return TRUE; |
---|
[dff0e0b] | 1101 | } |
---|
| 1102 | return FALSE; |
---|
| 1103 | } |
---|
| 1104 | |
---|
[c9b5817] | 1105 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs) |
---|
| 1106 | { |
---|
| 1107 | struct twitter_data *td = ic->proto_data; |
---|
| 1108 | int i; |
---|
[5ebff60] | 1109 | |
---|
[c9b5817] | 1110 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
| 1111 | if (td->log[i].id == txs->id) { |
---|
[29f72b7] | 1112 | /* Got a duplicate (RT, probably). Drop it. */ |
---|
[c9b5817] | 1113 | return TRUE; |
---|
| 1114 | } |
---|
| 1115 | } |
---|
[5ebff60] | 1116 | |
---|
[9f8bb17] | 1117 | if (!(g_strcasecmp(txs->user->screen_name, td->user) == 0 || |
---|
[2dcde94] | 1118 | set_getbool(&ic->acc->set, "fetch_mentions") || |
---|
[c9b5817] | 1119 | bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
| 1120 | /* Tweet is from an unknown person and the user does not want |
---|
| 1121 | to see @mentions, so drop it. twitter_stream_handle_event() |
---|
| 1122 | picks up new follows so this simple filter should be safe. */ |
---|
| 1123 | /* TODO: The streaming API seems to do poor @mention matching. |
---|
| 1124 | I.e. I'm getting mentions for @WilmerSomething, not just for |
---|
| 1125 | @Wilmer. But meh. You want spam, you get spam. */ |
---|
| 1126 | return TRUE; |
---|
| 1127 | } |
---|
[5ebff60] | 1128 | |
---|
[29f72b7] | 1129 | twitter_status_show(ic, txs); |
---|
[5ebff60] | 1130 | |
---|
[c9b5817] | 1131 | return TRUE; |
---|
| 1132 | } |
---|
| 1133 | |
---|
[5aa96fc8] | 1134 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o) |
---|
| 1135 | { |
---|
| 1136 | struct twitter_data *td = ic->proto_data; |
---|
| 1137 | json_value *source = json_o_get(o, "source"); |
---|
| 1138 | json_value *target = json_o_get(o, "target"); |
---|
| 1139 | const char *type = json_o_str(o, "event"); |
---|
[0d581bd] | 1140 | struct twitter_xml_user *us = NULL; |
---|
| 1141 | struct twitter_xml_user *ut = NULL; |
---|
[5ebff60] | 1142 | |
---|
[5aa96fc8] | 1143 | if (!type || !source || source->type != json_object |
---|
[5ebff60] | 1144 | || !target || target->type != json_object) { |
---|
[5aa96fc8] | 1145 | return FALSE; |
---|
| 1146 | } |
---|
[5ebff60] | 1147 | |
---|
[5aa96fc8] | 1148 | if (strcmp(type, "follow") == 0) { |
---|
[0d581bd] | 1149 | us = twitter_xt_get_user(source); |
---|
| 1150 | ut = twitter_xt_get_user(target); |
---|
[9f8bb17] | 1151 | if (g_strcasecmp(us->screen_name, td->user) == 0) { |
---|
[5aa96fc8] | 1152 | twitter_add_buddy(ic, ut->screen_name, ut->name); |
---|
| 1153 | } |
---|
[0d581bd] | 1154 | } else if (strcmp(type, "mute") == 0) { |
---|
[166a571] | 1155 | GSList *found; |
---|
| 1156 | char *uid_str; |
---|
[0d581bd] | 1157 | ut = twitter_xt_get_user(target); |
---|
[166a571] | 1158 | uid_str = g_strdup_printf("%" PRIu64, ut->uid); |
---|
| 1159 | if (!(found = g_slist_find_custom(td->mutes_ids, uid_str, |
---|
| 1160 | (GCompareFunc)strcmp))) { |
---|
| 1161 | td->mutes_ids = g_slist_prepend(td->mutes_ids, uid_str); |
---|
| 1162 | } |
---|
| 1163 | twitter_log(ic, "Muted user %s", ut->screen_name); |
---|
| 1164 | if (getenv("BITLBEE_DEBUG")) { |
---|
| 1165 | fprintf(stderr, "New mute: %s %"PRIu64"\n", |
---|
| 1166 | ut->screen_name, ut->uid); |
---|
| 1167 | } |
---|
[0d581bd] | 1168 | } else if (strcmp(type, "unmute") == 0) { |
---|
| 1169 | GSList *found; |
---|
| 1170 | char *uid_str; |
---|
| 1171 | ut = twitter_xt_get_user(target); |
---|
[166a571] | 1172 | uid_str = g_strdup_printf("%" PRIu64, ut->uid); |
---|
[0d581bd] | 1173 | if ((found = g_slist_find_custom(td->mutes_ids, uid_str, |
---|
| 1174 | (GCompareFunc)strcmp))) { |
---|
[a244877] | 1175 | char *found_str = found->data; |
---|
| 1176 | td->mutes_ids = g_slist_delete_link(td->mutes_ids, found); |
---|
| 1177 | g_free(found_str); |
---|
[0d581bd] | 1178 | } |
---|
| 1179 | g_free(uid_str); |
---|
[166a571] | 1180 | twitter_log(ic, "Unmuted user %s", ut->screen_name); |
---|
| 1181 | if (getenv("BITLBEE_DEBUG")) { |
---|
| 1182 | fprintf(stderr, "New unmute: %s %"PRIu64"\n", |
---|
| 1183 | ut->screen_name, ut->uid); |
---|
| 1184 | } |
---|
[5aa96fc8] | 1185 | } |
---|
[5ebff60] | 1186 | |
---|
[0d581bd] | 1187 | txu_free(us); |
---|
| 1188 | txu_free(ut); |
---|
| 1189 | |
---|
[5aa96fc8] | 1190 | return TRUE; |
---|
| 1191 | } |
---|
| 1192 | |
---|
[dff0e0b] | 1193 | gboolean twitter_open_stream(struct im_connection *ic) |
---|
| 1194 | { |
---|
| 1195 | struct twitter_data *td = ic->proto_data; |
---|
[5ebff60] | 1196 | char *args[2] = { "with", "followings" }; |
---|
| 1197 | |
---|
[dff0e0b] | 1198 | if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL, |
---|
[62f6b45] | 1199 | twitter_http_stream, ic, 0, args, 2))) { |
---|
[dff0e0b] | 1200 | /* This flag must be enabled or we'll get no data until EOF |
---|
| 1201 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
| 1202 | td->stream->flags |= HTTPC_STREAMING; |
---|
| 1203 | return TRUE; |
---|
| 1204 | } |
---|
[5ebff60] | 1205 | |
---|
[dff0e0b] | 1206 | return FALSE; |
---|
| 1207 | } |
---|
| 1208 | |
---|
[73ee390] | 1209 | static gboolean twitter_filter_stream(struct im_connection *ic) |
---|
| 1210 | { |
---|
| 1211 | struct twitter_data *td = ic->proto_data; |
---|
[5ebff60] | 1212 | char *args[4] = { "follow", NULL, "track", NULL }; |
---|
[73ee390] | 1213 | GString *followstr = g_string_new(""); |
---|
| 1214 | GString *trackstr = g_string_new(""); |
---|
| 1215 | gboolean ret = FALSE; |
---|
| 1216 | struct twitter_filter *tf; |
---|
| 1217 | GSList *l; |
---|
| 1218 | |
---|
| 1219 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
| 1220 | tf = l->data; |
---|
| 1221 | |
---|
| 1222 | switch (tf->type) { |
---|
| 1223 | case TWITTER_FILTER_TYPE_FOLLOW: |
---|
[5ebff60] | 1224 | if (followstr->len > 0) { |
---|
[73ee390] | 1225 | g_string_append_c(followstr, ','); |
---|
[5ebff60] | 1226 | } |
---|
[73ee390] | 1227 | |
---|
| 1228 | g_string_append_printf(followstr, "%" G_GUINT64_FORMAT, |
---|
[5ebff60] | 1229 | tf->uid); |
---|
[73ee390] | 1230 | break; |
---|
| 1231 | |
---|
| 1232 | case TWITTER_FILTER_TYPE_TRACK: |
---|
[5ebff60] | 1233 | if (trackstr->len > 0) { |
---|
[73ee390] | 1234 | g_string_append_c(trackstr, ','); |
---|
[5ebff60] | 1235 | } |
---|
[73ee390] | 1236 | |
---|
| 1237 | g_string_append(trackstr, tf->text); |
---|
| 1238 | break; |
---|
| 1239 | |
---|
| 1240 | default: |
---|
| 1241 | continue; |
---|
| 1242 | } |
---|
| 1243 | } |
---|
| 1244 | |
---|
| 1245 | args[1] = followstr->str; |
---|
| 1246 | args[3] = trackstr->str; |
---|
| 1247 | |
---|
[5ebff60] | 1248 | if (td->filter_stream) { |
---|
[73ee390] | 1249 | http_close(td->filter_stream); |
---|
[5ebff60] | 1250 | } |
---|
[73ee390] | 1251 | |
---|
| 1252 | if ((td->filter_stream = twitter_http(ic, TWITTER_FILTER_STREAM_URL, |
---|
| 1253 | twitter_http_stream, ic, 0, |
---|
[5ebff60] | 1254 | args, 4))) { |
---|
[73ee390] | 1255 | /* This flag must be enabled or we'll get no data until EOF |
---|
| 1256 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
| 1257 | td->filter_stream->flags |= HTTPC_STREAMING; |
---|
| 1258 | ret = TRUE; |
---|
| 1259 | } |
---|
| 1260 | |
---|
| 1261 | g_string_free(followstr, TRUE); |
---|
| 1262 | g_string_free(trackstr, TRUE); |
---|
| 1263 | |
---|
| 1264 | return ret; |
---|
| 1265 | } |
---|
| 1266 | |
---|
| 1267 | static void twitter_filter_users_post(struct http_request *req) |
---|
| 1268 | { |
---|
| 1269 | struct im_connection *ic = req->data; |
---|
| 1270 | struct twitter_data *td; |
---|
| 1271 | struct twitter_filter *tf; |
---|
| 1272 | GList *users = NULL; |
---|
| 1273 | json_value *parsed; |
---|
| 1274 | json_value *id; |
---|
| 1275 | const char *name; |
---|
| 1276 | GString *fstr; |
---|
| 1277 | GSList *l; |
---|
| 1278 | GList *u; |
---|
| 1279 | int i; |
---|
| 1280 | |
---|
| 1281 | // Check if the connection is still active. |
---|
[5ebff60] | 1282 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[73ee390] | 1283 | return; |
---|
[5ebff60] | 1284 | } |
---|
[73ee390] | 1285 | |
---|
| 1286 | td = ic->proto_data; |
---|
| 1287 | |
---|
[5ebff60] | 1288 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[73ee390] | 1289 | return; |
---|
[5ebff60] | 1290 | } |
---|
[73ee390] | 1291 | |
---|
| 1292 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
| 1293 | tf = l->data; |
---|
| 1294 | |
---|
[5ebff60] | 1295 | if (tf->type == TWITTER_FILTER_TYPE_FOLLOW) { |
---|
[73ee390] | 1296 | users = g_list_prepend(users, tf); |
---|
[5ebff60] | 1297 | } |
---|
[73ee390] | 1298 | } |
---|
| 1299 | |
---|
[5ebff60] | 1300 | if (parsed->type != json_array) { |
---|
[73ee390] | 1301 | goto finish; |
---|
[5ebff60] | 1302 | } |
---|
[73ee390] | 1303 | |
---|
| 1304 | for (i = 0; i < parsed->u.array.length; i++) { |
---|
| 1305 | id = json_o_get(parsed->u.array.values[i], "id"); |
---|
| 1306 | name = json_o_str(parsed->u.array.values[i], "screen_name"); |
---|
| 1307 | |
---|
[5ebff60] | 1308 | if (!name || !id || id->type != json_integer) { |
---|
[73ee390] | 1309 | continue; |
---|
[5ebff60] | 1310 | } |
---|
[73ee390] | 1311 | |
---|
| 1312 | for (u = users; u; u = g_list_next(u)) { |
---|
| 1313 | tf = u->data; |
---|
| 1314 | |
---|
| 1315 | if (g_strcasecmp(tf->text, name) == 0) { |
---|
| 1316 | tf->uid = id->u.integer; |
---|
| 1317 | users = g_list_delete_link(users, u); |
---|
| 1318 | break; |
---|
| 1319 | } |
---|
| 1320 | } |
---|
| 1321 | } |
---|
| 1322 | |
---|
| 1323 | finish: |
---|
| 1324 | json_value_free(parsed); |
---|
| 1325 | twitter_filter_stream(ic); |
---|
| 1326 | |
---|
[5ebff60] | 1327 | if (!users) { |
---|
[73ee390] | 1328 | return; |
---|
[5ebff60] | 1329 | } |
---|
[73ee390] | 1330 | |
---|
| 1331 | fstr = g_string_new(""); |
---|
| 1332 | |
---|
| 1333 | for (u = users; u; u = g_list_next(u)) { |
---|
[5ebff60] | 1334 | if (fstr->len > 0) { |
---|
[73ee390] | 1335 | g_string_append(fstr, ", "); |
---|
[5ebff60] | 1336 | } |
---|
[73ee390] | 1337 | |
---|
| 1338 | g_string_append(fstr, tf->text); |
---|
| 1339 | } |
---|
| 1340 | |
---|
| 1341 | imcb_error(ic, "Failed UID acquisitions: %s", fstr->str); |
---|
| 1342 | |
---|
| 1343 | g_string_free(fstr, TRUE); |
---|
| 1344 | g_list_free(users); |
---|
| 1345 | } |
---|
| 1346 | |
---|
| 1347 | gboolean twitter_open_filter_stream(struct im_connection *ic) |
---|
| 1348 | { |
---|
| 1349 | struct twitter_data *td = ic->proto_data; |
---|
[5ebff60] | 1350 | char *args[2] = { "screen_name", NULL }; |
---|
[73ee390] | 1351 | GString *ustr = g_string_new(""); |
---|
| 1352 | struct twitter_filter *tf; |
---|
| 1353 | struct http_request *req; |
---|
| 1354 | GSList *l; |
---|
| 1355 | |
---|
| 1356 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
| 1357 | tf = l->data; |
---|
| 1358 | |
---|
[5ebff60] | 1359 | if (tf->type != TWITTER_FILTER_TYPE_FOLLOW || tf->uid != 0) { |
---|
[73ee390] | 1360 | continue; |
---|
[5ebff60] | 1361 | } |
---|
[73ee390] | 1362 | |
---|
[5ebff60] | 1363 | if (ustr->len > 0) { |
---|
[73ee390] | 1364 | g_string_append_c(ustr, ','); |
---|
[5ebff60] | 1365 | } |
---|
[73ee390] | 1366 | |
---|
| 1367 | g_string_append(ustr, tf->text); |
---|
| 1368 | } |
---|
| 1369 | |
---|
| 1370 | if (ustr->len == 0) { |
---|
| 1371 | g_string_free(ustr, TRUE); |
---|
| 1372 | return twitter_filter_stream(ic); |
---|
| 1373 | } |
---|
| 1374 | |
---|
| 1375 | args[1] = ustr->str; |
---|
| 1376 | req = twitter_http(ic, TWITTER_USERS_LOOKUP_URL, |
---|
[5ebff60] | 1377 | twitter_filter_users_post, |
---|
| 1378 | ic, 0, args, 2); |
---|
[73ee390] | 1379 | |
---|
| 1380 | g_string_free(ustr, TRUE); |
---|
| 1381 | return req != NULL; |
---|
| 1382 | } |
---|
| 1383 | |
---|
[dff0e0b] | 1384 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor); |
---|
| 1385 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor); |
---|
| 1386 | |
---|
[2322a9f] | 1387 | /** |
---|
| 1388 | * Get the timeline with optionally mentions |
---|
| 1389 | */ |
---|
[2f9027c] | 1390 | gboolean twitter_get_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
[2322a9f] | 1391 | { |
---|
| 1392 | struct twitter_data *td = ic->proto_data; |
---|
| 1393 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
| 1394 | |
---|
| 1395 | if (td->flags & TWITTER_DOING_TIMELINE) { |
---|
[11ec078] | 1396 | if (++td->http_fails >= 5) { |
---|
| 1397 | imcb_error(ic, "Fetch timeout (%d)", td->flags); |
---|
| 1398 | imc_logout(ic, TRUE); |
---|
[2f9027c] | 1399 | return FALSE; |
---|
[11ec078] | 1400 | } |
---|
[2322a9f] | 1401 | } |
---|
| 1402 | |
---|
| 1403 | td->flags |= TWITTER_DOING_TIMELINE; |
---|
| 1404 | |
---|
| 1405 | twitter_get_home_timeline(ic, next_cursor); |
---|
| 1406 | |
---|
| 1407 | if (include_mentions) { |
---|
| 1408 | twitter_get_mentions(ic, next_cursor); |
---|
| 1409 | } |
---|
[5ebff60] | 1410 | |
---|
[2f9027c] | 1411 | return TRUE; |
---|
[2322a9f] | 1412 | } |
---|
| 1413 | |
---|
| 1414 | /** |
---|
| 1415 | * Call this one after receiving timeline/mentions. Show to user once we have |
---|
| 1416 | * both. |
---|
| 1417 | */ |
---|
| 1418 | void twitter_flush_timeline(struct im_connection *ic) |
---|
| 1419 | { |
---|
| 1420 | struct twitter_data *td = ic->proto_data; |
---|
| 1421 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
[b5fe39b] | 1422 | int show_old_mentions = set_getint(&ic->acc->set, "show_old_mentions"); |
---|
[2322a9f] | 1423 | struct twitter_xml_list *home_timeline = td->home_timeline_obj; |
---|
| 1424 | struct twitter_xml_list *mentions = td->mentions_obj; |
---|
[29f72b7] | 1425 | guint64 last_id = 0; |
---|
[2322a9f] | 1426 | GSList *output = NULL; |
---|
| 1427 | GSList *l; |
---|
| 1428 | |
---|
[29f72b7] | 1429 | imcb_connected(ic); |
---|
[5ebff60] | 1430 | |
---|
[2322a9f] | 1431 | if (!(td->flags & TWITTER_GOT_TIMELINE)) { |
---|
| 1432 | return; |
---|
| 1433 | } |
---|
| 1434 | |
---|
| 1435 | if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) { |
---|
| 1436 | return; |
---|
| 1437 | } |
---|
| 1438 | |
---|
| 1439 | if (home_timeline && home_timeline->list) { |
---|
| 1440 | for (l = home_timeline->list; l; l = g_slist_next(l)) { |
---|
| 1441 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
| 1442 | } |
---|
| 1443 | } |
---|
| 1444 | |
---|
| 1445 | if (include_mentions && mentions && mentions->list) { |
---|
| 1446 | for (l = mentions->list; l; l = g_slist_next(l)) { |
---|
[b5fe39b] | 1447 | if (show_old_mentions < 1 && output && twitter_compare_elements(l->data, output->data) < 0) { |
---|
[2322a9f] | 1448 | continue; |
---|
| 1449 | } |
---|
| 1450 | |
---|
| 1451 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
| 1452 | } |
---|
| 1453 | } |
---|
| 1454 | |
---|
| 1455 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
[29f72b7] | 1456 | while (output) { |
---|
| 1457 | struct twitter_xml_status *txs = output->data; |
---|
[5ebff60] | 1458 | if (txs->id != last_id) { |
---|
[29f72b7] | 1459 | twitter_status_show(ic, txs); |
---|
[5ebff60] | 1460 | } |
---|
[29f72b7] | 1461 | last_id = txs->id; |
---|
| 1462 | output = g_slist_remove(output, txs); |
---|
| 1463 | } |
---|
[2322a9f] | 1464 | |
---|
[199fea6] | 1465 | txl_free(home_timeline); |
---|
| 1466 | txl_free(mentions); |
---|
[2322a9f] | 1467 | |
---|
| 1468 | td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS); |
---|
[199fea6] | 1469 | td->home_timeline_obj = td->mentions_obj = NULL; |
---|
[2322a9f] | 1470 | } |
---|
| 1471 | |
---|
[ddc2de5] | 1472 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
| 1473 | static void twitter_http_get_mentions(struct http_request *req); |
---|
| 1474 | |
---|
[2322a9f] | 1475 | /** |
---|
| 1476 | * Get the timeline. |
---|
| 1477 | */ |
---|
[ddc2de5] | 1478 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
[2322a9f] | 1479 | { |
---|
| 1480 | struct twitter_data *td = ic->proto_data; |
---|
| 1481 | |
---|
[199fea6] | 1482 | txl_free(td->home_timeline_obj); |
---|
[2322a9f] | 1483 | td->home_timeline_obj = NULL; |
---|
| 1484 | td->flags &= ~TWITTER_GOT_TIMELINE; |
---|
| 1485 | |
---|
[15c4cd4] | 1486 | char *args[8]; |
---|
[2322a9f] | 1487 | args[0] = "cursor"; |
---|
[85c3004] | 1488 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
[429a9b1] | 1489 | args[2] = "include_entities"; |
---|
| 1490 | args[3] = "true"; |
---|
[15c4cd4] | 1491 | args[4] = "tweet_mode"; |
---|
| 1492 | args[5] = "extended"; |
---|
[2322a9f] | 1493 | if (td->timeline_id) { |
---|
[15c4cd4] | 1494 | args[6] = "since_id"; |
---|
| 1495 | args[7] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
[2322a9f] | 1496 | } |
---|
| 1497 | |
---|
[90fc864] | 1498 | if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, |
---|
[15c4cd4] | 1499 | td->timeline_id ? 8 : 6) == NULL) { |
---|
[5ebff60] | 1500 | if (++td->http_fails >= 5) { |
---|
[90fc864] | 1501 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 1502 | TWITTER_HOME_TIMELINE_URL, "connection failed"); |
---|
[5ebff60] | 1503 | } |
---|
[90fc864] | 1504 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
| 1505 | twitter_flush_timeline(ic); |
---|
| 1506 | } |
---|
[2322a9f] | 1507 | |
---|
| 1508 | g_free(args[1]); |
---|
| 1509 | if (td->timeline_id) { |
---|
[15c4cd4] | 1510 | g_free(args[7]); |
---|
[2322a9f] | 1511 | } |
---|
| 1512 | } |
---|
| 1513 | |
---|
| 1514 | /** |
---|
| 1515 | * Get mentions. |
---|
| 1516 | */ |
---|
[ddc2de5] | 1517 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor) |
---|
[2322a9f] | 1518 | { |
---|
| 1519 | struct twitter_data *td = ic->proto_data; |
---|
| 1520 | |
---|
[199fea6] | 1521 | txl_free(td->mentions_obj); |
---|
[2322a9f] | 1522 | td->mentions_obj = NULL; |
---|
| 1523 | td->flags &= ~TWITTER_GOT_MENTIONS; |
---|
| 1524 | |
---|
[15c4cd4] | 1525 | char *args[8]; |
---|
[2322a9f] | 1526 | args[0] = "cursor"; |
---|
[85c3004] | 1527 | args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor); |
---|
[429a9b1] | 1528 | args[2] = "include_entities"; |
---|
| 1529 | args[3] = "true"; |
---|
[2322a9f] | 1530 | if (td->timeline_id) { |
---|
[429a9b1] | 1531 | args[4] = "since_id"; |
---|
[85c3004] | 1532 | args[5] = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
[b5fe39b] | 1533 | } else { |
---|
| 1534 | args[4] = "count"; |
---|
| 1535 | args[5] = g_strdup_printf("%d", set_getint(&ic->acc->set, "show_old_mentions")); |
---|
[2322a9f] | 1536 | } |
---|
[15c4cd4] | 1537 | args[6] = "tweet_mode"; |
---|
| 1538 | args[7] = "extended"; |
---|
[2322a9f] | 1539 | |
---|
[b5fe39b] | 1540 | if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions, |
---|
[15c4cd4] | 1541 | ic, 0, args, 8) == NULL) { |
---|
[5ebff60] | 1542 | if (++td->http_fails >= 5) { |
---|
[90fc864] | 1543 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 1544 | TWITTER_MENTIONS_URL, "connection failed"); |
---|
[5ebff60] | 1545 | } |
---|
[90fc864] | 1546 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
| 1547 | twitter_flush_timeline(ic); |
---|
| 1548 | } |
---|
[2322a9f] | 1549 | |
---|
| 1550 | g_free(args[1]); |
---|
[2fb1262] | 1551 | g_free(args[5]); |
---|
[2322a9f] | 1552 | } |
---|
| 1553 | |
---|
[1b221e0] | 1554 | /** |
---|
| 1555 | * Callback for getting the home timeline. |
---|
| 1556 | */ |
---|
| 1557 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
| 1558 | { |
---|
[62d2cfb] | 1559 | struct im_connection *ic = req->data; |
---|
[37aa317] | 1560 | struct twitter_data *td; |
---|
[8e3b7ac] | 1561 | json_value *parsed; |
---|
[1b221e0] | 1562 | struct twitter_xml_list *txl; |
---|
[62d2cfb] | 1563 | |
---|
| 1564 | // Check if the connection is still active. |
---|
[5ebff60] | 1565 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[62d2cfb] | 1566 | return; |
---|
[5ebff60] | 1567 | } |
---|
[5983eca] | 1568 | |
---|
[37aa317] | 1569 | td = ic->proto_data; |
---|
[1b221e0] | 1570 | |
---|
[2322a9f] | 1571 | // The root <statuses> node should hold the list of statuses <status> |
---|
[5ebff60] | 1572 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[2a6da96] | 1573 | goto end; |
---|
[5ebff60] | 1574 | } |
---|
[bbff22d] | 1575 | |
---|
| 1576 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 1577 | txl->list = NULL; |
---|
| 1578 | |
---|
[d0752e8] | 1579 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
[2fb1262] | 1580 | json_value_free(parsed); |
---|
[2322a9f] | 1581 | |
---|
| 1582 | td->home_timeline_obj = txl; |
---|
| 1583 | |
---|
[5ebff60] | 1584 | end: |
---|
| 1585 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[fb351ce] | 1586 | return; |
---|
[5ebff60] | 1587 | } |
---|
[fb351ce] | 1588 | |
---|
[2322a9f] | 1589 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
| 1590 | |
---|
| 1591 | twitter_flush_timeline(ic); |
---|
| 1592 | } |
---|
| 1593 | |
---|
| 1594 | /** |
---|
| 1595 | * Callback for getting mentions. |
---|
| 1596 | */ |
---|
| 1597 | static void twitter_http_get_mentions(struct http_request *req) |
---|
| 1598 | { |
---|
| 1599 | struct im_connection *ic = req->data; |
---|
| 1600 | struct twitter_data *td; |
---|
[8e3b7ac] | 1601 | json_value *parsed; |
---|
[2322a9f] | 1602 | struct twitter_xml_list *txl; |
---|
| 1603 | |
---|
| 1604 | // Check if the connection is still active. |
---|
[5ebff60] | 1605 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[1b221e0] | 1606 | return; |
---|
[5ebff60] | 1607 | } |
---|
[2322a9f] | 1608 | |
---|
| 1609 | td = ic->proto_data; |
---|
| 1610 | |
---|
[1b221e0] | 1611 | // The root <statuses> node should hold the list of statuses <status> |
---|
[5ebff60] | 1612 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[2a6da96] | 1613 | goto end; |
---|
[5ebff60] | 1614 | } |
---|
[bbff22d] | 1615 | |
---|
| 1616 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 1617 | txl->list = NULL; |
---|
| 1618 | |
---|
[d0752e8] | 1619 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
[2fb1262] | 1620 | json_value_free(parsed); |
---|
[1b221e0] | 1621 | |
---|
[2322a9f] | 1622 | td->mentions_obj = txl; |
---|
[1b221e0] | 1623 | |
---|
[5ebff60] | 1624 | end: |
---|
| 1625 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[fb351ce] | 1626 | return; |
---|
[5ebff60] | 1627 | } |
---|
[fb351ce] | 1628 | |
---|
[2322a9f] | 1629 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
| 1630 | |
---|
| 1631 | twitter_flush_timeline(ic); |
---|
[1b221e0] | 1632 | } |
---|
| 1633 | |
---|
| 1634 | /** |
---|
[de923d5] | 1635 | * Callback to use after sending a POST request to twitter. |
---|
| 1636 | * (Generic, used for a few kinds of queries.) |
---|
[1b221e0] | 1637 | */ |
---|
[7d53efb] | 1638 | static void twitter_http_post(struct http_request *req) |
---|
[1b221e0] | 1639 | { |
---|
| 1640 | struct im_connection *ic = req->data; |
---|
[7b87539] | 1641 | struct twitter_data *td; |
---|
[24132ec] | 1642 | json_value *parsed, *id; |
---|
[1b221e0] | 1643 | |
---|
[62d2cfb] | 1644 | // Check if the connection is still active. |
---|
[5ebff60] | 1645 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[62d2cfb] | 1646 | return; |
---|
[5ebff60] | 1647 | } |
---|
[62d2cfb] | 1648 | |
---|
[7b87539] | 1649 | td = ic->proto_data; |
---|
| 1650 | td->last_status_id = 0; |
---|
[5983eca] | 1651 | |
---|
[5ebff60] | 1652 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
[1b221e0] | 1653 | return; |
---|
[5ebff60] | 1654 | } |
---|
| 1655 | |
---|
[67f6828] | 1656 | if ((id = json_o_get(parsed, "id")) && id->type == json_integer) { |
---|
[24132ec] | 1657 | td->last_status_id = id->u.integer; |
---|
[67f6828] | 1658 | } |
---|
[5ebff60] | 1659 | |
---|
[c751e51] | 1660 | json_value_free(parsed); |
---|
[5ebff60] | 1661 | |
---|
| 1662 | if (req->flags & TWITTER_HTTP_USER_ACK) { |
---|
[b235228] | 1663 | twitter_log(ic, "Command processed successfully"); |
---|
[5ebff60] | 1664 | } |
---|
[1b221e0] | 1665 | } |
---|
| 1666 | |
---|
| 1667 | /** |
---|
| 1668 | * Function to POST a new status to twitter. |
---|
[5983eca] | 1669 | */ |
---|
[b890626] | 1670 | void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to) |
---|
[1b221e0] | 1671 | { |
---|
[5983eca] | 1672 | char *args[4] = { |
---|
[b890626] | 1673 | "status", msg, |
---|
| 1674 | "in_reply_to_status_id", |
---|
[85c3004] | 1675 | g_strdup_printf("%" G_GUINT64_FORMAT, in_reply_to) |
---|
[b890626] | 1676 | }; |
---|
[5ebff60] | 1677 | |
---|
[b890626] | 1678 | twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, |
---|
[5ebff60] | 1679 | args, in_reply_to ? 4 : 2); |
---|
[b890626] | 1680 | g_free(args[3]); |
---|
[1b221e0] | 1681 | } |
---|
| 1682 | |
---|
| 1683 | |
---|
[62d2cfb] | 1684 | /** |
---|
| 1685 | * Function to POST a new message to twitter. |
---|
| 1686 | */ |
---|
| 1687 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
| 1688 | { |
---|
[5983eca] | 1689 | char *args[4]; |
---|
[5ebff60] | 1690 | |
---|
[62d2cfb] | 1691 | args[0] = "screen_name"; |
---|
| 1692 | args[1] = who; |
---|
| 1693 | args[2] = "text"; |
---|
| 1694 | args[3] = msg; |
---|
| 1695 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
[ba3233c] | 1696 | twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4); |
---|
[62d2cfb] | 1697 | } |
---|
[7d53efb] | 1698 | |
---|
| 1699 | void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) |
---|
| 1700 | { |
---|
[5983eca] | 1701 | char *args[2]; |
---|
[5ebff60] | 1702 | |
---|
[7d53efb] | 1703 | args[0] = "screen_name"; |
---|
| 1704 | args[1] = who; |
---|
[5983eca] | 1705 | twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, |
---|
[5ebff60] | 1706 | twitter_http_post, ic, 1, args, 2); |
---|
[a26af5c] | 1707 | } |
---|
[7b87539] | 1708 | |
---|
[9cf63ac] | 1709 | /** |
---|
| 1710 | * Mute or unmute a user |
---|
| 1711 | */ |
---|
| 1712 | void twitter_mute_create_destroy(struct im_connection *ic, char *who, int create) |
---|
| 1713 | { |
---|
| 1714 | char *args[2]; |
---|
| 1715 | |
---|
| 1716 | args[0] = "screen_name"; |
---|
| 1717 | args[1] = who; |
---|
| 1718 | twitter_http(ic, create ? TWITTER_MUTES_CREATE_URL : TWITTER_MUTES_DESTROY_URL, |
---|
| 1719 | twitter_http_post, ic, 1, args, 2); |
---|
| 1720 | } |
---|
| 1721 | |
---|
[7b87539] | 1722 | void twitter_status_destroy(struct im_connection *ic, guint64 id) |
---|
| 1723 | { |
---|
| 1724 | char *url; |
---|
[5ebff60] | 1725 | |
---|
[85c3004] | 1726 | url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", |
---|
| 1727 | TWITTER_STATUS_DESTROY_URL, id, ".json"); |
---|
[b235228] | 1728 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
| 1729 | TWITTER_HTTP_USER_ACK); |
---|
[7b87539] | 1730 | g_free(url); |
---|
| 1731 | } |
---|
[b890626] | 1732 | |
---|
| 1733 | void twitter_status_retweet(struct im_connection *ic, guint64 id) |
---|
| 1734 | { |
---|
| 1735 | char *url; |
---|
[5ebff60] | 1736 | |
---|
[85c3004] | 1737 | url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", |
---|
| 1738 | TWITTER_STATUS_RETWEET_URL, id, ".json"); |
---|
[b235228] | 1739 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
| 1740 | TWITTER_HTTP_USER_ACK); |
---|
[b890626] | 1741 | g_free(url); |
---|
| 1742 | } |
---|
[d18dee42] | 1743 | |
---|
| 1744 | /** |
---|
| 1745 | * Report a user for sending spam. |
---|
| 1746 | */ |
---|
| 1747 | void twitter_report_spam(struct im_connection *ic, char *screen_name) |
---|
| 1748 | { |
---|
| 1749 | char *args[2] = { |
---|
| 1750 | "screen_name", |
---|
| 1751 | NULL, |
---|
| 1752 | }; |
---|
[5ebff60] | 1753 | |
---|
[d18dee42] | 1754 | args[1] = screen_name; |
---|
[b235228] | 1755 | twitter_http_f(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post, |
---|
| 1756 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
[d18dee42] | 1757 | } |
---|
[b61c74c] | 1758 | |
---|
| 1759 | /** |
---|
| 1760 | * Favourite a tweet. |
---|
| 1761 | */ |
---|
| 1762 | void twitter_favourite_tweet(struct im_connection *ic, guint64 id) |
---|
| 1763 | { |
---|
[75bda8b] | 1764 | char *args[2] = { |
---|
| 1765 | "id", |
---|
| 1766 | NULL, |
---|
| 1767 | }; |
---|
[5ebff60] | 1768 | |
---|
[85c3004] | 1769 | args[1] = g_strdup_printf("%" G_GUINT64_FORMAT, id); |
---|
[75bda8b] | 1770 | twitter_http_f(ic, TWITTER_FAVORITE_CREATE_URL, twitter_http_post, |
---|
| 1771 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
| 1772 | g_free(args[1]); |
---|
[b61c74c] | 1773 | } |
---|
[1201fcb] | 1774 | |
---|
| 1775 | static void twitter_http_status_show_url(struct http_request *req) |
---|
| 1776 | { |
---|
| 1777 | struct im_connection *ic = req->data; |
---|
| 1778 | json_value *parsed, *id; |
---|
| 1779 | const char *name; |
---|
| 1780 | |
---|
| 1781 | // Check if the connection is still active. |
---|
| 1782 | if (!g_slist_find(twitter_connections, ic)) { |
---|
| 1783 | return; |
---|
| 1784 | } |
---|
| 1785 | |
---|
| 1786 | if (!(parsed = twitter_parse_response(ic, req))) { |
---|
| 1787 | return; |
---|
| 1788 | } |
---|
| 1789 | |
---|
| 1790 | /* for the parson branch: |
---|
| 1791 | name = json_object_dotget_string(json_object(parsed), "user.screen_name"); |
---|
| 1792 | id = json_object_get_integer(json_object(parsed), "id"); |
---|
| 1793 | */ |
---|
| 1794 | |
---|
| 1795 | name = json_o_str(json_o_get(parsed, "user"), "screen_name"); |
---|
| 1796 | id = json_o_get(parsed, "id"); |
---|
| 1797 | |
---|
| 1798 | if (name && id && id->type == json_integer) { |
---|
| 1799 | twitter_log(ic, "https://twitter.com/%s/status/%" G_GUINT64_FORMAT, name, id->u.integer); |
---|
| 1800 | } else { |
---|
| 1801 | twitter_log(ic, "Error: could not fetch tweet url."); |
---|
| 1802 | } |
---|
| 1803 | |
---|
| 1804 | json_value_free(parsed); |
---|
| 1805 | } |
---|
| 1806 | |
---|
| 1807 | void twitter_status_show_url(struct im_connection *ic, guint64 id) |
---|
| 1808 | { |
---|
| 1809 | char *url = g_strdup_printf("%s%" G_GUINT64_FORMAT "%s", TWITTER_STATUS_SHOW_URL, id, ".json"); |
---|
| 1810 | twitter_http(ic, url, twitter_http_status_show_url, ic, 0, NULL, 0); |
---|
| 1811 | g_free(url); |
---|
| 1812 | } |
---|