degesch: add a "command_delay" option to servers

E.g. for channels that are for registered users only.
This commit is contained in:
Přemysl Eric Janouch 2015-07-18 14:12:34 +02:00
parent 5c0a2975e8
commit b947a2e4bc
1 changed files with 37 additions and 13 deletions

View File

@ -1132,6 +1132,7 @@ struct server
struct poller_timer ping_tmr; ///< We should send a ping struct poller_timer ping_tmr; ///< We should send a ping
struct poller_timer timeout_tmr; ///< Connection seems to be dead struct poller_timer timeout_tmr; ///< Connection seems to be dead
struct poller_timer reconnect_tmr; ///< We should reconnect now struct poller_timer reconnect_tmr; ///< We should reconnect now
struct poller_timer autojoin_tmr; ///< Re/join channels as appropriate
// IRC: // IRC:
@ -1173,6 +1174,7 @@ struct server
static void on_irc_timeout (void *user_data); static void on_irc_timeout (void *user_data);
static void on_irc_ping_timeout (void *user_data); static void on_irc_ping_timeout (void *user_data);
static void on_irc_autojoin_timeout (void *user_data);
static void irc_initiate_connect (struct server *s); static void irc_initiate_connect (struct server *s);
static void static void
@ -1236,6 +1238,10 @@ server_init (struct server *self, struct poller *poller)
self->reconnect_tmr.dispatcher = (poller_timer_fn) irc_initiate_connect; self->reconnect_tmr.dispatcher = (poller_timer_fn) irc_initiate_connect;
self->reconnect_tmr.user_data = self; self->reconnect_tmr.user_data = self;
poller_timer_init (&self->autojoin_tmr, poller);
self->autojoin_tmr.dispatcher = on_irc_autojoin_timeout;
self->autojoin_tmr.user_data = self;
str_map_init (&self->irc_users); str_map_init (&self->irc_users);
self->irc_users.key_xfrm = irc_strxfrm; self->irc_users.key_xfrm = irc_strxfrm;
str_map_init (&self->irc_channels); str_map_init (&self->irc_channels);
@ -1276,6 +1282,7 @@ server_free (struct server *self)
poller_timer_reset (&self->ping_tmr); poller_timer_reset (&self->ping_tmr);
poller_timer_reset (&self->timeout_tmr); poller_timer_reset (&self->timeout_tmr);
poller_timer_reset (&self->reconnect_tmr); poller_timer_reset (&self->reconnect_tmr);
poller_timer_reset (&self->autojoin_tmr);
str_map_free (&self->irc_users); str_map_free (&self->irc_users);
str_map_free (&self->irc_channels); str_map_free (&self->irc_channels);
@ -1563,6 +1570,11 @@ static struct config_schema g_config_server[] =
{ .name = "command", { .name = "command",
.comment = "Command to execute after a successful connect", .comment = "Command to execute after a successful connect",
.type = CONFIG_ITEM_STRING }, .type = CONFIG_ITEM_STRING },
{ .name = "command_delay",
.comment = "Delay between executing \"command\" and joining channels",
.type = CONFIG_ITEM_INTEGER,
.validate = config_validate_nonnegative,
.default_ = "0" },
{ .name = "reconnect", { .name = "reconnect",
.comment = "Whether to reconnect on error", .comment = "Whether to reconnect on error",
.type = CONFIG_ITEM_BOOLEAN, .type = CONFIG_ITEM_BOOLEAN,
@ -3464,14 +3476,15 @@ irc_cancel_timers (struct server *s)
poller_timer_reset (&s->timeout_tmr); poller_timer_reset (&s->timeout_tmr);
poller_timer_reset (&s->ping_tmr); poller_timer_reset (&s->ping_tmr);
poller_timer_reset (&s->reconnect_tmr); poller_timer_reset (&s->reconnect_tmr);
poller_timer_reset (&s->autojoin_tmr);
} }
static void static void
irc_reset_connection_timeouts (struct server *s) irc_reset_connection_timeouts (struct server *s)
{ {
irc_cancel_timers (s);
poller_timer_set (&s->timeout_tmr, 3 * 60 * 1000); poller_timer_set (&s->timeout_tmr, 3 * 60 * 1000);
poller_timer_set (&s->ping_tmr, (3 * 60 + 30) * 1000); poller_timer_set (&s->ping_tmr, (3 * 60 + 30) * 1000);
poller_timer_reset (&s->reconnect_tmr);
} }
static int64_t static int64_t
@ -3741,6 +3754,25 @@ on_irc_timeout (void *user_data)
irc_send (s, "PING :%" PRIi64, (int64_t) time (NULL)); irc_send (s, "PING :%" PRIi64, (int64_t) time (NULL));
} }
static void
on_irc_autojoin_timeout (void *user_data)
{
struct server *s = user_data;
// TODO: split autojoin at commas and make a joined set with regular rejoins
const char *autojoin = get_config_string (s->config, "autojoin");
if (autojoin)
irc_send (s, "JOIN :%s", autojoin);
struct str_map_iter iter;
str_map_iter_init (&iter, &s->irc_channels);
struct channel *channel;
while ((channel = str_map_iter_next (&iter)))
if (!channel->left_manually)
irc_send (s, "JOIN :%s", channel->name);
}
// --- Server I/O -------------------------------------------------------------- // --- Server I/O --------------------------------------------------------------
static void irc_process_message static void irc_process_message
@ -5607,18 +5639,10 @@ irc_on_registered (struct server *s, const char *nickname)
free (copy); free (copy);
} }
// TODO: split autojoin at commas and make a joined set with regular rejoins int64_t command_delay = get_config_integer (s->config, "command_delay");
const char *autojoin = get_config_string (s->config, "autojoin"); log_server_debug (s, "Autojoining channels in #&s seconds...",
if (autojoin) xstrdup_printf ("%" PRId64, command_delay));
irc_send (s, "JOIN :%s", autojoin); poller_timer_set (&s->autojoin_tmr, command_delay * 1000);
struct str_map_iter iter;
str_map_iter_init (&iter, &s->irc_channels);
struct channel *channel;
while ((channel = str_map_iter_next (&iter)))
if (!channel->left_manually)
irc_send (s, "JOIN :%s", channel->name);
} }
static void static void