2015-04-12 04:52:39 +02:00
|
|
|
/*
|
|
|
|
* degesch.c: the experimental IRC client
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015, Přemysl Janouch <p.janouch@gmail.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// Some arbitrary limit for the history file
|
|
|
|
#define HISTORY_LIMIT 10000
|
|
|
|
|
|
|
|
// String constants for all attributes we use for output
|
|
|
|
#define ATTR_PROMPT "attr_prompt"
|
|
|
|
#define ATTR_RESET "attr_reset"
|
|
|
|
#define ATTR_WARNING "attr_warning"
|
|
|
|
#define ATTR_ERROR "attr_error"
|
|
|
|
|
2015-04-26 18:39:38 +02:00
|
|
|
#define ATTR_EXTERNAL "attr_external"
|
2015-04-26 18:23:43 +02:00
|
|
|
#define ATTR_TIMESTAMP "attr_timestamp"
|
2015-04-27 22:40:33 +02:00
|
|
|
#define ATTR_HIGHLIGHT "attr_highlight"
|
2015-04-26 18:23:43 +02:00
|
|
|
#define ATTR_ACTION "attr_action"
|
|
|
|
#define ATTR_JOIN "attr_join"
|
|
|
|
#define ATTR_PART "attr_part"
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
// User data for logger functions to enable formatted logging
|
|
|
|
#define print_fatal_data ATTR_ERROR
|
|
|
|
#define print_error_data ATTR_ERROR
|
|
|
|
#define print_warning_data ATTR_WARNING
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#undef PROGRAM_NAME
|
|
|
|
#define PROGRAM_NAME "degesch"
|
|
|
|
|
|
|
|
#include "common.c"
|
2015-04-21 22:08:18 +02:00
|
|
|
#include "kike-replies.c"
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
#include <langinfo.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <pwd.h>
|
2015-04-27 01:36:33 +02:00
|
|
|
#include <sys/utsname.h>
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
#include <curses.h>
|
|
|
|
#include <term.h>
|
2015-04-13 00:06:08 +02:00
|
|
|
|
|
|
|
// Literally cancer
|
|
|
|
#undef lines
|
|
|
|
#undef columns
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
|
|
|
// --- Configuration (application-specific) ------------------------------------
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
// TODO: reject all junk present in the configuration; there can be newlines
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
static struct config_item g_config_table[] =
|
|
|
|
{
|
|
|
|
{ "nickname", NULL, "IRC nickname" },
|
|
|
|
{ "username", NULL, "IRC user name" },
|
|
|
|
{ "realname", NULL, "IRC real name/e-mail" },
|
|
|
|
|
|
|
|
{ "irc_host", NULL, "Address of the IRC server" },
|
|
|
|
{ "irc_port", "6667", "Port of the IRC server" },
|
|
|
|
{ "ssl", "off", "Whether to use SSL" },
|
|
|
|
{ "ssl_cert", NULL, "Client SSL certificate (PEM)" },
|
|
|
|
{ "ssl_verify", "on", "Whether to verify certificates" },
|
|
|
|
{ "ssl_ca_file", NULL, "OpenSSL CA bundle file" },
|
|
|
|
{ "ssl_ca_path", NULL, "OpenSSL CA bundle path" },
|
|
|
|
{ "autojoin", NULL, "Channels to join on start" },
|
|
|
|
{ "reconnect", "on", "Whether to reconnect on error" },
|
|
|
|
{ "reconnect_delay", "5", "Time between reconnecting" },
|
|
|
|
|
|
|
|
{ "socks_host", NULL, "Address of a SOCKS 4a/5 proxy" },
|
|
|
|
{ "socks_port", "1080", "SOCKS port number" },
|
|
|
|
{ "socks_username", NULL, "SOCKS auth. username" },
|
|
|
|
{ "socks_password", NULL, "SOCKS auth. password" },
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
{ "isolate_buffers", "off", "Isolate global/server buffers" },
|
|
|
|
|
2015-04-15 12:37:22 +02:00
|
|
|
{ ATTR_PROMPT, NULL, "Terminal attributes for the prompt" },
|
|
|
|
{ ATTR_RESET, NULL, "String to reset terminal attributes" },
|
|
|
|
{ ATTR_WARNING, NULL, "Terminal attributes for warnings" },
|
|
|
|
{ ATTR_ERROR, NULL, "Terminal attributes for errors" },
|
|
|
|
|
2015-04-26 18:39:38 +02:00
|
|
|
{ ATTR_EXTERNAL, NULL, "Terminal attributes for external lines" },
|
2015-04-26 18:23:43 +02:00
|
|
|
{ ATTR_TIMESTAMP, NULL, "Terminal attributes for timestamps" },
|
2015-04-27 22:40:33 +02:00
|
|
|
{ ATTR_HIGHLIGHT, NULL, "Terminal attributes for highlights" },
|
2015-04-26 18:23:43 +02:00
|
|
|
{ ATTR_ACTION, NULL, "Terminal attributes for user actions" },
|
|
|
|
{ ATTR_JOIN, NULL, "Terminal attributes for joins" },
|
|
|
|
{ ATTR_PART, NULL, "Terminal attributes for parts" },
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
{ NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
// --- Application data --------------------------------------------------------
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
// All text stored in our data structures is encoded in UTF-8.
|
2015-04-19 02:12:59 +02:00
|
|
|
// Or at least should be. The exception is IRC identifiers.
|
2015-04-13 00:06:08 +02:00
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
static bool
|
|
|
|
isdigit_ascii (int c)
|
|
|
|
{
|
|
|
|
return c >= '0' && c <= '9';
|
|
|
|
}
|
|
|
|
|
2015-04-26 22:53:38 +02:00
|
|
|
static int
|
|
|
|
toupper_ascii (int c)
|
|
|
|
{
|
|
|
|
return c >= 'A' && c <= 'Z' ? c : c - ('a' - 'A');
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
/// Shorthand to set an error and return failure from the function
|
|
|
|
#define FAIL(...) \
|
|
|
|
BLOCK_START \
|
|
|
|
error_set (e, __VA_ARGS__); \
|
|
|
|
return false; \
|
|
|
|
BLOCK_END
|
|
|
|
|
2015-04-27 01:36:33 +02:00
|
|
|
// A few other debugging shorthands
|
|
|
|
#define LOG_FUNC_FAILURE(name, desc) \
|
|
|
|
print_debug ("%s: %s: %s", __func__, (name), (desc))
|
|
|
|
#define LOG_LIBC_FAILURE(name) \
|
|
|
|
print_debug ("%s: %s: %s", __func__, (name), strerror (errno))
|
|
|
|
|
2015-04-21 00:29:07 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
// We need a few reference countable objects with support
|
|
|
|
// for both strong and weak references
|
2015-04-19 02:12:59 +02:00
|
|
|
|
2015-04-21 00:04:34 +02:00
|
|
|
/// Callback just before a reference counted object is destroyed
|
|
|
|
typedef void (*destroy_cb_fn) (void *object, void *user_data);
|
|
|
|
|
2015-04-21 00:29:07 +02:00
|
|
|
#define REF_COUNTABLE_HEADER \
|
|
|
|
size_t ref_count; /**< Reference count */ \
|
|
|
|
destroy_cb_fn on_destroy; /**< To remove any weak references */ \
|
|
|
|
void *user_data; /**< User data for callbacks */
|
|
|
|
|
|
|
|
#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; \
|
|
|
|
if (self->on_destroy) \
|
|
|
|
self->on_destroy (self, self->user_data); \
|
|
|
|
name ## _destroy (self); \
|
|
|
|
}
|
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
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
|
2015-04-21 00:29:07 +02:00
|
|
|
user_channel_destroy (struct user_channel *self)
|
2015-04-19 02:12:59 +02:00
|
|
|
{
|
2015-04-25 00:36:02 +02:00
|
|
|
// The "channel" reference is weak and this object should get
|
|
|
|
// destroyed whenever the user stops being in the channel.
|
2015-04-19 02:12:59 +02:00
|
|
|
free (self);
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
// We keep references to user information in channels and buffers,
|
2015-04-21 00:04:34 +02:00
|
|
|
// and weak references in the name lookup table.
|
2015-04-19 02:12:59 +02:00
|
|
|
|
|
|
|
struct user
|
2015-04-18 22:09:05 +02:00
|
|
|
{
|
2015-04-21 00:29:07 +02:00
|
|
|
REF_COUNTABLE_HEADER
|
2015-04-21 00:04:34 +02:00
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
// TODO: eventually a reference to the server
|
|
|
|
|
2015-04-18 22:09:05 +02:00
|
|
|
char *nickname; ///< Literal nickname
|
2015-04-19 02:12:59 +02:00
|
|
|
// TODO: write code to poll for the away status
|
2015-04-18 22:09:05 +02:00
|
|
|
bool away; ///< User is away
|
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
struct user_channel *channels; ///< Channels the user is on
|
2015-04-18 22:09:05 +02:00
|
|
|
};
|
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
static struct user *
|
|
|
|
user_new (void)
|
|
|
|
{
|
|
|
|
struct user *self = xcalloc (1, sizeof *self);
|
|
|
|
self->ref_count = 1;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-04-18 22:09:05 +02:00
|
|
|
static void
|
2015-04-19 02:12:59 +02:00
|
|
|
user_destroy (struct user *self)
|
2015-04-18 22:09:05 +02:00
|
|
|
{
|
|
|
|
free (self->nickname);
|
2015-04-19 02:12:59 +02:00
|
|
|
LIST_FOR_EACH (struct user_channel, iter, self->channels)
|
|
|
|
user_channel_destroy (iter);
|
|
|
|
free (self);
|
|
|
|
}
|
|
|
|
|
2015-04-21 00:29:07 +02:00
|
|
|
REF_COUNTABLE_METHODS (user)
|
2015-04-19 02:12:59 +02:00
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
struct channel_user
|
|
|
|
{
|
|
|
|
LIST_HEADER (struct channel_user)
|
|
|
|
|
|
|
|
struct user *user; ///< Reference to user
|
2015-04-19 21:33:13 +02:00
|
|
|
char *modes; ///< Op/voice/... characters
|
2015-04-19 02:12:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct channel_user *
|
|
|
|
channel_user_new (void)
|
|
|
|
{
|
|
|
|
struct channel_user *self = xcalloc (1, sizeof *self);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-04-21 00:29:07 +02:00
|
|
|
channel_user_destroy (struct channel_user *self)
|
2015-04-19 02:12:59 +02:00
|
|
|
{
|
|
|
|
user_unref (self->user);
|
2015-04-19 21:33:13 +02:00
|
|
|
free (self->modes);
|
2015-04-19 02:12:59 +02:00
|
|
|
free (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2015-04-25 00:36:02 +02:00
|
|
|
// 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
|
2015-04-19 02:12:59 +02:00
|
|
|
|
|
|
|
struct channel
|
|
|
|
{
|
2015-04-21 00:29:07 +02:00
|
|
|
REF_COUNTABLE_HEADER
|
2015-04-21 00:04:34 +02:00
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
// TODO: eventually a reference to the server
|
|
|
|
|
|
|
|
char *name; ///< Channel name
|
|
|
|
char *mode; ///< Channel mode
|
|
|
|
char *topic; ///< Channel topic
|
|
|
|
|
|
|
|
struct channel_user *users; ///< Channel users
|
2015-04-21 22:08:18 +02:00
|
|
|
struct str_vector names_buf; ///< Buffer for RPL_NAMREPLY
|
2015-04-19 02:12:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct channel *
|
|
|
|
channel_new (void)
|
|
|
|
{
|
|
|
|
struct channel *self = xcalloc (1, sizeof *self);
|
|
|
|
self->ref_count = 1;
|
2015-04-21 22:08:18 +02:00
|
|
|
str_vector_init (&self->names_buf);
|
2015-04-19 02:12:59 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-04-21 00:04:34 +02:00
|
|
|
channel_destroy (struct channel *self)
|
2015-04-19 02:12:59 +02:00
|
|
|
{
|
|
|
|
free (self->name);
|
|
|
|
free (self->mode);
|
|
|
|
free (self->topic);
|
2015-04-25 00:36:02 +02:00
|
|
|
// Owner has to make sure we have no users by now
|
|
|
|
hard_assert (!self->users);
|
2015-04-21 22:08:18 +02:00
|
|
|
str_vector_free (&self->names_buf);
|
2015-04-18 22:09:05 +02:00
|
|
|
free (self);
|
|
|
|
}
|
|
|
|
|
2015-04-21 00:29:07 +02:00
|
|
|
REF_COUNTABLE_METHODS (channel)
|
2015-04-19 02:12:59 +02:00
|
|
|
|
2015-04-18 22:09:05 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
enum buffer_line_flags
|
|
|
|
{
|
|
|
|
BUFFER_LINE_HIGHLIGHT = 1 << 0 ///< The user was highlighted by this
|
|
|
|
};
|
|
|
|
|
|
|
|
enum buffer_line_type
|
|
|
|
{
|
2015-04-13 00:06:08 +02:00
|
|
|
BUFFER_LINE_PRIVMSG, ///< PRIVMSG
|
|
|
|
BUFFER_LINE_ACTION, ///< PRIVMSG ACTION
|
2015-04-12 04:52:39 +02:00
|
|
|
BUFFER_LINE_NOTICE, ///< NOTICE
|
2015-04-13 00:06:08 +02:00
|
|
|
BUFFER_LINE_JOIN, ///< JOIN
|
|
|
|
BUFFER_LINE_PART, ///< PART
|
|
|
|
BUFFER_LINE_KICK, ///< KICK
|
2015-04-20 21:56:32 +02:00
|
|
|
BUFFER_LINE_NICK, ///< NICK
|
2015-04-21 21:47:34 +02:00
|
|
|
BUFFER_LINE_TOPIC, ///< TOPIC
|
2015-04-13 00:06:08 +02:00
|
|
|
BUFFER_LINE_QUIT, ///< QUIT
|
2015-04-15 13:18:07 +02:00
|
|
|
BUFFER_LINE_STATUS, ///< Whatever status messages
|
|
|
|
BUFFER_LINE_ERROR ///< Whatever error messages
|
2015-04-12 04:52:39 +02:00
|
|
|
};
|
|
|
|
|
2015-04-25 13:41:10 +02:00
|
|
|
struct buffer_line_args
|
|
|
|
{
|
|
|
|
char *who; ///< Name of the origin or NULL (user)
|
|
|
|
char *object; ///< Object of action
|
|
|
|
char *text; ///< Text of message
|
|
|
|
char *reason; ///< Reason for PART, KICK, QUIT
|
|
|
|
};
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
struct buffer_line
|
|
|
|
{
|
|
|
|
LIST_HEADER (struct buffer_line)
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
// We use the "type" and "flags" mostly just as formatting hints
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
enum buffer_line_type type; ///< Type of the event
|
|
|
|
int flags; ///< Flags
|
|
|
|
|
|
|
|
time_t when; ///< Time of the event
|
2015-04-25 13:41:10 +02:00
|
|
|
struct buffer_line_args args; ///< Arguments
|
2015-04-12 04:52:39 +02:00
|
|
|
};
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
struct buffer_line *
|
|
|
|
buffer_line_new (void)
|
|
|
|
{
|
|
|
|
struct buffer_line *self = xcalloc (1, sizeof *self);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
buffer_line_destroy (struct buffer_line *self)
|
|
|
|
{
|
2015-04-25 13:41:10 +02:00
|
|
|
free (self->args.who);
|
|
|
|
free (self->args.object);
|
|
|
|
free (self->args.text);
|
|
|
|
free (self->args.reason);
|
2015-04-13 00:06:08 +02:00
|
|
|
free (self);
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
enum buffer_type type; ///< Type of the buffer
|
|
|
|
char *name; ///< The name of the buffer
|
|
|
|
|
2015-04-18 22:09:05 +02:00
|
|
|
// Readline state:
|
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
HISTORY_STATE *history; ///< Saved history state
|
|
|
|
char *saved_line; ///< Saved line
|
|
|
|
int saved_point; ///< Saved position in line
|
|
|
|
int saved_mark; ///< Saved mark
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
// Buffer contents:
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
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
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
unsigned unseen_messages_count; ///< # messages since last visited
|
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
// Origin information:
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
struct user *user; ///< Reference to user
|
|
|
|
struct channel *channel; ///< Reference to channel
|
|
|
|
|
|
|
|
// TODO: eventually a reference to the server for server buffers
|
2015-04-12 04:52:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct buffer *
|
|
|
|
buffer_new (void)
|
|
|
|
{
|
|
|
|
struct buffer *self = xcalloc (1, sizeof *self);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
buffer_destroy (struct buffer *self)
|
|
|
|
{
|
|
|
|
free (self->name);
|
2015-04-15 02:10:21 +02:00
|
|
|
// Can't really free "history" here
|
|
|
|
free (self->saved_line);
|
2015-04-19 02:12:59 +02:00
|
|
|
LIST_FOR_EACH (struct buffer_line, iter, self->lines)
|
|
|
|
buffer_line_destroy (iter);
|
|
|
|
if (self->user)
|
|
|
|
user_unref (self->user);
|
|
|
|
if (self->channel)
|
|
|
|
channel_unref (self->channel);
|
2015-04-12 04:52:39 +02:00
|
|
|
free (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
enum color_mode
|
|
|
|
{
|
|
|
|
COLOR_AUTO, ///< Autodetect if colours are available
|
|
|
|
COLOR_ALWAYS, ///< Always use coloured output
|
|
|
|
COLOR_NEVER ///< Never use coloured output
|
|
|
|
};
|
|
|
|
|
|
|
|
struct app_context
|
|
|
|
{
|
2015-04-13 00:06:08 +02:00
|
|
|
// Configuration:
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
struct str_map config; ///< User configuration
|
|
|
|
enum color_mode color_mode; ///< Colour output mode
|
|
|
|
bool reconnect; ///< Whether to reconnect on conn. fail.
|
|
|
|
unsigned long reconnect_delay; ///< Reconnect delay in seconds
|
2015-04-16 00:56:05 +02:00
|
|
|
bool isolate_buffers; ///< Isolate global/server buffers
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
// Server connection:
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
int irc_fd; ///< Socket FD of the server
|
|
|
|
struct str read_buffer; ///< Input yet to be processed
|
|
|
|
struct poller_fd irc_event; ///< IRC FD event
|
|
|
|
bool irc_ready; ///< Whether we may send messages now
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
SSL_CTX *ssl_ctx; ///< SSL context
|
|
|
|
SSL *ssl; ///< SSL connection
|
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
// TODO: an output queue to prevent excess floods (this will be needed
|
|
|
|
// especially for away status polling)
|
|
|
|
|
|
|
|
// XXX: there can be buffers for non-existent users
|
|
|
|
// TODO: initialize key_strxfrm according to server properties;
|
|
|
|
// note that collisions may arise on reconnecting
|
|
|
|
// TODO: when disconnected, get rid of all users everywhere;
|
|
|
|
// maybe also broadcast all buffers about the disconnection event
|
|
|
|
// TODO: when getting connected again, rejoin all current channels
|
|
|
|
|
|
|
|
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
|
2015-04-18 22:09:05 +02:00
|
|
|
|
2015-04-20 22:09:56 +02:00
|
|
|
struct user *irc_user; ///< Our own user
|
|
|
|
char *irc_user_mode; ///< Our current user mode
|
|
|
|
char *irc_user_host; ///< Our current user@host
|
2015-04-15 16:54:05 +02:00
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
// Events:
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
struct poller_fd tty_event; ///< Terminal input event
|
|
|
|
struct poller_fd signal_event; ///< Signal FD event
|
2015-04-18 22:09:05 +02:00
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
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
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
struct poller poller; ///< Manages polled descriptors
|
|
|
|
bool quitting; ///< User requested quitting
|
|
|
|
bool polling; ///< The event loop is running
|
|
|
|
|
|
|
|
// Buffers:
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
struct buffer *buffers; ///< All our buffers in order
|
|
|
|
struct buffer *buffers_tail; ///< The tail of our buffers
|
|
|
|
|
2015-04-27 22:51:40 +02:00
|
|
|
struct buffer *last_buffer; ///< Last used buffer
|
|
|
|
|
2015-04-17 21:29:18 +02:00
|
|
|
// XXX: when we go multiserver, there will be collisions
|
2015-04-19 02:12:59 +02:00
|
|
|
// TODO: make buffer names unique like weechat does
|
2015-04-13 00:06:08 +02:00
|
|
|
struct str_map buffers_by_name; ///< Excludes GLOBAL and SERVER
|
2015-04-18 22:09:05 +02:00
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
struct buffer *global_buffer; ///< The global buffer
|
|
|
|
struct buffer *server_buffer; ///< The server buffer
|
|
|
|
struct buffer *current_buffer; ///< The current buffer
|
|
|
|
|
|
|
|
// TODO: So that we always output proper date change messages
|
|
|
|
time_t last_displayed_msg_time; ///< Time of last displayed message
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
// Terminal:
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
iconv_t term_to_utf8; ///< Terminal encoding to UTF-8
|
|
|
|
iconv_t term_from_utf8; ///< UTF-8 to terminal encoding
|
2015-04-17 22:20:48 +02:00
|
|
|
iconv_t latin1_to_utf8; ///< ISO Latin 1 to UTF-8
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
int lines; ///< Current terminal height
|
|
|
|
int columns; ///< Current ternimal width
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
char *readline_prompt; ///< The prompt we use for readline
|
|
|
|
bool readline_prompt_shown; ///< Whether the prompt is shown now
|
|
|
|
}
|
|
|
|
*g_ctx;
|
|
|
|
|
|
|
|
static void on_irc_ping_timeout (void *user_data);
|
|
|
|
static void on_irc_timeout (void *user_data);
|
|
|
|
static void on_irc_reconnect_timeout (void *user_data);
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_context_init (struct app_context *self)
|
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
|
|
|
|
str_map_init (&self->config);
|
|
|
|
self->config.free = free;
|
|
|
|
load_config_defaults (&self->config, g_config_table);
|
|
|
|
|
|
|
|
self->irc_fd = -1;
|
|
|
|
str_init (&self->read_buffer);
|
|
|
|
self->irc_ready = false;
|
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
poller_init (&self->poller);
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
str_map_init (&self->buffers_by_name);
|
|
|
|
self->buffers_by_name.key_xfrm = irc_strxfrm;
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
self->last_displayed_msg_time = time (NULL);
|
|
|
|
|
|
|
|
char *encoding = nl_langinfo (CODESET);
|
|
|
|
#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
|
2015-04-17 22:20:48 +02:00
|
|
|
|| (self->latin1_to_utf8 =
|
|
|
|
iconv_open ("UTF-8", "ISO-8859-1")) == (iconv_t) -1
|
2015-04-12 04:52:39 +02:00
|
|
|
|| (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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_context_free (struct app_context *self)
|
|
|
|
{
|
|
|
|
str_map_free (&self->config);
|
|
|
|
str_free (&self->read_buffer);
|
|
|
|
|
|
|
|
if (self->irc_fd != -1)
|
|
|
|
{
|
|
|
|
xclose (self->irc_fd);
|
|
|
|
poller_fd_reset (&self->irc_event);
|
|
|
|
}
|
|
|
|
if (self->ssl)
|
|
|
|
SSL_free (self->ssl);
|
|
|
|
if (self->ssl_ctx)
|
|
|
|
SSL_CTX_free (self->ssl_ctx);
|
|
|
|
|
2015-04-20 23:09:13 +02:00
|
|
|
if (self->irc_user)
|
|
|
|
user_unref (self->irc_user);
|
2015-04-15 16:54:05 +02:00
|
|
|
free (self->irc_user_mode);
|
2015-04-20 22:09:56 +02:00
|
|
|
free (self->irc_user_host);
|
2015-04-15 16:54:05 +02:00
|
|
|
|
2015-04-24 23:30:48 +02:00
|
|
|
// FIXME: this doesn't free the history state
|
|
|
|
LIST_FOR_EACH (struct buffer, iter, self->buffers)
|
|
|
|
buffer_destroy (iter);
|
|
|
|
str_map_free (&self->buffers_by_name);
|
|
|
|
|
2015-04-21 00:40:30 +02:00
|
|
|
str_map_free (&self->irc_users);
|
|
|
|
str_map_free (&self->irc_channels);
|
|
|
|
str_map_free (&self->irc_buffer_map);
|
|
|
|
|
2015-04-19 02:12:59 +02:00
|
|
|
poller_free (&self->poller);
|
|
|
|
|
2015-04-17 22:20:48 +02:00
|
|
|
iconv_close (self->latin1_to_utf8);
|
2015-04-12 04:52:39 +02:00
|
|
|
iconv_close (self->term_from_utf8);
|
|
|
|
iconv_close (self->term_to_utf8);
|
2015-04-13 00:06:08 +02:00
|
|
|
|
|
|
|
free (self->readline_prompt);
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
static void refresh_prompt (struct app_context *ctx);
|
2015-04-19 23:05:49 +02:00
|
|
|
static char *irc_cut_nickname (const char *prefix);
|
|
|
|
static const char *irc_find_userhost (const char *prefix);
|
2015-04-15 02:10:21 +02:00
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
// --- Attributed output -------------------------------------------------------
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
bool initialized; ///< Terminal is available
|
|
|
|
bool stdout_is_tty; ///< `stdout' is a terminal
|
|
|
|
bool stderr_is_tty; ///< `stderr' is a terminal
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
char *color_set_fg[8]; ///< Codes to set the foreground colour
|
|
|
|
char *color_set_bg[8]; ///< Codes to set the background colour
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
|
|
|
g_terminal;
|
|
|
|
|
|
|
|
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
|
2015-04-26 18:23:43 +02:00
|
|
|
if (!set_a_foreground || !set_a_background
|
|
|
|
|| !enter_bold_mode || !exit_attribute_mode)
|
2015-04-12 04:52:39 +02:00
|
|
|
{
|
|
|
|
del_curterm (cur_term);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
for (size_t i = 0; i < N_ELEMENTS (g_terminal.color_set_fg); i++)
|
|
|
|
{
|
|
|
|
g_terminal.color_set_fg[i] = xstrdup (tparm (set_a_foreground,
|
2015-04-12 04:52:39 +02:00
|
|
|
i, 0, 0, 0, 0, 0, 0, 0, 0));
|
2015-04-26 18:23:43 +02:00
|
|
|
g_terminal.color_set_bg[i] = xstrdup (tparm (set_a_background,
|
|
|
|
i, 0, 0, 0, 0, 0, 0, 0, 0));
|
|
|
|
}
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
return g_terminal.initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_terminal (void)
|
|
|
|
{
|
|
|
|
if (!g_terminal.initialized)
|
|
|
|
return;
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
for (size_t i = 0; i < N_ELEMENTS (g_terminal.color_set_fg); i++)
|
|
|
|
{
|
|
|
|
free (g_terminal.color_set_fg[i]);
|
|
|
|
free (g_terminal.color_set_bg[i]);
|
|
|
|
}
|
2015-04-12 04:52:39 +02:00
|
|
|
del_curterm (cur_term);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
struct app_readline_state
|
|
|
|
{
|
|
|
|
char *saved_line;
|
|
|
|
int saved_point;
|
2015-04-15 02:10:21 +02:00
|
|
|
int saved_mark;
|
2015-04-12 04:52:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_readline_hide (struct app_readline_state *state)
|
|
|
|
{
|
|
|
|
state->saved_point = rl_point;
|
2015-04-18 16:03:50 +02:00
|
|
|
state->saved_mark = rl_mark;
|
2015-04-12 04:52:39 +02:00
|
|
|
state->saved_line = rl_copy_text (0, rl_end);
|
|
|
|
rl_set_prompt ("");
|
|
|
|
rl_replace_line ("", 0);
|
|
|
|
rl_redisplay ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
app_readline_restore (struct app_readline_state *state, const char *prompt)
|
|
|
|
{
|
|
|
|
rl_set_prompt (prompt);
|
|
|
|
rl_replace_line (state->saved_line, 0);
|
|
|
|
rl_point = state->saved_point;
|
2015-04-15 02:10:21 +02:00
|
|
|
rl_mark = state->saved_mark;
|
2015-04-12 04:52:39 +02:00
|
|
|
rl_redisplay ();
|
|
|
|
free (state->saved_line);
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:03:50 +02:00
|
|
|
static void
|
|
|
|
app_readline_erase_to_bol (const char *prompt)
|
|
|
|
{
|
|
|
|
rl_set_prompt ("");
|
|
|
|
rl_replace_line ("", 0);
|
|
|
|
rl_point = rl_mark = 0;
|
|
|
|
rl_redisplay ();
|
|
|
|
rl_set_prompt (prompt);
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
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, const char *attribute, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
terminal_printer_fn printer = get_attribute_printer (stream);
|
|
|
|
if (!attribute)
|
|
|
|
printer = NULL;
|
|
|
|
|
|
|
|
if (printer)
|
|
|
|
{
|
|
|
|
const char *value = str_map_find (&ctx->config, attribute);
|
|
|
|
tputs (value, 1, printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
vfprintf (stream, fmt, ap);
|
|
|
|
|
|
|
|
if (printer)
|
|
|
|
{
|
|
|
|
const char *value = str_map_find (&ctx->config, ATTR_RESET);
|
|
|
|
tputs (value, 1, printer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_attributed (struct app_context *ctx,
|
|
|
|
FILE *stream, const char *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_readline_state state;
|
|
|
|
if (g_ctx->readline_prompt_shown)
|
|
|
|
app_readline_hide (&state);
|
|
|
|
|
|
|
|
print_attributed (g_ctx, stream, user_data, "%s", quote);
|
|
|
|
vprint_attributed (g_ctx, stream, user_data, fmt, ap);
|
|
|
|
fputs ("\n", stream);
|
|
|
|
|
|
|
|
if (g_ctx->readline_prompt_shown)
|
|
|
|
app_readline_restore (&state, g_ctx->readline_prompt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_colors (struct app_context *ctx)
|
|
|
|
{
|
2015-04-21 20:52:41 +02:00
|
|
|
bool have_ti = init_terminal ();
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
// Use escape sequences from terminfo if possible, and SGR as a fallback
|
2015-04-21 20:52:41 +02:00
|
|
|
#define INIT_ATTR(id, ti, vt100) \
|
|
|
|
str_map_set (&ctx->config, (id), xstrdup (have_ti ? (ti) : (vt100)));
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
INIT_ATTR (ATTR_PROMPT, enter_bold_mode, "\x1b[1m");
|
|
|
|
INIT_ATTR (ATTR_RESET, exit_attribute_mode, "\x1b[0m");
|
|
|
|
INIT_ATTR (ATTR_WARNING, g_terminal.color_set_fg[3], "\x1b[33m");
|
|
|
|
INIT_ATTR (ATTR_ERROR, g_terminal.color_set_fg[1], "\x1b[31m");
|
|
|
|
|
2015-04-26 18:39:38 +02:00
|
|
|
INIT_ATTR (ATTR_EXTERNAL, g_terminal.color_set_fg[7], "\x1b[37m");
|
2015-04-26 18:23:43 +02:00
|
|
|
INIT_ATTR (ATTR_TIMESTAMP, g_terminal.color_set_fg[7], "\x1b[37m");
|
|
|
|
INIT_ATTR (ATTR_ACTION, g_terminal.color_set_fg[1], "\x1b[31m");
|
|
|
|
INIT_ATTR (ATTR_JOIN, g_terminal.color_set_fg[2], "\x1b[32m");
|
|
|
|
INIT_ATTR (ATTR_PART, g_terminal.color_set_fg[1], "\x1b[31m");
|
2015-04-21 20:52:41 +02:00
|
|
|
|
2015-04-27 22:40:33 +02:00
|
|
|
char *highlight = xstrdup_printf ("%s%s%s",
|
|
|
|
g_terminal.color_set_fg[3],
|
|
|
|
g_terminal.color_set_bg[5],
|
|
|
|
enter_bold_mode);
|
2015-04-27 23:02:15 +02:00
|
|
|
INIT_ATTR (ATTR_HIGHLIGHT, highlight, "\x1b[33;47;1m");
|
2015-04-27 22:40:33 +02:00
|
|
|
free (highlight);
|
|
|
|
|
2015-04-21 20:52:41 +02:00
|
|
|
#undef INIT_ATTR
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
switch (ctx->color_mode)
|
|
|
|
{
|
|
|
|
case COLOR_ALWAYS:
|
|
|
|
g_terminal.stdout_is_tty = true;
|
|
|
|
g_terminal.stderr_is_tty = true;
|
|
|
|
break;
|
|
|
|
case COLOR_AUTO:
|
|
|
|
if (!g_terminal.initialized)
|
|
|
|
{
|
|
|
|
case COLOR_NEVER:
|
|
|
|
g_terminal.stdout_is_tty = false;
|
|
|
|
g_terminal.stderr_is_tty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_log_message_real = log_message_attributed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Signals -----------------------------------------------------------------
|
|
|
|
|
|
|
|
static int g_signal_pipe[2]; ///< A pipe used to signal... signals
|
|
|
|
|
|
|
|
/// Program termination has been requested by a signal
|
|
|
|
static volatile sig_atomic_t g_termination_requested;
|
|
|
|
/// The window has changed in size
|
|
|
|
static volatile sig_atomic_t g_winch_received;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sigterm_handler (int signum)
|
|
|
|
{
|
|
|
|
(void) signum;
|
|
|
|
|
|
|
|
g_termination_requested = true;
|
|
|
|
|
|
|
|
int original_errno = errno;
|
|
|
|
if (write (g_signal_pipe[1], "t", 1) == -1)
|
|
|
|
soft_assert (errno == EAGAIN);
|
|
|
|
errno = original_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sigwinch_handler (int signum)
|
|
|
|
{
|
|
|
|
(void) signum;
|
|
|
|
|
|
|
|
g_winch_received = true;
|
|
|
|
|
|
|
|
int original_errno = errno;
|
|
|
|
if (write (g_signal_pipe[1], "w", 1) == -1)
|
|
|
|
soft_assert (errno == EAGAIN);
|
|
|
|
errno = original_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_signal_handlers (void)
|
|
|
|
{
|
|
|
|
if (pipe (g_signal_pipe) == -1)
|
|
|
|
exit_fatal ("%s: %s", "pipe", strerror (errno));
|
|
|
|
|
|
|
|
set_cloexec (g_signal_pipe[0]);
|
|
|
|
set_cloexec (g_signal_pipe[1]);
|
|
|
|
|
|
|
|
// So that the pipe cannot overflow; it would make write() block within
|
|
|
|
// the signal handler, which is something we really don't want to happen.
|
|
|
|
// The same holds true for read().
|
|
|
|
set_blocking (g_signal_pipe[0], false);
|
|
|
|
set_blocking (g_signal_pipe[1], false);
|
|
|
|
|
|
|
|
signal (SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sa.sa_handler = sigwinch_handler;
|
|
|
|
sigemptyset (&sa.sa_mask);
|
|
|
|
|
|
|
|
if (sigaction (SIGWINCH, &sa, NULL) == -1)
|
|
|
|
exit_fatal ("sigaction: %s", strerror (errno));
|
|
|
|
|
|
|
|
sa.sa_handler = sigterm_handler;
|
|
|
|
if (sigaction (SIGINT, &sa, NULL) == -1
|
|
|
|
|| sigaction (SIGTERM, &sa, NULL) == -1)
|
|
|
|
exit_fatal ("sigaction: %s", strerror (errno));
|
|
|
|
}
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
// --- 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
|
|
|
|
// #d inserts a signed integer; also supports the #<N> and #0<N> notation
|
|
|
|
//
|
|
|
|
// #a inserts named attributes (auto-resets)
|
|
|
|
// #r resets terminal attributes
|
|
|
|
// #c sets foreground color
|
|
|
|
// #C sets background color
|
|
|
|
|
|
|
|
enum formatter_item_type
|
|
|
|
{
|
|
|
|
FORMATTER_ITEM_TEXT, ///< Text
|
|
|
|
FORMATTER_ITEM_ATTR, ///< Named formatting attributes
|
|
|
|
FORMATTER_ITEM_FG_COLOR, ///< Foreground color
|
|
|
|
FORMATTER_ITEM_BG_COLOR ///< Background color
|
|
|
|
};
|
|
|
|
|
|
|
|
struct formatter_item
|
|
|
|
{
|
|
|
|
LIST_HEADER (struct formatter_item)
|
|
|
|
|
|
|
|
enum formatter_item_type type; ///< Type of this item
|
|
|
|
int color; ///< Color
|
|
|
|
char *data; ///< Either text or an attribute string
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct formatter_item *
|
|
|
|
formatter_item_new (void)
|
|
|
|
{
|
|
|
|
struct formatter_item *self = xcalloc (1, sizeof *self);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formatter_item_destroy (struct formatter_item *self)
|
|
|
|
{
|
|
|
|
free (self->data);
|
|
|
|
free (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
struct formatter
|
|
|
|
{
|
|
|
|
struct app_context *ctx; ///< Application context
|
2015-04-26 18:39:38 +02:00
|
|
|
bool ignore_new_attributes; ///< Whether to ignore new attributes
|
2015-04-26 18:23:43 +02:00
|
|
|
|
|
|
|
struct formatter_item *items; ///< Items
|
|
|
|
struct formatter_item *items_tail; ///< Tail of items
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
formatter_init (struct formatter *self, struct app_context *ctx)
|
|
|
|
{
|
|
|
|
memset (self, 0, sizeof *self);
|
|
|
|
self->ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formatter_free (struct formatter *self)
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH (struct formatter_item, iter, self->items)
|
|
|
|
formatter_item_destroy (iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct formatter_item *
|
|
|
|
formatter_add_blank (struct formatter *self)
|
|
|
|
{
|
|
|
|
struct formatter_item *item = formatter_item_new ();
|
|
|
|
LIST_APPEND_WITH_TAIL (self->items, self->items_tail, item);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-04-26 18:39:38 +02:00
|
|
|
formatter_add_text (struct formatter *self, const char *text)
|
2015-04-26 18:23:43 +02:00
|
|
|
{
|
|
|
|
struct formatter_item *item = formatter_add_blank (self);
|
2015-04-26 18:39:38 +02:00
|
|
|
item->type = FORMATTER_ITEM_TEXT;
|
|
|
|
item->data = xstrdup (text);
|
2015-04-26 18:23:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formatter_add_reset (struct formatter *self)
|
|
|
|
{
|
2015-04-26 18:39:38 +02:00
|
|
|
if (self->ignore_new_attributes)
|
|
|
|
return;
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
struct formatter_item *item = formatter_add_blank (self);
|
|
|
|
item->type = FORMATTER_ITEM_ATTR;
|
|
|
|
item->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-04-26 18:39:38 +02:00
|
|
|
formatter_add_attr (struct formatter *self, const char *attr_name)
|
2015-04-26 18:23:43 +02:00
|
|
|
{
|
2015-04-26 18:39:38 +02:00
|
|
|
if (self->ignore_new_attributes)
|
|
|
|
return;
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
struct formatter_item *item = formatter_add_blank (self);
|
2015-04-26 18:39:38 +02:00
|
|
|
item->type = FORMATTER_ITEM_ATTR;
|
|
|
|
item->data = xstrdup (str_map_find (&self->ctx->config, attr_name));
|
2015-04-26 18:23:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formatter_add_fg_color (struct formatter *self, int color)
|
|
|
|
{
|
2015-04-26 18:39:38 +02:00
|
|
|
if (self->ignore_new_attributes)
|
|
|
|
return;
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
struct formatter_item *item = formatter_add_blank (self);
|
|
|
|
item->type = FORMATTER_ITEM_FG_COLOR;
|
|
|
|
item->color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formatter_add_bg_color (struct formatter *self, int color)
|
|
|
|
{
|
2015-04-26 18:39:38 +02:00
|
|
|
if (self->ignore_new_attributes)
|
|
|
|
return;
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
struct formatter_item *item = formatter_add_blank (self);
|
|
|
|
item->type = FORMATTER_ITEM_BG_COLOR;
|
|
|
|
item->color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
formatter_parse_field (struct formatter *self,
|
|
|
|
const char *field, struct str *buf, va_list *ap)
|
|
|
|
{
|
|
|
|
size_t width = 0;
|
|
|
|
bool zero_padded = false;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
restart:
|
|
|
|
switch ((c = *field++))
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
// We can push boring text content to the caller's buffer
|
|
|
|
// and let it flush the buffer only when it's actually needed
|
|
|
|
case 's':
|
|
|
|
s = va_arg (*ap, char *);
|
|
|
|
for (size_t len = strlen (s); len < width; len++)
|
|
|
|
str_append_c (buf, ' ');
|
|
|
|
str_append (buf, s);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
s = xstrdup_printf ("%d", va_arg (*ap, int));
|
|
|
|
for (size_t len = strlen (s); len < width; len++)
|
|
|
|
str_append_c (buf, " 0"[zero_padded]);
|
|
|
|
str_append (buf, s);
|
|
|
|
free (s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
formatter_add_attr (self, va_arg (*ap, const char *));
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
formatter_add_fg_color (self, va_arg (*ap, int));
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
formatter_add_bg_color (self, va_arg (*ap, int));
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
formatter_add_reset (self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (c == '0' && !zero_padded)
|
|
|
|
zero_padded = true;
|
|
|
|
else if (isdigit_ascii (c))
|
|
|
|
width = width * 10 + (c - '0');
|
|
|
|
else if (c)
|
|
|
|
hard_assert (!"unexpected format specifier");
|
|
|
|
else
|
|
|
|
hard_assert (!"unexpected end of format string");
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formatter_add (struct formatter *self, const char *format, ...)
|
|
|
|
{
|
|
|
|
struct str buf;
|
|
|
|
str_init (&buf);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, format);
|
|
|
|
|
|
|
|
while (*format)
|
|
|
|
{
|
|
|
|
if (*format != '#' || *++format == '#')
|
|
|
|
{
|
|
|
|
str_append_c (&buf, *format++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (buf.len)
|
|
|
|
{
|
|
|
|
formatter_add_text (self, buf.str);
|
|
|
|
str_reset (&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
format = formatter_parse_field (self, format, &buf, &ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf.len)
|
|
|
|
formatter_add_text (self, buf.str);
|
|
|
|
|
|
|
|
str_free (&buf);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formatter_flush (struct formatter *self, FILE *stream)
|
|
|
|
{
|
|
|
|
terminal_printer_fn printer = get_attribute_printer (stream);
|
2015-04-28 17:25:50 +02:00
|
|
|
if (!printer)
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH (struct formatter_item, iter, self->items)
|
|
|
|
if (iter->type == FORMATTER_ITEM_TEXT)
|
|
|
|
fputs (iter->data, stream);
|
|
|
|
return;
|
|
|
|
}
|
2015-04-26 18:23:43 +02:00
|
|
|
|
|
|
|
const char *attr_reset = str_map_find (&self->ctx->config, ATTR_RESET);
|
2015-04-28 17:25:50 +02:00
|
|
|
tputs (attr_reset, 1, printer);
|
2015-04-26 18:23:43 +02:00
|
|
|
|
|
|
|
bool is_attributed = false;
|
|
|
|
LIST_FOR_EACH (struct formatter_item, iter, self->items)
|
|
|
|
{
|
|
|
|
switch (iter->type)
|
|
|
|
{
|
2015-04-28 17:25:50 +02:00
|
|
|
char *term;
|
2015-04-26 18:23:43 +02:00
|
|
|
case FORMATTER_ITEM_TEXT:
|
2015-04-28 17:25:50 +02:00
|
|
|
term = iconv_xstrdup
|
|
|
|
(self->ctx->term_from_utf8, iter->data, -1, NULL);
|
|
|
|
fputs (term, stream);
|
|
|
|
free (term);
|
2015-04-26 18:23:43 +02:00
|
|
|
break;
|
|
|
|
case FORMATTER_ITEM_ATTR:
|
|
|
|
if (is_attributed)
|
|
|
|
{
|
|
|
|
tputs (attr_reset, 1, printer);
|
|
|
|
is_attributed = false;
|
|
|
|
}
|
|
|
|
if (iter->data)
|
|
|
|
{
|
|
|
|
tputs (iter->data, 1, printer);
|
|
|
|
is_attributed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FORMATTER_ITEM_FG_COLOR:
|
|
|
|
tputs (g_terminal.color_set_fg[iter->color], 1, printer);
|
|
|
|
is_attributed = true;
|
|
|
|
break;
|
|
|
|
case FORMATTER_ITEM_BG_COLOR:
|
|
|
|
tputs (g_terminal.color_set_bg[iter->color], 1, printer);
|
|
|
|
is_attributed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_attributed)
|
|
|
|
tputs (attr_reset, 1, printer);
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
// --- Buffers -----------------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
2015-04-13 00:06:08 +02:00
|
|
|
buffer_update_time (struct app_context *ctx, time_t now)
|
|
|
|
{
|
|
|
|
struct tm last, current;
|
|
|
|
if (!localtime_r (&ctx->last_displayed_msg_time, &last)
|
|
|
|
|| !localtime_r (&now, ¤t))
|
|
|
|
{
|
|
|
|
// Strange but nonfatal
|
|
|
|
print_error ("%s: %s", "localtime_r", strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->last_displayed_msg_time = now;
|
|
|
|
if (last.tm_year == current.tm_year
|
|
|
|
&& last.tm_mon == current.tm_mon
|
|
|
|
&& last.tm_mday == current.tm_mday)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char buf[32] = "";
|
|
|
|
if (soft_assert (strftime (buf, sizeof buf, "%F", ¤t)))
|
|
|
|
print_status ("%s", buf);
|
|
|
|
// Else the buffer was too small, which is pretty weird
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-04-26 18:23:43 +02:00
|
|
|
buffer_line_display (struct app_context *ctx,
|
|
|
|
struct buffer_line *line, bool is_external)
|
2015-04-13 00:06:08 +02:00
|
|
|
{
|
|
|
|
// Normal timestamps don't include the date, this way the user won't be
|
|
|
|
// confused as to when an event has happened
|
|
|
|
buffer_update_time (ctx, line->when);
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
struct buffer_line_args *a = &line->args;
|
|
|
|
|
|
|
|
char *nick = NULL;
|
|
|
|
const char *userhost = NULL;
|
|
|
|
int nick_color = -1;
|
|
|
|
int object_color = -1;
|
|
|
|
|
|
|
|
if (a->who)
|
|
|
|
{
|
|
|
|
nick = irc_cut_nickname (a->who);
|
|
|
|
userhost = irc_find_userhost (a->who);
|
|
|
|
nick_color = str_map_hash (nick, strlen (nick)) % 8;
|
|
|
|
}
|
|
|
|
if (a->object)
|
|
|
|
object_color = str_map_hash (a->object, strlen (a->object)) % 8;
|
|
|
|
|
|
|
|
struct formatter f;
|
|
|
|
formatter_init (&f, ctx);
|
2015-04-13 00:06:08 +02:00
|
|
|
|
|
|
|
struct tm current;
|
|
|
|
if (!localtime_r (&line->when, ¤t))
|
|
|
|
print_error ("%s: %s", "localtime_r", strerror (errno));
|
|
|
|
else
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, "#a#02d:#02d:#02d#r ",
|
|
|
|
ATTR_TIMESTAMP, current.tm_hour, current.tm_min, current.tm_sec);
|
2015-04-25 13:41:10 +02:00
|
|
|
|
2015-04-26 18:39:38 +02:00
|
|
|
// Ignore all formatting for messages coming from other buffers, that is
|
|
|
|
// either from the global or server buffer. Instead print them in grey.
|
|
|
|
if (is_external)
|
|
|
|
{
|
|
|
|
formatter_add (&f, "#a", ATTR_EXTERNAL);
|
|
|
|
f.ignore_new_attributes = true;
|
|
|
|
}
|
2015-04-19 23:05:49 +02:00
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
// TODO: try to decode as much as possible using mIRC formatting;
|
|
|
|
// could either add a #m format specifier, or write a separate function
|
|
|
|
// to translate the formatting into formatter API calls
|
2015-04-19 23:05:49 +02:00
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
switch (line->type)
|
|
|
|
{
|
|
|
|
case BUFFER_LINE_PRIVMSG:
|
2015-04-27 22:40:33 +02:00
|
|
|
if (line->flags & BUFFER_LINE_HIGHLIGHT)
|
|
|
|
formatter_add (&f, "#a<#s>#r #s", ATTR_HIGHLIGHT, nick, a->text);
|
|
|
|
else
|
|
|
|
formatter_add (&f, "<#c#s#r> #s", nick_color, nick, a->text);
|
2015-04-13 00:06:08 +02:00
|
|
|
break;
|
|
|
|
case BUFFER_LINE_ACTION:
|
2015-04-27 22:40:33 +02:00
|
|
|
if (line->flags & BUFFER_LINE_HIGHLIGHT)
|
|
|
|
formatter_add (&f, " #a*#r ", ATTR_HIGHLIGHT);
|
|
|
|
else
|
|
|
|
formatter_add (&f, " #a*#r ", ATTR_ACTION);
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, "#c#s#r #s", nick_color, nick, a->text);
|
2015-04-13 00:06:08 +02:00
|
|
|
break;
|
|
|
|
case BUFFER_LINE_NOTICE:
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, " - ");
|
2015-04-27 22:40:33 +02:00
|
|
|
if (line->flags & BUFFER_LINE_HIGHLIGHT)
|
|
|
|
formatter_add (&f, "#a#s(#s)#r: #s",
|
|
|
|
ATTR_HIGHLIGHT, "Notice", nick, a->text);
|
|
|
|
else
|
|
|
|
formatter_add (&f, "#s(#c#s#r): #s",
|
|
|
|
"Notice", nick_color, nick, a->text);
|
2015-04-13 00:06:08 +02:00
|
|
|
break;
|
|
|
|
case BUFFER_LINE_JOIN:
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, "#a-->#r ", ATTR_JOIN);
|
|
|
|
formatter_add (&f, "#c#s#r (#s) #a#s#r #s",
|
|
|
|
nick_color, nick, userhost,
|
|
|
|
ATTR_JOIN, "has joined", a->object);
|
2015-04-13 00:06:08 +02:00
|
|
|
break;
|
|
|
|
case BUFFER_LINE_PART:
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, "#a<--#r ", ATTR_PART);
|
|
|
|
formatter_add (&f, "#c#s#r (#s) #a#s#r #s",
|
|
|
|
nick_color, nick, userhost,
|
|
|
|
ATTR_PART, "has left", a->object);
|
|
|
|
if (a->reason)
|
|
|
|
formatter_add (&f, " (#s)", a->reason);
|
2015-04-13 00:06:08 +02:00
|
|
|
break;
|
|
|
|
case BUFFER_LINE_KICK:
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, "#a<--#r ", ATTR_PART);
|
|
|
|
formatter_add (&f, "#c#s#r (#s) #a#s#r #c#s#r",
|
|
|
|
nick_color, nick, userhost,
|
|
|
|
ATTR_PART, "has kicked", object_color, a->object);
|
|
|
|
if (a->reason)
|
|
|
|
formatter_add (&f, " (#s)", a->reason);
|
2015-04-13 00:06:08 +02:00
|
|
|
break;
|
2015-04-20 21:56:32 +02:00
|
|
|
case BUFFER_LINE_NICK:
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, " - ");
|
|
|
|
if (a->who)
|
|
|
|
formatter_add (&f, "#c#s#r #s #c#s#r",
|
|
|
|
nick_color, nick,
|
|
|
|
"is now known as", object_color, a->object);
|
2015-04-20 21:56:32 +02:00
|
|
|
else
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, "#s #s",
|
|
|
|
"You are now known as", a->object);
|
2015-04-20 21:56:32 +02:00
|
|
|
break;
|
2015-04-21 21:47:34 +02:00
|
|
|
case BUFFER_LINE_TOPIC:
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, " - ");
|
|
|
|
formatter_add (&f, "#c#s#r #s \"#s\"",
|
|
|
|
nick_color, nick,
|
|
|
|
"has changed the topic to", a->text);
|
2015-04-21 21:47:34 +02:00
|
|
|
break;
|
2015-04-13 00:06:08 +02:00
|
|
|
case BUFFER_LINE_QUIT:
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, "#a<--#r ", ATTR_PART);
|
|
|
|
formatter_add (&f, "#c#s#r (%s) #a#s#r",
|
|
|
|
nick_color, nick, userhost,
|
|
|
|
ATTR_PART, "has quit");
|
|
|
|
if (a->reason)
|
|
|
|
formatter_add (&f, " (#s)", a->reason);
|
2015-04-13 00:06:08 +02:00
|
|
|
break;
|
|
|
|
case BUFFER_LINE_STATUS:
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, " - ");
|
|
|
|
formatter_add (&f, "#s", a->text);
|
2015-04-15 13:18:07 +02:00
|
|
|
break;
|
|
|
|
case BUFFER_LINE_ERROR:
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, "#a=!=#r ", ATTR_ERROR);
|
|
|
|
formatter_add (&f, "#s", a->text);
|
2015-04-13 00:06:08 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 23:05:49 +02:00
|
|
|
free (nick);
|
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
struct app_readline_state state;
|
|
|
|
if (ctx->readline_prompt_shown)
|
|
|
|
app_readline_hide (&state);
|
2015-04-13 00:06:08 +02:00
|
|
|
|
2015-04-15 12:38:21 +02:00
|
|
|
// TODO: write the line to a log file; note that the global and server
|
|
|
|
// buffers musn't collide with filenames
|
|
|
|
|
2015-04-26 18:23:43 +02:00
|
|
|
formatter_add (&f, "\n");
|
|
|
|
formatter_flush (&f, stdout);
|
|
|
|
formatter_free (&f);
|
2015-04-13 00:06:08 +02:00
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
if (ctx->readline_prompt_shown)
|
|
|
|
app_readline_restore (&state, ctx->readline_prompt);
|
2015-04-13 00:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-04-25 13:41:10 +02:00
|
|
|
buffer_send_internal (struct app_context *ctx, struct buffer *buffer,
|
2015-04-12 04:52:39 +02:00
|
|
|
enum buffer_line_type type, int flags,
|
2015-04-25 13:41:10 +02:00
|
|
|
struct buffer_line_args a)
|
2015-04-13 00:06:08 +02:00
|
|
|
{
|
|
|
|
struct buffer_line *line = buffer_line_new ();
|
|
|
|
line->type = type;
|
|
|
|
line->flags = flags;
|
|
|
|
line->when = time (NULL);
|
2015-04-25 13:41:10 +02:00
|
|
|
line->args = a;
|
2015-04-13 00:06:08 +02:00
|
|
|
|
|
|
|
LIST_APPEND_WITH_TAIL (buffer->lines, buffer->lines_tail, line);
|
|
|
|
buffer->lines_count++;
|
|
|
|
|
|
|
|
if (buffer == ctx->current_buffer)
|
2015-04-26 18:23:43 +02:00
|
|
|
buffer_line_display (ctx, line, false);
|
2015-04-17 21:48:56 +02:00
|
|
|
else if (!ctx->isolate_buffers &&
|
|
|
|
(buffer == ctx->global_buffer || buffer == ctx->server_buffer))
|
2015-04-26 18:23:43 +02:00
|
|
|
buffer_line_display (ctx, line, true);
|
2015-04-18 21:50:42 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer->unseen_messages_count++;
|
|
|
|
refresh_prompt (ctx);
|
|
|
|
}
|
2015-04-13 00:06:08 +02:00
|
|
|
}
|
|
|
|
|
2015-04-25 13:41:10 +02:00
|
|
|
#define buffer_send(ctx, buffer, type, flags, ...) \
|
|
|
|
buffer_send_internal ((ctx), (buffer), (type), (flags), \
|
|
|
|
(struct buffer_line_args) { __VA_ARGS__ })
|
|
|
|
|
|
|
|
#define buffer_send_status(ctx, buffer, ...) \
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_STATUS, 0, \
|
|
|
|
.text = xstrdup_printf (__VA_ARGS__))
|
|
|
|
#define buffer_send_error(ctx, buffer, ...) \
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_ERROR, 0, \
|
|
|
|
.text = xstrdup_printf (__VA_ARGS__))
|
2015-04-17 22:10:43 +02:00
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
static struct buffer *
|
|
|
|
buffer_by_name (struct app_context *ctx, const char *name)
|
|
|
|
{
|
|
|
|
return str_map_find (&ctx->buffers_by_name, name);
|
|
|
|
}
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
static void
|
2015-04-13 00:06:08 +02:00
|
|
|
buffer_add (struct app_context *ctx, struct buffer *buffer)
|
2015-04-12 04:52:39 +02:00
|
|
|
{
|
2015-04-13 00:06:08 +02:00
|
|
|
hard_assert (!buffer_by_name (ctx, buffer->name));
|
|
|
|
|
|
|
|
str_map_set (&ctx->buffers_by_name, buffer->name, buffer);
|
|
|
|
LIST_APPEND_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer);
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
// In theory this can't cause changes in the prompt
|
|
|
|
refresh_prompt (ctx);
|
2015-04-13 00:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
buffer_remove (struct app_context *ctx, struct buffer *buffer)
|
|
|
|
{
|
|
|
|
hard_assert (buffer != ctx->current_buffer);
|
|
|
|
|
2015-04-17 21:29:18 +02:00
|
|
|
// TODO: part from the channel if needed
|
|
|
|
|
2015-04-15 02:26:48 +02:00
|
|
|
// 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
|
2015-04-15 02:10:21 +02:00
|
|
|
if (buffer->history)
|
|
|
|
{
|
|
|
|
// See buffer_activate() 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);
|
2015-04-24 23:30:48 +02:00
|
|
|
free (buffer->history);
|
2015-04-15 02:10:21 +02:00
|
|
|
rl_clear_history ();
|
|
|
|
|
|
|
|
history_set_history_state (state);
|
|
|
|
free (state);
|
|
|
|
}
|
2015-04-15 02:26:48 +02:00
|
|
|
#endif // RL_READLINE_VERSION
|
2015-04-15 02:10:21 +02:00
|
|
|
|
2015-04-20 23:37:34 +02:00
|
|
|
// And make sure to unlink the buffer from "irc_buffer_map"
|
|
|
|
if (buffer->channel)
|
|
|
|
str_map_set (&ctx->irc_buffer_map, buffer->channel->name, NULL);
|
|
|
|
if (buffer->user)
|
|
|
|
str_map_set (&ctx->irc_buffer_map, buffer->user->nickname, NULL);
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
str_map_set (&ctx->buffers_by_name, buffer->name, NULL);
|
|
|
|
LIST_UNLINK_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer);
|
|
|
|
buffer_destroy (buffer);
|
|
|
|
|
2015-04-27 22:51:40 +02:00
|
|
|
if (buffer == ctx->last_buffer)
|
|
|
|
ctx->last_buffer = NULL;
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
// It's not a good idea to remove these buffers, but it's even a worse
|
|
|
|
// one to leave the pointers point to invalid memory
|
|
|
|
if (buffer == ctx->global_buffer)
|
|
|
|
ctx->global_buffer = NULL;
|
|
|
|
if (buffer == ctx->server_buffer)
|
|
|
|
ctx->server_buffer = NULL;
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
refresh_prompt (ctx);
|
2015-04-13 00:06:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
buffer_activate (struct app_context *ctx, struct buffer *buffer)
|
|
|
|
{
|
2015-04-15 02:10:21 +02:00
|
|
|
if (ctx->current_buffer == buffer)
|
|
|
|
return;
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
print_status ("%s", buffer->name);
|
|
|
|
|
|
|
|
// That is, minus the buffer switch line and the readline prompt
|
2015-04-15 02:10:21 +02:00
|
|
|
int to_display = MAX (10, ctx->lines - 2);
|
|
|
|
struct buffer_line *line = buffer->lines_tail;
|
|
|
|
while (line && line->prev && --to_display > 0)
|
|
|
|
line = line->prev;
|
|
|
|
|
|
|
|
// Once we've found where we want to start with the backlog, print it
|
|
|
|
for (; line; line = line->next)
|
2015-04-26 18:23:43 +02:00
|
|
|
buffer_line_display (ctx, line, false);
|
2015-04-18 21:50:42 +02:00
|
|
|
buffer->unseen_messages_count = 0;
|
2015-04-15 02:10:21 +02:00
|
|
|
|
|
|
|
// 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.
|
2015-04-13 00:06:08 +02:00
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
// 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 (ctx->current_buffer)
|
|
|
|
{
|
|
|
|
ctx->current_buffer->history = history_get_history_state ();
|
|
|
|
ctx->current_buffer->saved_line = rl_copy_text (0, rl_end);
|
|
|
|
ctx->current_buffer->saved_point = rl_point;
|
|
|
|
ctx->current_buffer->saved_mark = rl_mark;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// Just throw it away; there should always be an active buffer however
|
2015-04-15 02:40:11 +02:00
|
|
|
#if RL_READLINE_VERSION >= 0x0603
|
2015-04-15 02:10:21 +02:00
|
|
|
rl_clear_history ();
|
2015-04-15 02:40:11 +02:00
|
|
|
#else // RL_READLINE_VERSION < 0x0603
|
|
|
|
// At least something... this may leak undo entries
|
|
|
|
clear_history ();
|
|
|
|
#endif // RL_READLINE_VERSION < 0x0603
|
2015-04-15 02:10:21 +02:00
|
|
|
|
|
|
|
// Restore the target buffer's history
|
|
|
|
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;
|
|
|
|
history_set_history_state (state);
|
|
|
|
free (state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to restore the target buffer's readline state
|
|
|
|
if (buffer->saved_line)
|
|
|
|
{
|
|
|
|
rl_replace_line (buffer->saved_line, 0);
|
|
|
|
rl_point = buffer->saved_point;
|
|
|
|
rl_mark = buffer->saved_mark;
|
|
|
|
free (buffer->saved_line);
|
2015-04-24 22:50:53 +02:00
|
|
|
buffer->saved_line = 0;
|
2015-04-15 02:10:21 +02:00
|
|
|
|
|
|
|
if (ctx->readline_prompt_shown)
|
|
|
|
rl_redisplay ();
|
|
|
|
}
|
|
|
|
|
2015-04-18 03:25:10 +02:00
|
|
|
// Now at last we can switch the pointers
|
2015-04-27 22:51:40 +02:00
|
|
|
ctx->last_buffer = ctx->current_buffer;
|
2015-04-18 03:25:10 +02:00
|
|
|
ctx->current_buffer = buffer;
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
refresh_prompt (ctx);
|
2015-04-13 00:06:08 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 22:51:30 +02:00
|
|
|
static void
|
|
|
|
buffer_merge (struct app_context *ctx,
|
|
|
|
struct buffer *buffer, struct buffer *merged)
|
|
|
|
{
|
|
|
|
// TODO: try to merge the buffers as best as we can
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
buffer_rename (struct app_context *ctx,
|
|
|
|
struct buffer *buffer, const char *new_name)
|
|
|
|
{
|
|
|
|
hard_assert (buffer->type == BUFFER_PM);
|
|
|
|
|
|
|
|
struct buffer *collision =
|
|
|
|
str_map_find (&ctx->irc_buffer_map, new_name);
|
|
|
|
if (collision)
|
|
|
|
{
|
|
|
|
// TODO: use full weechat-style buffer names
|
|
|
|
// to prevent name collisions with the global buffer
|
|
|
|
hard_assert (collision->type == BUFFER_PM);
|
|
|
|
|
|
|
|
// When there's a collision, there's not much else we can do
|
|
|
|
// other than somehow trying to merge them
|
|
|
|
buffer_merge (ctx, collision, buffer);
|
|
|
|
// TODO: log a status message about the merge
|
|
|
|
if (ctx->current_buffer == buffer)
|
|
|
|
buffer_activate (ctx, collision);
|
|
|
|
buffer_remove (ctx, buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise we just rename the buffer and that's it
|
|
|
|
str_map_set (&ctx->buffers_by_name, buffer->name, NULL);
|
|
|
|
str_map_set (&ctx->buffers_by_name, new_name, buffer);
|
|
|
|
|
|
|
|
free (buffer->name);
|
|
|
|
buffer->name = xstrdup (new_name);
|
|
|
|
|
|
|
|
// We might have renamed the current buffer
|
|
|
|
refresh_prompt (ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 21:21:30 +02:00
|
|
|
static struct buffer *
|
|
|
|
buffer_at_index (struct app_context *ctx, int n)
|
2015-04-16 21:16:10 +02:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
LIST_FOR_EACH (struct buffer, iter, ctx->buffers)
|
|
|
|
if (++i == n)
|
2015-04-17 21:21:30 +02:00
|
|
|
return iter;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct buffer *
|
|
|
|
buffer_next (struct app_context *ctx, int count)
|
|
|
|
{
|
|
|
|
struct buffer *new_buffer = ctx->current_buffer;
|
|
|
|
while (count-- > 0)
|
|
|
|
if (!(new_buffer = new_buffer->next))
|
|
|
|
new_buffer = ctx->buffers;
|
|
|
|
return new_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct buffer *
|
|
|
|
buffer_previous (struct app_context *ctx, int count)
|
|
|
|
{
|
|
|
|
struct buffer *new_buffer = ctx->current_buffer;
|
|
|
|
while (count-- > 0)
|
|
|
|
if (!(new_buffer = new_buffer->prev))
|
|
|
|
new_buffer = ctx->buffers_tail;
|
|
|
|
return new_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
buffer_goto (struct app_context *ctx, int n)
|
|
|
|
{
|
|
|
|
struct buffer *buffer = buffer_at_index (ctx, n);
|
|
|
|
if (!buffer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buffer_activate (ctx, buffer);
|
|
|
|
return true;
|
2015-04-16 21:16:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-17 20:45:54 +02:00
|
|
|
static int
|
|
|
|
buffer_get_index (struct app_context *ctx, struct buffer *buffer)
|
|
|
|
{
|
|
|
|
int index = 1;
|
|
|
|
LIST_FOR_EACH (struct buffer, iter, ctx->buffers)
|
|
|
|
{
|
|
|
|
if (iter == buffer)
|
|
|
|
return index;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
static void
|
|
|
|
init_buffers (struct app_context *ctx)
|
|
|
|
{
|
|
|
|
// At the moment we have only two global everpresent buffers
|
2015-04-12 04:52:39 +02:00
|
|
|
struct buffer *global = ctx->global_buffer = buffer_new ();
|
|
|
|
struct buffer *server = ctx->server_buffer = buffer_new ();
|
|
|
|
|
|
|
|
global->type = BUFFER_GLOBAL;
|
|
|
|
global->name = xstrdup (PROGRAM_NAME);
|
|
|
|
|
|
|
|
server->type = BUFFER_SERVER;
|
|
|
|
server->name = xstrdup (str_map_find (&ctx->config, "irc_host"));
|
|
|
|
|
|
|
|
LIST_APPEND_WITH_TAIL (ctx->buffers, ctx->buffers_tail, global);
|
|
|
|
LIST_APPEND_WITH_TAIL (ctx->buffers, ctx->buffers_tail, server);
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:58:46 +02:00
|
|
|
// --- Users, channels ---------------------------------------------------------
|
|
|
|
|
2015-04-21 00:04:34 +02:00
|
|
|
static void
|
|
|
|
irc_user_on_destroy (void *object, void *user_data)
|
|
|
|
{
|
|
|
|
struct user *user = object;
|
|
|
|
struct app_context *ctx = user_data;
|
|
|
|
str_map_set (&ctx->irc_users, user->nickname, NULL);
|
|
|
|
}
|
|
|
|
|
2015-04-23 02:59:58 +02:00
|
|
|
static struct user *
|
2015-04-21 00:04:34 +02:00
|
|
|
irc_make_user (struct app_context *ctx, char *nickname)
|
|
|
|
{
|
|
|
|
hard_assert (!str_map_find (&ctx->irc_users, nickname));
|
|
|
|
|
|
|
|
struct user *user = user_new ();
|
|
|
|
user->on_destroy = irc_user_on_destroy;
|
|
|
|
user->user_data = ctx;
|
|
|
|
user->nickname = nickname;
|
|
|
|
str_map_set (&ctx->irc_users, user->nickname, user);
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2015-04-23 03:00:19 +02:00
|
|
|
static struct buffer *
|
2015-04-23 03:10:34 +02:00
|
|
|
irc_get_or_make_user_buffer (struct app_context *ctx, const char *nickname)
|
2015-04-23 03:00:19 +02:00
|
|
|
{
|
|
|
|
struct buffer *buffer = str_map_find (&ctx->irc_buffer_map, nickname);
|
|
|
|
if (buffer)
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
struct user *user = str_map_find (&ctx->irc_users, nickname);
|
|
|
|
if (!user)
|
|
|
|
user = irc_make_user (ctx, xstrdup (nickname));
|
2015-04-24 23:30:48 +02:00
|
|
|
else
|
|
|
|
user = user_ref (user);
|
2015-04-23 03:00:19 +02:00
|
|
|
|
|
|
|
// Open a new buffer for the user
|
|
|
|
buffer = buffer_new ();
|
|
|
|
buffer->type = BUFFER_PM;
|
|
|
|
buffer->name = xstrdup (nickname);
|
2015-04-24 23:30:48 +02:00
|
|
|
buffer->user = user;
|
2015-04-23 03:00:19 +02:00
|
|
|
LIST_APPEND_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer);
|
|
|
|
str_map_set (&ctx->irc_buffer_map, user->nickname, buffer);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2015-04-25 00:36:02 +02:00
|
|
|
static void
|
|
|
|
irc_channel_unlink_user
|
|
|
|
(struct channel *channel, struct channel_user *channel_user)
|
|
|
|
{
|
|
|
|
// First destroy the user's weak references to the channel
|
|
|
|
struct user *user = channel_user->user;
|
|
|
|
LIST_FOR_EACH (struct user_channel, iter, user->channels)
|
|
|
|
if (iter->channel == channel)
|
|
|
|
{
|
|
|
|
LIST_UNLINK (user->channels, iter);
|
|
|
|
user_channel_destroy (iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then just unlink the user from the channel
|
|
|
|
LIST_UNLINK (channel->users, channel_user);
|
|
|
|
channel_user_destroy (channel_user);
|
|
|
|
}
|
|
|
|
|
2015-04-21 00:04:34 +02:00
|
|
|
static void
|
|
|
|
irc_channel_on_destroy (void *object, void *user_data)
|
|
|
|
{
|
|
|
|
struct channel *channel = object;
|
|
|
|
struct app_context *ctx = user_data;
|
2015-04-25 00:36:02 +02:00
|
|
|
LIST_FOR_EACH (struct channel_user, iter, channel->users)
|
|
|
|
irc_channel_unlink_user (channel, iter);
|
2015-04-21 00:04:34 +02:00
|
|
|
str_map_set (&ctx->irc_channels, channel->name, NULL);
|
|
|
|
}
|
|
|
|
|
2015-04-23 02:59:58 +02:00
|
|
|
static struct channel *
|
2015-04-21 00:04:34 +02:00
|
|
|
irc_make_channel (struct app_context *ctx, char *name)
|
|
|
|
{
|
|
|
|
hard_assert (!str_map_find (&ctx->irc_channels, name));
|
|
|
|
|
|
|
|
struct channel *channel = channel_new ();
|
|
|
|
channel->on_destroy = irc_channel_on_destroy;
|
|
|
|
channel->user_data = ctx;
|
|
|
|
channel->name = name;
|
|
|
|
channel->mode = xstrdup ("");
|
|
|
|
channel->topic = NULL;
|
|
|
|
str_map_set (&ctx->irc_channels, channel->name, channel);
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
2015-04-21 21:36:16 +02:00
|
|
|
static void
|
2015-04-25 00:36:02 +02:00
|
|
|
irc_remove_user_from_channel (struct user *user, struct channel *channel)
|
2015-04-21 21:36:16 +02:00
|
|
|
{
|
|
|
|
LIST_FOR_EACH (struct channel_user, iter, channel->users)
|
|
|
|
if (iter->user == user)
|
2015-04-25 00:36:02 +02:00
|
|
|
irc_channel_unlink_user (channel, iter);
|
2015-04-21 21:36:16 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
// --- Supporting code ---------------------------------------------------------
|
|
|
|
|
2015-04-19 21:58:46 +02:00
|
|
|
static char *
|
|
|
|
irc_cut_nickname (const char *prefix)
|
|
|
|
{
|
|
|
|
return xstrndup (prefix, strcspn (prefix, "!@"));
|
|
|
|
}
|
|
|
|
|
2015-04-19 23:05:49 +02:00
|
|
|
static const char *
|
|
|
|
irc_find_userhost (const char *prefix)
|
|
|
|
{
|
|
|
|
const char *p = strchr (prefix, '!');
|
|
|
|
return p ? p + 1 : NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:58:46 +02:00
|
|
|
static bool
|
|
|
|
irc_is_this_us (struct app_context *ctx, const char *prefix)
|
|
|
|
{
|
|
|
|
char *nick = irc_cut_nickname (prefix);
|
2015-04-20 22:09:56 +02:00
|
|
|
bool result = !irc_strcmp (nick, ctx->irc_user->nickname);
|
2015-04-19 21:58:46 +02:00
|
|
|
free (nick);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
irc_is_channel (struct app_context *ctx, const char *ident)
|
|
|
|
{
|
|
|
|
(void) ctx; // TODO: parse prefixes from server features
|
|
|
|
|
|
|
|
return *ident && !!strchr ("#&+!", *ident);
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
static void
|
|
|
|
irc_shutdown (struct app_context *ctx)
|
|
|
|
{
|
|
|
|
// TODO: set a timer after which we cut the connection?
|
|
|
|
// Generally non-critical
|
|
|
|
if (ctx->ssl)
|
|
|
|
soft_assert (SSL_shutdown (ctx->ssl) != -1);
|
|
|
|
else
|
|
|
|
soft_assert (shutdown (ctx->irc_fd, SHUT_WR) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
try_finish_quit (struct app_context *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->quitting && ctx->irc_fd == -1)
|
|
|
|
ctx->polling = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initiate_quit (struct app_context *ctx)
|
|
|
|
{
|
2015-04-18 03:25:10 +02:00
|
|
|
// First get rid of readline
|
|
|
|
if (ctx->readline_prompt_shown)
|
2015-04-18 16:03:50 +02:00
|
|
|
{
|
|
|
|
app_readline_erase_to_bol (ctx->readline_prompt);
|
|
|
|
ctx->readline_prompt_shown = false;
|
|
|
|
}
|
2015-04-18 03:25:10 +02:00
|
|
|
|
2015-04-18 16:10:11 +02:00
|
|
|
// This is okay as long as we're not called from within readline
|
|
|
|
rl_callback_handler_remove ();
|
|
|
|
|
2015-04-18 03:25:10 +02:00
|
|
|
// Initiate a connection close
|
2015-04-18 17:51:28 +02:00
|
|
|
buffer_send_status (ctx, ctx->global_buffer, "Shutting down");
|
2015-04-12 04:52:39 +02:00
|
|
|
if (ctx->irc_fd != -1)
|
2015-04-19 21:34:33 +02:00
|
|
|
// XXX: when we go async, we'll have to flush output buffers first
|
2015-04-12 04:52:39 +02:00
|
|
|
irc_shutdown (ctx);
|
|
|
|
|
|
|
|
ctx->quitting = true;
|
|
|
|
try_finish_quit (ctx);
|
|
|
|
}
|
|
|
|
|
2015-04-25 14:53:29 +02:00
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
static bool irc_send (struct app_context *ctx,
|
|
|
|
const char *format, ...) ATTRIBUTE_PRINTF (2, 3);
|
|
|
|
|
|
|
|
static bool
|
|
|
|
irc_send (struct app_context *ctx, const char *format, ...)
|
|
|
|
{
|
2015-04-25 14:53:29 +02:00
|
|
|
if (!soft_assert (ctx->irc_fd != -1))
|
|
|
|
{
|
|
|
|
print_debug ("tried sending a message to a dead server connection");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
va_list ap;
|
2015-04-25 14:53:29 +02:00
|
|
|
va_start (ap, format);
|
|
|
|
struct str str;
|
|
|
|
str_init (&str);
|
|
|
|
str_append_vprintf (&str, format, ap);
|
|
|
|
va_end (ap);
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
if (g_debug_mode)
|
|
|
|
{
|
|
|
|
struct app_readline_state state;
|
|
|
|
if (ctx->readline_prompt_shown)
|
|
|
|
app_readline_hide (&state);
|
|
|
|
|
2015-04-25 14:53:29 +02:00
|
|
|
char *term = irc_to_term (ctx, str.str);
|
|
|
|
fprintf (stderr, "[IRC] <== \"%s\"\n", term);
|
|
|
|
free (term);
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
if (ctx->readline_prompt_shown)
|
|
|
|
app_readline_restore (&state, ctx->readline_prompt);
|
|
|
|
}
|
|
|
|
str_append (&str, "\r\n");
|
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
if (ctx->ssl)
|
|
|
|
{
|
|
|
|
// TODO: call SSL_get_error() to detect if a clean shutdown has occured
|
|
|
|
if (SSL_write (ctx->ssl, str.str, str.len) != (int) str.len)
|
|
|
|
{
|
2015-04-27 01:36:33 +02:00
|
|
|
LOG_FUNC_FAILURE ("SSL_write",
|
2015-04-12 04:52:39 +02:00
|
|
|
ERR_error_string (ERR_get_error (), NULL));
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (write (ctx->irc_fd, str.str, str.len) != (ssize_t) str.len)
|
|
|
|
{
|
2015-04-27 01:36:33 +02:00
|
|
|
LOG_LIBC_FAILURE ("write");
|
2015-04-12 04:52:39 +02:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
str_free (&str);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
irc_get_boolean_from_config
|
|
|
|
(struct app_context *ctx, const char *name, bool *value, struct error **e)
|
|
|
|
{
|
|
|
|
const char *str = str_map_find (&ctx->config, name);
|
|
|
|
hard_assert (str != NULL);
|
|
|
|
|
|
|
|
if (set_boolean_if_valid (value, str))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error_set (e, "invalid configuration value for `%s'", name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
irc_initialize_ssl_ctx (struct app_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
// XXX: maybe we should call SSL_CTX_set_options() for some workarounds
|
|
|
|
|
|
|
|
bool verify;
|
|
|
|
if (!irc_get_boolean_from_config (ctx, "ssl_verify", &verify, e))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!verify)
|
|
|
|
SSL_CTX_set_verify (ctx->ssl_ctx, SSL_VERIFY_NONE, NULL);
|
|
|
|
|
|
|
|
const char *ca_file = str_map_find (&ctx->config, "ca_file");
|
|
|
|
const char *ca_path = str_map_find (&ctx->config, "ca_path");
|
|
|
|
|
|
|
|
struct error *error = NULL;
|
|
|
|
if (ca_file || ca_path)
|
|
|
|
{
|
|
|
|
if (SSL_CTX_load_verify_locations (ctx->ssl_ctx, ca_file, ca_path))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error_set (&error, "%s: %s",
|
2015-04-18 17:51:28 +02:00
|
|
|
"Failed to set locations for the CA certificate bundle",
|
2015-04-12 04:52:39 +02:00
|
|
|
ERR_reason_error_string (ERR_get_error ()));
|
|
|
|
goto ca_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SSL_CTX_set_default_verify_paths (ctx->ssl_ctx))
|
|
|
|
{
|
|
|
|
error_set (&error, "%s: %s",
|
2015-04-18 17:51:28 +02:00
|
|
|
"Couldn't load the default CA certificate bundle",
|
2015-04-12 04:52:39 +02:00
|
|
|
ERR_reason_error_string (ERR_get_error ()));
|
|
|
|
goto ca_error;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ca_error:
|
|
|
|
if (verify)
|
|
|
|
{
|
|
|
|
error_propagate (e, error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only inform the user if we're not actually verifying
|
2015-04-18 17:50:56 +02:00
|
|
|
buffer_send_error (ctx, ctx->server_buffer, "%s", error->message);
|
2015-04-12 04:52:39 +02:00
|
|
|
error_free (error);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
irc_initialize_ssl (struct app_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
const char *error_info = NULL;
|
|
|
|
ctx->ssl_ctx = SSL_CTX_new (SSLv23_client_method ());
|
|
|
|
if (!ctx->ssl_ctx)
|
|
|
|
goto error_ssl_1;
|
|
|
|
if (!irc_initialize_ssl_ctx (ctx, e))
|
|
|
|
goto error_ssl_2;
|
|
|
|
|
|
|
|
ctx->ssl = SSL_new (ctx->ssl_ctx);
|
|
|
|
if (!ctx->ssl)
|
|
|
|
goto error_ssl_2;
|
|
|
|
|
|
|
|
const char *ssl_cert = str_map_find (&ctx->config, "ssl_cert");
|
|
|
|
if (ssl_cert)
|
|
|
|
{
|
|
|
|
char *path = resolve_config_filename (ssl_cert);
|
|
|
|
if (!path)
|
2015-04-18 17:50:56 +02:00
|
|
|
buffer_send_error (ctx, ctx->global_buffer,
|
|
|
|
"%s: %s", "Cannot open file", ssl_cert);
|
2015-04-12 04:52:39 +02:00
|
|
|
// XXX: perhaps we should read the file ourselves for better messages
|
|
|
|
else if (!SSL_use_certificate_file (ctx->ssl, path, SSL_FILETYPE_PEM)
|
|
|
|
|| !SSL_use_PrivateKey_file (ctx->ssl, path, SSL_FILETYPE_PEM))
|
2015-04-18 17:50:56 +02:00
|
|
|
buffer_send_error (ctx, ctx->global_buffer,
|
|
|
|
"%s: %s", "Setting the SSL client certificate failed",
|
2015-04-12 04:52:39 +02:00
|
|
|
ERR_error_string (ERR_get_error (), NULL));
|
|
|
|
free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_set_connect_state (ctx->ssl);
|
|
|
|
if (!SSL_set_fd (ctx->ssl, ctx->irc_fd))
|
|
|
|
goto error_ssl_3;
|
|
|
|
// Avoid SSL_write() returning SSL_ERROR_WANT_READ
|
|
|
|
SSL_set_mode (ctx->ssl, SSL_MODE_AUTO_RETRY);
|
|
|
|
|
|
|
|
switch (xssl_get_error (ctx->ssl, SSL_connect (ctx->ssl), &error_info))
|
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
return true;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
error_info = "server closed the connection";
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_ssl_3:
|
|
|
|
SSL_free (ctx->ssl);
|
|
|
|
ctx->ssl = NULL;
|
|
|
|
error_ssl_2:
|
|
|
|
SSL_CTX_free (ctx->ssl_ctx);
|
|
|
|
ctx->ssl_ctx = NULL;
|
|
|
|
error_ssl_1:
|
|
|
|
// XXX: these error strings are really nasty; also there could be
|
|
|
|
// multiple errors on the OpenSSL stack.
|
|
|
|
if (!error_info)
|
|
|
|
error_info = ERR_error_string (ERR_get_error (), NULL);
|
|
|
|
error_set (e, "%s: %s", "could not initialize SSL", error_info);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
irc_establish_connection (struct app_context *ctx,
|
|
|
|
const char *host, const char *port, struct error **e)
|
|
|
|
{
|
|
|
|
struct addrinfo gai_hints, *gai_result, *gai_iter;
|
|
|
|
memset (&gai_hints, 0, sizeof gai_hints);
|
|
|
|
gai_hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
int err = getaddrinfo (host, port, &gai_hints, &gai_result);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
error_set (e, "%s: %s: %s",
|
|
|
|
"connection failed", "getaddrinfo", gai_strerror (err));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sockfd;
|
|
|
|
for (gai_iter = gai_result; gai_iter; gai_iter = gai_iter->ai_next)
|
|
|
|
{
|
|
|
|
sockfd = socket (gai_iter->ai_family,
|
|
|
|
gai_iter->ai_socktype, gai_iter->ai_protocol);
|
|
|
|
if (sockfd == -1)
|
|
|
|
continue;
|
|
|
|
set_cloexec (sockfd);
|
|
|
|
|
|
|
|
int yes = 1;
|
|
|
|
soft_assert (setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE,
|
|
|
|
&yes, sizeof yes) != -1);
|
|
|
|
|
|
|
|
const char *real_host = host;
|
|
|
|
|
|
|
|
// Let's try to resolve the address back into a real hostname;
|
|
|
|
// we don't really need this, so we can let it quietly fail
|
|
|
|
char buf[NI_MAXHOST];
|
|
|
|
err = getnameinfo (gai_iter->ai_addr, gai_iter->ai_addrlen,
|
|
|
|
buf, sizeof buf, NULL, 0, NI_NUMERICHOST);
|
|
|
|
if (err)
|
2015-04-27 01:36:33 +02:00
|
|
|
LOG_FUNC_FAILURE ("getnameinfo", gai_strerror (err));
|
2015-04-12 04:52:39 +02:00
|
|
|
else
|
|
|
|
real_host = buf;
|
|
|
|
|
|
|
|
char *address = format_host_port_pair (real_host, port);
|
2015-04-18 03:25:10 +02:00
|
|
|
buffer_send_status (ctx, ctx->server_buffer,
|
2015-04-18 17:51:28 +02:00
|
|
|
"Connecting to %s...", address);
|
2015-04-12 04:52:39 +02:00
|
|
|
free (address);
|
|
|
|
|
|
|
|
if (!connect (sockfd, gai_iter->ai_addr, gai_iter->ai_addrlen))
|
|
|
|
break;
|
|
|
|
|
|
|
|
xclose (sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo (gai_result);
|
|
|
|
|
|
|
|
if (!gai_iter)
|
|
|
|
{
|
|
|
|
error_set (e, "connection failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->irc_fd = sockfd;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
// --- More readline funky stuff -----------------------------------------------
|
|
|
|
|
2015-04-15 16:18:43 +02:00
|
|
|
static char *
|
2015-04-18 22:09:05 +02:00
|
|
|
make_unseen_prefix (struct app_context *ctx)
|
2015-04-15 16:18:43 +02:00
|
|
|
{
|
|
|
|
struct str active_buffers;
|
|
|
|
str_init (&active_buffers);
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
LIST_FOR_EACH (struct buffer, iter, ctx->buffers)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
if (!iter->unseen_messages_count)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (active_buffers.len)
|
|
|
|
str_append_c (&active_buffers, ',');
|
|
|
|
str_append_printf (&active_buffers, "%zu", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (active_buffers.len)
|
|
|
|
return str_steal (&active_buffers);
|
|
|
|
|
|
|
|
str_free (&active_buffers);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
static void
|
2015-04-17 21:40:08 +02:00
|
|
|
make_prompt (struct app_context *ctx, struct str *output)
|
2015-04-12 04:52:39 +02:00
|
|
|
{
|
2015-04-17 21:40:08 +02:00
|
|
|
struct buffer *buffer = ctx->current_buffer;
|
|
|
|
if (!soft_assert (buffer))
|
|
|
|
return;
|
2015-04-15 16:18:43 +02:00
|
|
|
|
2015-04-17 21:40:08 +02:00
|
|
|
str_append_c (output, '[');
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-18 22:09:05 +02:00
|
|
|
char *unseen_prefix = make_unseen_prefix (ctx);
|
2015-04-17 21:40:08 +02:00
|
|
|
if (unseen_prefix)
|
|
|
|
str_append_printf (output, "(%s) ", unseen_prefix);
|
|
|
|
free (unseen_prefix);
|
2015-04-15 16:54:05 +02:00
|
|
|
|
2015-04-17 21:40:08 +02:00
|
|
|
str_append_printf (output, "%d:%s",
|
|
|
|
buffer_get_index (ctx, buffer), buffer->name);
|
2015-04-19 02:12:59 +02:00
|
|
|
if (buffer->type == BUFFER_CHANNEL && *buffer->channel->mode)
|
|
|
|
str_append_printf (output, "(%s)", buffer->channel->mode);
|
2015-04-15 16:54:05 +02:00
|
|
|
|
2015-04-17 21:40:08 +02:00
|
|
|
if (buffer != ctx->global_buffer)
|
|
|
|
{
|
|
|
|
str_append_c (output, ' ');
|
2015-04-26 18:24:19 +02:00
|
|
|
if (ctx->irc_fd == -1)
|
2015-04-19 22:47:45 +02:00
|
|
|
str_append (output, "(disconnected)");
|
|
|
|
else
|
|
|
|
{
|
2015-04-20 22:09:56 +02:00
|
|
|
str_append (output, ctx->irc_user->nickname);
|
2015-04-19 22:47:45 +02:00
|
|
|
if (*ctx->irc_user_mode)
|
|
|
|
str_append_printf (output, "(%s)", ctx->irc_user_mode);
|
|
|
|
}
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
2015-04-17 21:40:08 +02:00
|
|
|
|
|
|
|
str_append_c (output, ']');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
refresh_prompt (struct app_context *ctx)
|
|
|
|
{
|
|
|
|
bool have_attributes = !!get_attribute_printer (stdout);
|
|
|
|
|
|
|
|
struct str prompt;
|
|
|
|
str_init (&prompt);
|
|
|
|
make_prompt (ctx, &prompt);
|
|
|
|
str_append_c (&prompt, ' ');
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
// After building the new prompt, replace the old one
|
|
|
|
free (ctx->readline_prompt);
|
|
|
|
|
|
|
|
if (!have_attributes)
|
|
|
|
ctx->readline_prompt = xstrdup (prompt.str);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// XXX: to be completely correct, we should use tputs, but we cannot
|
|
|
|
const char *prompt_attrs = str_map_find (&ctx->config, ATTR_PROMPT);
|
|
|
|
const char *reset_attrs = str_map_find (&ctx->config, ATTR_RESET);
|
|
|
|
ctx->readline_prompt = xstrdup_printf ("%c%s%c%s%c%s%c",
|
|
|
|
RL_PROMPT_START_IGNORE, prompt_attrs, RL_PROMPT_END_IGNORE,
|
|
|
|
prompt.str,
|
|
|
|
RL_PROMPT_START_IGNORE, reset_attrs, RL_PROMPT_END_IGNORE);
|
|
|
|
}
|
|
|
|
str_free (&prompt);
|
|
|
|
|
2015-04-18 03:25:10 +02:00
|
|
|
// First reset the prompt to work around a bug in readline
|
|
|
|
rl_set_prompt ("");
|
|
|
|
if (ctx->readline_prompt_shown)
|
|
|
|
rl_redisplay ();
|
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
rl_set_prompt (ctx->readline_prompt);
|
|
|
|
if (ctx->readline_prompt_shown)
|
|
|
|
rl_redisplay ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
on_readline_goto_buffer (int count, int key)
|
|
|
|
{
|
|
|
|
(void) count;
|
|
|
|
|
2015-04-18 17:34:35 +02:00
|
|
|
int n = UNMETA (key) - '0';
|
2015-04-15 02:10:21 +02:00
|
|
|
if (n < 0 || n > 9)
|
|
|
|
return 0;
|
|
|
|
|
2015-04-27 22:51:40 +02:00
|
|
|
// There's no buffer zero
|
|
|
|
if (n == 0)
|
|
|
|
n = 10;
|
|
|
|
|
2015-04-15 16:54:05 +02:00
|
|
|
struct app_context *ctx = g_ctx;
|
2015-04-27 22:51:40 +02:00
|
|
|
if (ctx->last_buffer && buffer_get_index (ctx, ctx->current_buffer) == n)
|
|
|
|
// Fast switching between two buffers
|
|
|
|
buffer_activate (ctx, ctx->last_buffer);
|
|
|
|
else if (!buffer_goto (ctx, n == 0 ? 10 : n))
|
2015-04-16 21:16:10 +02:00
|
|
|
rl_ding ();
|
2015-04-15 02:10:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
on_readline_previous_buffer (int count, int key)
|
|
|
|
{
|
|
|
|
(void) key;
|
|
|
|
|
2015-04-15 16:54:05 +02:00
|
|
|
struct app_context *ctx = g_ctx;
|
2015-04-17 23:18:07 +02:00
|
|
|
if (ctx->current_buffer)
|
|
|
|
buffer_activate (ctx, buffer_previous (ctx, count));
|
2015-04-15 02:10:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
on_readline_next_buffer (int count, int key)
|
|
|
|
{
|
|
|
|
(void) key;
|
|
|
|
|
2015-04-15 16:54:05 +02:00
|
|
|
struct app_context *ctx = g_ctx;
|
2015-04-17 23:18:07 +02:00
|
|
|
if (ctx->current_buffer)
|
|
|
|
buffer_activate (ctx, buffer_next (ctx, count));
|
2015-04-15 02:10:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:03:50 +02:00
|
|
|
static int
|
|
|
|
on_readline_return (int count, int key)
|
|
|
|
{
|
|
|
|
(void) count;
|
|
|
|
(void) key;
|
|
|
|
|
|
|
|
struct app_context *ctx = g_ctx;
|
|
|
|
|
|
|
|
// Let readline pass the line to our input handler
|
|
|
|
rl_done = 1;
|
|
|
|
|
|
|
|
// Save readline state
|
|
|
|
int saved_point = rl_point;
|
|
|
|
int saved_mark = rl_mark;
|
|
|
|
char *saved_line = rl_copy_text (0, rl_end);
|
|
|
|
|
|
|
|
// Erase the entire line from screen
|
|
|
|
rl_set_prompt ("");
|
|
|
|
rl_replace_line ("", 0);
|
|
|
|
rl_redisplay ();
|
|
|
|
ctx->readline_prompt_shown = false;
|
|
|
|
|
|
|
|
// Restore readline state
|
|
|
|
rl_set_prompt (ctx->readline_prompt);
|
|
|
|
rl_replace_line (saved_line, 0);
|
|
|
|
rl_point = saved_point;
|
|
|
|
rl_mark = saved_mark;
|
|
|
|
free (saved_line);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-18 17:34:35 +02:00
|
|
|
static void
|
|
|
|
app_readline_bind_meta (char key, rl_command_func_t cb)
|
|
|
|
{
|
2015-04-28 18:15:53 +02:00
|
|
|
// This one seems to actually work
|
2015-04-18 17:34:35 +02:00
|
|
|
char keyseq[] = { '\\', 'e', key, 0 };
|
|
|
|
rl_bind_keyseq (keyseq, cb);
|
2015-04-28 18:15:53 +02:00
|
|
|
#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), cb);
|
|
|
|
#endif
|
2015-04-18 17:34:35 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
static int
|
|
|
|
init_readline (void)
|
|
|
|
{
|
2015-04-18 17:34:35 +02:00
|
|
|
// XXX: maybe use rl_make_bare_keymap() and start from there;
|
2015-04-15 02:10:21 +02:00
|
|
|
// our dear user could potentionally rig things up in a way that might
|
|
|
|
// result in some funny unspecified behaviour
|
|
|
|
|
|
|
|
rl_add_defun ("previous-buffer", on_readline_previous_buffer, -1);
|
|
|
|
rl_add_defun ("next-buffer", on_readline_next_buffer, -1);
|
|
|
|
|
|
|
|
// Redefine M-0 through M-9 to switch buffers
|
|
|
|
for (int i = 0; i <= 9; i++)
|
2015-04-18 17:34:35 +02:00
|
|
|
app_readline_bind_meta ('0' + i, on_readline_goto_buffer);
|
2015-04-15 02:10:21 +02:00
|
|
|
|
2015-04-18 03:25:10 +02:00
|
|
|
rl_bind_keyseq ("\\C-p", rl_named_function ("previous-buffer"));
|
|
|
|
rl_bind_keyseq ("\\C-n", rl_named_function ("next-buffer"));
|
2015-04-18 17:34:35 +02:00
|
|
|
app_readline_bind_meta ('p', rl_named_function ("previous-history"));
|
|
|
|
app_readline_bind_meta ('n', rl_named_function ("next-history"));
|
2015-04-15 02:10:21 +02:00
|
|
|
|
2015-04-18 17:34:35 +02:00
|
|
|
// We need to hide the prompt first
|
2015-04-18 16:03:50 +02:00
|
|
|
rl_bind_key (RETURN, on_readline_return);
|
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
return 0;
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
|
|
|
|
2015-04-26 21:46:59 +02:00
|
|
|
// --- CTCP decoding -----------------------------------------------------------
|
|
|
|
|
|
|
|
#define CTCP_M_QUOTE '\020'
|
|
|
|
#define CTCP_X_DELIM '\001'
|
|
|
|
#define CTCP_X_QUOTE '\\'
|
|
|
|
|
|
|
|
struct ctcp_chunk
|
|
|
|
{
|
|
|
|
LIST_HEADER (struct ctcp_chunk)
|
|
|
|
|
|
|
|
bool is_extended; ///< Is this a tagged extended message?
|
|
|
|
struct str tag; ///< The tag, if any
|
|
|
|
struct str text; ///< Message contents
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ctcp_chunk *
|
|
|
|
ctcp_chunk_new (void)
|
|
|
|
{
|
|
|
|
struct ctcp_chunk *self = xcalloc (1, sizeof *self);
|
|
|
|
str_init (&self->tag);
|
|
|
|
str_init (&self->text);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ctcp_chunk_destroy (struct ctcp_chunk *self)
|
|
|
|
{
|
|
|
|
str_free (&self->tag);
|
|
|
|
str_free (&self->text);
|
|
|
|
free (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
|
|
|
static void
|
|
|
|
ctcp_low_level_decode (const char *message, struct str *output)
|
|
|
|
{
|
|
|
|
bool escape = false;
|
|
|
|
for (const char *p = message; *p; p++)
|
|
|
|
{
|
|
|
|
if (escape)
|
|
|
|
{
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case '0': str_append_c (output, '\0'); break;
|
|
|
|
case 'r': str_append_c (output, '\r'); break;
|
|
|
|
case 'n': str_append_c (output, '\n'); break;
|
|
|
|
default: str_append_c (output, *p);
|
|
|
|
}
|
|
|
|
escape = false;
|
|
|
|
}
|
|
|
|
else if (*p == CTCP_M_QUOTE)
|
|
|
|
escape = true;
|
|
|
|
else
|
|
|
|
str_append_c (output, *p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ctcp_intra_decode (const char *chunk, size_t len, struct str *output)
|
|
|
|
{
|
|
|
|
bool escape = false;
|
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
char c = chunk[i];
|
|
|
|
if (escape)
|
|
|
|
{
|
|
|
|
if (c == 'a')
|
|
|
|
str_append_c (output, CTCP_X_DELIM);
|
|
|
|
else
|
|
|
|
str_append_c (output, c);
|
|
|
|
escape = false;
|
|
|
|
}
|
|
|
|
else if (c == CTCP_X_QUOTE)
|
|
|
|
escape = true;
|
|
|
|
else
|
|
|
|
str_append_c (output, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ctcp_parse_tagged (const char *chunk, size_t len, struct ctcp_chunk *output)
|
|
|
|
{
|
|
|
|
// We may search for the space before doing the higher level decoding,
|
|
|
|
// as it doesn't concern space characters at all
|
|
|
|
size_t tag_end = len;
|
|
|
|
for (size_t i = 0; i < len; i++)
|
|
|
|
if (chunk[i] == ' ')
|
|
|
|
{
|
|
|
|
tag_end = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
output->is_extended = true;
|
|
|
|
ctcp_intra_decode (chunk, tag_end, &output->tag);
|
|
|
|
if (tag_end++ != len)
|
|
|
|
ctcp_intra_decode (chunk + tag_end, len - tag_end, &output->text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ctcp_chunk *
|
|
|
|
ctcp_parse (const char *message)
|
|
|
|
{
|
|
|
|
struct str m;
|
|
|
|
str_init (&m);
|
|
|
|
ctcp_low_level_decode (message, &m);
|
|
|
|
|
|
|
|
struct ctcp_chunk *result = NULL, *result_tail = NULL;
|
|
|
|
|
|
|
|
size_t start = 0;
|
|
|
|
bool in_ctcp = false;
|
|
|
|
for (size_t i = 0; i < m.len; i++)
|
|
|
|
{
|
|
|
|
char c = m.str[i];
|
|
|
|
if (c != CTCP_X_DELIM)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Remember the current state
|
|
|
|
size_t my_start = start;
|
|
|
|
bool my_is_ctcp = in_ctcp;
|
|
|
|
|
|
|
|
start = i + 1;
|
|
|
|
in_ctcp = !in_ctcp;
|
|
|
|
|
|
|
|
// Skip empty chunks
|
|
|
|
if (my_start == i)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct ctcp_chunk *chunk = ctcp_chunk_new ();
|
|
|
|
if (my_is_ctcp)
|
|
|
|
ctcp_parse_tagged (m.str + my_start, i - my_start, chunk);
|
|
|
|
else
|
|
|
|
ctcp_intra_decode (m.str + my_start, i - my_start, &chunk->text);
|
|
|
|
LIST_APPEND_WITH_TAIL (result, result_tail, chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish the last text part. We ignore unended tagged chunks.
|
|
|
|
if (!in_ctcp && start != m.len)
|
|
|
|
{
|
|
|
|
struct ctcp_chunk *chunk = ctcp_chunk_new ();
|
|
|
|
ctcp_intra_decode (m.str + start, m.len - start, &chunk->text);
|
|
|
|
LIST_APPEND_WITH_TAIL (result, result_tail, chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
str_free (&m);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ctcp_destroy (struct ctcp_chunk *list)
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH (struct ctcp_chunk, iter, list)
|
|
|
|
ctcp_chunk_destroy (iter);
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
// --- Input handling ----------------------------------------------------------
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
// TODO: we will need a proper mode parser; to be shared with kike
|
|
|
|
// TODO: we alse definitely need to parse server capability messages
|
|
|
|
|
2015-04-27 21:52:16 +02:00
|
|
|
static struct buffer *
|
|
|
|
irc_get_buffer_for_message (struct app_context *ctx,
|
|
|
|
const struct irc_message *msg, const char *target)
|
|
|
|
{
|
|
|
|
struct buffer *buffer = str_map_find (&ctx->irc_buffer_map, target);
|
|
|
|
if (irc_is_channel (ctx, target))
|
|
|
|
{
|
|
|
|
struct channel *channel = str_map_find (&ctx->irc_channels, target);
|
|
|
|
hard_assert ((channel && buffer) ||
|
|
|
|
(channel && !buffer) || (!channel && !buffer));
|
|
|
|
|
|
|
|
// This is weird
|
|
|
|
if (!channel)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (!buffer)
|
|
|
|
{
|
|
|
|
// Implying that the target is us
|
|
|
|
|
|
|
|
// Don't make user buffers for servers (they can send NOTICEs)
|
|
|
|
if (!irc_find_userhost (msg->prefix))
|
|
|
|
return ctx->server_buffer;
|
|
|
|
|
|
|
|
char *nickname = irc_cut_nickname (msg->prefix);
|
|
|
|
buffer = irc_get_or_make_user_buffer (ctx, nickname);
|
|
|
|
free (nickname);
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2015-04-27 22:40:33 +02:00
|
|
|
static bool
|
|
|
|
irc_is_highlight (struct app_context *ctx, const char *message)
|
|
|
|
{
|
|
|
|
// Well, this is rather crude but it should make most users happy.
|
|
|
|
// Ideally we could do this at least in proper Unicode.
|
|
|
|
char *copy = xstrdup (message);
|
|
|
|
for (char *p = copy; *p; p++)
|
|
|
|
*p = irc_tolower (*p);
|
|
|
|
|
|
|
|
char *nick = xstrdup (ctx->irc_user->nickname);
|
|
|
|
for (char *p = nick; *p; p++)
|
|
|
|
*p = irc_tolower (*p);
|
|
|
|
|
|
|
|
// Special characters allowed in nicknames by RFC 2812: []\`_^{|} and -
|
|
|
|
// Also excluded from the ASCII: common user channel prefixes: +%@&~
|
|
|
|
const char *delimiters = ",.;:!?()<>/=#$* \t\r\n\v\f\"'";
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
char *save = NULL;
|
|
|
|
for (char *token = strtok_r (copy, delimiters, &save);
|
|
|
|
token; token = strtok_r (NULL, delimiters, &save))
|
|
|
|
if (!strcmp (token, nick))
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (copy);
|
|
|
|
free (nick);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-04-27 21:52:16 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
|
2015-04-17 22:45:58 +02:00
|
|
|
static void
|
|
|
|
irc_handle_join (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
2015-04-19 21:58:46 +02:00
|
|
|
if (!msg->prefix || msg->params.len < 1)
|
|
|
|
return;
|
|
|
|
|
2015-04-21 21:36:44 +02:00
|
|
|
const char *channel_name = msg->params.vector[0];
|
|
|
|
if (!irc_is_channel (ctx, channel_name))
|
2015-04-19 21:58:46 +02:00
|
|
|
return;
|
|
|
|
|
2015-04-21 21:36:44 +02:00
|
|
|
struct channel *channel = str_map_find (&ctx->irc_channels, channel_name);
|
|
|
|
struct buffer *buffer = str_map_find (&ctx->irc_buffer_map, channel_name);
|
2015-04-19 21:58:46 +02:00
|
|
|
hard_assert ((channel && buffer) ||
|
|
|
|
(channel && !buffer) || (!channel && !buffer));
|
|
|
|
|
|
|
|
// We've joined a new channel
|
|
|
|
if (!channel && irc_is_this_us (ctx, msg->prefix))
|
|
|
|
{
|
|
|
|
buffer = buffer_new ();
|
|
|
|
buffer->type = BUFFER_CHANNEL;
|
2015-04-21 21:36:44 +02:00
|
|
|
buffer->name = xstrdup (channel_name);
|
2015-04-24 23:30:48 +02:00
|
|
|
buffer->channel = channel =
|
|
|
|
irc_make_channel (ctx, xstrdup (channel_name));
|
2015-04-19 21:58:46 +02:00
|
|
|
LIST_APPEND_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer);
|
|
|
|
str_map_set (&ctx->irc_buffer_map, channel->name, buffer);
|
|
|
|
|
|
|
|
buffer_activate (ctx, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is weird, ignoring
|
|
|
|
if (!channel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get or make a user object
|
|
|
|
char *nickname = irc_cut_nickname (msg->prefix);
|
|
|
|
struct user *user = str_map_find (&ctx->irc_users, nickname);
|
|
|
|
if (!user)
|
2015-04-21 00:04:34 +02:00
|
|
|
user = irc_make_user (ctx, nickname);
|
2015-04-19 21:58:46 +02:00
|
|
|
else
|
2015-04-24 23:30:48 +02:00
|
|
|
{
|
|
|
|
user = user_ref (user);
|
2015-04-19 21:58:46 +02:00
|
|
|
free (nickname);
|
2015-04-24 23:30:48 +02:00
|
|
|
}
|
2015-04-19 21:58:46 +02:00
|
|
|
|
|
|
|
// Link the user with the channel
|
|
|
|
struct user_channel *user_channel = user_channel_new ();
|
2015-04-25 00:36:02 +02:00
|
|
|
user_channel->channel = channel;
|
2015-04-19 21:58:46 +02:00
|
|
|
LIST_PREPEND (user->channels, user_channel);
|
|
|
|
|
|
|
|
struct channel_user *channel_user = channel_user_new ();
|
2015-04-24 23:30:48 +02:00
|
|
|
channel_user->user = user;
|
2015-04-19 21:58:46 +02:00
|
|
|
channel_user->modes = xstrdup ("");
|
|
|
|
LIST_PREPEND (channel->users, channel_user);
|
|
|
|
|
|
|
|
// Finally log the message
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_JOIN, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, msg->prefix),
|
|
|
|
.object = irc_to_utf8 (ctx, channel_name));
|
2015-04-19 21:58:46 +02:00
|
|
|
}
|
2015-04-17 22:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_handle_kick (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
2015-04-21 21:36:16 +02:00
|
|
|
if (!msg->prefix || msg->params.len < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char *channel_name = msg->params.vector[0];
|
|
|
|
const char *target = msg->params.vector[1];
|
|
|
|
if (!irc_is_channel (ctx, channel_name)
|
|
|
|
|| irc_is_channel (ctx, target))
|
|
|
|
return;
|
|
|
|
|
2015-04-25 13:41:10 +02:00
|
|
|
const char *message = "";
|
2015-04-21 21:36:16 +02:00
|
|
|
if (msg->params.len > 2)
|
|
|
|
message = msg->params.vector[2];
|
|
|
|
|
|
|
|
struct user *user = str_map_find (&ctx->irc_users, target);
|
|
|
|
struct channel *channel = str_map_find (&ctx->irc_channels, channel_name);
|
|
|
|
struct buffer *buffer = str_map_find (&ctx->irc_buffer_map, channel_name);
|
|
|
|
hard_assert ((channel && buffer) ||
|
|
|
|
(channel && !buffer) || (!channel && !buffer));
|
|
|
|
|
|
|
|
// It would be is weird for this to be false
|
|
|
|
if (user && channel)
|
2015-04-25 00:36:02 +02:00
|
|
|
irc_remove_user_from_channel (user, channel);
|
2015-04-21 21:36:16 +02:00
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_KICK, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, msg->prefix),
|
|
|
|
.object = irc_to_utf8 (ctx, target),
|
|
|
|
.reason = irc_to_utf8 (ctx, message));
|
2015-04-21 21:36:16 +02:00
|
|
|
}
|
2015-04-17 22:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_handle_mode (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
|
|
|
// TODO: parse the mode change and apply it
|
|
|
|
// TODO: log a message
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_handle_nick (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
2015-04-20 00:08:18 +02:00
|
|
|
if (!msg->prefix || msg->params.len < 1)
|
|
|
|
return;
|
|
|
|
|
2015-04-20 22:51:30 +02:00
|
|
|
const char *new_nickname = msg->params.vector[0];
|
|
|
|
|
2015-04-20 00:08:18 +02:00
|
|
|
char *nickname = irc_cut_nickname (msg->prefix);
|
|
|
|
struct user *user = str_map_find (&ctx->irc_users, nickname);
|
|
|
|
free (nickname);
|
|
|
|
if (!user)
|
|
|
|
return;
|
|
|
|
|
2015-04-20 22:51:30 +02:00
|
|
|
// What the fuck
|
|
|
|
// TODO: probably log a message and force a reconnect
|
|
|
|
if (str_map_find (&ctx->irc_users, new_nickname))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Log a message in any PM buffer and rename it;
|
|
|
|
// we may even have one for ourselves
|
|
|
|
struct buffer *pm_buffer =
|
|
|
|
str_map_find (&ctx->irc_buffer_map, user->nickname);
|
|
|
|
if (pm_buffer)
|
|
|
|
{
|
2015-04-24 23:30:48 +02:00
|
|
|
str_map_set (&ctx->irc_buffer_map, new_nickname, pm_buffer);
|
2015-04-20 23:37:34 +02:00
|
|
|
str_map_set (&ctx->irc_buffer_map, user->nickname, NULL);
|
|
|
|
|
2015-04-27 22:59:23 +02:00
|
|
|
char *who = irc_is_this_us (ctx, msg->prefix)
|
|
|
|
? irc_to_utf8 (ctx, msg->prefix)
|
|
|
|
: NULL;
|
2015-04-20 22:51:30 +02:00
|
|
|
buffer_send (ctx, pm_buffer, BUFFER_LINE_NICK, 0,
|
2015-04-27 22:59:23 +02:00
|
|
|
.who = who,
|
2015-04-25 14:53:29 +02:00
|
|
|
.object = irc_to_utf8 (ctx, new_nickname));
|
2015-04-20 22:51:30 +02:00
|
|
|
// TODO: use a full weechat-style buffer name here
|
|
|
|
buffer_rename (ctx, pm_buffer, new_nickname);
|
|
|
|
}
|
|
|
|
|
2015-04-20 00:08:18 +02:00
|
|
|
if (irc_is_this_us (ctx, msg->prefix))
|
|
|
|
{
|
|
|
|
// Log a message in all open buffers on this server
|
|
|
|
struct str_map_iter iter;
|
|
|
|
str_map_iter_init (&iter, &ctx->irc_buffer_map);
|
|
|
|
struct buffer *buffer;
|
|
|
|
while ((buffer = str_map_iter_next (&iter)))
|
|
|
|
{
|
2015-04-20 22:51:30 +02:00
|
|
|
// We've already done that
|
|
|
|
if (buffer == pm_buffer)
|
|
|
|
continue;
|
|
|
|
|
2015-04-20 21:56:32 +02:00
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_NICK, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.object = irc_to_utf8 (ctx, new_nickname));
|
2015-04-20 00:08:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Log a message in all channels the user is in
|
|
|
|
LIST_FOR_EACH (struct user_channel, iter, user->channels)
|
|
|
|
{
|
2015-04-20 22:51:30 +02:00
|
|
|
struct buffer *buffer =
|
|
|
|
str_map_find (&ctx->irc_buffer_map, iter->channel->name);
|
2015-04-20 00:08:18 +02:00
|
|
|
hard_assert (buffer != NULL);
|
2015-04-20 21:56:32 +02:00
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_NICK, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, msg->prefix),
|
|
|
|
.object = irc_to_utf8 (ctx, new_nickname));
|
2015-04-20 00:08:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally rename the user
|
2015-04-20 22:51:30 +02:00
|
|
|
str_map_set (&ctx->irc_users, new_nickname, user_ref (user));
|
|
|
|
str_map_set (&ctx->irc_users, user->nickname, NULL);
|
|
|
|
|
2015-04-20 00:08:18 +02:00
|
|
|
free (user->nickname);
|
|
|
|
user->nickname = xstrdup (new_nickname);
|
2015-04-20 22:09:56 +02:00
|
|
|
|
|
|
|
// We might have renamed ourselves
|
|
|
|
refresh_prompt (ctx);
|
2015-04-17 22:45:58 +02:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:52:16 +02:00
|
|
|
static void
|
|
|
|
irc_handle_ctcp_reply (struct app_context *ctx,
|
|
|
|
const struct irc_message *msg, struct ctcp_chunk *chunk)
|
|
|
|
{
|
|
|
|
char *nickname = irc_cut_nickname (msg->prefix);
|
|
|
|
char *nickname_utf8 = irc_to_utf8 (ctx, nickname);
|
|
|
|
char *tag_utf8 = irc_to_utf8 (ctx, chunk->tag.str);
|
|
|
|
char *text_utf8 = irc_to_utf8 (ctx, chunk->text.str);
|
|
|
|
|
|
|
|
buffer_send_status (ctx, ctx->server_buffer,
|
|
|
|
"CTCP reply from %s: %s %s", nickname_utf8, tag_utf8, text_utf8);
|
|
|
|
|
|
|
|
free (nickname);
|
|
|
|
free (nickname_utf8);
|
|
|
|
free (tag_utf8);
|
|
|
|
free (text_utf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_handle_notice_text (struct app_context *ctx,
|
|
|
|
const struct irc_message *msg, struct str *text)
|
|
|
|
{
|
|
|
|
const char *target = msg->params.vector[0];
|
|
|
|
struct buffer *buffer = irc_get_buffer_for_message (ctx, msg, target);
|
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
{
|
2015-04-27 22:40:33 +02:00
|
|
|
// TODO: some more obvious indication of highlights
|
|
|
|
int flags = irc_is_highlight (ctx, text->str)
|
|
|
|
? BUFFER_LINE_HIGHLIGHT
|
|
|
|
: 0;
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_NOTICE, flags,
|
2015-04-27 21:52:16 +02:00
|
|
|
.who = irc_to_utf8 (ctx, msg->prefix),
|
|
|
|
.text = irc_to_utf8 (ctx, text->str));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:45:58 +02:00
|
|
|
static void
|
|
|
|
irc_handle_notice (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
2015-04-27 21:52:16 +02:00
|
|
|
if (!msg->prefix || msg->params.len < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// This ignores empty messages which we should never receive anyway
|
|
|
|
struct ctcp_chunk *chunks = ctcp_parse (msg->params.vector[1]);
|
|
|
|
LIST_FOR_EACH (struct ctcp_chunk, iter, chunks)
|
|
|
|
if (!iter->is_extended)
|
|
|
|
irc_handle_notice_text (ctx, msg, &iter->text);
|
|
|
|
else
|
|
|
|
irc_handle_ctcp_reply (ctx, msg, iter);
|
|
|
|
ctcp_destroy (chunks);
|
2015-04-17 22:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_handle_part (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
2015-04-20 23:03:52 +02:00
|
|
|
if (!msg->prefix || msg->params.len < 1)
|
|
|
|
return;
|
|
|
|
|
2015-04-21 21:36:16 +02:00
|
|
|
const char *channel_name = msg->params.vector[0];
|
|
|
|
if (!irc_is_channel (ctx, channel_name))
|
2015-04-20 23:03:52 +02:00
|
|
|
return;
|
|
|
|
|
2015-04-25 13:41:10 +02:00
|
|
|
const char *message = "";
|
2015-04-20 23:03:52 +02:00
|
|
|
if (msg->params.len > 1)
|
|
|
|
message = msg->params.vector[1];
|
|
|
|
|
2015-04-21 21:36:16 +02:00
|
|
|
char *nickname = irc_cut_nickname (msg->prefix);
|
|
|
|
struct user *user = str_map_find (&ctx->irc_users, nickname);
|
|
|
|
free (nickname);
|
|
|
|
|
|
|
|
struct channel *channel = str_map_find (&ctx->irc_channels, channel_name);
|
|
|
|
struct buffer *buffer = str_map_find (&ctx->irc_buffer_map, channel_name);
|
2015-04-20 23:03:52 +02:00
|
|
|
hard_assert ((channel && buffer) ||
|
|
|
|
(channel && !buffer) || (!channel && !buffer));
|
|
|
|
|
2015-04-21 21:36:16 +02:00
|
|
|
// It would be is weird for this to be false
|
|
|
|
if (user && channel)
|
2015-04-25 00:36:02 +02:00
|
|
|
irc_remove_user_from_channel (user, channel);
|
2015-04-21 21:36:16 +02:00
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_PART, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, msg->prefix),
|
|
|
|
.object = irc_to_utf8 (ctx, channel_name),
|
|
|
|
.reason = irc_to_utf8 (ctx, message));
|
2015-04-21 21:36:16 +02:00
|
|
|
}
|
2015-04-17 22:45:58 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
static void
|
|
|
|
irc_handle_ping (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
|
|
|
if (msg->params.len)
|
|
|
|
irc_send (ctx, "PONG :%s", msg->params.vector[0]);
|
|
|
|
else
|
|
|
|
irc_send (ctx, "PONG");
|
|
|
|
}
|
|
|
|
|
2015-04-27 01:36:33 +02:00
|
|
|
static char *
|
|
|
|
ctime_now (char buf[26])
|
|
|
|
{
|
|
|
|
struct tm tm_;
|
|
|
|
time_t now = time (NULL);
|
|
|
|
if (!asctime_r (localtime_r (&now, &tm_), buf))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Annoying thing
|
|
|
|
*strchr (buf, '\n') = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-04-27 01:47:21 +02:00
|
|
|
static void irc_send_ctcp_reply (struct app_context *ctx, const char *recipient,
|
|
|
|
const char *format, ...) ATTRIBUTE_PRINTF (3, 4);
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_send_ctcp_reply (struct app_context *ctx,
|
|
|
|
const char *recipient, const char *format, ...)
|
|
|
|
{
|
|
|
|
struct str m;
|
|
|
|
str_init (&m);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, format);
|
|
|
|
str_append_vprintf (&m, format, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
irc_send (ctx, "NOTICE %s :\x01%s\x01", recipient, m.str);
|
|
|
|
|
|
|
|
char *text_utf8 = irc_to_utf8 (ctx, m.str);
|
|
|
|
char *recipient_utf8 = irc_to_utf8 (ctx, recipient);
|
|
|
|
str_free (&m);
|
|
|
|
|
|
|
|
buffer_send_status (ctx, ctx->server_buffer,
|
|
|
|
"CTCP reply to %s: %s", recipient_utf8, text_utf8);
|
|
|
|
free (text_utf8);
|
|
|
|
free (recipient_utf8);
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:45:58 +02:00
|
|
|
static void
|
2015-04-26 22:31:07 +02:00
|
|
|
irc_handle_ctcp_request (struct app_context *ctx,
|
|
|
|
const struct irc_message *msg, struct ctcp_chunk *chunk)
|
2015-04-17 22:45:58 +02:00
|
|
|
{
|
2015-04-26 22:31:07 +02:00
|
|
|
char *nickname = irc_cut_nickname (msg->prefix);
|
|
|
|
char *nickname_utf8 = irc_to_utf8 (ctx, nickname);
|
|
|
|
char *tag_utf8 = irc_to_utf8 (ctx, chunk->tag.str);
|
2015-04-19 22:19:52 +02:00
|
|
|
|
2015-04-26 22:31:07 +02:00
|
|
|
buffer_send_status (ctx, ctx->server_buffer,
|
|
|
|
"CTCP requested by %s: %s", nickname_utf8, tag_utf8);
|
2015-04-19 22:19:52 +02:00
|
|
|
|
2015-04-27 01:36:33 +02:00
|
|
|
const char *target = msg->params.vector[0];
|
|
|
|
const char *recipient = nickname;
|
|
|
|
if (irc_is_channel (ctx, target))
|
|
|
|
recipient = target;
|
|
|
|
|
|
|
|
if (!strcmp (chunk->tag.str, "CLIENTINFO"))
|
2015-04-27 01:47:21 +02:00
|
|
|
irc_send_ctcp_reply (ctx, recipient, "CLIENTINFO %s %s %s %s",
|
|
|
|
"PING", "VERSION", "TIME", "CLIENTINFO");
|
2015-04-27 01:36:33 +02:00
|
|
|
else if (!strcmp (chunk->tag.str, "PING"))
|
2015-04-27 01:47:21 +02:00
|
|
|
irc_send_ctcp_reply (ctx, recipient, "PING %s", chunk->text.str);
|
2015-04-27 01:36:33 +02:00
|
|
|
else if (!strcmp (chunk->tag.str, "VERSION"))
|
|
|
|
{
|
|
|
|
struct utsname info;
|
|
|
|
if (uname (&info))
|
|
|
|
LOG_LIBC_FAILURE ("uname");
|
|
|
|
else
|
2015-04-27 01:47:21 +02:00
|
|
|
irc_send_ctcp_reply (ctx, recipient, "VERSION %s %s on %s %s",
|
|
|
|
PROGRAM_NAME, PROGRAM_VERSION, info.sysname, info.machine);
|
2015-04-27 01:36:33 +02:00
|
|
|
}
|
|
|
|
else if (!strcmp (chunk->tag.str, "TIME"))
|
|
|
|
{
|
|
|
|
char buf[26];
|
|
|
|
if (!ctime_now (buf))
|
|
|
|
LOG_LIBC_FAILURE ("asctime_r");
|
|
|
|
else
|
2015-04-27 01:47:21 +02:00
|
|
|
irc_send_ctcp_reply (ctx, recipient, "TIME %s", buf);
|
2015-04-27 01:36:33 +02:00
|
|
|
}
|
2015-04-26 22:31:07 +02:00
|
|
|
|
2015-04-27 01:36:33 +02:00
|
|
|
free (nickname);
|
2015-04-26 22:31:07 +02:00
|
|
|
free (nickname_utf8);
|
|
|
|
free (tag_utf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_handle_privmsg_text (struct app_context *ctx,
|
|
|
|
const struct irc_message *msg, struct str *text, bool is_action)
|
|
|
|
{
|
|
|
|
const char *target = msg->params.vector[0];
|
|
|
|
struct buffer *buffer = irc_get_buffer_for_message (ctx, msg, target);
|
2015-04-19 22:19:52 +02:00
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
{
|
2015-04-27 22:40:33 +02:00
|
|
|
// TODO: some more obvious indication of highlights
|
|
|
|
int flags = irc_is_highlight (ctx, text->str)
|
|
|
|
? BUFFER_LINE_HIGHLIGHT
|
|
|
|
: 0;
|
2015-04-26 22:31:07 +02:00
|
|
|
enum buffer_line_type type = is_action
|
|
|
|
? BUFFER_LINE_ACTION
|
|
|
|
: BUFFER_LINE_PRIVMSG;
|
2015-04-27 22:40:33 +02:00
|
|
|
buffer_send (ctx, buffer, type, flags,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, msg->prefix),
|
2015-04-26 22:31:07 +02:00
|
|
|
.text = irc_to_utf8 (ctx, text->str));
|
2015-04-19 22:19:52 +02:00
|
|
|
}
|
2015-04-17 22:45:58 +02:00
|
|
|
}
|
|
|
|
|
2015-04-26 22:31:07 +02:00
|
|
|
static void
|
|
|
|
irc_handle_privmsg (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
|
|
|
if (!msg->prefix || msg->params.len < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// This ignores empty messages which we should never receive anyway
|
|
|
|
struct ctcp_chunk *chunks = ctcp_parse (msg->params.vector[1]);
|
|
|
|
LIST_FOR_EACH (struct ctcp_chunk, iter, chunks)
|
|
|
|
if (!iter->is_extended)
|
|
|
|
irc_handle_privmsg_text (ctx, msg, &iter->text, false);
|
|
|
|
else if (!strcmp (iter->tag.str, "ACTION"))
|
|
|
|
irc_handle_privmsg_text (ctx, msg, &iter->text, true);
|
|
|
|
else
|
|
|
|
irc_handle_ctcp_request (ctx, msg, iter);
|
|
|
|
ctcp_destroy (chunks);
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:45:58 +02:00
|
|
|
static void
|
|
|
|
irc_handle_quit (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
2015-04-20 00:08:18 +02:00
|
|
|
if (!msg->prefix)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// What the fuck
|
|
|
|
if (irc_is_this_us (ctx, msg->prefix))
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *nickname = irc_cut_nickname (msg->prefix);
|
|
|
|
struct user *user = str_map_find (&ctx->irc_users, nickname);
|
|
|
|
free (nickname);
|
|
|
|
if (!user)
|
|
|
|
return;
|
|
|
|
|
2015-04-25 13:41:10 +02:00
|
|
|
const char *message = "";
|
2015-04-20 00:08:18 +02:00
|
|
|
if (msg->params.len > 0)
|
|
|
|
message = msg->params.vector[0];
|
|
|
|
|
|
|
|
// Log a message in any PM buffer
|
|
|
|
struct buffer *buffer =
|
|
|
|
str_map_find (&ctx->irc_buffer_map, user->nickname);
|
|
|
|
if (buffer)
|
|
|
|
{
|
2015-04-20 21:49:46 +02:00
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_QUIT, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, msg->prefix),
|
|
|
|
.reason = irc_to_utf8 (ctx, message));
|
2015-04-20 21:49:46 +02:00
|
|
|
|
|
|
|
// TODO: set some kind of a flag in the buffer and when the user
|
|
|
|
// reappers on a channel (JOIN), log a "is back online" message.
|
|
|
|
// Also set this flag when we receive a "no such nick" numeric
|
|
|
|
// and reset it when we send something to the buffer.
|
2015-04-20 00:08:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log a message in all channels the user is in
|
|
|
|
LIST_FOR_EACH (struct user_channel, iter, user->channels)
|
|
|
|
{
|
|
|
|
buffer = str_map_find (&ctx->irc_buffer_map, iter->channel->name);
|
2015-04-21 21:36:59 +02:00
|
|
|
if (buffer)
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_QUIT, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, msg->prefix),
|
|
|
|
.reason = irc_to_utf8 (ctx, message));
|
2015-04-20 21:49:46 +02:00
|
|
|
|
2015-04-25 00:36:02 +02:00
|
|
|
// This destroys "iter" which doesn't matter to us
|
|
|
|
irc_remove_user_from_channel (user, iter->channel);
|
2015-04-20 00:08:18 +02:00
|
|
|
}
|
2015-04-17 22:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_handle_topic (struct app_context *ctx, const struct irc_message *msg)
|
|
|
|
{
|
2015-04-21 21:47:34 +02:00
|
|
|
if (!msg->prefix || msg->params.len < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char *channel_name = msg->params.vector[0];
|
|
|
|
const char *topic = msg->params.vector[1];
|
|
|
|
if (!irc_is_channel (ctx, channel_name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct channel *channel = str_map_find (&ctx->irc_channels, channel_name);
|
|
|
|
struct buffer *buffer = str_map_find (&ctx->irc_buffer_map, channel_name);
|
|
|
|
hard_assert ((channel && buffer) ||
|
|
|
|
(channel && !buffer) || (!channel && !buffer));
|
|
|
|
|
|
|
|
// It would be is weird for this to be false
|
|
|
|
if (channel)
|
|
|
|
{
|
|
|
|
free (channel->topic);
|
|
|
|
channel->topic = xstrdup (topic);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_TOPIC, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, msg->prefix),
|
|
|
|
.text = irc_to_utf8 (ctx, topic));
|
2015-04-21 21:47:34 +02:00
|
|
|
}
|
2015-04-17 22:45:58 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
static struct irc_handler
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
void (*handler) (struct app_context *ctx, const struct irc_message *msg);
|
|
|
|
}
|
|
|
|
g_irc_handlers[] =
|
|
|
|
{
|
|
|
|
// This list needs to stay sorted
|
2015-04-17 22:45:58 +02:00
|
|
|
{ "JOIN", irc_handle_join },
|
|
|
|
{ "KICK", irc_handle_kick },
|
|
|
|
{ "MODE", irc_handle_mode },
|
|
|
|
{ "NICK", irc_handle_nick },
|
|
|
|
{ "NOTICE", irc_handle_notice },
|
|
|
|
{ "PART", irc_handle_part },
|
|
|
|
{ "PING", irc_handle_ping },
|
|
|
|
{ "PRIVMSG", irc_handle_privmsg },
|
|
|
|
{ "QUIT", irc_handle_quit },
|
|
|
|
{ "TOPIC", irc_handle_topic },
|
2015-04-16 00:56:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
irc_handler_cmp_by_name (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct irc_handler *first = a;
|
|
|
|
const struct irc_handler *second = b;
|
|
|
|
return strcasecmp_ascii (first->name, second->name);
|
|
|
|
}
|
|
|
|
|
2015-04-22 21:59:50 +02:00
|
|
|
static bool
|
|
|
|
irc_try_parse_word_for_userhost (struct app_context *ctx, const char *word)
|
|
|
|
{
|
|
|
|
regex_t re;
|
|
|
|
int err = regcomp (&re, "^[^!@]+!([^!@]+@[^!@]+)$", REG_EXTENDED);
|
|
|
|
if (!soft_assert (!err))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
regmatch_t matches[2];
|
|
|
|
bool result = false;
|
|
|
|
if (!regexec (&re, word, 2, matches, 0))
|
|
|
|
{
|
|
|
|
free (ctx->irc_user_host);
|
|
|
|
ctx->irc_user_host = xstrndup (word + matches[1].rm_so,
|
|
|
|
matches[1].rm_eo - matches[1].rm_so);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
regfree (&re);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_try_parse_welcome_for_userhost (struct app_context *ctx, const char *m)
|
|
|
|
{
|
|
|
|
struct str_vector v;
|
|
|
|
str_vector_init (&v);
|
|
|
|
split_str_ignore_empty (m, ' ', &v);
|
|
|
|
for (size_t i = 0; i < v.len; i++)
|
|
|
|
if (irc_try_parse_word_for_userhost (ctx, v.vector[i]))
|
|
|
|
break;
|
|
|
|
str_vector_free (&v);
|
|
|
|
}
|
|
|
|
|
2015-04-21 22:08:18 +02:00
|
|
|
static void
|
|
|
|
irc_process_numeric (struct app_context *ctx,
|
|
|
|
const struct irc_message *msg, unsigned long numeric)
|
|
|
|
{
|
|
|
|
// Numerics typically have human-readable information
|
2015-04-23 02:59:58 +02:00
|
|
|
// TODO: try to output certain replies in more specific buffers
|
2015-04-21 22:08:18 +02:00
|
|
|
|
|
|
|
// 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 (©);
|
|
|
|
str_vector_add_vector (©, 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 (©, ' ');
|
|
|
|
str_vector_free (©);
|
2015-04-25 14:53:29 +02:00
|
|
|
buffer_send (ctx, ctx->server_buffer, BUFFER_LINE_STATUS, 0,
|
|
|
|
.text = irc_to_utf8 (ctx, reconstructed));
|
2015-04-21 22:08:18 +02:00
|
|
|
free (reconstructed);
|
|
|
|
|
|
|
|
switch (numeric)
|
|
|
|
{
|
2015-04-22 21:59:50 +02:00
|
|
|
case IRC_RPL_WELCOME:
|
|
|
|
// We still issue a USERHOST anyway as this is in general unreliable
|
2015-04-24 22:33:23 +02:00
|
|
|
if (msg->params.len == 2)
|
|
|
|
irc_try_parse_welcome_for_userhost (ctx, msg->params.vector[1]);
|
2015-04-22 21:59:50 +02:00
|
|
|
break;
|
2015-04-21 22:34:16 +02:00
|
|
|
case IRC_RPL_ISUPPORT:
|
|
|
|
// TODO: parse this, mainly PREFIX; see
|
|
|
|
// http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt
|
|
|
|
break;
|
2015-04-21 22:08:18 +02:00
|
|
|
case IRC_RPL_NAMREPLY:
|
|
|
|
// TODO: find the channel and if found, push nicks to names_buf
|
|
|
|
break;
|
|
|
|
case IRC_RPL_ENDOFNAMES:
|
2015-04-23 02:59:58 +02:00
|
|
|
// TODO: find the channel and if found, overwrite users;
|
|
|
|
// however take care to combine channel user modes
|
|
|
|
break;
|
|
|
|
case IRC_ERR_NICKNAMEINUSE:
|
|
|
|
// TODO: if not connected yet (irc_ready), use a different nick;
|
|
|
|
// either use a number suffix, or accept commas in "nickname" config
|
2015-04-21 22:08:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
static void
|
|
|
|
irc_process_message (const struct irc_message *msg,
|
|
|
|
const char *raw, void *user_data)
|
|
|
|
{
|
|
|
|
struct app_context *ctx = user_data;
|
|
|
|
|
|
|
|
if (g_debug_mode)
|
|
|
|
{
|
|
|
|
struct app_readline_state state;
|
|
|
|
if (ctx->readline_prompt_shown)
|
|
|
|
app_readline_hide (&state);
|
|
|
|
|
2015-04-25 14:53:29 +02:00
|
|
|
char *term = irc_to_term (ctx, raw);
|
|
|
|
fprintf (stderr, "[IRC] ==> \"%s\"\n", term);
|
|
|
|
free (term);
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
if (ctx->readline_prompt_shown)
|
|
|
|
app_readline_restore (&state, ctx->readline_prompt);
|
|
|
|
}
|
|
|
|
|
2015-04-19 22:47:45 +02:00
|
|
|
// XXX: or is the 001 numeric enough? For what?
|
2015-04-16 00:56:05 +02:00
|
|
|
if (!ctx->irc_ready && (!strcasecmp (msg->command, "MODE")
|
2015-04-12 04:52:39 +02:00
|
|
|
|| !strcasecmp (msg->command, "376") // RPL_ENDOFMOTD
|
|
|
|
|| !strcasecmp (msg->command, "422"))) // ERR_NOMOTD
|
|
|
|
{
|
2015-04-17 22:11:36 +02:00
|
|
|
// XXX: should we really print this?
|
2015-04-18 17:51:28 +02:00
|
|
|
buffer_send_status (ctx, ctx->server_buffer, "Successfully connected");
|
2015-04-12 04:52:39 +02:00
|
|
|
ctx->irc_ready = true;
|
2015-04-18 03:25:10 +02:00
|
|
|
refresh_prompt (ctx);
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-19 22:47:45 +02:00
|
|
|
// 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)
|
2015-04-20 22:09:56 +02:00
|
|
|
irc_send (ctx, "USERHOST %s", ctx->irc_user->nickname);
|
2015-04-19 22:47:45 +02:00
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
const char *autojoin = str_map_find (&ctx->config, "autojoin");
|
|
|
|
if (autojoin)
|
|
|
|
irc_send (ctx, "JOIN :%s", autojoin);
|
|
|
|
}
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
struct irc_handler key = { .name = msg->command };
|
|
|
|
struct irc_handler *handler = bsearch (&key, g_irc_handlers,
|
|
|
|
N_ELEMENTS (g_irc_handlers), sizeof key, irc_handler_cmp_by_name);
|
|
|
|
if (handler)
|
|
|
|
handler->handler (ctx, msg);
|
|
|
|
|
2015-04-21 22:08:18 +02:00
|
|
|
unsigned long numeric;
|
|
|
|
if (xstrtoul (&numeric, msg->command, 10))
|
|
|
|
irc_process_numeric (ctx, msg, numeric);
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
|
|
|
|
2015-04-23 02:48:25 +02:00
|
|
|
// --- Message autosplitting magic ---------------------------------------------
|
|
|
|
|
2015-04-24 22:34:44 +02:00
|
|
|
// This is the most basic acceptable algorithm; something like ICU with proper
|
|
|
|
// locale specification would be needed to make it work better.
|
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
static size_t
|
|
|
|
wrap_text_for_single_line (const char *text, size_t text_len,
|
|
|
|
size_t line_len, struct str *output)
|
2015-04-23 02:48:25 +02:00
|
|
|
{
|
2015-04-25 01:34:31 +02:00
|
|
|
int eaten = 0;
|
2015-04-23 02:48:25 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
// First try going word by word
|
|
|
|
const char *word_start;
|
|
|
|
const char *word_end = text + strcspn (text, " ");
|
|
|
|
size_t word_len = word_end - text;
|
|
|
|
while (line_len && word_len <= line_len)
|
2015-04-23 02:48:25 +02:00
|
|
|
{
|
2015-04-25 01:34:31 +02:00
|
|
|
if (word_len)
|
2015-04-24 22:34:44 +02:00
|
|
|
{
|
2015-04-25 01:34:31 +02:00
|
|
|
str_append_data (output, text, word_len);
|
|
|
|
|
|
|
|
text += word_len;
|
|
|
|
eaten += word_len;
|
|
|
|
line_len -= word_len;
|
2015-04-24 22:34:44 +02:00
|
|
|
}
|
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
// Find the next word's end
|
|
|
|
word_start = text + strspn (text, " ");
|
|
|
|
word_end = word_start + strcspn (word_start, " ");
|
|
|
|
word_len = word_end - text;
|
|
|
|
}
|
2015-04-24 22:34:44 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
if (eaten)
|
|
|
|
// Discard whitespace between words if split
|
|
|
|
return eaten + (word_start - text);
|
2015-04-24 22:34:44 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
// And if that doesn't help, cut the longest valid block of characters
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
const char *next = utf8_next (text, text_len - eaten);
|
|
|
|
hard_assert (next);
|
2015-04-23 02:48:25 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
size_t char_len = next - text;
|
|
|
|
if (char_len > line_len)
|
|
|
|
break;
|
2015-04-23 02:48:25 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
str_append_data (output, text, char_len);
|
2015-04-23 02:48:25 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
text += char_len;
|
|
|
|
eaten += char_len;
|
|
|
|
line_len -= char_len;
|
|
|
|
}
|
|
|
|
return eaten;
|
|
|
|
}
|
2015-04-23 02:48:25 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2015-04-23 02:48:25 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
static bool
|
|
|
|
wrap_message (const char *message,
|
|
|
|
int line_max, struct str_vector *output, struct error **e)
|
|
|
|
{
|
|
|
|
if (line_max <= 0)
|
|
|
|
goto error;
|
2015-04-23 02:48:25 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
for (size_t message_left = strlen (message); message_left; )
|
|
|
|
{
|
|
|
|
struct str m;
|
|
|
|
str_init (&m);
|
2015-04-24 22:34:44 +02:00
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
size_t eaten = wrap_text_for_single_line (message,
|
|
|
|
MIN ((size_t) line_max, message_left), message_left, &m);
|
|
|
|
if (!eaten)
|
|
|
|
{
|
|
|
|
str_free (&m);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
str_vector_add_owned (output, str_steal (&m));
|
|
|
|
message += eaten;
|
|
|
|
message_left -= eaten;
|
2015-04-23 02:48:25 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
2015-04-25 01:34:31 +02:00
|
|
|
error:
|
|
|
|
// Well, that's just weird
|
|
|
|
error_set (e,
|
|
|
|
"Message splitting was unsuccessful as there was "
|
|
|
|
"too little room for UTF-8 characters");
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-23 02:48:25 +02:00
|
|
|
|
|
|
|
/// Automatically splits messages that arrive at other clients with our prefix
|
|
|
|
/// so that they don't arrive cut off by the server
|
|
|
|
static bool
|
|
|
|
irc_autosplit_message (struct app_context *ctx, const char *message,
|
|
|
|
int fixed_part, struct str_vector *output, struct error **e)
|
|
|
|
{
|
|
|
|
// :<nick>!<user>@<host> <fixed-part><message>
|
|
|
|
int space_in_one_message = 0;
|
|
|
|
if (ctx->irc_user_host)
|
|
|
|
space_in_one_message = 510
|
|
|
|
- 1 - (int) strlen (ctx->irc_user->nickname)
|
|
|
|
- 1 - (int) strlen (ctx->irc_user_host)
|
|
|
|
- 1 - fixed_part;
|
|
|
|
|
|
|
|
// However we don't always have the full info for message splitting
|
|
|
|
if (!space_in_one_message)
|
|
|
|
str_vector_add (output, message);
|
2015-04-25 01:34:31 +02:00
|
|
|
else if (!wrap_message (message, space_in_one_message, output, e))
|
2015-04-23 02:48:25 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct send_autosplit_args;
|
|
|
|
|
|
|
|
typedef void (*send_autosplit_logger_fn) (struct app_context *ctx,
|
|
|
|
struct send_autosplit_args *args, struct buffer *buffer, const char *line);
|
|
|
|
|
|
|
|
struct send_autosplit_args
|
|
|
|
{
|
|
|
|
const char *command; ///< E.g. PRIVMSG or NOTICE
|
|
|
|
const char *target; ///< User or channel
|
|
|
|
const char *message; ///< A message to be autosplit
|
|
|
|
send_autosplit_logger_fn logger; ///< Logger for all resulting lines
|
2015-04-26 23:30:54 +02:00
|
|
|
const char *prefix; ///< E.g. "\x01ACTION"
|
|
|
|
const char *suffix; ///< E.g. "\x01"
|
2015-04-23 02:48:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
send_autosplit_message (struct app_context *ctx, struct send_autosplit_args a)
|
|
|
|
{
|
|
|
|
struct buffer *buffer = str_map_find (&ctx->irc_buffer_map, a.target);
|
2015-04-26 23:30:54 +02:00
|
|
|
int fixed_part = strlen (a.command) + 1 + strlen (a.target) + 1 + 1
|
|
|
|
+ strlen (a.prefix) + strlen (a.suffix);
|
2015-04-23 02:48:25 +02:00
|
|
|
|
|
|
|
struct str_vector lines;
|
|
|
|
str_vector_init (&lines);
|
|
|
|
struct error *e = NULL;
|
|
|
|
if (!irc_autosplit_message (ctx, a.message, fixed_part, &lines, &e))
|
|
|
|
{
|
|
|
|
buffer_send_error (ctx,
|
|
|
|
buffer ? buffer : ctx->server_buffer, "%s", e->message);
|
|
|
|
error_free (e);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < lines.len; i++)
|
|
|
|
{
|
2015-04-26 23:30:54 +02:00
|
|
|
irc_send (ctx, "%s %s :%s%s%s", a.command, a.target,
|
|
|
|
a.prefix, lines.vector[i], a.suffix);
|
2015-04-23 02:48:25 +02:00
|
|
|
a.logger (ctx, &a, buffer, lines.vector[i]);
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
str_vector_free (&lines);
|
|
|
|
}
|
|
|
|
|
2015-04-26 23:30:54 +02:00
|
|
|
static void
|
|
|
|
log_outcoming_action (struct app_context *ctx,
|
|
|
|
struct send_autosplit_args *a, struct buffer *buffer, const char *line)
|
|
|
|
{
|
|
|
|
(void) a;
|
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_ACTION, 0,
|
|
|
|
.who = irc_to_utf8 (ctx, ctx->irc_user->nickname),
|
|
|
|
.text = irc_to_utf8 (ctx, line));
|
|
|
|
|
|
|
|
// This can only be sent from a user or channel buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SEND_AUTOSPLIT_ACTION(ctx, target, message) \
|
|
|
|
send_autosplit_message ((ctx), (struct send_autosplit_args) \
|
|
|
|
{ "PRIVMSG", (target), (message), log_outcoming_action, \
|
|
|
|
"\x01" "ACTION ", "\x01" })
|
|
|
|
|
2015-04-23 02:48:25 +02:00
|
|
|
static void
|
|
|
|
log_outcoming_privmsg (struct app_context *ctx,
|
|
|
|
struct send_autosplit_args *a, struct buffer *buffer, const char *line)
|
|
|
|
{
|
|
|
|
if (buffer)
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_PRIVMSG, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, ctx->irc_user->nickname),
|
|
|
|
.text = irc_to_utf8 (ctx, line));
|
2015-04-23 02:48:25 +02:00
|
|
|
else
|
2015-04-25 14:53:29 +02:00
|
|
|
// TODO: fix logging and encoding
|
2015-04-23 02:48:25 +02:00
|
|
|
buffer_send (ctx, ctx->server_buffer, BUFFER_LINE_STATUS, 0,
|
2015-04-25 13:41:10 +02:00
|
|
|
.text = xstrdup_printf ("MSG(%s): %s", a->target, line));
|
2015-04-23 02:48:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SEND_AUTOSPLIT_PRIVMSG(ctx, target, message) \
|
|
|
|
send_autosplit_message ((ctx), (struct send_autosplit_args) \
|
2015-04-26 23:30:54 +02:00
|
|
|
{ "PRIVMSG", (target), (message), log_outcoming_privmsg, "", "" })
|
2015-04-23 02:48:25 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
log_outcoming_notice (struct app_context *ctx,
|
|
|
|
struct send_autosplit_args *a, struct buffer *buffer, const char *line)
|
|
|
|
{
|
|
|
|
if (buffer)
|
|
|
|
buffer_send (ctx, buffer, BUFFER_LINE_NOTICE, 0,
|
2015-04-25 14:53:29 +02:00
|
|
|
.who = irc_to_utf8 (ctx, ctx->irc_user->nickname),
|
|
|
|
.text = irc_to_utf8 (ctx, line));
|
2015-04-23 02:48:25 +02:00
|
|
|
else
|
2015-04-25 14:53:29 +02:00
|
|
|
// TODO: fix logging and encoding
|
2015-04-23 02:48:25 +02:00
|
|
|
buffer_send (ctx, ctx->server_buffer, BUFFER_LINE_STATUS, 0,
|
2015-04-25 13:41:10 +02:00
|
|
|
.text = xstrdup_printf ("Notice -> %s: %s", a->target, line));
|
2015-04-23 02:48:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SEND_AUTOSPLIT_NOTICE(ctx, target, message) \
|
|
|
|
send_autosplit_message ((ctx), (struct send_autosplit_args) \
|
2015-04-26 23:30:54 +02:00
|
|
|
{ "NOTICE", (target), (message), log_outcoming_notice, "", "" })
|
2015-04-23 02:48:25 +02:00
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
// --- User input handling -----------------------------------------------------
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool handle_command_help (struct app_context *, char *);
|
2015-04-17 21:29:18 +02:00
|
|
|
|
|
|
|
/// Cuts the longest non-whitespace portion of text and advances the pointer
|
|
|
|
static char *
|
|
|
|
cut_word (char **s)
|
|
|
|
{
|
|
|
|
char *start = *s;
|
|
|
|
size_t word_len = strcspn (*s, " \t");
|
|
|
|
char *end = start + word_len;
|
|
|
|
*s = end + strspn (end, " \t");
|
|
|
|
*end = '\0';
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
try_handle_buffer_goto (struct app_context *ctx, const char *word)
|
|
|
|
{
|
|
|
|
unsigned long n;
|
|
|
|
if (!xstrtoul (&n, word, 10))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (n > INT_MAX || !buffer_goto (ctx, n))
|
2015-04-17 23:18:07 +02:00
|
|
|
buffer_send_error (ctx, ctx->global_buffer,
|
|
|
|
"%s: %s", "no such buffer", word);
|
2015-04-17 21:29:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct buffer *
|
|
|
|
try_decode_buffer (struct app_context *ctx, const char *word)
|
|
|
|
{
|
|
|
|
unsigned long n;
|
|
|
|
struct buffer *buffer = NULL;
|
|
|
|
if (xstrtoul (&n, word, 10) && n <= INT_MAX)
|
|
|
|
buffer = buffer_at_index (ctx, n);
|
|
|
|
if (!buffer)
|
2015-04-18 22:09:05 +02:00
|
|
|
buffer = buffer_by_name (ctx, word);
|
2015-04-17 21:29:18 +02:00
|
|
|
// TODO: decode the global and server buffers, partial matches
|
|
|
|
return buffer;
|
|
|
|
}
|
2015-04-16 00:56:05 +02:00
|
|
|
|
2015-04-23 03:07:48 +02:00
|
|
|
static bool
|
|
|
|
server_command_check (struct app_context *ctx, const char *action)
|
|
|
|
{
|
|
|
|
if (ctx->current_buffer->type == BUFFER_GLOBAL)
|
|
|
|
buffer_send_error (ctx, ctx->current_buffer,
|
|
|
|
"Can't do this from a global buffer (%s)", action);
|
|
|
|
else if (ctx->irc_fd == -1)
|
|
|
|
buffer_send_error (ctx, ctx->server_buffer, "Not connected");
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-25 02:41:52 +02:00
|
|
|
static void
|
|
|
|
show_buffers_list (struct app_context *ctx)
|
|
|
|
{
|
|
|
|
buffer_send_status (ctx, ctx->global_buffer, "%s", "");
|
|
|
|
buffer_send_status (ctx, ctx->global_buffer, "Buffers list:");
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
LIST_FOR_EACH (struct buffer, iter, ctx->buffers)
|
|
|
|
buffer_send_status (ctx, ctx->global_buffer,
|
|
|
|
" [%d] %s", i++, iter->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_buffer_close (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
|
|
|
struct buffer *buffer = NULL;
|
|
|
|
const char *which = NULL;
|
|
|
|
if (!*arguments)
|
|
|
|
buffer = ctx->current_buffer;
|
|
|
|
else
|
|
|
|
buffer = try_decode_buffer (ctx, (which = cut_word (&arguments)));
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
buffer_send_error (ctx, ctx->global_buffer,
|
|
|
|
"%s: %s", "No such buffer", which);
|
|
|
|
else if (buffer == ctx->global_buffer)
|
|
|
|
buffer_send_error (ctx, ctx->global_buffer,
|
|
|
|
"Can't close the global buffer");
|
|
|
|
else if (buffer == ctx->server_buffer)
|
|
|
|
buffer_send_error (ctx, ctx->global_buffer,
|
|
|
|
"Can't close the server buffer");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (buffer == ctx->current_buffer)
|
|
|
|
buffer_activate (ctx, buffer_next (ctx, 1));
|
|
|
|
buffer_remove (ctx, buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool
|
2015-04-17 21:29:18 +02:00
|
|
|
handle_command_buffer (struct app_context *ctx, char *arguments)
|
2015-04-16 00:56:05 +02:00
|
|
|
{
|
2015-04-17 21:29:18 +02:00
|
|
|
char *action = cut_word (&arguments);
|
|
|
|
if (try_handle_buffer_goto (ctx, action))
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-17 21:29:18 +02:00
|
|
|
|
|
|
|
// XXX: also build a prefix map?
|
|
|
|
// TODO: some subcommand to print N last lines from the buffer
|
|
|
|
if (!strcasecmp_ascii (action, "list"))
|
2015-04-25 02:41:52 +02:00
|
|
|
show_buffers_list (ctx);
|
2015-04-17 21:29:18 +02:00
|
|
|
else if (!strcasecmp_ascii (action, "clear"))
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
else if (!strcasecmp_ascii (action, "move"))
|
|
|
|
{
|
|
|
|
// TODO: unlink the buffer and link it back at index;
|
|
|
|
// we will probably need to extend liberty for this
|
|
|
|
}
|
|
|
|
else if (!strcasecmp_ascii (action, "close"))
|
2015-04-25 02:41:52 +02:00
|
|
|
handle_buffer_close (ctx, arguments);
|
2015-04-17 21:29:18 +02:00
|
|
|
else
|
2015-04-25 01:49:34 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2015-04-16 00:56:05 +02:00
|
|
|
}
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool
|
2015-04-23 02:48:25 +02:00
|
|
|
handle_command_msg (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
2015-04-23 03:07:48 +02:00
|
|
|
if (!server_command_check (ctx, "send messages"))
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-23 02:48:25 +02:00
|
|
|
if (!*arguments)
|
2015-04-25 01:49:34 +02:00
|
|
|
return false;
|
2015-04-23 02:48:25 +02:00
|
|
|
|
|
|
|
char *target = cut_word (&arguments);
|
|
|
|
if (!*arguments)
|
|
|
|
buffer_send_error (ctx, ctx->server_buffer, "No text to send");
|
|
|
|
else
|
|
|
|
SEND_AUTOSPLIT_PRIVMSG (ctx, target, arguments);
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-23 02:48:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool
|
2015-04-23 02:48:25 +02:00
|
|
|
handle_command_query (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
2015-04-23 03:07:48 +02:00
|
|
|
if (!server_command_check (ctx, "send messages"))
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-23 02:48:25 +02:00
|
|
|
if (!*arguments)
|
2015-04-25 01:49:34 +02:00
|
|
|
return false;
|
2015-04-23 02:48:25 +02:00
|
|
|
|
|
|
|
char *target = cut_word (&arguments);
|
|
|
|
if (irc_is_channel (ctx, target))
|
|
|
|
buffer_send_error (ctx, ctx->server_buffer, "Cannot query a channel");
|
2015-04-23 03:07:48 +02:00
|
|
|
else if (!*arguments)
|
2015-04-23 02:48:25 +02:00
|
|
|
buffer_send_error (ctx, ctx->server_buffer, "No text to send");
|
|
|
|
else
|
|
|
|
{
|
2015-04-23 03:10:34 +02:00
|
|
|
buffer_activate (ctx, irc_get_or_make_user_buffer (ctx, target));
|
2015-04-23 02:48:25 +02:00
|
|
|
SEND_AUTOSPLIT_PRIVMSG (ctx, target, arguments);
|
|
|
|
}
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-23 02:48:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool
|
2015-04-23 02:48:25 +02:00
|
|
|
handle_command_notice (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
2015-04-23 03:07:48 +02:00
|
|
|
if (!server_command_check (ctx, "send messages"))
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-23 02:48:25 +02:00
|
|
|
if (!*arguments)
|
2015-04-25 01:49:34 +02:00
|
|
|
return false;
|
2015-04-23 02:48:25 +02:00
|
|
|
|
|
|
|
char *target = cut_word (&arguments);
|
|
|
|
if (!*arguments)
|
|
|
|
buffer_send_error (ctx, ctx->server_buffer, "No text to send");
|
|
|
|
else
|
|
|
|
SEND_AUTOSPLIT_NOTICE (ctx, target, arguments);
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-23 02:48:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-26 22:53:38 +02:00
|
|
|
static bool
|
|
|
|
handle_command_ctcp (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
|
|
|
if (!server_command_check (ctx, "send messages"))
|
|
|
|
return true;
|
|
|
|
if (!*arguments)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char *target = cut_word (&arguments);
|
|
|
|
if (!*arguments)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char *tag = cut_word (&arguments);
|
|
|
|
for (char *p = tag; *p; p++)
|
|
|
|
*p = toupper_ascii (*p);
|
|
|
|
|
|
|
|
if (*arguments)
|
|
|
|
irc_send (ctx, "PRIVMSG %s :\x01%s %s\x01", target, tag, arguments);
|
|
|
|
else
|
|
|
|
irc_send (ctx, "PRIVMSG %s :\x01%s\x01", target, tag);
|
|
|
|
|
|
|
|
buffer_send_status (ctx, ctx->server_buffer,
|
|
|
|
"CTCP query to %s: %s", target, tag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
handle_command_me (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
|
|
|
if (!server_command_check (ctx, "send messages"))
|
|
|
|
return true;
|
|
|
|
|
2015-04-26 23:30:54 +02:00
|
|
|
if (ctx->current_buffer->type == BUFFER_CHANNEL)
|
|
|
|
SEND_AUTOSPLIT_ACTION (ctx,
|
|
|
|
ctx->current_buffer->channel->name, arguments);
|
|
|
|
else if (ctx->current_buffer->type == BUFFER_PM)
|
|
|
|
SEND_AUTOSPLIT_ACTION (ctx,
|
|
|
|
ctx->current_buffer->user->nickname, arguments);
|
|
|
|
else
|
|
|
|
buffer_send_error (ctx, ctx->server_buffer,
|
|
|
|
"Can't do this from a server buffer (%s)",
|
|
|
|
"send CTCP actions");
|
2015-04-26 22:53:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool
|
2015-04-17 21:29:18 +02:00
|
|
|
handle_command_quit (struct app_context *ctx, char *arguments)
|
2015-04-16 00:56:05 +02:00
|
|
|
{
|
|
|
|
if (ctx->irc_fd != -1)
|
|
|
|
{
|
|
|
|
if (*arguments)
|
|
|
|
irc_send (ctx, "QUIT :%s", arguments);
|
|
|
|
else
|
|
|
|
irc_send (ctx, "QUIT :%s", PROGRAM_NAME " " PROGRAM_VERSION);
|
|
|
|
}
|
|
|
|
initiate_quit (ctx);
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-16 00:56:05 +02:00
|
|
|
}
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool
|
2015-04-19 21:34:11 +02:00
|
|
|
handle_command_join (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
2015-04-23 03:07:48 +02:00
|
|
|
if (!server_command_check (ctx, "join"))
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-19 21:34:11 +02:00
|
|
|
|
|
|
|
if (*arguments)
|
|
|
|
// TODO: check if the arguments are in the form of
|
|
|
|
// "channel(,channel)* key(,key)*"
|
|
|
|
irc_send (ctx, "JOIN %s", arguments);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ctx->current_buffer->type != BUFFER_CHANNEL)
|
|
|
|
buffer_send_error (ctx, ctx->current_buffer,
|
|
|
|
"%s: %s", "Can't join",
|
|
|
|
"no argument given and this buffer is not a channel");
|
|
|
|
// TODO: have a better way of checking if we're on the channel
|
|
|
|
else if (ctx->current_buffer->channel->users)
|
|
|
|
buffer_send_error (ctx, ctx->current_buffer,
|
|
|
|
"%s: %s", "Can't join",
|
|
|
|
"you already are on the channel");
|
|
|
|
else
|
|
|
|
// TODO: send the key if known
|
|
|
|
irc_send (ctx, "JOIN %s", ctx->current_buffer->channel->name);
|
|
|
|
}
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-19 21:34:11 +02:00
|
|
|
}
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool
|
2015-04-19 21:34:11 +02:00
|
|
|
handle_command_part (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
2015-04-23 03:07:48 +02:00
|
|
|
if (!server_command_check (ctx, "part"))
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-19 21:34:11 +02:00
|
|
|
|
|
|
|
if (*arguments)
|
|
|
|
// TODO: check if the arguments are in the form of "channel(,channel)*"
|
2015-04-23 02:59:58 +02:00
|
|
|
// TODO: make sure to send the reason as one argument
|
2015-04-19 21:34:11 +02:00
|
|
|
irc_send (ctx, "PART %s", arguments);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ctx->current_buffer->type != BUFFER_CHANNEL)
|
|
|
|
buffer_send_error (ctx, ctx->current_buffer,
|
|
|
|
"%s: %s", "Can't part",
|
|
|
|
"no argument given and this buffer is not a channel");
|
|
|
|
// TODO: have a better way of checking if we're on the channel
|
|
|
|
else if (!ctx->current_buffer->channel->users)
|
|
|
|
buffer_send_error (ctx, ctx->current_buffer,
|
|
|
|
"%s: %s", "Can't join", "you're not on the channel");
|
|
|
|
else
|
|
|
|
irc_send (ctx, "PART %s", ctx->current_buffer->channel->name);
|
|
|
|
}
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-19 21:34:11 +02:00
|
|
|
}
|
|
|
|
|
2015-04-26 23:06:19 +02:00
|
|
|
static bool
|
|
|
|
handle_command_list (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
|
|
|
if (!server_command_check (ctx, "list channels"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (*arguments)
|
|
|
|
irc_send (ctx, "LIST %s", arguments);
|
|
|
|
else
|
|
|
|
irc_send (ctx, "LIST");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-27 23:06:20 +02:00
|
|
|
static bool
|
|
|
|
handle_command_nick (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
|
|
|
if (!server_command_check (ctx, "change nickname"))
|
|
|
|
return true;
|
|
|
|
if (!*arguments)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
irc_send (ctx, "NICK %s", cut_word (&arguments));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool
|
2015-04-17 23:18:07 +02:00
|
|
|
handle_command_quote (struct app_context *ctx, char *arguments)
|
|
|
|
{
|
2015-04-23 03:07:48 +02:00
|
|
|
if (!server_command_check (ctx, "quote"))
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-23 03:07:48 +02:00
|
|
|
|
2015-04-26 18:24:19 +02:00
|
|
|
irc_send (ctx, "%s", arguments);
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-17 23:18:07 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
static struct command_handler
|
2015-04-12 04:52:39 +02:00
|
|
|
{
|
2015-04-18 17:19:56 +02:00
|
|
|
const char *name;
|
2015-04-25 01:49:34 +02:00
|
|
|
bool (*handler) (struct app_context *ctx, char *arguments);
|
2015-04-18 17:19:56 +02:00
|
|
|
const char *description;
|
|
|
|
const char *usage;
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
2015-04-16 00:56:05 +02:00
|
|
|
g_command_handlers[] =
|
2015-04-12 04:52:39 +02:00
|
|
|
{
|
2015-04-18 17:19:56 +02:00
|
|
|
{ "help", handle_command_help, "Show help",
|
|
|
|
"[command]" },
|
|
|
|
{ "quit", handle_command_quit, "Quit the program",
|
|
|
|
"[message]" },
|
|
|
|
{ "buffer", handle_command_buffer, "Manage buffers",
|
|
|
|
"list | clear | move | { close [<number> | <name>] } | <number>" },
|
2015-04-23 02:48:25 +02:00
|
|
|
|
|
|
|
{ "msg", handle_command_msg, "Send message to a nick or channel",
|
|
|
|
"<target> <message>" },
|
|
|
|
{ "query", handle_command_query, "Send a private message to a nick",
|
|
|
|
"<nick> <message>" },
|
|
|
|
{ "notice", handle_command_notice, "Send notice to a nick or channel",
|
|
|
|
"<target> <message>" },
|
2015-04-26 22:53:38 +02:00
|
|
|
{ "ctcp", handle_command_ctcp, "Send a CTCP query",
|
|
|
|
"<target> <tag>" },
|
|
|
|
{ "me", handle_command_me, "Send a CTCP action",
|
|
|
|
"<message>" },
|
2015-04-18 17:19:56 +02:00
|
|
|
|
2015-04-19 21:34:11 +02:00
|
|
|
{ "join", handle_command_join, "Join channels",
|
2015-04-26 23:06:19 +02:00
|
|
|
"[<channel>[,<channel>...]]" },
|
2015-04-19 21:34:11 +02:00
|
|
|
{ "part", handle_command_part, "Leave channels",
|
2015-04-26 23:06:19 +02:00
|
|
|
"[<channel>[,<channel>...]]" },
|
2015-04-19 21:34:11 +02:00
|
|
|
#if 0
|
2015-04-18 17:19:56 +02:00
|
|
|
{ "cycle", NULL, "", "" },
|
|
|
|
|
|
|
|
{ "mode", NULL, "", "" },
|
|
|
|
{ "topic", NULL, "", "" },
|
|
|
|
{ "kick", NULL, "", "" },
|
|
|
|
{ "kickban", NULL, "", "" },
|
|
|
|
{ "ban", NULL, "", "" },
|
|
|
|
{ "invite", NULL, "", "" },
|
2015-04-26 23:06:19 +02:00
|
|
|
#endif
|
2015-04-18 17:19:56 +02:00
|
|
|
|
2015-04-26 23:06:19 +02:00
|
|
|
{ "list", handle_command_list, "List channels and their topic",
|
|
|
|
"[<channel>[,<channel>...]] [server]" },
|
|
|
|
#if 0
|
2015-04-18 17:19:56 +02:00
|
|
|
{ "names", NULL, "", "" },
|
|
|
|
{ "who", NULL, "", "" },
|
|
|
|
{ "whois", NULL, "", "" },
|
|
|
|
|
|
|
|
{ "motd", NULL, "", "" },
|
|
|
|
{ "away", NULL, "", "" },
|
2015-04-16 00:56:05 +02:00
|
|
|
#endif
|
2015-04-27 23:06:20 +02:00
|
|
|
{ "nick", handle_command_nick, "Change current nick",
|
|
|
|
"<nickname>" },
|
2015-04-18 17:19:56 +02:00
|
|
|
{ "quote", handle_command_quote, "Send a raw command to the server",
|
2015-04-27 23:06:20 +02:00
|
|
|
"<command>" },
|
2015-04-12 04:52:39 +02:00
|
|
|
};
|
|
|
|
|
2015-04-25 01:49:34 +02:00
|
|
|
static bool
|
2015-04-17 21:29:18 +02:00
|
|
|
handle_command_help (struct app_context *ctx, char *arguments)
|
2015-04-12 04:52:39 +02:00
|
|
|
{
|
2015-04-25 02:08:14 +02:00
|
|
|
if (!*arguments)
|
2015-04-18 17:19:56 +02:00
|
|
|
{
|
2015-04-25 02:08:14 +02:00
|
|
|
buffer_send_status (ctx, ctx->global_buffer, "%s", "");
|
|
|
|
buffer_send_status (ctx, ctx->global_buffer, "Commands:");
|
|
|
|
for (size_t i = 0; i < N_ELEMENTS (g_command_handlers); i++)
|
2015-04-26 18:58:39 +02:00
|
|
|
{
|
|
|
|
struct command_handler *handler = &g_command_handlers[i];
|
|
|
|
buffer_send_status (ctx, ctx->global_buffer, " %s: %s",
|
|
|
|
handler->name, handler->description);
|
|
|
|
}
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-18 17:19:56 +02:00
|
|
|
}
|
|
|
|
|
2015-04-25 02:08:14 +02:00
|
|
|
char *command = cut_word (&arguments);
|
2015-04-18 17:19:56 +02:00
|
|
|
for (size_t i = 0; i < N_ELEMENTS (g_command_handlers); i++)
|
2015-04-26 18:58:39 +02:00
|
|
|
{
|
|
|
|
struct command_handler *handler = &g_command_handlers[i];
|
|
|
|
if (!strcasecmp_ascii (command, handler->name))
|
2015-04-25 02:08:14 +02:00
|
|
|
{
|
|
|
|
buffer_send_status (ctx, ctx->global_buffer, "%s", "");
|
2015-04-26 18:58:39 +02:00
|
|
|
buffer_send_status (ctx, ctx->global_buffer, "%s: %s",
|
|
|
|
handler->name, handler->description);
|
|
|
|
buffer_send_status (ctx, ctx->global_buffer, " Arguments: %s",
|
|
|
|
handler->usage);
|
2015-04-25 02:08:14 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-26 18:58:39 +02:00
|
|
|
}
|
2015-04-25 02:08:14 +02:00
|
|
|
buffer_send_error (ctx, ctx->global_buffer,
|
|
|
|
"%s: %s", "No such command", command);
|
2015-04-25 01:49:34 +02:00
|
|
|
return true;
|
2015-04-16 00:56:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_handler_cmp_by_length (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct command_handler *first = a;
|
|
|
|
const struct command_handler *second = b;
|
|
|
|
return strlen (first->name) - strlen (second->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_partial_matching_user_command_map (struct str_map *partial)
|
|
|
|
{
|
2015-04-17 23:18:07 +02:00
|
|
|
// Trivially create a partial matching map
|
2015-04-16 00:56:05 +02:00
|
|
|
str_map_init (partial);
|
|
|
|
partial->key_xfrm = tolower_ascii_strxfrm;
|
|
|
|
|
|
|
|
// We process them from the longest to the shortest one,
|
|
|
|
// so that common prefixes favor shorter entries
|
|
|
|
struct command_handler *by_length[N_ELEMENTS (g_command_handlers)];
|
|
|
|
for (size_t i = 0; i < N_ELEMENTS (by_length); i++)
|
|
|
|
by_length[i] = &g_command_handlers[i];
|
|
|
|
qsort (by_length, N_ELEMENTS (by_length), sizeof *by_length,
|
|
|
|
command_handler_cmp_by_length);
|
|
|
|
|
|
|
|
for (size_t i = N_ELEMENTS (by_length); i--; )
|
|
|
|
{
|
|
|
|
char *copy = xstrdup (by_length[i]->name);
|
|
|
|
for (size_t part = strlen (copy); part; part--)
|
|
|
|
{
|
|
|
|
copy[part] = '\0';
|
|
|
|
str_map_set (partial, copy, by_length[i]);
|
|
|
|
}
|
|
|
|
free (copy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_user_command (struct app_context *ctx, char *command)
|
|
|
|
{
|
|
|
|
static bool initialized = false;
|
2015-04-18 03:25:10 +02:00
|
|
|
static struct str_map partial;
|
2015-04-16 00:56:05 +02:00
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
init_partial_matching_user_command_map (&partial);
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
2015-04-17 21:29:18 +02:00
|
|
|
char *name = cut_word (&command);
|
|
|
|
if (try_handle_buffer_goto (ctx, name))
|
|
|
|
return;
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-17 21:29:18 +02:00
|
|
|
struct command_handler *handler = str_map_find (&partial, name);
|
2015-04-25 01:49:34 +02:00
|
|
|
if (!handler)
|
2015-04-17 23:18:07 +02:00
|
|
|
buffer_send_error (ctx, ctx->global_buffer,
|
2015-04-18 17:51:28 +02:00
|
|
|
"%s: %s", "No such command", name);
|
2015-04-25 01:49:34 +02:00
|
|
|
else if (!handler->handler (ctx, command))
|
|
|
|
buffer_send_error (ctx, ctx->global_buffer,
|
|
|
|
"%s: /%s %s", "Usage", handler->name, handler->usage);
|
2015-04-16 00:56:05 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 22:20:03 +02:00
|
|
|
static void
|
|
|
|
send_message_to_target (struct app_context *ctx,
|
|
|
|
const char *target, char *message, struct buffer *buffer)
|
|
|
|
{
|
|
|
|
if (ctx->irc_fd == -1)
|
|
|
|
{
|
|
|
|
buffer_send_error (ctx, buffer, "Not connected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-23 02:48:25 +02:00
|
|
|
SEND_AUTOSPLIT_PRIVMSG (ctx, target, message);
|
2015-04-19 22:20:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
static void
|
|
|
|
send_message_to_current_buffer (struct app_context *ctx, char *message)
|
|
|
|
{
|
|
|
|
struct buffer *buffer = ctx->current_buffer;
|
2015-04-17 23:18:07 +02:00
|
|
|
hard_assert (buffer != NULL);
|
2015-04-16 00:56:05 +02:00
|
|
|
|
|
|
|
switch (buffer->type)
|
|
|
|
{
|
|
|
|
case BUFFER_GLOBAL:
|
|
|
|
case BUFFER_SERVER:
|
2015-04-18 17:51:28 +02:00
|
|
|
buffer_send_error (ctx, buffer, "This buffer is not a channel");
|
2015-04-16 00:56:05 +02:00
|
|
|
break;
|
|
|
|
case BUFFER_CHANNEL:
|
2015-04-19 22:20:03 +02:00
|
|
|
send_message_to_target (ctx, buffer->channel->name, message, buffer);
|
2015-04-19 21:34:52 +02:00
|
|
|
break;
|
2015-04-16 00:56:05 +02:00
|
|
|
case BUFFER_PM:
|
2015-04-19 22:20:03 +02:00
|
|
|
send_message_to_target (ctx, buffer->user->nickname, message, buffer);
|
2015-04-16 00:56:05 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_input (struct app_context *ctx, char *user_input)
|
|
|
|
{
|
|
|
|
char *input;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!(input = iconv_xstrdup (ctx->term_to_utf8, user_input, -1, &len)))
|
|
|
|
print_error ("character conversion failed for `%s'", "user input");
|
2015-04-16 20:51:50 +02:00
|
|
|
else if (input[0] != '/')
|
2015-04-16 00:56:05 +02:00
|
|
|
send_message_to_current_buffer (ctx, input);
|
2015-04-16 20:51:50 +02:00
|
|
|
else if (input[1] == '/')
|
|
|
|
send_message_to_current_buffer (ctx, input + 1);
|
|
|
|
else
|
|
|
|
process_user_command (ctx, input + 1);
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
free (input);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Supporting code (continued) ---------------------------------------------
|
|
|
|
|
|
|
|
enum irc_read_result
|
|
|
|
{
|
|
|
|
IRC_READ_OK, ///< Some data were read successfully
|
|
|
|
IRC_READ_EOF, ///< The server has closed connection
|
|
|
|
IRC_READ_AGAIN, ///< No more data at the moment
|
|
|
|
IRC_READ_ERROR ///< General connection failure
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum irc_read_result
|
|
|
|
irc_fill_read_buffer_ssl (struct app_context *ctx, struct str *buf)
|
|
|
|
{
|
|
|
|
int n_read;
|
|
|
|
start:
|
|
|
|
n_read = SSL_read (ctx->ssl, buf->str + buf->len,
|
|
|
|
buf->alloc - buf->len - 1 /* null byte */);
|
|
|
|
|
|
|
|
const char *error_info = NULL;
|
|
|
|
switch (xssl_get_error (ctx->ssl, n_read, &error_info))
|
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
buf->str[buf->len += n_read] = '\0';
|
|
|
|
return IRC_READ_OK;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
return IRC_READ_EOF;
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
return IRC_READ_AGAIN;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
{
|
|
|
|
// Let it finish the handshake as we don't poll for writability;
|
|
|
|
// any errors are to be collected by SSL_read() in the next iteration
|
|
|
|
struct pollfd pfd = { .fd = ctx->irc_fd, .events = POLLOUT };
|
|
|
|
soft_assert (poll (&pfd, 1, 0) > 0);
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
case XSSL_ERROR_TRY_AGAIN:
|
|
|
|
goto start;
|
|
|
|
default:
|
2015-04-27 01:36:33 +02:00
|
|
|
LOG_FUNC_FAILURE ("SSL_read", error_info);
|
2015-04-12 04:52:39 +02:00
|
|
|
return IRC_READ_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum irc_read_result
|
|
|
|
irc_fill_read_buffer (struct app_context *ctx, struct str *buf)
|
|
|
|
{
|
|
|
|
ssize_t n_read;
|
|
|
|
start:
|
|
|
|
n_read = recv (ctx->irc_fd, buf->str + buf->len,
|
|
|
|
buf->alloc - buf->len - 1 /* null byte */, 0);
|
|
|
|
|
|
|
|
if (n_read > 0)
|
|
|
|
{
|
|
|
|
buf->str[buf->len += n_read] = '\0';
|
|
|
|
return IRC_READ_OK;
|
|
|
|
}
|
|
|
|
if (n_read == 0)
|
|
|
|
return IRC_READ_EOF;
|
|
|
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return IRC_READ_AGAIN;
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto start;
|
|
|
|
|
2015-04-27 01:36:33 +02:00
|
|
|
LOG_LIBC_FAILURE ("recv");
|
2015-04-12 04:52:39 +02:00
|
|
|
return IRC_READ_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool irc_connect (struct app_context *, struct error **);
|
|
|
|
static void irc_queue_reconnect (struct app_context *);
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_cancel_timers (struct app_context *ctx)
|
|
|
|
{
|
|
|
|
poller_timer_reset (&ctx->timeout_tmr);
|
|
|
|
poller_timer_reset (&ctx->ping_tmr);
|
|
|
|
poller_timer_reset (&ctx->reconnect_tmr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_irc_reconnect_timeout (void *user_data)
|
|
|
|
{
|
|
|
|
struct app_context *ctx = user_data;
|
|
|
|
|
|
|
|
struct error *e = NULL;
|
|
|
|
if (irc_connect (ctx, &e))
|
|
|
|
return;
|
|
|
|
|
2015-04-17 23:18:07 +02:00
|
|
|
buffer_send_error (ctx, ctx->server_buffer, "%s", e->message);
|
2015-04-12 04:52:39 +02:00
|
|
|
error_free (e);
|
|
|
|
irc_queue_reconnect (ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_queue_reconnect (struct app_context *ctx)
|
|
|
|
{
|
2015-04-19 21:34:33 +02:00
|
|
|
// TODO: exponentional backoff
|
2015-04-12 04:52:39 +02:00
|
|
|
hard_assert (ctx->irc_fd == -1);
|
2015-04-17 23:18:07 +02:00
|
|
|
buffer_send_status (ctx, ctx->server_buffer,
|
2015-04-18 17:51:28 +02:00
|
|
|
"Trying to reconnect in %ld seconds...", ctx->reconnect_delay);
|
2015-04-12 04:52:39 +02:00
|
|
|
poller_timer_set (&ctx->reconnect_tmr, ctx->reconnect_delay * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_irc_disconnected (struct app_context *ctx)
|
|
|
|
{
|
|
|
|
// Get rid of the dead socket and related things
|
|
|
|
if (ctx->ssl)
|
|
|
|
{
|
|
|
|
SSL_free (ctx->ssl);
|
|
|
|
ctx->ssl = NULL;
|
|
|
|
SSL_CTX_free (ctx->ssl_ctx);
|
|
|
|
ctx->ssl_ctx = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
xclose (ctx->irc_fd);
|
|
|
|
ctx->irc_fd = -1;
|
|
|
|
ctx->irc_ready = false;
|
2015-04-20 22:51:30 +02:00
|
|
|
|
2015-04-20 22:09:56 +02:00
|
|
|
user_unref (ctx->irc_user);
|
|
|
|
ctx->irc_user = NULL;
|
2015-04-20 22:51:30 +02:00
|
|
|
|
2015-04-19 22:47:45 +02:00
|
|
|
free (ctx->irc_user_mode);
|
|
|
|
ctx->irc_user_mode = NULL;
|
2015-04-20 22:09:56 +02:00
|
|
|
free (ctx->irc_user_host);
|
|
|
|
ctx->irc_user_host = NULL;
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
ctx->irc_event.closed = true;
|
|
|
|
poller_fd_reset (&ctx->irc_event);
|
|
|
|
|
|
|
|
// All of our timers have lost their meaning now
|
|
|
|
irc_cancel_timers (ctx);
|
|
|
|
|
|
|
|
if (ctx->quitting)
|
|
|
|
try_finish_quit (ctx);
|
|
|
|
else if (!ctx->reconnect)
|
2015-04-19 21:34:33 +02:00
|
|
|
// XXX: not sure if we want this in a client
|
2015-04-12 04:52:39 +02:00
|
|
|
initiate_quit (ctx);
|
|
|
|
else
|
|
|
|
irc_queue_reconnect (ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_irc_ping_timeout (void *user_data)
|
|
|
|
{
|
|
|
|
struct app_context *ctx = user_data;
|
2015-04-18 17:51:28 +02:00
|
|
|
buffer_send_error (ctx, ctx->server_buffer, "Connection timeout");
|
2015-04-12 04:52:39 +02:00
|
|
|
on_irc_disconnected (ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_irc_timeout (void *user_data)
|
|
|
|
{
|
|
|
|
// Provoke a response from the server
|
|
|
|
struct app_context *ctx = user_data;
|
|
|
|
irc_send (ctx, "PING :%s",
|
|
|
|
(char *) str_map_find (&ctx->config, "nickname"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
irc_reset_connection_timeouts (struct app_context *ctx)
|
|
|
|
{
|
|
|
|
irc_cancel_timers (ctx);
|
|
|
|
poller_timer_set (&ctx->timeout_tmr, 3 * 60 * 1000);
|
|
|
|
poller_timer_set (&ctx->ping_tmr, (3 * 60 + 30) * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_irc_readable (const struct pollfd *fd, struct app_context *ctx)
|
|
|
|
{
|
|
|
|
if (fd->revents & ~(POLLIN | POLLHUP | POLLERR))
|
|
|
|
print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);
|
|
|
|
|
|
|
|
(void) set_blocking (ctx->irc_fd, false);
|
|
|
|
|
|
|
|
struct str *buf = &ctx->read_buffer;
|
|
|
|
enum irc_read_result (*fill_buffer)(struct app_context *, struct str *)
|
|
|
|
= ctx->ssl
|
|
|
|
? irc_fill_read_buffer_ssl
|
|
|
|
: irc_fill_read_buffer;
|
|
|
|
bool disconnected = false;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
str_ensure_space (buf, 512);
|
|
|
|
switch (fill_buffer (ctx, buf))
|
|
|
|
{
|
|
|
|
case IRC_READ_AGAIN:
|
|
|
|
goto end;
|
|
|
|
case IRC_READ_ERROR:
|
2015-04-17 23:18:07 +02:00
|
|
|
buffer_send_error (ctx, ctx->server_buffer,
|
2015-04-18 17:51:28 +02:00
|
|
|
"Reading from the IRC server failed");
|
2015-04-12 04:52:39 +02:00
|
|
|
disconnected = true;
|
|
|
|
goto end;
|
|
|
|
case IRC_READ_EOF:
|
2015-04-17 23:18:07 +02:00
|
|
|
buffer_send_error (ctx, ctx->server_buffer,
|
2015-04-18 17:51:28 +02:00
|
|
|
"The IRC server closed the connection");
|
2015-04-12 04:52:39 +02:00
|
|
|
disconnected = true;
|
|
|
|
goto end;
|
|
|
|
case IRC_READ_OK:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf->len >= (1 << 20))
|
|
|
|
{
|
2015-04-17 23:18:07 +02:00
|
|
|
buffer_send_error (ctx, ctx->server_buffer,
|
2015-04-18 17:51:28 +02:00
|
|
|
"The IRC server seems to spew out data frantically");
|
2015-04-12 04:52:39 +02:00
|
|
|
irc_shutdown (ctx);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
(void) set_blocking (ctx->irc_fd, true);
|
|
|
|
irc_process_buffer (buf, irc_process_message, ctx);
|
|
|
|
|
|
|
|
if (disconnected)
|
|
|
|
on_irc_disconnected (ctx);
|
|
|
|
else
|
|
|
|
irc_reset_connection_timeouts (ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
irc_connect (struct app_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
const char *irc_host = str_map_find (&ctx->config, "irc_host");
|
|
|
|
const char *irc_port = str_map_find (&ctx->config, "irc_port");
|
|
|
|
|
|
|
|
const char *socks_host = str_map_find (&ctx->config, "socks_host");
|
|
|
|
const char *socks_port = str_map_find (&ctx->config, "socks_port");
|
|
|
|
const char *socks_username = str_map_find (&ctx->config, "socks_username");
|
|
|
|
const char *socks_password = str_map_find (&ctx->config, "socks_password");
|
|
|
|
|
|
|
|
const char *nickname = str_map_find (&ctx->config, "nickname");
|
|
|
|
const char *username = str_map_find (&ctx->config, "username");
|
|
|
|
const char *realname = str_map_find (&ctx->config, "realname");
|
|
|
|
|
|
|
|
// We have a default value for these
|
|
|
|
hard_assert (irc_port && socks_port);
|
|
|
|
|
|
|
|
// These are filled automatically if needed
|
|
|
|
hard_assert (nickname && username && realname);
|
|
|
|
|
|
|
|
// TODO: again, get rid of `struct error' in here. The question is: how
|
|
|
|
// do we tell our caller that he should not try to reconnect?
|
|
|
|
bool use_ssl;
|
|
|
|
if (!irc_get_boolean_from_config (ctx, "ssl", &use_ssl, e))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (socks_host)
|
|
|
|
{
|
|
|
|
char *address = format_host_port_pair (irc_host, irc_port);
|
|
|
|
char *socks_address = format_host_port_pair (socks_host, socks_port);
|
2015-04-17 23:18:07 +02:00
|
|
|
buffer_send_status (ctx, ctx->server_buffer,
|
2015-04-18 17:51:28 +02:00
|
|
|
"Connecting to %s via %s...", address, socks_address);
|
2015-04-12 04:52:39 +02:00
|
|
|
free (socks_address);
|
|
|
|
free (address);
|
|
|
|
|
|
|
|
struct error *error = NULL;
|
|
|
|
int fd = socks_connect (socks_host, socks_port, irc_host, irc_port,
|
|
|
|
socks_username, socks_password, &error);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
error_set (e, "%s: %s", "SOCKS connection failed", error->message);
|
|
|
|
error_free (error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ctx->irc_fd = fd;
|
|
|
|
}
|
|
|
|
else if (!irc_establish_connection (ctx, irc_host, irc_port, e))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (use_ssl && !irc_initialize_ssl (ctx, e))
|
|
|
|
{
|
|
|
|
xclose (ctx->irc_fd);
|
|
|
|
ctx->irc_fd = -1;
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-18 17:51:28 +02:00
|
|
|
buffer_send_status (ctx, ctx->server_buffer, "Connection established");
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
poller_fd_init (&ctx->irc_event, &ctx->poller, ctx->irc_fd);
|
|
|
|
ctx->irc_event.dispatcher = (poller_fd_fn) on_irc_readable;
|
|
|
|
ctx->irc_event.user_data = ctx;
|
|
|
|
|
|
|
|
poller_fd_set (&ctx->irc_event, POLLIN);
|
|
|
|
irc_reset_connection_timeouts (ctx);
|
|
|
|
|
|
|
|
irc_send (ctx, "NICK %s", nickname);
|
|
|
|
irc_send (ctx, "USER %s 8 * :%s", username, realname);
|
2015-04-19 22:47:45 +02:00
|
|
|
|
|
|
|
// XXX: maybe we should wait for the first message from the server
|
2015-04-21 00:04:34 +02:00
|
|
|
ctx->irc_user = irc_make_user (ctx, xstrdup (nickname));
|
2015-04-19 22:47:45 +02:00
|
|
|
ctx->irc_user_mode = xstrdup ("");
|
2015-04-20 22:09:56 +02:00
|
|
|
ctx->irc_user_host = NULL;
|
2015-04-12 04:52:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- I/O event handlers ------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_signal_pipe_readable (const struct pollfd *fd, struct app_context *ctx)
|
|
|
|
{
|
|
|
|
char dummy;
|
|
|
|
(void) read (fd->fd, &dummy, 1);
|
|
|
|
|
|
|
|
if (g_termination_requested && !ctx->quitting)
|
|
|
|
{
|
|
|
|
// There may be a timer set to reconnect to the server
|
|
|
|
irc_cancel_timers (ctx);
|
|
|
|
|
|
|
|
if (ctx->irc_fd != -1)
|
|
|
|
irc_send (ctx, "QUIT :Terminated by signal");
|
|
|
|
initiate_quit (ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_winch_received)
|
2015-04-13 00:06:08 +02:00
|
|
|
{
|
2015-04-12 04:52:39 +02:00
|
|
|
// 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 ();
|
2015-04-13 00:06:08 +02:00
|
|
|
rl_get_screen_size (&ctx->lines, &ctx->columns);
|
|
|
|
}
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_tty_readable (const struct pollfd *fd, struct app_context *ctx)
|
|
|
|
{
|
|
|
|
(void) ctx;
|
|
|
|
|
|
|
|
if (fd->revents & ~(POLLIN | POLLHUP | POLLERR))
|
|
|
|
print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);
|
|
|
|
|
|
|
|
rl_callback_read_char ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_readline_input (char *line)
|
|
|
|
{
|
|
|
|
if (line)
|
|
|
|
{
|
|
|
|
if (*line)
|
|
|
|
add_history (line);
|
|
|
|
|
|
|
|
process_input (g_ctx, line);
|
|
|
|
free (line);
|
|
|
|
}
|
2015-04-18 03:25:10 +02:00
|
|
|
else
|
2015-04-18 16:03:50 +02:00
|
|
|
{
|
|
|
|
app_readline_erase_to_bol (g_ctx->readline_prompt);
|
|
|
|
rl_ding ();
|
|
|
|
}
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-18 16:10:11 +02:00
|
|
|
// initiate_quit() disables readline; we just wait then
|
|
|
|
if (!g_ctx->quitting)
|
2015-04-18 03:25:10 +02:00
|
|
|
g_ctx->readline_prompt_shown = true;
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Configuration loading ---------------------------------------------------
|
|
|
|
|
|
|
|
static bool
|
|
|
|
read_hexa_escape (const char **cursor, struct str *output)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char c, code = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
c = tolower (*(*cursor));
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
code = (code << 4) | (c - '0');
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
code = (code << 4) | (c - 'a' + 10);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
(*cursor)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
str_append_c (output, code);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
read_octal_escape (const char **cursor, struct str *output)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char c, code = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
c = *(*cursor);
|
|
|
|
if (c < '0' || c > '7')
|
|
|
|
break;
|
|
|
|
|
|
|
|
code = (code << 3) | (c - '0');
|
|
|
|
(*cursor)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!i)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
str_append_c (output, code);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
read_string_escape_sequence (const char **cursor,
|
|
|
|
struct str *output, struct error **e)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
switch ((c = *(*cursor)++))
|
|
|
|
{
|
|
|
|
case '?': str_append_c (output, '?'); break;
|
|
|
|
case '"': str_append_c (output, '"'); break;
|
|
|
|
case '\\': str_append_c (output, '\\'); break;
|
|
|
|
case 'a': str_append_c (output, '\a'); break;
|
|
|
|
case 'b': str_append_c (output, '\b'); break;
|
|
|
|
case 'f': str_append_c (output, '\f'); break;
|
|
|
|
case 'n': str_append_c (output, '\n'); break;
|
|
|
|
case 'r': str_append_c (output, '\r'); break;
|
|
|
|
case 't': str_append_c (output, '\t'); break;
|
|
|
|
case 'v': str_append_c (output, '\v'); break;
|
|
|
|
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
str_append_c (output, '\x1b');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
if (!read_hexa_escape (cursor, output))
|
|
|
|
FAIL ("invalid hexadecimal escape");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
FAIL ("premature end of escape sequence");
|
|
|
|
|
|
|
|
default:
|
|
|
|
(*cursor)--;
|
|
|
|
if (!read_octal_escape (cursor, output))
|
|
|
|
FAIL ("unknown escape sequence");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
unescape_string (const char *s, struct str *output, struct error **e)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
while ((c = *s++))
|
|
|
|
{
|
|
|
|
if (c != '\\')
|
|
|
|
str_append_c (output, c);
|
|
|
|
else if (!read_string_escape_sequence (&s, output, e))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
autofill_user_info (struct app_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
const char *nickname = str_map_find (&ctx->config, "nickname");
|
|
|
|
const char *username = str_map_find (&ctx->config, "username");
|
|
|
|
const char *realname = str_map_find (&ctx->config, "realname");
|
|
|
|
|
|
|
|
if (nickname && username && realname)
|
|
|
|
return true;
|
|
|
|
|
2015-04-17 23:18:07 +02:00
|
|
|
// Read POSIX user info and fill the configuration if needed
|
2015-04-12 04:52:39 +02:00
|
|
|
struct passwd *pwd = getpwuid (geteuid ());
|
|
|
|
if (!pwd)
|
|
|
|
FAIL ("cannot retrieve user information: %s", strerror (errno));
|
|
|
|
|
|
|
|
if (!nickname)
|
|
|
|
str_map_set (&ctx->config, "nickname", xstrdup (pwd->pw_name));
|
|
|
|
if (!username)
|
|
|
|
str_map_set (&ctx->config, "username", xstrdup (pwd->pw_name));
|
|
|
|
|
|
|
|
// Not all systems have the GECOS field but the vast majority does
|
|
|
|
if (!realname)
|
|
|
|
{
|
|
|
|
char *gecos = pwd->pw_gecos;
|
|
|
|
|
|
|
|
// The first comma, if any, ends the user's real name
|
|
|
|
char *comma = strchr (gecos, ',');
|
|
|
|
if (comma)
|
|
|
|
*comma = '\0';
|
|
|
|
|
2015-04-18 03:25:10 +02:00
|
|
|
str_map_set (&ctx->config, "realname", xstrdup (gecos));
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-04-15 15:49:33 +02:00
|
|
|
unescape_config (struct str_map *input, struct str_map *output, struct error **e)
|
2015-04-12 04:52:39 +02:00
|
|
|
{
|
2015-04-15 15:49:33 +02:00
|
|
|
struct error *error = NULL;
|
2015-04-12 04:52:39 +02:00
|
|
|
struct str_map_iter iter;
|
2015-04-15 15:49:33 +02:00
|
|
|
str_map_iter_init (&iter, input);
|
2015-04-12 04:52:39 +02:00
|
|
|
while (str_map_iter_next (&iter))
|
|
|
|
{
|
|
|
|
struct str value;
|
|
|
|
str_init (&value);
|
2015-04-15 15:49:33 +02:00
|
|
|
if (!unescape_string (iter.link->data, &value, &error))
|
2015-04-12 04:52:39 +02:00
|
|
|
{
|
2015-04-15 15:49:33 +02:00
|
|
|
error_set (e, "error reading configuration: %s: %s",
|
|
|
|
iter.link->key, error->message);
|
|
|
|
error_free (error);
|
|
|
|
return false;
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 15:49:33 +02:00
|
|
|
str_map_set (output, iter.link->key, str_steal (&value));
|
2015-04-12 04:52:39 +02:00
|
|
|
}
|
2015-04-15 15:49:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load_config (struct app_context *ctx, struct error **e)
|
|
|
|
{
|
|
|
|
// TODO: employ a better configuration file format, so that we don't have
|
|
|
|
// to do this convoluted post-processing anymore.
|
|
|
|
|
|
|
|
struct str_map map;
|
|
|
|
str_map_init (&map);
|
|
|
|
map.free = free;
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-15 15:49:33 +02:00
|
|
|
bool success = read_config_file (&map, e) &&
|
|
|
|
unescape_config (&map, &ctx->config, e) &&
|
|
|
|
autofill_user_info (ctx, e);
|
|
|
|
str_map_free (&map);
|
|
|
|
if (!success)
|
2015-04-12 04:52:39 +02:00
|
|
|
return false;
|
|
|
|
|
2015-04-18 03:25:10 +02:00
|
|
|
const char *irc_host = str_map_find (&ctx->config, "irc_host");
|
|
|
|
if (!irc_host)
|
|
|
|
{
|
|
|
|
error_set (e, "no hostname specified in configuration");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-16 00:56:05 +02:00
|
|
|
if (!irc_get_boolean_from_config (ctx,
|
|
|
|
"reconnect", &ctx->reconnect, e)
|
|
|
|
|| !irc_get_boolean_from_config (ctx,
|
|
|
|
"isolate_buffers", &ctx->isolate_buffers, e))
|
2015-04-12 04:52:39 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const char *delay_str = str_map_find (&ctx->config, "reconnect_delay");
|
|
|
|
hard_assert (delay_str != NULL); // We have a default value for this
|
|
|
|
if (!xstrtoul (&ctx->reconnect_delay, delay_str, 10))
|
|
|
|
{
|
|
|
|
error_set (e, "invalid configuration value for `%s'",
|
|
|
|
"reconnect_delay");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Main program ------------------------------------------------------------
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_poller_events (struct app_context *ctx)
|
|
|
|
{
|
|
|
|
poller_timer_init (&ctx->timeout_tmr, &ctx->poller);
|
|
|
|
ctx->timeout_tmr.dispatcher = on_irc_timeout;
|
|
|
|
ctx->timeout_tmr.user_data = ctx;
|
|
|
|
|
|
|
|
poller_timer_init (&ctx->ping_tmr, &ctx->poller);
|
|
|
|
ctx->ping_tmr.dispatcher = on_irc_ping_timeout;
|
|
|
|
ctx->ping_tmr.user_data = ctx;
|
|
|
|
|
|
|
|
poller_timer_init (&ctx->reconnect_tmr, &ctx->poller);
|
|
|
|
ctx->reconnect_tmr.dispatcher = on_irc_reconnect_timeout;
|
|
|
|
ctx->reconnect_tmr.user_data = ctx;
|
|
|
|
|
|
|
|
poller_fd_init (&ctx->signal_event, &ctx->poller, g_signal_pipe[0]);
|
|
|
|
ctx->signal_event.dispatcher = (poller_fd_fn) on_signal_pipe_readable;
|
2015-04-18 03:25:10 +02:00
|
|
|
ctx->signal_event.user_data = ctx;
|
2015-04-12 04:52:39 +02:00
|
|
|
poller_fd_set (&ctx->signal_event, POLLIN);
|
|
|
|
|
|
|
|
poller_fd_init (&ctx->tty_event, &ctx->poller, STDIN_FILENO);
|
|
|
|
ctx->tty_event.dispatcher = (poller_fd_fn) on_tty_readable;
|
|
|
|
ctx->tty_event.user_data = &ctx;
|
|
|
|
poller_fd_set (&ctx->tty_event, POLLIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2015-04-21 22:08:18 +02:00
|
|
|
// 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;
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
static const struct opt opts[] =
|
|
|
|
{
|
|
|
|
{ 'd', "debug", NULL, 0, "run in debug mode" },
|
|
|
|
{ 'h', "help", NULL, 0, "display this help and exit" },
|
|
|
|
{ 'V', "version", NULL, 0, "output version information and exit" },
|
|
|
|
{ 'w', "write-default-cfg", "FILENAME",
|
|
|
|
OPT_OPTIONAL_ARG | OPT_LONG_ONLY,
|
|
|
|
"write a default configuration file and exit" },
|
|
|
|
{ 0, NULL, NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct opt_handler oh;
|
|
|
|
opt_handler_init (&oh, argc, argv, opts, NULL, "Experimental IRC client.");
|
|
|
|
|
|
|
|
int c;
|
|
|
|
while ((c = opt_handler_get (&oh)) != -1)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
g_debug_mode = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
opt_handler_usage (&oh, stdout);
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'V':
|
|
|
|
printf (PROGRAM_NAME " " PROGRAM_VERSION "\n");
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'w':
|
|
|
|
call_write_default_config (optarg, g_config_table);
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
default:
|
|
|
|
print_error ("wrong options");
|
|
|
|
opt_handler_usage (&oh, stderr);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
opt_handler_free (&oh);
|
|
|
|
|
|
|
|
print_status (PROGRAM_NAME " " PROGRAM_VERSION " starting");
|
|
|
|
|
|
|
|
// We only need to convert to and from the terminal encoding
|
|
|
|
setlocale (LC_CTYPE, "");
|
|
|
|
|
2015-04-19 23:07:23 +02:00
|
|
|
struct app_context ctx;
|
|
|
|
app_context_init (&ctx);
|
|
|
|
g_ctx = &ctx;
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
SSL_library_init ();
|
|
|
|
atexit (EVP_cleanup);
|
|
|
|
SSL_load_error_strings ();
|
|
|
|
atexit (ERR_free_strings);
|
|
|
|
|
|
|
|
using_history ();
|
2015-04-15 02:10:21 +02:00
|
|
|
// This can cause memory leaks, or maybe even a segfault. Funny, eh?
|
2015-04-12 04:52:39 +02:00
|
|
|
stifle_history (HISTORY_LIMIT);
|
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
setup_signal_handlers ();
|
|
|
|
|
2015-04-18 03:25:10 +02:00
|
|
|
struct error *e = NULL;
|
|
|
|
if (!load_config (&ctx, &e))
|
|
|
|
{
|
|
|
|
print_error ("%s", e->message);
|
|
|
|
error_free (e);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-04-12 04:52:39 +02:00
|
|
|
init_colors (&ctx);
|
|
|
|
init_poller_events (&ctx);
|
2015-04-13 00:06:08 +02:00
|
|
|
init_buffers (&ctx);
|
2015-04-26 18:59:58 +02:00
|
|
|
ctx.current_buffer = ctx.server_buffer;
|
2015-04-13 00:06:08 +02:00
|
|
|
refresh_prompt (&ctx);
|
2015-04-12 04:52:39 +02:00
|
|
|
|
2015-04-13 00:06:08 +02:00
|
|
|
// TODO: connect asynchronously (first step towards multiple servers)
|
2015-04-18 03:25:10 +02:00
|
|
|
if (!irc_connect (&ctx, &e))
|
2015-04-12 04:52:39 +02:00
|
|
|
{
|
2015-04-18 03:25:10 +02:00
|
|
|
buffer_send_error (&ctx, ctx.server_buffer, "%s", e->message);
|
2015-04-12 04:52:39 +02:00
|
|
|
error_free (e);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-04-15 02:10:21 +02:00
|
|
|
rl_startup_hook = init_readline;
|
2015-04-12 04:52:39 +02:00
|
|
|
rl_catch_sigwinch = false;
|
|
|
|
rl_callback_handler_install (ctx.readline_prompt, on_readline_input);
|
2015-04-13 00:06:08 +02:00
|
|
|
rl_get_screen_size (&ctx.lines, &ctx.columns);
|
|
|
|
ctx.readline_prompt_shown = true;
|
2015-04-12 04:52:39 +02:00
|
|
|
|
|
|
|
ctx.polling = true;
|
|
|
|
while (ctx.polling)
|
|
|
|
poller_run (&ctx.poller);
|
|
|
|
|
|
|
|
app_context_free (&ctx);
|
|
|
|
free_terminal ();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|