tmate/tmate-msg.c

77 lines
1.7 KiB
C
Raw Normal View History

2013-06-13 01:40:30 +02:00
#include <time.h>
#include "tmate.h"
void status_message_callback(int, short, void *);
/* Very similar to status.c:status_message_set */
static void tmate_status_message_client(struct client *c, const char *message)
{
struct timeval tv;
2015-12-23 11:41:41 +01:00
struct message_entry *msg, *msg1;
2013-06-13 01:40:30 +02:00
int delay;
2016-01-01 22:44:01 +01:00
u_int limit;
2013-06-13 01:40:30 +02:00
2015-12-24 05:19:55 +01:00
limit = options_get_number(global_options, "message-limit");
2016-01-01 01:55:12 +01:00
delay = options_get_number(c->session ? c->session->options : global_s_options,
"tmate-display-time");
2015-12-24 05:19:55 +01:00
2013-06-13 01:40:30 +02:00
status_prompt_clear(c);
status_message_clear(c);
2013-06-13 10:31:25 +02:00
xasprintf(&c->message_string, "[tmate] %s", message);
2013-06-13 01:40:30 +02:00
2015-12-23 11:41:41 +01:00
msg = xcalloc(1, sizeof *msg);
2013-06-13 01:40:30 +02:00
msg->msg_time = time(NULL);
2015-12-23 11:41:41 +01:00
msg->msg_num = c->message_next++;
2013-06-13 01:40:30 +02:00
msg->msg = xstrdup(c->message_string);
2015-12-23 11:41:41 +01:00
TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
2013-06-13 01:40:30 +02:00
2015-12-23 11:41:41 +01:00
TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
2016-01-01 01:55:12 +01:00
if (msg->msg_num + limit >= c->message_next)
break;
2015-12-23 11:41:41 +01:00
free(msg->msg);
TAILQ_REMOVE(&c->message_log, msg, entry);
free(msg);
2013-06-13 01:40:30 +02:00
}
2015-12-23 11:41:41 +01:00
if (delay > 0) {
tv.tv_sec = delay / 1000;
tv.tv_usec = (delay % 1000) * 1000L;
2013-06-13 01:40:30 +02:00
2015-12-23 11:41:41 +01:00
if (event_initialized(&c->message_timer))
evtimer_del(&c->message_timer);
evtimer_set(&c->message_timer, status_message_callback, c);
evtimer_add(&c->message_timer, &tv);
}
2013-06-13 01:40:30 +02:00
c->flags |= CLIENT_STATUS | CLIENT_FORCE_STATUS;
recalculate_sizes();
}
void __tmate_status_message(const char *fmt, va_list ap)
{
struct client *c;
char *message;
xvasprintf(&message, fmt, ap);
tmate_debug("%s", message);
2015-12-23 11:41:41 +01:00
TAILQ_FOREACH(c, &clients, entry) {
2013-06-13 01:40:30 +02:00
if (c && !(c->flags & CLIENT_READONLY))
tmate_status_message_client(c, message);
}
free(message);
}
void tmate_status_message(const char *fmt, ...)
2013-06-13 01:40:30 +02:00
{
va_list ap;
va_start(ap, fmt);
__tmate_status_message(fmt, ap);
va_end(ap);
}