commit 194705d19cfdbe98d8dddf5cb6f5bf7e89076e73
parent 404efb03a8ab6b812ec066c05085ecc6a3acd1b5
Author: hhvn <dev@hhvn.uk>
Date: Sun, 14 Nov 2021 13:41:16 +0000
commands.c handle.c hirc.h serv.c struct.h: per server expect
Diffstat:
5 files changed, 34 insertions(+), 37 deletions(-)
diff --git a/commands.c b/commands.c
@@ -68,7 +68,7 @@ command_join(struct Server *server, char *str) {
ircprintf(server, "JOIN %s\r\n", str);
else
ircprintf(server, "JOIN #%s\r\n", str);
- handle_expect("JOIN", str);
+ handle_expect(server, Expect_join, str);
}
void
@@ -77,7 +77,7 @@ command_part(struct Server *server, char *str) {
ircprintf(server, "PART %s\r\n", str);
else
ircprintf(server, "PART #%s\r\n", str);
- handle_expect("PART", str);
+ handle_expect(server, Expect_join, str);
}
void
@@ -88,7 +88,7 @@ command_ping(struct Server *server, char *str) {
}
ircprintf(server, "PING :%s\r\n", str);
- handle_expect("PONG", str);
+ handle_expect(server, Expect_pong, str);
}
void
diff --git a/handle.c b/handle.c
@@ -6,13 +6,6 @@
#include <stdlib.h>
#include "hirc.h"
-struct Expect expect[] = {
- { "JOIN", NULL },
- { "PART", NULL },
- { "PONG", NULL },
- { NULL, NULL },
-};
-
struct Handler handlers[] = {
{ "PING", handle_PING },
{ "PONG", handle_PONG },
@@ -50,9 +43,9 @@ handle_PONG(char *msg, char **params, struct Server *server, time_t timestamp) {
if (param_len(params) < 2)
return;
- if (strcmp_n(*(params+1), handle_expect_get("PONG"))) {
+ if (strcmp_n(*(params+1), handle_expect_get(server, Expect_pong))) {
hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL);
- handle_expect("PONG", NULL);
+ handle_expect(server, Expect_pong, NULL);
}
}
@@ -78,11 +71,11 @@ handle_JOIN(char *msg, char **params, struct Server *server, time_t timestamp) {
hist_add(chan->history, nick, msg, params, Activity_status, timestamp, HIST_SHOW);
if (nick_isself(nick)) {
- if (strcmp_n(target, handle_expect_get("JOIN")) == 0)
+ if (strcmp_n(target, handle_expect_get(server, Expect_join)) == 0)
ui_select(server, chan);
else
windows[Win_buflist].refresh = 1;
- handle_expect("JOIN", NULL);
+ handle_expect(server, Expect_join, NULL);
} else if (selected.channel == chan) {
windows[Win_nicklist].refresh = 1;
}
@@ -107,9 +100,9 @@ handle_PART(char *msg, char **params, struct Server *server, time_t timestamp) {
if (nick_isself(nick)) {
chan_setold(chan, 1);
nick_free_list(&chan->nicks);
- if (chan == selected.channel && strcmp_n(target, handle_expect_get("PART"))) {
+ if (chan == selected.channel && strcmp_n(target, handle_expect_get(server, Expect_part))) {
ui_select(selected.server, NULL);
- handle_expect("PART", NULL);
+ handle_expect(server, Expect_part, NULL);
}
windows[Win_buflist].refresh = 1;
} else {
@@ -329,25 +322,23 @@ handle_ENDOFMOTD(char *msg, char **params, struct Server *server, time_t timesta
hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL);
}
+/* Expect stuff should probably be moved to serv.c.
+ * Also, it might be better to have an enum for all commands and numerics somewhere */
void
-handle_expect(char *cmd, char *about) {
- int i;
+handle_expect(struct Server *server, enum Expect cmd, char *about) {
+ if (cmd >= Expect_last || cmd < 0)
+ return;
- for (i=0; expect[i].cmd; i++) {
- if (strcmp(expect[i].cmd, cmd) == 0) {
- free(expect[i].about);
- expect[i].about = about ? strdup(about) : NULL;
- }
- }
+ free(server->expect[cmd]);
+ server->expect[cmd] = about ? strdup(about) : NULL;
}
char *
-handle_expect_get(char *cmd) {
- int i;
-
- for (i=0; expect[i].cmd; i++)
- if (strcmp(expect[i].cmd, cmd) == 0)
- return expect[i].about;
+handle_expect_get(struct Server *server, enum Expect cmd) {
+ if (cmd >= Expect_last || cmd < 0)
+ return NULL;
+ else
+ return server->expect[cmd];
}
void
diff --git a/hirc.h b/hirc.h
@@ -85,8 +85,8 @@ void support_set(struct Server *server, char *key, char *value);
/* handle.c */
void handle(int rfd, struct Server *server);
-void handle_expect(char *cmd, char *about);
-char * handle_expect_get(char *cmd);
+void handle_expect(struct Server *server, enum Expect cmd, char *about);
+char * handle_expect_get(struct Server *server, enum Expect 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);
diff --git a/serv.c b/serv.c
@@ -47,6 +47,7 @@ serv_create(char *name, char *host, char *port, char *nick,
char *username, char *realname, int tls, int tls_verify) {
struct Server *server;
struct tls_config *conf;
+ int i;
if (!name || !host || !port || !nick)
return NULL;
@@ -78,6 +79,8 @@ serv_create(char *name, char *host, char *port, char *nick,
server->channels = NULL;
server->privs = NULL;
server->reconnect = 0;
+ for (i=0; i < Expect_last; i++)
+ server->expect[i] = NULL;
server->connectfail = 0;
server->lastconnected = server->lastrecv = server->pingsent = 0;
diff --git a/struct.h b/struct.h
@@ -85,6 +85,13 @@ struct Support {
struct Support *next;
};
+enum Expect {
+ Expect_join,
+ Expect_part,
+ Expect_pong,
+ Expect_last,
+};
+
struct Server {
struct Server *prev;
int wfd;
@@ -103,6 +110,7 @@ struct Server {
struct Channel *channels;
struct Channel *privs;
int reconnect;
+ char *expect[Expect_last];
int connectfail; /* number of failed connections */
time_t lastconnected; /* last time a connection was lost */
time_t lastrecv; /* last time a message was received from server */
@@ -120,11 +128,6 @@ struct Handler {
void (*func)(char *msg, char **params, struct Server *server, time_t timestamp);
};
-struct Expect {
- char *cmd;
- char *about;
-};
-
/* commands received from user */
struct Command {
char *name;