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

View File

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