Skip to content

Commit

Permalink
Hook up get_history to buddy menu
Browse files Browse the repository at this point in the history
Unfortunately, the menu only seems to be for actual buddies, not
chats...
  • Loading branch information
dylex committed Aug 17, 2017
1 parent e3c2587 commit e281475
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 28 deletions.
73 changes: 60 additions & 13 deletions slack-blist.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <string.h>

#include <request.h>
#include <debug.h>

#include "slack-json.h"
#include "slack-channel.h"
#include "slack-user.h"
#include "slack-api.h"
#include "slack-message.h"
#include "slack-blist.h"

void slack_blist_uncache(SlackAccount *sa, PurpleBlistNode *b) {
Expand All @@ -25,8 +27,8 @@ void slack_blist_cache(SlackAccount *sa, PurpleBlistNode *b, const char *id) {

void slack_buddy_free(PurpleBuddy *b) {
/* This should be unnecessary, as there's no analogue for PurpleChat so we have to deal with cleanup elsewhere anyway */
if (b->account->gc && b->account->gc->proto_data)
slack_blist_uncache(b->account->gc->proto_data, &b->node);
SlackAccount *sa = get_slack_account(b->account);
if (sa) slack_blist_uncache(sa, &b->node);
}

#define PURPLE_BLIST_ACCOUNT(n) \
Expand All @@ -36,6 +38,17 @@ void slack_buddy_free(PurpleBuddy *b) {
? PURPLE_CHAT(n)->account \
: NULL)

SlackObject *slack_blist_node_get_obj(PurpleBlistNode *buddy, SlackAccount **sap) {
*sap = get_slack_account(PURPLE_BLIST_ACCOUNT(buddy));
if (!*sap)
return NULL;
if (PURPLE_BLIST_NODE_IS_BUDDY(buddy))
return g_hash_table_lookup((*sap)->user_names, purple_buddy_get_name(PURPLE_BUDDY(buddy)));
else if (PURPLE_BLIST_NODE_IS_CHAT(buddy))
return g_hash_table_lookup((*sap)->channel_names, purple_chat_get_name(PURPLE_CHAT(buddy)));
return NULL;
}

void slack_blist_init(SlackAccount *sa) {
char *id = sa->team.id ?: "";
if (!sa->blist) {
Expand Down Expand Up @@ -71,17 +84,51 @@ void slack_blist_init(SlackAccount *sa) {
}

PurpleChat *slack_find_blist_chat(PurpleAccount *account, const char *name) {
if (account->gc && account->gc->proto_data) {
SlackAccount *sa = account->gc->proto_data;
if (sa->channel_names) {
SlackChannel *chan = g_hash_table_lookup(sa->channel_names, name);
if (chan && chan->buddy)
return chan->buddy;
}
SlackAccount *sa = get_slack_account(account);
if (sa && sa->channel_names) {
SlackChannel *chan = g_hash_table_lookup(sa->channel_names, name);
if (chan && chan->buddy)
return chan->buddy;
}
return purple_blist_find_chat(account, name);
}

static void get_history_cb(PurpleBlistNode *buddy, PurpleRequestFields *fields) {
SlackAccount *sa;
SlackObject *obj = slack_blist_node_get_obj(buddy, &sa);
g_return_if_fail(obj);

slack_get_history(sa, obj, NULL, purple_request_fields_get_integer(fields, "count"));
}

static void get_history_prompt(PurpleBlistNode *buddy) {
SlackAccount *sa = get_slack_account(PURPLE_BLIST_ACCOUNT(buddy));
const char *name = PURPLE_BLIST_NODE_NAME(buddy);
g_return_if_fail(sa && name);

PurpleRequestFields *fields = purple_request_fields_new();
PurpleRequestFieldGroup *group = purple_request_field_group_new("NULL");
PurpleRequestField *field = purple_request_field_int_new("count", "Count", 100);
purple_request_field_set_required(field, TRUE);
purple_request_field_group_add_field(group, field);
purple_request_fields_add_group(fields, group);
gchar *primary = g_strdup_printf("Retrieve message history for %c%s", PURPLE_BLIST_NODE_IS_BUDDY(buddy) ? '@' : '#', name);
PurpleConversation *conv = purple_find_conversation_with_account(PURPLE_BLIST_NODE_IS_BUDDY(buddy) ? PURPLE_CONV_TYPE_IM : PURPLE_CONV_TYPE_CHAT, name, sa->account);
purple_request_fields(sa->gc, "Get History", primary, NULL, fields, "Get", G_CALLBACK(get_history_cb), "Cancel", NULL, sa->account, name, conv, buddy);
g_free(primary);
}

GList *slack_blist_node_menu(PurpleBlistNode *buddy) {
GList *menu = NULL;
SlackAccount *sa = get_slack_account(PURPLE_BLIST_ACCOUNT(buddy));

if (sa) {
menu = g_list_append(menu, purple_menu_action_new("Get history", G_CALLBACK(get_history_prompt), buddy, NULL));
}

return menu;
}

struct roomlist_expand {
PurpleRoomlist *list;
PurpleRoomlistRoom *parent;
Expand Down Expand Up @@ -127,9 +174,9 @@ static void roomlist_cb(SlackAccount *sa, gpointer data, json_value *json, const
}

void slack_roomlist_expand_category(PurpleRoomlist *list, PurpleRoomlistRoom *parent) {
if (!list->account || !list->account->gc || !list->account->gc->proto_data || strcmp(list->account->protocol_id, SLACK_PLUGIN_ID))
SlackAccount *sa = get_slack_account(list->account);
if (!sa)
return;
SlackAccount *sa = list->account->gc->proto_data;

g_warn_if_fail(list == sa->roomlist);

Expand Down Expand Up @@ -189,9 +236,9 @@ PurpleRoomlist *slack_roomlist_get_list(PurpleConnection *gc) {
}

void slack_roomlist_cancel(PurpleRoomlist *list) {
if (!list->account || !list->account->gc || !list->account->gc->proto_data || strcmp(list->account->protocol_id, SLACK_PLUGIN_ID))
SlackAccount *sa = get_slack_account(list->account);
if (!sa)
return;
SlackAccount *sa = list->account->gc->proto_data;

if (sa->roomlist == list) {
purple_roomlist_unref(list);
Expand Down
3 changes: 3 additions & 0 deletions slack-blist.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
#define _PURPLE_SLACK_BLIST_H

#include "slack.h"
#include "slack-object.h"

/* the key we store the channel ID in */
#define SLACK_BLIST_KEY "slack"

void slack_blist_uncache(SlackAccount *sa, PurpleBlistNode *b);
void slack_blist_cache(SlackAccount *sa, PurpleBlistNode *b, const char *id);
SlackObject *slack_blist_node_get_obj(PurpleBlistNode *b, SlackAccount **);

/* Initialization */
void slack_blist_init(SlackAccount *sa);

/* Purple protocol handlers */
PurpleChat *slack_find_blist_chat(PurpleAccount *account, const char *name);
GList *slack_blist_node_menu(PurpleBlistNode *buddy);
PurpleRoomlist *slack_roomlist_get_list(PurpleConnection *gc);
void slack_roomlist_expand_category(PurpleRoomlist *list, PurpleRoomlistRoom *parent);
void slack_roomlist_cancel(PurpleRoomlist *list);
Expand Down
6 changes: 4 additions & 2 deletions slack-message.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static gchar *slack_message_to_html(SlackAccount *sa, gchar *s, const char *subt
case '!':
s++;
if (!strcmp(s, "channel") || !strcmp(s, "group") || !strcmp(s, "here") || !strcmp(s, "everyone")) {
*flags |= PURPLE_MESSAGE_NOTIFY;
*flags |= PURPLE_MESSAGE_NICK;
g_string_append_c(html, '@');
g_string_append(html, b ?: s);
} else {
Expand Down Expand Up @@ -234,7 +234,7 @@ static void get_history_cb(SlackAccount *sa, gpointer data, json_value *json, co
if (g_strcmp0(json_get_prop_strptr(msg, "type"), "message"))
continue;

handle_message(sa, obj, msg, PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_DELAYED);
handle_message(sa, obj, msg, PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_DELAYED | PURPLE_MESSAGE_NO_LOG);
}

g_object_unref(obj);
Expand All @@ -244,6 +244,8 @@ void slack_get_history(SlackAccount *sa, SlackObject *obj, const char *since, un
const char *call = NULL, *id = NULL;
if (SLACK_IS_CHANNEL(obj)) {
SlackChannel *chan = (SlackChannel*)obj;
if (!chan->cid)
slack_chat_open(sa, chan);
switch (chan->type) {
case SLACK_CHANNEL_MEMBER:
call = "channels.history";
Expand Down
1 change: 1 addition & 0 deletions slack-message.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "slack.h"

gchar *slack_html_to_message(SlackAccount *sa, const char *s, PurpleMessageFlags flags);
void slack_get_history(SlackAccount *sa, SlackObject *obj, const char *since, unsigned count);

/* RTM event handlers */
void slack_message(SlackAccount *sa, json_value *json);
Expand Down
16 changes: 5 additions & 11 deletions slack-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "slack-json.h"
#include "slack-api.h"
#include "slack-blist.h"
#include "slack-user.h"
#include "slack-im.h"

Expand Down Expand Up @@ -129,17 +130,10 @@ void slack_presence_change(SlackAccount *sa, json_value *json) {
}

char *slack_status_text(PurpleBuddy *buddy) {
PurpleConnection *gc = buddy->account->gc;
if (!gc)
return NULL;
SlackAccount *sa = gc->proto_data;
g_return_val_if_fail(sa, NULL);
/*
const char *bid = purple_blist_node_get_string(buddy, SLACK_BLIST_KEY);
if (bid)
user = slack_object_hash_table_lookup(sa->users, bid);
*/
SlackUser *user = g_hash_table_lookup(sa->user_names, purple_buddy_get_name(buddy));
SlackAccount *sa;
SlackObject *obj = slack_blist_node_get_obj(PURPLE_BLIST_NODE(buddy), &sa);
g_return_val_if_fail(SLACK_IS_USER(obj), NULL);
SlackUser *user = (SlackUser*)obj;
return user ? g_strdup(user->status) : NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions slack.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static PurplePluginProtocolInfo prpl_info = {
slack_status_text, /* status_text */
NULL, /* tooltip_text */
slack_status_types, /* status_types */
NULL, /* blist_node_menu */
slack_blist_node_menu, /* blist_node_menu */
slack_chat_info, /* chat_info */
slack_chat_info_defaults, /* chat_info_defaults */
slack_login, /* login */
Expand Down Expand Up @@ -231,7 +231,7 @@ static PurplePluginProtocolInfo prpl_info = {
NULL, /* send_attention */
NULL, /* attention_types */
sizeof(PurplePluginProtocolInfo), /* struct_size */
NULL, /*campfire_get_account_text_table *//* get_account_text_table */
NULL, /* get_account_text_table */
NULL, /* initiate_media */
NULL, /* get_media_caps */
NULL, /* get_moods */
Expand Down
8 changes: 8 additions & 0 deletions slack.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _PURPLE_SLACK_H
#define _PURPLE_SLACK_H

#include <string.h>

#include <account.h>

#include "purple-websocket.h"
Expand Down Expand Up @@ -42,4 +44,10 @@ typedef struct _SlackAccount {

GHashTable *slack_chat_info_defaults(PurpleConnection *gc, const char *name);

static inline SlackAccount *get_slack_account(PurpleAccount *account) {
if (!account || !account->gc || strcmp(account->protocol_id, SLACK_PLUGIN_ID))
return NULL;
return account->gc->proto_data;
}

#endif // _PURPLE_SLACK_H

0 comments on commit e281475

Please sign in to comment.