2017-01-29 14:13:18 +00:00
|
|
|
/* Copyright (c) 2015-2017, Michael Santos <michael.santos@gmail.com>
|
2015-05-11 19:19:35 +00:00
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
|
|
|
|
#include <strophe.h>
|
|
|
|
|
2015-07-01 13:12:16 +00:00
|
|
|
#define XMPPIPE_VERSION "0.7.1"
|
2015-05-11 19:19:35 +00:00
|
|
|
|
2015-07-21 13:44:08 +00:00
|
|
|
#define XMPPIPE_STREQ(a,b) (strcmp((a),(b)) == 0)
|
|
|
|
#define XMPPIPE_STRNEQ(a,b) (strcmp((a),(b)) != 0)
|
2015-05-11 19:19:35 +00:00
|
|
|
|
2015-05-24 14:27:59 +00:00
|
|
|
#define BASE64_LENGTH(n) ((((n) + 2) / 3) * 4)
|
|
|
|
|
2015-05-11 19:19:35 +00:00
|
|
|
enum {
|
|
|
|
XMPPIPE_S_DISCONNECTED,
|
|
|
|
XMPPIPE_S_CONNECTING,
|
|
|
|
XMPPIPE_S_CONNECTED,
|
|
|
|
|
|
|
|
XMPPIPE_S_MUC_SERVICE_LOOKUP,
|
|
|
|
XMPPIPE_S_MUC_FEATURE_LOOKUP,
|
|
|
|
XMPPIPE_S_MUC_WAITJOIN,
|
|
|
|
XMPPIPE_S_MUC_JOIN,
|
|
|
|
XMPPIPE_S_MUC_UNLOCK,
|
|
|
|
|
|
|
|
XMPPIPE_S_READY,
|
|
|
|
XMPPIPE_S_READY_AVAIL,
|
|
|
|
XMPPIPE_S_READY_EMPTY,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
XMPPIPE_OPT_DISCARD = 1 << 0, /* Throw away stdin if no occupants in MUC */
|
|
|
|
XMPPIPE_OPT_DISCARD_TO_STDOUT = 1 << 1, /* Throw away stdin and send to local stdout */
|
|
|
|
XMPPIPE_OPT_EOF = 1 << 2, /* Keep running on stdin EOF */
|
|
|
|
XMPPIPE_OPT_SIGPIPE = 1 << 3, /* Exit if no occupants in MUC */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
xmpp_ctx_t *ctx;
|
|
|
|
xmpp_conn_t *conn;
|
2015-05-16 18:45:22 +00:00
|
|
|
int handled;
|
2015-05-11 19:19:35 +00:00
|
|
|
|
|
|
|
char *room; /* room, room@conference.xmpp.example.com */
|
|
|
|
char *server; /* xmpp.example.com */
|
|
|
|
char *resource; /* nick */
|
|
|
|
char *mucservice; /* conference.xmpp.example.com */
|
|
|
|
char *mucjid; /* room@conference.xmpp.example.com/nick */
|
|
|
|
char *subject; /* topic/subject for MUC */
|
|
|
|
char *out; /* room@conference.xmpp.example.com */
|
|
|
|
|
|
|
|
int status;
|
|
|
|
int occupants;
|
|
|
|
u_int32_t poll; /* milliseconds */
|
|
|
|
u_int32_t keepalive; /* periodically send a keepalive (milliseconds) */
|
2015-05-16 14:55:26 +00:00
|
|
|
u_int32_t keepalive_fail; /* number of consecutive keepalives without a reply */
|
|
|
|
u_int32_t keepalive_limit; /* number of keepalives without a reply */
|
|
|
|
u_int32_t interval; /* time since last keepalive (milliseconds) */
|
2015-05-11 19:19:35 +00:00
|
|
|
size_t bufsz; /* size of read buffer */
|
|
|
|
|
2015-05-16 18:45:22 +00:00
|
|
|
int sm_enabled; /* stanzas: iq, message, presence */
|
|
|
|
|
|
|
|
u_int32_t sm_request; /* count of sent stanzas */
|
2015-06-01 13:39:22 +00:00
|
|
|
u_int32_t sm_request_unack; /* count of unacknowledged stream management requests */
|
2015-05-16 18:45:22 +00:00
|
|
|
u_int32_t sm_request_interval; /* request ack every interval stanzas */
|
|
|
|
|
|
|
|
u_int32_t sm_ack_recv; /* count of stanzas received from server */
|
2015-06-01 13:39:22 +00:00
|
|
|
u_int32_t sm_ack_sent; /* server's count of stanzas we've sent */
|
|
|
|
|
|
|
|
u_int32_t sm_unacked;
|
|
|
|
u_int32_t sm_fc;
|
2015-05-16 18:45:22 +00:00
|
|
|
|
2015-05-11 19:19:35 +00:00
|
|
|
int opt;
|
|
|
|
int verbose;
|
2015-05-24 14:27:59 +00:00
|
|
|
int encode; /* base64 encode/decode data to MUC */
|
2015-05-11 19:19:35 +00:00
|
|
|
} xmppipe_state_t;
|
|
|
|
|
|
|
|
|
2015-05-24 14:27:59 +00:00
|
|
|
int xmppipe_fmt_init();
|
|
|
|
char *xmppipe_fmt(const char *);
|
|
|
|
char *xmppipe_nfmt(const char *, size_t);
|
2015-05-14 10:55:48 +00:00
|
|
|
char *xmppipe_id_alloc();
|
2015-05-11 19:19:35 +00:00
|
|
|
int xmppipe_set_nonblock(int fd);
|
|
|
|
|
|
|
|
char *xmppipe_servername(char *);
|
|
|
|
char *xmppipe_roomname(char *);
|
|
|
|
char *xmppipe_conference(char *, char *);
|
|
|
|
char *xmppipe_mucjid(char *, char *);
|
|
|
|
|
|
|
|
char *xmppipe_getenv(const char *);
|
|
|
|
char *xmppipe_strdup(const char *);
|
|
|
|
void *xmppipe_malloc(size_t);
|
|
|
|
void *xmppipe_calloc(size_t, size_t);
|
2015-05-22 13:48:15 +00:00
|
|
|
|
|
|
|
xmpp_stanza_t *xmppipe_stanza_new(xmpp_ctx_t *);
|
|
|
|
void xmppipe_stanza_set_attribute(xmpp_stanza_t * const, const char * const,
|
|
|
|
const char * const);
|
|
|
|
void xmppipe_stanza_set_id(xmpp_stanza_t * const, const char * const);
|
|
|
|
void xmppipe_stanza_set_name(xmpp_stanza_t *, const char * const);
|
|
|
|
void xmppipe_stanza_set_ns(xmpp_stanza_t * const, const char * const);
|
|
|
|
void xmppipe_stanza_set_text(xmpp_stanza_t *, const char * const);
|
|
|
|
void xmppipe_stanza_set_type(xmpp_stanza_t * const, const char * const);
|
|
|
|
void xmppipe_stanza_add_child(xmpp_stanza_t *, xmpp_stanza_t *);
|
2015-05-24 14:27:59 +00:00
|
|
|
|
2017-01-29 14:13:18 +00:00
|
|
|
int xmppipe_sandbox_init(xmppipe_state_t *state);
|
2017-02-01 15:25:38 +00:00
|
|
|
int xmppipe_conn_fd(xmppipe_state_t *state);
|
2017-01-29 14:13:18 +00:00
|
|
|
|
2015-05-24 14:27:59 +00:00
|
|
|
int b64_ntop(u_char const *src, size_t srclength, char *target,
|
|
|
|
size_t targsize);
|
|
|
|
int b64_pton(char const *src, u_char *target, size_t targsize);
|