forked from necrosis/slack-libpurple
-
Notifications
You must be signed in to change notification settings - Fork 41
/
slack-blist.c
235 lines (196 loc) · 8.68 KB
/
slack-blist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#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-conversation.h"
#include "slack-blist.h"
void slack_blist_uncache(SlackAccount *sa, PurpleBlistNode *b) {
const char *bid = purple_blist_node_get_string(b, SLACK_BLIST_KEY);
if (bid)
g_hash_table_remove(sa->buddies, bid);
purple_blist_node_remove_setting(b, SLACK_BLIST_KEY);
}
void slack_blist_cache(SlackAccount *sa, PurpleBlistNode *b, const char *id) {
if (id)
purple_blist_node_set_string(b, SLACK_BLIST_KEY, id);
const char *bid = purple_blist_node_get_string(b, SLACK_BLIST_KEY);
if (bid)
g_hash_table_insert(sa->buddies, (gpointer)bid, b);
}
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 */
SlackAccount *sa = get_slack_account(b->account);
if (sa) slack_blist_uncache(sa, &b->node);
}
#define PURPLE_BLIST_ACCOUNT(n) \
( PURPLE_BLIST_NODE_IS_BUDDY(n) \
? PURPLE_BUDDY(n)->account \
: PURPLE_BLIST_NODE_IS_CHAT(n) \
? PURPLE_CHAT(n)->account \
: NULL)
static const char *get_chat_name(PurpleChat *chat)
{
return g_hash_table_lookup(purple_chat_get_components(chat), "name");
}
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, get_chat_name(PURPLE_CHAT(buddy)));
return NULL;
}
void slack_blist_init(SlackAccount *sa) {
char *id = sa->team.id ?: "";
if (!sa->blist) {
PurpleBlistNode *g;
for (g = purple_blist_get_root(); g; g = purple_blist_node_next(g, TRUE)) {
const char *bid;
if (PURPLE_BLIST_NODE_IS_GROUP(g) &&
(bid = purple_blist_node_get_string(g, SLACK_BLIST_KEY)) &&
!strcmp(bid, id)) {
sa->blist = PURPLE_GROUP(g);
break;
}
}
if (!sa->blist) {
sa->blist = purple_group_new(sa->team.name ?: "Slack");
purple_blist_node_set_string(&sa->blist->node, SLACK_BLIST_KEY, id);
purple_blist_add_group(sa->blist, NULL);
}
}
/* Find all leaf nodes on this account (buddies and chats) with slack ids and cache them */
PurpleBlistNode *node;
for (node = purple_blist_get_root(); node; node = node->next) {
while (node->child)
node = node->child;
if (PURPLE_BLIST_ACCOUNT(node) == sa->account)
slack_blist_cache(sa, node, NULL);
while (node->parent && !node->next)
node = node->parent;
}
}
PurpleChat *slack_find_blist_chat(PurpleAccount *account, const char *name) {
SlackAccount *sa = get_slack_account(account);
if (sa && sa->channel_names) {
SlackChannel *chan = g_hash_table_lookup(sa->channel_names, name);
if (chan && chan->object.buddy)
return channel_buddy(chan);
}
return NULL;
}
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);
int count = purple_request_fields_get_integer(fields, "count");
if (count > 0)
slack_get_history(sa, obj, NULL, count, NULL, FALSE);
else
slack_get_conversation_unread(sa, obj);
}
static void get_history_prompt(PurpleBlistNode *buddy) {
SlackAccount *sa = get_slack_account(PURPLE_BLIST_ACCOUNT(buddy));
const char *name = PURPLE_BLIST_NODE_IS_BUDDY(buddy) ? PURPLE_BLIST_NODE_NAME(buddy) : get_chat_name(PURPLE_CHAT(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 (0 for unread)", 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;
};
#define ROOMLIST_CALL(sa, expand, ARGS...) \
slack_api_get(sa, roomlist_cb, expand, "conversations.list", "exclude_archived", expand->parent ? "false" : "true", "type", "public_channel,private_channel,mpim,im", SLACK_PAGINATE_LIMIT_ARG, ##ARGS, NULL)
static gboolean roomlist_cb(SlackAccount *sa, gpointer data, json_value *json, const char *error) {
struct roomlist_expand *expand = data;
char *cursor = json_get_prop_strptr(json_get_prop(json, "response_metadata"), "next_cursor");
json = json_get_prop_type(json, "channels", array);
if (sa->roomlist_stop)
json = NULL;
else if (!json || error) {
purple_notify_error(sa->gc, "Channel list error", "Could not read channel list", error);
json = NULL;
}
else
for (unsigned i = 0; i < json->u.array.length; i++) {
json_value *chan = json->u.array.values[i];
gboolean archived = json_get_prop_boolean(chan, "is_archived", FALSE) || json_get_prop_boolean(chan, "is_deleted", FALSE);
if (expand->parent && !archived)
continue;
PurpleRoomlistRoom *room = purple_roomlist_room_new(PURPLE_ROOMLIST_ROOMTYPE_ROOM, json_get_prop_strptr(chan, "name"), expand->parent);
purple_roomlist_room_add_field(expand->list, room, json_get_prop_strptr(chan, "id"));
purple_roomlist_room_add_field(expand->list, room, json_get_prop_strptr(json_get_prop(chan, "topic"), "value"));
purple_roomlist_room_add_field(expand->list, room, json_get_prop_strptr(json_get_prop(chan, "purpose"), "value"));
purple_roomlist_room_add_field(expand->list, room, GUINT_TO_POINTER((gulong) json_get_val(json_get_prop(chan, "num_members"), integer, 0)));
time_t t = slack_parse_time(json_get_prop(chan, "created"));
purple_roomlist_room_add_field(expand->list, room, purple_date_format_long(localtime(&t)));
SlackUser *creator = (SlackUser*)slack_object_hash_table_lookup(sa->users, json_get_prop_strptr(chan, "creator"));
purple_roomlist_room_add_field(expand->list, room, creator ? creator->object.name : NULL);
purple_roomlist_room_add(expand->list, room);
}
if (json && cursor && *cursor)
ROOMLIST_CALL(sa, expand, "cursor", cursor);
else {
purple_roomlist_set_in_progress(expand->list, FALSE);
purple_roomlist_unref(expand->list);
g_free(expand);
}
return FALSE;
}
void slack_roomlist_expand_category(PurpleRoomlist *list, PurpleRoomlistRoom *parent) {
SlackAccount *sa = get_slack_account(list->account);
if (!sa)
return;
sa->roomlist_stop = FALSE;
struct roomlist_expand *expand = g_new0(struct roomlist_expand, 1);
expand->list = list;
expand->parent = parent;
purple_roomlist_ref(list);
purple_roomlist_set_in_progress(list, TRUE);
ROOMLIST_CALL(sa, expand);
}
PurpleRoomlist *slack_roomlist_get_list(PurpleConnection *gc) {
SlackAccount *sa = gc->proto_data;
PurpleRoomlist *list = purple_roomlist_new(sa->account);
GList *fields = NULL;
fields = g_list_append(fields, purple_roomlist_field_new(PURPLE_ROOMLIST_FIELD_STRING, "ID", "id", TRUE));
fields = g_list_append(fields, purple_roomlist_field_new(PURPLE_ROOMLIST_FIELD_STRING, "Topic", "topic", FALSE));
fields = g_list_append(fields, purple_roomlist_field_new(PURPLE_ROOMLIST_FIELD_STRING, "Purpose", "purpose", FALSE));
fields = g_list_append(fields, purple_roomlist_field_new(PURPLE_ROOMLIST_FIELD_INT, "Members", "members", FALSE));
fields = g_list_append(fields, purple_roomlist_field_new(PURPLE_ROOMLIST_FIELD_STRING, "Created", "created", FALSE));
fields = g_list_append(fields, purple_roomlist_field_new(PURPLE_ROOMLIST_FIELD_STRING, "Creator", "creator", FALSE));
purple_roomlist_set_fields(list, fields);
purple_roomlist_room_add(list, purple_roomlist_room_new(PURPLE_ROOMLIST_ROOMTYPE_CATEGORY, "Archived", NULL));
slack_roomlist_expand_category(list, NULL);
purple_roomlist_unref(list);
return list;
}
void slack_roomlist_cancel(PurpleRoomlist *list) {
SlackAccount *sa = get_slack_account(list->account);
if (!sa)
return;
sa->roomlist_stop = TRUE;
}