|
|
|
@ -22,14 +22,16 @@ void xmpp_log_emit_weechat(void *const userdata, const xmpp_log_level_t level, c
|
|
|
|
|
{
|
|
|
|
|
(void) userdata;
|
|
|
|
|
|
|
|
|
|
static const char *log_level_name[4] = {"debug", "info", "warn", "error"};
|
|
|
|
|
|
|
|
|
|
time_t date = time(NULL);
|
|
|
|
|
const char *timestamp = weechat_util_get_time_string(&date);
|
|
|
|
|
|
|
|
|
|
weechat_printf(
|
|
|
|
|
NULL,
|
|
|
|
|
_("%s%s %d | %s: %s - %s"),
|
|
|
|
|
weechat_prefix("error"), XMPP_PLUGIN_NAME,
|
|
|
|
|
level, timestamp, area, msg);
|
|
|
|
|
_("%s%s/%s (%s): %s"),
|
|
|
|
|
weechat_prefix("error"), XMPP_PLUGIN_NAME, area,
|
|
|
|
|
log_level_name[level], msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xmpp_log_t xmpp_logger = {
|
|
|
|
@ -42,24 +44,142 @@ void xmpp_connection_init()
|
|
|
|
|
xmpp_initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void xmpp_connection_autoconnect()
|
|
|
|
|
int xmpp_connection_autoconnect(const void *pointer, void *data, int remaining_calls)
|
|
|
|
|
{
|
|
|
|
|
xmpp_connection_connect(weechat_config_string(xmpp_config_server_jid),
|
|
|
|
|
weechat_config_string(xmpp_config_server_password));
|
|
|
|
|
|
|
|
|
|
return WEECHAT_RC_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int version_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
|
|
|
|
|
{
|
|
|
|
|
xmpp_connection_connect(weechat_config_string(xmpp_config_serverdef_jid),
|
|
|
|
|
weechat_config_string(xmpp_config_serverdef_password));
|
|
|
|
|
weechat_printf(NULL, _("xmpp: %s # %s"),
|
|
|
|
|
weechat_config_string(xmpp_config_serverdef_jid),
|
|
|
|
|
weechat_config_string(xmpp_config_serverdef_password));
|
|
|
|
|
xmpp_stanza_t *reply, *query, *name, *version, *text;
|
|
|
|
|
const char *ns;
|
|
|
|
|
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
|
|
|
|
|
|
|
|
|
|
weechat_printf(NULL, "Received version request from %s", xmpp_stanza_get_from(stanza));
|
|
|
|
|
|
|
|
|
|
reply = xmpp_stanza_reply(stanza);
|
|
|
|
|
xmpp_stanza_set_type(reply, "result");
|
|
|
|
|
|
|
|
|
|
query = xmpp_stanza_new(ctx);
|
|
|
|
|
xmpp_stanza_set_name(query, "query");
|
|
|
|
|
ns = xmpp_stanza_get_ns(xmpp_stanza_get_children(stanza));
|
|
|
|
|
if (ns) {
|
|
|
|
|
xmpp_stanza_set_ns(query, ns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name = xmpp_stanza_new(ctx);
|
|
|
|
|
xmpp_stanza_set_name(name, "name");
|
|
|
|
|
xmpp_stanza_add_child(query, name);
|
|
|
|
|
xmpp_stanza_release(name);
|
|
|
|
|
|
|
|
|
|
text = xmpp_stanza_new(ctx);
|
|
|
|
|
xmpp_stanza_set_text(text, "libstrophe example bot");
|
|
|
|
|
xmpp_stanza_add_child(name, text);
|
|
|
|
|
xmpp_stanza_release(text);
|
|
|
|
|
|
|
|
|
|
version = xmpp_stanza_new(ctx);
|
|
|
|
|
xmpp_stanza_set_name(version, "version");
|
|
|
|
|
xmpp_stanza_add_child(query, version);
|
|
|
|
|
xmpp_stanza_release(version);
|
|
|
|
|
|
|
|
|
|
text = xmpp_stanza_new(ctx);
|
|
|
|
|
xmpp_stanza_set_text(text, "1.0");
|
|
|
|
|
xmpp_stanza_add_child(version, text);
|
|
|
|
|
xmpp_stanza_release(text);
|
|
|
|
|
|
|
|
|
|
xmpp_stanza_add_child(reply, query);
|
|
|
|
|
xmpp_stanza_release(query);
|
|
|
|
|
|
|
|
|
|
xmpp_send(conn, reply);
|
|
|
|
|
xmpp_stanza_release(reply);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
|
|
|
|
|
{
|
|
|
|
|
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
|
|
|
|
|
xmpp_stanza_t *body, *reply;
|
|
|
|
|
const char *type;
|
|
|
|
|
char *intext, *replytext;
|
|
|
|
|
int quit = 0;
|
|
|
|
|
|
|
|
|
|
body = xmpp_stanza_get_child_by_name(stanza, "body");
|
|
|
|
|
if (body == NULL)
|
|
|
|
|
return 1;
|
|
|
|
|
type = xmpp_stanza_get_type(stanza);
|
|
|
|
|
if (type != NULL && strcmp(type, "error") == 0)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
intext = xmpp_stanza_get_text(body);
|
|
|
|
|
|
|
|
|
|
weechat_printf(NULL, "Incoming message from %s: %s", xmpp_stanza_get_from(stanza),
|
|
|
|
|
intext);
|
|
|
|
|
|
|
|
|
|
reply = xmpp_stanza_reply(stanza);
|
|
|
|
|
if (xmpp_stanza_get_type(reply) == NULL)
|
|
|
|
|
xmpp_stanza_set_type(reply, "chat");
|
|
|
|
|
|
|
|
|
|
if (strcmp(intext, "quit") == 0) {
|
|
|
|
|
replytext = strdup("bye!");
|
|
|
|
|
quit = 1;
|
|
|
|
|
} else {
|
|
|
|
|
replytext = (char *)malloc(strlen(" to you too!") + strlen(intext) + 1);
|
|
|
|
|
strcpy(replytext, intext);
|
|
|
|
|
strcat(replytext, " to you too!");
|
|
|
|
|
}
|
|
|
|
|
xmpp_free(ctx, intext);
|
|
|
|
|
xmpp_message_set_body(reply, replytext);
|
|
|
|
|
|
|
|
|
|
xmpp_send(conn, reply);
|
|
|
|
|
xmpp_stanza_release(reply);
|
|
|
|
|
free(replytext);
|
|
|
|
|
|
|
|
|
|
if (quit)
|
|
|
|
|
xmpp_disconnect(conn);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void xmpp_connection_on_connected(xmpp_conn_t *conn, xmpp_conn_event_t status,
|
|
|
|
|
int error, xmpp_stream_error_t *stream_error,
|
|
|
|
|
void *userdata)
|
|
|
|
|
{
|
|
|
|
|
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
|
|
|
|
|
|
|
|
|
|
(void)error;
|
|
|
|
|
(void)stream_error;
|
|
|
|
|
|
|
|
|
|
if (status == XMPP_CONN_CONNECT) {
|
|
|
|
|
xmpp_stanza_t *pres;
|
|
|
|
|
weechat_printf(NULL, "DEBUG: connected");
|
|
|
|
|
xmpp_handler_add(conn, version_handler, "jabber:iq:version", "iq", NULL,
|
|
|
|
|
ctx);
|
|
|
|
|
xmpp_handler_add(conn, message_handler, NULL, "message", NULL, ctx);
|
|
|
|
|
|
|
|
|
|
/* Send initial <presence/> so that we appear online to contacts */
|
|
|
|
|
pres = xmpp_presence_new(ctx);
|
|
|
|
|
xmpp_send(conn, pres);
|
|
|
|
|
xmpp_stanza_release(pres);
|
|
|
|
|
} else {
|
|
|
|
|
weechat_printf(NULL, "DEBUG: disconnected");
|
|
|
|
|
xmpp_stop(ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void xmpp_connection_connect(const char* jid, const char* password)
|
|
|
|
|
{
|
|
|
|
|
xmpp_ctx_t *xmpp_context = xmpp_ctx_new(NULL, &xmpp_logger);
|
|
|
|
|
|
|
|
|
|
xmpp_conn_t *xmpp_connection = xmpp_conn_new(xmpp_context);
|
|
|
|
|
|
|
|
|
|
xmpp_connection = xmpp_conn_new(xmpp_context);
|
|
|
|
|
xmpp_conn_set_jid(xmpp_connection, jid);
|
|
|
|
|
xmpp_conn_set_pass(xmpp_connection, password);
|
|
|
|
|
auto flags = xmpp_conn_get_flags(xmpp_connection);
|
|
|
|
|
//flags |= XMPP_CONN_FLAG_TRUST_TLS;
|
|
|
|
|
xmpp_conn_set_flags(xmpp_connection, flags);
|
|
|
|
|
xmpp_connect_client(xmpp_connection, NULL, 0, xmpp_connection_on_connected, xmpp_context);
|
|
|
|
|
//struct lws_context_creation_info ctxinfo;
|
|
|
|
|
//struct lws_client_connect_info ccinfo;
|
|
|
|
|
//const char *url_protocol, *url_path;
|
|
|
|
@ -121,7 +241,22 @@ void xmpp_connection_connect(const char* jid, const char* password)
|
|
|
|
|
//ccinfo.userdata = workspace;
|
|
|
|
|
|
|
|
|
|
//lws_client_connect_via_info(&ccinfo);
|
|
|
|
|
return xmpp_connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int xmpp_connection_check_events(const void *pointer, void *data, int remaining_calls)
|
|
|
|
|
{
|
|
|
|
|
(void) pointer;
|
|
|
|
|
(void) data;
|
|
|
|
|
(void) remaining_calls;
|
|
|
|
|
|
|
|
|
|
if (xmpp_connection)
|
|
|
|
|
{
|
|
|
|
|
xmpp_ctx_t *xmpp_context = xmpp_conn_get_context(xmpp_connection);
|
|
|
|
|
|
|
|
|
|
xmpp_run_once(xmpp_context, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return WEECHAT_RC_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int xmpp_connection_route_message(xmpp_conn_t *workspace,
|
|
|
|
|