Release 0.1

v1
Tony Olagbaiye 7 years ago
parent 32046373b4
commit 33eb0d6bb1

2
.gitattributes vendored

@ -0,0 +1,2 @@
# Github language display
*.h linguist-language=C

@ -27,6 +27,7 @@ SRCS=slack.c \
api/message/slack-api-message-unimplemented.c \
request/slack-request-chat-postmessage.c \
request/slack-request-channels-list.c \
request/slack-request-conversations-members.c \
request/slack-request-users-list.c
OBJS=$(subst .c,.o,$(SRCS)) libwebsockets/lib/libwebsockets.a json-c/libjson-c.a
@ -57,6 +58,9 @@ clean:
distclean: clean
$(RM) *~ .depend
install: slack.so
install slack.so ~/.weechat/plugins/
.PHONY: tags cs
tags:

@ -0,0 +1,2 @@
8131853dbc8c3be5171fa96353de7a884a79d3f1d6b3fbf48f99037f5f95fe27 json-c/config.guess
1ee0b1581032c46aac6ebd1de74b130f35082fb45a12e9ead42ad5c775f9b64f json-c/config.sub

6
debian/changelog vendored

@ -0,0 +1,6 @@
weechat-slack (0~1805071648-1) UNRELEASED; urgency=low
* Initial release. Closes: #nnnn
<nnnn is the bug number of your ITP>
-- Bao <bao@localhost> Mon, 07 May 2018 17:48:57 +0100

1
debian/compat vendored

@ -0,0 +1 @@
11

19
debian/control vendored

@ -0,0 +1,19 @@
Source: weechat-slack
Section: net
Priority: optional
Maintainer: Tony Olagbaiye <frony0@gmail.com>
Build-Depends: cmake, debhelper (>= 11), pkg-config
Standards-Version: 4.1.3
Homepage: https://weechat.org/
Vcs-Git: https://github.com/bqv/weechat-slack.git
Vcs-Browser: https://github.com/bqv/weechat-slack
Package: weechat-slack
Architecture: all
Depends:
weechat (>= 1.4)
Description: Fast, light and extensible chat client - Slack plugin
WeeChat (Wee Enhanced Environment for Chat) is a fast and light chat client
for many operating systems. Everything can be done with a keyboard.
.
This package provides the Slack API plugin.

2865
debian/copyright vendored

File diff suppressed because it is too large Load Diff

@ -0,0 +1 @@
weechat-slack

18
debian/rules vendored

@ -0,0 +1,18 @@
#!/usr/bin/make -f
# You must remove unused comment lines for the released package.
#export DH_VERBOSE = 1
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
export JAVA_HOME=/usr/lib/jvm/default-java
export CLASSPATH=/usr/share/java/csv.jar:/usr/share/java/debug-disable.jar:/usr/share/java/itext.jar
%:
dh $@ --without autoreconf
#override_dh_auto_install:
# dh_auto_install -- prefix=/usr
#override_dh_install:
# dh_install --list-missing -X.pyc -X.pyo

@ -29,10 +29,13 @@ static inline int json_valid(json_object *object, struct t_slack_workspace *work
return 1;
}
static const struct lws_protocols protocols[];
static int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
struct t_slack_request *request = (struct t_slack_request *)user;
struct lws_client_connect_info ccinfo;
int status;
@ -45,7 +48,26 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
_("%s%s: (%d) error connecting to slack: %s"),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx,
in ? (char *)in : "(null)");
request->client_wsi = NULL;
weechat_printf(
request->workspace->buffer,
_("%s%s: (%d) reconnecting..."),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx);
memset(&ccinfo, 0, sizeof(ccinfo)); /* otherwise uninitialized garbage */
ccinfo.context = request->context;
ccinfo.ssl_connection = LCCSCF_USE_SSL;
ccinfo.port = 443;
ccinfo.address = "slack.com";
ccinfo.path = request->uri;
ccinfo.host = ccinfo.address;
ccinfo.origin = ccinfo.address;
ccinfo.method = "GET";
ccinfo.protocol = protocols[0].name;
ccinfo.pwsi = &request->client_wsi;
ccinfo.userdata = request;
lws_client_connect_via_info(&ccinfo);
break;
case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:

@ -29,10 +29,13 @@ static inline int json_valid(json_object *object, struct t_slack_workspace *work
return 1;
}
static const struct lws_protocols protocols[];
static int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
struct t_slack_request *request = (struct t_slack_request *)user;
struct lws_client_connect_info ccinfo;
int status;
@ -45,7 +48,26 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
_("%s%s: (%d) error connecting to slack: %s"),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx,
in ? (char *)in : "(null)");
request->client_wsi = NULL;
weechat_printf(
request->workspace->buffer,
_("%s%s: (%d) reconnecting..."),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx);
memset(&ccinfo, 0, sizeof(ccinfo)); /* otherwise uninitialized garbage */
ccinfo.context = request->context;
ccinfo.ssl_connection = LCCSCF_USE_SSL;
ccinfo.port = 443;
ccinfo.address = "slack.com";
ccinfo.path = request->uri;
ccinfo.host = ccinfo.address;
ccinfo.origin = ccinfo.address;
ccinfo.method = "GET";
ccinfo.protocol = protocols[0].name;
ccinfo.pwsi = &request->client_wsi;
ccinfo.userdata = request;
lws_client_connect_via_info(&ccinfo);
break;
case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:

@ -0,0 +1,326 @@
#include <libwebsockets.h>
#include <json.h>
#include <stdlib.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "../slack.h"
#include "../slack-workspace.h"
#include "../slack-channel.h"
#include "../slack-request.h"
#include "../slack-user.h"
#include "../request/slack-request-conversations-members.h"
static const char *const endpoint = "/api/conversations.members?"
"token=%s&channel=%s&cursor=%s&limit=100";
static inline int json_valid(json_object *object, struct t_slack_workspace *workspace)
{
if (!object)
{
weechat_printf(
workspace->buffer,
_("%s%s: error retrieving members: unexpected response from server"),
weechat_prefix("error"), SLACK_PLUGIN_NAME);
//__asm__("int3");
return 0;
}
return 1;
}
static const struct lws_protocols protocols[];
static int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
struct t_slack_request *request = (struct t_slack_request *)user;
struct lws_client_connect_info ccinfo;
struct t_slack_channel *channel;
const char *channelid;
int status;
switch (reason)
{
/* because we are protocols[0] ... */
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
weechat_printf(
request->workspace->buffer,
_("%s%s: (%d) error connecting to slack: %s"),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx,
in ? (char *)in : "(null)");
weechat_printf(
request->workspace->buffer,
_("%s%s: (%d) reconnecting..."),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx);
memset(&ccinfo, 0, sizeof(ccinfo)); /* otherwise uninitialized garbage */
ccinfo.context = request->context;
ccinfo.ssl_connection = LCCSCF_USE_SSL;
ccinfo.port = 443;
ccinfo.address = "slack.com";
ccinfo.path = request->uri;
ccinfo.host = ccinfo.address;
ccinfo.origin = ccinfo.address;
ccinfo.method = "GET";
ccinfo.protocol = protocols[0].name;
ccinfo.pwsi = &request->client_wsi;
ccinfo.userdata = request;
lws_client_connect_via_info(&ccinfo);
break;
case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
status = lws_http_client_http_response(wsi);
weechat_printf(
request->workspace->buffer,
_("%s%s: (%d) retrieving members... (%d)"),
weechat_prefix("network"), SLACK_PLUGIN_NAME, request->idx,
status);
break;
/* chunks of chunked content, with header removed */
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
{
struct t_json_chunk *new_chunk, *last_chunk;
new_chunk = malloc(sizeof(*new_chunk));
new_chunk->data = malloc((1024 * sizeof(char)) + 1);
new_chunk->data[0] = '\0';
new_chunk->next = NULL;
strncat(new_chunk->data, in, (int)len);
if (request->json_chunks)
{
for (last_chunk = request->json_chunks; last_chunk->next;
last_chunk = last_chunk->next);
last_chunk->next = new_chunk;
}
else
{
request->json_chunks = new_chunk;
}
}
return 0; /* don't passthru */
/* uninterpreted http content */
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
{
char buffer[1024 + LWS_PRE];
char *px = buffer + LWS_PRE;
int lenx = sizeof(buffer) - LWS_PRE;
if (lws_http_client_read(wsi, &px, &lenx) < 0)
return -1;
}
return 0; /* don't passthru */
case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
{
int chunk_count, i;
char *json_string;
char cursor[64];
json_object *response, *ok, *error, *members;
json_object *user, *metadata, *next_cursor;
struct t_json_chunk *chunk_ptr;
channelid = (const char *)request->pointer;
channel = slack_channel_search(request->workspace, channelid);
chunk_count = 0;
if (request->json_chunks)
{
chunk_count++;
for (chunk_ptr = request->json_chunks; chunk_ptr->next;
chunk_ptr = chunk_ptr->next)
{
chunk_count++;
}
}
json_string = malloc((1024 * sizeof(char) * chunk_count) + 1);
json_string[0] = '\0';
chunk_ptr = request->json_chunks;
for (i = 0; i < chunk_count; i++)
{
strncat(json_string, chunk_ptr->data, 1024);
chunk_ptr = chunk_ptr->next;
free(request->json_chunks->data);
free(request->json_chunks);
request->json_chunks = chunk_ptr;
}
weechat_printf(
request->workspace->buffer,
_("%s%s: (%d) got response: %s"),
weechat_prefix("network"), SLACK_PLUGIN_NAME, request->idx,
json_string);
response = json_tokener_parse(json_string);
ok = json_object_object_get(response, "ok");
if (!json_valid(ok, request->workspace))
{
json_object_put(response);
free(json_string);
return 0;
}
if(json_object_get_boolean(ok))
{
members = json_object_object_get(response, "members");
if (!json_valid(members, request->workspace))
{
json_object_put(response);
free(json_string);
return 0;
}
for (i = json_object_array_length(members); i > 0; i--)
{
user = json_object_array_get_idx(members, i - 1);
if (!json_valid(user, request->workspace))
{
json_object_put(response);
free(json_string);
return 0;
}
slack_channel_add_member(request->workspace,
channel,
json_object_get_string(user));
}
metadata = json_object_object_get(response, "response_metadata");
if (!json_valid(metadata, request->workspace))
{
json_object_put(response);
free(json_string);
return 0;
}
next_cursor = json_object_object_get(metadata, "next_cursor");
if (!json_valid(next_cursor, request->workspace))
{
json_object_put(response);
free(json_string);
return 0;
}
lws_urlencode(cursor, json_object_get_string(next_cursor), sizeof(cursor));
if (cursor[0])
{
struct t_slack_request *next_request;
next_request = slack_request_conversations_members(request->workspace,
weechat_config_string(
request->workspace->options[SLACK_WORKSPACE_OPTION_TOKEN]),
channelid,
cursor);
if (next_request)
slack_workspace_register_request(request->workspace, next_request);
}
}
else
{
error = json_object_object_get(response, "error");
if (!json_valid(error, request->workspace))
{
json_object_put(response);
free(json_string);
return 0;
}
weechat_printf(
request->workspace->buffer,
_("%s%s: (%d) failed to retrieve users: %s"),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx,
json_object_get_string(error));
}
json_object_put(response);
free(json_string);
}
/* fallthrough */
case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
request->client_wsi = NULL;
/* Does not doing this cause a leak?
lws_cancel_service(lws_get_context(wsi));*/ /* abort poll wait */
break;
default:
break;
}
return lws_callback_http_dummy(wsi, reason, user, in, len);
}
static const struct lws_protocols protocols[] = {
{
"http",
callback_http,
0,
0,
},
{ NULL, NULL, 0, 0 }
};
struct t_slack_request *slack_request_conversations_members(
struct t_slack_workspace *workspace,
const char *token, const char *channel,
const char *cursor)
{
struct t_slack_request *request;
struct lws_context_creation_info ctxinfo;
struct lws_client_connect_info ccinfo;
request = slack_request_alloc(workspace);
request->pointer = channel;
size_t urilen = snprintf(NULL, 0, endpoint, token, channel, cursor) + 1;
request->uri = malloc(urilen);
snprintf(request->uri, urilen, endpoint, token, channel, cursor);
memset(&ctxinfo, 0, sizeof(ctxinfo)); /* otherwise uninitialized garbage */
ctxinfo.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
ctxinfo.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
ctxinfo.protocols = protocols;
request->context = lws_create_context(&ctxinfo);
if (!request->context)
{
weechat_printf(
workspace->buffer,
_("%s%s: (%d) error connecting to slack: lws init failed"),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx);
return NULL;
}
else
{
weechat_printf(
workspace->buffer,
_("%s%s: (%d) contacting slack.com:443"),
weechat_prefix("network"), SLACK_PLUGIN_NAME, request->idx);
}
memset(&ccinfo, 0, sizeof(ccinfo)); /* otherwise uninitialized garbage */
ccinfo.context = request->context;
ccinfo.ssl_connection = LCCSCF_USE_SSL;
ccinfo.port = 443;
ccinfo.address = "slack.com";
ccinfo.path = request->uri;
ccinfo.host = ccinfo.address;
ccinfo.origin = ccinfo.address;
ccinfo.method = "GET";
ccinfo.protocol = protocols[0].name;
ccinfo.pwsi = &request->client_wsi;
ccinfo.userdata = request;
lws_client_connect_via_info(&ccinfo);
return request;
}

@ -0,0 +1,9 @@
#ifndef _SLACK_REQUEST_CONVERSATIONS_MEMBERS_H_
#define _SLACK_REQUEST_CONVERSATIONS_MEMBERS_H_
struct t_slack_request *slack_request_conversations_members(
struct t_slack_workspace *workspace,
const char *token, const char *channel,
const char *cursor);
#endif /*SLACK_REQUEST_CONVERSATIONS_MEMBERS_H*/

@ -6,8 +6,10 @@
#include "../weechat-plugin.h"
#include "../slack.h"
#include "../slack-workspace.h"
#include "../slack-channel.h"
#include "../slack-request.h"
#include "../slack-user.h"
#include "../request/slack-request-conversations-members.h"
#include "../request/slack-request-users-list.h"
static const char *const endpoint = "/api/users.list?"
@ -29,10 +31,13 @@ static inline int json_valid(json_object *object, struct t_slack_workspace *work
return 1;
}
static const struct lws_protocols protocols[];
static int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
struct t_slack_request *request = (struct t_slack_request *)user;
struct lws_client_connect_info ccinfo;
int status;
@ -45,7 +50,26 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
_("%s%s: (%d) error connecting to slack: %s"),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx,
in ? (char *)in : "(null)");
request->client_wsi = NULL;
weechat_printf(
request->workspace->buffer,
_("%s%s: (%d) reconnecting..."),
weechat_prefix("error"), SLACK_PLUGIN_NAME, request->idx);
memset(&ccinfo, 0, sizeof(ccinfo)); /* otherwise uninitialized garbage */
ccinfo.context = request->context;
ccinfo.ssl_connection = LCCSCF_USE_SSL;
ccinfo.port = 443;
ccinfo.address = "slack.com";
ccinfo.path = request->uri;
ccinfo.host = ccinfo.address;
ccinfo.origin = ccinfo.address;
ccinfo.method = "GET";
ccinfo.protocol = protocols[0].name;
ccinfo.pwsi = &request->client_wsi;
ccinfo.userdata = request;
lws_client_connect_via_info(&ccinfo);
break;
case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
@ -230,6 +254,23 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason,
if (next_request)
slack_workspace_register_request(request->workspace, next_request);
}
else
{
struct t_slack_request *next_request;
struct t_slack_channel *ptr_channel;
for (ptr_channel = request->workspace->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel)
{
next_request = slack_request_conversations_members(request->workspace,
weechat_config_string(
request->workspace->options[SLACK_WORKSPACE_OPTION_TOKEN]),
ptr_channel->id,
cursor);
if (next_request)
slack_workspace_register_request(request->workspace, next_request);
}
}
}
else
{

@ -515,3 +515,29 @@ void slack_channel_update_purpose(struct t_slack_channel *channel,
channel->purpose.creator = (creator) ? strdup(creator) : NULL;
channel->purpose.last_set = last_set;
}
struct t_slack_channel_member *slack_channel_add_member(
struct t_slack_workspace *workspace,
struct t_slack_channel *channel,
const char *id)
{
struct t_slack_channel_member *member;
struct t_slack_user *user;
member = malloc(sizeof(struct t_slack_channel_member));
member->id = strdup(id);
member->prev_member = channel->last_member;
member->next_member = NULL;
if (channel->last_member)
(channel->last_member)->next_member = member;
else
channel->members = member;
channel->last_member = member;
user = slack_user_search(workspace, id);
if (user)
slack_user_nicklist_add(workspace, channel, user);
return member;
}

@ -121,4 +121,9 @@ void slack_channel_update_purpose(struct t_slack_channel *channel,
const char* creator,
int last_set);
struct t_slack_channel_member *slack_channel_add_member(
struct t_slack_workspace *workspace,
struct t_slack_channel *channel,
const char *id);
#endif /*SLACK_CHANNEL_H*/

@ -7,6 +7,9 @@ struct t_slack_request
int idx;
const void *pointer;
void *data;
char *uri;
struct lws *client_wsi;
struct lws_context *context;

@ -56,4 +56,8 @@ struct t_slack_user *slack_user_new(struct t_slack_workspace *workspace,
void slack_user_free_all(struct t_slack_workspace *workspace);
void slack_user_nicklist_add(struct t_slack_workspace *workspace,
struct t_slack_channel *channel,
struct t_slack_user *user);
#endif /*SLACK_USER_H*/

@ -846,6 +846,12 @@ int slack_workspace_timer_cb(const void *pointer, void *data, int remaining_call
free(ptr_request->uri);
ptr_request->uri = NULL;
}
ptr_request->pointer = NULL;
if (ptr_request->data)
{
free(ptr_request->data);
ptr_request->data = NULL;
}
/* remove request from requests list */
if (ptr_workspace->last_request == ptr_request)

@ -45,9 +45,9 @@ int weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[])
weechat_plugin = plugin;
lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_INFO | LLL_DEBUG
lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE /*| LLL_INFO | LLL_DEBUG
| LLL_PARSER | LLL_HEADER | LLL_EXT | LLL_CLIENT
| LLL_LATENCY | LLL_USER | LLL_COUNT,
| LLL_LATENCY | LLL_USER | LLL_COUNT*/,
slack_lwsl_emit_weechat);
if (!slack_config_init())

Loading…
Cancel
Save