Generate a unique ID per message

Add a ID per message. Seems to fix the duplicate messages seen with MUCs
on the Conversations android client whenever the network is switched
(e.g., from wifi to cell).

Introduce a dependency on Linux's libuuid which introduces portability
issues. However, it should be simple to add support for the BSD UUID
interface.

libuuid was used because it is a fast and simple way of generating
a unique id.  Realistically, the ID only needs to be unique within
the MUC and so could probably be replaced with an sprintf() combining
time of day, PID and a random number.
pull/1/head
Michael Santos 9 years ago
parent 4325445536
commit 353951298e

@ -1,7 +1,7 @@
RM=rm
all:
$(CC) -g -Wall $(CFLAGS) -o xmppipe src/*.c $(LDFLAGS) -lstrophe
$(CC) -g -Wall $(CFLAGS) -o xmppipe src/*.c $(LDFLAGS) -lstrophe -luuid
clean:
-@$(RM) xmppipe

@ -145,7 +145,9 @@ main(int argc, char **argv)
state->mucjid = xmppipe_mucjid(state->out, state->resource);
}
encode_init();
if (xmppipe_encode_init() < 0)
errx(EXIT_FAILURE, "xmppipe_encode_init");
xmpp_initialize();
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
@ -366,7 +368,7 @@ handle_stdin(xmppipe_state_t *state, int fd, char *buf, size_t len)
if ((state->opt & XMPPIPE_OPT_DISCARD) && state->occupants == 0) {
if (state->opt & XMPPIPE_OPT_DISCARD_TO_STDOUT) {
char *enc = NULL;
enc = encode(buf);
enc = xmppipe_encode(buf);
(void)printf("!:%s\n", enc);
free(enc);
}
@ -598,9 +600,9 @@ handle_presence(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
if (state->status == XMPPIPE_S_READY_AVAIL && state->occupants == 0)
state->status = XMPPIPE_S_READY_EMPTY;
etype = encode(type);
efrom = encode(from);
eto = encode(to);
etype = xmppipe_encode(type);
efrom = xmppipe_encode(from);
eto = xmppipe_encode(to);
(void)printf("p:%s:%s:%s\n", etype, efrom, eto);
state->interval = 0;
@ -687,9 +689,9 @@ handle_message(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
if (!message)
return 1;
etype = encode(type);
efrom = encode(from);
emessage = encode(message);
etype = xmppipe_encode(type);
efrom = xmppipe_encode(from);
emessage = xmppipe_encode(message);
(void)printf("m:%s:%s:%s\n", etype, efrom, emessage);
state->interval = 0;
@ -782,11 +784,15 @@ xmppipe_send_message(xmppipe_state_t *state, char *to, char *type, char *buf)
xmpp_stanza_t *message = NULL;
xmpp_stanza_t *body = NULL;
xmpp_stanza_t *text = NULL;
char *id = NULL;
id = xmppipe_id_alloc();
message = xmpp_stanza_new(state->ctx);
xmpp_stanza_set_name(message, "message");
xmpp_stanza_set_type(message, type);
xmpp_stanza_set_attribute(message, "to", to);
xmpp_stanza_set_id(message, id);
body = xmpp_stanza_new(state->ctx);
xmpp_stanza_set_name(body, "body");
@ -799,6 +805,7 @@ xmppipe_send_message(xmppipe_state_t *state, char *to, char *type, char *buf)
xmpp_send(state->conn, message);
xmpp_stanza_release(message);
free(id);
}
void

@ -73,8 +73,9 @@ typedef struct {
} xmppipe_state_t;
void encode_init();
char *encode(const char *);
int xmppipe_encode_init();
char *xmppipe_encode(const char *);
char *xmppipe_id_alloc();
int xmppipe_set_nonblock(int fd);
char *xmppipe_servername(char *);

@ -16,17 +16,19 @@
static unsigned char rfc3986[256];
void
encode_init()
int
xmppipe_encode_init()
{
int i = 0;
for (i = 0; i < 256; i++)
rfc3986[i] = isalnum(i) || i == '~' || i == '-' || i == '.' || i == '_'
? i : 0;
return 0;
}
char *
encode(const char *s)
xmppipe_encode(const char *s)
{
// XXX overflow
size_t len = strlen(s);

@ -0,0 +1,30 @@
/* Copyright (c) 2015, Michael Santos <michael.santos@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "xmppipe.h"
#include <uuid/uuid.h>
char *
xmppipe_id_alloc()
{
uuid_t uu = {0};
char *out = NULL;
out = xmppipe_calloc(1,37);
uuid_generate(uu);
uuid_unparse(uu, out);
return out;
}
Loading…
Cancel
Save