mirror of
https://github.com/profanity-im/profanity.git
synced 2024-11-03 19:37:16 -05:00
Added /ping command
This commit is contained in:
parent
b50b786dcc
commit
4ba33cb13c
@ -722,6 +722,14 @@ static struct cmd_t command_defs[] =
|
||||
"A value of 0 will switch off autopinging the server.",
|
||||
NULL } } },
|
||||
|
||||
{ "/ping",
|
||||
cmd_ping, parse_args, 0, 1, NULL,
|
||||
{ "/ping [target]", "Send ping IQ request.",
|
||||
{ "/ping [rarget]",
|
||||
"--------------",
|
||||
"Sends an IQ ping stanza to the specificed target.",
|
||||
NULL } } },
|
||||
|
||||
{ "/autoaway",
|
||||
cmd_autoaway, parse_args_with_freetext, 2, 2, &cons_autoaway_setting,
|
||||
{ "/autoaway setting value", "Set auto idle/away properties.",
|
||||
@ -1592,7 +1600,7 @@ _cmd_complete_parameters(char *input, int *size)
|
||||
}
|
||||
}
|
||||
|
||||
gchar *resource_choices[] = { "/caps", "/software" };
|
||||
gchar *resource_choices[] = { "/caps", "/software", "/ping" };
|
||||
for (i = 0; i < ARRAY_SIZE(resource_choices); i++) {
|
||||
result = autocomplete_param_with_func(input, size, resource_choices[i],
|
||||
roster_find_resource);
|
||||
|
@ -2576,6 +2576,26 @@ cmd_autoping(gchar **args, struct cmd_help_t help)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
cmd_ping(gchar **args, struct cmd_help_t help)
|
||||
{
|
||||
jabber_conn_status_t conn_status = jabber_get_connection_status();
|
||||
|
||||
if (conn_status != JABBER_CONNECTED) {
|
||||
cons_show("You are not currenlty connected.");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
iq_send_ping(args[0]);
|
||||
|
||||
if (args[0] == NULL) {
|
||||
cons_show("Pinged server...");
|
||||
} else {
|
||||
cons_show("Pinged %s...", args[0]);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
cmd_autoaway(gchar **args, struct cmd_help_t help)
|
||||
{
|
||||
|
@ -124,5 +124,6 @@ gboolean cmd_wins(gchar **args, struct cmd_help_t help);
|
||||
gboolean cmd_xa(gchar **args, struct cmd_help_t help);
|
||||
gboolean cmd_alias(gchar **args, struct cmd_help_t help);
|
||||
gboolean cmd_xmlconsole(gchar **args, struct cmd_help_t help);
|
||||
gboolean cmd_ping(gchar **args, struct cmd_help_t help);
|
||||
|
||||
#endif
|
||||
|
@ -579,3 +579,13 @@ handle_xmpp_stanza(const char * const msg)
|
||||
{
|
||||
ui_handle_stanza(msg);
|
||||
}
|
||||
|
||||
void
|
||||
handle_ping_result(const char * const from, int millis)
|
||||
{
|
||||
if (from == NULL) {
|
||||
cons_show("Ping response from server: %dms.", millis);
|
||||
} else {
|
||||
cons_show("Ping response from %s: %dms.", from, millis);
|
||||
}
|
||||
}
|
||||
|
@ -94,5 +94,6 @@ void handle_message_error(const char * const from, const char * const type,
|
||||
void handle_presence_error(const char *from, const char * const type,
|
||||
const char *err_msg);
|
||||
void handle_xmpp_stanza(const char * const msg);
|
||||
void handle_ping_result(const char * const from, int millis);
|
||||
|
||||
#endif
|
||||
|
@ -75,6 +75,8 @@ static int _disco_items_get_handler(xmpp_conn_t * const conn,
|
||||
xmpp_stanza_t * const stanza, void * const userdata);
|
||||
static int _destroy_room_result_handler(xmpp_conn_t * const conn,
|
||||
xmpp_stanza_t * const stanza, void * const userdata);
|
||||
static int _manual_pong_handler(xmpp_conn_t *const conn,
|
||||
xmpp_stanza_t * const stanza, void * const userdata);
|
||||
static int _ping_timed_handler(xmpp_conn_t * const conn,
|
||||
void * const userdata);
|
||||
|
||||
@ -186,6 +188,21 @@ _iq_destroy_instant_room(const char * const room_jid)
|
||||
xmpp_stanza_release(iq);
|
||||
}
|
||||
|
||||
static void
|
||||
_iq_send_ping(const char * const target)
|
||||
{
|
||||
xmpp_conn_t * const conn = connection_get_conn();
|
||||
xmpp_ctx_t * const ctx = connection_get_ctx();
|
||||
xmpp_stanza_t *iq = stanza_create_ping_iq(ctx, target);
|
||||
char *id = xmpp_stanza_get_id(iq);
|
||||
|
||||
GDateTime *now = g_date_time_new_now_local();
|
||||
xmpp_id_handler_add(conn, _manual_pong_handler, id, now);
|
||||
|
||||
xmpp_send(conn, iq);
|
||||
xmpp_stanza_release(iq);
|
||||
}
|
||||
|
||||
static int
|
||||
_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
||||
void * const userdata)
|
||||
@ -240,6 +257,26 @@ _pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_manual_pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza,
|
||||
void * const userdata)
|
||||
{
|
||||
GDateTime *sent = (GDateTime *)userdata;
|
||||
GDateTime *now = g_date_time_new_now_local();
|
||||
|
||||
GTimeSpan elapsed = g_date_time_difference(now, sent);
|
||||
int elapsed_millis = elapsed / 1000;
|
||||
|
||||
g_date_time_unref(sent);
|
||||
g_date_time_unref(now);
|
||||
|
||||
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||
|
||||
handle_ping_result(from, elapsed_millis);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_ping_timed_handler(xmpp_conn_t * const conn, void * const userdata)
|
||||
{
|
||||
@ -247,7 +284,7 @@ _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata)
|
||||
|
||||
if (jabber_get_connection_status() == JABBER_CONNECTED) {
|
||||
|
||||
xmpp_stanza_t *iq = stanza_create_ping_iq(ctx);
|
||||
xmpp_stanza_t *iq = stanza_create_ping_iq(ctx, NULL);
|
||||
char *id = xmpp_stanza_get_id(iq);
|
||||
|
||||
// add pong handler
|
||||
@ -762,4 +799,5 @@ iq_init_module(void)
|
||||
iq_set_autoping = _iq_set_autoping;
|
||||
iq_confirm_instant_room = _iq_confirm_instant_room;
|
||||
iq_destroy_instant_room = _iq_destroy_instant_room;
|
||||
iq_send_ping = _iq_send_ping;
|
||||
}
|
||||
|
@ -579,11 +579,14 @@ stanza_contains_chat_state(xmpp_stanza_t *stanza)
|
||||
}
|
||||
|
||||
xmpp_stanza_t *
|
||||
stanza_create_ping_iq(xmpp_ctx_t *ctx)
|
||||
stanza_create_ping_iq(xmpp_ctx_t *ctx, const char * const target)
|
||||
{
|
||||
xmpp_stanza_t *iq = xmpp_stanza_new(ctx);
|
||||
xmpp_stanza_set_name(iq, STANZA_NAME_IQ);
|
||||
xmpp_stanza_set_type(iq, STANZA_TYPE_GET);
|
||||
if (target != NULL) {
|
||||
xmpp_stanza_set_attribute(iq, STANZA_ATTR_TO, target);
|
||||
}
|
||||
char *id = create_unique_id("ping");
|
||||
xmpp_stanza_set_id(iq, id);
|
||||
free(id);
|
||||
|
@ -185,7 +185,7 @@ xmpp_stanza_t* stanza_create_room_leave_presence(xmpp_ctx_t *ctx,
|
||||
xmpp_stanza_t* stanza_create_presence(xmpp_ctx_t * const ctx);
|
||||
|
||||
xmpp_stanza_t* stanza_create_roster_iq(xmpp_ctx_t *ctx);
|
||||
xmpp_stanza_t* stanza_create_ping_iq(xmpp_ctx_t *ctx);
|
||||
xmpp_stanza_t* stanza_create_ping_iq(xmpp_ctx_t *ctx, const char * const target);
|
||||
xmpp_stanza_t* stanza_create_disco_info_iq(xmpp_ctx_t *ctx, const char * const id,
|
||||
const char * const to, const char * const node);
|
||||
|
||||
|
@ -141,6 +141,7 @@ void (*iq_disco_items_request)(gchar *jid);
|
||||
void (*iq_set_autoping)(int seconds);
|
||||
void (*iq_confirm_instant_room)(const char * const room_jid);
|
||||
void (*iq_destroy_instant_room)(const char * const room_jid);
|
||||
void (*iq_send_ping)(const char * const target);
|
||||
|
||||
// caps functions
|
||||
Capabilities* (*caps_get)(const char * const caps_str);
|
||||
|
Loading…
Reference in New Issue
Block a user