aboutsummaryrefslogtreecommitdiffstats
path: root/protocols
diff options
context:
space:
mode:
authorMarius Halden <marius.h@lden.org>2016-08-01 13:51:12 +0200
committerMarius Halden <marius.h@lden.org>2016-08-01 13:51:12 +0200
commitf02b57f2ed850f163c068ecaa144640e9f8e011a (patch)
tree2a94cffad6bee18f959ba561b55f9eadadd78c6b /protocols
parent14dc4bb2fec1422721dc038bd9a4412c8bb8e67f (diff)
Basic support for blocks
Diffstat (limited to 'protocols')
-rw-r--r--protocols/twitter/twitter.c11
-rw-r--r--protocols/twitter/twitter.h1
-rw-r--r--protocols/twitter/twitter_lib.c104
-rw-r--r--protocols/twitter/twitter_lib.h7
4 files changed, 120 insertions, 3 deletions
diff --git a/protocols/twitter/twitter.c b/protocols/twitter/twitter.c
index f374c33d..c1f54681 100644
--- a/protocols/twitter/twitter.c
+++ b/protocols/twitter/twitter.c
@@ -352,6 +352,7 @@ void twitter_login_finish(struct im_connection *ic)
imcb_log(ic, "Getting contact list");
twitter_get_friends_ids(ic, -1);
twitter_get_mutes_ids(ic, -1);
+ twitter_get_blocks_ids(ic, -1);
twitter_get_noretweets_ids(ic, -1);
} else {
twitter_main_loop_start(ic);
@@ -738,6 +739,9 @@ static void twitter_logout(struct im_connection *ic)
g_slist_foreach(td->mutes_ids, (GFunc) g_free, NULL);
g_slist_free(td->mutes_ids);
+ g_slist_foreach(td->blocks_ids, (GFunc) g_free, NULL);
+ g_slist_free(td->blocks_ids);
+
g_slist_foreach(td->noretweets_ids, (GFunc) g_free, NULL);
g_slist_free(td->noretweets_ids);
@@ -1102,8 +1106,15 @@ static void twitter_handle_command(struct im_connection *ic, char *message)
} else if (g_strcasecmp(cmd[0], "rton") == 0 && cmd[1]) {
twitter_retweet_enable_disable(ic, cmd[1], 1);
goto eof;
+ } else if (g_strcasecmp(cmd[0], "block") == 0 && cmd[1]) {
+ twitter_block_create_destroy(ic, cmd[1], 1);
+ goto eof;
+ } else if (g_strcasecmp(cmd[0], "unblock") == 0 && cmd[1]) {
+ twitter_block_create_destroy(ic, cmd[1], 0);
+ goto eof;
}
+
if (allow_post) {
char *s;
diff --git a/protocols/twitter/twitter.h b/protocols/twitter/twitter.h
index 6b7c0c0c..8d17da58 100644
--- a/protocols/twitter/twitter.h
+++ b/protocols/twitter/twitter.h
@@ -61,6 +61,7 @@ struct twitter_data {
GSList *follow_ids;
GSList *mutes_ids;
+ GSList *blocks_ids;
GSList *noretweets_ids;
GSList *filters;
diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c
index 011c31ab..0201aff2 100644
--- a/protocols/twitter/twitter_lib.c
+++ b/protocols/twitter/twitter_lib.c
@@ -240,6 +240,7 @@ static json_value *twitter_parse_response(struct im_connection *ic, struct http_
static void twitter_http_get_friends_ids(struct http_request *req);
static void twitter_http_get_mutes_ids(struct http_request *req);
+static void twitter_http_get_blocks_ids(struct http_request *req);
static void twitter_http_get_noretweets_ids(struct http_request *req);
/**
@@ -272,6 +273,20 @@ void twitter_get_mutes_ids(struct im_connection *ic, gint64 next_cursor)
}
/**
+ * Get the blocked users ids.
+ */
+void twitter_get_blocks_ids(struct im_connection *ic, gint64 next_cursor)
+{
+ char *args[2];
+
+ args[0] = "cursor";
+ args[1] = g_strdup_printf("%" G_GINT64_FORMAT, next_cursor);
+ twitter_http(ic, TWITTER_BLOCKS_IDS_URL, twitter_http_get_blocks_ids, ic, 0, args, 2);
+
+ g_free(args[1]);
+}
+
+/**
* Get the ids for users from whom we should ignore retweets.
*/
void twitter_get_noretweets_ids(struct im_connection *ic, gint64 next_cursor)
@@ -412,6 +427,48 @@ static void twitter_http_get_mutes_ids(struct http_request *req)
}
/**
+ * Callback for getting the blocks ids.
+ */
+static void twitter_http_get_blocks_ids(struct http_request *req)
+{
+ struct im_connection *ic = req->data;
+ json_value *parsed;
+ struct twitter_xml_list *txl;
+ struct twitter_data *td;
+
+ // Check if the connection is still active
+ if (!g_slist_find(twitter_connections, ic)) {
+ return;
+ }
+
+ td = ic->proto_data;
+
+ if (req->status_code != 200) {
+ /* Fail silently */
+ return;
+ }
+
+ // Parse the data.
+ if (!(parsed = twitter_parse_response(ic, req))) {
+ return;
+ }
+
+ txl = g_new0(struct twitter_xml_list, 1);
+ txl->list = td->blocks_ids;
+
+ twitter_xt_get_friends_id_list(parsed, txl);
+ json_value_free(parsed);
+
+ td->blocks_ids = txl->list;
+ if (txl->next_cursor) {
+ twitter_get_blocks_ids(ic, txl->next_cursor);
+ }
+
+ txl->list = NULL;
+ txl_free(txl);
+}
+
+/**
* Callback for getting the no-retweets ids.
*/
static void twitter_http_get_noretweets_ids(struct http_request *req)
@@ -978,7 +1035,8 @@ static void twitter_status_show(struct im_connection *ic, struct twitter_xml_sta
/* Check this is not a tweet that should be muted */
uid_str = g_strdup_printf("%" PRIu64, status->user->uid);
- if (g_slist_find_custom(td->mutes_ids, uid_str, (GCompareFunc)strcmp)) {
+ if (g_slist_find_custom(td->mutes_ids, uid_str, (GCompareFunc)strcmp) ||
+ g_slist_find_custom(td->blocks_ids, uid_str, (GCompareFunc)strcmp)) {
g_free(uid_str);
return;
}
@@ -1176,6 +1234,37 @@ static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value
if (g_strcasecmp(us->screen_name, td->user) == 0) {
twitter_add_buddy(ic, ut->screen_name, ut->name);
}
+ } else if (strcmp(type, "block") == 0) {
+ GSList *found;
+ char *uid_str;
+ ut = twitter_xt_get_user(target);
+ uid_str = g_strdup_printf("%" PRIu64, ut->uid);
+ if (!(found = g_slist_find_custom(td->blocks_ids, uid_str,
+ (GCompareFunc)strcmp))) {
+ td->blocks_ids = g_slist_prepend(td->blocks_ids, uid_str);
+ }
+ twitter_log(ic, "Blocked user %s", ut->screen_name);
+ if (getenv("BITLBEE_DEBUG")) {
+ fprintf(stderr, "New block: %s %"PRIu64"\n",
+ ut->screen_name, ut->uid);
+ }
+ } else if (strcmp(type, "unblock") == 0) {
+ GSList *found;
+ char *uid_str;
+ ut = twitter_xt_get_user(target);
+ uid_str = g_strdup_printf("%" PRIu64, ut->uid);
+ if ((found = g_slist_find_custom(td->blocks_ids, uid_str,
+ (GCompareFunc)strcmp))) {
+ char *found_str = found->data;
+ td->blocks_ids = g_slist_delete_link(td->blocks_ids, found);
+ g_free(found_str);
+ }
+ g_free(uid_str);
+ twitter_log(ic, "Unblocked user %s", ut->screen_name);
+ if (getenv("BITLBEE_DEBUG")) {
+ fprintf(stderr, "New unblock: %s %"PRIu64"\n",
+ ut->screen_name, ut->uid);
+ }
} else if (strcmp(type, "mute") == 0) {
GSList *found;
char *uid_str;
@@ -1743,6 +1832,19 @@ void twitter_mute_create_destroy(struct im_connection *ic, char *who, int create
}
/**
+ * Block or unblock a user
+ */
+void twitter_block_create_destroy(struct im_connection *ic, char *who, int create)
+{
+ char *args[2];
+
+ args[0] = "screen_name";
+ args[1] = who;
+ twitter_http(ic, create ? TWITTER_BLOCKS_CREATE_URL : TWITTER_BLOCKS_DESTROY_URL,
+ twitter_http_post, ic, 1, args, 2);
+}
+
+/**
* Enable or disable retweets for user
*/
void twitter_retweet_enable_disable(struct im_connection *ic, char *who, int enable)
diff --git a/protocols/twitter/twitter_lib.h b/protocols/twitter/twitter_lib.h
index e520e8b5..88396a1e 100644
--- a/protocols/twitter/twitter_lib.h
+++ b/protocols/twitter/twitter_lib.h
@@ -65,6 +65,7 @@
#define TWITTER_FOLLOWERS_IDS_URL "/followers/ids.json"
#define TWITTER_MUTES_IDS_URL "/mutes/users/ids.json"
#define TWITTER_NORETWEETS_IDS_URL "/friendships/no_retweets/ids.json"
+#define TWITTER_BLOCKS_IDS_URL "/blocks/ids.json"
/* Account URLs */
#define TWITTER_ACCOUNT_RATE_LIMIT_URL "/account/rate_limit_status.json"
@@ -75,8 +76,8 @@
#define TWITTER_FAVORITE_DESTROY_URL "/favorites/destroy.json"
/* Block URLs */
-#define TWITTER_BLOCKS_CREATE_URL "/blocks/create/"
-#define TWITTER_BLOCKS_DESTROY_URL "/blocks/destroy/"
+#define TWITTER_BLOCKS_CREATE_URL "/blocks/create.json"
+#define TWITTER_BLOCKS_DESTROY_URL "/blocks/destroy.json"
/* Mute URLs */
#define TWITTER_MUTES_CREATE_URL "/mutes/users/create.json"
@@ -94,6 +95,7 @@ gboolean twitter_open_filter_stream(struct im_connection *ic);
gboolean twitter_get_timeline(struct im_connection *ic, gint64 next_cursor);
void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor);
void twitter_get_mutes_ids(struct im_connection *ic, gint64 next_cursor);
+void twitter_get_blocks_ids(struct im_connection *ic, gint64 next_cursor);
void twitter_get_noretweets_ids(struct im_connection *ic, gint64 next_cursor);
void twitter_get_statuses_friends(struct im_connection *ic, gint64 next_cursor);
@@ -101,6 +103,7 @@ void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_t
void twitter_direct_messages_new(struct im_connection *ic, char *who, char *message);
void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create);
void twitter_mute_create_destroy(struct im_connection *ic, char *who, int create);
+void twitter_block_create_destroy(struct im_connection *ic, char *who, int create);
void twitter_retweet_enable_disable(struct im_connection *ic, char *who, int enable);
void twitter_status_destroy(struct im_connection *ic, guint64 id);
void twitter_status_retweet(struct im_connection *ic, guint64 id);