From 583262ae67a1fb02834574b034c5ed5937e23c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Fri, 17 Apr 2015 23:18:07 +0200 Subject: [PATCH] degesch: printing to buffers etc. --- degesch.c | 99 +++++++++++++++++++++++++++---------------------------- 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/degesch.c b/degesch.c index be20eb9..839b9e8 100644 --- a/degesch.c +++ b/degesch.c @@ -1416,10 +1416,8 @@ on_readline_previous_buffer (int count, int key) (void) key; struct app_context *ctx = g_ctx; - if (!ctx->current_buffer) - return 0; - - buffer_activate (ctx, buffer_previous (ctx, count)); + if (ctx->current_buffer) + buffer_activate (ctx, buffer_previous (ctx, count)); return 0; } @@ -1429,10 +1427,8 @@ on_readline_next_buffer (int count, int key) (void) key; struct app_context *ctx = g_ctx; - if (!ctx->current_buffer) - return 0; - - buffer_activate (ctx, buffer_next (ctx, count)); + if (ctx->current_buffer) + buffer_activate (ctx, buffer_next (ctx, count)); return 0; } @@ -1649,9 +1645,8 @@ try_handle_buffer_goto (struct app_context *ctx, const char *word) return false; if (n > INT_MAX || !buffer_goto (ctx, n)) - { - // TODO: print a "no such buffer" error message - } + buffer_send_error (ctx, ctx->global_buffer, + "%s: %s", "no such buffer", word); return true; } @@ -1678,10 +1673,16 @@ handle_command_buffer (struct app_context *ctx, char *arguments) struct buffer *buffer = NULL; // XXX: also build a prefix map? + // It looks like we'll want to split this into functions anyway. // TODO: some subcommand to print N last lines from the buffer if (!strcasecmp_ascii (action, "list")) { - // TODO: print a list of "%d: %s", index, name + buffer_send_status (ctx, ctx->global_buffer, "Buffers list:"); + + int i = 1; + LIST_FOR_EACH (struct buffer, iter, ctx->buffers) + buffer_send_status (ctx, ctx->global_buffer, + " [%d] %s", i++, iter->name); } else if (!strcasecmp_ascii (action, "clear")) { @@ -1702,17 +1703,20 @@ handle_command_buffer (struct app_context *ctx, char *arguments) if (!buffer) { - // TODO: print a "no such buffer: %s" message + buffer_send_error (ctx, ctx->global_buffer, + "%s: %s", "no such buffer", which); return; } if (buffer == ctx->global_buffer) { - // TODO: print a "can't close the global buffer" message + buffer_send_error (ctx, ctx->global_buffer, + "can't close the global buffer"); return; } if (buffer == ctx->server_buffer) { - // TODO: print a "can't close the server buffer" message + buffer_send_error (ctx, ctx->global_buffer, + "can't close the server buffer"); return; } @@ -1739,6 +1743,12 @@ handle_command_quit (struct app_context *ctx, char *arguments) initiate_quit (ctx); } +static void +handle_command_quote (struct app_context *ctx, char *arguments) +{ + irc_send (ctx, arguments); +} + static struct command_handler { char *name; @@ -1775,8 +1785,8 @@ g_command_handlers[] = { "motd", NULL }, { "away", NULL }, - { "quote", NULL }, #endif + { "quote", handle_command_quote }, }; static void @@ -1796,6 +1806,7 @@ command_handler_cmp_by_length (const void *a, const void *b) static void init_partial_matching_user_command_map (struct str_map *partial) { + // Trivially create a partial matching map str_map_init (partial); partial->key_xfrm = tolower_ascii_strxfrm; @@ -1822,7 +1833,6 @@ init_partial_matching_user_command_map (struct str_map *partial) static void process_user_command (struct app_context *ctx, char *command) { - // Trivially create a partial matching map static bool initialized = false; struct str_map partial; if (!initialized) @@ -1839,36 +1849,28 @@ process_user_command (struct app_context *ctx, char *command) if (handler) handler->handler (ctx, command); else - { - // TODO: print a "no such command" error message - } + buffer_send_error (ctx, ctx->global_buffer, + "%s: %s", "no such command", name); } static void send_message_to_current_buffer (struct app_context *ctx, char *message) { struct buffer *buffer = ctx->current_buffer; - if (!buffer) - { - // TODO: print an error message to the global buffer - return; - } + hard_assert (buffer != NULL); switch (buffer->type) { case BUFFER_GLOBAL: case BUFFER_SERVER: - // TODO: print a message to the buffer that it's not a channel + buffer_send_error (ctx, buffer, "this buffer is not a channel"); break; case BUFFER_CHANNEL: - // TODO: print a message to the buffer - // TODO: autosplit - irc_send (ctx, "PRIVMSG %s :%s", buffer->name, message); - break; case BUFFER_PM: - // TODO: print a message to the buffer // TODO: autosplit irc_send (ctx, "PRIVMSG %s :%s", buffer->name, message); + buffer_send (ctx, buffer, BUFFER_LINE_PRIVMSG, 0, + ctx->irc_nickname, NULL, "%s", message); break; } } @@ -1980,8 +1982,7 @@ on_irc_reconnect_timeout (void *user_data) if (irc_connect (ctx, &e)) return; - // FIXME: print to the server buffer - print_error ("%s", e->message); + buffer_send_error (ctx, ctx->server_buffer, "%s", e->message); error_free (e); irc_queue_reconnect (ctx); } @@ -1990,9 +1991,8 @@ static void irc_queue_reconnect (struct app_context *ctx) { hard_assert (ctx->irc_fd == -1); - // FIXME: print to the server buffer - print_status ("trying to reconnect in %ld seconds...", - ctx->reconnect_delay); + buffer_send_status (ctx, ctx->server_buffer, + "trying to reconnect in %ld seconds...", ctx->reconnect_delay); poller_timer_set (&ctx->reconnect_tmr, ctx->reconnect_delay * 1000); } @@ -2030,8 +2030,7 @@ static void on_irc_ping_timeout (void *user_data) { struct app_context *ctx = user_data; - // FIXME: print to the server buffer - print_error ("connection timeout"); + buffer_send_error (ctx, ctx->server_buffer, "connection timeout"); on_irc_disconnected (ctx); } @@ -2074,13 +2073,13 @@ on_irc_readable (const struct pollfd *fd, struct app_context *ctx) case IRC_READ_AGAIN: goto end; case IRC_READ_ERROR: - // FIXME: print to the server buffer - print_error ("reading from the IRC server failed"); + buffer_send_error (ctx, ctx->server_buffer, + "reading from the IRC server failed"); disconnected = true; goto end; case IRC_READ_EOF: - // FIXME: print to the server buffer - print_status ("the IRC server closed the connection"); + buffer_send_error (ctx, ctx->server_buffer, + "the IRC server closed the connection"); disconnected = true; goto end; case IRC_READ_OK: @@ -2089,8 +2088,8 @@ on_irc_readable (const struct pollfd *fd, struct app_context *ctx) if (buf->len >= (1 << 20)) { - // FIXME: print to the server buffer - print_error ("the IRC server seems to spew out data frantically"); + buffer_send_error (ctx, ctx->server_buffer, + "the IRC server seems to spew out data frantically"); irc_shutdown (ctx); goto end; } @@ -2142,8 +2141,8 @@ irc_connect (struct app_context *ctx, struct error **e) { char *address = format_host_port_pair (irc_host, irc_port); char *socks_address = format_host_port_pair (socks_host, socks_port); - // FIXME: print to the server buffer - print_status ("connecting to %s via %s...", address, socks_address); + buffer_send_status (ctx, ctx->server_buffer, + "connecting to %s via %s...", address, socks_address); free (socks_address); free (address); @@ -2167,8 +2166,7 @@ irc_connect (struct app_context *ctx, struct error **e) ctx->irc_fd = -1; return false; } - // FIXME: print to the server buffer - print_status ("connection established"); + buffer_send_status (ctx, ctx->server_buffer, "connection established"); poller_fd_init (&ctx->irc_event, &ctx->poller, ctx->irc_fd); ctx->irc_event.dispatcher = (poller_fd_fn) on_irc_readable; @@ -2354,7 +2352,7 @@ autofill_user_info (struct app_context *ctx, struct error **e) if (nickname && username && realname) return true; - // TODO: read POSIX user info and fill the configuration if needed + // Read POSIX user info and fill the configuration if needed struct passwd *pwd = getpwuid (geteuid ()); if (!pwd) FAIL ("cannot retrieve user information: %s", strerror (errno)); @@ -2538,8 +2536,7 @@ main (int argc, char *argv[]) if (!load_config (&ctx, &e) || !irc_connect (&ctx, &e)) { - // FIXME: print to the global buffer - print_error ("%s", e->message); + buffer_send_error (&ctx, ctx.global_buffer, "%s", e->message); error_free (e); exit (EXIT_FAILURE); }