Changeset 991c75f for protocols


Ignore:
Timestamp:
2016-04-16T17:21:33Z (8 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
a244877
Parents:
c9603a3 (diff), 166a571 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge remote-tracking branch 'bitlbee/pr/70'

Location:
protocols/twitter
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter.c

    rc9603a3 r991c75f  
    345345                imcb_log(ic, "Getting contact list");
    346346                twitter_get_friends_ids(ic, -1);
     347                twitter_get_mutes_ids(ic, -1);
     348                twitter_get_noretweets_ids(ic, -1);
    347349        } else {
    348350                twitter_main_loop_start(ic);
     
    955957        } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) {
    956958                twitter_remove_buddy(ic, cmd[1], NULL);
     959                goto eof;
     960        } else if (g_strcasecmp(cmd[0], "mute") == 0 && cmd[1]) {
     961                twitter_mute_create_destroy(ic, cmd[1], 1);
     962                goto eof;
     963        } else if (g_strcasecmp(cmd[0], "unmute") == 0 && cmd[1]) {
     964                twitter_mute_create_destroy(ic, cmd[1], 0);
    957965                goto eof;
    958966        } else if ((g_strcasecmp(cmd[0], "report") == 0 ||
  • protocols/twitter/twitter.h

    rc9603a3 r991c75f  
    6161
    6262        GSList *follow_ids;
     63        GSList *mutes_ids;
     64        GSList *noretweets_ids;
    6365        GSList *filters;
    6466
  • protocols/twitter/twitter_lib.c

    rc9603a3 r991c75f  
    240240
    241241static void twitter_http_get_friends_ids(struct http_request *req);
     242static void twitter_http_get_mutes_ids(struct http_request *req);
     243static void twitter_http_get_noretweets_ids(struct http_request *req);
    242244
    243245/**
     
    252254        args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor);
    253255        twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2);
     256
     257        g_free(args[1]);
     258}
     259
     260/**
     261 * Get the muted users ids.
     262 */
     263void 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 */
     277void 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);
    254284
    255285        g_free(args[1]);
     
    332362                twitter_get_users_lookup(ic);
    333363        }
     364
     365        txl->list = NULL;
     366        txl_free(txl);
     367}
     368
     369/**
     370 * Callback for getting the mutes ids.
     371 */
     372static void twitter_http_get_mutes_ids(struct http_request *req)
     373{
     374        struct im_connection *ic = req->data;
     375        json_value *parsed;
     376        struct twitter_xml_list *txl;
     377        struct twitter_data *td;
     378
     379        // Check if the connection is stil active
     380        if (!g_slist_find(twitter_connections, ic)) {
     381                return;
     382        }
     383
     384        td = ic->proto_data;
     385
     386        // Parse the data.
     387        if (!(parsed = twitter_parse_response(ic, req))) {
     388                return;
     389        }
     390
     391        txl = g_new0(struct twitter_xml_list, 1);
     392        txl->list = td->mutes_ids;
     393
     394        /* mute ids API response is similar enough to friends response
     395           to reuse this method */
     396        twitter_xt_get_friends_id_list(parsed, txl);
     397        json_value_free(parsed);
     398
     399        td->mutes_ids = txl->list;
     400        if (txl->next_cursor) {
     401                /* Recurse while there are still more pages */
     402                twitter_get_mutes_ids(ic, txl->next_cursor);
     403        }
     404
     405        txl->list = NULL;
     406        txl_free(txl);
     407}
     408
     409/**
     410 * Callback for getting the no-retweets ids.
     411 */
     412static void twitter_http_get_noretweets_ids(struct http_request *req)
     413{
     414        struct im_connection *ic = req->data;
     415        json_value *parsed;
     416        struct twitter_xml_list *txl;
     417        struct twitter_data *td;
     418
     419        // Check if the connection is stil active
     420        if (!g_slist_find(twitter_connections, ic)) {
     421                return;
     422        }
     423
     424        td = ic->proto_data;
     425
     426        // Parse the data.
     427        if (!(parsed = twitter_parse_response(ic, req))) {
     428                return;
     429        }
     430
     431        txl = g_new0(struct twitter_xml_list, 1);
     432        txl->list = td->noretweets_ids;
     433       
     434        // Process the retweet ids
     435        txl->type = TXL_ID;
     436        if (parsed->type == json_array) {
     437                unsigned int i;
     438                for (i = 0; i < parsed->u.array.length; i++) {
     439                        json_value *c = parsed->u.array.values[i];
     440                        if (c->type != json_integer) {
     441                                continue;
     442                        }
     443                        txl->list = g_slist_prepend(txl->list,
     444                                                    g_strdup_printf("%"PRIu64, c->u.integer));
     445                }
     446        }
     447
     448        json_value_free(parsed);
     449        td->noretweets_ids = txl->list;
    334450
    335451        txl->list = NULL;
     
    831947        struct twitter_data *td = ic->proto_data;
    832948        char *last_id_str;
     949        char *uid_str;
    833950
    834951        if (status->user == NULL || status->text == NULL) {
     952                return;
     953        }
     954       
     955        /* Check this is not a tweet that should be muted */
     956        uid_str = g_strdup_printf("%" PRIu64, status->user->uid);
     957        if (getenv("BITLBEE_DEBUG")) {
     958                GSList *item;
     959                fprintf(stderr, "Checking mutes; this uid=%s\n", uid_str);
     960                for (item = td->mutes_ids; item != NULL; item = item->next) {
     961                        fprintf(stderr, "  id: %s\n", (char *)item->data);
     962                }
     963        }
     964        if (g_slist_find_custom(td->mutes_ids, uid_str, (GCompareFunc)strcmp)) {
     965                g_free(uid_str);
     966                return;
     967        }
     968        if (status->id != status->rt_id && g_slist_find_custom(td->noretweets_ids, uid_str, (GCompareFunc)strcmp)) {
     969                g_free(uid_str);
    835970                return;
    836971        }
     
    857992        set_setstr(&ic->acc->set, "_last_tweet", last_id_str);
    858993        g_free(last_id_str);
     994        g_free(uid_str);
    859995}
    860996
     
    9931129        json_value *target = json_o_get(o, "target");
    9941130        const char *type = json_o_str(o, "event");
     1131        struct twitter_xml_user *us = NULL;
     1132        struct twitter_xml_user *ut = NULL;
    9951133
    9961134        if (!type || !source || source->type != json_object
     
    10001138
    10011139        if (strcmp(type, "follow") == 0) {
    1002                 struct twitter_xml_user *us = twitter_xt_get_user(source);
    1003                 struct twitter_xml_user *ut = twitter_xt_get_user(target);
     1140                us = twitter_xt_get_user(source);
     1141                ut = twitter_xt_get_user(target);
    10041142                if (g_strcasecmp(us->screen_name, td->user) == 0) {
    10051143                        twitter_add_buddy(ic, ut->screen_name, ut->name);
    10061144                }
    1007                 txu_free(us);
    1008                 txu_free(ut);
    1009         }
     1145        } else if (strcmp(type, "mute") == 0) {
     1146                GSList *found;
     1147                char *uid_str;
     1148                ut = twitter_xt_get_user(target);
     1149                uid_str = g_strdup_printf("%" PRIu64, ut->uid);
     1150                if (!(found = g_slist_find_custom(td->mutes_ids, uid_str,
     1151                                                  (GCompareFunc)strcmp))) {
     1152                        td->mutes_ids = g_slist_prepend(td->mutes_ids, uid_str);
     1153                }
     1154                twitter_log(ic, "Muted user %s", ut->screen_name);
     1155                if (getenv("BITLBEE_DEBUG")) {
     1156                        fprintf(stderr, "New mute: %s %"PRIu64"\n",
     1157                                ut->screen_name, ut->uid);
     1158                }
     1159        } else if (strcmp(type, "unmute") == 0) {
     1160                GSList *found;
     1161                char *uid_str;
     1162                ut = twitter_xt_get_user(target);
     1163                uid_str = g_strdup_printf("%" PRIu64, ut->uid);
     1164                if ((found = g_slist_find_custom(td->mutes_ids, uid_str,
     1165                                                (GCompareFunc)strcmp))) {
     1166                        td->mutes_ids = g_slist_remove(td->mutes_ids, found);
     1167                }
     1168                g_free(uid_str);
     1169                twitter_log(ic, "Unmuted user %s", ut->screen_name);
     1170                if (getenv("BITLBEE_DEBUG")) {
     1171                        fprintf(stderr, "New unmute: %s %"PRIu64"\n",
     1172                                ut->screen_name, ut->uid);
     1173                }
     1174        }
     1175
     1176        txu_free(us);
     1177        txu_free(ut);
    10101178
    10111179        return TRUE;
     
    15241692}
    15251693
     1694/**
     1695 * Mute or unmute a user
     1696 */
     1697void twitter_mute_create_destroy(struct im_connection *ic, char *who, int create)
     1698{
     1699        char *args[2];
     1700
     1701        args[0] = "screen_name";
     1702        args[1] = who;
     1703        twitter_http(ic, create ? TWITTER_MUTES_CREATE_URL : TWITTER_MUTES_DESTROY_URL,
     1704                     twitter_http_post, ic, 1, args, 2);
     1705}
     1706
    15261707void twitter_status_destroy(struct im_connection *ic, guint64 id)
    15271708{
  • protocols/twitter/twitter_lib.h

    rc9603a3 r991c75f  
    6363#define TWITTER_FRIENDS_IDS_URL "/friends/ids.json"
    6464#define TWITTER_FOLLOWERS_IDS_URL "/followers/ids.json"
     65#define TWITTER_MUTES_IDS_URL "/mutes/users/ids.json"
     66#define TWITTER_NORETWEETS_IDS_URL "/friendships/no_retweets/ids.json"
    6567
    6668/* Account URLs */
     
    7678#define TWITTER_BLOCKS_DESTROY_URL "/blocks/destroy/"
    7779
     80/* Mute URLs */
     81#define TWITTER_MUTES_CREATE_URL "/mutes/users/create.json"
     82#define TWITTER_MUTES_DESTROY_URL "/mutes/users/destroy.json"
     83
    7884/* Report spam */
    7985#define TWITTER_REPORT_SPAM_URL "/users/report_spam.json"
     
    8793gboolean twitter_get_timeline(struct im_connection *ic, gint64 next_cursor);
    8894void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor);
     95void twitter_get_mutes_ids(struct im_connection *ic, gint64 next_cursor);
     96void twitter_get_noretweets_ids(struct im_connection *ic, gint64 next_cursor);
    8997void twitter_get_statuses_friends(struct im_connection *ic, gint64 next_cursor);
    9098
     
    92100void twitter_direct_messages_new(struct im_connection *ic, char *who, char *message);
    93101void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create);
     102void twitter_mute_create_destroy(struct im_connection *ic, char *who, int create);
    94103void twitter_status_destroy(struct im_connection *ic, guint64 id);
    95104void twitter_status_retweet(struct im_connection *ic, guint64 id);
Note: See TracChangeset for help on using the changeset viewer.