Fix spacing in handler.c

This commit is contained in:
Dmitry Podgorny
2016-12-02 16:56:45 +02:00
parent 1092447edf
commit 80a7ea0b7c

View File

@@ -1,7 +1,7 @@
/* handler.c /* handler.c
** strophe XMPP client library -- event handler management ** strophe XMPP client library -- event handler management
** **
** Copyright (C) 2005-2009 Collecta, Inc. ** Copyright (C) 2005-2009 Collecta, Inc.
** **
** This software is provided AS-IS with no warranty, either express ** This software is provided AS-IS with no warranty, either express
** or implied. ** or implied.
@@ -32,92 +32,92 @@
* @param stanza a Strophe stanza object * @param stanza a Strophe stanza object
*/ */
void handler_fire_stanza(xmpp_conn_t * const conn, void handler_fire_stanza(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza) xmpp_stanza_t * const stanza)
{ {
xmpp_handlist_t *item, *prev; xmpp_handlist_t *item, *prev;
const char *id, *ns, *name, *type; const char *id, *ns, *name, *type;
/* call id handlers */ /* call id handlers */
id = xmpp_stanza_get_id(stanza); id = xmpp_stanza_get_id(stanza);
if (id) { if (id) {
prev = NULL; prev = NULL;
item = (xmpp_handlist_t *)hash_get(conn->id_handlers, id); item = (xmpp_handlist_t *)hash_get(conn->id_handlers, id);
while (item) { while (item) {
xmpp_handlist_t *next = item->next; xmpp_handlist_t *next = item->next;
if (item->user_handler && !conn->authenticated) { if (item->user_handler && !conn->authenticated) {
item = next; item = next;
continue; continue;
} }
if (!((xmpp_handler)(item->handler))(conn, stanza, item->userdata)) { if (!((xmpp_handler)(item->handler))(conn, stanza, item->userdata)) {
/* handler is one-shot, so delete it */ /* handler is one-shot, so delete it */
if (prev) if (prev)
prev->next = next; prev->next = next;
else { else {
hash_drop(conn->id_handlers, id); hash_drop(conn->id_handlers, id);
hash_add(conn->id_handlers, id, next); hash_add(conn->id_handlers, id, next);
} }
xmpp_free(conn->ctx, item->id); xmpp_free(conn->ctx, item->id);
xmpp_free(conn->ctx, item); xmpp_free(conn->ctx, item);
item = NULL; item = NULL;
} }
if (item) if (item)
prev = item; prev = item;
item = next; item = next;
} }
} }
/* call handlers */ /* call handlers */
ns = xmpp_stanza_get_ns(stanza); ns = xmpp_stanza_get_ns(stanza);
name = xmpp_stanza_get_name(stanza); name = xmpp_stanza_get_name(stanza);
type = xmpp_stanza_get_type(stanza); type = xmpp_stanza_get_type(stanza);
/* enable all added handlers */ /* enable all added handlers */
for (item = conn->handlers; item; item = item->next) for (item = conn->handlers; item; item = item->next)
item->enabled = 1; item->enabled = 1;
prev = NULL; prev = NULL;
item = conn->handlers; item = conn->handlers;
while (item) { while (item) {
/* skip newly added handlers */ /* skip newly added handlers */
if (!item->enabled) { if (!item->enabled) {
prev = item; prev = item;
item = item->next; item = item->next;
continue; continue;
} }
/* don't call user handlers until authentication succeeds */ /* don't call user handlers until authentication succeeds */
if (item->user_handler && !conn->authenticated) { if (item->user_handler && !conn->authenticated) {
prev = item; prev = item;
item = item->next; item = item->next;
continue; continue;
} }
if ((!item->ns || (ns && strcmp(ns, item->ns) == 0) || if ((!item->ns || (ns && strcmp(ns, item->ns) == 0) ||
xmpp_stanza_get_child_by_ns(stanza, item->ns)) && xmpp_stanza_get_child_by_ns(stanza, item->ns)) &&
(!item->name || (name && strcmp(name, item->name) == 0)) && (!item->name || (name && strcmp(name, item->name) == 0)) &&
(!item->type || (type && strcmp(type, item->type) == 0))) (!item->type || (type && strcmp(type, item->type) == 0)))
if (!((xmpp_handler)(item->handler))(conn, stanza, item->userdata)) { if (!((xmpp_handler)(item->handler))(conn, stanza, item->userdata)) {
/* handler is one-shot, so delete it */ /* handler is one-shot, so delete it */
if (prev) if (prev)
prev->next = item->next; prev->next = item->next;
else else
conn->handlers = item->next; conn->handlers = item->next;
if (item->ns) xmpp_free(conn->ctx, item->ns); if (item->ns) xmpp_free(conn->ctx, item->ns);
if (item->name) xmpp_free(conn->ctx, item->name); if (item->name) xmpp_free(conn->ctx, item->name);
if (item->type) xmpp_free(conn->ctx, item->type); if (item->type) xmpp_free(conn->ctx, item->type);
xmpp_free(conn->ctx, item); xmpp_free(conn->ctx, item);
item = NULL; item = NULL;
} }
if (item) { if (item) {
prev = item; prev = item;
item = item->next; item = item->next;
} else if (prev) } else if (prev)
item = prev->next; item = prev->next;
else else
item = conn->handlers; item = conn->handlers;
} }
} }
@@ -139,49 +139,49 @@ uint64_t handler_fire_timed(xmpp_ctx_t * const ctx)
connitem = ctx->connlist; connitem = ctx->connlist;
while (connitem) { while (connitem) {
if (connitem->conn->state != XMPP_STATE_CONNECTED) { if (connitem->conn->state != XMPP_STATE_CONNECTED) {
connitem = connitem->next; connitem = connitem->next;
continue; continue;
} }
/* enable all handlers that were added */
for (handitem = connitem->conn->timed_handlers; handitem;
handitem = handitem->next)
handitem->enabled = 1;
handitem = connitem->conn->timed_handlers; /* enable all handlers that were added */
while (handitem) { for (handitem = connitem->conn->timed_handlers; handitem;
/* skip newly added handlers */ handitem = handitem->next)
if (!handitem->enabled) { handitem->enabled = 1;
handitem = handitem->next;
continue;
}
/* only fire user handlers after authentication */ handitem = connitem->conn->timed_handlers;
if (handitem->user_handler && !connitem->conn->authenticated) { while (handitem) {
handitem = handitem->next; /* skip newly added handlers */
continue; if (!handitem->enabled) {
} handitem = handitem->next;
continue;
}
fired = 0; /* only fire user handlers after authentication */
elapsed = time_elapsed(handitem->last_stamp, time_stamp()); if (handitem->user_handler && !connitem->conn->authenticated) {
if (elapsed >= handitem->period) { handitem = handitem->next;
/* fire! */ continue;
fired = 1; }
handitem->last_stamp = time_stamp();
ret = ((xmpp_timed_handler)handitem->handler)(connitem->conn, handitem->userdata);
} else if (min > (handitem->period - elapsed))
min = handitem->period - elapsed;
temp = handitem;
handitem = handitem->next;
/* delete handler if it returned false */ fired = 0;
if (fired && !ret) elapsed = time_elapsed(handitem->last_stamp, time_stamp());
xmpp_timed_handler_delete(connitem->conn, temp->handler); if (elapsed >= handitem->period) {
} /* fire! */
fired = 1;
handitem->last_stamp = time_stamp();
ret = ((xmpp_timed_handler)handitem->handler)(connitem->conn, handitem->userdata);
} else if (min > (handitem->period - elapsed))
min = handitem->period - elapsed;
connitem = connitem->next; temp = handitem;
handitem = handitem->next;
/* delete handler if it returned false */
if (fired && !ret)
xmpp_timed_handler_delete(connitem->conn, temp->handler);
}
connitem = connitem->next;
} }
return min; return min;
@@ -199,25 +199,25 @@ void handler_reset_timed(xmpp_conn_t *conn, int user_only)
handitem = conn->timed_handlers; handitem = conn->timed_handlers;
while (handitem) { while (handitem) {
if ((user_only && handitem->user_handler) || !user_only) if ((user_only && handitem->user_handler) || !user_only)
handitem->last_stamp = time_stamp(); handitem->last_stamp = time_stamp();
handitem = handitem->next; handitem = handitem->next;
} }
} }
static void _timed_handler_add(xmpp_conn_t * const conn, static void _timed_handler_add(xmpp_conn_t * const conn,
xmpp_timed_handler handler, xmpp_timed_handler handler,
const unsigned long period, const unsigned long period,
void * const userdata, void * const userdata,
const int user_handler) const int user_handler)
{ {
xmpp_handlist_t *item, *tail; xmpp_handlist_t *item, *tail;
/* check if handler is already in the list */ /* check if handler is already in the list */
for (item = conn->timed_handlers; item; item = item->next) { for (item = conn->timed_handlers; item; item = item->next) {
if (item->handler == (void *)handler) if (item->handler == (void *)handler)
break; break;
} }
if (item) return; if (item) return;
@@ -236,12 +236,12 @@ static void _timed_handler_add(xmpp_conn_t * const conn,
/* append item to list */ /* append item to list */
if (!conn->timed_handlers) if (!conn->timed_handlers)
conn->timed_handlers = item; conn->timed_handlers = item;
else { else {
tail = conn->timed_handlers; tail = conn->timed_handlers;
while (tail->next) while (tail->next)
tail = tail->next; tail = tail->next;
tail->next = item; tail->next = item;
} }
} }
@@ -253,7 +253,7 @@ static void _timed_handler_add(xmpp_conn_t * const conn,
* @ingroup Handlers * @ingroup Handlers
*/ */
void xmpp_timed_handler_delete(xmpp_conn_t * const conn, void xmpp_timed_handler_delete(xmpp_conn_t * const conn,
xmpp_timed_handler handler) xmpp_timed_handler handler)
{ {
xmpp_handlist_t *item, *prev; xmpp_handlist_t *item, *prev;
@@ -262,35 +262,35 @@ void xmpp_timed_handler_delete(xmpp_conn_t * const conn,
prev = NULL; prev = NULL;
item = conn->timed_handlers; item = conn->timed_handlers;
while (item) { while (item) {
if (item->handler == (void *)handler) if (item->handler == (void *)handler)
break; break;
prev = item; prev = item;
item = item->next; item = item->next;
} }
if (item) { if (item) {
if (prev) if (prev)
prev->next = item->next; prev->next = item->next;
else else
conn->timed_handlers = item->next; conn->timed_handlers = item->next;
xmpp_free(conn->ctx, item); xmpp_free(conn->ctx, item);
} }
} }
static void _id_handler_add(xmpp_conn_t * const conn, static void _id_handler_add(xmpp_conn_t * const conn,
xmpp_handler handler, xmpp_handler handler,
const char * const id, const char * const id,
void * const userdata, int user_handler) void * const userdata, int user_handler)
{ {
xmpp_handlist_t *item, *tail; xmpp_handlist_t *item, *tail;
/* check if handler is already in the list */ /* check if handler is already in the list */
item = (xmpp_handlist_t *)hash_get(conn->id_handlers, id); item = (xmpp_handlist_t *)hash_get(conn->id_handlers, id);
while (item) { while (item) {
if (item->handler == (void *)handler) if (item->handler == (void *)handler)
break; break;
item = item->next; item = item->next;
} }
if (item) return; if (item) return;
@@ -306,18 +306,18 @@ static void _id_handler_add(xmpp_conn_t * const conn,
item->id = xmpp_strdup(conn->ctx, id); item->id = xmpp_strdup(conn->ctx, id);
if (!item->id) { if (!item->id) {
xmpp_free(conn->ctx, item); xmpp_free(conn->ctx, item);
return; return;
} }
/* put on list in hash table */ /* put on list in hash table */
tail = (xmpp_handlist_t *)hash_get(conn->id_handlers, id); tail = (xmpp_handlist_t *)hash_get(conn->id_handlers, id);
if (!tail) if (!tail)
hash_add(conn->id_handlers, id, item); hash_add(conn->id_handlers, id, item);
else { else {
while (tail->next) while (tail->next)
tail = tail->next; tail = tail->next;
tail->next = item; tail->next = item;
} }
} }
@@ -330,8 +330,8 @@ static void _id_handler_add(xmpp_conn_t * const conn,
* @ingroup Handlers * @ingroup Handlers
*/ */
void xmpp_id_handler_delete(xmpp_conn_t * const conn, void xmpp_id_handler_delete(xmpp_conn_t * const conn,
xmpp_handler handler, xmpp_handler handler,
const char * const id) const char * const id)
{ {
xmpp_handlist_t *item, *prev; xmpp_handlist_t *item, *prev;
@@ -340,39 +340,39 @@ void xmpp_id_handler_delete(xmpp_conn_t * const conn,
if (!item) return; if (!item) return;
while (item) { while (item) {
if (item->handler == (void *)handler) if (item->handler == (void *)handler)
break; break;
prev = item; prev = item;
item = item->next; item = item->next;
} }
if (item) { if (item) {
if (prev) if (prev)
prev->next = item->next; prev->next = item->next;
else { else {
hash_drop(conn->id_handlers, id); hash_drop(conn->id_handlers, id);
hash_add(conn->id_handlers, id, item->next); hash_add(conn->id_handlers, id, item->next);
} }
xmpp_free(conn->ctx, item->id); xmpp_free(conn->ctx, item->id);
xmpp_free(conn->ctx, item); xmpp_free(conn->ctx, item);
} }
} }
/* add a stanza handler */ /* add a stanza handler */
static void _handler_add(xmpp_conn_t * const conn, static void _handler_add(xmpp_conn_t * const conn,
xmpp_handler handler, xmpp_handler handler,
const char * const ns, const char * const ns,
const char * const name, const char * const name,
const char * const type, const char * const type,
void * const userdata, int user_handler) void * const userdata, int user_handler)
{ {
xmpp_handlist_t *item, *tail; xmpp_handlist_t *item, *tail;
/* check if handler already in list */ /* check if handler already in list */
for (item = conn->handlers; item; item = item->next) { for (item = conn->handlers; item; item = item->next) {
if (item->handler == (void *)handler) if (item->handler == (void *)handler)
break; break;
} }
if (item) return; if (item) return;
@@ -385,42 +385,42 @@ static void _handler_add(xmpp_conn_t * const conn,
item->userdata = userdata; item->userdata = userdata;
item->enabled = 0; item->enabled = 0;
item->next = NULL; item->next = NULL;
if (ns) { if (ns) {
item->ns = xmpp_strdup(conn->ctx, ns); item->ns = xmpp_strdup(conn->ctx, ns);
if (!item->ns) { if (!item->ns) {
xmpp_free(conn->ctx, item); xmpp_free(conn->ctx, item);
return; return;
} }
} else } else
item->ns = NULL; item->ns = NULL;
if (name) { if (name) {
item->name = xmpp_strdup(conn->ctx, name); item->name = xmpp_strdup(conn->ctx, name);
if (!item->name) { if (!item->name) {
if (item->ns) xmpp_free(conn->ctx, item->ns); if (item->ns) xmpp_free(conn->ctx, item->ns);
xmpp_free(conn->ctx, item); xmpp_free(conn->ctx, item);
return; return;
} }
} else } else
item->name = NULL; item->name = NULL;
if (type) { if (type) {
item->type = xmpp_strdup(conn->ctx, type); item->type = xmpp_strdup(conn->ctx, type);
if (!item->type) { if (!item->type) {
if (item->ns) xmpp_free(conn->ctx, item->ns); if (item->ns) xmpp_free(conn->ctx, item->ns);
if (item->name) xmpp_free(conn->ctx, item->name); if (item->name) xmpp_free(conn->ctx, item->name);
xmpp_free(conn->ctx, item); xmpp_free(conn->ctx, item);
} }
} else } else
item->type = NULL; item->type = NULL;
/* append to list */ /* append to list */
if (!conn->handlers) if (!conn->handlers)
conn->handlers = item; conn->handlers = item;
else { else {
tail = conn->handlers; tail = conn->handlers;
while (tail->next) while (tail->next)
tail = tail->next; tail = tail->next;
tail->next = item; tail->next = item;
} }
} }
@@ -432,7 +432,7 @@ static void _handler_add(xmpp_conn_t * const conn,
* @ingroup Handlers * @ingroup Handlers
*/ */
void xmpp_handler_delete(xmpp_conn_t * const conn, void xmpp_handler_delete(xmpp_conn_t * const conn,
xmpp_handler handler) xmpp_handler handler)
{ {
xmpp_handlist_t *prev, *item; xmpp_handlist_t *prev, *item;
@@ -441,23 +441,23 @@ void xmpp_handler_delete(xmpp_conn_t * const conn,
prev = NULL; prev = NULL;
item = conn->handlers; item = conn->handlers;
while (item) { while (item) {
if (item->handler == (void *)handler) if (item->handler == (void *)handler)
break; break;
prev = item; prev = item;
item = item->next; item = item->next;
} }
if (item) { if (item) {
if (prev) if (prev)
prev->next = item->next; prev->next = item->next;
else else
conn->handlers = item->next; conn->handlers = item->next;
if (item->ns) xmpp_free(conn->ctx, item->ns); if (item->ns) xmpp_free(conn->ctx, item->ns);
if (item->name) xmpp_free(conn->ctx, item->name); if (item->name) xmpp_free(conn->ctx, item->name);
if (item->type) xmpp_free(conn->ctx, item->type); if (item->type) xmpp_free(conn->ctx, item->type);
xmpp_free(conn->ctx, item); xmpp_free(conn->ctx, item);
} }
} }
@@ -466,7 +466,7 @@ void xmpp_handler_delete(xmpp_conn_t * const conn,
* and continue firing regularly after that. Strophe will try its best * and continue firing regularly after that. Strophe will try its best
* to fire handlers as close to the period times as it can, but accuracy * to fire handlers as close to the period times as it can, but accuracy
* will vary depending on the resolution of the event loop. * will vary depending on the resolution of the event loop.
* *
* If the handler function returns true, it will be kept, and if it * If the handler function returns true, it will be kept, and if it
* returns false, it will be deleted from the list of handlers. * returns false, it will be deleted from the list of handlers.
* *
@@ -478,9 +478,9 @@ void xmpp_handler_delete(xmpp_conn_t * const conn,
* @ingroup Handlers * @ingroup Handlers
*/ */
void xmpp_timed_handler_add(xmpp_conn_t * const conn, void xmpp_timed_handler_add(xmpp_conn_t * const conn,
xmpp_timed_handler handler, xmpp_timed_handler handler,
const unsigned long period, const unsigned long period,
void * const userdata) void * const userdata)
{ {
_timed_handler_add(conn, handler, period, userdata, 1); _timed_handler_add(conn, handler, period, userdata, 1);
} }
@@ -495,9 +495,9 @@ void xmpp_timed_handler_add(xmpp_conn_t * const conn,
* @param userdata an opaque data pointer that will be passed to the handler * @param userdata an opaque data pointer that will be passed to the handler
*/ */
void handler_add_timed(xmpp_conn_t * const conn, void handler_add_timed(xmpp_conn_t * const conn,
xmpp_timed_handler handler, xmpp_timed_handler handler,
const unsigned long period, const unsigned long period,
void * const userdata) void * const userdata)
{ {
_timed_handler_add(conn, handler, period, userdata, 0); _timed_handler_add(conn, handler, period, userdata, 0);
} }
@@ -519,9 +519,9 @@ void handler_add_timed(xmpp_conn_t * const conn,
* @ingroup Handlers * @ingroup Handlers
*/ */
void xmpp_id_handler_add(xmpp_conn_t * const conn, void xmpp_id_handler_add(xmpp_conn_t * const conn,
xmpp_handler handler, xmpp_handler handler,
const char * const id, const char * const id,
void * const userdata) void * const userdata)
{ {
_id_handler_add(conn, handler, id, userdata, 1); _id_handler_add(conn, handler, id, userdata, 1);
} }
@@ -536,9 +536,9 @@ void xmpp_id_handler_add(xmpp_conn_t * const conn,
* @param userdata an opaque data pointer that will be passed to the handler * @param userdata an opaque data pointer that will be passed to the handler
*/ */
void handler_add_id(xmpp_conn_t * const conn, void handler_add_id(xmpp_conn_t * const conn,
xmpp_handler handler, xmpp_handler handler,
const char * const id, const char * const id,
void * const userdata) void * const userdata)
{ {
_id_handler_add(conn, handler, id, userdata, 0); _id_handler_add(conn, handler, id, userdata, 0);
} }
@@ -566,11 +566,11 @@ void handler_add_id(xmpp_conn_t * const conn,
* @ingroup Handlers * @ingroup Handlers
*/ */
void xmpp_handler_add(xmpp_conn_t * const conn, void xmpp_handler_add(xmpp_conn_t * const conn,
xmpp_handler handler, xmpp_handler handler,
const char * const ns, const char * const ns,
const char * const name, const char * const name,
const char * const type, const char * const type,
void * const userdata) void * const userdata)
{ {
_handler_add(conn, handler, ns, name, type, userdata, 1); _handler_add(conn, handler, ns, name, type, userdata, 1);
} }
@@ -587,11 +587,11 @@ void xmpp_handler_add(xmpp_conn_t * const conn,
* @param userdata an opaque data pointer that will be passed to the handler * @param userdata an opaque data pointer that will be passed to the handler
*/ */
void handler_add(xmpp_conn_t * const conn, void handler_add(xmpp_conn_t * const conn,
xmpp_handler handler, xmpp_handler handler,
const char * const ns, const char * const ns,
const char * const name, const char * const name,
const char * const type, const char * const type,
void * const userdata) void * const userdata)
{ {
_handler_add(conn, handler, ns, name, type, userdata, 0); _handler_add(conn, handler, ns, name, type, userdata, 0);
} }