From ae0ada316eebd358616786498e852906c1bbe2ba Mon Sep 17 00:00:00 2001 From: Tony Olagbaiye Date: Mon, 28 Jun 2021 02:00:49 +0100 Subject: [PATCH] basic function --- xmpp-config.c | 24 ++----- xmpp-config.h | 4 +- xmpp-connection.c | 159 ++++++++++++++++++++++++++++++++++++++++++---- xmpp-connection.h | 4 +- xmpp.c | 21 ++---- 5 files changed, 166 insertions(+), 46 deletions(-) diff --git a/xmpp-config.c b/xmpp-config.c index 5783fdf..2169e49 100644 --- a/xmpp-config.c +++ b/xmpp-config.c @@ -15,8 +15,8 @@ struct t_config_file *xmpp_config_file; //struct t_config_section *xmpp_config_section_workspace_default; //struct t_config_section *xmpp_config_section_workspace; -struct t_config_option *xmpp_config_serverdef_jid; -struct t_config_option *xmpp_config_serverdef_password; +struct t_config_option *xmpp_config_server_jid; +struct t_config_option *xmpp_config_server_password; struct t_config_option *xmpp_config_look_nick_completion_smart; /* @@ -238,7 +238,6 @@ int xmpp_config_reload (const void *pointer, void *data, int xmpp_config_init() { - struct t_config_section *ptr_section_serverdef; struct t_config_section *ptr_section_server; struct t_config_section *ptr_section_look; @@ -248,15 +247,6 @@ int xmpp_config_init() if(!xmpp_config_file) return 0; - ptr_section_serverdef = weechat_config_new_section( - xmpp_config_file, "server_default", - 0, 0, - NULL, NULL, NULL, - NULL, NULL, NULL, - NULL, NULL, NULL, - NULL, NULL, NULL, - NULL, NULL, NULL); - ptr_section_server = weechat_config_new_section( xmpp_config_file, "server", 0, 0, @@ -275,7 +265,7 @@ int xmpp_config_init() NULL, NULL, NULL, NULL, NULL, NULL); - if (!ptr_section_serverdef + if (!ptr_section_server || !ptr_section_server || !ptr_section_look) { @@ -284,15 +274,15 @@ int xmpp_config_init() return 0; } - xmpp_config_serverdef_jid = weechat_config_new_option ( - xmpp_config_file, ptr_section_serverdef, + xmpp_config_server_jid = weechat_config_new_option ( + xmpp_config_file, ptr_section_server, "jid", "string", N_("XMPP Server JID"), NULL, 0, 0, "", "", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); - xmpp_config_serverdef_password = weechat_config_new_option ( - xmpp_config_file, ptr_section_serverdef, + xmpp_config_server_password = weechat_config_new_option ( + xmpp_config_file, ptr_section_server, "password", "string", N_("XMPP Server Password"), NULL, 0, 0, "", "", 0, diff --git a/xmpp-config.h b/xmpp-config.h index c514960..128d8a4 100644 --- a/xmpp-config.h +++ b/xmpp-config.h @@ -19,8 +19,8 @@ extern struct t_config_file *xmpp_config_file; //extern struct t_config_section *xmpp_config_section_workspace_default; //extern struct t_config_section *xmpp_config_section_workspace; -extern struct t_config_option *xmpp_config_serverdef_jid; -extern struct t_config_option *xmpp_config_serverdef_password; +extern struct t_config_option *xmpp_config_server_jid; +extern struct t_config_option *xmpp_config_server_password; extern struct t_config_option *xmpp_config_look_nick_completion_smart; //extern struct t_config_option *xmpp_config_workspace_default[]; diff --git a/xmpp-connection.c b/xmpp-connection.c index 993996d..1fdc6d6 100644 --- a/xmpp-connection.c +++ b/xmpp-connection.c @@ -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_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_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_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 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, diff --git a/xmpp-connection.h b/xmpp-connection.h index 339b6db..9d323fb 100644 --- a/xmpp-connection.h +++ b/xmpp-connection.h @@ -9,10 +9,12 @@ extern xmpp_conn_t *xmpp_connection; void xmpp_connection_init(); -void xmpp_connection_autoconnect(); +int xmpp_connection_autoconnect(const void *pointer, void *data, int remaining_calls); void xmpp_connection_connect(const char* jid, const char* password); +int xmpp_connection_check_events(const void *pointer, void *data, int remaining_calls); + int xmpp_connection_route_message(xmpp_conn_t *connection, const char *type, json_object *message); diff --git a/xmpp.c b/xmpp.c index 788f573..6138be1 100644 --- a/xmpp.c +++ b/xmpp.c @@ -31,15 +31,6 @@ struct t_hook *xmpp_hook_timer = NULL; struct t_gui_bar_item *xmpp_typing_bar_item = NULL; -/* -void connection_check_events(void) -{ - conn.xmpp_in_event_loop = TRUE; - xmpp_run_once(conn.xmpp_ctx, 10); - conn.xmpp_in_event_loop = FALSE; -} -*/ - int weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[]) { (void) argc; @@ -56,13 +47,15 @@ int weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char *argv[]) xmpp_command_init(); - xmpp_connection_autoconnect(); - //xmpp_completion_init(); - //xmpp_hook_timer = weechat_hook_timer(0.1 * 1000, 0, 0, - // &xmpp_workspace_timer_cb, - // NULL, NULL); + xmpp_hook_timer = weechat_hook_timer(1 * 1000, 0, 1, + &xmpp_connection_autoconnect, + NULL, NULL); + + xmpp_hook_timer = weechat_hook_timer(0.1 * 1000, 0, 0, + &xmpp_connection_check_events, + NULL, NULL); if (!weechat_bar_search("typing")) {