forked from necrosis/slack-libpurple
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathslack-thread.c
286 lines (246 loc) · 8.28 KB
/
slack-thread.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "slack-api.h"
#include "slack-channel.h"
#include "slack-conversation.h"
#include "slack-json.h"
#include "slack-message.h"
#include "slack-thread.h"
#include <debug.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
/**
* Returns a deterministic color for a thread.
*
* Returned string must be g_string_free'd.
*
* @param ts "thread_ts" string value from Slack.
*/
static void slack_get_thread_color(char s[8], const char *ts) {
// Produce a color that works against white background by the following
// algorithm:
//
// 1. Pick a pseudo-random number by seeding it with ts, IOW it is
// deterministic.
//
// 2. Pick a 24-bit RGB value, but throw away the the original highest
// bit of each byte, resulting in a range from 000000 to 7f7f7f.
//
// 3. Pick exactly one base color that will receive its highest bit, or
// possibly none of them.
//
// This gives quite a random color, but often with a dominant RGB
// component, never white, and deterministic from ts.
guint r = g_str_hash(ts);
// Pick random RGB color.
guint color = r & 0x7f7f7f;
// Pick random RGB high bit by shifting 0x800000 down by 0 (B), 8 (G),
// 16 (R), or 24 (0). 0x3000000 are the first bits of 'r' we didn't use
// yet.
guint pref_color = (0x800000 >> ((r & 0x3000000) >> 21));
color |= pref_color;
snprintf(s, 7, "%06x", color);
}
#ifdef _WIN32
static struct tm *localtime_r(time_t *_clock, struct tm *_result)
{
struct tm *p = localtime(_clock);
if (p)
*(_result) = *p;
return p;
}
#endif
static void slack_format_thread_time(SlackAccount *sa, char s[128], const char *ts, gboolean exact) {
char *te;
time_t tt = strtoul(ts, &te, 10); // slack_parse_time_str(ts)
if (!tt) {
strncpy(s, ts, 127);
return;
}
time_t now = time(NULL);
struct tm now_time, thread_time;
localtime_r(&tt, &thread_time);
localtime_r(&now, &now_time);
const char *time_fmt;
if (thread_time.tm_yday == now_time.tm_yday && thread_time.tm_year == now_time.tm_year)
time_fmt = purple_account_get_string(sa->account, "thread_timestamp", "%X");
else
time_fmt = purple_account_get_string(sa->account, "thread_datestamp", "%x %X");
size_t r = strftime(s, 128, time_fmt, &thread_time);
if (!r) {
/* fall back */
r = snprintf(s, 128, "%ld", tt);
}
if (exact)
strncpy(&s[r], te, 127-r);
}
static gboolean slack_parse_thread_time(SlackAccount *sa, const char *s, char start[20], char end[20], const char **rest) {
time_t t = 0;
char *e = NULL;
if (rest)
*rest = NULL;
/* first try posix seconds */
if (s[0] >= '0' && s[0] <= '9') {
t = strtoul(s, &e, 10);
if (t && e - s >= 10)
goto sub;
}
#ifndef _WIN32
char *su = NULL;
const char *formats[] = {
purple_account_get_string(sa->account, "thread_datestamp", "%x %X"),
purple_account_get_string(sa->account, "thread_timestamp", "%X"),
NULL };
const char **fmt;
time(&t);
for (fmt = formats; *fmt; fmt++) {
struct tm tm;
localtime_r(&t, &tm);
e = strptime(s, *fmt, &tm);
if (!e && su)
e = strptime(su, *fmt, &tm);
if (e) {
t = mktime(&tm);
break;
}
}
g_free(su);
#endif
sub:
if (!e)
return FALSE;
if (*e == '.') {
e++;
int i = 0;
while (e[i] >= '0' && e[i] <= '9')
i ++;
if (i == 6) {
snprintf(start, 19, "%lu.%s", t, e);
*end = 0;
e += i;
}
}
else
{
snprintf(start, 19, "%lu.000000", t);
snprintf(end, 19, "%lu.999999", t);
}
if (*e && !isspace(*e))
// Don't accept other strings right next to the timestamp
// (require at least one space).
return FALSE;
if (rest) {
while (isspace(*e))
e++;
*rest = e;
}
return TRUE;
}
void slack_append_formatted_thread_timestamp(SlackAccount *sa, GString *str, const char *ts, gboolean exact) {
char color[8] = "";
slack_get_thread_color(color, ts);
char time_str[128] = "";
slack_format_thread_time(sa, time_str, ts, exact);
g_string_append(str, "<font color=\"#");
g_string_append(str, color);
g_string_append(str, "\">");
g_string_append(str, time_str);
g_string_append(str, "</font>");
}
typedef void slack_thread_lookup_ts_cb(SlackAccount *sa, SlackObject *conv, gpointer data, const char *thread_ts, const char *rest);
struct thread_lookup_ts {
SlackObject *conv;
slack_thread_lookup_ts_cb *cb;
void *data;
char *timestr;
char *rest;
};
static gboolean slack_thread_lookup_ts_history_cb(SlackAccount *sa, gpointer data, json_value *json, const char *error) {
struct thread_lookup_ts *lookup = data;
const char *ts = NULL;
json_value *list = json_get_prop_type(json, "messages", array);
if (!list || error) {
purple_debug_error("slack", "Error querying threads: %s\n", error ?: "missing");
}
else if (list->u.array.length <= 0) {
slack_write_message(sa, lookup->conv, "Thread not found. If the thread start date is not today, make sure you specify the date in the thread timestamp.", PURPLE_MESSAGE_SYSTEM);
}
else if (list->u.array.length > 1) {
GString *errmsg = g_string_new("Thread timestamp is ambiguous. Please use one of the following unambiguous thread IDs:<br>");
for (unsigned i = 0; i < list->u.array.length; i++) {
json_value *entry = list->u.array.values[i];
/* matching slack_json_to_html */
const char *ts = json_get_prop_strptr(entry, "ts");
g_string_append(errmsg, purple_account_get_string(sa->account, "parent_indicator", "◈ "));
if (ts)
slack_append_formatted_thread_timestamp(sa, errmsg, ts, TRUE);
g_string_append(errmsg, ": ");
slack_message_to_html(errmsg, sa, json_get_prop_strptr(entry, "text"), 0, NULL);
g_string_append(errmsg, "<br>");
}
slack_write_message(sa, lookup->conv, errmsg->str, PURPLE_MESSAGE_SYSTEM);
g_string_free(errmsg, TRUE);
}
else {
json_value *entry = list->u.array.values[0];
ts = json_get_prop_strptr(entry, "ts");
}
if (ts != NULL) {
g_free(lookup->conv->last_thread_timestr);
g_free(lookup->conv->last_thread_ts);
lookup->conv->last_thread_timestr = lookup->timestr;
lookup->timestr = NULL; // Take ownership, avoid strdup.
lookup->conv->last_thread_ts = g_strdup(ts);
}
lookup->cb(sa, lookup->conv, lookup->data, ts, lookup->rest);
g_object_unref(lookup->conv);
g_free(lookup->timestr);
g_free(lookup->rest);
g_free(lookup);
return FALSE;
}
static void slack_thread_lookup_ts(SlackAccount *sa, slack_thread_lookup_ts_cb *cb, SlackObject *conv, gpointer data, const char *timestr) {
char start[20] = "";
char end[20] = "";
const char *rest;
if (!slack_parse_thread_time(sa, timestr, start, end, &rest)) {
slack_write_message(sa, conv, "Could not parse thread timestamp.", PURPLE_MESSAGE_SYSTEM);
return cb(sa, conv, data, NULL, rest);
}
if (!*end)
return cb(sa, conv, data, start, rest);
if (conv->last_thread_timestr && conv->last_thread_ts && strncmp(timestr, conv->last_thread_timestr, rest - timestr) == 0)
return cb(sa, conv, data, conv->last_thread_ts, rest);
struct thread_lookup_ts *lookup = g_new(struct thread_lookup_ts, 1);
lookup->conv = g_object_ref(conv);
lookup->cb = cb;
lookup->data = data;
lookup->timestr = g_strndup(timestr, rest - timestr);
lookup->rest = g_strdup(rest);
const char *id = slack_conversation_id(conv);
slack_api_get(sa, slack_thread_lookup_ts_history_cb, lookup, "conversations.history", "channel", id, "oldest", start, "latest", end, "inclusive", "1", NULL);
}
static void slack_thread_post_lookup_cb(SlackAccount *sa, SlackObject *conv, gpointer data, const char *thread_ts, const char *msg) {
if (!msg || !*msg) {
slack_write_message(sa, conv, "Please supply a message.", PURPLE_MESSAGE_SYSTEM);
return;
}
if (thread_ts) {
int r = slack_conversation_send(sa, conv, msg, 0, thread_ts);
if (r < 0)
purple_debug_error("slack", "Not able to send message \"%s\": %s\n", msg, strerror(-r));
}
}
void slack_thread_post_to_timestamp(SlackAccount *sa, SlackObject *obj, const char *timestr_and_msg) {
slack_thread_lookup_ts(sa, slack_thread_post_lookup_cb, obj, NULL, timestr_and_msg);
}
static void slack_thread_get_replies_lookup_cb(SlackAccount *sa, SlackObject *conv, gpointer data, const char *thread_ts, const char *rest) {
if (rest && *rest) {
slack_write_message(sa, conv, "Too many arguments.", PURPLE_MESSAGE_SYSTEM);
return;
}
if (thread_ts)
slack_get_history(sa, conv, NULL, SLACK_HISTORY_LIMIT_COUNT, thread_ts, TRUE);
}
void slack_thread_get_replies(SlackAccount *sa, SlackObject *obj, const char *timestr) {
slack_thread_lookup_ts(sa, slack_thread_get_replies_lookup_cb, obj, NULL, timestr);
}