Fix channel mentions. Closes #2

v1
Tony Olagbaiye 6 years ago
parent 638abde1f9
commit c76c93fd71

@ -19,34 +19,83 @@ char *slack_message_translate_code(struct t_slack_workspace *workspace,
{ {
struct t_slack_channel *channel; struct t_slack_channel *channel;
struct t_slack_user *user; struct t_slack_user *user;
char *command; size_t resultlen;
char *identifier, *alttext, *result, *symbol, *prefix;
switch (code[0]) identifier = strdup(code);
alttext = strchr(identifier, '|');
if (alttext)
*alttext++ = '\0';
switch (identifier[0])
{ {
case '#': /* channel */ case '#': /* channel */
channel = slack_channel_search(workspace, code+1); if (alttext)
if (channel) {
return strdup(channel->name); prefix = "#";
symbol = strdup(alttext);
}
else else
return strdup(code); {
channel = slack_channel_search(workspace, identifier+1);
if (channel)
{
prefix = "#";
symbol = strdup(channel->name);
}
else
{
prefix = "Channel:";
symbol = strdup(identifier+1);
}
}
break;
case '@': /* user */ case '@': /* user */
user = slack_user_search(workspace, code+1); if (alttext)
if (user)
{ {
size_t nicklen = snprintf(NULL, 0, "%s@%s%s", weechat_color("chat_nick"), user->profile.display_name, weechat_color("reset")) + 1; prefix = "@";
char *tag = malloc(nicklen); symbol = strdup(alttext);
snprintf(tag, nicklen, "%s@%s%s", weechat_color("chat_nick"), user->profile.display_name, weechat_color("reset"));
return tag;
} }
else else
return strdup(code); {
user = slack_user_search(workspace, identifier+1);
if (user)
{
prefix = "@";
symbol = strdup(user->profile.display_name);
}
else
{
prefix = "User:";
symbol = strdup(identifier+1);
}
}
break;
case '!': /* special */ case '!': /* special */
command = strdup(code); if (alttext)
command[0] = '@'; {
return command; prefix = "@";
symbol = strdup(alttext);
}
else
{
prefix = "@";
symbol = strdup(identifier+1);
}
break;
default: /* url */ default: /* url */
return strdup(code); prefix = "";
symbol = strdup(code);
break;
} }
free(identifier);
resultlen = snprintf(NULL, 0, "%s%s%s%s", weechat_color("chat_nick"), prefix, symbol, weechat_color("reset")) + 1;
result = malloc(resultlen);
snprintf(result, resultlen, "%s%s%s%s", weechat_color("chat_nick"), prefix, symbol, weechat_color("reset"));
free(symbol);
return result;
} }
void slack_message_htmldecode(char *dest, const char *src, size_t n) void slack_message_htmldecode(char *dest, const char *src, size_t n)

Loading…
Cancel
Save