Unethical IRC client, daemon and bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

12597 lines
339 KiB

/*
* degesch.c: the experimental IRC client
*
* Copyright (c) 2015 - 2016, Přemysl Janouch <p.janouch@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
// A table of all attributes we use for output
// FIXME: awful naming, collides with ATTRIBUTE_*
#define ATTR_TABLE(XX) \
XX( PROMPT, "prompt", "Terminal attrs for the prompt" ) \
XX( RESET, "reset", "String to reset terminal attributes" ) \
XX( READ_MARKER, "read_marker", "Terminal attrs for the read marker" ) \
XX( WARNING, "warning", "Terminal attrs for warnings" ) \
XX( ERROR, "error", "Terminal attrs for errors" ) \
XX( EXTERNAL, "external", "Terminal attrs for external lines" ) \
XX( TIMESTAMP, "timestamp", "Terminal attrs for timestamps" ) \
XX( HIGHLIGHT, "highlight", "Terminal attrs for highlights" ) \
XX( ACTION, "action", "Terminal attrs for user actions" ) \
XX( USERHOST, "userhost", "Terminal attrs for user@host" ) \
XX( JOIN, "join", "Terminal attrs for joins" ) \
XX( PART, "part", "Terminal attrs for parts" )
enum
{
#define XX(x, y, z) ATTR_ ## x,
ATTR_TABLE (XX)
#undef XX
ATTR_COUNT
};
// User data for logger functions to enable formatted logging
#define print_fatal_data ((void *) ATTR_ERROR)
#define print_error_data ((void *) ATTR_ERROR)
#define print_warning_data ((void *) ATTR_WARNING)
#include "config.h"
#define PROGRAM_NAME "degesch"
#include "common.c"
#include "kike-replies.c"
#include <langinfo.h>
#include <locale.h>
#include <pwd.h>
#include <sys/utsname.h>
#include <wchar.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <curses.h>
#include <term.h>
// Literally cancer
#undef lines
#undef columns
#include <ffi.h>
#ifdef HAVE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif // HAVE_READLINE
#ifdef HAVE_EDITLINE
#include <histedit.h>
#endif // HAVE_EDITLINE
#ifdef HAVE_LUA
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#endif // HAVE_LUA
/// Some arbitrary limit for the history file
#define HISTORY_LIMIT 10000
/// Characters that separate words
#define WORD_BREAKING_CHARS " \f\n\r\t\v"
// --- User interface ----------------------------------------------------------
// I'm not sure which one of these backends is worse: whether it's GNU Readline
// or BSD Editline. They both have their own annoying problems.
struct input_buffer
{
#ifdef HAVE_READLINE
HISTORY_STATE *history; ///< Saved history state
char *saved_line; ///< Saved line content
int saved_mark; ///< Saved mark
#elif defined HAVE_EDITLINE
HistoryW *history; ///< The history object
wchar_t *saved_line; ///< Saved line content
int saved_len; ///< Length of the saved line
#endif // HAVE_EDITLINE
int saved_point; ///< Saved cursor position
};
static struct input_buffer *
input_buffer_new (void)
{
struct input_buffer *self = xcalloc (1, sizeof *self);
#ifdef HAVE_EDITLINE
self->history = history_winit ();
HistEventW ev;
history_w (self->history, &ev, H_SETSIZE, HISTORY_LIMIT);
#endif // HAVE_EDITLINE
return self;
}
static void
input_buffer_destroy (struct input_buffer *self)
{
#ifdef HAVE_READLINE
// Can't really free "history" contents from here
free (self->history);
#elif defined HAVE_EDITLINE
history_wend (self->history);
#endif // HAVE_EDITLINE
free (self->saved_line);
free (self);
}
typedef bool (*input_fn) (int count, int key, void *user_data);
struct input_fn_data
{
ffi_closure closure; ///< Closure
LIST_HEADER (struct input_fn_data)
input_fn callback; ///< Real callback
void *user_data; ///< Real callback user data
#ifdef HAVE_EDITLINE
wchar_t *name; ///< Function name
wchar_t *help; ///< Function help
#endif // HAVE_EDITLINE
};
struct input
{
bool active; ///< Are we a thing?
#if defined HAVE_READLINE
char *saved_line; ///< Saved line content
int saved_point; ///< Saved cursor position
int saved_mark; ///< Saved mark
#elif defined HAVE_EDITLINE
EditLine *editline; ///< The EditLine object
#endif // HAVE_EDITLINE
struct input_fn_data *fns; ///< Functions
char *prompt; ///< The prompt we use
int prompt_shown; ///< Whether the prompt is shown now
struct input_buffer *current; ///< Current input buffer
};
static void
input_init (struct input *self)
{
memset (self, 0, sizeof *self);
}
static void
input_free (struct input *self)
{
#ifdef HAVE_READLINE
free (self->saved_line);
#endif // HAVE_READLINE
LIST_FOR_EACH (struct input_fn_data, iter, self->fns)
{
#ifdef HAVE_EDITLINE
free (iter->name);
free (iter->help);
#endif // HAVE_EDITLINE
ffi_closure_free (iter);
}
free (self->prompt);
}
// --- GNU Readline ------------------------------------------------------------
#ifdef HAVE_READLINE
#define INPUT_START_IGNORE RL_PROMPT_START_IGNORE
#define INPUT_END_IGNORE RL_PROMPT_END_IGNORE
#define input_ding(self) rl_ding ()
static void
input_on_terminal_resized (struct input *self)
{
(void) self;
// This fucks up big time on terminals with automatic wrapping such as
// rxvt-unicode or newer VTE when the current line overflows, however we
// can't do much about that
rl_resize_terminal ();
}
static void
input_on_readable (struct input *self)
{
(void) self;
rl_callback_read_char ();
}
static void
input_set_prompt (struct input *self, char *prompt)
{
free (self->prompt);
self->prompt = prompt;
if (!self->active)
return;
// First reset the prompt to work around a bug in readline
rl_set_prompt ("");
if (self->prompt_shown > 0)
rl_redisplay ();
rl_set_prompt (self->prompt);
if (self->prompt_shown > 0)
rl_redisplay ();
}
static void
input_erase_content (struct input *self)
{
(void) self;
rl_replace_line ("", false);
rl_redisplay ();
}
static void
input_erase (struct input *self)
{
(void) self;
rl_set_prompt ("");
input_erase_content (self);
}
static void
input_bind (struct input *self, const char *seq, const char *function_name)
{
(void) self;
rl_bind_keyseq (seq, rl_named_function (function_name));
}
static void
input_bind_meta (struct input *self, char key, const char *function_name)
{
// This one seems to actually work
char keyseq[] = { '\\', 'e', key, 0 };
input_bind (self, keyseq, function_name);
#if 0
// While this one only fucks up UTF-8
// Tested with urxvt and xterm, on Debian Jessie/Arch, default settings
// \M-<key> behaves exactly the same
rl_bind_key (META (key), rl_named_function (function_name));
#endif
}
static void
input_bind_control (struct input *self, char key, const char *function_name)
{
char keyseq[] = { '\\', 'C', '-', key, 0 };
input_bind (self, keyseq, function_name);
}
static bool
input_insert (struct input *self, const char *s)
{
rl_insert_text (s);
if (self->prompt_shown > 0)
rl_redisplay ();
// GNU Readline, contrary to Editline, doesn't care about validity
return true;
}
static char *
input_get_content (struct input *self)
{
(void) self;
return rl_copy_text (0, rl_end);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
input_closure_forwarder (ffi_cif *cif, void *ret, void **args, void *user_data)
{
(void) cif;
struct input_fn_data *data = user_data;
if (!data->callback
(*(int *) args[0], UNMETA (*(int *) args[1]), data->user_data))
rl_ding ();
*(int *) ret = 0;
}
static void
input_add_fn (struct input *self,
const char *name, const char *help, input_fn callback, void *user_data)
{
(void) help;
void *bound_fn = NULL;
struct input_fn_data *data = ffi_closure_alloc (sizeof *data, &bound_fn);
hard_assert (data);
static ffi_cif cif;
static ffi_type *args[2] = { &ffi_type_sint, &ffi_type_sint };
hard_assert (ffi_prep_cif
(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_sint, args) == FFI_OK);
data->prev = data->next = NULL;
data->callback = callback;
data->user_data = user_data;
hard_assert (ffi_prep_closure_loc (&data->closure,
&cif, input_closure_forwarder, data, bound_fn) == FFI_OK);
rl_add_defun (name, (rl_command_func_t *) bound_fn, -1);
LIST_PREPEND (self->fns, data);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static int app_readline_init (void);
static void on_readline_input (char *line);
static char **app_readline_completion (const char *text, int start, int end);
static void
input_start (struct input *self, const char *program_name)
{
using_history ();
// This can cause memory leaks, or maybe even a segfault. Funny, eh?
stifle_history (HISTORY_LIMIT);
const char *slash = strrchr (program_name, '/');
rl_readline_name = slash ? ++slash : program_name;
rl_startup_hook = app_readline_init;
rl_catch_sigwinch = false;
rl_basic_word_break_characters = WORD_BREAKING_CHARS;
rl_completer_word_break_characters = NULL;
rl_attempted_completion_function = app_readline_completion;
hard_assert (self->prompt != NULL);
rl_callback_handler_install (self->prompt, on_readline_input);
self->prompt_shown = 1;
self->active = true;
}
static void
input_stop (struct input *self)
{
if (self->prompt_shown > 0)
input_erase (self);
// This is okay as long as we're not called from within readline
rl_callback_handler_remove ();
self->active = false;
self->prompt_shown = false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// The following part shows you why it's not a good idea to use
// GNU Readline for this kind of software. Or for anything else, really.
static void
input_save_buffer (struct input *self, struct input_buffer *buffer)
{
(void) self;
buffer->history = history_get_history_state ();
buffer->saved_line = rl_copy_text (0, rl_end);
buffer->saved_point = rl_point;
buffer->saved_mark = rl_mark;
rl_replace_line ("", true);
if (self->prompt_shown > 0)
rl_redisplay ();
}
static void
input_restore_buffer (struct input *self, struct input_buffer *buffer)
{
if (buffer->history)
{
// history_get_history_state() just allocates a new HISTORY_STATE
// and fills it with its current internal data. We don't need that
// shell anymore after reviving it.
history_set_history_state (buffer->history);
free (buffer->history);
buffer->history = NULL;
}
else
{
// This should get us a clean history while keeping the flags.
// Note that we've either saved the previous history entries, or we've
// cleared them altogether, so there should be nothing to leak.
HISTORY_STATE *state = history_get_history_state ();
state->offset = state->length = state->size = 0;
state->entries = NULL;
history_set_history_state (state);
free (state);
}
if (buffer->saved_line)
{
rl_replace_line (buffer->saved_line, true);
rl_point = buffer->saved_point;
rl_mark = buffer->saved_mark;
free (buffer->saved_line);
buffer->saved_line = NULL;
if (self->prompt_shown > 0)
rl_redisplay ();
}
}
static void
input_switch_buffer (struct input *self, struct input_buffer *buffer)
{
// There could possibly be occurences of the current undo list in some
// history entry. We either need to free the undo list, or move it
// somewhere else to load back later, as the buffer we're switching to
// has its own history state.
rl_free_undo_list ();
// Save this buffer's history so that it's independent for each buffer
if (self->current)
input_save_buffer (self, self->current);
else
// Just throw it away; there should always be an active buffer however
#if RL_READLINE_VERSION >= 0x0603
rl_clear_history ();
#else // RL_READLINE_VERSION < 0x0603
// At least something... this may leak undo entries
clear_history ();
#endif // RL_READLINE_VERSION < 0x0603
input_restore_buffer (self, buffer);
self->current = buffer;
}
static void
input_destroy_buffer (struct input *self, struct input_buffer *buffer)
{
(void) self;
// rl_clear_history, being the only way I know of to get rid of the complete
// history including attached data, is a pretty recent addition. *sigh*
#if RL_READLINE_VERSION >= 0x0603
if (buffer->history)
{
// See input_switch_buffer() for why we need to do this BS
rl_free_undo_list ();
// This is probably the only way we can free the history fully
HISTORY_STATE *state = history_get_history_state ();
history_set_history_state (buffer->history);
rl_clear_history ();
// rl_clear_history just removes history entries,
// we have to reclaim memory for their actual container ourselves
free (buffer->history->entries);
free (buffer->history);
buffer->history = NULL;
history_set_history_state (state);
free (state);
}
#endif // RL_READLINE_VERSION
input_buffer_destroy (buffer);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
input_save (struct input *self)
{
hard_assert (!self->saved_line);
self->saved_point = rl_point;
self->saved_mark = rl_mark;
self->saved_line = rl_copy_text (0, rl_end);
}
static void
input_restore (struct input *self)
{
hard_assert (self->saved_line);
rl_set_prompt (self->prompt);
rl_replace_line (self->saved_line, false);
rl_point = self->saved_point;
rl_mark = self->saved_mark;
free (self->saved_line);
self->saved_line = NULL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
input_hide (struct input *self)
{
if (!self->active || self->prompt_shown-- < 1)
return;
input_save (self);
input_erase (self);
}
static void
input_show (struct input *self)
{
if (!self->active || ++self->prompt_shown < 1)
return;
input_restore (self);
rl_redisplay ();
}
#endif // HAVE_READLINE
// --- BSD Editline ------------------------------------------------------------
#ifdef HAVE_EDITLINE
#define INPUT_START_IGNORE '\x01'
#define INPUT_END_IGNORE '\x01'
static void app_editline_init (struct input *self);
static void
input_ding (struct input *self)
{
(void) self;
// XXX: this isn't probably very portable;
// we could use "bell" from terminfo but that creates a dependency
write (STDOUT_FILENO, "\a", 1);
}
static void
input_on_terminal_resized (struct input *self)
{
el_resize (self->editline);
}
static void
input_bind (struct input *self, const char *seq, const char *function_name)
{
el_set (self->editline, EL_BIND, seq, function_name, NULL);
}
static void
input_bind_meta (struct input *self, char key, const char *function_name)
{
char keyseq[] = { 'M', '-', key, 0 };
input_bind (self, keyseq, function_name);
}
static void
input_bind_control (struct input *self, char key, const char *function_name)
{
char keyseq[] = { '^', key, 0 };
input_bind (self, keyseq, function_name);
}
static void
input_redisplay (struct input *self)
{
// See rl_redisplay()
// The character is VREPRINT (usually C-r)
// TODO: read it from terminal info
// XXX: could we potentially break UTF-8 with this?
char x[] = { ('R' - 'A' + 1), 0 };
el_push (self->editline, x);
// We have to do this or it gets stuck and nothing is done
(void) el_gets (self->editline, NULL);
}
static void
input_set_prompt (struct input *self, char *prompt)
{
free (self->prompt);
self->prompt = prompt;
if (self->prompt_shown > 0)
input_redisplay (self);
}
static char *
input_make_prompt (EditLine *editline)
{
struct input *self;
el_get (editline, EL_CLIENTDATA, &self);
if (!self->prompt)
return "";
return self->prompt;
}
static char *
input_make_empty_prompt (EditLine *editline)
{
(void) editline;
return "";
}
static void
input_erase_content (struct input *self)
{
const LineInfoW *info = el_wline (self->editline);
int len = info->lastchar - info->buffer;
int point = info->cursor - info->buffer;
el_cursor (self->editline, len - point);
el_wdeletestr (self->editline, len);
input_redisplay (self);
}
static void
input_erase (struct input *self)
{
el_set (self->editline, EL_PROMPT, input_make_empty_prompt);
input_erase_content (self);
}
static bool
input_insert (struct input *self, const char *s)
{
bool success = !*s || !el_insertstr (self->editline, s);
if (self->prompt_shown > 0)
input_redisplay (self);
return success;
}
static char *
input_get_content (struct input *self)
{
const LineInfo *info = el_line (self->editline);
return xstrndup (info->buffer, info->lastchar - info->buffer);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
input_closure_forwarder (ffi_cif *cif, void *ret, void **args, void *user_data)
{
(void) cif;
struct input_fn_data *data = user_data;
*(unsigned char *) ret = data->callback
(1, *(int *) args[1], data->user_data) ? CC_NORM : CC_ERROR;
}
static wchar_t *
ascii_to_wide (const char *ascii)
{
size_t len = strlen (ascii) + 1;
wchar_t *wide = xcalloc (sizeof *wide, len);
while (len--)
hard_assert ((wide[len] = (unsigned char) ascii[len]) < 0x80);
return wide;
}
static void
input_add_fn (struct input *self,
const char *name, const char *help, input_fn callback, void *user_data)
{
void *bound_fn = NULL;
struct input_fn_data *data = ffi_closure_alloc (sizeof *data, &bound_fn);
hard_assert (data);
static ffi_cif cif;
static ffi_type *args[2] = { &ffi_type_pointer, &ffi_type_sint };
hard_assert (ffi_prep_cif
(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_uchar, args) == FFI_OK);
data->user_data = user_data;
data->callback = callback;
data->name = ascii_to_wide (name);
data->help = ascii_to_wide (help);
hard_assert (ffi_prep_closure_loc (&data->closure,
&cif, input_closure_forwarder, data, bound_fn) == FFI_OK);
el_wset (self->editline, EL_ADDFN, data->name, data->help, bound_fn);
LIST_PREPEND (self->fns, data);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
input_start (struct input *self, const char *program_name)
{
self->editline = el_init (program_name, stdin, stdout, stderr);
el_set (self->editline, EL_CLIENTDATA, self);
el_set (self->editline, EL_PROMPT_ESC,
input_make_prompt, INPUT_START_IGNORE);
el_set (self->editline, EL_SIGNAL, false);
el_set (self->editline, EL_UNBUFFERED, true);
el_set (self->editline, EL_EDITOR, "emacs");
app_editline_init (self);
self->prompt_shown = 1;
self->active = true;
}
static void
input_stop (struct input *self)
{
if (self->prompt_shown > 0)
input_erase (self);
el_end (self->editline);
self->editline = NULL;
self->active = false;
self->prompt_shown = false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
input_save_buffer (struct input *self, struct input_buffer *buffer)
{
const LineInfoW *info = el_wline (self->editline);
int len = info->lastchar - info->buffer;
int point = info->cursor - info->buffer;
wchar_t *line = calloc (sizeof *info->buffer, len + 1);
memcpy (line, info->buffer, sizeof *info->buffer * len);
el_cursor (self->editline, len - point);
el_wdeletestr (self->editline, len);
buffer->saved_line = line;
buffer->saved_point = point;
buffer->saved_len = len;
}
static void
input_restore_buffer (struct input *self, struct input_buffer *buffer)
{
if (buffer->saved_line)
{
el_winsertstr (self->editline, buffer->saved_line);
el_cursor (self->editline,
-(buffer->saved_len - buffer->saved_point));
free (buffer->saved_line);
buffer->saved_line = NULL;
}
}
static void
input_switch_buffer (struct input *self, struct input_buffer *buffer)
{
if (self->current)
input_save_buffer (self, self->current);
input_restore_buffer (self, buffer);
el_wset (self->editline, EL_HIST, history, buffer->history);
self->current = buffer;
}
static void
input_destroy_buffer (struct input *self, struct input_buffer *buffer)
{
(void) self;
input_buffer_destroy (buffer);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
input_save (struct input *self)
{
if (self->current)
input_save_buffer (self, self->current);
}
static void
input_restore (struct input *self)
{
if (self->current)
input_restore_buffer (self, self->current);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
input_hide (struct input *self)
{
if (!self->active || self->prompt_shown-- < 1)
return;
input_save (self);
input_erase (self);
}
static void
input_show (struct input *self)
{
if (!self->active || ++self->prompt_shown < 1)
return;
input_restore (self);
// XXX: the ignore doesn't quite work, see https://gnats.netbsd.org/47539
el_set (self->editline,
EL_PROMPT_ESC, input_make_prompt, INPUT_START_IGNORE);
input_redisplay (self);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
input_on_readable (struct input *self)
{
// We bind the return key to process it how we need to
// el_gets() with EL_UNBUFFERED doesn't work with UTF-8,
// we must use the wide-character interface
int count = 0;
const wchar_t *buf = el_wgets (self->editline, &count);
if (!buf || count-- <= 0)
return;
// The character is VEOF (usually C-d)
// TODO: read it from terminal info
if (count == 0 && buf[0] == ('D' - 'A' + 1))
{
el_deletestr (self->editline, 1);
input_redisplay (self);
input_ding (self);
}
}
#endif // HAVE_EDITLINE
// --- Application data --------------------------------------------------------
// All text stored in our data structures is encoded in UTF-8.
// Or at least should be. The exception is IRC identifiers.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// We need a few reference countable objects with support for both strong
// and weak references (mainly used for scripted plugins).
//
// Beware that if you don't own the object, you will most probably want
// to keep the weak reference link so that you can get rid of it later.
// Also note that you have to make sure the user_data don't leak resources.
//
// Having a callback is more versatile than just nulling out a pointer.
/// Callback just before a reference counted object is destroyed
typedef void (*destroy_cb_fn) (void *object, void *user_data);
struct weak_ref_link
{
LIST_HEADER (struct weak_ref_link)
destroy_cb_fn on_destroy; ///< Called when object is destroyed
void *user_data; ///< User data
};
static struct weak_ref_link *
weak_ref (struct weak_ref_link **list, destroy_cb_fn cb, void *user_data)
{
struct weak_ref_link *link = xcalloc (1, sizeof *link);
link->on_destroy = cb;
link->user_data = user_data;
LIST_PREPEND (*list, link);
return link;
}
static void
weak_unref (struct weak_ref_link **list, struct weak_ref_link **link)
{
if (*link)
LIST_UNLINK (*list, *link);
free (*link);
*link = NULL;
}
#define REF_COUNTABLE_HEADER \
size_t ref_count; /**< Reference count */ \
struct weak_ref_link *weak_refs; /**< To remove any weak references */
#define REF_COUNTABLE_METHODS(name) \
static struct name * \
name ## _ref (struct name *self) \
{ \
self->ref_count++; \
return self; \
} \
\
static void \
name ## _unref (struct name *self) \
{ \
if (--self->ref_count) \
return; \
LIST_FOR_EACH (struct weak_ref_link, iter, self->weak_refs) \
{ \
iter->on_destroy (self, iter->user_data); \
free (iter); \
} \
name ## _destroy (self); \
} \
\
static struct weak_ref_link * \
name ## _weak_ref (struct name *self, destroy_cb_fn cb, void *user_data) \
{ return weak_ref (&self->weak_refs, cb, user_data); } \
\
static void \
name ## _weak_unref (struct name *self, struct weak_ref_link **link) \
{ weak_unref (&self->weak_refs, link); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct user_channel
{
LIST_HEADER (struct user_channel)
struct channel *channel; ///< Reference to channel
};
static struct user_channel *
user_channel_new (void)
{
struct user_channel *self = xcalloc (1, sizeof *self);
return self;
}
static void
user_channel_destroy (struct user_channel *self)
{
// The "channel" reference is weak and this object should get
// destroyed whenever the user stops being in the channel.
free (self);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// We keep references to user information in channels and buffers,
// and weak references in the name lookup table.
struct user
{
REF_COUNTABLE_HEADER
char *nickname; ///< Literal nickname
// TODO: write code to poll for the away status
bool away; ///< User is away
struct user_channel *channels; ///< Channels the user is on
};
static struct user *
user_new (void)
{
struct user *self = xcalloc (1, sizeof *self);
self->ref_count = 1;
return self;
}
static void
user_destroy (struct user *self)
{
free (self->nickname);
LIST_FOR_EACH (struct user_channel, iter, self->channels)
user_channel_destroy (iter);
free (self);
}
REF_COUNTABLE_METHODS (user)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct channel_user
{
LIST_HEADER (struct channel_user)
struct user *user; ///< Reference to user
struct str prefixes; ///< Ordered @+... characters
};
static struct channel_user *
channel_user_new (void)
{
struct channel_user *self = xcalloc (1, sizeof *self);
str_init (&self->prefixes);
return self;
}
static void
channel_user_destroy (struct channel_user *self)
{
user_unref (self->user);
str_free (&self->prefixes);
free (self);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// We keep references to channels in their buffers,
// and weak references in their users and the name lookup table.
// XXX: this doesn't really have to be reference countable
struct channel
{
REF_COUNTABLE_HEADER
char *name; ///< Channel name
char *topic; ///< Channel topic
// XXX: write something like an ordered set of characters object?
struct str no_param_modes; ///< No parameter channel modes
struct str_map param_modes; ///< Parametrized channel modes
struct channel_user *users; ///< Channel users
struct str_vector names_buf; ///< Buffer for RPL_NAMREPLY
bool left_manually; ///< Don't rejoin on reconnect
};
static struct channel *
channel_new (void)
{
struct channel *self = xcalloc (1, sizeof *self);
self->ref_count = 1;
str_init (&self->no_param_modes);
str_map_init (&self->param_modes);
self->param_modes.free = free;
str_vector_init (&self->names_buf);
return self;
}
static void
channel_destroy (struct channel *self)
{
free (self->name);
free (self->topic);
str_free (&self->no_param_modes);
str_map_free (&self->param_modes);
// Owner has to make sure we have no users by now
hard_assert (!self->users);
str_vector_free (&self->names_buf);
free (self);
}
REF_COUNTABLE_METHODS (channel)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
enum formatter_item_type
{
FORMATTER_ITEM_END, ///< Sentinel value for arrays
FORMATTER_ITEM_TEXT, ///< Text
FORMATTER_ITEM_ATTR, ///< Formatting attributes
FORMATTER_ITEM_FG_COLOR, ///< Foreground color
FORMATTER_ITEM_BG_COLOR, ///< Background color
FORMATTER_ITEM_SIMPLE, ///< Toggle mIRC formatting
FORMATTER_ITEM_IGNORE_ATTR ///< Un/set attribute ignoration
};
struct formatter_item
{
enum formatter_item_type type : 16; ///< Type of this item
int attribute : 16; ///< Attribute ID
int color; ///< Color
char *text; ///< String
};
static void
formatter_item_free (struct formatter_item *self)
{
free (self->text);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct formatter
{
struct app_context *ctx; ///< Application context
struct server *s; ///< Server
struct formatter_item *items; ///< Items
size_t items_len; ///< Items used
size_t items_alloc; ///< Items allocated
};
static void
formatter_init (struct formatter *self,
struct app_context *ctx, struct server *s)
{
memset (self, 0, sizeof *self);
self->ctx = ctx;
self->s = s;
self->items = xcalloc (sizeof *self->items, (self->items_alloc = 16));
self->items_len = 0;
}
static void
formatter_free (struct formatter *self)
{
for (size_t i = 0; i < self->items_len; i++)
formatter_item_free (&self->items[i]);
free (self->items);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
enum buffer_line_flags
{
BUFFER_LINE_STATUS = 1 << 0, ///< Status message
BUFFER_LINE_ERROR = 1 << 1, ///< Error message
BUFFER_LINE_HIGHLIGHT = 1 << 2, ///< The user was highlighted by this
BUFFER_LINE_SKIP_FILE = 1 << 3, ///< Don't log this to file
BUFFER_LINE_INDENT = 1 << 4, ///< Just indent the line
BUFFER_LINE_UNIMPORTANT = 1 << 5 ///< Joins, parts, similar spam
};
struct buffer_line
{
LIST_HEADER (struct buffer_line)
int flags; ///< Flags
time_t when; ///< Time of the event
struct formatter_item items[]; ///< Line data
};
/// Create a new buffer line stealing all data from the provided formatter
struct buffer_line *
buffer_line_new (struct formatter *f)
{
// We make space for one more item that gets initialized to all zeros,
// meaning FORMATTER_ITEM_END (because it's the first value in the enum)
size_t items_size = f->items_len * sizeof *f->items;
struct buffer_line *self =
xcalloc (1, sizeof *self + items_size + sizeof *self->items);
memcpy (self->items, f->items, items_size);
// We've stolen pointers from the formatter, let's destroy it altogether
free (f->items);
memset (f, 0, sizeof *f);
return self;
}
static void
buffer_line_destroy (struct buffer_line *self)
{
for (struct formatter_item *iter = self->items; iter->type; iter++)
formatter_item_free (iter);
free (self);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
enum buffer_type
{
BUFFER_GLOBAL, ///< Global information
BUFFER_SERVER, ///< Server-related messages
BUFFER_CHANNEL, ///< Channels
BUFFER_PM ///< Private messages (query)
};
struct buffer
{
LIST_HEADER (struct buffer)
REF_COUNTABLE_HEADER
enum buffer_type type; ///< Type of the buffer
char *name; ///< The name of the buffer
struct input_buffer *input_data; ///< User interface data
// Buffer contents:
struct buffer_line *lines; ///< All lines in this buffer
struct buffer_line *lines_tail; ///< The tail of buffer lines
unsigned lines_count; ///< How many lines we have
unsigned unseen_messages_count; ///< # messages since last visited
unsigned unseen_unimportant_count; ///< How much of that is unimportant
bool highlighted; ///< We've been highlighted
FILE *log_file; ///< Log file
// Origin information:
struct server *server; ///< Reference to server
struct channel *channel; ///< Reference to channel
struct user *user; ///< Reference to user
};
static struct buffer *
buffer_new (void)
{
struct buffer *self = xcalloc (1, sizeof *self);
self->ref_count = 1;
self->input_data = input_buffer_new ();
return self;
}
static void
buffer_destroy (struct buffer *self)
{
free (self->name);
if (self->input_data)
input_buffer_destroy (self->input_data);
LIST_FOR_EACH (struct buffer_line, iter, self->lines)
buffer_line_destroy (iter);
if (self->log_file)
(void) fclose (self->log_file);
if (self->user)
user_unref (self->user);
if (self->channel)
channel_unref (self->channel);
free (self);
}
REF_COUNTABLE_METHODS (buffer)
#define buffer_ref do_not_use_dangerous
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// The only real purpose of this is to abstract away TLS
struct transport
{
/// Initialize the transport
bool (*init) (struct server *s, const char *hostname, struct error **e);
/// Destroy the user data pointer
void (*cleanup) (struct server *s);
/// The underlying socket may have become readable, update `read_buffer'
enum socket_io_result (*try_read) (struct server *s);
/// The underlying socket may have become writeable, flush `write_buffer'
enum socket_io_result (*try_write) (struct server *s);
/// Return event mask to use in the poller
int (*get_poll_events) (struct server *s);
/// Called just before closing the connection from our side
void (*in_before_shutdown) (struct server *s);
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
enum server_state
{
IRC_DISCONNECTED, ///< Not connected
IRC_CONNECTING, ///< Connecting to the server
IRC_CONNECTED, ///< Trying to register
IRC_REGISTERED, ///< We can chat now
IRC_CLOSING, ///< Flushing output before shutdown
IRC_HALF_CLOSED ///< Connection shutdown from our side
};
/// Convert an IRC identifier character to lower-case
typedef int (*irc_tolower_fn) (int);
/// Key conversion function for hashmap lookups
typedef size_t (*irc_strxfrm_fn) (char *, const char *, size_t);
struct server
{
REF_COUNTABLE_HEADER
struct app_context *ctx; ///< Application context
char *name; ///< Server identifier
struct buffer *buffer; ///< The buffer for this server
struct config_item *config; ///< Configuration root
// Connection:
enum server_state state; ///< Connection state
struct connector *connector; ///< Connection establisher
struct socks_connector *socks_conn; ///< SOCKS connection establisher
unsigned reconnect_attempt; ///< Number of reconnect attempt
bool manual_disconnect; ///< Don't reconnect after disconnect
int socket; ///< Socket FD of the server
struct str read_buffer; ///< Input yet to be processed
struct str write_buffer; ///< Outut yet to be be sent out
struct poller_fd socket_event; ///< We can read from the socket
struct transport *transport; ///< Transport method
void *transport_data; ///< Transport data
// Events:
struct poller_timer ping_tmr; ///< We should send a ping
struct poller_timer timeout_tmr; ///< Connection seems to be dead
struct poller_timer reconnect_tmr; ///< We should reconnect now
struct poller_timer autojoin_tmr; ///< Re/join channels as appropriate
// IRC:
// TODO: an output queue to prevent excess floods (this will be needed
// especially for away status polling)
bool rehashing; ///< Rehashing IRC identifiers
struct str_map irc_users; ///< IRC user data
struct str_map irc_channels; ///< IRC channel data
struct str_map irc_buffer_map; ///< Maps IRC identifiers to buffers
struct user *irc_user; ///< Our own user
int nick_counter; ///< Iterates "nicks" when registering
struct str irc_user_mode; ///< Our current user modes
char *irc_user_host; ///< Our current user@host
bool autoaway_active; ///< Autoaway is currently active
bool cap_echo_message; ///< Whether the server echos messages
// Server-specific information (from RPL_ISUPPORT):
irc_tolower_fn irc_tolower; ///< Server tolower()
irc_strxfrm_fn irc_strxfrm; ///< Server strxfrm()
char *irc_chantypes; ///< Channel types (name prefixes)
char *irc_idchan_prefixes; ///< Prefixes for "safe channels"
char *irc_statusmsg; ///< Prefixes for channel targets
char *irc_chanmodes_list; ///< Channel modes for lists
char *irc_chanmodes_param_always; ///< Channel modes with mandatory param
char *irc_chanmodes_param_when_set; ///< Channel modes with param when set
char *irc_chanmodes_param_never; ///< Channel modes without param
char *irc_chanuser_prefixes; ///< Channel user prefixes
char *irc_chanuser_modes; ///< Channel user modes
unsigned irc_max_modes; ///< Max parametrized modes per command
};
static void on_irc_timeout (void *user_data);
static void on_irc_ping_timeout (void *user_data);
static void on_irc_autojoin_timeout (void *user_data);
static void irc_initiate_connect (struct server *s);
static void
server_init_specifics (struct server *self)
{
// Defaults as per the RPL_ISUPPORT drafts, or RFC 1459
self->irc_tolower = irc_tolower;
self->irc_strxfrm = irc_strxfrm;
self->irc_chantypes = xstrdup ("#&");
self->irc_idchan_prefixes = xstrdup ("");
self->irc_statusmsg = xstrdup ("");
self->irc_chanmodes_list = xstrdup ("b");
self->irc_chanmodes_param_always = xstrdup ("k");
self->irc_chanmodes_param_when_set = xstrdup ("l");
self->irc_chanmodes_param_never = xstrdup ("imnpst");
self->irc_chanuser_prefixes = xstrdup ("@+");
self->irc_chanuser_modes = xstrdup ("ov");
self->irc_max_modes = 3;
}
static void
server_free_specifics (struct server *self)
{
free (self->irc_chantypes);
free (self->irc_idchan_prefixes);
free (self->irc_statusmsg);
free (self->irc_chanmodes_list);
free (self->irc_chanmodes_param_always);
free (self->irc_chanmodes_param_when_set);
free (self->irc_chanmodes_param_never);
free (self->irc_chanuser_prefixes);
free (self->irc_chanuser_modes);
}
static struct server *
server_new (struct poller *poller)
{
struct server *self = xcalloc (1, sizeof *self);
self->ref_count = 1;
self->socket = -1;
str_init (&self->read_buffer);
str_init (&self->write_buffer);
self->state = IRC_DISCONNECTED;
poller_timer_init (&self->timeout_tmr, poller);
self->timeout_tmr.dispatcher = on_irc_timeout;
self->timeout_tmr.user_data = self;
poller_timer_init (&self->ping_tmr, poller);
self->ping_tmr.dispatcher = on_irc_ping_timeout;
self->ping_tmr.user_data = self;
poller_timer_init (&self->reconnect_tmr, poller);
self->reconnect_tmr.dispatcher = (poller_timer_fn) irc_initiate_connect;
self->reconnect_tmr.user_data = self;
poller_timer_init (&self->autojoin_tmr, poller);
self->autojoin_tmr.dispatcher = on_irc_autojoin_timeout;
self->autojoin_tmr.user_data = self;
str_map_init (&self->irc_users);
self->irc_users.key_xfrm = irc_strxfrm;
str_map_init (&self->irc_channels);
self->irc_channels.key_xfrm = irc_strxfrm;
str_map_init (&self->irc_buffer_map);
self->irc_buffer_map.key_xfrm = irc_strxfrm;
str_init (&self->irc_user_mode);
server_init_specifics (self);
return self;
}
static void
server_destroy (struct server *self)
{
free (self->name);
if (self->connector)
{
connector_free (self->connector);
free (self->connector);
}
if (self->socks_conn)
{
socks_connector_free (self->socks_conn);
free (self->socks_conn);
}
if (self->transport
&& self->transport->cleanup)
self->transport->cleanup (self);
if (self->socket != -1)
{
xclose (self->socket);
self->socket_event.closed = true;
poller_fd_reset (&self->socket_event);
}
str_free (&self->read_buffer);
str_free (&self->write_buffer);
poller_timer_reset (&self->ping_tmr);
poller_timer_reset (&self->timeout_tmr);
poller_timer_reset (&self->reconnect_tmr);
poller_timer_reset (&self->autojoin_tmr);
str_map_free (&self->irc_users);
str_map_free (&self->irc_channels);
str_map_free (&self->irc_buffer_map);
if (self->irc_user)
user_unref (self->irc_user);
str_free (&self->irc_user_mode);
free (self->irc_user_host);
server_free_specifics (self);
free (self);
}
REF_COUNTABLE_METHODS (server)
#define server_ref do_not_use_dangerous
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct plugin
{
LIST_HEADER (struct plugin)
char *name; ///< Name of the plugin
struct plugin_vtable *vtable; ///< Methods
};
struct plugin_vtable
{
/// Unregister and free the plugin including all relevant resources
void (*free) (struct plugin *self);
};
static void
plugin_destroy (struct plugin *self)
{
self->vtable->free (self);
free (self->name);
free (self);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// This is a bit ugly since insertion is O(n) and the need to get rid of the
// specific type because of list macros, however I don't currently posses any
// strictly better, ordered data structure
struct hook
{
LIST_HEADER (struct hook)
int priority; ///< The lesser the sooner
};
static struct hook *
hook_insert (struct hook *list, struct hook *item)
{
// Corner cases: list is empty or we precede everything
if (!list || item->priority < list->priority)
{
LIST_PREPEND (list, item);
return list;
}
// Otherwise fast-forward to the last entry that precedes us
struct hook *before = list;
while (before->next && before->next->priority < item->priority)
before = before->next;
// And link ourselves in between it and its successor
if ((item->next = before->next))
item->next->prev = item;
before->next = item;
item->prev = before;
return list;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct input_hook
{
struct hook super; ///< Common hook fields
struct input_hook_vtable *vtable; ///< Methods
};
struct input_hook_vtable
{
/// Takes over the ownership of "input", returns either NULL if input
/// was thrown away, or a possibly modified version of it
char *(*filter) (struct input_hook *self,
struct buffer *buffer, char *input);
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct irc_hook
{
struct hook super; ///< Common hook fields
struct irc_hook_vtable *vtable; ///< Methods
};
struct irc_hook_vtable
{
/// Takes over the ownership of "message", returns either NULL if message
/// was thrown away, or a possibly modified version of it
char *(*filter) (struct irc_hook *self,
struct server *server, char *message);
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct completion_word
{
size_t start; ///< Offset to start of word
size_t end; ///< Offset to end of word
};
struct completion
{
char *line; ///< The line which is being completed
struct completion_word *words; ///< Word locations
size_t words_len; ///< Number of words
size_t words_alloc; ///< Number of words allocated
size_t location; ///< Which word is being completed
};
struct completion_hook
{
struct hook super; ///< Common hook fields
struct completion_hook_vtable *vtable;
};
struct completion_hook_vtable
{
/// Tries to add possible completions of "word" to "output"
void (*complete) (struct completion_hook *self,
struct completion *data, const char *word, struct str_vector *output);
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct app_context
{
bool no_colors; ///< Disable attribute printing
char *attrs_defaults[ATTR_COUNT]; ///< Default terminal attributes
// Configuration:
struct config config; ///< Program configuration
char *attrs[ATTR_COUNT]; ///< Terminal attributes
bool isolate_buffers; ///< Isolate global/server buffers
bool beep_on_highlight; ///< Beep on highlight
bool logging; ///< Logging to file enabled
bool show_all_prefixes; ///< Show all prefixes before nicks
struct str_map servers; ///< Our servers
// Events:
struct poller_fd tty_event; ///< Terminal input event
struct poller_fd signal_event; ///< Signal FD event
struct poller_timer flush_timer; ///< Flush all open files (e.g. logs)
struct poller_timer date_chg_tmr; ///< Print a date change
struct poller_timer autoaway_tmr; ///< Autoaway timer
struct poller poller; ///< Manages polled descriptors
bool quitting; ///< User requested quitting
bool polling; ///< The event loop is running
// Buffers:
struct buffer *buffers; ///< All our buffers in order
struct buffer *buffers_tail; ///< The tail of our buffers
struct buffer *global_buffer; ///< The global buffer
struct buffer *current_buffer; ///< The current buffer
struct buffer *last_buffer; ///< Last used buffer
// TODO: make buffer names fully unique like weechat does
struct str_map buffers_by_name; ///< Buffers by name
unsigned backlog_limit; ///< Limit for buffer lines
time_t last_displayed_msg_time; ///< Time of last displayed message
// Terminal:
iconv_t term_to_utf8; ///< Terminal encoding to UTF-8
iconv_t term_from_utf8; ///< UTF-8 to terminal encoding
iconv_t latin1_to_utf8; ///< ISO Latin 1 to UTF-8
struct input input; ///< User interface
struct poller_idle input_event; ///< Pending input event
struct str_vector pending_input; ///< Pending input lines
int *nick_palette; ///< A 256-color palette for nicknames
size_t nick_palette_len; ///< Number of entries in nick_palette
bool awaiting_mirc_escape; ///< Awaiting a mIRC attribute escape
bool in_bracketed_paste; ///< User is pasting some content
struct str input_buffer; ///< Buffered pasted content
bool running_backlog_helper; ///< Running a backlog helper
bool running_editor; ///< Running editor for the input
char *editor_filename; ///< The file being edited by user
int terminal_suspended; ///< Terminal suspension level
struct plugin *plugins; ///< Loaded plugins
struct hook *input_hooks; ///< Input hooks
struct hook *irc_hooks; ///< IRC hooks
struct hook *completion_hooks; ///< Autocomplete hooks
}
*g_ctx;
static int *
filter_color_cube_for_acceptable_nick_colors (size_t *len)
{
// This is a pure function and we don't use threads, static storage is fine
static int table[6 * 6 * 6];
size_t len_counter = 0;
for (int x = 0; x < 6 * 6 * 6; x++)
{
int r = x / 36;
int g = (x / 6) % 6;
int b = (x % 6);
// Use the luma value of colours within the cube to filter colours that
// look okay-ish on terminals with both black and white backgrounds
double luma = 0.2126 * r / 6. + 0.7152 * g / 6. + 0.0722 * b / 6.;
if (luma >= .3 && luma <= .5)
table[len_counter++] = 16 + x;
}
*len = len_counter;
return table;
}
static void
app_context_init (struct app_context *self)
{
memset (self, 0, sizeof *self);
config_init (&self->config);
poller_init (&self->poller);
str_map_init (&self->servers);
self->servers.free = (str_map_free_fn) server_unref;
self->servers.key_xfrm = tolower_ascii_strxfrm;
str_map_init (&self->buffers_by_name);
self->buffers_by_name.key_xfrm = tolower_ascii_strxfrm;
// So that we don't lose the logo shortly after startup
self->backlog_limit = 1000;
self->last_displayed_msg_time = time (NULL);
char *encoding = nl_langinfo (CODESET);
// FIXME: put a check for "//TRANSLIT" in CMakeLists.txt
#ifdef __linux__
encoding = xstrdup_printf ("%s//TRANSLIT", encoding);
#else // ! __linux__
encoding = xstrdup (encoding);
#endif // ! __linux__
if ((self->term_from_utf8 =
iconv_open (encoding, "UTF-8")) == (iconv_t) -1
|| (self->latin1_to_utf8 =
iconv_open ("UTF-8", "ISO-8859-1")) == (iconv_t) -1
|| (self->term_to_utf8 =
iconv_open ("UTF-8", nl_langinfo (CODESET))) == (iconv_t) -1)
exit_fatal ("creating the UTF-8 conversion object failed: %s",
strerror (errno));
free (encoding);
input_init (&self->input);
str_vector_init (&self->pending_input);
str_init (&self->input_buffer);
self->nick_palette =
filter_color_cube_for_acceptable_nick_colors (&self->nick_palette_len);
}
static void
app_context_free (struct app_context *self)
{
// Plugins can try to use of the other fields when destroyed
LIST_FOR_EACH (struct plugin, iter, self->plugins)
plugin_destroy (iter);
config_free (&self->config);
for (size_t i = 0; i < ATTR_COUNT; i++)
{
free (self->attrs_defaults[i]);
free (self->attrs[i]);
}
LIST_FOR_EACH (struct buffer, iter, self->buffers)
{
#ifdef HAVE_READLINE
input_destroy_buffer (&self->input, iter->input_data);
iter->input_data = NULL;
#endif // HAVE_READLINE
buffer_unref (iter);
}
str_map_free (&self->buffers_by_name);
str_map_free (&self->servers);
poller_free (&self->poller);
iconv_close (self->latin1_to_utf8);
iconv_close (self->term_from_utf8);
iconv_close (self->term_to_utf8);
input_free (&self->input);
str_vector_free (&self->pending_input);
str_free (&self->input_buffer);
free (self->editor_filename);
}
static void refresh_prompt (struct app_context *ctx);
// --- Configuration -----------------------------------------------------------
static void
on_config_debug_mode_change (struct config_item *item)
{
g_debug_mode = item->value.boolean;
}
static void
on_config_show_all_prefixes_change (struct config_item *item)
{
struct app_context *ctx = item->user_data;
ctx->show_all_prefixes = item->value.boolean;
refresh_prompt (ctx);
}
static void on_config_backlog_limit_change (struct config_item *item);
static void on_config_attribute_change (struct config_item *item);
static void on_config_logging_change (struct config_item *item);
#define TRIVIAL_BOOLEAN_ON_CHANGE(name) \
static void \
on_config_ ## name ## _change (struct config_item *item) \
{ \
struct app_context *ctx = item->user_data; \
ctx->name = item->value.boolean; \
}
TRIVIAL_BOOLEAN_ON_CHANGE (isolate_buffers)
TRIVIAL_BOOLEAN_ON_CHANGE (beep_on_highlight)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static bool
config_validate_nonjunk_string
(const struct config_item *item, struct error **e)
{
if (item->type == CONFIG_ITEM_NULL)
return true;
hard_assert (config_item_type_is_string (item->type));
for (size_t i = 0; i < item->value.string.len; i++)
{
// Not even a tabulator
unsigned char c = item->value.string.str[i];
if (c < 32)
{
error_set (e, "control characters are not allowed");
return false;
}
}
return true;
}
static bool
config_validate_addresses
(const struct config_item *item, struct error **e)
{
if (item->type == CONFIG_ITEM_NULL)
return true;
if (!config_validate_nonjunk_string (item, e))
return false;
// Comma-separated list of "host[:port]" pairs
regex_t re;
int err = regcomp (&re, "^([^/:,]+(:[^/:,]+)?)?"
"(,([^/:,]+(:[^/:,]+)?)?)*$", REG_EXTENDED | REG_NOSUB);
hard_assert (!err);
bool result = !regexec (&re, item->value.string.str, 0, NULL, 0);
if (!result)
error_set (e, "invalid address list string");
regfree (&re);
return result;
}
static bool
config_validate_nonnegative
(const struct config_item *item, struct error **e)
{
if (item->type == CONFIG_ITEM_NULL)
return true;
hard_assert (item->type == CONFIG_ITEM_INTEGER);
if (item->value.integer >= 0)
return true;
error_set (e, "must be non-negative");
return false;
}
static struct config_schema g_config_server[] =
{
{ .name = "nicks",
.comment = "IRC nickname",
.type = CONFIG_ITEM_STRING_ARRAY,
.validate = config_validate_nonjunk_string },
{ .name = "username",
.comment = "IRC user name",
.type = CONFIG_ITEM_STRING,
.validate = config_validate_nonjunk_string },
{ .name = "realname",
.comment = "IRC real name/e-mail",
.type = CONFIG_ITEM_STRING,
.validate = config_validate_nonjunk_string },
{ .name = "addresses",
.comment = "Addresses of the IRC network (e.g. \"irc.net:6667\")",
.type = CONFIG_ITEM_STRING_ARRAY,
.validate = config_validate_addresses },
{ .name = "password",
.comment = "Password to connect to the server, if any",
.type = CONFIG_ITEM_STRING,
.validate = config_validate_nonjunk_string },
// XXX: if we add support for new capabilities, the value stays unchanged
{ .name = "capabilities",
.comment = "Capabilities to use if supported by server",
.type = CONFIG_ITEM_STRING_ARRAY,
.validate = config_validate_nonjunk_string,
.default_ = "\"multi-prefix,invite-notify,server-time,echo-message\"" },
{ .name = "tls",
.comment = "Whether to use TLS",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "off" },
{ .name = "tls_cert",
.comment = "Client TLS certificate (PEM)",
.type = CONFIG_ITEM_STRING },
{ .name = "tls_verify",
.comment = "Whether to verify certificates",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "on" },
{ .name = "tls_ca_file",
.comment = "OpenSSL CA bundle file",
.type = CONFIG_ITEM_STRING },
{ .name = "tls_ca_path",
.comment = "OpenSSL CA bundle path",
.type = CONFIG_ITEM_STRING },
{ .name = "tls_ciphers",
.comment = "OpenSSL cipher preference list",
.type = CONFIG_ITEM_STRING,
.default_ = "\"DEFAULT:!MEDIUM:!LOW\"" },
{ .name = "autoconnect",
.comment = "Connect automatically on startup",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "on" },
{ .name = "autojoin",
.comment = "Channels to join on start",
.type = CONFIG_ITEM_STRING_ARRAY,
.validate = config_validate_nonjunk_string },
{ .name = "command",
.comment = "Command to execute after a successful connect",
.type = CONFIG_ITEM_STRING },
{ .name = "command_delay",
.comment = "Delay between executing \"command\" and joining channels",
.type = CONFIG_ITEM_INTEGER,
.validate = config_validate_nonnegative,
.default_ = "0" },
{ .name = "reconnect",
.comment = "Whether to reconnect on error",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "on" },
{ .name = "reconnect_delay",
.comment = "Time between reconnecting",
.type = CONFIG_ITEM_INTEGER,
.validate = config_validate_nonnegative,
.default_ = "5" },
{ .name = "socks_host",
.comment = "Address of a SOCKS 4a/5 proxy",
.type = CONFIG_ITEM_STRING,
.validate = config_validate_nonjunk_string },
{ .name = "socks_port",
.comment = "SOCKS port number",
.type = CONFIG_ITEM_INTEGER,
.validate = config_validate_nonnegative,
.default_ = "1080" },
{ .name = "socks_username",
.comment = "SOCKS auth. username",
.type = CONFIG_ITEM_STRING },
{ .name = "socks_password",
.comment = "SOCKS auth. password",
.type = CONFIG_ITEM_STRING },
{}
};
static struct config_schema g_config_behaviour[] =
{
{ .name = "isolate_buffers",
.comment = "Don't leak messages from the server and global buffers",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "off",
.on_change = on_config_isolate_buffers_change },
{ .name = "beep_on_highlight",
.comment = "Beep when highlighted or on a new invisible PM",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "on",
.on_change = on_config_beep_on_highlight_change },
{ .name = "show_all_prefixes",
.comment = "Show all prefixes in front of nicknames",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "off",
.on_change = on_config_show_all_prefixes_change },
{ .name = "logging",
.comment = "Log buffer contents to file",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "off",
.on_change = on_config_logging_change },
{ .name = "save_on_quit",
.comment = "Save configuration before quitting",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "on" },
{ .name = "debug_mode",
.comment = "Produce some debugging output",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "off",
.on_change = on_config_debug_mode_change },
// GNU screen has an ^O in its formatting attributes reset string,
// therefore we can't just pipe raw formatting to `less -R`.
// You can use the -r switch, however that makes `less` very confused
// about line wrapping, and the result is suboptimal.
{ .name = "backlog_limit",
.comment = "Maximum number of lines stored in the backlog",
.type = CONFIG_ITEM_INTEGER,
.validate = config_validate_nonnegative,
.default_ = "1000",
.on_change = on_config_backlog_limit_change },
{ .name = "backlog_helper",
.comment = "Shell command to display a buffer's history",
.type = CONFIG_ITEM_STRING,
.default_ = "\"LESSSECURE=1 less -M -R +G\"" },
{ .name = "backlog_helper_strip_formatting",
.comment = "Strip formatting from backlog helper input",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "on" },
{ .name = "reconnect_delay_growing",
.comment = "Growing factor for reconnect delay",
.type = CONFIG_ITEM_INTEGER,
.validate = config_validate_nonnegative,
.default_ = "2" },
{ .name = "reconnect_delay_max",
.comment = "Maximum reconnect delay in seconds",
.type = CONFIG_ITEM_INTEGER,
.validate = config_validate_nonnegative,
.default_ = "600" },
{ .name = "autoaway_message",
.comment = "Automated away message",
.type = CONFIG_ITEM_STRING,
.default_ = "\"I'm not here right now\"" },
{ .name = "autoaway_delay",
.comment = "Delay from the last keypress in seconds",
.type = CONFIG_ITEM_INTEGER,
.validate = config_validate_nonnegative,
.default_ = "1800" },
{ .name = "plugin_autoload",
.comment = "Plugins to automatically load on start",
.type = CONFIG_ITEM_STRING_ARRAY,
.validate = config_validate_nonjunk_string },
{}
};
static struct config_schema g_config_attributes[] =
{
#define XX(x, y, z) { .name = y, .comment = z, .type = CONFIG_ITEM_STRING, \
.on_change = on_config_attribute_change },
ATTR_TABLE (XX)
#undef XX
{}
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
load_config_behaviour (struct config_item *subtree, void *user_data)
{
config_schema_apply_to_object (g_config_behaviour, subtree, user_data);
}
static void
load_config_attributes (struct config_item *subtree, void *user_data)
{
config_schema_apply_to_object (g_config_attributes, subtree, user_data);
}
static void
register_config_modules (struct app_context *ctx)
{
struct config *config = &ctx->config;
// The servers are loaded later when we can create buffers for them
config_register_module (config, "servers", NULL, NULL);
config_register_module (config, "aliases", NULL, NULL);
config_register_module (config, "plugins", NULL, NULL);
config_register_module (config, "behaviour", load_config_behaviour, ctx);
config_register_module (config, "attributes", load_config_attributes, ctx);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static const char *
get_config_string (struct config_item *root, const char *key)
{
struct config_item *item = config_item_get (root, key, NULL);
hard_assert (item);
if (item->type == CONFIG_ITEM_NULL)
return NULL;
hard_assert (config_item_type_is_string (item->type));
return item->value.string.str;
}
static bool
set_config_string
(struct config_item *root, const char *key, const char *value)
{
struct config_item *item = config_item_get (root, key, NULL);
hard_assert (item);
struct config_item *new_ = config_item_string_from_cstr (value);
struct error *e = NULL;
if (config_item_set_from (item, new_, &e))
return true;
config_item_destroy (new_);
print_error ("couldn't set `%s' in configuration: %s", key, e->message);
error_free (e);
return false;
}
static int64_t
get_config_integer (struct config_item *root, const char *key)
{
struct config_item *item = config_item_get (root, key, NULL);
hard_assert (item && item->type == CONFIG_ITEM_INTEGER);
return item->value.integer;
}
static bool
get_config_boolean (struct config_item *root, const char *key)
{
struct config_item *item = config_item_get (root, key, NULL);
hard_assert (item && item->type == CONFIG_ITEM_BOOLEAN);
return item->value.boolean;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static struct str_map *
get_servers_config (struct app_context *ctx)
{
return &config_item_get (ctx->config.root, "servers", NULL)->value.object;
}
static struct str_map *
get_aliases_config (struct app_context *ctx)
{
return &config_item_get (ctx->config.root, "aliases", NULL)->value.object;
}
static struct str_map *
get_plugins_config (struct app_context *ctx)
{
return &config_item_get (ctx->config.root, "plugins", NULL)->value.object;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
serialize_configuration (struct config_item *root, struct str *output)
{
str_append (output,
"# " PROGRAM_NAME " " PROGRAM_VERSION " configuration file\n"
"#\n"
"# Relative paths are searched for in ${XDG_CONFIG_HOME:-~/.config}\n"
"# /" PROGRAM_NAME " as well as in $XDG_CONFIG_DIRS/" PROGRAM_NAME "\n"
"#\n"
"# Everything is in UTF-8. Any custom comments will be overwritten.\n"
"\n");
config_item_write (root, true, output);
}
// --- Terminal output ---------------------------------------------------------
/// Default color pair
#define COLOR_DEFAULT -1
/// Bright versions of the basic color set
#define COLOR_BRIGHT(x) (COLOR_ ## x + 8)
/// Builds a color pair for 256-color terminals with a 16-color backup value
#define COLOR_256(name, c256) \
(((COLOR_ ## name) & 0xFFFF) | (((c256) & 0xFFFF) << 16))
static struct
{
bool initialized; ///< Terminal is available
bool stdout_is_tty; ///< `stdout' is a terminal
bool stderr_is_tty; ///< `stderr' is a terminal
char *color_set_fg[256]; ///< Codes to set the foreground colour
char *color_set_bg[256]; ///< Codes to set the background colour
int lines; ///< Number of lines
int columns; ///< Number of columns
}
g_terminal;
static void
update_screen_size (void)
{
#ifdef TIOCGWINSZ
if (!g_terminal.stdout_is_tty)
return;
struct winsize size;
if (!ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &size))
{
char *row = getenv ("LINES");
char *col = getenv ("COLUMNS");
unsigned long tmp;
g_terminal.lines =
(row && xstrtoul (&tmp, row, 10)) ? tmp : size.ws_row;
g_terminal.columns =
(col && xstrtoul (&tmp, col, 10)) ? tmp : size.ws_col;
}
#endif // TIOCGWINSZ
}
static bool
init_terminal (void)
{
int tty_fd = -1;
if ((g_terminal.stderr_is_tty = isatty (STDERR_FILENO)))
tty_fd = STDERR_FILENO;
if ((g_terminal.stdout_is_tty = isatty (STDOUT_FILENO)))
tty_fd = STDOUT_FILENO;
int err;
if (tty_fd == -1 || setupterm (NULL, tty_fd, &err) == ERR)
return false;
// Make sure all terminal features used by us are supported
if (!set_a_foreground || !set_a_background
|| !enter_bold_mode || !exit_attribute_mode)
{
del_curterm (cur_term);
return false;
}
// Make sure newlines are output correctly
struct termios termios;
if (!tcgetattr (tty_fd, &termios))
{
termios.c_oflag |= ONLCR;
(void) tcsetattr (tty_fd, TCSADRAIN, &termios);
}
g_terminal.lines = tigetnum ("lines");
g_terminal.columns = tigetnum ("cols");
update_screen_size ();
int max = MIN (256, max_colors);
for (int i = 0; i < max; i++)
{
g_terminal.color_set_fg[i] = xstrdup (tparm (set_a_foreground,
i, 0, 0, 0, 0, 0, 0, 0, 0));
g_terminal.color_set_bg[i] = xstrdup (tparm (set_a_background,
i, 0, 0, 0, 0, 0, 0, 0, 0));
}
return g_terminal.initialized = true;
}
static void
free_terminal (void)
{
if (!g_terminal.initialized)
return;
for (int i = 0; i < 256; i++)
{
free (g_terminal.color_set_fg[i]);
free (g_terminal.color_set_bg[i]);
}
del_curterm (cur_term);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
typedef int (*terminal_printer_fn) (int);
static int
putchar_stderr (int c)
{
return fputc (c, stderr);
}
static terminal_printer_fn
get_attribute_printer (FILE *stream)
{
if (stream == stdout && g_terminal.stdout_is_tty)
return putchar;
if (stream == stderr && g_terminal.stderr_is_tty)
return putchar_stderr;
return NULL;
}
static void
vprint_attributed (struct app_context *ctx,
FILE *stream, intptr_t attribute, const char *fmt, va_list ap)
{
terminal_printer_fn printer = get_attribute_printer (stream);
if (!attribute)
printer = NULL;
if (printer)
tputs (ctx->attrs[attribute], 1, printer);
vfprintf (stream, fmt, ap);
if (printer)
tputs (ctx->attrs[ATTR_RESET], 1, printer);
}
static void
print_attributed (struct app_context *ctx,
FILE *stream, intptr_t attribute, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vprint_attributed (ctx, stream, attribute, fmt, ap);
va_end (ap);
}
static void
log_message_attributed (void *user_data, const char *quote, const char *fmt,
va_list ap)
{
FILE *stream = stderr;
struct app_context *ctx = g_ctx;
input_hide (&ctx->input);
print_attributed (ctx, stream, (intptr_t) user_data, "%s", quote);
vprint_attributed (ctx, stream, (intptr_t) user_data, fmt, ap);
fputs ("\n", stream);
input_show (&ctx->input);
}
static ssize_t
attr_by_name (const char *name)
{
static const char *table[ATTR_COUNT] =
{
#define XX(x, y, z) [ATTR_ ## x] = y,
ATTR_TABLE (XX)
#undef XX
};
for (size_t i = 0; i < N_ELEMENTS (table); i++)
if (!strcmp (name, table[i]))
return i;
return -1;
}
static void
on_config_attribute_change (struct config_item *item)
{
struct app_context *ctx = item->user_data;
ssize_t id = attr_by_name (item->schema->name);
if (id != -1)
{
free (ctx->attrs[id]);
ctx->attrs[id] = xstrdup (item->type == CONFIG_ITEM_NULL
? ctx->attrs_defaults[id]
: item->value.string.str);
}
}
static void
init_colors (struct app_context *ctx)
{
bool have_ti = init_terminal ();
char **defaults = ctx->attrs_defaults;
#define INIT_ATTR(id, ti) defaults[ATTR_ ## id] = xstrdup (have_ti ? (ti) : "")
INIT_ATTR (PROMPT, enter_bold_mode);
INIT_ATTR (RESET, exit_attribute_mode);
INIT_ATTR (READ_MARKER, g_terminal.color_set_fg[COLOR_MAGENTA]);
INIT_ATTR (WARNING, g_terminal.color_set_fg[COLOR_YELLOW]);
INIT_ATTR (ERROR, g_terminal.color_set_fg[COLOR_RED]);
INIT_ATTR (EXTERNAL, g_terminal.color_set_fg[COLOR_WHITE]);
INIT_ATTR (TIMESTAMP, g_terminal.color_set_fg[COLOR_WHITE]);
INIT_ATTR (ACTION, g_terminal.color_set_fg[COLOR_RED]);
INIT_ATTR (USERHOST, g_terminal.color_set_fg[COLOR_CYAN]);
INIT_ATTR (JOIN, g_terminal.color_set_fg[COLOR_GREEN]);
INIT_ATTR (PART, g_terminal.color_set_fg[COLOR_RED]);
char *highlight = xstrdup_printf ("%s%s%s",
g_terminal.color_set_fg[COLOR_YELLOW],
g_terminal.color_set_bg[COLOR_MAGENTA],
enter_bold_mode);
INIT_ATTR (HIGHLIGHT, highlight);
free (highlight);
#undef INIT_ATTR
if (ctx->no_colors)
{
g_terminal.stdout_is_tty = false;
g_terminal.stderr_is_tty = false;
}
g_log_message_real = log_message_attributed;
// Apply the default values so that we start with any formatting at all
config_schema_call_changed
(config_item_get (ctx->config.root, "attributes", NULL));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// A little tool that tries to make the most of the terminal's capabilities
// to set up text attributes. It mostly targets just terminal emulators as that
// is what people are using these days. At least no stupid ncurses limits us
// with color pairs.
enum
{
ATTRIBUTE_BOLD = 1 << 0,
ATTRIBUTE_ITALIC = 1 << 1,
ATTRIBUTE_UNDERLINE = 1 << 2,
ATTRIBUTE_INVERSE = 1 << 3,
ATTRIBUTE_BLINK = 1 << 4
};
struct attribute_printer
{
struct app_context *ctx; ///< Application context
FILE *stream; ///< Output stream
bool dirty; ///< Attributes are set
int want; ///< Desired attributes
int want_foreground; ///< Desired foreground color
int want_background; ///< Desired background color
};
static void
attribute_printer_tputs (struct attribute_printer *self, const char *attr)
{
terminal_printer_fn printer = get_attribute_printer (self->stream);
if (printer)
tputs (attr, 1, printer);
else
// We shouldn't really do this but we need it to
// output formatting to the backlog
fputs (attr, self->stream);
}
static void
attribute_printer_reset (struct attribute_printer *self)
{
if (self->dirty)
attribute_printer_tputs (self, self->ctx->attrs[ATTR_RESET]);
self->dirty = false;
}
static void
attribute_printer_init (struct attribute_printer *self,
struct app_context *ctx, FILE *stream)
{
self->ctx = ctx;
self->stream = stream;
self->dirty = true;
self->want = 0;
self->want_foreground = -1;
self->want_background = -1;
}
static void
attribute_printer_apply (struct attribute_printer *self, int attribute)
{
attribute_printer_reset (self);
if (attribute != ATTR_RESET)
{
attribute_printer_tputs (self, self->ctx->attrs[attribute]);
self->dirty = true;
}
}
// NOTE: commonly terminals have:
// 8 colors (worst, bright fg with BOLD, bg sometimes with BLINK)
// 16 colors (okayish, we have the full basic range guaranteed)
// 88 colors (the same plus a 4^3 RGB cube and a few shades of gray)
// 256 colors (best, like above but with a larger cube and more gray)
/// Interpolate from the 256-color palette to the 88-color one
static int
attribute_printer_256_to_88 (int color)
{
// These colours are the same everywhere
if (color < 16)
return color;
// 24 -> 8 extra shades of gray
if (color >= 232)
return 80 + (color - 232) / 3;
// 6 * 6 * 6 cube -> 4 * 4 * 4 cube
int x[6] = { 0, 1, 1, 2, 2, 3 };
int index = color - 16;
return 16 +
( x[ index / 36 ] << 8
| x[(index / 6) % 6 ] << 4
| x[(index % 6) ] );
}
static int
attribute_printer_decode_color (int color, bool *is_bright)
{
int16_t c16 = color; hard_assert (c16 < 16);
int16_t c256 = color >> 16; hard_assert (c256 < 256);
*is_bright = false;
switch (max_colors)
{
case 8:
if (c16 >= 8)
{
c16 -= 8;
*is_bright = true;
}
case 16:
return c16;
case 88:
return c256 <= 0 ? c16 : attribute_printer_256_to_88 (c256);
case 256:
return c256 <= 0 ? c16 : c256;
default:
// Unsupported palette
return -1;
}
}
static void
attribute_printer_update (struct attribute_printer *self)
{
bool fg_is_bright;
int fg = attribute_printer_decode_color
(self->want_foreground, &fg_is_bright);
bool bg_is_bright;
int bg = attribute_printer_decode_color
(self->want_background, &bg_is_bright);
int attributes = self->want;
bool have_inverse = !!(attributes & ATTRIBUTE_INVERSE);
if (have_inverse)
{
bool tmp = fg_is_bright;
fg_is_bright = bg_is_bright;
bg_is_bright = tmp;
}
// In 8 colour mode, some terminals don't support bright backgrounds.
// However, we can make use of the fact that the brightness change caused
// by the bold attribute is retained when inverting the colours.
// This has the downside of making the text bold when it's not supposed
// to be, and we still can't make both colours bright, so it's more of
// an interesting hack rather than anything else.
if (!fg_is_bright && bg_is_bright && have_inverse)
attributes |= ATTRIBUTE_BOLD;
else if (!fg_is_bright && bg_is_bright
&& !have_inverse && fg >= 0 && bg >= 0)
{
// As long as none of the colours is the default, we can swap them
int tmp = fg; fg = bg; bg = tmp;
attributes |= ATTRIBUTE_BOLD | ATTRIBUTE_INVERSE;
}
else
{
// This is what works on normal, decent terminals
if (fg_is_bright) attributes |= ATTRIBUTE_BOLD;
if (bg_is_bright) attributes |= ATTRIBUTE_BLINK;
}
attribute_printer_reset (self);
if (attributes)
attribute_printer_tputs (self, tparm (set_attributes,
0, // standout
attributes & ATTRIBUTE_UNDERLINE,
attributes & ATTRIBUTE_INVERSE,
attributes & ATTRIBUTE_BLINK,
0, // dim
attributes & ATTRIBUTE_BOLD,
0, // blank
0, // protect
0)); // acs
if (enter_italics_mode && (attributes & ATTRIBUTE_ITALIC))
attribute_printer_tputs (self, enter_italics_mode);
if (fg >= 0)
attribute_printer_tputs (self, g_terminal.color_set_fg[fg]);
if (bg >= 0)
attribute_printer_tputs (self, g_terminal.color_set_bg[bg]);
self->dirty = true;
}
// --- Helpers -----------------------------------------------------------------
static int
irc_server_strcmp (struct server *s, const char *a, const char *b)
{
int x;
while (*a || *b)
if ((x = s->irc_tolower (*a++) - s->irc_tolower (*b++)))
return x;
return 0;
}
static int
irc_server_strncmp (struct server *s, const char *a, const char *b, size_t n)
{
int x;
while (n-- && (*a || *b))
if ((x = s->irc_tolower (*a++) - s->irc_tolower (*b++)))
return x;
return 0;
}
static char *
irc_cut_nickname (const char *prefix)
{
return cstr_cut_until (prefix, "!@");
}
static const char *
irc_find_userhost (const char *prefix)
{
const char *p = strchr (prefix, '!');
return p ? p + 1 : NULL;
}
static bool
irc_is_this_us (struct server *s, const char *prefix)
{
// This shouldn't be called before successfully registering.
// Better safe than sorry, though.
if (!s->irc_user)
return false;
char *nick = irc_cut_nickname (prefix);
bool result = !irc_server_strcmp (s, nick, s->irc_user->nickname);
free (nick);
return result;
}
static bool
irc_is_channel (struct server *s, const char *ident)
{
return *ident
&& (!!strchr (s->irc_chantypes, *ident) ||
!!strchr (s->irc_idchan_prefixes, *ident));
}
// Message targets can be prefixed by a character filtering their targets
static const char *
irc_skip_statusmsg (struct server *s, const char *target)
{
return target + (*target && strchr (s->irc_statusmsg, *target));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// As of 2015, everything should be in UTF-8. And if it's not, we'll decode it
// as ISO Latin 1. This function should not be called on the whole message.
static char *
irc_to_utf8 (struct app_context *ctx, const char *text)
{
if (!text)
return NULL;
size_t len = strlen (text) + 1;
if (utf8_validate (text, len))
return xstrdup (text);
return iconv_xstrdup (ctx->latin1_to_utf8, (char *) text, len, NULL);
}
// This function is used to output debugging IRC traffic to the terminal.
// It's far from ideal, as any non-UTF-8 text degrades the entire line to
// ISO Latin 1. But it should work good enough most of the time.
static char *
irc_to_term (struct app_context *ctx, const char *text)
{
char *utf8 = irc_to_utf8 (ctx, text);
char *term = iconv_xstrdup (ctx->term_from_utf8, utf8, -1, NULL);
free (utf8);
return term;
}
// --- Output formatter --------------------------------------------------------
// This complicated piece of code makes attributed text formatting simple.
// We use a printf-inspired syntax to push attributes and text to the object,
// then flush it either to a terminal, or a log file with formatting stripped.
//
// Format strings use a #-quoted notation, to differentiate from printf:
// #s inserts a string (expected to be in UTF-8)
// #d inserts a signed integer
// #l inserts a locale-encoded string
//
// #S inserts a string from the server with unknown encoding
// #m inserts a mIRC-formatted string (auto-resets at boundaries)
// #n cuts the nickname from a string and automatically colours it
// #N is like #n but also appends userhost, if present
//
// #a inserts named attributes (auto-resets)
// #r resets terminal attributes
// #c sets foreground color
// #C sets background color
//
// Modifiers:
// & free() the string argument after using it
static void
formatter_add_item (struct formatter *self, struct formatter_item template_)
{
if (template_.text)
template_.text = xstrdup (template_.text);
if (self->items_len == self->items_alloc)
self->items = xreallocarray
(self->items, sizeof *self->items, (self->items_alloc <<= 1));
self->items[self->items_len++] = template_;
}
#define FORMATTER_ADD_ITEM(self, type_, ...) formatter_add_item ((self), \
(struct formatter_item) { .type = FORMATTER_ITEM_ ## type_, __VA_ARGS__ })
#define FORMATTER_ADD_RESET(self) \
FORMATTER_ADD_ITEM ((self), ATTR, .attribute = ATTR_RESET)
#define FORMATTER_ADD_TEXT(self, text_) \
FORMATTER_ADD_ITEM ((self), TEXT, .text = (text_))
#define FORMATTER_ADD_SIMPLE(self, attribute_) \
FORMATTER_ADD_ITEM ((self), SIMPLE, .attribute = ATTRIBUTE_ ## attribute_)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
enum
{
MIRC_WHITE, MIRC_BLACK, MIRC_BLUE, MIRC_GREEN,
MIRC_L_RED, MIRC_RED, MIRC_PURPLE, MIRC_ORANGE,
MIRC_YELLOW, MIRC_L_GREEN, MIRC_CYAN, MIRC_L_CYAN,
MIRC_L_BLUE, MIRC_L_PURPLE, MIRC_GRAY, MIRC_L_GRAY,
};
// We use estimates from the 16 color terminal palette, or the 256 color cube,
// which is not always available. The mIRC orange colour is only in the cube.
static const int g_mirc_to_terminal[] =
{
[MIRC_WHITE] = COLOR_256 (BRIGHT (WHITE), 231),
[MIRC_BLACK] = COLOR_256 (BLACK, 16),
[MIRC_BLUE] = COLOR_256 (BLUE, 19),
[MIRC_GREEN] = COLOR_256 (GREEN, 34),
[MIRC_L_RED] = COLOR_256 (BRIGHT (RED), 196),
[MIRC_RED] = COLOR_256 (RED, 124),
[MIRC_PURPLE] = COLOR_256 (MAGENTA, 127),
[MIRC_ORANGE] = COLOR_256 (BRIGHT (YELLOW), 214),
[MIRC_YELLOW] = COLOR_256 (BRIGHT (YELLOW), 226),
[MIRC_L_GREEN] = COLOR_256 (BRIGHT (GREEN), 46),
[MIRC_CYAN] = COLOR_256 (CYAN, 37),
[MIRC_L_CYAN] = COLOR_256 (BRIGHT (CYAN), 51),
[MIRC_L_BLUE] = COLOR_256 (BRIGHT (BLUE), 21),
[MIRC_L_PURPLE] = COLOR_256 (BRIGHT (MAGENTA),201),
[MIRC_GRAY] = COLOR_256 (BRIGHT (BLACK), 244),
[MIRC_L_GRAY] = COLOR_256 (WHITE, 252),
};
static const char *
formatter_parse_mirc_color (struct formatter *self, const char *s)
{
if (!isdigit_ascii (*s))
return s;
int fg = *s++ - '0';
if (isdigit_ascii (*s))
fg = fg * 10 + (*s++ - '0');
if (fg >= 0 && fg < 16)
FORMATTER_ADD_ITEM (self, FG_COLOR, .color = g_mirc_to_terminal[fg]);
if (*s != ',' || !isdigit_ascii (s[1]))
return s;
s++;
int bg = *s++ - '0';
if (isdigit_ascii (*s))
bg = bg * 10 + (*s++ - '0');
<