summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/api.c11
-rw-r--r--src/h.c28
2 files changed, 26 insertions, 13 deletions
diff --git a/src/api.c b/src/api.c
index c405cff..7171e3a 100644
--- a/src/api.c
+++ b/src/api.c
@@ -238,7 +238,11 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
role = &pass->api_io.guild->role;
while (*role)
role = &(*role)->next;
- *role = pass->api_io.guild->role;
+ if (!dc_find_ll_role(pass->api_io.guild->role, pass->api_io.role->id)) {
+ fprintf(stderr, "new role id=%lld (:\n", pass->api_io.role->id);
+ pass->api_io.role->next = NULL;
+ *role = pass->api_io.role;
+ }
pass->api_io.role->guild = pass->api_io.guild;
if (pass->api_io.role->name && !strncmp(pass->api_io.role->name, "@everyone", strlen(pass->api_io.role->name)))
pass->api_io.role->status |= DC_EVERYONE;
@@ -681,6 +685,11 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun
}
}
struct dc_api_io dc_api_o (struct dc_api_io i /* for ->program */) {
+ if (!i.program) {
+ i.type = DC_API_STATUS;
+ i.status = DC_USER_ERROR;
+ return i;
+ }
if (i.program->lws_context)
lws_service(i.program->lws_context, 0);
for (size_t x = 0; x < i.program->clients_length; x++) {
diff --git a/src/h.c b/src/h.c
index bd20bf8..df7907f 100644
--- a/src/h.c
+++ b/src/h.c
@@ -51,6 +51,7 @@ enum dc_status { /* theese are flags and should be and-checked */
DC_REPLACE = 1 << 23, /* dc_add_x replace old with new on found, _free: only free members */
DC_EXPLICIT_NULL = 1 << 24, /* MEMB_GC will NULL this member (PROGRES?aftr free) (& clear bit) */
DC_EVERYONE = 1 << 25, /* role applies to all guild users */
+ DC_USER_ERROR = 1 << 26, /* lib user made an error, passed NULL pointer to _o for example */
DC_INTERNAL = DC_FROM_LWS | DC_FROM_API, /* call originates from an internal function */
}; /* note: when checking status, first check for DC_OK, if it's set then disregard errors! */
#define DC_ADMIN (1 << 3) /* not all enum fields are implemented/understood */
@@ -108,7 +109,7 @@ enum dc_api_io_type {
DC_API_REGISTER,/* i: pass a dc_client, to relogin FIX pr rt cl&cl->user not creat new */
/* o: the previously passed dc_client with set status */
DC_API_STATUS, /* i: N/A */
- /* o: new status message, pointer to internal string - tr0 */
+ /* o: check ->status */
DC_API_USER, /* i: query for user by id, pass dc_user-tr1 */
/* o: prev passed dc_user but filled (or not: ->status may indicate error) */
DC_API_ROLE, /* i: query for role by id, pass dc_role-tr1 */
@@ -665,6 +666,18 @@ enum dc_status dc_handle_ping (struct dc_api_io io, void * userdata) { /* tries
}
struct dc_program * dc_program_init () {
struct dc_program * s = calloc(1, sizeof(struct dc_program));
+ /* lws init, it may fail so we first do this to return NULL without memleak (thanks, clang) */
+ struct lws_context_creation_info info;
+ memset(&info, 0, sizeof(info));
+ info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
+ info.port = CONTEXT_PORT_NO_LISTEN; /* we are not listening - we are a client */
+ info.protocols = dc_lws_protocols;
+ info.fd_limit_per_thread = DC_LWS_MAX_FD;
+ if (!(s->lws_context = lws_create_context(&info))) {
+ lwsl_err("lws init failed\n");
+ free(s);
+ return NULL; /* of course our UI does not check for failure of init function, so the */
+ } /* program will just crash, but I assume lws init will never fail (correct me if I'm wrong) */
DC_ISASIQ(client);
DC_ISASIQ(guild);
DC_ISASIQ(channel);
@@ -674,17 +687,6 @@ struct dc_program * dc_program_init () {
DC_ISASIQ(permission);
DC_ISASIQ(attached_function);
DC_ISASIQ(api_io);
- /* lws init */
- struct lws_context_creation_info info;
- memset(&info, 0, sizeof(info));
- info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
- info.port = CONTEXT_PORT_NO_LISTEN; /* we are not listening - we are a client */
- info.protocols = dc_lws_protocols;
- info.fd_limit_per_thread = DC_LWS_MAX_FD;
- if (!(s->lws_context = lws_create_context(&info))) {
- lwsl_err("lws init failed\n");
- return NULL;
- }
struct dc_api_io io; /* attach a function for pinging wss of every client */
memset(&io, 0, sizeof(io));
io.type = DC_API_ATTACH;
@@ -780,6 +782,7 @@ DC_GEN_X(channel, CHANNEL)
DC_FIND_LL_X(channel)
DC_GEN_X(guild, GUILD)
DC_GEN_X(role, ROLE)
+DC_FIND_LL_X(role)
#define DC_ISAE(a) &(a), &(a##_sizeof), &(a##_length) /* ISA Expand */
#define DC_ISAN NULL, NULL, NULL /* ISA NULL */
#define DC_TRANSFER_CHANNEL(n, o) do { /* n is going to DC_REPLACE o, transfer important data */ \
@@ -799,6 +802,7 @@ DC_GEN_X(role, ROLE)
void dc_transfer_role (struct dc_role * n, struct dc_role * o) {
n->next = o->next;
if (!n->users_length) { /* if we didn't update users array in the new role object */
+ free(n->users); /* thanks, clang, we have to free pointer array before overwriting it! */
n->users = o->users;
n->users_sizeof = o->users_sizeof;
n->users_length = o->users_sizeof;