degesch: allow executing a command when registered

To authenticate with NickServ or whatever.
This commit is contained in:
Přemysl Eric Janouch 2015-07-18 13:23:22 +02:00
parent b3acc4904f
commit baacb27d4b
1 changed files with 30 additions and 14 deletions

View File

@ -1560,6 +1560,9 @@ static struct config_schema g_config_server[] =
.comment = "Channels to join on start",
.type = CONFIG_ITEM_STRING_ARRAY,
.validate = config_validate_nonjunk_string },
{ .name = "command",
.comment = "Command to execute after a successful connect",
.type = CONFIG_ITEM_STRING },
{ .name = "reconnect",
.comment = "Whether to reconnect on error",
.type = CONFIG_ITEM_BOOLEAN,
@ -5574,6 +5577,9 @@ irc_try_parse_welcome_for_userhost (struct server *s, const char *m)
str_vector_free (&v);
}
static bool process_input_utf8
(struct app_context *, struct buffer *, char *, int);
static void
irc_on_registered (struct server *s, const char *nickname)
{
@ -5587,6 +5593,16 @@ irc_on_registered (struct server *s, const char *nickname)
// XXX: we can also use WHOIS if it's not supported (optional by RFC 2812)
irc_send (s, "USERHOST %s", s->irc_user->nickname);
const char *command = get_config_string (s->config, "command");
if (command)
{
log_server_debug (s, "Executing \"#s\"", command);
char *copy = xstrdup (command);
process_input_utf8 (s->ctx, s->buffer, copy, 0);
free (copy);
}
// TODO: split autojoin at commas and make a joined set with regular rejoins
const char *autojoin = get_config_string (s->config, "autojoin");
if (autojoin)
@ -8012,8 +8028,8 @@ init_user_command_map (struct str_map *map)
}
static bool
process_user_command
(struct app_context *ctx, const char *command_name, char *input)
process_user_command (struct app_context *ctx, struct buffer *buffer,
const char *command_name, char *input)
{
static bool initialized = false;
static struct str_map map;
@ -8029,7 +8045,7 @@ process_user_command
struct handler_args args =
{
.ctx = ctx,
.buffer = ctx->current_buffer,
.buffer = buffer,
.arguments = input,
};
@ -8146,9 +8162,9 @@ send_message_to_target (struct server *s,
}
static void
send_message_to_current_buffer (struct app_context *ctx, char *message)
send_message_to_buffer (struct app_context *ctx, struct buffer *buffer,
char *message)
{
struct buffer *buffer = ctx->current_buffer;
hard_assert (buffer != NULL);
switch (buffer->type)
@ -8167,31 +8183,31 @@ send_message_to_current_buffer (struct app_context *ctx, char *message)
}
}
static bool process_input_utf8 (struct app_context *, char *, int);
static bool
process_alias (struct app_context *ctx, struct str_vector *commands, int level)
process_alias (struct app_context *ctx, struct buffer *buffer,
struct str_vector *commands, int level)
{
for (size_t i = 0; i < commands->len; i++)
log_global_debug (ctx, "Alias expanded to: ###d: \"#s\"",
(int) i, commands->vector[i]);
for (size_t i = 0; i < commands->len; i++)
if (!process_input_utf8 (ctx, commands->vector[i], ++level))
if (!process_input_utf8 (ctx, buffer, commands->vector[i], ++level))
return false;
return true;
}
static bool
process_input_utf8 (struct app_context *ctx, char *input, int alias_level)
process_input_utf8 (struct app_context *ctx, struct buffer *buffer,
char *input, int alias_level)
{
if (*input != '/' || *++input == '/')
{
send_message_to_current_buffer (ctx, input);
send_message_to_buffer (ctx, buffer, input);
return true;
}
char *name = cut_word (&input);
if (process_user_command (ctx, name, input))
if (process_user_command (ctx, buffer, name, input))
return true;
struct str_vector commands;
@ -8203,7 +8219,7 @@ process_input_utf8 (struct app_context *ctx, char *input, int alias_level)
else if (alias_level != 0)
log_global_error (ctx, "#s: /#s", "Aliases can't nest", name);
else
result = process_alias (ctx, &commands, alias_level);
result = process_alias (ctx, buffer, &commands, alias_level);
str_vector_free (&commands);
return result;
@ -8216,7 +8232,7 @@ process_input (struct app_context *ctx, char *user_input)
if (!(input = iconv_xstrdup (ctx->term_to_utf8, user_input, -1, NULL)))
print_error ("character conversion failed for `%s'", "user input");
else
(void) process_input_utf8 (ctx, input, 0);
(void) process_input_utf8 (ctx, ctx->current_buffer, input, 0);
free (input);
}