[1b221e0] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple module to facilitate twitter functionality. * |
---|
| 5 | * * |
---|
| 6 | * Copyright 2009 Geert Mulders <g.c.w.m.mulders@gmail.com> * |
---|
| 7 | * * |
---|
| 8 | * This library is free software; you can redistribute it and/or * |
---|
| 9 | * modify it under the terms of the GNU Lesser General Public * |
---|
| 10 | * License as published by the Free Software Foundation, version * |
---|
| 11 | * 2.1. * |
---|
| 12 | * * |
---|
| 13 | * This library is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
| 16 | * Lesser General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
| 21 | * * |
---|
| 22 | ****************************************************************************/ |
---|
| 23 | |
---|
| 24 | #include "nogaim.h" |
---|
[713d611] | 25 | #include "oauth.h" |
---|
[1b221e0] | 26 | #include "twitter.h" |
---|
| 27 | #include "twitter_http.h" |
---|
| 28 | #include "twitter_lib.h" |
---|
[bb5ce4d1] | 29 | #include "url.h" |
---|
[1b221e0] | 30 | |
---|
[665c24f] | 31 | #define twitter_msg( ic, fmt... ) \ |
---|
[2322a9f] | 32 | do { \ |
---|
| 33 | struct twitter_data *td = ic->proto_data; \ |
---|
| 34 | if( td->timeline_gc ) \ |
---|
| 35 | imcb_chat_log( td->timeline_gc, fmt ); \ |
---|
| 36 | else \ |
---|
| 37 | imcb_log( ic, fmt ); \ |
---|
[665c24f] | 38 | } while( 0 ); |
---|
[5983eca] | 39 | |
---|
[9c9a29c] | 40 | GSList *twitter_connections = NULL; |
---|
[665c24f] | 41 | |
---|
[1b221e0] | 42 | /** |
---|
[62d2cfb] | 43 | * Main loop function |
---|
| 44 | */ |
---|
[1b221e0] | 45 | gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond) |
---|
| 46 | { |
---|
| 47 | struct im_connection *ic = data; |
---|
[5983eca] | 48 | |
---|
[1b221e0] | 49 | // Check if we are still logged in... |
---|
[5983eca] | 50 | if (!g_slist_find(twitter_connections, ic)) |
---|
[1b221e0] | 51 | return 0; |
---|
| 52 | |
---|
| 53 | // Do stuff.. |
---|
[2322a9f] | 54 | twitter_get_timeline(ic, -1); |
---|
[1b221e0] | 55 | |
---|
| 56 | // If we are still logged in run this function again after timeout. |
---|
| 57 | return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN; |
---|
| 58 | } |
---|
| 59 | |
---|
[5983eca] | 60 | static void twitter_main_loop_start(struct im_connection *ic) |
---|
[713d611] | 61 | { |
---|
| 62 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 63 | |
---|
| 64 | imcb_log(ic, "Getting initial statuses"); |
---|
[713d611] | 65 | |
---|
| 66 | // Run this once. After this queue the main loop function. |
---|
| 67 | twitter_main_loop(ic, -1, 0); |
---|
| 68 | |
---|
| 69 | // Queue the main_loop |
---|
| 70 | // Save the return value, so we can remove the timeout on logout. |
---|
[2322a9f] | 71 | td->main_loop_id = |
---|
| 72 | b_timeout_add(set_getint(&ic->acc->set, "fetch_interval") * 1000, twitter_main_loop, ic); |
---|
[713d611] | 73 | } |
---|
| 74 | |
---|
[5983eca] | 75 | static void twitter_oauth_start(struct im_connection *ic); |
---|
[d6aa6dd] | 76 | |
---|
[5983eca] | 77 | void twitter_login_finish(struct im_connection *ic) |
---|
[d6aa6dd] | 78 | { |
---|
| 79 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 80 | |
---|
[2322a9f] | 81 | td->flags &= ~TWITTER_DOING_TIMELINE; |
---|
| 82 | |
---|
[5983eca] | 83 | if (set_getbool(&ic->acc->set, "oauth") && !td->oauth_info) |
---|
| 84 | twitter_oauth_start(ic); |
---|
| 85 | else if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") != 0 && |
---|
| 86 | !(td->flags & TWITTER_HAVE_FRIENDS)) { |
---|
| 87 | imcb_log(ic, "Getting contact list"); |
---|
[de923d5] | 88 | twitter_get_friends_ids(ic, -1); |
---|
| 89 | //twitter_get_statuses_friends(ic, -1); |
---|
[5983eca] | 90 | } else |
---|
| 91 | twitter_main_loop_start(ic); |
---|
[d6aa6dd] | 92 | } |
---|
[c2ecadc] | 93 | |
---|
[5983eca] | 94 | static const struct oauth_service twitter_oauth = { |
---|
[3f808ca] | 95 | "https://api.twitter.com/oauth/request_token", |
---|
| 96 | "https://api.twitter.com/oauth/access_token", |
---|
[c01bbd1] | 97 | "https://api.twitter.com/oauth/authorize", |
---|
[c2ecadc] | 98 | .consumer_key = "xsDNKJuNZYkZyMcu914uEA", |
---|
| 99 | .consumer_secret = "FCxqcr0pXKzsF9ajmP57S3VQ8V6Drk4o2QYtqMcOszo", |
---|
| 100 | }; |
---|
| 101 | |
---|
[5983eca] | 102 | static const struct oauth_service identica_oauth = { |
---|
[3f808ca] | 103 | "https://identi.ca/api/oauth/request_token", |
---|
| 104 | "https://identi.ca/api/oauth/access_token", |
---|
[ce617f0] | 105 | "https://identi.ca/api/oauth/authorize", |
---|
| 106 | .consumer_key = "e147ff789fcbd8a5a07963afbb43f9da", |
---|
| 107 | .consumer_secret = "c596267f277457ec0ce1ab7bb788d828", |
---|
| 108 | }; |
---|
| 109 | |
---|
[5983eca] | 110 | static gboolean twitter_oauth_callback(struct oauth_info *info); |
---|
[713d611] | 111 | |
---|
[5983eca] | 112 | static const struct oauth_service *get_oauth_service(struct im_connection *ic) |
---|
[ce617f0] | 113 | { |
---|
| 114 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 115 | |
---|
| 116 | if (strstr(td->url_host, "identi.ca")) |
---|
[ce617f0] | 117 | return &identica_oauth; |
---|
| 118 | else |
---|
| 119 | return &twitter_oauth; |
---|
[5983eca] | 120 | |
---|
[ce617f0] | 121 | /* Could add more services, or allow configuring your own base URL + |
---|
| 122 | API keys. */ |
---|
| 123 | } |
---|
| 124 | |
---|
[5983eca] | 125 | static void twitter_oauth_start(struct im_connection *ic) |
---|
[713d611] | 126 | { |
---|
[18dbb20] | 127 | struct twitter_data *td = ic->proto_data; |
---|
[c42e8b9] | 128 | |
---|
[5983eca] | 129 | imcb_log(ic, "Requesting OAuth request token"); |
---|
| 130 | |
---|
| 131 | td->oauth_info = oauth_request_token(get_oauth_service(ic), twitter_oauth_callback, ic); |
---|
| 132 | |
---|
[748bcdd] | 133 | /* We need help from the user to complete OAuth login, so don't time |
---|
| 134 | out on this login. */ |
---|
| 135 | ic->flags |= OPT_SLOW_LOGIN; |
---|
[713d611] | 136 | } |
---|
| 137 | |
---|
[5983eca] | 138 | static gboolean twitter_oauth_callback(struct oauth_info *info) |
---|
[713d611] | 139 | { |
---|
| 140 | struct im_connection *ic = info->data; |
---|
[18dbb20] | 141 | struct twitter_data *td; |
---|
[5983eca] | 142 | |
---|
| 143 | if (!g_slist_find(twitter_connections, ic)) |
---|
[18dbb20] | 144 | return FALSE; |
---|
[5983eca] | 145 | |
---|
[18dbb20] | 146 | td = ic->proto_data; |
---|
[5983eca] | 147 | if (info->stage == OAUTH_REQUEST_TOKEN) { |
---|
| 148 | char name[strlen(ic->acc->user) + 9], *msg; |
---|
| 149 | |
---|
| 150 | if (info->request_token == NULL) { |
---|
| 151 | imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); |
---|
| 152 | imc_logout(ic, TRUE); |
---|
[18dbb20] | 153 | return FALSE; |
---|
[c42e8b9] | 154 | } |
---|
[5983eca] | 155 | |
---|
| 156 | sprintf(name, "%s_%s", td->prefix, ic->acc->user); |
---|
| 157 | msg = g_strdup_printf("To finish OAuth authentication, please visit " |
---|
| 158 | "%s and respond with the resulting PIN code.", |
---|
| 159 | info->auth_url); |
---|
| 160 | imcb_buddy_msg(ic, name, msg, 0, 0); |
---|
| 161 | g_free(msg); |
---|
| 162 | } else if (info->stage == OAUTH_ACCESS_TOKEN) { |
---|
| 163 | if (info->token == NULL || info->token_secret == NULL) { |
---|
| 164 | imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); |
---|
| 165 | imc_logout(ic, TRUE); |
---|
[18dbb20] | 166 | return FALSE; |
---|
[5983eca] | 167 | } else { |
---|
| 168 | const char *sn = oauth_params_get(&info->params, "screen_name"); |
---|
| 169 | |
---|
| 170 | if (sn != NULL && ic->acc->prpl->handle_cmp(sn, ic->acc->user) != 0) { |
---|
| 171 | imcb_log(ic, "Warning: You logged in via OAuth as %s " |
---|
| 172 | "instead of %s.", sn, ic->acc->user); |
---|
[93cc86f] | 173 | } |
---|
[de923d5] | 174 | g_free(td->user); |
---|
| 175 | td->user = g_strdup(sn); |
---|
[93cc86f] | 176 | } |
---|
[5983eca] | 177 | |
---|
[288b215] | 178 | /* IM mods didn't do this so far and it's ugly but I should |
---|
| 179 | be able to get away with it... */ |
---|
[5983eca] | 180 | g_free(ic->acc->pass); |
---|
| 181 | ic->acc->pass = oauth_to_string(info); |
---|
| 182 | |
---|
| 183 | twitter_login_finish(ic); |
---|
[c42e8b9] | 184 | } |
---|
[5983eca] | 185 | |
---|
[18dbb20] | 186 | return TRUE; |
---|
[713d611] | 187 | } |
---|
| 188 | |
---|
[c2ecadc] | 189 | |
---|
[5983eca] | 190 | static char *set_eval_mode(set_t * set, char *value) |
---|
[e88fbe27] | 191 | { |
---|
[5983eca] | 192 | if (g_strcasecmp(value, "one") == 0 || |
---|
| 193 | g_strcasecmp(value, "many") == 0 || g_strcasecmp(value, "chat") == 0) |
---|
[e88fbe27] | 194 | return value; |
---|
| 195 | else |
---|
| 196 | return NULL; |
---|
| 197 | } |
---|
[1b221e0] | 198 | |
---|
[5983eca] | 199 | static gboolean twitter_length_check(struct im_connection *ic, gchar * msg) |
---|
[9997691] | 200 | { |
---|
[5983eca] | 201 | int max = set_getint(&ic->acc->set, "message_length"), len; |
---|
| 202 | |
---|
| 203 | if (max == 0 || (len = g_utf8_strlen(msg, -1)) <= max) |
---|
[9997691] | 204 | return TRUE; |
---|
[5983eca] | 205 | |
---|
| 206 | imcb_error(ic, "Maximum message length exceeded: %d > %d", len, max); |
---|
| 207 | |
---|
[9997691] | 208 | return FALSE; |
---|
| 209 | } |
---|
| 210 | |
---|
[5983eca] | 211 | static void twitter_init(account_t * acc) |
---|
[1b221e0] | 212 | { |
---|
[62d2cfb] | 213 | set_t *s; |
---|
[ffcdf13] | 214 | char *def_url; |
---|
| 215 | char *def_oauth; |
---|
[5983eca] | 216 | |
---|
| 217 | if (strcmp(acc->prpl->name, "twitter") == 0) { |
---|
[ffcdf13] | 218 | def_url = TWITTER_API_URL; |
---|
| 219 | def_oauth = "true"; |
---|
[5983eca] | 220 | } else { /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */ |
---|
[ffcdf13] | 221 | def_url = IDENTICA_API_URL; |
---|
[e306fbf] | 222 | def_oauth = "true"; |
---|
[ffcdf13] | 223 | } |
---|
[5983eca] | 224 | |
---|
| 225 | s = set_add(&acc->set, "auto_reply_timeout", "10800", set_eval_int, acc); |
---|
| 226 | |
---|
| 227 | s = set_add(&acc->set, "base_url", def_url, NULL, acc); |
---|
[bb5ce4d1] | 228 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 229 | |
---|
| 230 | s = set_add(&acc->set, "commands", "true", set_eval_bool, acc); |
---|
| 231 | |
---|
[2322a9f] | 232 | s = set_add(&acc->set, "fetch_interval", "60", set_eval_int, acc); |
---|
| 233 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 234 | |
---|
| 235 | s = set_add(&acc->set, "fetch_mentions", "true", set_eval_bool, acc); |
---|
| 236 | |
---|
[5983eca] | 237 | s = set_add(&acc->set, "message_length", "140", set_eval_int, acc); |
---|
| 238 | |
---|
| 239 | s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc); |
---|
[2abceca] | 240 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 241 | |
---|
[ce199b7] | 242 | s = set_add(&acc->set, "oauth", def_oauth, set_eval_oauth, acc); |
---|
| 243 | |
---|
[5983eca] | 244 | s = set_add(&acc->set, "show_ids", "false", set_eval_bool, acc); |
---|
[ce81acd] | 245 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 246 | |
---|
[2322a9f] | 247 | s = set_add(&acc->set, "show_old_mentions", "true", set_eval_bool, acc); |
---|
[1b221e0] | 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | * Login method. Since the twitter API works with seperate HTTP request we |
---|
| 252 | * only save the user and pass to the twitter_data object. |
---|
| 253 | */ |
---|
[5983eca] | 254 | static void twitter_login(account_t * acc) |
---|
[1b221e0] | 255 | { |
---|
[5983eca] | 256 | struct im_connection *ic = imcb_new(acc); |
---|
[bb5ce4d1] | 257 | struct twitter_data *td; |
---|
[5983eca] | 258 | char name[strlen(acc->user) + 9]; |
---|
[bb5ce4d1] | 259 | url_t url; |
---|
[c0f33f1] | 260 | char *s; |
---|
| 261 | |
---|
[5983eca] | 262 | if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) || |
---|
| 263 | (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) { |
---|
| 264 | imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url")); |
---|
| 265 | imc_logout(ic, FALSE); |
---|
[bb5ce4d1] | 266 | return; |
---|
| 267 | } |
---|
[5983eca] | 268 | |
---|
[c0f33f1] | 269 | imcb_log(ic, "Connecting"); |
---|
| 270 | |
---|
[5983eca] | 271 | twitter_connections = g_slist_append(twitter_connections, ic); |
---|
| 272 | td = g_new0(struct twitter_data, 1); |
---|
[713d611] | 273 | ic->proto_data = td; |
---|
[de923d5] | 274 | td->user = g_strdup(acc->user); |
---|
[5983eca] | 275 | |
---|
[bb5ce4d1] | 276 | td->url_ssl = url.proto == PROTO_HTTPS; |
---|
| 277 | td->url_port = url.port; |
---|
[5983eca] | 278 | td->url_host = g_strdup(url.host); |
---|
| 279 | if (strcmp(url.file, "/") != 0) |
---|
| 280 | td->url_path = g_strdup(url.file); |
---|
[c0f33f1] | 281 | else { |
---|
[5983eca] | 282 | td->url_path = g_strdup(""); |
---|
[c0f33f1] | 283 | if (g_str_has_suffix(url.host, "twitter.com")) |
---|
| 284 | /* May fire for people who turned on HTTPS. */ |
---|
| 285 | imcb_error(ic, "Warning: Twitter requires a version number in API calls " |
---|
| 286 | "now. Try resetting the base_url account setting."); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com |
---|
| 290 | into twitter, and try to be sensible if we get anything else. */ |
---|
| 291 | td->prefix = g_strdup(url.host); |
---|
| 292 | if (g_str_has_suffix(td->prefix, ".com")) |
---|
| 293 | td->prefix[strlen(url.host) - 4] = '\0'; |
---|
[4bc66ae] | 294 | if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) { |
---|
| 295 | /* If we have at least 3 chars after the last dot, cut off the rest. |
---|
| 296 | (mostly a www/api prefix or sth) */ |
---|
[c0f33f1] | 297 | s = g_strdup(s + 1); |
---|
| 298 | g_free(td->prefix); |
---|
| 299 | td->prefix = s; |
---|
| 300 | } |
---|
| 301 | |
---|
[5983eca] | 302 | if (strstr(acc->pass, "oauth_token=")) |
---|
| 303 | td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic)); |
---|
| 304 | |
---|
| 305 | sprintf(name, "%s_%s", td->prefix, acc->user); |
---|
| 306 | imcb_add_buddy(ic, name, NULL); |
---|
| 307 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
| 308 | |
---|
| 309 | if (set_getbool(&acc->set, "show_ids")) |
---|
| 310 | td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH); |
---|
| 311 | |
---|
| 312 | twitter_login_finish(ic); |
---|
[1b221e0] | 313 | } |
---|
| 314 | |
---|
| 315 | /** |
---|
| 316 | * Logout method. Just free the twitter_data. |
---|
| 317 | */ |
---|
[5983eca] | 318 | static void twitter_logout(struct im_connection *ic) |
---|
[1b221e0] | 319 | { |
---|
| 320 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 321 | |
---|
[1b221e0] | 322 | // Set the status to logged out. |
---|
[5983eca] | 323 | ic->flags &= ~OPT_LOGGED_IN; |
---|
[1b221e0] | 324 | |
---|
[2abceca] | 325 | // Remove the main_loop function from the function queue. |
---|
| 326 | b_event_remove(td->main_loop_id); |
---|
| 327 | |
---|
[2322a9f] | 328 | if (td->timeline_gc) |
---|
| 329 | imcb_chat_free(td->timeline_gc); |
---|
[1014cab] | 330 | |
---|
[5983eca] | 331 | if (td) { |
---|
| 332 | oauth_info_free(td->oauth_info); |
---|
[de923d5] | 333 | g_free(td->user); |
---|
[5983eca] | 334 | g_free(td->prefix); |
---|
| 335 | g_free(td->url_host); |
---|
| 336 | g_free(td->url_path); |
---|
| 337 | g_free(td->log); |
---|
| 338 | g_free(td); |
---|
[1b221e0] | 339 | } |
---|
[62d2cfb] | 340 | |
---|
[5983eca] | 341 | twitter_connections = g_slist_remove(twitter_connections, ic); |
---|
[1b221e0] | 342 | } |
---|
| 343 | |
---|
[5983eca] | 344 | static void twitter_handle_command(struct im_connection *ic, char *message); |
---|
[7b87539] | 345 | |
---|
[1b221e0] | 346 | /** |
---|
| 347 | * |
---|
| 348 | */ |
---|
[5983eca] | 349 | static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away) |
---|
[1b221e0] | 350 | { |
---|
[c42e8b9] | 351 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 352 | int plen = strlen(td->prefix); |
---|
| 353 | |
---|
[ffcdf13] | 354 | if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' && |
---|
[5983eca] | 355 | g_strcasecmp(who + plen + 1, ic->acc->user) == 0) { |
---|
| 356 | if (set_getbool(&ic->acc->set, "oauth") && |
---|
| 357 | td->oauth_info && td->oauth_info->token == NULL) { |
---|
| 358 | char pin[strlen(message) + 1], *s; |
---|
| 359 | |
---|
| 360 | strcpy(pin, message); |
---|
| 361 | for (s = pin + sizeof(pin) - 2; s > pin && isspace(*s); s--) |
---|
[64f8c425] | 362 | *s = '\0'; |
---|
[5983eca] | 363 | for (s = pin; *s && isspace(*s); s++) { |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | if (!oauth_access_token(s, td->oauth_info)) { |
---|
| 367 | imcb_error(ic, "OAuth error: %s", |
---|
| 368 | "Failed to send access token request"); |
---|
| 369 | imc_logout(ic, TRUE); |
---|
[c2ecadc] | 370 | return FALSE; |
---|
| 371 | } |
---|
[5983eca] | 372 | } else |
---|
[7b87539] | 373 | twitter_handle_command(ic, message); |
---|
[5983eca] | 374 | } else { |
---|
[e88fbe27] | 375 | twitter_direct_messages_new(ic, who, message); |
---|
[c42e8b9] | 376 | } |
---|
[5983eca] | 377 | return (0); |
---|
[1b221e0] | 378 | } |
---|
| 379 | |
---|
| 380 | /** |
---|
| 381 | * |
---|
| 382 | */ |
---|
[5983eca] | 383 | static void twitter_set_my_name(struct im_connection *ic, char *info) |
---|
[1b221e0] | 384 | { |
---|
| 385 | } |
---|
| 386 | |
---|
[5983eca] | 387 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
[1b221e0] | 388 | { |
---|
| 389 | } |
---|
| 390 | |
---|
[5983eca] | 391 | static void twitter_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
[1b221e0] | 392 | { |
---|
[7d53efb] | 393 | twitter_friendships_create_destroy(ic, who, 1); |
---|
[1b221e0] | 394 | } |
---|
| 395 | |
---|
[5983eca] | 396 | static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
[1b221e0] | 397 | { |
---|
[7d53efb] | 398 | twitter_friendships_create_destroy(ic, who, 0); |
---|
[1b221e0] | 399 | } |
---|
| 400 | |
---|
[5983eca] | 401 | static void twitter_chat_msg(struct groupchat *c, char *message, int flags) |
---|
[1b221e0] | 402 | { |
---|
[5983eca] | 403 | if (c && message) |
---|
| 404 | twitter_handle_command(c->ic, message); |
---|
[1b221e0] | 405 | } |
---|
| 406 | |
---|
[5983eca] | 407 | static void twitter_chat_invite(struct groupchat *c, char *who, char *message) |
---|
[1b221e0] | 408 | { |
---|
| 409 | } |
---|
| 410 | |
---|
[5983eca] | 411 | static void twitter_chat_leave(struct groupchat *c) |
---|
[1b221e0] | 412 | { |
---|
[16592d8] | 413 | struct twitter_data *td = c->ic->proto_data; |
---|
[5983eca] | 414 | |
---|
[2322a9f] | 415 | if (c != td->timeline_gc) |
---|
[5983eca] | 416 | return; /* WTF? */ |
---|
| 417 | |
---|
[16592d8] | 418 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
| 419 | tweets come in. */ |
---|
[2322a9f] | 420 | imcb_chat_free(td->timeline_gc); |
---|
| 421 | td->timeline_gc = NULL; |
---|
[1b221e0] | 422 | } |
---|
| 423 | |
---|
[5983eca] | 424 | static void twitter_keepalive(struct im_connection *ic) |
---|
[1b221e0] | 425 | { |
---|
| 426 | } |
---|
| 427 | |
---|
[5983eca] | 428 | static void twitter_add_permit(struct im_connection *ic, char *who) |
---|
[1b221e0] | 429 | { |
---|
| 430 | } |
---|
| 431 | |
---|
[5983eca] | 432 | static void twitter_rem_permit(struct im_connection *ic, char *who) |
---|
[1b221e0] | 433 | { |
---|
| 434 | } |
---|
| 435 | |
---|
[5983eca] | 436 | static void twitter_add_deny(struct im_connection *ic, char *who) |
---|
[1b221e0] | 437 | { |
---|
| 438 | } |
---|
| 439 | |
---|
[5983eca] | 440 | static void twitter_rem_deny(struct im_connection *ic, char *who) |
---|
[1b221e0] | 441 | { |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
| 445 | //{ |
---|
[5983eca] | 446 | // return value; |
---|
[1b221e0] | 447 | //} |
---|
[7b87539] | 448 | |
---|
[5983eca] | 449 | static void twitter_buddy_data_add(struct bee_user *bu) |
---|
[203a2d2] | 450 | { |
---|
[5983eca] | 451 | bu->data = g_new0(struct twitter_user_data, 1); |
---|
[203a2d2] | 452 | } |
---|
| 453 | |
---|
[5983eca] | 454 | static void twitter_buddy_data_free(struct bee_user *bu) |
---|
[203a2d2] | 455 | { |
---|
[5983eca] | 456 | g_free(bu->data); |
---|
[203a2d2] | 457 | } |
---|
| 458 | |
---|
[5983eca] | 459 | static void twitter_handle_command(struct im_connection *ic, char *message) |
---|
[7b87539] | 460 | { |
---|
| 461 | struct twitter_data *td = ic->proto_data; |
---|
[15bc063] | 462 | char *cmds, **cmd, *new = NULL; |
---|
| 463 | guint64 in_reply_to = 0; |
---|
[5983eca] | 464 | |
---|
| 465 | cmds = g_strdup(message); |
---|
| 466 | cmd = split_command_parts(cmds); |
---|
| 467 | |
---|
| 468 | if (cmd[0] == NULL) { |
---|
| 469 | g_free(cmds); |
---|
[7b87539] | 470 | return; |
---|
[5983eca] | 471 | } else if (!set_getbool(&ic->acc->set, "commands")) { |
---|
[7b87539] | 472 | /* Not supporting commands. */ |
---|
[5983eca] | 473 | } else if (g_strcasecmp(cmd[0], "undo") == 0) { |
---|
[7b87539] | 474 | guint64 id; |
---|
[5983eca] | 475 | |
---|
[aa2f575] | 476 | if (cmd[1] == NULL) |
---|
| 477 | twitter_status_destroy(ic, td->last_status_id); |
---|
| 478 | else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
| 479 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
| 480 | id = td->log[id].id; |
---|
| 481 | |
---|
[5983eca] | 482 | twitter_status_destroy(ic, id); |
---|
[aa2f575] | 483 | } else |
---|
[5983eca] | 484 | twitter_msg(ic, "Could not undo last action"); |
---|
| 485 | |
---|
| 486 | g_free(cmds); |
---|
[7b87539] | 487 | return; |
---|
[5983eca] | 488 | } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) { |
---|
| 489 | twitter_add_buddy(ic, cmd[1], NULL); |
---|
| 490 | g_free(cmds); |
---|
[b890626] | 491 | return; |
---|
[5983eca] | 492 | } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) { |
---|
| 493 | twitter_remove_buddy(ic, cmd[1], NULL); |
---|
| 494 | g_free(cmds); |
---|
[b890626] | 495 | return; |
---|
[5983eca] | 496 | } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) { |
---|
[b890626] | 497 | struct twitter_user_data *tud; |
---|
| 498 | bee_user_t *bu; |
---|
| 499 | guint64 id; |
---|
[5983eca] | 500 | |
---|
[aa2f575] | 501 | if (g_str_has_prefix(cmd[1], "#") && |
---|
| 502 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
[5f1e78d] | 503 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
| 504 | id = td->log[id].id; |
---|
[d6b6906] | 505 | } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && |
---|
[5983eca] | 506 | (tud = bu->data) && tud->last_id) |
---|
[b890626] | 507 | id = tud->last_id; |
---|
[aa2f575] | 508 | else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1){ |
---|
[5983eca] | 509 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
[4f50ea5] | 510 | id = td->log[id].id; |
---|
| 511 | } |
---|
[5983eca] | 512 | |
---|
[b890626] | 513 | td->last_status_id = 0; |
---|
[5983eca] | 514 | if (id) |
---|
| 515 | twitter_status_retweet(ic, id); |
---|
[665c24f] | 516 | else |
---|
[5983eca] | 517 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
| 518 | "post any statuses recently", cmd[1]); |
---|
| 519 | |
---|
| 520 | g_free(cmds); |
---|
[b890626] | 521 | return; |
---|
[5983eca] | 522 | } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) { |
---|
[15bc063] | 523 | struct twitter_user_data *tud; |
---|
| 524 | bee_user_t *bu = NULL; |
---|
| 525 | guint64 id = 0; |
---|
[5983eca] | 526 | |
---|
[aa2f575] | 527 | if (g_str_has_prefix(cmd[1], "#") && |
---|
| 528 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
| 529 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
| 530 | bu = td->log[id].bu; |
---|
| 531 | if (g_slist_find(ic->bee->users, bu)) |
---|
| 532 | id = td->log[id].id; |
---|
| 533 | else |
---|
| 534 | bu = NULL; |
---|
| 535 | } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && |
---|
[5983eca] | 536 | (tud = bu->data) && tud->last_id) { |
---|
[15bc063] | 537 | id = tud->last_id; |
---|
[7ceb6b2] | 538 | } else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
| 539 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
[15bc063] | 540 | bu = td->log[id].bu; |
---|
[5983eca] | 541 | if (g_slist_find(ic->bee->users, bu)) |
---|
[15bc063] | 542 | id = td->log[id].id; |
---|
| 543 | else |
---|
| 544 | bu = NULL; |
---|
| 545 | } |
---|
[aa2f575] | 546 | |
---|
[5983eca] | 547 | if (!id || !bu) { |
---|
| 548 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
| 549 | "post any statuses recently", cmd[1]); |
---|
[15bc063] | 550 | return; |
---|
| 551 | } |
---|
[5983eca] | 552 | message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0])); |
---|
[15bc063] | 553 | in_reply_to = id; |
---|
[5983eca] | 554 | } else if (g_strcasecmp(cmd[0], "post") == 0) { |
---|
[7b87539] | 555 | message += 5; |
---|
| 556 | } |
---|
[5983eca] | 557 | |
---|
[7b87539] | 558 | { |
---|
[15bc063] | 559 | char *s; |
---|
[7b87539] | 560 | bee_user_t *bu; |
---|
[5983eca] | 561 | |
---|
| 562 | if (!twitter_length_check(ic, message)) { |
---|
| 563 | g_free(new); |
---|
| 564 | g_free(cmds); |
---|
| 565 | return; |
---|
[7b87539] | 566 | } |
---|
[5983eca] | 567 | |
---|
| 568 | s = cmd[0] + strlen(cmd[0]) - 1; |
---|
| 569 | if (!new && s > cmd[0] && (*s == ':' || *s == ',')) { |
---|
[7b87539] | 570 | *s = '\0'; |
---|
[5983eca] | 571 | |
---|
| 572 | if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) { |
---|
[b890626] | 573 | struct twitter_user_data *tud = bu->data; |
---|
[5983eca] | 574 | |
---|
| 575 | new = g_strdup_printf("@%s %s", bu->handle, |
---|
| 576 | message + (s - cmd[0]) + 2); |
---|
[7b87539] | 577 | message = new; |
---|
[5983eca] | 578 | |
---|
| 579 | if (time(NULL) < tud->last_time + |
---|
| 580 | set_getint(&ic->acc->set, "auto_reply_timeout")) |
---|
[b890626] | 581 | in_reply_to = tud->last_id; |
---|
[7b87539] | 582 | } |
---|
| 583 | } |
---|
[5983eca] | 584 | |
---|
[b890626] | 585 | /* If the user runs undo between this request and its response |
---|
| 586 | this would delete the second-last Tweet. Prevent that. */ |
---|
| 587 | td->last_status_id = 0; |
---|
[5983eca] | 588 | twitter_post_status(ic, message, in_reply_to); |
---|
| 589 | g_free(new); |
---|
[7b87539] | 590 | } |
---|
[5983eca] | 591 | g_free(cmds); |
---|
[7b87539] | 592 | } |
---|
[1b221e0] | 593 | |
---|
| 594 | void twitter_initmodule() |
---|
| 595 | { |
---|
| 596 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
[5983eca] | 597 | |
---|
[1dd3470] | 598 | ret->options = OPT_NOOTR; |
---|
[1b221e0] | 599 | ret->name = "twitter"; |
---|
| 600 | ret->login = twitter_login; |
---|
| 601 | ret->init = twitter_init; |
---|
| 602 | ret->logout = twitter_logout; |
---|
| 603 | ret->buddy_msg = twitter_buddy_msg; |
---|
| 604 | ret->get_info = twitter_get_info; |
---|
| 605 | ret->set_my_name = twitter_set_my_name; |
---|
| 606 | ret->add_buddy = twitter_add_buddy; |
---|
| 607 | ret->remove_buddy = twitter_remove_buddy; |
---|
| 608 | ret->chat_msg = twitter_chat_msg; |
---|
| 609 | ret->chat_invite = twitter_chat_invite; |
---|
| 610 | ret->chat_leave = twitter_chat_leave; |
---|
| 611 | ret->keepalive = twitter_keepalive; |
---|
| 612 | ret->add_permit = twitter_add_permit; |
---|
| 613 | ret->rem_permit = twitter_rem_permit; |
---|
| 614 | ret->add_deny = twitter_add_deny; |
---|
| 615 | ret->rem_deny = twitter_rem_deny; |
---|
[203a2d2] | 616 | ret->buddy_data_add = twitter_buddy_data_add; |
---|
| 617 | ret->buddy_data_free = twitter_buddy_data_free; |
---|
[1b221e0] | 618 | ret->handle_cmp = g_strcasecmp; |
---|
[5983eca] | 619 | |
---|
[ffcdf13] | 620 | register_protocol(ret); |
---|
[1b221e0] | 621 | |
---|
[ffcdf13] | 622 | /* And an identi.ca variant: */ |
---|
| 623 | ret = g_memdup(ret, sizeof(struct prpl)); |
---|
| 624 | ret->name = "identica"; |
---|
[1b221e0] | 625 | register_protocol(ret); |
---|
| 626 | } |
---|