kike: add support for IRCv3.2 server-time

This commit is contained in:
Přemysl Eric Janouch 2016-01-31 20:06:45 +01:00
parent 2ec6258ff3
commit 10a264ec3d
4 changed files with 40 additions and 3 deletions

2
NEWS
View File

@ -12,6 +12,8 @@
* degesch: backlog limit was made configurable * degesch: backlog limit was made configurable
* kike: add support for IRCv3.2 server-time
* Remote addresses are now resolved asynchronously * Remote addresses are now resolved asynchronously
* Various bugfixes * Various bugfixes

View File

@ -52,6 +52,21 @@ str_vector_find (const struct str_vector *v, const char *s)
return -1; return -1;
} }
static time_t
unixtime_msec (long *msec)
{
#ifdef _POSIX_TIMERS
struct timespec tp;
hard_assert (clock_gettime (CLOCK_REALTIME, &tp) != -1);
*msec = tp.tv_nsec / 1000000;
#else // ! _POSIX_TIMERS
struct timeval tp;
hard_assert (gettimeofday (&tp, NULL) != -1);
*msec = tp.tv_usec / 1000;
#endif // ! _POSIX_TIMERS
return tp.tv_sec;
}
/// This differs from the non-unique version in that we expect the filename /// This differs from the non-unique version in that we expect the filename
/// to be something like a pattern for mkstemp(), so the resulting path can /// to be something like a pattern for mkstemp(), so the resulting path can
/// reside in a system-wide directory with no risk of a conflict. /// reside in a system-wide directory with no risk of a conflict.

View File

@ -3144,6 +3144,7 @@ log_formatter (struct app_context *ctx,
// Move the formatter inside // Move the formatter inside
struct buffer_line *line = buffer_line_new (f); struct buffer_line *line = buffer_line_new (f);
line->flags = flags; line->flags = flags;
// TODO: allow providing custom time (IRCv3.2 server-time)
line->when = time (NULL); line->when = time (NULL);
buffer_pop_excess_lines (ctx, buffer); buffer_pop_excess_lines (ctx, buffer);
@ -5474,6 +5475,7 @@ irc_handle_cap (struct server *s, const struct irc_message *msg)
const char *cap = v.vector[i]; const char *cap = v.vector[i];
if (!strcasecmp_ascii (cap, "multi-prefix") if (!strcasecmp_ascii (cap, "multi-prefix")
|| !strcasecmp_ascii (cap, "invite-notify") || !strcasecmp_ascii (cap, "invite-notify")
|| !strcasecmp_ascii (cap, "server-time")
|| !strcasecmp_ascii (cap, "echo-message")) || !strcasecmp_ascii (cap, "echo-message"))
str_vector_add (&chosen, cap); str_vector_add (&chosen, cap);
} }
@ -6827,6 +6829,9 @@ irc_process_numeric (struct server *s,
static void static void
irc_process_message (const struct irc_message *msg, struct server *s) irc_process_message (const struct irc_message *msg, struct server *s)
{ {
// TODO: make use of IRCv3.2 server-time (with fallback to unixtime_msec())
// -> change all calls to log_{server,nick,outcoming,ctcp}*() to take
// an extra argument specifying time
struct irc_handler key = { .name = msg->command }; struct irc_handler key = { .name = msg->command };
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);

21
kike.c
View File

@ -307,7 +307,8 @@ enum
IRC_CAP_MULTI_PREFIX = (1 << 0), IRC_CAP_MULTI_PREFIX = (1 << 0),
IRC_CAP_INVITE_NOTIFY = (1 << 1), IRC_CAP_INVITE_NOTIFY = (1 << 1),
IRC_CAP_ECHO_MESSAGE = (1 << 2), IRC_CAP_ECHO_MESSAGE = (1 << 2),
IRC_CAP_USERHOST_IN_NAMES = (1 << 3) IRC_CAP_USERHOST_IN_NAMES = (1 << 3),
IRC_CAP_SERVER_TIME = (1 << 4)
}; };
struct client struct client
@ -832,9 +833,22 @@ client_send_str (struct client *c, const struct str *s)
hard_assert (!c->closing_link); hard_assert (!c->closing_link);
size_t old_sendq = c->write_buffer.len; size_t old_sendq = c->write_buffer.len;
// So far there's only one message tag we use, so we can do it simple;
// note that a 512-character limit applies to messages with tags on
if (c->caps_enabled & IRC_CAP_SERVER_TIME)
{
long milliseconds; char buf[32]; struct tm tm;
time_t now = unixtime_msec (&milliseconds);
if (soft_assert (strftime (buf, sizeof buf,
"%Y-%m-%dT%T", gmtime_r (&now, &tm))))
str_append_printf (&c->write_buffer,
"@time=%s.%03ldZ ", buf, milliseconds);
}
// TODO: kill the connection above some "SendQ" threshold (careful!) // TODO: kill the connection above some "SendQ" threshold (careful!)
str_append_data (&c->write_buffer, s->str, str_append_data (&c->write_buffer, s->str,
s->len > IRC_MAX_MESSAGE_LENGTH ? IRC_MAX_MESSAGE_LENGTH : s->len); MIN (s->len, IRC_MAX_MESSAGE_LENGTH));
str_append (&c->write_buffer, "\r\n"); str_append (&c->write_buffer, "\r\n");
// XXX: we might want to move this elsewhere, so that it doesn't get called // XXX: we might want to move this elsewhere, so that it doesn't get called
// as often; it's going to cause a lot of syscalls with epoll. // as often; it's going to cause a lot of syscalls with epoll.
@ -1251,6 +1265,7 @@ irc_cap_table[] =
{ IRC_CAP_INVITE_NOTIFY, "invite-notify" }, { IRC_CAP_INVITE_NOTIFY, "invite-notify" },
{ IRC_CAP_ECHO_MESSAGE, "echo-message" }, { IRC_CAP_ECHO_MESSAGE, "echo-message" },
{ IRC_CAP_USERHOST_IN_NAMES, "userhost-in-names" }, { IRC_CAP_USERHOST_IN_NAMES, "userhost-in-names" },
{ IRC_CAP_SERVER_TIME, "server-time" },
}; };
static void static void
@ -1263,7 +1278,7 @@ irc_handle_cap_ls (struct client *c, struct irc_cap_args *a)
c->cap_negotiating = true; c->cap_negotiating = true;
client_send (c, ":%s CAP %s LS :multi-prefix invite-notify echo-message" client_send (c, ":%s CAP %s LS :multi-prefix invite-notify echo-message"
" userhost-in-names", c->ctx->server_name, a->target); " userhost-in-names server-time", c->ctx->server_name, a->target);
} }
static void static void