degesch: preparations for numerics processing

This commit is contained in:
Přemysl Eric Janouch 2015-04-21 22:08:18 +02:00
parent e937ac12d5
commit 295e4c8bf9
2 changed files with 45 additions and 23 deletions

View File

@ -60,7 +60,8 @@ set_source_files_properties (${PROJECT_BINARY_DIR}/kike-replies.c
add_executable (zyklonb zyklonb.c ${common_sources} ${common_headers}) add_executable (zyklonb zyklonb.c ${common_sources} ${common_headers})
target_link_libraries (zyklonb ${project_libraries}) target_link_libraries (zyklonb ${project_libraries})
add_executable (degesch degesch.c ${common_sources} ${common_headers}) add_executable (degesch degesch.c kike-replies.c
${common_sources} ${common_headers})
target_link_libraries (degesch ${project_libraries} readline) target_link_libraries (degesch ${project_libraries} readline)
add_executable (kike kike.c kike-replies.c ${common_sources} ${common_headers}) add_executable (kike kike.c kike-replies.c ${common_sources} ${common_headers})

View File

@ -37,6 +37,7 @@
#define PROGRAM_NAME "degesch" #define PROGRAM_NAME "degesch"
#include "common.c" #include "common.c"
#include "kike-replies.c"
#include <langinfo.h> #include <langinfo.h>
#include <locale.h> #include <locale.h>
@ -238,6 +239,7 @@ struct channel
char *topic; ///< Channel topic char *topic; ///< Channel topic
struct channel_user *users; ///< Channel users struct channel_user *users; ///< Channel users
struct str_vector names_buf; ///< Buffer for RPL_NAMREPLY
}; };
static struct channel * static struct channel *
@ -245,6 +247,7 @@ channel_new (void)
{ {
struct channel *self = xcalloc (1, sizeof *self); struct channel *self = xcalloc (1, sizeof *self);
self->ref_count = 1; self->ref_count = 1;
str_vector_init (&self->names_buf);
return self; return self;
} }
@ -256,6 +259,7 @@ channel_destroy (struct channel *self)
free (self->topic); free (self->topic);
LIST_FOR_EACH (struct channel_user, iter, self->users) LIST_FOR_EACH (struct channel_user, iter, self->users)
channel_user_destroy (iter); channel_user_destroy (iter);
str_vector_free (&self->names_buf);
free (self); free (self);
} }
@ -2252,6 +2256,38 @@ irc_handler_cmp_by_name (const void *a, const void *b)
return strcasecmp_ascii (first->name, second->name); return strcasecmp_ascii (first->name, second->name);
} }
static void
irc_process_numeric (struct app_context *ctx,
const struct irc_message *msg, unsigned long numeric)
{
// Numerics typically have human-readable information
// Get rid of the first parameter, if there's any at all,
// as it contains our nickname and is of no practical use to the user
struct str_vector copy;
str_vector_init (&copy);
str_vector_add_vector (&copy, msg->params.vector + !!msg->params.len);
// Join the parameter vector back, recode it to our internal encoding
// and send it to the server buffer
char *reconstructed = join_str_vector (&copy, ' ');
str_vector_free (&copy);
char *utf8 = irc_to_utf8 (ctx, reconstructed);
free (reconstructed);
buffer_send_status (ctx, ctx->server_buffer, "%s", utf8);
free (utf8);
switch (numeric)
{
case IRC_RPL_NAMREPLY:
// TODO: find the channel and if found, push nicks to names_buf
break;
case IRC_RPL_ENDOFNAMES:
// TODO: find the channel and if found, overwrite users
break;
}
}
static void static void
irc_process_message (const struct irc_message *msg, irc_process_message (const struct irc_message *msg,
const char *raw, void *user_data) const char *raw, void *user_data)
@ -2296,30 +2332,11 @@ irc_process_message (const struct irc_message *msg,
struct irc_handler *handler = bsearch (&key, g_irc_handlers, struct irc_handler *handler = bsearch (&key, g_irc_handlers,
N_ELEMENTS (g_irc_handlers), sizeof key, irc_handler_cmp_by_name); N_ELEMENTS (g_irc_handlers), sizeof key, irc_handler_cmp_by_name);
if (handler) if (handler)
{
handler->handler (ctx, msg); handler->handler (ctx, msg);
return;
}
// Numerics typically have human-readable information unsigned long numeric;
unsigned long dummy; if (xstrtoul (&numeric, msg->command, 10))
if (xstrtoul (&dummy, msg->command, 10)) irc_process_numeric (ctx, msg, numeric);
{
// Get rid of the first parameter, if there's any at all,
// as it contains our nickname and is of no practical use to the user
struct str_vector copy;
str_vector_init (&copy);
str_vector_add_vector (&copy, msg->params.vector + !!msg->params.len);
// Join the parameter vector back, recode it to our internal encoding
// and send it to the server buffer
char *reconstructed = join_str_vector (&copy, ' ');
str_vector_free (&copy);
char *utf8 = irc_to_utf8 (ctx, reconstructed);
free (reconstructed);
buffer_send_status (ctx, ctx->server_buffer, "%s", utf8);
free (utf8);
}
} }
// --- User input handling ----------------------------------------------------- // --- User input handling -----------------------------------------------------
@ -3302,6 +3319,10 @@ init_poller_events (struct app_context *ctx)
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
// We include a generated file from kike including this array we don't use;
// let's just keep it there and silence the compiler warning instead
(void) g_default_replies;
static const struct opt opts[] = static const struct opt opts[] =
{ {
{ 'd', "debug", NULL, 0, "run in debug mode" }, { 'd', "debug", NULL, 0, "run in debug mode" },