Run slash command sequences after IRC registration
This commit is contained in:
parent
a95f4f42b3
commit
b208b5b696
3 changed files with 84 additions and 37 deletions
94
irc9.c
94
irc9.c
|
|
@ -136,6 +136,8 @@ typedef struct Server {
|
|||
char sasl_user[NICKLEN];
|
||||
char sasl_pass[128];
|
||||
char *autocommand;
|
||||
const char *next_autocommand;
|
||||
struct timespec autocommand_at;
|
||||
BlacklistedNick *blacklist;
|
||||
int cap_state;
|
||||
char caps[512]; /* accumulated multi-line CAP LS reply */
|
||||
|
|
@ -705,9 +707,8 @@ static void load_blacklist(Server *s, char *list)
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef USE_AUTOJOIN
|
||||
/* A comma starts another command only when followed by a slash. */
|
||||
static const char *autojoin_command_end(const char *command, const char **next)
|
||||
static const char *configured_command_end(const char *command, const char **next)
|
||||
{
|
||||
for (const char *comma = strchr(command, ','); comma; comma = strchr(comma + 1, ',')) {
|
||||
const char *following = comma + 1 + strspn(comma + 1, " \t");
|
||||
|
|
@ -720,20 +721,23 @@ static const char *autojoin_command_end(const char *command, const char **next)
|
|||
return command + strlen(command);
|
||||
}
|
||||
|
||||
static int valid_autojoin_commands(const char *command)
|
||||
static int valid_configured_commands(const char *command, int allow_raw)
|
||||
{
|
||||
if (*command != '/' || strchr(command, '\r')) return 0;
|
||||
if (!*command || strchr(command, '\r')) return 0;
|
||||
while (command) {
|
||||
const char *next;
|
||||
const char *end = autojoin_command_end(command, &next);
|
||||
if (end - command >= MAXINPUT ||
|
||||
command + 1 + strspn(command + 1, " \t") >= end)
|
||||
const char *end = configured_command_end(command, &next);
|
||||
if (*command == '/') {
|
||||
if (end - command >= MAXINPUT ||
|
||||
command + 1 + strspn(command + 1, " \t") >= end)
|
||||
return 0;
|
||||
} else if (!allow_raw || end - command > MAXLINE - 2) {
|
||||
return 0;
|
||||
}
|
||||
command = next;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void load_config(const char *path)
|
||||
{
|
||||
|
|
@ -781,13 +785,11 @@ static void load_config(const char *path)
|
|||
}
|
||||
if (strcasecmp(key, "autocommand") == 0) {
|
||||
if (save) save += strspn(save, " \t");
|
||||
if (last && save && *save && strlen(save) <= MAXLINE - 2 &&
|
||||
!strchr(save, '\r')) {
|
||||
if (last && save && valid_configured_commands(save, 1)) {
|
||||
free(last->autocommand);
|
||||
last->autocommand = dupstr(save);
|
||||
} else {
|
||||
fprintf(stderr, "irc9: autocommand requires a server and an IRC command of at most %d bytes\n",
|
||||
MAXLINE - 2);
|
||||
fprintf(stderr, "irc9: autocommand requires a server and valid commands\n");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
|
@ -802,7 +804,7 @@ static void load_config(const char *path)
|
|||
channel_tail = &channel->next;
|
||||
|
||||
const char *command = save ? save + strspn(save, " \t") : "";
|
||||
if (valid_autojoin_commands(command)) {
|
||||
if (valid_configured_commands(command, 0)) {
|
||||
channel->commands = dupstr(command);
|
||||
channel->next_command = channel->commands;
|
||||
} else if (*command)
|
||||
|
|
@ -1125,7 +1127,7 @@ static void handle_connect_results(void)
|
|||
/* Pace each server's startup JOINs to avoid flooding the connection. */
|
||||
static void send_autojoin(Server *s)
|
||||
{
|
||||
if (!s->connected || !s->autojoin) return;
|
||||
if (!s->connected || s->next_autocommand || !s->autojoin) return;
|
||||
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
|
|
@ -1173,6 +1175,7 @@ static void disconnect_server(Server *s)
|
|||
for (Window *w = s->windows; w; w = w->next)
|
||||
w->query_offline = 0;
|
||||
s->connected = 0;
|
||||
s->next_autocommand = NULL;
|
||||
s->inlen = 0;
|
||||
s->cap_state = CAP_NONE;
|
||||
s->caps[0] = 0;
|
||||
|
|
@ -1312,21 +1315,56 @@ static int handle_ctcp(Server *s, const char *nick, char *text,
|
|||
return 1;
|
||||
}
|
||||
|
||||
#ifdef USE_AUTOJOIN
|
||||
static void do_command(Window *window, char *line);
|
||||
|
||||
static void run_autojoin_command(AutojoinChannel **link, Window *w)
|
||||
/* Copy one command into a mutable buffer for do_command. */
|
||||
static void take_configured_command(const char **cursor, char line[MAXINPUT])
|
||||
{
|
||||
AutojoinChannel *channel = *link;
|
||||
const char *command = channel->next_command;
|
||||
const char *end = autojoin_command_end(command, &channel->next_command);
|
||||
if (channel->next_command)
|
||||
const char *command = *cursor;
|
||||
const char *end = configured_command_end(command, cursor);
|
||||
if (*cursor)
|
||||
while (end > command && (end[-1] == ' ' || end[-1] == '\t')) end--;
|
||||
|
||||
char line[MAXINPUT];
|
||||
size_t len = (size_t)(end - command);
|
||||
memcpy(line, command, len);
|
||||
line[len] = 0;
|
||||
}
|
||||
|
||||
static void run_autocommand(Server *s)
|
||||
{
|
||||
char line[MAXINPUT];
|
||||
take_configured_command(&s->next_autocommand, line);
|
||||
if (s->next_autocommand) {
|
||||
clock_gettime(CLOCK_MONOTONIC, &s->autocommand_at);
|
||||
s->autocommand_at.tv_sec += 2;
|
||||
}
|
||||
|
||||
if (*line == '/')
|
||||
do_command(serverwin(s), line);
|
||||
else
|
||||
sendf(s, "%s", line);
|
||||
}
|
||||
|
||||
static int command_due(struct timespec now, struct timespec deadline)
|
||||
{
|
||||
return now.tv_sec > deadline.tv_sec ||
|
||||
(now.tv_sec == deadline.tv_sec && now.tv_nsec >= deadline.tv_nsec);
|
||||
}
|
||||
|
||||
static void run_pending_autocommands(Server *s)
|
||||
{
|
||||
if (!s->next_autocommand) return;
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
if (command_due(now, s->autocommand_at)) run_autocommand(s);
|
||||
}
|
||||
|
||||
#ifdef USE_AUTOJOIN
|
||||
static void run_autojoin_command(AutojoinChannel **link, Window *w)
|
||||
{
|
||||
AutojoinChannel *channel = *link;
|
||||
char line[MAXINPUT];
|
||||
take_configured_command(&channel->next_command, line);
|
||||
|
||||
if (channel->next_command) {
|
||||
clock_gettime(CLOCK_MONOTONIC, &channel->command_at);
|
||||
|
|
@ -1357,9 +1395,7 @@ static void run_pending_autojoin_commands(Server *s)
|
|||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
for (AutojoinChannel **link = &s->pending_autojoin; *link; link = &(*link)->next) {
|
||||
AutojoinChannel *channel = *link;
|
||||
if (!channel->joined || now.tv_sec < channel->command_at.tv_sec ||
|
||||
(now.tv_sec == channel->command_at.tv_sec &&
|
||||
now.tv_nsec < channel->command_at.tv_nsec)) continue;
|
||||
if (!channel->joined || !command_due(now, channel->command_at)) continue;
|
||||
|
||||
Window *w = window_find(s, channel->name);
|
||||
if (w) {
|
||||
|
|
@ -1820,9 +1856,11 @@ static void handle_line(Server *s, char *line)
|
|||
if (strcmp(cmd, "001") == 0) {
|
||||
int first_welcome = !s->connected;
|
||||
s->connected = 1;
|
||||
if (first_welcome && s->autocommand)
|
||||
sendf(s, "%s", s->autocommand);
|
||||
appendf(sw, "%s", narg > 0 ? arg[narg - 1] : cmd);
|
||||
if (first_welcome && s->autocommand) {
|
||||
s->next_autocommand = s->autocommand;
|
||||
run_autocommand(s);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -3239,12 +3277,16 @@ int main(int argc, char **argv)
|
|||
for (Server *s = servers; s; s = s->next) {
|
||||
if (s->fd < 0 && !s->connecting && now >= s->retry_at)
|
||||
connect_server(s);
|
||||
run_pending_autocommands(s);
|
||||
if (quit_flag) break;
|
||||
#ifdef USE_AUTOJOIN
|
||||
send_autojoin(s);
|
||||
run_pending_autojoin_commands(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (quit_flag) break;
|
||||
|
||||
struct pollfd fds[64];
|
||||
Server *owner[64];
|
||||
int nf = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue