1
0
mirror of https://github.com/irssi/irssi.git synced 2024-11-03 04:27:19 -05:00

Cleaned up code.

git-svn-id: http://svn.irssi.org/repos/irssi/trunk@480 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
Timo Sirainen 2000-07-16 20:18:05 +00:00 committed by cras
parent 2a1052bbce
commit c2397475c5
24 changed files with 489 additions and 363 deletions

View File

@ -44,18 +44,17 @@
#define g_free_and_null(a) \
if (a) { g_free(a); (a) = NULL; }
typedef enum
{
typedef enum {
G_INPUT_READ = 1 << 0,
G_INPUT_WRITE = 1 << 1,
G_INPUT_EXCEPTION = 1 << 2
} GInputCondition;
typedef void (*GInputFunction) (gpointer data, int source,
typedef void (*GInputFunction) (void *data, int source,
GInputCondition condition);
int g_input_add(int source, GInputCondition condition,
GInputFunction function, gpointer data);
GInputFunction function, void *data);
#define MAX_INT_STRLEN ((sizeof(int) * CHAR_BIT + 2) / 3 + 1)

View File

@ -3,6 +3,7 @@ noinst_LTLIBRARIES = libcore.la
INCLUDES = \
$(GLIB_CFLAGS) \
-DSYSCONFDIR=\""$(sysconfdir)"\" \
-DMODULEDIR=\""$(libdir)/irssi/modules"\" \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/core
@ -24,6 +25,7 @@ libcore_la_SOURCES = \
modules.c \
net-disconnect.c \
net-nonblock.c \
net-sendbuffer.c \
network.c \
pidwait.c \
rawlog.c \
@ -47,6 +49,7 @@ noinst_HEADERS = \
net-disconnect.h \
net-internal.h \
net-nonblock.h \
net-sendbuffer.h \
network.h \
pidwait.h \
rawlog.h \

View File

@ -25,10 +25,13 @@ GArray *iopt_tables = NULL;
void args_register(struct poptOption *options)
{
if (iopt_tables == NULL)
iopt_tables = g_array_new(TRUE, TRUE, sizeof(struct poptOption));
if (iopt_tables == NULL) {
iopt_tables = g_array_new(TRUE, TRUE,
sizeof(struct poptOption));
}
while (options->longName != NULL || options->shortName != '\0' || options->arg != NULL) {
while (options->longName != NULL || options->shortName != '\0' ||
options->arg != NULL) {
g_array_append_val(iopt_tables, *options);
options = options+1;
}
@ -42,13 +45,16 @@ void args_execute(int argc, char *argv[])
if (iopt_tables == NULL)
return;
con = poptGetContext(PACKAGE, argc, argv, (struct poptOption *) (iopt_tables->data), 0);
con = poptGetContext(PACKAGE, argc, argv,
(struct poptOption *) (iopt_tables->data), 0);
poptReadDefaultConfig(con, TRUE);
while ((nextopt = poptGetNextOpt(con)) > 0) ;
if (nextopt != -1) {
printf(_("Error on option %s: %s.\nRun '%s --help' to see a full list of available command line options.\n"),
printf(_("Error on option %s: %s.\n"
"Run '%s --help' to see a full list of "
"available command line options.\n"),
poptBadOption(con, 0),
poptStrerror(nextopt),
argv[0]);

View File

@ -64,14 +64,16 @@ int command_have_sub(const char *command)
for (tmp = commands; tmp != NULL; tmp = tmp->next) {
COMMAND_REC *rec = tmp->data;
if (g_strncasecmp(rec->cmd, command, len) == 0 && rec->cmd[len] == ' ')
if (g_strncasecmp(rec->cmd, command, len) == 0 &&
rec->cmd[len] == ' ')
return TRUE;
}
return FALSE;
}
void command_bind_to(int pos, const char *cmd, const char *category, SIGNAL_FUNC func)
void command_bind_to(int pos, const char *cmd,
const char *category, SIGNAL_FUNC func)
{
COMMAND_REC *rec;
char *str;
@ -161,14 +163,16 @@ static const char *command_expand(char *cmd)
}
if (multiple) {
signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_AMBIGUOUS), cmd);
signal_emit("error command", 2,
GINT_TO_POINTER(CMDERR_AMBIGUOUS), cmd);
return NULL;
}
return match != NULL ? match : cmd;
}
void command_runsub(const char *cmd, const char *data, void *server, void *item)
void command_runsub(const char *cmd, const char *data,
void *server, void *item)
{
const char *newcmd;
char *orig, *subcmd, *defcmd, *args;
@ -177,7 +181,8 @@ void command_runsub(const char *cmd, const char *data, void *server, void *item)
if (*data == '\0') {
/* no subcommand given - unknown command? */
signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_UNKNOWN), cmd);
signal_emit("error command", 2,
GINT_TO_POINTER(CMDERR_UNKNOWN), cmd);
return;
}
@ -200,8 +205,10 @@ void command_runsub(const char *cmd, const char *data, void *server, void *item)
g_strdown(subcmd);
if (!signal_emit(subcmd, 3, args, server, item)) {
defcmd = g_strdup_printf("default command %s", cmd);
if (!signal_emit(defcmd, 3, data, server, item))
signal_emit("error command", 2, GINT_TO_POINTER(CMDERR_UNKNOWN), subcmd+8);
if (!signal_emit(defcmd, 3, data, server, item)) {
signal_emit("error command", 2,
GINT_TO_POINTER(CMDERR_UNKNOWN), subcmd+8);
}
g_free(defcmd);
}
@ -390,7 +397,8 @@ static int get_cmd_options(char **data, int ignore_unknown,
option = cmd_get_param(data);
/* check if this option can have argument */
pos = optlist == NULL ? -1 : option_find(optlist, option);
pos = optlist == NULL ? -1 :
option_find(optlist, option);
if (pos == -1 && !ignore_unknown) {
/* unknown option! */
*data = option;
@ -404,9 +412,11 @@ static int get_cmd_options(char **data, int ignore_unknown,
if (pos >= 0) {
/* if we used a shortcut of parameter, put
the whole parameter name in options table */
option = optlist[pos] + iscmdtype(*optlist[pos]);
option = optlist[pos] +
iscmdtype(*optlist[pos]);
}
if (options != NULL) g_hash_table_insert(options, option, "");
if (options != NULL)
g_hash_table_insert(options, option, "");
if (pos == -1 || !iscmdtype(*optlist[pos]))
option = NULL;
@ -464,7 +474,7 @@ int cmd_get_params(const char *data, gpointer *free_me, int count, ...)
GHashTable **opthash;
char **str, *arg, *datad, *old;
va_list args;
int cnt, error, len;
int cnt, error, len, ignore_unknown;
g_return_val_if_fail(data != NULL, FALSE);
@ -506,8 +516,13 @@ int cmd_get_params(const char *data, gpointer *free_me, int count, ...)
arg = (char *) va_arg(args, char *);
opthash = (GHashTable **) va_arg(args, GHashTable **);
rec->options = *opthash = g_hash_table_new((GHashFunc) g_istr_hash, (GCompareFunc) g_istr_equal);
error = get_cmd_options(&datad, count & PARAM_FLAG_UNKNOWN_OPTIONS, arg, *opthash);
rec->options = *opthash =
g_hash_table_new((GHashFunc) g_istr_hash,
(GCompareFunc) g_istr_equal);
ignore_unknown = count & PARAM_FLAG_UNKNOWN_OPTIONS;
error = get_cmd_options(&datad, ignore_unknown,
arg, *opthash);
if (error) break;
count &= ~PARAM_FLAG_OPTIONS;
@ -557,7 +572,8 @@ void cmd_get_remove_func(CMD_GET_FUNC func)
cmdget_funcs = g_slist_prepend(cmdget_funcs, (void *) func);
}
static void parse_command(const char *command, int expand_aliases, SERVER_REC *server, void *item)
static void parse_command(const char *command, int expand_aliases,
SERVER_REC *server, void *item)
{
const char *alias, *newcmd;
char *cmd, *orig, *args, *oldcmd;
@ -589,8 +605,10 @@ static void parse_command(const char *command, int expand_aliases, SERVER_REC *s
g_strdown(cmd);
oldcmd = current_command;
current_command = cmd+8;
if (!signal_emit(cmd, 3, args, server, item))
signal_emit_id(signal_default_command, 3, command, server, item);
if (!signal_emit(cmd, 3, args, server, item)) {
signal_emit_id(signal_default_command, 3,
command, server, item);
}
current_command = oldcmd;
g_free(cmd);

View File

@ -33,15 +33,24 @@ enum {
/* Returning from command function with error */
#define cmd_return_error(a) \
{ signal_emit("error command", 1, GINT_TO_POINTER(a)); signal_stop(); return; }
G_STMT_START { \
signal_emit("error command", 1, GINT_TO_POINTER(a)); \
signal_stop(); \
return; \
} G_STMT_END
#define cmd_param_error(a) \
{ cmd_params_free(free_arg); cmd_return_error(a); }
G_STMT_START { \
cmd_params_free(free_arg); \
cmd_return_error(a); \
} G_STMT_END
extern GSList *commands;
extern char *current_command; /* the command we're right now. */
/* Bind command to specified function. */
void command_bind_to(int pos, const char *cmd, const char *category, SIGNAL_FUNC func);
void command_bind_to(int pos, const char *cmd,
const char *category, SIGNAL_FUNC func);
#define command_bind(a, b, c) command_bind_to(1, a, b, c)
#define command_bind_first(a, b, c) command_bind_to(0, a, b, c)
#define command_bind_last(a, b, c) command_bind_to(2, a, b, c)
@ -49,7 +58,8 @@ void command_unbind(const char *cmd, SIGNAL_FUNC func);
/* Run subcommand, `cmd' contains the base command, first word in `data'
contains the subcommand */
void command_runsub(const char *cmd, const char *data, void *server, void *item);
void command_runsub(const char *cmd, const char *data,
void *server, void *item);
COMMAND_REC *command_find(const char *cmd);
int command_have_sub(const char *command);

View File

@ -21,8 +21,7 @@
#include "module.h"
#include "levels.h"
static const char *levels[] =
{
static const char *levels[] = {
"CRAP",
"MSGS",
"PUBLICS",
@ -52,7 +51,7 @@ static const char *levels[] =
int level_get(const char *level)
{
int n, len;
int n, len, match;
if (strcmp(level, "ALL") == 0)
return MSGLEVEL_ALL;
@ -60,24 +59,28 @@ int level_get(const char *level)
if (strcmp(level, "NEVER") == 0)
return MSGLEVEL_NEVER;
/* I never remember if it was PUBLIC or PUBLICS, MSG or MSGS, etc.
So, make it work with both. */
len = strlen(level);
if (toupper(level[len-1]) == 'S') len--;
if (len == 0) return 0;
/* partial match allowed, as long as it's the only one that matches */
match = 0;
for (n = 0; levels[n] != NULL; n++) {
if (strncmp(levels[n], level, len) == 0 &&
(levels[n][len] == '\0' || strcmp(levels[n]+len, "S") == 0))
return 1 << n;
if (strncmp(levels[n], level, len) == 0) {
if (match > 0) {
/* ambiguous - abort */
return 0;
}
match = 1L << n;
}
}
return 0;
return match;
}
int level2bits(const char *level)
{
char *orig, *str, *ptr;
int ret, slevel, neg;
int ret, singlelevel, negative;
g_return_val_if_fail(level != NULL, 0);
@ -94,13 +97,13 @@ int level2bits(const char *level)
else if (*str != '\0')
continue;
neg = *ptr == '-' ? 1 : 0;
negative = *ptr == '-';
if (*ptr == '-' || *ptr == '+') ptr++;
slevel = level_get(ptr);
if (slevel != 0) {
ret = !neg ? (ret | slevel) :
(ret & ~slevel);
singlelevel = level_get(ptr);
if (singlelevel != 0) {
ret = !negative ? (ret | singlelevel) :
(ret & ~singlelevel);
}
while (*str == ' ') str++;
@ -130,10 +133,11 @@ char *bits2level(int bits)
g_string_append(str, "NEVER ");
for (n = 0; levels[n] != NULL; n++) {
if (bits & (1 << n))
if (bits & (1L << n))
g_string_sprintfa(str, "%s ", levels[n]);
}
g_string_truncate(str, str->len-1);
if (str->len > 0)
g_string_truncate(str, str->len-1);
ret = str->str;
g_string_free(str, FALSE);
@ -143,15 +147,17 @@ char *bits2level(int bits)
int combine_level(int dest, const char *src)
{
char **list, **item;
char **list, **item, *itemname;
int itemlevel;
g_return_val_if_fail(src != NULL, dest);
list = g_strsplit(src, " ", -1);
for (item = list; *item != NULL; item++) {
g_strup(*item);
itemlevel = level_get(*item + (**item == '+' || **item == '-' ? 1 : 0));
itemname = *item + (**item == '+' || **item == '-' ? 1 : 0);
g_strup(itemname);
itemlevel = level_get(itemname);
if (**item == '-')
dest &= ~(itemlevel);
else

View File

@ -7,35 +7,35 @@
needed..). */
/* Message levels */
#define MSGLEVEL_CRAP 0x0000001
#define MSGLEVEL_MSGS 0x0000002
#define MSGLEVEL_PUBLIC 0x0000004
#define MSGLEVEL_NOTICES 0x0000008
#define MSGLEVEL_SNOTES 0x0000010
#define MSGLEVEL_CTCPS 0x0000020
#define MSGLEVEL_ACTIONS 0x0000040
#define MSGLEVEL_JOINS 0x0000080
#define MSGLEVEL_PARTS 0x0000100
#define MSGLEVEL_QUITS 0x0000200
#define MSGLEVEL_KICKS 0x0000400
#define MSGLEVEL_MODES 0x0000800
#define MSGLEVEL_TOPICS 0x0001000
#define MSGLEVEL_WALLOPS 0x0002000
#define MSGLEVEL_INVITES 0x0004000
#define MSGLEVEL_NICKS 0x0008000
#define MSGLEVEL_DCC 0x0010000
#define MSGLEVEL_DCCMSGS 0x0020000
#define MSGLEVEL_CLIENTNOTICE 0x0040000
#define MSGLEVEL_CLIENTCRAP 0x0080000
#define MSGLEVEL_CLIENTERROR 0x0100000
#define MSGLEVEL_HILIGHT 0x0200000
#define MSGLEVEL_CRAP 0x0000001
#define MSGLEVEL_MSGS 0x0000002
#define MSGLEVEL_PUBLIC 0x0000004
#define MSGLEVEL_NOTICES 0x0000008
#define MSGLEVEL_SNOTES 0x0000010
#define MSGLEVEL_CTCPS 0x0000020
#define MSGLEVEL_ACTIONS 0x0000040
#define MSGLEVEL_JOINS 0x0000080
#define MSGLEVEL_PARTS 0x0000100
#define MSGLEVEL_QUITS 0x0000200
#define MSGLEVEL_KICKS 0x0000400
#define MSGLEVEL_MODES 0x0000800
#define MSGLEVEL_TOPICS 0x0001000
#define MSGLEVEL_WALLOPS 0x0002000
#define MSGLEVEL_INVITES 0x0004000
#define MSGLEVEL_NICKS 0x0008000
#define MSGLEVEL_DCC 0x0010000
#define MSGLEVEL_DCCMSGS 0x0020000
#define MSGLEVEL_CLIENTNOTICE 0x0040000
#define MSGLEVEL_CLIENTCRAP 0x0080000
#define MSGLEVEL_CLIENTERROR 0x0100000
#define MSGLEVEL_HILIGHT 0x0200000
#define MSGLEVEL_ALL 0x03fffff
#define MSGLEVEL_ALL 0x03fffff
#define MSGLEVEL_NOHILIGHT 0x1000000 /* Don't try to hilight words in this message */
#define MSGLEVEL_NO_ACT 0x2000000 /* Don't trigger channel activity */
#define MSGLEVEL_NEVER 0x4000000 /* never ignore / never log */
#define MSGLEVEL_LASTLOG 0x8000000 /* never ignore / never log */
#define MSGLEVEL_NOHILIGHT 0x1000000 /* Don't highlight this message */
#define MSGLEVEL_NO_ACT 0x2000000 /* Don't trigger channel activity */
#define MSGLEVEL_NEVER 0x4000000 /* never ignore / never log */
#define MSGLEVEL_LASTLOG 0x8000000 /* never ignore / never log */
int level2bits(const char *level);
char *bits2level(int bits);

View File

@ -123,7 +123,8 @@ int line_split(const char *data, int len, char **output, LINEBUF_REC **buffer)
len = 0;
if (linebuf_find(rec, '\n') == NULL) {
/* connection closed and last line is missing \n ..
just add it so we can see if it had anything useful.. */
just add it so we can see if it had
anything useful.. */
linebuf_append(rec, "\n", 1);
}
}

View File

@ -34,13 +34,22 @@
static struct flock lock;
#endif
const char *rotate_strings[] = {
"never",
"hourly",
"daily",
"weekly",
"monthly"
};
GSList *logs;
const char *log_timestamp;
static int log_file_create_mode;
static int rotate_tag;
static void log_write_timestamp(int handle, const char *format, const char *suffix, time_t t)
static void log_write_timestamp(int handle, const char *format,
const char *suffix, time_t stamp)
{
struct tm *tm;
char str[256];
@ -48,7 +57,7 @@ static void log_write_timestamp(int handle, const char *format, const char *suff
g_return_if_fail(format != NULL);
if (*format == '\0') return;
tm = localtime(&t);
tm = localtime(&stamp);
str[sizeof(str)-1] = '\0';
strftime(str, sizeof(str)-1, format, tm);
@ -61,20 +70,21 @@ int log_start_logging(LOG_REC *log)
{
char *str, fname[1024];
struct tm *tm;
time_t t;
time_t now;
g_return_val_if_fail(log != NULL, FALSE);
if (log->handle != -1)
return TRUE;
t = time(NULL);
tm = localtime(&t);
now = time(NULL);
tm = localtime(&now);
/* Append/create log file */
str = convert_home(log->fname);
strftime(fname, sizeof(fname), str, tm);
log->handle = open(fname, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode);
log->handle = open(fname, O_WRONLY | O_APPEND | O_CREAT,
log_file_create_mode);
g_free(str);
if (log->handle == -1) {
@ -94,7 +104,9 @@ int log_start_logging(LOG_REC *log)
lseek(log->handle, 0, SEEK_END);
log->opened = log->last = time(NULL);
log_write_timestamp(log->handle, settings_get_str("log_open_string"), "\n", log->last);
log_write_timestamp(log->handle,
settings_get_str("log_open_string"),
"\n", log->last);
signal_emit("log started", 1, log);
return TRUE;
@ -109,7 +121,9 @@ void log_stop_logging(LOG_REC *log)
signal_emit("log stopped", 1, log);
log_write_timestamp(log->handle, settings_get_str("log_close_string"), "\n", time(NULL));
log_write_timestamp(log->handle,
settings_get_str("log_close_string"),
"\n", time(NULL));
#ifdef HAVE_FCNTL
memset(&lock, 0, sizeof(lock));
@ -124,7 +138,7 @@ void log_stop_logging(LOG_REC *log)
void log_write_rec(LOG_REC *log, const char *str)
{
struct tm *tm;
time_t t;
time_t now;
int day;
g_return_if_fail(log != NULL);
@ -133,24 +147,27 @@ void log_write_rec(LOG_REC *log, const char *str)
if (log->handle == -1)
return;
t = time(NULL);
tm = localtime(&t);
now = time(NULL);
tm = localtime(&now);
day = tm->tm_mday;
tm = localtime(&log->last);
if (tm->tm_mday != day) {
/* day changed */
log_write_timestamp(log->handle, settings_get_str("log_day_changed"), "\n", t);
log_write_timestamp(log->handle,
settings_get_str("log_day_changed"),
"\n", now);
}
log->last = t;
log->last = now;
log_write_timestamp(log->handle, log_timestamp, str, t);
log_write_timestamp(log->handle, log_timestamp, str, now);
write(log->handle, "\n", 1);
signal_emit("log written", 2, log, str);
}
void log_file_write(const char *item, int level, const char *str, int no_fallbacks)
static void log_file_write(const char *item, int level,
const char *str, int no_fallbacks)
{
GSList *tmp, *fallbacks;
char *tmpstr;
@ -181,7 +198,8 @@ void log_file_write(const char *item, int level, const char *str, int no_fallbac
if (level & MSGLEVEL_PUBLIC)
tmpstr = g_strconcat(item, ": ", str, NULL);
g_slist_foreach(fallbacks, (GFunc) log_write_rec, tmpstr ? tmpstr : (char *)str);
g_slist_foreach(fallbacks, (GFunc) log_write_rec,
tmpstr ? tmpstr : (char *)str);
g_free_not_null(tmpstr);
}
g_slist_free(fallbacks);
@ -208,18 +226,9 @@ LOG_REC *log_find(const char *fname)
const char *log_rotate2str(int rotate)
{
switch (rotate) {
case LOG_ROTATE_HOURLY:
return "hourly";
case LOG_ROTATE_DAILY:
return "daily";
case LOG_ROTATE_WEEKLY:
return "weekly";
case LOG_ROTATE_MONTHLY:
return "monthly";
}
g_return_val_if_fail(rotate >= 0 && rotate <= LOG_ROTATE_MONTHLY, NULL);
return NULL;
return rotate_strings[rotate];
}
int log_str2rotate(const char *str)
@ -331,7 +340,9 @@ void log_close(LOG_REC *log)
log_destroy(log);
}
static void sig_printtext_stripped(void *window, void *server, const char *item, gpointer levelp, const char *str)
static void sig_printtext_stripped(void *window, void *server,
const char *item, gpointer levelp,
const char *str)
{
int level;
@ -416,7 +427,8 @@ static void log_read_config(void)
log->fname = g_strdup(node->key);
log->autoopen = config_node_get_bool(node, "auto_open", FALSE);
log->level = level2bits(config_node_get_str(node, "level", 0));
log->rotate = log_str2rotate(config_node_get_str(node, "rotate", NULL));
log->rotate = log_str2rotate(config_node_get_str(
node, "rotate", NULL));
if (log->rotate < 0) log->rotate = LOG_ROTATE_NEVER;
node = config_node_section(node, "items", -1);
@ -441,11 +453,15 @@ void log_init(void)
rotate_tag = g_timeout_add(60000, (GSourceFunc) sig_rotate_check, NULL);
logs = NULL;
settings_add_int("log", "log_create_mode", DEFAULT_LOG_FILE_CREATE_MODE);
settings_add_int("log", "log_create_mode",
DEFAULT_LOG_FILE_CREATE_MODE);
settings_add_str("log", "log_timestamp", "%H:%M ");
settings_add_str("log", "log_open_string", "--- Log opened %a %b %d %H:%M:%S %Y");
settings_add_str("log", "log_close_string", "--- Log closed %a %b %d %H:%M:%S %Y");
settings_add_str("log", "log_day_changed", "--- Day changed %a %b %d %Y");
settings_add_str("log", "log_open_string",
"--- Log opened %a %b %d %H:%M:%S %Y");
settings_add_str("log", "log_close_string",
"--- Log closed %a %b %d %H:%M:%S %Y");
settings_add_str("log", "log_day_changed",
"--- Day changed %a %b %d %Y");
read_settings();
log_read_config();

View File

@ -28,10 +28,11 @@
typedef struct {
GInputCondition condition;
GInputFunction function;
gpointer data;
void *data;
} IRSSI_INPUT_REC;
static gboolean irssi_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
static int irssi_io_invoke(GIOChannel *source, GIOCondition condition,
void *data)
{
IRSSI_INPUT_REC *rec = data;
GInputCondition icond = 0;
@ -43,14 +44,16 @@ static gboolean irssi_io_invoke(GIOChannel *source, GIOCondition condition, gpoi
if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
icond |= G_INPUT_EXCEPTION;
if (rec->condition & icond)
rec->function(rec->data, g_io_channel_unix_get_fd(source), icond);
if (rec->condition & icond) {
rec->function(rec->data, g_io_channel_unix_get_fd(source),
icond);
}
return TRUE;
}
int g_input_add(int source, GInputCondition condition,
GInputFunction function, gpointer data)
GInputFunction function, void *data)
{
IRSSI_INPUT_REC *rec;
unsigned int result;
@ -109,7 +112,8 @@ int find_substr(const char *list, const char *item)
ptr = strchr(list, ' ');
if (ptr == NULL) ptr = list+strlen(list);
if (g_strncasecmp(list, item, ptr-list) == 0 && item[ptr-list] == '\0')
if (g_strncasecmp(list, item, ptr-list) == 0 &&
item[ptr-list] == '\0')
return TRUE;
list = ptr;
@ -149,42 +153,10 @@ int strarray_find(char **array, const char *item)
return -1;
}
int copyfile(const char *src, const char *dest)
{
FILE *fs, *fd;
int ret;
g_return_val_if_fail(src != NULL, FALSE);
g_return_val_if_fail(dest != NULL, FALSE);
fs = fopen(src, "rb");
if (fs == NULL) return FALSE;
ret = FALSE;
remove(dest); /* just to be sure there's no link already in dest */
fd = fopen(dest, "w+");
if (fd != NULL) {
int len;
char buf[1024];
while ((len = fread(buf, 1, sizeof(buf), fs)) > 0) {
if (fwrite(buf, 1, len, fd) != len)
break;
}
fclose(fd);
ret = TRUE;
}
fclose(fs);
return ret;
}
int execute(const char *cmd)
{
char **args;
char *prev, *dupcmd;
int pid, max, cnt;
int pid;
g_return_val_if_fail(cmd != NULL, -1);
@ -195,25 +167,9 @@ int execute(const char *cmd)
return pid;
}
dupcmd = g_strdup(cmd);
max = 5; cnt = 0;
args = g_malloc(sizeof(char *)*max);
for (prev = dupcmd; ; dupcmd++) {
if (*dupcmd == '\0' || *dupcmd == ' ') {
args[cnt++] = prev;
if (cnt == max) {
max += 5;
args = g_realloc(args, sizeof(char *)*max);
}
if (*dupcmd == '\0') break;
*dupcmd++ = '\0';
prev = dupcmd;
}
}
args[cnt] = NULL;
args = g_strsplit(cmd, " ", -1);
execvp(args[0], args);
g_free(dupcmd);
g_strfreev(args);
_exit(99);
return -1;

View File

@ -16,7 +16,6 @@ int strarray_length(char **array);
/* return index of `item' in `array' or -1 if not found */
int strarray_find(char **array, const char *item);
int copyfile(const char *src, const char *dest);
int execute(const char *cmd); /* returns pid or -1 = error */
GSList *gslist_find_string(GSList *list, const char *key);

View File

@ -22,8 +22,6 @@
#include "modules.h"
#include "signals.h"
#define PLUGINSDIR "/usr/local/lib/irssi/plugins" /*FIXME: configurable*/
GSList *modules;
static GHashTable *uniqids, *uniqstrids;
@ -34,7 +32,7 @@ static int next_uniq_id;
int module_get_uniq_id(const char *module, int id)
{
GHashTable *ids;
gpointer origkey, uniqid;
gpointer origkey, uniqid, idp;
int ret;
g_return_val_if_fail(module != NULL, -1);
@ -42,15 +40,17 @@ int module_get_uniq_id(const char *module, int id)
ids = g_hash_table_lookup(idlookup, module);
if (ids == NULL) {
/* new module */
ids = g_hash_table_new((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal);
ids = g_hash_table_new((GHashFunc) g_direct_hash,
(GCompareFunc) g_direct_equal);
g_hash_table_insert(idlookup, g_strdup(module), ids);
}
if (!g_hash_table_lookup_extended(ids, GINT_TO_POINTER(id), &origkey, &uniqid)) {
idp = GINT_TO_POINTER(id);
if (!g_hash_table_lookup_extended(ids, idp, &origkey, &uniqid)) {
/* not found */
ret = next_uniq_id++;
g_hash_table_insert(ids, GINT_TO_POINTER(id), GINT_TO_POINTER(ret));
g_hash_table_insert(uniqids, GINT_TO_POINTER(ret), GINT_TO_POINTER(id));
g_hash_table_insert(ids, idp, GINT_TO_POINTER(ret));
g_hash_table_insert(uniqids, GINT_TO_POINTER(ret), idp);
} else {
ret = GPOINTER_TO_INT(uniqid);
}
@ -70,7 +70,8 @@ int module_get_uniq_id_str(const char *module, const char *id)
ids = g_hash_table_lookup(stridlookup, module);
if (ids == NULL) {
/* new module */
ids = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal);
ids = g_hash_table_new((GHashFunc) g_str_hash,
(GCompareFunc) g_str_equal);
g_hash_table_insert(stridlookup, g_strdup(module), ids);
}
@ -92,22 +93,25 @@ int module_get_uniq_id_str(const char *module, const char *id)
/* returns the original module specific id, -1 = not found */
int module_find_id(const char *module, int uniqid)
{
GHashTable *ids;
GHashTable *idlist;
gpointer origkey, id;
int ret;
g_return_val_if_fail(module != NULL, -1);
ret = g_hash_table_lookup_extended(uniqids, GINT_TO_POINTER(uniqid), &origkey, &id) ?
GPOINTER_TO_INT(id) : -1;
if (!g_hash_table_lookup_extended(uniqids, GINT_TO_POINTER(uniqid),
&origkey, &id))
return -1;
if (ret != -1) {
/* check that module matches */
ids = g_hash_table_lookup(idlookup, module);
if (ids == NULL || !g_hash_table_lookup_extended(ids, GINT_TO_POINTER(ret), &origkey, &id) ||
GPOINTER_TO_INT(id) != uniqid)
ret = -1;
}
/* check that module matches */
idlist = g_hash_table_lookup(idlookup, module);
if (idlist == NULL)
return -1;
ret = GPOINTER_TO_INT(id);
if (!g_hash_table_lookup_extended(idlist, id, &origkey, &id) ||
GPOINTER_TO_INT(id) != uniqid)
ret = -1;
return ret;
}
@ -115,32 +119,35 @@ int module_find_id(const char *module, int uniqid)
/* returns the original module specific id, NULL = not found */
const char *module_find_id_str(const char *module, int uniqid)
{
GHashTable *ids;
GHashTable *idlist;
gpointer origkey, id;
const char *ret;
g_return_val_if_fail(module != NULL, NULL);
ret = g_hash_table_lookup_extended(uniqstrids, GINT_TO_POINTER(uniqid),
&origkey, &id) ? id : NULL;
if (!g_hash_table_lookup_extended(uniqstrids, GINT_TO_POINTER(uniqid),
&origkey, &id))
return NULL;
if (ret != NULL) {
/* check that module matches */
ids = g_hash_table_lookup(stridlookup, module);
if (ids == NULL || !g_hash_table_lookup_extended(ids, GINT_TO_POINTER(ret), &origkey, &id) ||
(GPOINTER_TO_INT(id) != uniqid))
ret = NULL;
}
/* check that module matches */
idlist = g_hash_table_lookup(stridlookup, module);
if (idlist == NULL)
return NULL;
ret = id;
if (!g_hash_table_lookup_extended(idlist, id, &origkey, &id) ||
GPOINTER_TO_INT(id) != uniqid)
ret = NULL;
return ret;
}
static void gh_uniq_destroy(gpointer key, gpointer value)
static void uniq_destroy(gpointer key, gpointer value)
{
g_hash_table_remove(uniqids, value);
}
static void gh_uniq_destroy_str(gpointer key, gpointer value)
static void uniq_destroy_str(gpointer key, gpointer value)
{
g_hash_table_remove(uniqstrids, value);
g_free(key);
@ -150,23 +157,25 @@ static void gh_uniq_destroy_str(gpointer key, gpointer value)
when module is destroyed with module's name as the parameter. */
void module_uniq_destroy(const char *module)
{
GHashTable *ids;
GHashTable *idlist;
gpointer key;
if (g_hash_table_lookup_extended(idlookup, module, &key, (gpointer *) &ids)) {
if (g_hash_table_lookup_extended(idlookup, module, &key,
(gpointer *) &idlist)) {
g_hash_table_remove(idlookup, key);
g_free(key);
g_hash_table_foreach(ids, (GHFunc) gh_uniq_destroy, NULL);
g_hash_table_destroy(ids);
g_hash_table_foreach(idlist, (GHFunc) uniq_destroy, NULL);
g_hash_table_destroy(idlist);
}
if (g_hash_table_lookup_extended(stridlookup, module, &key, (gpointer *) &ids)) {
if (g_hash_table_lookup_extended(stridlookup, module, &key,
(gpointer *) &idlist)) {
g_hash_table_remove(stridlookup, key);
g_free(key);
g_hash_table_foreach(ids, (GHFunc) gh_uniq_destroy_str, NULL);
g_hash_table_destroy(ids);
g_hash_table_foreach(idlist, (GHFunc) uniq_destroy_str, NULL);
g_hash_table_destroy(idlist);
}
}
@ -216,13 +225,14 @@ GModule *module_open(const char *name)
if (g_path_is_absolute(name))
path = g_strdup(name);
else {
path = g_module_build_path(PLUGINSDIR, name);
path = g_module_build_path(MODULEDIR, name);
module = g_module_open(path, 0);
g_free(path);
if (module != NULL) return module;
/* Plugin not found from global plugin dir, check from home dir */
str = g_strdup_printf("%s/.irssi/plugins", g_get_home_dir());
/* module not found from global module dir,
check from home dir */
str = g_strdup_printf("%s/.irssi/modules", g_get_home_dir());
path = g_module_build_path(str, name);
g_free(str);
}
@ -232,44 +242,39 @@ GModule *module_open(const char *name)
return module;
}
int module_load(const char *path)
#define module_error(error, module, text) \
signal_emit("module error", 3, GINT_TO_POINTER(error), module, text)
static int module_load_name(const char *path, const char *name)
{
void (*module_init) (void);
GModule *gmodule;
MODULE_REC *rec;
char *name, *initfunc;
char *initfunc;
g_return_val_if_fail(path != NULL, FALSE);
if (!g_module_supported())
return FALSE;
name = module_get_name(path);
if (module_find(name)) {
signal_emit("module error", 2, GINT_TO_POINTER(MODULE_ERROR_ALREADY_LOADED), name);
g_free(name);
module_error(MODULE_ERROR_ALREADY_LOADED, name, NULL);
return FALSE;
}
gmodule = module_open(path);
if (gmodule == NULL) {
signal_emit("module error", 3, GINT_TO_POINTER(MODULE_ERROR_LOAD), name, g_module_error());
g_free(name);
module_error(MODULE_ERROR_LOAD, name, g_module_error());
return FALSE;
}
/* get the module's init() function */
initfunc = g_strconcat(name, "_init", NULL);
if (!g_module_symbol(gmodule, initfunc, (gpointer *) &module_init)) {
signal_emit("module error", 2, GINT_TO_POINTER(MODULE_ERROR_INVALID), name);
module_error(MODULE_ERROR_INVALID, name, NULL);
g_module_close(gmodule);
g_free(initfunc);
g_free(name);
return FALSE;
}
g_free(initfunc);
rec = g_new0(MODULE_REC, 1);
rec->name = name;
rec->name = g_strdup(name);
rec->gmodule = gmodule;
modules = g_slist_append(modules, rec);
@ -279,6 +284,23 @@ int module_load(const char *path)
return TRUE;
}
int module_load(const char *path)
{
char *name;
int ret;
g_return_val_if_fail(path != NULL, FALSE);
if (!g_module_supported())
return FALSE;
name = module_get_name(path);
ret = module_load_name(path, name);
g_free(name);
return ret;
}
void module_unload(MODULE_REC *module)
{
void (*module_deinit) (void);
@ -290,8 +312,10 @@ void module_unload(MODULE_REC *module)
signal_emit("module unloaded", 1, module);
/* call the module's deinit() function */
deinitfunc = g_strconcat(module->name, "_deinit", NULL);
if (g_module_symbol(module->gmodule, deinitfunc, (gpointer *) &module_deinit))
if (g_module_symbol(module->gmodule, deinitfunc,
(gpointer *) &module_deinit))
module_deinit();
g_free(deinitfunc);
@ -304,11 +328,15 @@ void modules_init(void)
{
modules = NULL;
idlookup = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal);
uniqids = g_hash_table_new((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal);
idlookup = g_hash_table_new((GHashFunc) g_str_hash,
(GCompareFunc) g_str_equal);
uniqids = g_hash_table_new((GHashFunc) g_direct_hash,
(GCompareFunc) g_direct_equal);
stridlookup = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal);
uniqstrids = g_hash_table_new((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal);
stridlookup = g_hash_table_new((GHashFunc) g_str_hash,
(GCompareFunc) g_str_equal);
uniqstrids = g_hash_table_new((GHashFunc) g_direct_hash,
(GCompareFunc) g_direct_equal);
next_uniq_id = 0;
}

View File

@ -51,11 +51,7 @@ static void sig_disconnect(NET_DISCONNECT_REC *rec)
int ret;
/* check if there's any data waiting in socket */
for (;;) {
ret = net_receive(rec->handle, buf, sizeof(buf));
if (ret <= 0)
break;
}
while ((ret = net_receive(rec->handle, buf, sizeof(buf))) > 0) ;
if (ret == -1) {
/* socket was closed */
@ -68,7 +64,6 @@ static int sig_timeout_disconnect(void)
NET_DISCONNECT_REC *rec;
GSList *tmp, *next;
time_t now;
int ret;
/* check if we've waited enough for sockets to close themselves */
now = time(NULL);
@ -77,7 +72,7 @@ static int sig_timeout_disconnect(void)
next = tmp->next;
if (rec->created+MAX_CLOSE_WAIT <= now)
sig_disconnect(rec);
net_disconnect_remove(rec);
}
if (disconnects == NULL) {
@ -85,9 +80,7 @@ static int sig_timeout_disconnect(void)
function */
timeout_tag = -1;
}
ret = disconnects != NULL ? 1 : 0;
return ret;
return disconnects != NULL;
}
/* Try to let the other side close the connection, if it still isn't
@ -97,14 +90,17 @@ void net_disconnect_later(int handle)
NET_DISCONNECT_REC *rec;
rec = g_new(NET_DISCONNECT_REC, 1);
disconnects = g_slist_append(disconnects, rec);
rec->created = time(NULL);
rec->handle = handle;
rec->tag = g_input_add(handle, G_INPUT_READ, (GInputFunction) sig_disconnect, rec);
rec->tag = g_input_add(handle, G_INPUT_READ,
(GInputFunction) sig_disconnect, rec);
if (timeout_tag == -1)
timeout_tag = g_timeout_add(10000, (GSourceFunc) sig_timeout_disconnect, NULL);
if (timeout_tag == -1) {
timeout_tag = g_timeout_add(10000, (GSourceFunc)
sig_timeout_disconnect, NULL);
}
disconnects = g_slist_append(disconnects, rec);
}
void net_disconnect_init(void)
@ -121,9 +117,6 @@ void net_disconnect_deinit(void)
struct timeval tv;
fd_set set;
if (disconnects == NULL)
return;
/* give the sockets a chance to disconnect themselves.. */
max = time(NULL)+MAX_QUIT_CLOSE_WAIT;
first = 1;
@ -141,12 +134,15 @@ void net_disconnect_deinit(void)
FD_SET(rec->handle, &set);
tv.tv_sec = first ? 0 : max-now;
tv.tv_usec = first ? 100000 : 0;
if (select(rec->handle+1, &set, NULL, NULL, &tv) > 0 && FD_ISSET(rec->handle, &set)) {
if (select(rec->handle+1, &set, NULL, NULL, &tv) > 0 &&
FD_ISSET(rec->handle, &set)) {
/* data coming .. check if we can close the handle */
sig_disconnect(rec);
} else if (first) {
/* Display the text when we have already waited for a while */
printf(_("Please wait, waiting for servers to close connections..\n"));
/* Display the text when we have already waited
for a while */
printf(_("Please wait, waiting for servers to close "
"connections..\n"));
fflush(stdout);
first = 0;

View File

@ -25,8 +25,7 @@
#include "pidwait.h"
#include "net-nonblock.h"
typedef struct
{
typedef struct {
NET_CALLBACK func;
void *data;
@ -34,8 +33,7 @@ typedef struct
int port;
IPADDR *my_ip;
int tag;
}
SIMPLE_THREAD_REC;
} SIMPLE_THREAD_REC;
/* nonblocking gethostbyname(), ip (IPADDR) + error (int, 0 = not error) is
written to pipe when found PID of the resolver child is returned */
@ -56,7 +54,8 @@ int net_gethostbyname_nonblock(const char *addr, int pipe)
if (pid != 0) {
/* failed! */
g_warning("net_connect_thread(): fork() failed! Using blocking resolving");
g_warning("net_connect_thread(): fork() failed! "
"Using blocking resolving");
}
/* child */
@ -186,7 +185,8 @@ static void simple_readpipe(SIMPLE_THREAD_REC *rec, int pipe)
}
/* Connect to server, call func when finished */
int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip, NET_CALLBACK func, void *data)
int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip,
NET_CALLBACK func, void *data)
{
SIMPLE_THREAD_REC *rec;
int fd[2];
@ -212,7 +212,8 @@ int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip, NET_
rec->data = data;
rec->pipes[0] = fd[0];
rec->pipes[1] = fd[1];
rec->tag = g_input_add(fd[0], G_INPUT_READ, (GInputFunction) simple_readpipe, rec);
rec->tag = g_input_add(fd[0], G_INPUT_READ,
(GInputFunction) simple_readpipe, rec);
return 1;
}

View File

@ -134,8 +134,10 @@ int net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
/* set socket options */
fcntl(handle, F_SETFL, O_NONBLOCK);
setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt));
setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
(char *) &opt, sizeof(opt));
setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
(char *) &opt, sizeof(opt));
/* set our own address, ignore if bind() fails */
if (my_ip != NULL) {
@ -185,8 +187,10 @@ int net_listen(IPADDR *my_ip, int *port)
/* set socket options */
fcntl(handle, F_SETFL, O_NONBLOCK);
setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt));
setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, sizeof(opt));
setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
(char *) &opt, sizeof(opt));
setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
(char *) &opt, sizeof(opt));
/* specify the address/port we want to listen in */
ret = bind(handle, &so.sa, sizeof(so));
@ -260,7 +264,8 @@ int net_receive(int handle, char *buf, int len)
if (ret == 0)
return -1; /* disconnected */
if (ret == -1 && (errno == EWOULDBLOCK || errno == EAGAIN))
if (ret == -1 && (errno == EWOULDBLOCK || errno == EAGAIN ||
errno == EINTR))
return 0; /* no bytes received */
return ret;
@ -275,7 +280,8 @@ int net_transmit(int handle, const char *data, int len)
g_return_val_if_fail(data != NULL, -1);
n = send(handle, data, len, 0);
if (n == -1 && (errno == EWOULDBLOCK || errno == EAGAIN))
if (n == -1 && (errno == EWOULDBLOCK || errno == EAGAIN ||
errno == EINTR))
return 0;
return n > 0 ? n : -1;
@ -332,7 +338,8 @@ int net_gethostbyname(const char *addr, IPADDR *ip)
if (host_error != 0)
return host_error;
if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
return 1;
so = (union sockaddr_union *) ai->ai_addr;
@ -377,7 +384,8 @@ int net_gethostbyaddr(IPADDR *ip, char **name)
if (host_error != 0)
return host_error;
if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0, 0))
if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
sizeof(hbuf), NULL, 0, 0))
return 1;
/*FIXME: how does this work? *name = g_strdup(ai->?);*/
@ -461,7 +469,7 @@ const char *net_gethosterror(int error)
if (error == 1) {
/* getnameinfo() failed ..
FIXME: does strerror return the right error message?? */
FIXME: does strerror return the right error message? */
return g_strerror(errno);
}

View File

@ -48,11 +48,13 @@ static int child_check(void)
/* wait for each pid.. */
for (tmp = pids; tmp != NULL; tmp = next) {
next = tmp->next;
if (waitpid(GPOINTER_TO_INT(tmp->data), &status, WNOHANG) > 0) {
int pid = GPOINTER_TO_INT(tmp->data);
next = tmp->next;
if (waitpid(pid, &status, WNOHANG) > 0) {
/* process terminated, remove from list */
pids = g_slist_remove(pids, tmp->data);
signal_emit_id(signal_pidwait, 1, GPOINTER_TO_INT(tmp->data));
signal_emit_id(signal_pidwait, 1, tmp->data);
}
}
return 1;

View File

@ -56,7 +56,8 @@ static void rawlog_add(RAWLOG_REC *rawlog, char *str)
rawlog->nlines++;
else {
g_free(rawlog->lines->data);
rawlog->lines = g_slist_remove(rawlog->lines, rawlog->lines->data);
rawlog->lines = g_slist_remove(rawlog->lines,
rawlog->lines->data);
}
if (rawlog->logging) {
@ -113,7 +114,8 @@ void rawlog_open(RAWLOG_REC *rawlog, const char *fname)
return;
path = convert_home(fname);
rawlog->handle = open(path, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode);
rawlog->handle = open(path, O_WRONLY | O_APPEND | O_CREAT,
log_file_create_mode);
g_free(path);
rawlog_dump(rawlog, rawlog->handle);

View File

@ -63,20 +63,24 @@ static void sig_disconnected(SERVER_REC *server)
g_return_if_fail(server != NULL);
if (server->eventtable != NULL) {
g_hash_table_foreach(server->eventtable, (GHFunc) server_eventtable_destroy, NULL);
g_hash_table_foreach(server->eventtable,
(GHFunc) server_eventtable_destroy, NULL);
g_hash_table_destroy(server->eventtable);
}
g_hash_table_foreach(server->eventgrouptable, (GHFunc) server_eventgrouptable_destroy, NULL);
g_hash_table_foreach(server->eventgrouptable,
(GHFunc) server_eventgrouptable_destroy, NULL);
g_hash_table_destroy(server->eventgrouptable);
if (server->cmdtable != NULL) {
g_hash_table_foreach(server->cmdtable, (GHFunc) server_cmdtable_destroy, NULL);
g_hash_table_foreach(server->cmdtable,
(GHFunc) server_cmdtable_destroy, NULL);
g_hash_table_destroy(server->cmdtable);
}
}
void server_redirect_initv(SERVER_REC *server, const char *command, int last, GSList *list)
void server_redirect_initv(SERVER_REC *server, const char *command,
int last, GSList *list)
{
REDIRECT_CMD_REC *rec;
@ -85,7 +89,7 @@ void server_redirect_initv(SERVER_REC *server, const char *command, int last, GS
g_return_if_fail(last > 0);
if (g_hash_table_lookup(server->cmdtable, command) != NULL) {
/* already in hash table. list of events SHOULD be the same... */
/* already in hash table. list of events SHOULD be the same. */
g_slist_foreach(list, (GFunc) g_free, NULL);
g_slist_free(list);
return;
@ -97,7 +101,8 @@ void server_redirect_initv(SERVER_REC *server, const char *command, int last, GS
g_hash_table_insert(server->cmdtable, g_strdup(command), rec);
}
void server_redirect_init(SERVER_REC *server, const char *command, int last, ...)
void server_redirect_init(SERVER_REC *server, const char *command,
int last, ...)
{
va_list args;
GSList *list;
@ -112,8 +117,9 @@ void server_redirect_init(SERVER_REC *server, const char *command, int last, ...
server_redirect_initv(server, command, last, list);
}
int server_redirect_single_event(SERVER_REC *server, const char *arg, int last, int group,
const char *event, const char *signal, int argpos)
int server_redirect_single_event(SERVER_REC *server, const char *arg,
int last, int group, const char *event,
const char *signal, int argpos)
{
REDIRECT_REC *rec;
GSList *list, *grouplist;
@ -133,21 +139,28 @@ int server_redirect_single_event(SERVER_REC *server, const char *arg, int last,
rec->group = group;
rec->last = last;
if (g_hash_table_lookup_extended(server->eventtable, event, (gpointer *) &origkey, (gpointer *) &list))
if (g_hash_table_lookup_extended(server->eventtable, event,
(gpointer *) &origkey,
(gpointer *) &list)) {
g_hash_table_remove(server->eventtable, origkey);
else {
} else {
list = NULL;
origkey = g_strdup(event);
}
grouplist = g_hash_table_lookup(server->eventgrouptable, GINT_TO_POINTER(group));
if (grouplist != NULL) g_hash_table_remove(server->eventgrouptable, GINT_TO_POINTER(group));
grouplist = g_hash_table_lookup(server->eventgrouptable,
GINT_TO_POINTER(group));
if (grouplist != NULL) {
g_hash_table_remove(server->eventgrouptable,
GINT_TO_POINTER(group));
}
list = g_slist_append(list, rec);
grouplist = g_slist_append(grouplist, g_strdup(event));
g_hash_table_insert(server->eventtable, origkey, list);
g_hash_table_insert(server->eventgrouptable, GINT_TO_POINTER(group), grouplist);
g_hash_table_insert(server->eventgrouptable,
GINT_TO_POINTER(group), grouplist);
return group;
}
@ -167,7 +180,9 @@ void server_redirect_event(SERVER_REC *server, const char *arg, int last, ...)
signal = va_arg(args, gchar *);
argpos = va_arg(args, gint);
group = server_redirect_single_event(server, arg, last > 0, group, event, signal, argpos);
group = server_redirect_single_event(server, arg, last > 0,
group, event, signal,
argpos);
last--;
}
@ -193,12 +208,14 @@ void server_redirect_default(SERVER_REC *server, const char *command)
/* add all events used by command to eventtable and eventgrouptable */
redirect_group++; grouplist = NULL; last = cmdrec->last;
for (events = cmdrec->events; events != NULL; events = events->next, last--) {
for (events = cmdrec->events; events != NULL; events = events->next) {
event = events->data;
if (g_hash_table_lookup_extended(server->eventtable, event, (gpointer *) &origkey, (gpointer *) &list))
if (g_hash_table_lookup_extended(server->eventtable, event,
(gpointer *) &origkey,
(gpointer *) &list)) {
g_hash_table_remove(server->eventtable, origkey);
else {
} else {
list = NULL;
origkey = g_strdup(event);
}
@ -212,12 +229,16 @@ void server_redirect_default(SERVER_REC *server, const char *command)
grouplist = g_slist_append(grouplist, g_strdup(event));
list = g_slist_append(list, rec);
g_hash_table_insert(server->eventtable, origkey, list);
last--;
}
g_hash_table_insert(server->eventgrouptable, GINT_TO_POINTER(redirect_group), grouplist);
g_hash_table_insert(server->eventgrouptable,
GINT_TO_POINTER(redirect_group), grouplist);
}
void server_redirect_remove_next(SERVER_REC *server, const char *event, GSList *item)
void server_redirect_remove_next(SERVER_REC *server, const char *event,
GSList *item)
{
REDIRECT_REC *rec;
GSList *grouplist, *list, *events, *tmp;
@ -227,7 +248,9 @@ void server_redirect_remove_next(SERVER_REC *server, const char *event, GSList *
g_return_if_fail(server != NULL);
g_return_if_fail(event != NULL);
if (!g_hash_table_lookup_extended(server->eventtable, event, (gpointer *) &origkey, (gpointer *) &list))
if (!g_hash_table_lookup_extended(server->eventtable, event,
(gpointer *) &origkey,
(gpointer *) &list))
return;
rec = item == NULL ? list->data : item->data;
@ -238,14 +261,19 @@ void server_redirect_remove_next(SERVER_REC *server, const char *event, GSList *
group = rec->group;
/* get list of events from this group */
grouplist = g_hash_table_lookup(server->eventgrouptable, GINT_TO_POINTER(group));
grouplist = g_hash_table_lookup(server->eventgrouptable,
GINT_TO_POINTER(group));
/* remove all of them */
for (list = grouplist; list != NULL; list = list->next) {
char *event = list->data;
if (!g_hash_table_lookup_extended(server->eventtable, event, (gpointer *) &origkey, (gpointer *) &events)) {
g_warning("server_redirect_remove_next() : event in eventgrouptable but not in eventtable");
if (!g_hash_table_lookup_extended(server->eventtable, event,
(gpointer *) &origkey,
(gpointer *) &events)) {
g_warning("server_redirect_remove_next() : "
"event in eventgrouptable but not in "
"eventtable");
continue;
}
@ -258,7 +286,9 @@ void server_redirect_remove_next(SERVER_REC *server, const char *event, GSList *
}
if (rec == NULL) {
g_warning("server_redirect_remove_next() : event in eventgrouptable but not in eventtable (group)");
g_warning("server_redirect_remove_next() : "
"event in eventgrouptable but not in "
"eventtable (group)");
continue;
}
@ -273,15 +303,18 @@ void server_redirect_remove_next(SERVER_REC *server, const char *event, GSList *
g_hash_table_remove(server->eventtable, origkey);
if (events == NULL)
g_free(origkey);
else
g_hash_table_insert(server->eventtable, origkey, events);
else {
g_hash_table_insert(server->eventtable,
origkey, events);
}
}
g_hash_table_remove(server->eventgrouptable, GINT_TO_POINTER(group));
g_slist_free(grouplist);
}
GSList *server_redirect_getqueue(SERVER_REC *server, const char *event, const char *args)
GSList *server_redirect_getqueue(SERVER_REC *server, const char *event,
const char *args)
{
REDIRECT_REC *rec;
GSList *list;

View File

@ -129,6 +129,7 @@ static void server_connect_callback_readpipe(SERVER_REC *server)
{
SERVER_CONNECT_REC *conn;
RESOLVED_IP_REC iprec;
const char *errormsg;
int handle;
g_source_remove(server->connect_tag);
@ -157,21 +158,33 @@ static void server_connect_callback_readpipe(SERVER_REC *server)
other error in nameserver */
server->connection_lost = TRUE;
}
server_cant_connect(server,
iprec.error == 0 ? g_strerror(errno) : /* connect() failed */
(iprec.errorstr != NULL ? iprec.errorstr : "Host lookup failed")); /* gethostbyname() failed */
if (iprec.error == 0) {
/* connect() failed */
errormsg = g_strerror(errno);
} else {
/* gethostbyname() failed */
errormsg = iprec.errorstr != NULL ? iprec.errorstr :
"Host lookup failed";
}
server_cant_connect(server, rrormsg);
g_free_not_null(iprec.errorstr);
return;
}
server->handle = net_sendbuffer_create(handle, 0);
server->connect_tag = g_input_add(handle, G_INPUT_WRITE|G_INPUT_READ|G_INPUT_EXCEPTION,
(GInputFunction) server_connect_callback_init, server);
server->connect_tag =
g_input_add(handle, G_INPUT_WRITE | G_INPUT_READ |
G_INPUT_EXCEPTION,
(GInputFunction) server_connect_callback_init,
server);
signal_emit("server connecting", 2, server, &iprec.ip);
}
int server_connect(SERVER_REC *server)
{
const char *connect_address;
g_return_val_if_fail(server != NULL, FALSE);
MODULE_DATA_INIT(server);
@ -182,9 +195,11 @@ int server_connect(SERVER_REC *server)
}
server->tag = server_create_tag(server->connrec);
connect_address = server->connrec->proxy != NULL ?
server->connrec->proxy : server->connrec->address;
server->connect_pid =
net_gethostbyname_nonblock(server->connrec->proxy != NULL ?
server->connrec->proxy : server->connrec->address,
net_gethostbyname_nonblock(connect_address,
server->connect_pipe[1]);
server->connect_tag =
g_input_add(server->connect_pipe[0], G_INPUT_READ,

View File

@ -32,7 +32,7 @@ typedef struct {
int connected:1; /* connected to server */
int connection_lost:1; /* Connection lost unintentionally */
int handle; /* socket handle */
void *handle; /* NET_SENDBUF_REC socket */
int readtag; /* input tag */
/* for net_connect_nonblock() */

View File

@ -42,7 +42,8 @@ static const char *settings_get_default_str(const char *key)
rec = g_hash_table_lookup(settings, key);
if (rec == NULL) {
g_warning("settings_get_default_str(%s) : unknown setting", key);
g_warning("settings_get_default_str(%s) : "
"unknown setting", key);
return NULL;
}
@ -57,7 +58,8 @@ static int settings_get_default_int(const char *key)
rec = g_hash_table_lookup(settings, key);
if (rec == NULL) {
g_warning("settings_get_default_int(%s) : unknown setting", key);
g_warning("settings_get_default_int(%s) : "
"unknown setting", key);
return -1;
}
@ -76,7 +78,8 @@ const int settings_get_int(const char *key)
const int settings_get_bool(const char *key)
{
return iconfig_get_bool("settings", key, settings_get_default_int(key));
return iconfig_get_bool("settings", key,
settings_get_default_int(key));
}
void settings_add_str(const char *section, const char *key, const char *def)
@ -180,9 +183,11 @@ static int settings_compare(SETTINGS_REC *v1, SETTINGS_REC *v2)
return strcmp(v1->section, v2->section);
}
static void settings_hash_get(const char *key, SETTINGS_REC *rec, GSList **list)
static void settings_hash_get(const char *key, SETTINGS_REC *rec,
GSList **list)
{
*list = g_slist_insert_sorted(*list, rec, (GCompareFunc) settings_compare);
*list = g_slist_insert_sorted(*list, rec,
(GCompareFunc) settings_compare);
}
GSList *settings_get_sorted(void)
@ -215,14 +220,9 @@ static CONFIG_REC *parse_configfile(const char *fname)
g_strdup_printf("%s/.irssi/config", g_get_home_dir());
config = config_open(real_fname, -1);
if (config == NULL && fname != NULL) {
g_free(real_fname);
return NULL; /* specified config file not found */
}
if (config != NULL)
config_parse(config);
else {
else if (fname == NULL) {
/* user configuration file not found, use the default one
from sysconfdir */
config = config_open(SYSCONFDIR"/irssi/config", -1);
@ -245,7 +245,8 @@ static CONFIG_REC *parse_configfile(const char *fname)
static void sig_print_config_error(void)
{
signal_emit("gui dialog", 2, "error", last_error_msg);
signal_remove("irssi init finished", (SIGNAL_FUNC) sig_print_config_error);
signal_remove("irssi init finished",
(SIGNAL_FUNC) sig_print_config_error);
g_free_and_null(last_error_msg);
}
@ -258,12 +259,14 @@ static void init_configfile(void)
str = g_strdup_printf("%s/.irssi", g_get_home_dir());
if (stat(str, &statbuf) != 0) {
/* ~/.irssi not found, create it. */
if (mkdir(str, 0700) != 0)
g_error(_("Couldn't create %s/.irssi directory"), g_get_home_dir());
} else {
if (!S_ISDIR(statbuf.st_mode)) {
g_error(_("%s/.irssi is not a directory.\nYou should remove it with command: rm ~/.irssi"), g_get_home_dir());
if (mkdir(str, 0700) != 0) {
g_error(_("Couldn't create %s/.irssi directory"),
g_get_home_dir());
}
} else if (!S_ISDIR(statbuf.st_mode)) {
g_error(_("%s/.irssi is not a directory.\n"
"You should remove it with command: rm ~/.irssi"),
g_get_home_dir());
}
g_free(str);
@ -271,9 +274,12 @@ static void init_configfile(void)
/* any errors? */
if (config_last_error(mainconfig) != NULL) {
last_error_msg = g_strdup_printf(_("Ignored errors in configuration file:\n%s"),
config_last_error(mainconfig));
signal_add("irssi init finished", (SIGNAL_FUNC) sig_print_config_error);
last_error_msg =
g_strdup_printf(_("Ignored errors in configuration "
"file:\n%s"),
config_last_error(mainconfig));
signal_add("irssi init finished",
(SIGNAL_FUNC) sig_print_config_error);
}
signal(SIGTERM, sig_term);
@ -296,11 +302,11 @@ void settings_reread(const char *fname)
}
if (config_last_error(tempconfig) != NULL) {
/* error */
str = g_strdup_printf(_("Errors in configuration file:\n%s"),
config_last_error(tempconfig));
signal_emit("gui dialog", 2, "error", str);
g_free(str);
config_close(tempconfig);
return;
}
@ -328,7 +334,8 @@ void settings_save(const char *fname)
void settings_init(void)
{
settings = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal);
settings = g_hash_table_new((GHashFunc) g_str_hash,
(GCompareFunc) g_str_equal);
init_configfile();
}

View File

@ -43,7 +43,8 @@ static SIGNAL_REC *first_signal_rec, *last_signal_rec; /* "signal" and "last sig
static SIGNAL_REC *current_emitted_signal;
/* bind a signal */
void signal_add_to(const char *module, int pos, const char *signal, SIGNAL_FUNC func)
void signal_add_to(const char *module, int pos, const char *signal,
SIGNAL_FUNC func)
{
SIGNAL_REC *rec;
int signal_id;
@ -104,7 +105,8 @@ static int signal_list_find(GPtrArray *array, void *data)
return -1;
}
static void signal_remove_from_list(SIGNAL_REC *rec, int signal_id, int list, int index)
static void signal_remove_from_list(SIGNAL_REC *rec, int signal_id,
int list, int index)
{
if (rec->emitting) {
g_ptr_array_index(rec->siglist[list], index) = NULL;
@ -118,7 +120,8 @@ static void signal_remove_from_list(SIGNAL_REC *rec, int signal_id, int list, in
}
/* Remove signal from emit lists */
static int signal_remove_from_lists(SIGNAL_REC *rec, int signal_id, SIGNAL_FUNC func)
static int signal_remove_from_lists(SIGNAL_REC *rec, int signal_id,
SIGNAL_FUNC func)
{
int n, index;
@ -149,9 +152,13 @@ void signal_remove(const char *signal, SIGNAL_FUNC func)
signal_id = signal_get_uniq_id(signal);
rec = g_hash_table_lookup(signals, GINT_TO_POINTER(signal_id));
found = rec == NULL ? 0 : signal_remove_from_lists(rec, signal_id, func);
found = rec == NULL ? 0 :
signal_remove_from_lists(rec, signal_id, func);
if (!found) g_warning("signal_remove() : signal \"%s\" isn't grabbed for %p", signal, func);
if (!found) {
g_warning("signal_remove() : signal \"%s\" isn't "
"grabbed for %p", signal, func);
}
}
/* Remove all NULL functions from signal list */

View File

@ -99,7 +99,8 @@ static char *get_internal_setting(const char *key, int type, int *free_ret)
return NULL;
}
static char *get_long_variable_value(const char *key, void *server, void *item, int *free_ret)
static char *get_long_variable_value(const char *key, void *server,
void *item, int *free_ret)
{
EXPANDO_FUNC func;
char *ret;
@ -125,7 +126,8 @@ static char *get_long_variable_value(const char *key, void *server, void *item,
return NULL;
}
static char *get_long_variable(char **cmd, void *server, void *item, int *free_ret)
static char *get_long_variable(char **cmd, void *server,
void *item, int *free_ret)
{
char *start, *var, *ret;
@ -140,7 +142,8 @@ static char *get_long_variable(char **cmd, void *server, void *item, int *free_r
}
/* return the value of the variable found from `cmd' */
static char *get_variable(char **cmd, void *server, void *item, char **arglist, int *free_ret, int *arg_used)
static char *get_variable(char **cmd, void *server, void *item,
char **arglist, int *free_ret, int *arg_used)
{
if (isdigit(**cmd) || **cmd == '*' || **cmd == '-' || **cmd == '~') {
/* argument */
@ -180,7 +183,8 @@ static char *get_history(char **cmd, void *item, int *free_ret)
return ret;
}
static char *get_special_value(char **cmd, void *server, void *item, char **arglist, int *free_ret, int *arg_used)
static char *get_special_value(char **cmd, void *server, void *item,
char **arglist, int *free_ret, int *arg_used)
{
char command, *value, *p;
int len;
@ -309,7 +313,8 @@ static char *get_alignment(const char *text, int align, int flags, char pad)
/* Parse and expand text after '$' character. return value has to be
g_free()'d if `free_ret' is TRUE. */
char *parse_special(char **cmd, void *server, void *item, char **arglist, int *free_ret, int *arg_used)
char *parse_special(char **cmd, void *server, void *item,
char **arglist, int *free_ret, int *arg_used)
{
static char **nested_orig_cmd = NULL; /* FIXME: KLUDGE! */
char command, *value;
@ -326,9 +331,9 @@ char *parse_special(char **cmd, void *server, void *item, char **arglist, int *f
switch (command) {
case '[':
/* alignment */
if (!get_alignment_args(cmd, &align, &align_flags, &align_pad) ||
**cmd == '\0') {
(*cmd)--;
if (!get_alignment_args(cmd, &align, &align_flags,
&align_pad) || **cmd == '\0') {
(*cmd)--;
return NULL;
}
break;
@ -349,12 +354,14 @@ char *parse_special(char **cmd, void *server, void *item, char **arglist, int *f
nest_value = *cmd;
} else {
(*cmd)++;
nest_value = parse_special(cmd, server, item, arglist, &nest_free, arg_used);
nest_value = parse_special(cmd, server, item, arglist,
&nest_free, arg_used);
}
while ((*nested_orig_cmd)[1] != '\0') {
(*nested_orig_cmd)++;
if (**nested_orig_cmd == ')') break;
if (**nested_orig_cmd == ')')
break;
}
cmd = &nest_value;
@ -369,7 +376,8 @@ char *parse_special(char **cmd, void *server, void *item, char **arglist, int *f
brackets = TRUE;
}
value = get_special_value(cmd, server, item, arglist, free_ret, arg_used);
value = get_special_value(cmd, server, item, arglist,
free_ret, arg_used);
if (**cmd == '\0')
g_error("parse_special() : buffer overflow!");
@ -397,7 +405,8 @@ char *parse_special(char **cmd, void *server, void *item, char **arglist, int *f
}
/* parse the whole string. $ and \ chars are replaced */
char *parse_special_string(const char *cmd, void *server, void *item, const char *data, int *arg_used)
char *parse_special_string(const char *cmd, void *server, void *item,
const char *data, int *arg_used)
{
char code, **arglist, *ret;
GString *str;
@ -428,7 +437,8 @@ char *parse_special_string(const char *cmd, void *server, void *item, const char
} else if (code == '$') {
char *ret;
ret = parse_special((char **) &cmd, server, item, arglist, &need_free, arg_used);
ret = parse_special((char **) &cmd, server, item,
arglist, &need_free, arg_used);
if (ret != NULL) {
g_string_append(str, ret);
if (need_free) g_free(ret);
@ -450,8 +460,13 @@ char *parse_special_string(const char *cmd, void *server, void *item, const char
return ret;
}
#define is_split_char(str, start) \
((str)[0] == ';' && ((start) == (str) || \
((str)[-1] != '\\' && (str)[-1] != '$')))
/* execute the commands in string - commands can be split with ';' */
void eval_special_string(const char *cmd, const char *data, void *server, void *item)
void eval_special_string(const char *cmd, const char *data,
void *server, void *item)
{
const char *cmdchars;
char *orig, *str, *start, *ret;
@ -460,14 +475,15 @@ void eval_special_string(const char *cmd, const char *data, void *server, void *
cmdchars = settings_get_str("cmdchars");
orig = start = str = g_strdup(cmd);
do {
if (*str == ';' && (start == str || (str[-1] != '\\' && str[-1] != '$')))
if (is_split_char(str, start))
*str++ = '\0';
else if (*str != '\0') {
str++;
continue;
}
ret = parse_special_string(start, server, item, data, &arg_used);
ret = parse_special_string(start, server, item,
data, &arg_used);
if (strchr(cmdchars, *ret) == NULL) {
/* no command char - let's put it there.. */
char *old = ret;
@ -505,7 +521,8 @@ void expando_create(const char *key, EXPANDO_FUNC func)
return;
}
if (g_hash_table_lookup_extended(expandos, key, &origkey, &origvalue)) {
if (g_hash_table_lookup_extended(expandos, key, &origkey,
&origvalue)) {
g_free(origkey);
g_hash_table_remove(expandos, key);
}
@ -527,7 +544,8 @@ void expando_destroy(const char *key, EXPANDO_FUNC func)
return;
}
if (g_hash_table_lookup_extended(expandos, key, &origkey, &origvalue)) {
if (g_hash_table_lookup_extended(expandos, key, &origkey,
&origvalue)) {
g_free(origkey);
g_hash_table_remove(expandos, key);
}
@ -618,7 +636,8 @@ void special_vars_init(void)
client_start_time = time(NULL);
memset(char_expandos, 0, sizeof(char_expandos));
expandos = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal);
expandos = g_hash_table_new((GHashFunc) g_str_hash,
(GCompareFunc) g_str_equal);
history_func = NULL;
char_expandos['F'] = expando_clientstarted;

View File

@ -122,8 +122,7 @@ static void cmd_connect(const char *data)
static void cmd_disconnect(const char *data, IRC_SERVER_REC *server)
{
IRC_SERVER_REC *ircserver;
char *tag, *msg;
char *tag, *msg, *str;
void *free_arg;
g_return_if_fail(data != NULL);
@ -142,15 +141,10 @@ static void cmd_disconnect(const char *data, IRC_SERVER_REC *server)
if (*msg == '\0') msg = (char *) settings_get_str("quit_message");
signal_emit("server quit", 2, server, msg);
ircserver = (IRC_SERVER_REC *) server;
if (ircserver->handle != -1 && ircserver->buffer != NULL) {
/* flush transmit queue */
g_slist_foreach(ircserver->cmdqueue, (GFunc) g_free, NULL);
g_slist_free(ircserver->cmdqueue);
ircserver->cmdqueue = NULL;
ircserver->cmdcount = 0;
irc_send_cmdv(ircserver, "QUIT :%s", msg);
if (server->handle != NULL && server->buffer != NULL) {
str = g_strdup_printf("QUIT :%s", msg);
irc_send_cmd_now(server, str);
g_free(str);
}
cmd_params_free(free_arg);