diff --git a/Makefile b/Makefile index 2efaf28..385ddda 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ SRCS=slack.c \ api/slack-api-user-typing.c \ api/message/slack-api-message-bot-message.c \ api/message/slack-api-message-slackbot-response.c \ + api/message/slack-api-message-me-message.c \ api/message/slack-api-message-unimplemented.c \ request/slack-request-chat-postmessage.c \ request/slack-request-channels-list.c \ diff --git a/README.org b/README.org index 688b150..4d963cd 100644 --- a/README.org +++ b/README.org @@ -1,6 +1,6 @@ #+TITLE: weechat-slack #+AUTHOR: Tony Olagbaiye -#+EMAIL: frony0.com +#+EMAIL: frony0@gmail.com #+DATE: 2018-05-09 #+DESCRIPTION: Weechat plugin for Slack #+KEYWORDS: weechat slack c api @@ -43,10 +43,10 @@ * Tasks ** TODO [#A] Implement essential api endpoints and events - - [ ] Implement handling api message =message.me_message= (see [[http://github.com/bqv/weechat-slack/issues/5][#5]]) + - [X] +Implement handling api message =message.me_message= (see [[http://github.com/bqv/weechat-slack/issues/5][#5]])+ - [ ] Implement sending request =chat.meMessage= (see [[http://github.com/bqv/weechat-slack/issues/5][#5]]) - [ ] Implement handling api message =message.thread_broadcast= - - [X] Implement handling api message =message.bot_message= (see [[http://github.com/bqv/weechat-slack/issues/2][#2]]) + - [X] +Implement handling api message =message.bot_message= (see [[http://github.com/bqv/weechat-slack/issues/2][#2]])+ - [ ] Implement handling api message =message.message_changed= - [ ] Implement handling api message =message.message_deleted= - [ ] Implement handling api message =message.message_replied= diff --git a/api/message/slack-api-message-me-message.c b/api/message/slack-api-message-me-message.c new file mode 100644 index 0000000..fcc91d1 --- /dev/null +++ b/api/message/slack-api-message-me-message.c @@ -0,0 +1,102 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, version 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include +#include + +#include "../../weechat-plugin.h" +#include "../../slack.h" +#include "../../slack-workspace.h" +#include "../../slack-message.h" +#include "../../slack-api.h" +#include "../../slack-channel.h" +#include "../../slack-user.h" +#include "../slack-api-message.h" +#include "slack-api-message-me-message.h" + +static const char *subtype = "me_message"; + +static inline int json_valid(json_object *object, struct t_slack_workspace *workspace) +{ + if (!object) + { + weechat_printf( + workspace->buffer, + _("%s%s: error handling websocket %smessage.%s%s message: " + "unexpected response from server"), + weechat_prefix("error"), SLACK_PLUGIN_NAME, + weechat_color("chat_value"), subtype, weechat_color("reset")); + return 0; + } + + return 1; +} + +int slack_api_message_me_message_handle(struct t_slack_workspace *workspace, + const char *channel, const char *user, + const char *text, const char *ts) +{ + struct t_slack_channel *ptr_channel; + struct t_slack_user *ptr_user; + struct t_slack_channel_typing *ptr_typing; + + ptr_channel = slack_channel_search(workspace, channel); + if (!ptr_channel) + return 1; /* silently ignore if channel hasn't been loaded yet */ + ptr_user = slack_user_search(workspace, user); + if (!ptr_user) + return 1; /* silently ignore if user hasn't been loaded yet */ + + char *message = slack_message_decode(workspace, text); + weechat_printf_date_tags( + ptr_channel->buffer, + (time_t)atof(ts), + "slack_message,slack_me_message", + _("%s%s%s%s%s%s"), + weechat_prefix("action"), + slack_user_get_colour(ptr_user), + ptr_user->profile.display_name, + weechat_color("reset"), + message[0] ? " " : "", + message); + free(message); + + ptr_typing = slack_channel_typing_search(ptr_channel, + ptr_user->profile.display_name); + if (ptr_typing) + { + slack_channel_typing_free(ptr_channel, ptr_typing); + slack_channel_typing_cb(ptr_channel, NULL, 0); + } + + return 1; +} + +int slack_api_message_me_message(struct t_slack_workspace *workspace, + json_object *message) +{ + json_object *channel, *user, *text, *ts; + channel = json_object_object_get(message, "channel"); + if (!json_valid(channel, workspace)) + return 0; + + user = json_object_object_get(message, "user"); + if (!json_valid(user, workspace)) + return 0; + + text = json_object_object_get(message, "text"); + if (!json_valid(text, workspace)) + return 0; + + ts = json_object_object_get(message, "ts"); + if (!json_valid(ts, workspace)) + return 0; + + return slack_api_message_me_message_handle(workspace, + json_object_get_string(channel), + json_object_get_string(user), + json_object_get_string(text), + json_object_get_string(ts)); +} + diff --git a/api/message/slack-api-message-me-message.h b/api/message/slack-api-message-me-message.h new file mode 100644 index 0000000..1131898 --- /dev/null +++ b/api/message/slack-api-message-me-message.h @@ -0,0 +1,12 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, version 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef _SLACK_API_MESSAGE_ME_MESSAGE_H_ +#define _SLACK_API_MESSAGE_ME_MESSAGE_H_ + +int slack_api_message_me_message( + struct t_slack_workspace *workspace, + json_object *message); + +#endif /*SLACK_API_MESSAGE_ME_MESSAGE_H*/ diff --git a/api/message/slack-api-message-slackbot-response.c b/api/message/slack-api-message-slackbot-response.c index 4f013af..8252d28 100644 --- a/api/message/slack-api-message-slackbot-response.c +++ b/api/message/slack-api-message-slackbot-response.c @@ -46,7 +46,7 @@ int slack_api_message_slackbot_response_handle(struct t_slack_workspace *workspa return 1; /* silently ignore if channel hasn't been loaded yet */ ptr_user = slack_user_search(workspace, user); if (!ptr_user) - return 1; /* silently ignore if bot user hasn't been loaded yet */ + return 1; /* silently ignore if slackbot user hasn't been loaded yet */ char *message = slack_message_decode(workspace, text); weechat_printf_date_tags( diff --git a/api/slack-api-message.c b/api/slack-api-message.c index 534f7b0..0fd0a04 100644 --- a/api/slack-api-message.c +++ b/api/slack-api-message.c @@ -15,6 +15,7 @@ #include "slack-api-message.h" #include "message/slack-api-message-unimplemented.h" #include "message/slack-api-message-bot-message.h" +#include "message/slack-api-message-me-message.h" #include "message/slack-api-message-slackbot-response.h" static const char *type = "message"; @@ -45,7 +46,7 @@ static struct stringcase cases[] = , { "group_purpose", &slack_api_message_unimplemented } , { "group_topic", &slack_api_message_unimplemented } , { "group_unarchive", &slack_api_message_unimplemented } -, { "me_message", &slack_api_message_unimplemented } +, { "me_message", &slack_api_message_me_message } , { "message_changed", &slack_api_message_unimplemented } , { "message_deleted", &slack_api_message_unimplemented } , { "message_replied", &slack_api_message_unimplemented } diff --git a/slack-user.h b/slack-user.h index 763c832..cc97ec4 100644 --- a/slack-user.h +++ b/slack-user.h @@ -50,6 +50,8 @@ struct t_slack_user struct t_slack_user *next_user; }; +const char *slack_user_get_colour(struct t_slack_user *user); + const char *slack_user_as_prefix(struct t_slack_workspace *workspace, struct t_slack_user *user, const char *name); diff --git a/weechat-plugin.h b/weechat-plugin.h index 5abb6d0..02dc525 100644 --- a/weechat-plugin.h +++ b/weechat-plugin.h @@ -1,7 +1,3 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, version 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - /* * weechat-plugin.h - header to compile WeeChat plugins *