commit 988afea88bdacb7854ad9ff1d70fc011e97ab484
parent 206c7815ad9c3f962af66367e2af4ea86308e3c7
Author: hhvn <dev@hhvn.uk>
Date: Sun, 14 Nov 2021 13:16:41 +0000
commands.c handle.c hirc.h: /ping command
Diffstat:
3 files changed, 34 insertions(+), 0 deletions(-)
diff --git a/commands.c b/commands.c
@@ -27,6 +27,11 @@ static struct Command commands[] = {
{"part", command_part, {
"usage: /part <channel>",
"Part channel", NULL}},
+ {"ping", command_ping, {
+ "usage: /ping message...",
+ "Send a PING to server.",
+ "hirc will do this itself in the background,",
+ "but will hide it unless this command is used.", NULL}},
{"connect", command_connect, {
"usage: /connect [-network <name>] [-nick <nick>] [-user <user>]",
" [-real <comment>] [-tls] [-verify] <host> [port]",
@@ -74,6 +79,17 @@ command_part(struct Server *server, char *str) {
}
void
+command_ping(struct Server *server, char *str) {
+ if (!str) {
+ ui_error("/ping requires argument", NULL);
+ return;
+ }
+
+ ircprintf(server, "PING :%s\r\n", str);
+ handle_expect("PONG", str);
+}
+
+void
command_quote(struct Server *server, char *str) {
if (!str) {
ui_error("/quote requires argument", NULL);
diff --git a/handle.c b/handle.c
@@ -7,11 +7,13 @@
struct Expect expect[] = {
{ "JOIN", NULL },
{ "PART", NULL },
+ { "PONG", NULL },
{ NULL, NULL },
};
struct Handler handlers[] = {
{ "PING", handle_PING },
+ { "PONG", handle_PONG },
{ "JOIN", handle_JOIN },
{ "PART", handle_PART },
{ "QUIT", handle_QUIT },
@@ -39,6 +41,20 @@ handle_PING(char *msg, char **params, struct Server *server, time_t timestamp) {
}
void
+handle_PONG(char *msg, char **params, struct Server *server, time_t timestamp) {
+ if (**params == ':')
+ params++;
+
+ if (param_len(params) < 2)
+ return;
+
+ if (strcmp_n(*(params+1), handle_expect_get("PONG"))) {
+ hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL);
+ handle_expect("PONG", NULL);
+ }
+}
+
+void
handle_JOIN(char *msg, char **params, struct Server *server, time_t timestamp) {
struct Channel *chan;
struct Nick *nick;
diff --git a/hirc.h b/hirc.h
@@ -86,6 +86,7 @@ void handle(int rfd, struct Server *server);
void handle_expect(char *cmd, char *about);
char * handle_expect_get(char *cmd);
void handle_PING(char *msg, char **params, struct Server *server, time_t timestamp);
+void handle_PONG(char *msg, char **params, struct Server *server, time_t timestamp);
void handle_JOIN(char *msg, char **params, struct Server *server, time_t timestamp);
void handle_PART(char *msg, char **params, struct Server *server, time_t timestamp);
void handle_QUIT(char *msg, char **params, struct Server *server, time_t timestamp);
@@ -134,6 +135,7 @@ int command_getopt(char **str, struct CommandOpts *opts);
void command_quit(struct Server *server, char *str);
void command_join(struct Server *server, char *str);
void command_part(struct Server *server, char *str);
+void command_ping(struct Server *server, char *str);
void command_quote(struct Server *server, char *str);
void command_connect(struct Server *server, char *str);
void command_select(struct Server *server, char *str);