format: use empty element to set default values

An empty string in the type, to and from uses a default value. For
example to send a message to the groupchat specified on the command
line:

~~~

m::::this is a test message
~~~
master
Michael Santos 6 years ago
parent 231bee7c74
commit 9598e01579

@ -1236,7 +1236,7 @@ xmppipe_send_stanza(xmppipe_state_t *state, char *buf0, size_t len)
buf = xmppipe_strdup(buf0);
tok[0] = strtok(buf, ":");
tok[0] = xmppipe_strtok(buf, ":");
switch (tok[0][0]) {
case 'm':
j = 5;
@ -1254,7 +1254,7 @@ xmppipe_send_stanza(xmppipe_state_t *state, char *buf0, size_t len)
while (tok[i] != NULL && i < j) {
i++;
tok[i] = strtok(NULL, ":");
tok[i] = xmppipe_strtok(NULL, ":");
if (state->verbose)
(void)fprintf(stderr, "message:%d:%s\n", i,
@ -1268,6 +1268,9 @@ xmppipe_send_stanza(xmppipe_state_t *state, char *buf0, size_t len)
return;
}
if (strlen(tok[1]) == 0)
tok[1] = (state->opt & XMPPIPE_OPT_GROUPCHAT) ? "groupchat" : "chat";
if (tok[2] == NULL) {
if (state->verbose)
(void)fprintf(stderr, "to address required\n");
@ -1275,6 +1278,9 @@ xmppipe_send_stanza(xmppipe_state_t *state, char *buf0, size_t len)
return;
}
if (strlen(tok[2]) == 0)
tok[2] = state->out;
if (tok[4] == NULL) {
if (state->verbose)
(void)fprintf(stderr, "body required\n");
@ -1300,6 +1306,7 @@ xmppipe_send_stanza(xmppipe_state_t *state, char *buf0, size_t len)
xmppipe_send_message(state, to, type, body, strlen(body));
XMPPIPE_ERR:
free(buf);
free(type);
free(to);
free(body);

@ -117,6 +117,7 @@ char *xmppipe_getenv(const char *);
char *xmppipe_strdup(const char *);
void *xmppipe_malloc(size_t);
void *xmppipe_calloc(size_t, size_t);
char *xmppipe_strtok(char *str, char const *delims);
xmpp_stanza_t *xmppipe_stanza_new(xmpp_ctx_t *);
void xmppipe_stanza_set_attribute(xmpp_stanza_t * const, const char * const,

@ -92,6 +92,30 @@ xmppipe_calloc(size_t nmemb, size_t size)
return buf;
}
// https://stackoverflow.com/a/30295426
char
*xmppipe_strtok(char *str, char const *delims)
{
static char *src = NULL;
char *p, *ret = 0;
if (str != NULL) src = str;
if (src == NULL || *src == '\0')
return NULL;
ret = src;
if ((p = strpbrk(src, delims)) != NULL)
{
*p = 0;
src = ++p;
}
else
src += strlen(src);
return ret;
}
xmpp_stanza_t *
xmppipe_stanza_new(xmpp_ctx_t *ctx)
{

Loading…
Cancel
Save