[1b221e0] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple module to facilitate twitter functionality. * |
---|
| 5 | * * |
---|
[96dd574] | 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 | |
---|
| 25 | #include "nogaim.h" |
---|
[713d611] | 26 | #include "oauth.h" |
---|
[1b221e0] | 27 | #include "twitter.h" |
---|
| 28 | #include "twitter_http.h" |
---|
| 29 | #include "twitter_lib.h" |
---|
[bb5ce4d1] | 30 | #include "url.h" |
---|
[1b221e0] | 31 | |
---|
[9c9a29c] | 32 | GSList *twitter_connections = NULL; |
---|
[73ee390] | 33 | |
---|
| 34 | static int twitter_filter_cmp(struct twitter_filter *tf1, |
---|
| 35 | struct twitter_filter *tf2) |
---|
| 36 | { |
---|
| 37 | int i1 = 0; |
---|
| 38 | int i2 = 0; |
---|
| 39 | int i; |
---|
| 40 | |
---|
| 41 | static const twitter_filter_type_t types[] = { |
---|
| 42 | /* Order of the types */ |
---|
| 43 | TWITTER_FILTER_TYPE_FOLLOW, |
---|
| 44 | TWITTER_FILTER_TYPE_TRACK |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | for (i = 0; i < G_N_ELEMENTS(types); i++) { |
---|
| 48 | if (types[i] == tf1->type) { |
---|
| 49 | i1 = i + 1; |
---|
| 50 | break; |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | for (i = 0; i < G_N_ELEMENTS(types); i++) { |
---|
| 55 | if (types[i] == tf2->type) { |
---|
| 56 | i2 = i + 1; |
---|
| 57 | break; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | if (i1 != i2) { |
---|
| 62 | /* With different types, return their difference */ |
---|
| 63 | return i1 - i2; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /* With the same type, return the text comparison */ |
---|
| 67 | return g_strcasecmp(tf1->text, tf2->text); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | static gboolean twitter_filter_update(gpointer data, gint fd, |
---|
| 71 | b_input_condition cond) |
---|
| 72 | { |
---|
| 73 | struct im_connection *ic = data; |
---|
| 74 | struct twitter_data *td = ic->proto_data; |
---|
| 75 | |
---|
| 76 | if (td->filters) { |
---|
| 77 | twitter_open_filter_stream(ic); |
---|
| 78 | } else if (td->filter_stream) { |
---|
| 79 | http_close(td->filter_stream); |
---|
| 80 | td->filter_stream = NULL; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | td->filter_update_id = 0; |
---|
| 84 | return FALSE; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | static struct twitter_filter *twitter_filter_get(struct groupchat *c, |
---|
| 88 | twitter_filter_type_t type, |
---|
| 89 | const char *text) |
---|
| 90 | { |
---|
| 91 | struct twitter_data *td = c->ic->proto_data; |
---|
| 92 | struct twitter_filter *tf = NULL; |
---|
[5ebff60] | 93 | struct twitter_filter tfc = { type, (char *) text }; |
---|
[73ee390] | 94 | GSList *l; |
---|
| 95 | |
---|
| 96 | for (l = td->filters; l; l = g_slist_next(l)) { |
---|
| 97 | tf = l->data; |
---|
| 98 | |
---|
[5ebff60] | 99 | if (twitter_filter_cmp(tf, &tfc) == 0) { |
---|
[73ee390] | 100 | break; |
---|
[5ebff60] | 101 | } |
---|
[73ee390] | 102 | |
---|
| 103 | tf = NULL; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | if (!tf) { |
---|
| 107 | tf = g_new0(struct twitter_filter, 1); |
---|
| 108 | tf->type = type; |
---|
| 109 | tf->text = g_strdup(text); |
---|
| 110 | td->filters = g_slist_prepend(td->filters, tf); |
---|
| 111 | } |
---|
| 112 | |
---|
[5ebff60] | 113 | if (!g_slist_find(tf->groupchats, c)) { |
---|
[73ee390] | 114 | tf->groupchats = g_slist_prepend(tf->groupchats, c); |
---|
[5ebff60] | 115 | } |
---|
[73ee390] | 116 | |
---|
[5ebff60] | 117 | if (td->filter_update_id > 0) { |
---|
[73ee390] | 118 | b_event_remove(td->filter_update_id); |
---|
[5ebff60] | 119 | } |
---|
[73ee390] | 120 | |
---|
| 121 | /* Wait for other possible filter changes to avoid request spam */ |
---|
| 122 | td->filter_update_id = b_timeout_add(TWITTER_FILTER_UPDATE_WAIT, |
---|
[5ebff60] | 123 | twitter_filter_update, c->ic); |
---|
[73ee390] | 124 | return tf; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | static void twitter_filter_free(struct twitter_filter *tf) |
---|
| 128 | { |
---|
| 129 | g_slist_free(tf->groupchats); |
---|
| 130 | g_free(tf->text); |
---|
| 131 | g_free(tf); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | static void twitter_filter_remove(struct groupchat *c) |
---|
| 135 | { |
---|
| 136 | struct twitter_data *td = c->ic->proto_data; |
---|
| 137 | struct twitter_filter *tf; |
---|
| 138 | GSList *l = td->filters; |
---|
| 139 | GSList *p; |
---|
| 140 | |
---|
| 141 | while (l != NULL) { |
---|
| 142 | tf = l->data; |
---|
| 143 | tf->groupchats = g_slist_remove(tf->groupchats, c); |
---|
| 144 | |
---|
| 145 | p = l; |
---|
| 146 | l = g_slist_next(l); |
---|
| 147 | |
---|
| 148 | if (!tf->groupchats) { |
---|
| 149 | twitter_filter_free(tf); |
---|
| 150 | td->filters = g_slist_delete_link(td->filters, p); |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
[5ebff60] | 154 | if (td->filter_update_id > 0) { |
---|
[73ee390] | 155 | b_event_remove(td->filter_update_id); |
---|
[5ebff60] | 156 | } |
---|
[73ee390] | 157 | |
---|
| 158 | /* Wait for other possible filter changes to avoid request spam */ |
---|
| 159 | td->filter_update_id = b_timeout_add(TWITTER_FILTER_UPDATE_WAIT, |
---|
[5ebff60] | 160 | twitter_filter_update, c->ic); |
---|
| 161 | } |
---|
[73ee390] | 162 | |
---|
| 163 | static void twitter_filter_remove_all(struct im_connection *ic) |
---|
| 164 | { |
---|
| 165 | struct twitter_data *td = ic->proto_data; |
---|
| 166 | GSList *chats = NULL; |
---|
| 167 | struct twitter_filter *tf; |
---|
| 168 | GSList *l = td->filters; |
---|
| 169 | GSList *p; |
---|
| 170 | |
---|
| 171 | while (l != NULL) { |
---|
| 172 | tf = l->data; |
---|
| 173 | |
---|
| 174 | /* Build up a list of groupchats to be freed */ |
---|
| 175 | for (p = tf->groupchats; p; p = g_slist_next(p)) { |
---|
[5ebff60] | 176 | if (!g_slist_find(chats, p->data)) { |
---|
[73ee390] | 177 | chats = g_slist_prepend(chats, p->data); |
---|
[5ebff60] | 178 | } |
---|
[73ee390] | 179 | } |
---|
| 180 | |
---|
| 181 | p = l; |
---|
| 182 | l = g_slist_next(l); |
---|
| 183 | twitter_filter_free(p->data); |
---|
| 184 | td->filters = g_slist_delete_link(td->filters, p); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | l = chats; |
---|
| 188 | |
---|
| 189 | while (l != NULL) { |
---|
| 190 | p = l; |
---|
| 191 | l = g_slist_next(l); |
---|
| 192 | |
---|
| 193 | /* Freed each remaining groupchat */ |
---|
| 194 | imcb_chat_free(p->data); |
---|
| 195 | chats = g_slist_delete_link(chats, p); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | if (td->filter_stream) { |
---|
| 199 | http_close(td->filter_stream); |
---|
| 200 | td->filter_stream = NULL; |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | static GSList *twitter_filter_parse(struct groupchat *c, const char *text) |
---|
| 205 | { |
---|
| 206 | char **fs = g_strsplit(text, ";", 0); |
---|
| 207 | GSList *ret = NULL; |
---|
| 208 | struct twitter_filter *tf; |
---|
| 209 | char **f; |
---|
| 210 | char *v; |
---|
| 211 | int i; |
---|
| 212 | int t; |
---|
| 213 | |
---|
| 214 | static const twitter_filter_type_t types[] = { |
---|
| 215 | TWITTER_FILTER_TYPE_FOLLOW, |
---|
| 216 | TWITTER_FILTER_TYPE_TRACK |
---|
| 217 | }; |
---|
| 218 | |
---|
| 219 | static const char *typestrs[] = { |
---|
| 220 | "follow", |
---|
| 221 | "track" |
---|
| 222 | }; |
---|
| 223 | |
---|
| 224 | for (f = fs; *f; f++) { |
---|
[5ebff60] | 225 | if ((v = strchr(*f, ':')) == NULL) { |
---|
[73ee390] | 226 | continue; |
---|
[5ebff60] | 227 | } |
---|
[73ee390] | 228 | |
---|
| 229 | *(v++) = 0; |
---|
| 230 | |
---|
| 231 | for (t = -1, i = 0; i < G_N_ELEMENTS(types); i++) { |
---|
| 232 | if (g_strcasecmp(typestrs[i], *f) == 0) { |
---|
| 233 | t = i; |
---|
| 234 | break; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
[5ebff60] | 238 | if (t < 0 || strlen(v) == 0) { |
---|
[73ee390] | 239 | continue; |
---|
[5ebff60] | 240 | } |
---|
[73ee390] | 241 | |
---|
| 242 | tf = twitter_filter_get(c, types[t], v); |
---|
| 243 | ret = g_slist_prepend(ret, tf); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | g_strfreev(fs); |
---|
| 247 | return ret; |
---|
| 248 | } |
---|
| 249 | |
---|
[1b221e0] | 250 | /** |
---|
[62d2cfb] | 251 | * Main loop function |
---|
| 252 | */ |
---|
[1b221e0] | 253 | gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond) |
---|
| 254 | { |
---|
| 255 | struct im_connection *ic = data; |
---|
[5983eca] | 256 | |
---|
[1b221e0] | 257 | // Check if we are still logged in... |
---|
[5ebff60] | 258 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[2f9027c] | 259 | return FALSE; |
---|
[5ebff60] | 260 | } |
---|
[1b221e0] | 261 | |
---|
| 262 | // Do stuff.. |
---|
[2f9027c] | 263 | return twitter_get_timeline(ic, -1) && |
---|
| 264 | ((ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN); |
---|
[1b221e0] | 265 | } |
---|
| 266 | |
---|
[5983eca] | 267 | static void twitter_main_loop_start(struct im_connection *ic) |
---|
[713d611] | 268 | { |
---|
| 269 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 270 | |
---|
[eb4ad8d] | 271 | char *last_tweet = set_getstr(&ic->acc->set, "_last_tweet"); |
---|
[5ebff60] | 272 | |
---|
| 273 | if (last_tweet) { |
---|
[7549d00] | 274 | td->timeline_id = g_ascii_strtoull(last_tweet, NULL, 0); |
---|
[5ebff60] | 275 | } |
---|
[7549d00] | 276 | |
---|
[631ec80] | 277 | /* Create the room now that we "logged in". */ |
---|
[5ebff60] | 278 | if (td->flags & TWITTER_MODE_CHAT) { |
---|
[631ec80] | 279 | twitter_groupchat_init(ic); |
---|
[5ebff60] | 280 | } |
---|
[631ec80] | 281 | |
---|
[5983eca] | 282 | imcb_log(ic, "Getting initial statuses"); |
---|
[713d611] | 283 | |
---|
[f26d9a3e] | 284 | // Run this once. After this queue the main loop function (or open the |
---|
| 285 | // stream if available). |
---|
[713d611] | 286 | twitter_main_loop(ic, -1, 0); |
---|
[5ebff60] | 287 | |
---|
[dff0e0b] | 288 | if (set_getbool(&ic->acc->set, "stream")) { |
---|
| 289 | /* That fetch was just to get backlog, the stream will give |
---|
| 290 | us the rest. \o/ */ |
---|
| 291 | twitter_open_stream(ic); |
---|
[5ebff60] | 292 | |
---|
[f26d9a3e] | 293 | /* Stream sends keepalives (empty lines) or actual data at |
---|
| 294 | least twice a minute. Disconnect if this stops. */ |
---|
[e132b60] | 295 | ic->flags |= OPT_PONGS; |
---|
[dff0e0b] | 296 | } else { |
---|
| 297 | /* Not using the streaming API, so keep polling the old- |
---|
| 298 | fashioned way. :-( */ |
---|
| 299 | td->main_loop_id = |
---|
[5ebff60] | 300 | b_timeout_add(set_getint(&ic->acc->set, "fetch_interval") * 1000, |
---|
| 301 | twitter_main_loop, ic); |
---|
[dff0e0b] | 302 | } |
---|
[713d611] | 303 | } |
---|
| 304 | |
---|
[631ec80] | 305 | struct groupchat *twitter_groupchat_init(struct im_connection *ic) |
---|
| 306 | { |
---|
| 307 | char *name_hint; |
---|
| 308 | struct groupchat *gc; |
---|
| 309 | struct twitter_data *td = ic->proto_data; |
---|
| 310 | GSList *l; |
---|
| 311 | |
---|
[5ebff60] | 312 | if (td->timeline_gc) { |
---|
[631ec80] | 313 | return td->timeline_gc; |
---|
[5ebff60] | 314 | } |
---|
[631ec80] | 315 | |
---|
| 316 | td->timeline_gc = gc = imcb_chat_new(ic, "twitter/timeline"); |
---|
| 317 | |
---|
| 318 | name_hint = g_strdup_printf("%s_%s", td->prefix, ic->acc->user); |
---|
| 319 | imcb_chat_name_hint(gc, name_hint); |
---|
| 320 | g_free(name_hint); |
---|
| 321 | |
---|
| 322 | for (l = ic->bee->users; l; l = l->next) { |
---|
| 323 | bee_user_t *bu = l->data; |
---|
[5ebff60] | 324 | if (bu->ic == ic) { |
---|
[29f72b7] | 325 | imcb_chat_add_buddy(gc, bu->handle); |
---|
[5ebff60] | 326 | } |
---|
[631ec80] | 327 | } |
---|
| 328 | imcb_chat_add_buddy(gc, ic->acc->user); |
---|
[5ebff60] | 329 | |
---|
[631ec80] | 330 | return gc; |
---|
| 331 | } |
---|
| 332 | |
---|
[5983eca] | 333 | static void twitter_oauth_start(struct im_connection *ic); |
---|
[d6aa6dd] | 334 | |
---|
[5983eca] | 335 | void twitter_login_finish(struct im_connection *ic) |
---|
[d6aa6dd] | 336 | { |
---|
| 337 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 338 | |
---|
[2322a9f] | 339 | td->flags &= ~TWITTER_DOING_TIMELINE; |
---|
| 340 | |
---|
[5ebff60] | 341 | if (set_getbool(&ic->acc->set, "oauth") && !td->oauth_info) { |
---|
[5983eca] | 342 | twitter_oauth_start(ic); |
---|
[5ebff60] | 343 | } else if (!(td->flags & TWITTER_MODE_ONE) && |
---|
| 344 | !(td->flags & TWITTER_HAVE_FRIENDS)) { |
---|
[5983eca] | 345 | imcb_log(ic, "Getting contact list"); |
---|
[de923d5] | 346 | twitter_get_friends_ids(ic, -1); |
---|
[fb62f132] | 347 | twitter_get_mutes_ids(ic, -1); |
---|
| 348 | twitter_get_noretweets_ids(ic, -1); |
---|
[5ebff60] | 349 | } else { |
---|
[5983eca] | 350 | twitter_main_loop_start(ic); |
---|
[5ebff60] | 351 | } |
---|
[d6aa6dd] | 352 | } |
---|
[c2ecadc] | 353 | |
---|
[5983eca] | 354 | static const struct oauth_service twitter_oauth = { |
---|
[3f808ca] | 355 | "https://api.twitter.com/oauth/request_token", |
---|
| 356 | "https://api.twitter.com/oauth/access_token", |
---|
[c01bbd1] | 357 | "https://api.twitter.com/oauth/authorize", |
---|
[cad74fb] | 358 | .consumer_key = "ItChnf5KelJE5DOWVhz9ZuwYb", |
---|
| 359 | .consumer_secret = "yoWJ16DRmJlILoB9Tf5T5lIUUPnIarqoO0YUWkYUOD7ttR0J6T", |
---|
[c2ecadc] | 360 | }; |
---|
| 361 | |
---|
[5983eca] | 362 | static gboolean twitter_oauth_callback(struct oauth_info *info); |
---|
[713d611] | 363 | |
---|
[5983eca] | 364 | static const struct oauth_service *get_oauth_service(struct im_connection *ic) |
---|
[ce617f0] | 365 | { |
---|
[686ac51] | 366 | return &twitter_oauth; |
---|
[5983eca] | 367 | |
---|
[ce617f0] | 368 | /* Could add more services, or allow configuring your own base URL + |
---|
| 369 | API keys. */ |
---|
| 370 | } |
---|
| 371 | |
---|
[5983eca] | 372 | static void twitter_oauth_start(struct im_connection *ic) |
---|
[713d611] | 373 | { |
---|
[18dbb20] | 374 | struct twitter_data *td = ic->proto_data; |
---|
[f2d8aa2] | 375 | const char *url = set_getstr(&ic->acc->set, "base_url"); |
---|
[c42e8b9] | 376 | |
---|
[5983eca] | 377 | imcb_log(ic, "Requesting OAuth request token"); |
---|
[5ebff60] | 378 | |
---|
[686ac51] | 379 | if (!strstr(url, "twitter.com")) { |
---|
| 380 | imcb_log(ic, "Warning: OAuth only works with Twitter."); |
---|
[5ebff60] | 381 | } |
---|
[5983eca] | 382 | |
---|
| 383 | td->oauth_info = oauth_request_token(get_oauth_service(ic), twitter_oauth_callback, ic); |
---|
| 384 | |
---|
[748bcdd] | 385 | /* We need help from the user to complete OAuth login, so don't time |
---|
| 386 | out on this login. */ |
---|
| 387 | ic->flags |= OPT_SLOW_LOGIN; |
---|
[713d611] | 388 | } |
---|
| 389 | |
---|
[5983eca] | 390 | static gboolean twitter_oauth_callback(struct oauth_info *info) |
---|
[713d611] | 391 | { |
---|
| 392 | struct im_connection *ic = info->data; |
---|
[18dbb20] | 393 | struct twitter_data *td; |
---|
[5983eca] | 394 | |
---|
[5ebff60] | 395 | if (!g_slist_find(twitter_connections, ic)) { |
---|
[18dbb20] | 396 | return FALSE; |
---|
[5ebff60] | 397 | } |
---|
[5983eca] | 398 | |
---|
[18dbb20] | 399 | td = ic->proto_data; |
---|
[5983eca] | 400 | if (info->stage == OAUTH_REQUEST_TOKEN) { |
---|
[f2d8aa2] | 401 | char *name, *msg; |
---|
[5983eca] | 402 | |
---|
| 403 | if (info->request_token == NULL) { |
---|
| 404 | imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); |
---|
| 405 | imc_logout(ic, TRUE); |
---|
[18dbb20] | 406 | return FALSE; |
---|
[c42e8b9] | 407 | } |
---|
[5983eca] | 408 | |
---|
[f2d8aa2] | 409 | name = g_strdup_printf("%s_%s", td->prefix, ic->acc->user); |
---|
[5983eca] | 410 | msg = g_strdup_printf("To finish OAuth authentication, please visit " |
---|
[5ebff60] | 411 | "%s and respond with the resulting PIN code.", |
---|
| 412 | info->auth_url); |
---|
[5983eca] | 413 | imcb_buddy_msg(ic, name, msg, 0, 0); |
---|
[f2d8aa2] | 414 | g_free(name); |
---|
[5983eca] | 415 | g_free(msg); |
---|
| 416 | } else if (info->stage == OAUTH_ACCESS_TOKEN) { |
---|
[cfbecc9] | 417 | const char *sn; |
---|
[5ebff60] | 418 | |
---|
[5983eca] | 419 | if (info->token == NULL || info->token_secret == NULL) { |
---|
| 420 | imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); |
---|
| 421 | imc_logout(ic, TRUE); |
---|
[18dbb20] | 422 | return FALSE; |
---|
[cfbecc9] | 423 | } |
---|
[5ebff60] | 424 | |
---|
[cfbecc9] | 425 | if ((sn = oauth_params_get(&info->params, "screen_name"))) { |
---|
[5ebff60] | 426 | if (ic->acc->prpl->handle_cmp(sn, ic->acc->user) != 0) { |
---|
[5983eca] | 427 | imcb_log(ic, "Warning: You logged in via OAuth as %s " |
---|
[cfbecc9] | 428 | "instead of %s.", sn, ic->acc->user); |
---|
[5ebff60] | 429 | } |
---|
[de923d5] | 430 | g_free(td->user); |
---|
| 431 | td->user = g_strdup(sn); |
---|
[93cc86f] | 432 | } |
---|
[5983eca] | 433 | |
---|
[288b215] | 434 | /* IM mods didn't do this so far and it's ugly but I should |
---|
| 435 | be able to get away with it... */ |
---|
[5983eca] | 436 | g_free(ic->acc->pass); |
---|
| 437 | ic->acc->pass = oauth_to_string(info); |
---|
| 438 | |
---|
| 439 | twitter_login_finish(ic); |
---|
[c42e8b9] | 440 | } |
---|
[5983eca] | 441 | |
---|
[18dbb20] | 442 | return TRUE; |
---|
[713d611] | 443 | } |
---|
| 444 | |
---|
[7d27962] | 445 | int twitter_url_len_diff(gchar *msg, unsigned int target_len) |
---|
| 446 | { |
---|
| 447 | int url_len_diff = 0; |
---|
| 448 | |
---|
| 449 | static GRegex *regex = NULL; |
---|
| 450 | GMatchInfo *match_info; |
---|
| 451 | |
---|
[5ebff60] | 452 | if (regex == NULL) { |
---|
[7d27962] | 453 | regex = g_regex_new("(^|\\s)(http(s)?://[^\\s$]+)", 0, 0, NULL); |
---|
[5ebff60] | 454 | } |
---|
| 455 | |
---|
[7d27962] | 456 | g_regex_match(regex, msg, 0, &match_info); |
---|
| 457 | while (g_match_info_matches(match_info)) { |
---|
[0843bbe] | 458 | gchar *url; |
---|
[61e7e02] | 459 | |
---|
| 460 | url = g_match_info_fetch(match_info, 2); |
---|
[7d27962] | 461 | url_len_diff += target_len - g_utf8_strlen(url, -1); |
---|
[61e7e02] | 462 | |
---|
[7d27962] | 463 | g_free(url); |
---|
| 464 | g_match_info_next(match_info, NULL); |
---|
| 465 | } |
---|
| 466 | g_match_info_free(match_info); |
---|
| 467 | |
---|
| 468 | return url_len_diff; |
---|
| 469 | } |
---|
| 470 | |
---|
[33528bd] | 471 | int twitter_message_len(gchar *msg, int target_len) |
---|
[9997691] | 472 | { |
---|
[7d27962] | 473 | int url_len_diff = 0; |
---|
[a685409] | 474 | |
---|
[5ebff60] | 475 | if (target_len > 0) { |
---|
[7d27962] | 476 | url_len_diff = twitter_url_len_diff(msg, target_len); |
---|
[5ebff60] | 477 | } |
---|
[5983eca] | 478 | |
---|
[33528bd] | 479 | return g_utf8_strlen(msg, -1) + url_len_diff; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | static gboolean twitter_length_check(struct im_connection *ic, gchar * msg) |
---|
| 483 | { |
---|
| 484 | int max = set_getint(&ic->acc->set, "message_length"); |
---|
| 485 | int target_len = set_getint(&ic->acc->set, "target_url_length"); |
---|
| 486 | int len = twitter_message_len(msg, target_len); |
---|
| 487 | |
---|
| 488 | if (max == 0 || len <= max) { |
---|
[9997691] | 489 | return TRUE; |
---|
[5ebff60] | 490 | } |
---|
[5983eca] | 491 | |
---|
[96dd574] | 492 | twitter_log(ic, "Maximum message length exceeded: %d > %d", len, max); |
---|
[5983eca] | 493 | |
---|
[9997691] | 494 | return FALSE; |
---|
| 495 | } |
---|
| 496 | |
---|
[3592b95] | 497 | static char *set_eval_commands(set_t * set, char *value) |
---|
| 498 | { |
---|
[5ebff60] | 499 | if (g_strcasecmp(value, "strict") == 0) { |
---|
[3592b95] | 500 | return value; |
---|
[5ebff60] | 501 | } else { |
---|
[3592b95] | 502 | return set_eval_bool(set, value); |
---|
[5ebff60] | 503 | } |
---|
[3592b95] | 504 | } |
---|
| 505 | |
---|
| 506 | static char *set_eval_mode(set_t * set, char *value) |
---|
| 507 | { |
---|
| 508 | if (g_strcasecmp(value, "one") == 0 || |
---|
[5ebff60] | 509 | g_strcasecmp(value, "many") == 0 || g_strcasecmp(value, "chat") == 0) { |
---|
[3592b95] | 510 | return value; |
---|
[5ebff60] | 511 | } else { |
---|
[3592b95] | 512 | return NULL; |
---|
[5ebff60] | 513 | } |
---|
[3592b95] | 514 | } |
---|
| 515 | |
---|
[5983eca] | 516 | static void twitter_init(account_t * acc) |
---|
[1b221e0] | 517 | { |
---|
[62d2cfb] | 518 | set_t *s; |
---|
[ffcdf13] | 519 | char *def_url; |
---|
[7d27962] | 520 | char *def_tul; |
---|
[777461b] | 521 | char *def_mentions; |
---|
[5983eca] | 522 | |
---|
| 523 | if (strcmp(acc->prpl->name, "twitter") == 0) { |
---|
[ffcdf13] | 524 | def_url = TWITTER_API_URL; |
---|
[9456255] | 525 | def_tul = "23"; |
---|
[777461b] | 526 | def_mentions = "true"; |
---|
[686ac51] | 527 | } else { |
---|
| 528 | def_url = ""; |
---|
[7d27962] | 529 | def_tul = "0"; |
---|
[777461b] | 530 | def_mentions = "false"; |
---|
[ffcdf13] | 531 | } |
---|
[5983eca] | 532 | |
---|
| 533 | s = set_add(&acc->set, "auto_reply_timeout", "10800", set_eval_int, acc); |
---|
| 534 | |
---|
| 535 | s = set_add(&acc->set, "base_url", def_url, NULL, acc); |
---|
[bb5ce4d1] | 536 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 537 | |
---|
[3592b95] | 538 | s = set_add(&acc->set, "commands", "true", set_eval_commands, acc); |
---|
[5983eca] | 539 | |
---|
[2322a9f] | 540 | s = set_add(&acc->set, "fetch_interval", "60", set_eval_int, acc); |
---|
| 541 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 542 | |
---|
[777461b] | 543 | s = set_add(&acc->set, "fetch_mentions", def_mentions, set_eval_bool, acc); |
---|
[2322a9f] | 544 | |
---|
[d168091] | 545 | s = set_add(&acc->set, "message_length", "280", set_eval_int, acc); |
---|
[5983eca] | 546 | |
---|
[7d27962] | 547 | s = set_add(&acc->set, "target_url_length", def_tul, set_eval_int, acc); |
---|
| 548 | |
---|
[5983eca] | 549 | s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc); |
---|
[2abceca] | 550 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 551 | |
---|
[b3d99e3] | 552 | s = set_add(&acc->set, "oauth", "true", set_eval_oauth, acc); |
---|
[ce199b7] | 553 | |
---|
[e93fa05] | 554 | s = set_add(&acc->set, "show_ids", "true", set_eval_bool, acc); |
---|
[5983eca] | 555 | |
---|
[f75b3a7] | 556 | s = set_add(&acc->set, "show_old_mentions", "0", set_eval_int, acc); |
---|
[948abab] | 557 | |
---|
| 558 | s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc); |
---|
[5ebff60] | 559 | |
---|
[eb4ad8d] | 560 | s = set_add(&acc->set, "_last_tweet", "0", NULL, acc); |
---|
| 561 | s->flags |= SET_HIDDEN | SET_NOSAVE; |
---|
[7549d00] | 562 | |
---|
[952e264] | 563 | s = set_add(&acc->set, "in_korea", "false", set_eval_bool, acc); |
---|
| 564 | s->flags |= SET_HIDDEN; |
---|
| 565 | |
---|
[f26d9a3e] | 566 | if (strcmp(acc->prpl->name, "twitter") == 0) { |
---|
[0b1448f] | 567 | s = set_add(&acc->set, "stream", "false", set_eval_bool, acc); |
---|
[f26d9a3e] | 568 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 569 | } |
---|
[1b221e0] | 570 | } |
---|
| 571 | |
---|
| 572 | /** |
---|
[f26d9a3e] | 573 | * Login method. Since the twitter API works with separate HTTP request we |
---|
[1b221e0] | 574 | * only save the user and pass to the twitter_data object. |
---|
| 575 | */ |
---|
[5983eca] | 576 | static void twitter_login(account_t * acc) |
---|
[1b221e0] | 577 | { |
---|
[5983eca] | 578 | struct im_connection *ic = imcb_new(acc); |
---|
[bb5ce4d1] | 579 | struct twitter_data *td; |
---|
[5983eca] | 580 | char name[strlen(acc->user) + 9]; |
---|
[bb5ce4d1] | 581 | url_t url; |
---|
[c0f33f1] | 582 | char *s; |
---|
[5ebff60] | 583 | |
---|
[5983eca] | 584 | if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) || |
---|
| 585 | (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) { |
---|
| 586 | imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url")); |
---|
| 587 | imc_logout(ic, FALSE); |
---|
[bb5ce4d1] | 588 | return; |
---|
| 589 | } |
---|
[5983eca] | 590 | |
---|
[f26d9a3e] | 591 | if (!strstr(url.host, "twitter.com") && |
---|
| 592 | set_getbool(&ic->acc->set, "stream")) { |
---|
| 593 | imcb_error(ic, "Warning: The streaming API is only supported by Twitter, " |
---|
[5ebff60] | 594 | "and you seem to be connecting to a different service."); |
---|
[f26d9a3e] | 595 | } |
---|
| 596 | |
---|
[c0f33f1] | 597 | imcb_log(ic, "Connecting"); |
---|
| 598 | |
---|
[5983eca] | 599 | twitter_connections = g_slist_append(twitter_connections, ic); |
---|
| 600 | td = g_new0(struct twitter_data, 1); |
---|
[713d611] | 601 | ic->proto_data = td; |
---|
[de923d5] | 602 | td->user = g_strdup(acc->user); |
---|
[5983eca] | 603 | |
---|
[bb5ce4d1] | 604 | td->url_ssl = url.proto == PROTO_HTTPS; |
---|
| 605 | td->url_port = url.port; |
---|
[5983eca] | 606 | td->url_host = g_strdup(url.host); |
---|
[5ebff60] | 607 | if (strcmp(url.file, "/") != 0) { |
---|
[5983eca] | 608 | td->url_path = g_strdup(url.file); |
---|
[5ebff60] | 609 | } else { |
---|
[5983eca] | 610 | td->url_path = g_strdup(""); |
---|
[5ebff60] | 611 | if (g_str_has_suffix(url.host, "twitter.com")) { |
---|
[c0f33f1] | 612 | /* May fire for people who turned on HTTPS. */ |
---|
| 613 | imcb_error(ic, "Warning: Twitter requires a version number in API calls " |
---|
[5ebff60] | 614 | "now. Try resetting the base_url account setting."); |
---|
| 615 | } |
---|
[c0f33f1] | 616 | } |
---|
[5ebff60] | 617 | |
---|
[c0f33f1] | 618 | /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com |
---|
| 619 | into twitter, and try to be sensible if we get anything else. */ |
---|
| 620 | td->prefix = g_strdup(url.host); |
---|
[5ebff60] | 621 | if (g_str_has_suffix(td->prefix, ".com")) { |
---|
[c0f33f1] | 622 | td->prefix[strlen(url.host) - 4] = '\0'; |
---|
[5ebff60] | 623 | } |
---|
[4bc66ae] | 624 | if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) { |
---|
| 625 | /* If we have at least 3 chars after the last dot, cut off the rest. |
---|
| 626 | (mostly a www/api prefix or sth) */ |
---|
[c0f33f1] | 627 | s = g_strdup(s + 1); |
---|
| 628 | g_free(td->prefix); |
---|
| 629 | td->prefix = s; |
---|
| 630 | } |
---|
[5ebff60] | 631 | |
---|
| 632 | if (strstr(acc->pass, "oauth_token=")) { |
---|
[5983eca] | 633 | td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic)); |
---|
[5ebff60] | 634 | } |
---|
[5983eca] | 635 | |
---|
| 636 | sprintf(name, "%s_%s", td->prefix, acc->user); |
---|
| 637 | imcb_add_buddy(ic, name, NULL); |
---|
| 638 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
| 639 | |
---|
[c9b5817] | 640 | td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH); |
---|
| 641 | td->log_id = -1; |
---|
[5ebff60] | 642 | |
---|
[631ec80] | 643 | s = set_getstr(&ic->acc->set, "mode"); |
---|
[5ebff60] | 644 | if (g_strcasecmp(s, "one") == 0) { |
---|
[631ec80] | 645 | td->flags |= TWITTER_MODE_ONE; |
---|
[5ebff60] | 646 | } else if (g_strcasecmp(s, "many") == 0) { |
---|
[631ec80] | 647 | td->flags |= TWITTER_MODE_MANY; |
---|
[5ebff60] | 648 | } else { |
---|
[631ec80] | 649 | td->flags |= TWITTER_MODE_CHAT; |
---|
[5ebff60] | 650 | } |
---|
[5983eca] | 651 | |
---|
| 652 | twitter_login_finish(ic); |
---|
[1b221e0] | 653 | } |
---|
| 654 | |
---|
| 655 | /** |
---|
| 656 | * Logout method. Just free the twitter_data. |
---|
| 657 | */ |
---|
[5983eca] | 658 | static void twitter_logout(struct im_connection *ic) |
---|
[1b221e0] | 659 | { |
---|
| 660 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 661 | |
---|
[1b221e0] | 662 | // Set the status to logged out. |
---|
[5983eca] | 663 | ic->flags &= ~OPT_LOGGED_IN; |
---|
[1b221e0] | 664 | |
---|
[bbff22d] | 665 | if (td) { |
---|
| 666 | // Remove the main_loop function from the function queue. |
---|
| 667 | b_event_remove(td->main_loop_id); |
---|
[2abceca] | 668 | |
---|
[bbff22d] | 669 | if (td->timeline_gc) { |
---|
| 670 | imcb_chat_free(td->timeline_gc); |
---|
| 671 | } |
---|
[1014cab] | 672 | |
---|
[5ebff60] | 673 | if (td->filter_update_id > 0) { |
---|
[73ee390] | 674 | b_event_remove(td->filter_update_id); |
---|
[5ebff60] | 675 | } |
---|
[73ee390] | 676 | |
---|
[a244877] | 677 | g_slist_foreach(td->mutes_ids, (GFunc) g_free, NULL); |
---|
| 678 | g_slist_free(td->mutes_ids); |
---|
| 679 | |
---|
| 680 | g_slist_foreach(td->noretweets_ids, (GFunc) g_free, NULL); |
---|
| 681 | g_slist_free(td->noretweets_ids); |
---|
| 682 | |
---|
[1388d30] | 683 | http_close(td->stream); |
---|
[73ee390] | 684 | twitter_filter_remove_all(ic); |
---|
[5983eca] | 685 | oauth_info_free(td->oauth_info); |
---|
[de923d5] | 686 | g_free(td->user); |
---|
[5983eca] | 687 | g_free(td->prefix); |
---|
| 688 | g_free(td->url_host); |
---|
| 689 | g_free(td->url_path); |
---|
| 690 | g_free(td->log); |
---|
| 691 | g_free(td); |
---|
[1b221e0] | 692 | } |
---|
[62d2cfb] | 693 | |
---|
[5983eca] | 694 | twitter_connections = g_slist_remove(twitter_connections, ic); |
---|
[1b221e0] | 695 | } |
---|
| 696 | |
---|
[5983eca] | 697 | static void twitter_handle_command(struct im_connection *ic, char *message); |
---|
[7b87539] | 698 | |
---|
[1b221e0] | 699 | /** |
---|
| 700 | * |
---|
| 701 | */ |
---|
[5983eca] | 702 | static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away) |
---|
[1b221e0] | 703 | { |
---|
[c42e8b9] | 704 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 705 | int plen = strlen(td->prefix); |
---|
| 706 | |
---|
[ffcdf13] | 707 | if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' && |
---|
[5983eca] | 708 | g_strcasecmp(who + plen + 1, ic->acc->user) == 0) { |
---|
| 709 | if (set_getbool(&ic->acc->set, "oauth") && |
---|
| 710 | td->oauth_info && td->oauth_info->token == NULL) { |
---|
| 711 | char pin[strlen(message) + 1], *s; |
---|
| 712 | |
---|
| 713 | strcpy(pin, message); |
---|
[5ebff60] | 714 | for (s = pin + sizeof(pin) - 2; s > pin && g_ascii_isspace(*s); s--) { |
---|
[64f8c425] | 715 | *s = '\0'; |
---|
[5ebff60] | 716 | } |
---|
[6b13103] | 717 | for (s = pin; *s && g_ascii_isspace(*s); s++) { |
---|
[5983eca] | 718 | } |
---|
| 719 | |
---|
| 720 | if (!oauth_access_token(s, td->oauth_info)) { |
---|
| 721 | imcb_error(ic, "OAuth error: %s", |
---|
[5ebff60] | 722 | "Failed to send access token request"); |
---|
[5983eca] | 723 | imc_logout(ic, TRUE); |
---|
[c2ecadc] | 724 | return FALSE; |
---|
| 725 | } |
---|
[5ebff60] | 726 | } else { |
---|
[7b87539] | 727 | twitter_handle_command(ic, message); |
---|
[5ebff60] | 728 | } |
---|
[5983eca] | 729 | } else { |
---|
[e88fbe27] | 730 | twitter_direct_messages_new(ic, who, message); |
---|
[c42e8b9] | 731 | } |
---|
[5983eca] | 732 | return (0); |
---|
[1b221e0] | 733 | } |
---|
| 734 | |
---|
[5983eca] | 735 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
[1b221e0] | 736 | { |
---|
| 737 | } |
---|
| 738 | |
---|
[5983eca] | 739 | static void twitter_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
[1b221e0] | 740 | { |
---|
[7d53efb] | 741 | twitter_friendships_create_destroy(ic, who, 1); |
---|
[1b221e0] | 742 | } |
---|
| 743 | |
---|
[5983eca] | 744 | static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
[1b221e0] | 745 | { |
---|
[7d53efb] | 746 | twitter_friendships_create_destroy(ic, who, 0); |
---|
[1b221e0] | 747 | } |
---|
| 748 | |
---|
[5983eca] | 749 | static void twitter_chat_msg(struct groupchat *c, char *message, int flags) |
---|
[1b221e0] | 750 | { |
---|
[5ebff60] | 751 | if (c && message) { |
---|
[5983eca] | 752 | twitter_handle_command(c->ic, message); |
---|
[5ebff60] | 753 | } |
---|
[1b221e0] | 754 | } |
---|
| 755 | |
---|
[5983eca] | 756 | static void twitter_chat_invite(struct groupchat *c, char *who, char *message) |
---|
[1b221e0] | 757 | { |
---|
| 758 | } |
---|
| 759 | |
---|
[73ee390] | 760 | static struct groupchat *twitter_chat_join(struct im_connection *ic, |
---|
| 761 | const char *room, const char *nick, |
---|
| 762 | const char *password, set_t **sets) |
---|
| 763 | { |
---|
| 764 | struct groupchat *c = imcb_chat_new(ic, room); |
---|
| 765 | GSList *fs = twitter_filter_parse(c, room); |
---|
| 766 | GString *topic = g_string_new(""); |
---|
| 767 | struct twitter_filter *tf; |
---|
| 768 | GSList *l; |
---|
| 769 | |
---|
| 770 | fs = g_slist_sort(fs, (GCompareFunc) twitter_filter_cmp); |
---|
| 771 | |
---|
| 772 | for (l = fs; l; l = g_slist_next(l)) { |
---|
| 773 | tf = l->data; |
---|
| 774 | |
---|
[5ebff60] | 775 | if (topic->len > 0) { |
---|
[73ee390] | 776 | g_string_append(topic, ", "); |
---|
[5ebff60] | 777 | } |
---|
[73ee390] | 778 | |
---|
[5ebff60] | 779 | if (tf->type == TWITTER_FILTER_TYPE_FOLLOW) { |
---|
[73ee390] | 780 | g_string_append_c(topic, '@'); |
---|
[5ebff60] | 781 | } |
---|
[73ee390] | 782 | |
---|
| 783 | g_string_append(topic, tf->text); |
---|
| 784 | } |
---|
| 785 | |
---|
[5ebff60] | 786 | if (topic->len > 0) { |
---|
[73ee390] | 787 | g_string_prepend(topic, "Twitter Filter: "); |
---|
[5ebff60] | 788 | } |
---|
[73ee390] | 789 | |
---|
| 790 | imcb_chat_topic(c, NULL, topic->str, 0); |
---|
| 791 | imcb_chat_add_buddy(c, ic->acc->user); |
---|
| 792 | |
---|
| 793 | if (topic->len == 0) { |
---|
| 794 | imcb_error(ic, "Failed to handle any filters"); |
---|
| 795 | imcb_chat_free(c); |
---|
| 796 | c = NULL; |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | g_string_free(topic, TRUE); |
---|
| 800 | g_slist_free(fs); |
---|
| 801 | |
---|
| 802 | return c; |
---|
| 803 | } |
---|
| 804 | |
---|
[5983eca] | 805 | static void twitter_chat_leave(struct groupchat *c) |
---|
[1b221e0] | 806 | { |
---|
[16592d8] | 807 | struct twitter_data *td = c->ic->proto_data; |
---|
[5983eca] | 808 | |
---|
[73ee390] | 809 | if (c != td->timeline_gc) { |
---|
| 810 | twitter_filter_remove(c); |
---|
| 811 | imcb_chat_free(c); |
---|
| 812 | return; |
---|
| 813 | } |
---|
[5983eca] | 814 | |
---|
[16592d8] | 815 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
| 816 | tweets come in. */ |
---|
[2322a9f] | 817 | imcb_chat_free(td->timeline_gc); |
---|
| 818 | td->timeline_gc = NULL; |
---|
[1b221e0] | 819 | } |
---|
| 820 | |
---|
[5983eca] | 821 | static void twitter_keepalive(struct im_connection *ic) |
---|
[1b221e0] | 822 | { |
---|
| 823 | } |
---|
| 824 | |
---|
[5983eca] | 825 | static void twitter_add_permit(struct im_connection *ic, char *who) |
---|
[1b221e0] | 826 | { |
---|
| 827 | } |
---|
| 828 | |
---|
[5983eca] | 829 | static void twitter_rem_permit(struct im_connection *ic, char *who) |
---|
[1b221e0] | 830 | { |
---|
| 831 | } |
---|
| 832 | |
---|
[5983eca] | 833 | static void twitter_add_deny(struct im_connection *ic, char *who) |
---|
[1b221e0] | 834 | { |
---|
| 835 | } |
---|
| 836 | |
---|
[5983eca] | 837 | static void twitter_rem_deny(struct im_connection *ic, char *who) |
---|
[1b221e0] | 838 | { |
---|
| 839 | } |
---|
| 840 | |
---|
| 841 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
| 842 | //{ |
---|
[5983eca] | 843 | // return value; |
---|
[1b221e0] | 844 | //} |
---|
[7b87539] | 845 | |
---|
[5983eca] | 846 | static void twitter_buddy_data_add(struct bee_user *bu) |
---|
[203a2d2] | 847 | { |
---|
[5983eca] | 848 | bu->data = g_new0(struct twitter_user_data, 1); |
---|
[203a2d2] | 849 | } |
---|
| 850 | |
---|
[5983eca] | 851 | static void twitter_buddy_data_free(struct bee_user *bu) |
---|
[203a2d2] | 852 | { |
---|
[5983eca] | 853 | g_free(bu->data); |
---|
[203a2d2] | 854 | } |
---|
| 855 | |
---|
[2ca933c] | 856 | bee_user_t twitter_log_local_user; |
---|
| 857 | |
---|
[b61c74c] | 858 | /** Convert the given bitlbee tweet ID, bitlbee username, or twitter tweet ID |
---|
| 859 | * into a twitter tweet ID. |
---|
| 860 | * |
---|
| 861 | * Returns 0 if the user provides garbage. |
---|
| 862 | */ |
---|
[5ebff60] | 863 | static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, char *arg, bee_user_t **bu_) |
---|
| 864 | { |
---|
[3ca001b] | 865 | struct twitter_data *td = ic->proto_data; |
---|
[b61c74c] | 866 | struct twitter_user_data *tud; |
---|
[3ca001b] | 867 | bee_user_t *bu = NULL; |
---|
[b61c74c] | 868 | guint64 id = 0; |
---|
[5ebff60] | 869 | |
---|
| 870 | if (bu_) { |
---|
[3ca001b] | 871 | *bu_ = NULL; |
---|
[5ebff60] | 872 | } |
---|
| 873 | if (!arg || !arg[0]) { |
---|
[3ca001b] | 874 | return 0; |
---|
[5ebff60] | 875 | } |
---|
| 876 | |
---|
[3ca001b] | 877 | if (arg[0] != '#' && (bu = bee_user_by_handle(ic->bee, ic, arg))) { |
---|
[5ebff60] | 878 | if ((tud = bu->data)) { |
---|
[3ca001b] | 879 | id = tud->last_id; |
---|
[5ebff60] | 880 | } |
---|
[3ca001b] | 881 | } else { |
---|
[5ebff60] | 882 | if (arg[0] == '#') { |
---|
[3ca001b] | 883 | arg++; |
---|
[5ebff60] | 884 | } |
---|
[73f0a01] | 885 | if (parse_int64(arg, 16, &id) && id < TWITTER_LOG_LENGTH) { |
---|
[3ca001b] | 886 | bu = td->log[id].bu; |
---|
[b61c74c] | 887 | id = td->log[id].id; |
---|
[73f0a01] | 888 | } else if (parse_int64(arg, 10, &id)) { |
---|
[3ca001b] | 889 | /* Allow normal tweet IDs as well; not a very useful |
---|
| 890 | feature but it's always been there. Just ignore |
---|
| 891 | very low IDs to avoid accidents. */ |
---|
[5ebff60] | 892 | if (id < 1000000) { |
---|
[3ca001b] | 893 | id = 0; |
---|
[5ebff60] | 894 | } |
---|
[3ca001b] | 895 | } |
---|
[b61c74c] | 896 | } |
---|
[5ebff60] | 897 | if (bu_) { |
---|
[c43146d] | 898 | if (bu == &twitter_log_local_user) { |
---|
| 899 | /* HACK alert. There's no bee_user object for the local |
---|
| 900 | * user so just fake one for the few cmds that need it. */ |
---|
| 901 | twitter_log_local_user.handle = td->user; |
---|
| 902 | } else { |
---|
| 903 | /* Beware of dangling pointers! */ |
---|
| 904 | if (!g_slist_find(ic->bee->users, bu)) { |
---|
| 905 | bu = NULL; |
---|
| 906 | } |
---|
| 907 | } |
---|
[3ca001b] | 908 | *bu_ = bu; |
---|
[5ebff60] | 909 | } |
---|
[b61c74c] | 910 | return id; |
---|
| 911 | } |
---|
| 912 | |
---|
[5983eca] | 913 | static void twitter_handle_command(struct im_connection *ic, char *message) |
---|
[7b87539] | 914 | { |
---|
| 915 | struct twitter_data *td = ic->proto_data; |
---|
[15bc063] | 916 | char *cmds, **cmd, *new = NULL; |
---|
[3ca001b] | 917 | guint64 in_reply_to = 0, id; |
---|
| 918 | gboolean allow_post = |
---|
[5ebff60] | 919 | g_strcasecmp(set_getstr(&ic->acc->set, "commands"), "strict") != 0; |
---|
[b8b931d] | 920 | gboolean auto_populate_reply_metadata = FALSE; |
---|
[3ca001b] | 921 | bee_user_t *bu = NULL; |
---|
[5983eca] | 922 | |
---|
| 923 | cmds = g_strdup(message); |
---|
[269580c] | 924 | cmd = split_command_parts(cmds, 2); |
---|
[5983eca] | 925 | |
---|
| 926 | if (cmd[0] == NULL) { |
---|
[3ca001b] | 927 | goto eof; |
---|
| 928 | } else if (!set_getbool(&ic->acc->set, "commands") && allow_post) { |
---|
| 929 | /* Not supporting commands if "commands" is set to true/strict. */ |
---|
[5983eca] | 930 | } else if (g_strcasecmp(cmd[0], "undo") == 0) { |
---|
[5ebff60] | 931 | if (cmd[1] == NULL) { |
---|
[aa2f575] | 932 | twitter_status_destroy(ic, td->last_status_id); |
---|
[5ebff60] | 933 | } else if ((id = twitter_message_id_from_command_arg(ic, cmd[1], NULL))) { |
---|
[5983eca] | 934 | twitter_status_destroy(ic, id); |
---|
[5ebff60] | 935 | } else { |
---|
[96dd574] | 936 | twitter_log(ic, "Could not undo last action"); |
---|
[5ebff60] | 937 | } |
---|
[5983eca] | 938 | |
---|
[3ca001b] | 939 | goto eof; |
---|
[c203533] | 940 | } else if ((g_strcasecmp(cmd[0], "favourite") == 0 || |
---|
[5ebff60] | 941 | g_strcasecmp(cmd[0], "favorite") == 0 || |
---|
[59a7dc5] | 942 | g_strcasecmp(cmd[0], "fav") == 0 || |
---|
| 943 | g_strcasecmp(cmd[0], "like") == 0) && cmd[1]) { |
---|
[3ca001b] | 944 | if ((id = twitter_message_id_from_command_arg(ic, cmd[1], NULL))) { |
---|
[b61c74c] | 945 | twitter_favourite_tweet(ic, id); |
---|
| 946 | } else { |
---|
[96dd574] | 947 | twitter_log(ic, "Please provide a message ID or username."); |
---|
[b61c74c] | 948 | } |
---|
[3ca001b] | 949 | goto eof; |
---|
[5983eca] | 950 | } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) { |
---|
| 951 | twitter_add_buddy(ic, cmd[1], NULL); |
---|
[3ca001b] | 952 | goto eof; |
---|
[5983eca] | 953 | } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) { |
---|
| 954 | twitter_remove_buddy(ic, cmd[1], NULL); |
---|
[3ca001b] | 955 | goto eof; |
---|
[9cf63ac] | 956 | } else if (g_strcasecmp(cmd[0], "mute") == 0 && cmd[1]) { |
---|
| 957 | twitter_mute_create_destroy(ic, cmd[1], 1); |
---|
| 958 | goto eof; |
---|
| 959 | } else if (g_strcasecmp(cmd[0], "unmute") == 0 && cmd[1]) { |
---|
| 960 | twitter_mute_create_destroy(ic, cmd[1], 0); |
---|
| 961 | goto eof; |
---|
[d18dee42] | 962 | } else if ((g_strcasecmp(cmd[0], "report") == 0 || |
---|
| 963 | g_strcasecmp(cmd[0], "spam") == 0) && cmd[1]) { |
---|
[3ca001b] | 964 | char *screen_name; |
---|
[5ebff60] | 965 | |
---|
[d18dee42] | 966 | /* Report nominally works on users but look up the user who |
---|
| 967 | posted the given ID if the user wants to do it that way */ |
---|
[3ca001b] | 968 | twitter_message_id_from_command_arg(ic, cmd[1], &bu); |
---|
[5ebff60] | 969 | if (bu) { |
---|
[3ca001b] | 970 | screen_name = bu->handle; |
---|
[5ebff60] | 971 | } else { |
---|
[3ca001b] | 972 | screen_name = cmd[1]; |
---|
[5ebff60] | 973 | } |
---|
| 974 | |
---|
[d18dee42] | 975 | twitter_report_spam(ic, screen_name); |
---|
[3ca001b] | 976 | goto eof; |
---|
[5983eca] | 977 | } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) { |
---|
[3ca001b] | 978 | id = twitter_message_id_from_command_arg(ic, cmd[1], NULL); |
---|
[5983eca] | 979 | |
---|
[b890626] | 980 | td->last_status_id = 0; |
---|
[5ebff60] | 981 | if (id) { |
---|
[5983eca] | 982 | twitter_status_retweet(ic, id); |
---|
[5ebff60] | 983 | } else { |
---|
[96dd574] | 984 | twitter_log(ic, "User `%s' does not exist or didn't " |
---|
[5ebff60] | 985 | "post any statuses recently", cmd[1]); |
---|
| 986 | } |
---|
[5983eca] | 987 | |
---|
[3ca001b] | 988 | goto eof; |
---|
[5983eca] | 989 | } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) { |
---|
[3ca001b] | 990 | id = twitter_message_id_from_command_arg(ic, cmd[1], &bu); |
---|
[5983eca] | 991 | if (!id || !bu) { |
---|
[96dd574] | 992 | twitter_log(ic, "User `%s' does not exist or didn't " |
---|
[5ebff60] | 993 | "post any statuses recently", cmd[1]); |
---|
[3ca001b] | 994 | goto eof; |
---|
[15bc063] | 995 | } |
---|
[269580c] | 996 | message = new = g_strdup_printf("@%s %s", bu->handle, cmd[2]); |
---|
[15bc063] | 997 | in_reply_to = id; |
---|
[3ca001b] | 998 | allow_post = TRUE; |
---|
[b8b931d] | 999 | } else if (g_strcasecmp(cmd[0], "greply") == 0 && cmd[1] && cmd[2]) { |
---|
| 1000 | id = twitter_message_id_from_command_arg(ic, cmd[1], &bu); |
---|
| 1001 | if (!id || !bu) { |
---|
| 1002 | twitter_log(ic, "User `%s' does not exist or didn't " |
---|
| 1003 | "post any statuses recently", cmd[1]); |
---|
| 1004 | goto eof; |
---|
| 1005 | } |
---|
| 1006 | message = new = g_strdup_printf("%s", cmd[2]); |
---|
| 1007 | in_reply_to = id; |
---|
| 1008 | auto_populate_reply_metadata = TRUE; |
---|
| 1009 | allow_post = TRUE; |
---|
[dfaa46d] | 1010 | } else if (g_strcasecmp(cmd[0], "rawreply") == 0 && cmd[1] && cmd[2]) { |
---|
| 1011 | id = twitter_message_id_from_command_arg(ic, cmd[1], NULL); |
---|
| 1012 | if (!id) { |
---|
| 1013 | twitter_log(ic, "Tweet `%s' does not exist", cmd[1]); |
---|
| 1014 | goto eof; |
---|
| 1015 | } |
---|
| 1016 | message = cmd[2]; |
---|
| 1017 | in_reply_to = id; |
---|
| 1018 | allow_post = TRUE; |
---|
[9ed8175] | 1019 | } else if (g_strcasecmp(cmd[0], "url") == 0) { |
---|
| 1020 | id = twitter_message_id_from_command_arg(ic, cmd[1], &bu); |
---|
| 1021 | if (!id) { |
---|
| 1022 | twitter_log(ic, "Tweet `%s' does not exist", cmd[1]); |
---|
| 1023 | } else { |
---|
[1201fcb] | 1024 | twitter_status_show_url(ic, id); |
---|
[9ed8175] | 1025 | } |
---|
| 1026 | goto eof; |
---|
| 1027 | |
---|
[5983eca] | 1028 | } else if (g_strcasecmp(cmd[0], "post") == 0) { |
---|
[7b87539] | 1029 | message += 5; |
---|
[3ca001b] | 1030 | allow_post = TRUE; |
---|
[7b87539] | 1031 | } |
---|
[5983eca] | 1032 | |
---|
[3ca001b] | 1033 | if (allow_post) { |
---|
[15bc063] | 1034 | char *s; |
---|
[5983eca] | 1035 | |
---|
[5ebff60] | 1036 | if (!twitter_length_check(ic, message)) { |
---|
[3ca001b] | 1037 | goto eof; |
---|
[5ebff60] | 1038 | } |
---|
[5983eca] | 1039 | |
---|
| 1040 | s = cmd[0] + strlen(cmd[0]) - 1; |
---|
| 1041 | if (!new && s > cmd[0] && (*s == ':' || *s == ',')) { |
---|
[7b87539] | 1042 | *s = '\0'; |
---|
[5983eca] | 1043 | |
---|
| 1044 | if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) { |
---|
[b890626] | 1045 | struct twitter_user_data *tud = bu->data; |
---|
[5983eca] | 1046 | |
---|
| 1047 | new = g_strdup_printf("@%s %s", bu->handle, |
---|
[5ebff60] | 1048 | message + (s - cmd[0]) + 2); |
---|
[7b87539] | 1049 | message = new; |
---|
[5983eca] | 1050 | |
---|
| 1051 | if (time(NULL) < tud->last_time + |
---|
[5ebff60] | 1052 | set_getint(&ic->acc->set, "auto_reply_timeout")) { |
---|
[b890626] | 1053 | in_reply_to = tud->last_id; |
---|
[5ebff60] | 1054 | } |
---|
[7b87539] | 1055 | } |
---|
| 1056 | } |
---|
[5983eca] | 1057 | |
---|
[b890626] | 1058 | /* If the user runs undo between this request and its response |
---|
| 1059 | this would delete the second-last Tweet. Prevent that. */ |
---|
| 1060 | td->last_status_id = 0; |
---|
[b8b931d] | 1061 | twitter_post_status(ic, message, in_reply_to, auto_populate_reply_metadata); |
---|
[3ca001b] | 1062 | } else { |
---|
| 1063 | twitter_log(ic, "Unknown command: %s", cmd[0]); |
---|
[7b87539] | 1064 | } |
---|
[3ca001b] | 1065 | eof: |
---|
| 1066 | g_free(new); |
---|
[5983eca] | 1067 | g_free(cmds); |
---|
[7b87539] | 1068 | } |
---|
[1b221e0] | 1069 | |
---|
[5ebff60] | 1070 | void twitter_log(struct im_connection *ic, char *format, ...) |
---|
[96dd574] | 1071 | { |
---|
| 1072 | struct twitter_data *td = ic->proto_data; |
---|
| 1073 | va_list params; |
---|
| 1074 | char *text; |
---|
[5ebff60] | 1075 | |
---|
[96dd574] | 1076 | va_start(params, format); |
---|
| 1077 | text = g_strdup_vprintf(format, params); |
---|
| 1078 | va_end(params); |
---|
[5ebff60] | 1079 | |
---|
| 1080 | if (td->timeline_gc) { |
---|
[96dd574] | 1081 | imcb_chat_log(td->timeline_gc, "%s", text); |
---|
[5ebff60] | 1082 | } else { |
---|
[96dd574] | 1083 | imcb_log(ic, "%s", text); |
---|
[5ebff60] | 1084 | } |
---|
| 1085 | |
---|
[96dd574] | 1086 | g_free(text); |
---|
| 1087 | } |
---|
| 1088 | |
---|
| 1089 | |
---|
[1b221e0] | 1090 | void twitter_initmodule() |
---|
| 1091 | { |
---|
| 1092 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
[5983eca] | 1093 | |
---|
[9f03c47] | 1094 | ret->options = PRPL_OPT_NOOTR | PRPL_OPT_NO_PASSWORD; |
---|
[1b221e0] | 1095 | ret->name = "twitter"; |
---|
| 1096 | ret->login = twitter_login; |
---|
| 1097 | ret->init = twitter_init; |
---|
| 1098 | ret->logout = twitter_logout; |
---|
| 1099 | ret->buddy_msg = twitter_buddy_msg; |
---|
| 1100 | ret->get_info = twitter_get_info; |
---|
| 1101 | ret->add_buddy = twitter_add_buddy; |
---|
| 1102 | ret->remove_buddy = twitter_remove_buddy; |
---|
| 1103 | ret->chat_msg = twitter_chat_msg; |
---|
| 1104 | ret->chat_invite = twitter_chat_invite; |
---|
[73ee390] | 1105 | ret->chat_join = twitter_chat_join; |
---|
[1b221e0] | 1106 | ret->chat_leave = twitter_chat_leave; |
---|
| 1107 | ret->keepalive = twitter_keepalive; |
---|
| 1108 | ret->add_permit = twitter_add_permit; |
---|
| 1109 | ret->rem_permit = twitter_rem_permit; |
---|
| 1110 | ret->add_deny = twitter_add_deny; |
---|
| 1111 | ret->rem_deny = twitter_rem_deny; |
---|
[203a2d2] | 1112 | ret->buddy_data_add = twitter_buddy_data_add; |
---|
| 1113 | ret->buddy_data_free = twitter_buddy_data_free; |
---|
[1b221e0] | 1114 | ret->handle_cmp = g_strcasecmp; |
---|
[5983eca] | 1115 | |
---|
[ffcdf13] | 1116 | register_protocol(ret); |
---|
[1b221e0] | 1117 | |
---|
[ffcdf13] | 1118 | /* And an identi.ca variant: */ |
---|
[552da22] | 1119 | ret = g_memdup2(ret, sizeof(struct prpl)); |
---|
[ffcdf13] | 1120 | ret->name = "identica"; |
---|
[9f03c47] | 1121 | ret->options = PRPL_OPT_NOOTR; |
---|
[1b221e0] | 1122 | register_protocol(ret); |
---|
| 1123 | } |
---|