degesch: make a "struct user" for ourselves

This commit is contained in:
Přemysl Eric Janouch 2015-04-20 22:09:56 +02:00
parent a43051132a
commit 3fa9a67a59
1 changed files with 19 additions and 17 deletions

View File

@ -417,8 +417,9 @@ struct app_context
struct str_map irc_channels; ///< IRC channel data
struct str_map irc_buffer_map; ///< Maps IRC identifiers to buffers
char *irc_nickname; ///< Current nickname
char *irc_user_mode; ///< Current user mode
struct user *irc_user; ///< Our own user
char *irc_user_mode; ///< Our current user mode
char *irc_user_host; ///< Our current user@host
// Events:
@ -535,8 +536,9 @@ app_context_free (struct app_context *self)
str_map_free (&self->irc_channels);
str_map_free (&self->irc_buffer_map);
free (self->irc_nickname);
user_unref (self->irc_user);
free (self->irc_user_mode);
free (self->irc_user_host);
poller_free (&self->poller);
@ -1246,7 +1248,7 @@ static bool
irc_is_this_us (struct app_context *ctx, const char *prefix)
{
char *nick = irc_cut_nickname (prefix);
bool result = !irc_strcmp (nick, ctx->irc_nickname);
bool result = !irc_strcmp (nick, ctx->irc_user->nickname);
free (nick);
return result;
}
@ -1596,7 +1598,7 @@ make_prompt (struct app_context *ctx, struct str *output)
str_append (output, "(disconnected)");
else
{
str_append (output, ctx->irc_nickname);
str_append (output, ctx->irc_user->nickname);
if (*ctx->irc_user_mode)
str_append_printf (output, "(%s)", ctx->irc_user_mode);
}
@ -1846,8 +1848,6 @@ irc_handle_nick (struct app_context *ctx, const struct irc_message *msg)
if (!msg->prefix || msg->params.len < 1)
return;
// FIXME: make sure we have an associated user;
// we might be able to get rid of "irc_nickname" then
char *nickname = irc_cut_nickname (msg->prefix);
struct user *user = str_map_find (&ctx->irc_users, nickname);
free (nickname);
@ -1857,11 +1857,6 @@ irc_handle_nick (struct app_context *ctx, const struct irc_message *msg)
const char *new_nickname = msg->params.vector[0];
if (irc_is_this_us (ctx, msg->prefix))
{
// Update user information
free (ctx->irc_nickname);
ctx->irc_nickname = xstrdup (new_nickname);
refresh_prompt (ctx);
// Log a message in all open buffers on this server
struct str_map_iter iter;
str_map_iter_init (&iter, &ctx->irc_buffer_map);
@ -1897,6 +1892,9 @@ irc_handle_nick (struct app_context *ctx, const struct irc_message *msg)
// Finally rename the user
free (user->nickname);
user->nickname = xstrdup (new_nickname);
// We might have renamed ourselves
refresh_prompt (ctx);
}
static void
@ -2104,7 +2102,7 @@ irc_process_message (const struct irc_message *msg,
// TODO: parse any response and store the result for us in app_context;
// this enables proper message splitting on output;
// we can also use WHOIS if it's not supported (optional by RFC 2812)
irc_send (ctx, "USERHOST %s", ctx->irc_nickname);
irc_send (ctx, "USERHOST %s", ctx->irc_user->nickname);
const char *autojoin = str_map_find (&ctx->config, "autojoin");
if (autojoin)
@ -2485,7 +2483,7 @@ send_message_to_target (struct app_context *ctx,
// TODO: autosplit
irc_send (ctx, "PRIVMSG %s :%s", target, message);
buffer_send (ctx, buffer, BUFFER_LINE_PRIVMSG, 0,
ctx->irc_nickname, NULL, "%s", message);
ctx->irc_user->nickname, NULL, "%s", message);
}
static void
@ -2646,10 +2644,12 @@ on_irc_disconnected (struct app_context *ctx)
xclose (ctx->irc_fd);
ctx->irc_fd = -1;
ctx->irc_ready = false;
free (ctx->irc_nickname);
ctx->irc_nickname = NULL;
user_unref (ctx->irc_user);
ctx->irc_user = NULL;
free (ctx->irc_user_mode);
ctx->irc_user_mode = NULL;
free (ctx->irc_user_host);
ctx->irc_user_host = NULL;
ctx->irc_event.closed = true;
poller_fd_reset (&ctx->irc_event);
@ -2813,8 +2813,10 @@ irc_connect (struct app_context *ctx, struct error **e)
irc_send (ctx, "USER %s 8 * :%s", username, realname);
// XXX: maybe we should wait for the first message from the server
ctx->irc_nickname = xstrdup (nickname);
ctx->irc_user = user_new ();
ctx->irc_user->nickname = xstrdup (nickname);
ctx->irc_user_mode = xstrdup ("");
ctx->irc_user_host = NULL;
return true;
}