1
0
mirror of https://github.com/irssi/irssi.git synced 2024-06-16 06:25:24 +00:00

Split expandos from special-vars.c to expandos.c. Added list of signals

to each expando that can might change it's value.


git-svn-id: http://svn.irssi.org/repos/irssi/trunk@964 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
Timo Sirainen 2000-12-05 00:53:04 +00:00 committed by cras
parent 5f941b8fa6
commit d1eaeca255
19 changed files with 687 additions and 520 deletions

View File

@ -120,7 +120,7 @@ irc-servers.c:
irc.c:
"server event", char *data, SERVER_REC, char *sender_nick, char *sender_address
"server event", SERVER_REC, char *data, char *sender_nick, char *sender_address
"event "<cmd>, char *args, SERVER_REC, char *sender_nick, char *sender_address
"default event", char *data, SERVER_REC, char *sender_nick, char *sender_address

View File

@ -22,6 +22,7 @@ libcore_a_SOURCES = \
chat-protocols.c \
chatnets.c \
core.c \
expandos.c \
ignore.c \
levels.c \
line-split.c \
@ -63,6 +64,7 @@ noinst_HEADERS = \
chat-protocols.h \
chatnets.h \
core.h \
expandos.h \
ignore.h \
levels.h \
line-split.h \

View File

@ -31,10 +31,10 @@
#include "servers.h"
#include "chatnets.h"
#include "commands.h"
#include "expandos.h"
#include "log.h"
#include "rawlog.h"
#include "ignore.h"
#include "special-vars.h"
#include "channels.h"
#include "queries.h"
@ -60,11 +60,11 @@ void core_init(void)
chat_protocols_init();
chatnets_init();
expandos_init();
ignore_init();
servers_init();
log_init();
rawlog_init();
special_vars_init();
channels_init();
queries_init();
@ -81,11 +81,11 @@ void core_deinit(void)
queries_deinit();
channels_deinit();
special_vars_deinit();
rawlog_deinit();
log_deinit();
servers_deinit();
ignore_deinit();
expandos_deinit();
chatnets_deinit();
chat_protocols_deinit();

491
src/core/expandos.c Normal file
View File

@ -0,0 +1,491 @@
/*
expandos.c : irssi
Copyright (C) 2000 Timo Sirainen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "module.h"
#include "signals.h"
#include "expandos.h"
#include "settings.h"
#include "commands.h"
#include "misc.h"
#include "irssi-version.h"
#include "channels.h"
#include "queries.h"
#include "window-item-def.h"
#ifdef HAVE_SYS_UTSNAME_H
# include <sys/utsname.h>
#endif
typedef struct {
EXPANDO_FUNC func;
GPtrArray *signals, *signal_args;
} EXPANDO_REC;
static EXPANDO_REC *char_expandos[127];
static GHashTable *expandos;
static time_t client_start_time;
static char *last_sent_msg, *last_sent_msg_body;
static char *last_privmsg_from, *last_public_from;
/* Create expando - overrides any existing ones. */
void expando_create(const char *key, EXPANDO_FUNC func, ...)
{
EXPANDO_REC *rec;
const char *signal;
va_list va;
g_return_if_fail(key != NULL || *key == '\0');
g_return_if_fail(func != NULL);
if (key[1] != '\0')
rec = g_hash_table_lookup(expandos, key);
else {
/* single character expando */
rec = char_expandos[(int) *key];
}
if (rec == NULL) {
rec = g_new0(EXPANDO_REC, 1);
if (key[1] != '\0')
g_hash_table_insert(expandos, g_strdup(key), rec);
else
char_expandos[(int) *key] = rec;
} else if (rec->signals != NULL) {
g_ptr_array_free(rec->signals, TRUE);
rec->signals = NULL;
}
rec->func = func;
va_start(va, func);
while ((signal = (const char *) va_arg(va, const char *)) != NULL)
expando_add_signal(key, signal, (int) va_arg(va, int));
va_end(va);
}
/* Add new signal to expando */
void expando_add_signal(const char *key, const char *signal, ExpandoArg arg)
{
EXPANDO_REC *rec;
g_return_if_fail(key != NULL);
g_return_if_fail(signal != NULL);
if (key[1] != '\0')
rec = g_hash_table_lookup(expandos, key);
else {
/* single character expando */
rec = char_expandos[(int) *key];
}
g_return_if_fail(rec != NULL);
if (rec->signals == NULL) {
rec->signals = g_ptr_array_new();
rec->signal_args = g_ptr_array_new();
}
g_ptr_array_add(rec->signals,
GINT_TO_POINTER(signal_get_uniq_id(signal)));
g_ptr_array_add(rec->signal_args,
GINT_TO_POINTER(arg));
}
static void expando_destroy_rec(EXPANDO_REC *rec)
{
g_return_if_fail(rec != NULL);
if (rec->signals != NULL) {
g_ptr_array_free(rec->signals, TRUE);
g_ptr_array_free(rec->signal_args, TRUE);
}
g_free(rec);
}
/* Destroy expando */
void expando_destroy(const char *key, EXPANDO_FUNC func)
{
gpointer origkey;
EXPANDO_REC *rec;
g_return_if_fail(key != NULL || *key == '\0');
g_return_if_fail(func != NULL);
if (key[1] == '\0') {
/* single character expando */
rec = char_expandos[(int) *key];
if (rec != NULL && rec->func == func) {
char_expandos[(int) *key] = NULL;
expando_destroy_rec(rec);
}
} else if (g_hash_table_lookup_extended(expandos, key, &origkey,
(gpointer *) &rec)) {
if (rec->func == func) {
g_free(origkey);
g_hash_table_remove(expandos, key);
expando_destroy_rec(rec);
}
}
}
EXPANDO_FUNC expando_find_char(char chr)
{
g_return_val_if_fail(chr < sizeof(char_expandos) /
sizeof(char_expandos[0]), NULL);
return char_expandos[(int) chr] == NULL ? NULL :
char_expandos[(int) chr]->func;
}
EXPANDO_FUNC expando_find_long(const char *key)
{
EXPANDO_REC *rec = g_hash_table_lookup(expandos, key);
return rec == NULL ? NULL : rec->func;
}
/* last person who sent you a MSG */
static char *expando_lastmsg(SERVER_REC *server, void *item, int *free_ret)
{
return last_privmsg_from;
}
/* last person to whom you sent a MSG */
static char *expando_lastmymsg(SERVER_REC *server, void *item, int *free_ret)
{
return last_sent_msg;
}
/* last person to send a public message to a channel you are on */
static char *expando_lastpublic(SERVER_REC *server, void *item, int *free_ret)
{
return last_public_from;
}
/* text of your AWAY message, if any */
static char *expando_awaymsg(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->away_reason;
}
/* body of last MSG you sent */
static char *expando_lastmymsg_body(SERVER_REC *server, void *item, int *free_ret)
{
return last_sent_msg_body;
}
/* current channel */
static char *expando_channel(SERVER_REC *server, void *item, int *free_ret)
{
return !IS_CHANNEL(item) ? NULL : CHANNEL(item)->name;
}
/* time client was started, $time() format */
static char *expando_clientstarted(SERVER_REC *server, void *item, int *free_ret)
{
*free_ret = TRUE;
return g_strdup_printf("%ld", (long) client_start_time);
}
/* channel you were last INVITEd to */
static char *expando_last_invite(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->last_invite;
}
/* client version text string */
static char *expando_version(SERVER_REC *server, void *item, int *free_ret)
{
return IRSSI_VERSION;
}
/* current value of CMDCHARS */
static char *expando_cmdchars(SERVER_REC *server, void *item, int *free_ret)
{
return (char *) settings_get_str("cmdchars");
}
/* modes of current channel, if any */
static char *expando_chanmode(SERVER_REC *server, void *item, int *free_ret)
{
return !IS_CHANNEL(item) ? NULL : CHANNEL(item)->mode;
}
/* current nickname */
static char *expando_nick(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->nick;
}
/* value of STATUS_OPER if you are an irc operator */
static char *expando_statusoper(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL || !server->server_operator ? "" :
(char *) settings_get_str("STATUS_OPER");
}
/* if you are a channel operator in $C, expands to a '@' */
static char *expando_chanop(SERVER_REC *server, void *item, int *free_ret)
{
return IS_CHANNEL(item) && CHANNEL(item)->chanop ? "@" : "";
}
/* nickname of whomever you are QUERYing */
static char *expando_query(SERVER_REC *server, void *item, int *free_ret)
{
return !IS_QUERY(item) ? "" : QUERY(item)->name;
}
/* version of current server */
static char *expando_serverversion(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->version;
}
/* target of current input (channel or QUERY nickname) */
static char *expando_target(SERVER_REC *server, void *item, int *free_ret)
{
return ((WI_ITEM_REC *) item)->name;
}
/* client release date (numeric version string) */
static char *expando_releasedate(SERVER_REC *server, void *item, int *free_ret)
{
return IRSSI_VERSION_DATE;
}
/* current working directory */
static char *expando_workdir(SERVER_REC *server, void *item, int *free_ret)
{
*free_ret = TRUE;
return g_get_current_dir();
}
/* value of REALNAME */
static char *expando_realname(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->connrec->realname;
}
/* time of day (hh:mm) */
static char *expando_time(SERVER_REC *server, void *item, int *free_ret)
{
time_t now = time(NULL);
struct tm *tm;
tm = localtime(&now);
*free_ret = TRUE;
return g_strdup_printf("%02d:%02d", tm->tm_hour, tm->tm_min);
}
/* a literal '$' */
static char *expando_dollar(SERVER_REC *server, void *item, int *free_ret)
{
return "$";
}
/* system name */
static char *expando_sysname(SERVER_REC *server, void *item, int *free_ret)
{
#ifdef HAVE_SYS_UTSNAME_H
struct utsname un;
if (uname(&un) == -1)
return NULL;
*free_ret = TRUE;
return g_strdup(un.sysname);
#else
return NULL;
#endif
}
/* system release */
static char *expando_sysrelease(SERVER_REC *server, void *item, int *free_ret)
{
#ifdef HAVE_SYS_UTSNAME_H
struct utsname un;
if (uname(&un) == -1)
return NULL;
*free_ret = TRUE;
return g_strdup(un.release);
#else
return NULL;
#endif
}
/* Server tag */
static char *expando_servertag(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->tag;
}
/* Server chatnet */
static char *expando_chatnet(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->connrec->chatnet;
}
static void sig_message_private(SERVER_REC *server, const char *msg,
const char *nick, const char *address)
{
g_free_not_null(last_privmsg_from);
last_privmsg_from = g_strdup(nick);
}
static void sig_message_public(SERVER_REC *server, const char *msg,
const char *nick, const char *address,
const char *target)
{
g_free_not_null(last_public_from);
last_public_from = g_strdup(nick);
}
static void cmd_msg(const char *data, SERVER_REC *server)
{
GHashTable *optlist;
char *target, *msg;
void *free_arg;
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS |
PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
"msg", &optlist, &target, &msg))
return;
if (*target != '\0' && *msg != '\0' &&
!server->ischannel(*target) && isalpha(*target)) {
g_free_not_null(last_sent_msg);
g_free_not_null(last_sent_msg_body);
last_sent_msg = g_strdup(target);
last_sent_msg_body = g_strdup(msg);
}
cmd_params_free(free_arg);
}
void expandos_init(void)
{
settings_add_str("misc", "STATUS_OPER", "*");
client_start_time = time(NULL);
last_sent_msg = NULL; last_sent_msg_body = NULL;
last_privmsg_from = NULL; last_public_from = NULL;
memset(char_expandos, 0, sizeof(char_expandos));
expandos = g_hash_table_new((GHashFunc) g_str_hash,
(GCompareFunc) g_str_equal);
expando_create(",", expando_lastmsg,
"message private", EXPANDO_ARG_SERVER, NULL);
expando_create(".", expando_lastmymsg,
"command msg", EXPANDO_ARG_NONE, NULL);
expando_create(";", expando_lastpublic,
"message public", EXPANDO_ARG_SERVER, NULL);
expando_create("A", expando_awaymsg,
"away mode changed", EXPANDO_ARG_NONE, NULL);
expando_create("B", expando_lastmymsg_body,
"command msg", EXPANDO_ARG_NONE, NULL);
expando_create("C", expando_channel,
"window changed", EXPANDO_ARG_NONE,
"window item changed", EXPANDO_ARG_WINDOW, NULL);
expando_create("F", expando_clientstarted,
"", EXPANDO_NEVER, NULL);
expando_create("I", expando_last_invite, NULL);
expando_create("J", expando_version,
"", EXPANDO_NEVER, NULL);
expando_create("K", expando_cmdchars,
"setup changed", EXPANDO_ARG_NONE, NULL);
expando_create("M", expando_chanmode,
"window changed", EXPANDO_ARG_NONE,
"window item changed", EXPANDO_ARG_WINDOW,
"channel mode changed", EXPANDO_ARG_WINDOW_ITEM, NULL);
expando_create("N", expando_nick,
"window changed", EXPANDO_ARG_NONE,
"window server changed", EXPANDO_ARG_WINDOW,
"server nick changed", EXPANDO_ARG_SERVER, NULL);
expando_create("O", expando_statusoper,
"setup changed", EXPANDO_ARG_NONE,
"window changed", EXPANDO_ARG_NONE,
"window server changed", EXPANDO_ARG_WINDOW,
"user mode changed", EXPANDO_ARG_WINDOW, NULL);
expando_create("P", expando_chanop,
"window changed", EXPANDO_ARG_NONE,
"window item changed", EXPANDO_ARG_WINDOW,
"nick mode changed", EXPANDO_ARG_WINDOW_ITEM, NULL);
expando_create("Q", expando_query,
"window changed", EXPANDO_ARG_NONE,
"window item changed", EXPANDO_ARG_WINDOW, NULL);
expando_create("R", expando_serverversion,
"window changed", EXPANDO_ARG_NONE,
"window server changed", EXPANDO_ARG_WINDOW, NULL);
expando_create("T", expando_target,
"window changed", EXPANDO_ARG_NONE,
"window item changed", EXPANDO_ARG_WINDOW, NULL);
expando_create("V", expando_releasedate,
"", EXPANDO_NEVER, NULL);
expando_create("W", expando_workdir, NULL);
expando_create("Y", expando_realname,
"window changed", EXPANDO_ARG_NONE,
"window server changed", EXPANDO_ARG_WINDOW, NULL);
expando_create("Z", expando_time, NULL);
expando_create("$", expando_dollar,
"", EXPANDO_NEVER, NULL);
expando_create("sysname", expando_sysname,
"", EXPANDO_NEVER, NULL);
expando_create("sysrelease", expando_sysrelease,
"", EXPANDO_NEVER, NULL);
expando_create("tag", expando_servertag,
"window changed", EXPANDO_ARG_NONE,
"window server changed", EXPANDO_ARG_WINDOW, NULL);
expando_create("chatnet", expando_chatnet,
"window changed", EXPANDO_ARG_NONE,
"window server changed", EXPANDO_ARG_WINDOW, NULL);
signal_add("command msg", (SIGNAL_FUNC) cmd_msg);
signal_add("message public", (SIGNAL_FUNC) sig_message_public);
signal_add("message private", (SIGNAL_FUNC) sig_message_private);
}
void expandos_deinit(void)
{
int n;
for (n = 0; n < sizeof(char_expandos)/sizeof(char_expandos[0]); n++) {
if (char_expandos[n] != NULL)
expando_destroy_rec(char_expandos[n]);
}
expando_destroy("sysname", expando_sysname);
expando_destroy("sysrelease", expando_sysrelease);
expando_destroy("tag", expando_servertag);
expando_destroy("chatnet", expando_chatnet);
g_hash_table_destroy(expandos);
g_free_not_null(last_sent_msg); g_free_not_null(last_sent_msg_body);
g_free_not_null(last_privmsg_from); g_free_not_null(last_public_from);
signal_remove("message public", (SIGNAL_FUNC) sig_message_public);
signal_remove("message private", (SIGNAL_FUNC) sig_message_private);
signal_remove("command msg", (SIGNAL_FUNC) cmd_msg);
}

36
src/core/expandos.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef __EXPANDOS_H
#define __EXPANDOS_H
#include "servers.h"
/* first argument of signal must match to active .. */
typedef enum {
EXPANDO_ARG_NONE,
EXPANDO_ARG_SERVER,
EXPANDO_ARG_SERVER2, /* second argument.. */
EXPANDO_ARG_WINDOW,
EXPANDO_ARG_WINDOW_ITEM,
EXPANDO_NEVER /* special: expando never changes */
} ExpandoArg;
typedef char* (*EXPANDO_FUNC)
(SERVER_REC *server, void *item, int *free_ret);
/* Create expando - overrides any existing ones.
... = signal, type, ..., NULL - list of signals that might change the
value of this expando */
void expando_create(const char *key, EXPANDO_FUNC func, ...);
/* Add new signal to expando */
void expando_add_signal(const char *key, const char *signal, ExpandoArg arg);
/* Destroy expando */
void expando_destroy(const char *key, EXPANDO_FUNC func);
/* internal: */
EXPANDO_FUNC expando_find_char(char chr);
EXPANDO_FUNC expando_find_long(const char *key);
void expandos_init(void);
void expandos_deinit(void);
#endif

View File

@ -21,25 +21,14 @@
#include "module.h"
#include "signals.h"
#include "special-vars.h"
#include "expandos.h"
#include "settings.h"
#include "misc.h"
#include "irssi-version.h"
#include "channels.h"
#include "queries.h"
#include "window-item-def.h"
#ifdef HAVE_SYS_UTSNAME_H
# include <sys/utsname.h>
#endif
#define ALIGN_RIGHT 0x01
#define ALIGN_CUT 0x02
static EXPANDO_FUNC char_expandos[256];
static GHashTable *expandos;
static time_t client_start_time;
static SPECIAL_HISTORY_FUNC history_func;
static SPECIAL_HISTORY_FUNC history_func = NULL;
static char *get_argument(char **cmd, char **arglist)
{
@ -115,7 +104,7 @@ static char *get_long_variable_value(const char *key, SERVER_REC *server,
*free_ret = FALSE;
/* expando? */
func = (EXPANDO_FUNC) g_hash_table_lookup(expandos, key);
func = expando_find_long(key);
if (func != NULL)
return func(server, item, free_ret);
@ -151,6 +140,8 @@ static char *get_long_variable(char **cmd, SERVER_REC *server,
static char *get_variable(char **cmd, SERVER_REC *server, void *item,
char **arglist, int *free_ret, int *arg_used)
{
EXPANDO_FUNC func;
if (isdigit(**cmd) || **cmd == '*' || **cmd == '-' || **cmd == '~') {
/* argument */
*free_ret = TRUE;
@ -165,8 +156,8 @@ static char *get_variable(char **cmd, SERVER_REC *server, void *item,
/* single character variable. */
*free_ret = FALSE;
return char_expandos[(int) **cmd] == NULL ? NULL :
char_expandos[(int) **cmd](server, item, free_ret);
func = expando_find_char(**cmd);
return func == NULL ? NULL : func(server, item, free_ret);
}
static char *get_history(char **cmd, void *item, int *free_ret)
@ -527,250 +518,7 @@ void eval_special_string(const char *cmd, const char *data,
g_free(orig);
}
/* Create expando - overrides any existing ones. */
void expando_create(const char *key, EXPANDO_FUNC func)
{
gpointer origkey, origvalue;
g_return_if_fail(key != NULL || *key == '\0');
g_return_if_fail(func != NULL);
if (key[1] == '\0') {
/* single character expando */
char_expandos[(int) *key] = func;
return;
}
if (g_hash_table_lookup_extended(expandos, key, &origkey,
&origvalue)) {
g_free(origkey);
g_hash_table_remove(expandos, key);
}
g_hash_table_insert(expandos, g_strdup(key), (void *) func);
}
/* Destroy expando */
void expando_destroy(const char *key, EXPANDO_FUNC func)
{
gpointer origkey, origvalue;
g_return_if_fail(key != NULL || *key == '\0');
g_return_if_fail(func != NULL);
if (key[1] == '\0') {
/* single character expando */
if (char_expandos[(int) *key] == func)
char_expandos[(int) *key] = NULL;
return;
}
if (g_hash_table_lookup_extended(expandos, key, &origkey,
&origvalue)) {
g_free(origkey);
g_hash_table_remove(expandos, key);
}
}
void special_history_func_set(SPECIAL_HISTORY_FUNC func)
{
history_func = func;
}
/* text of your AWAY message, if any */
static char *expando_awaymsg(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->away_reason;
}
/* current channel */
static char *expando_channel(SERVER_REC *server, void *item, int *free_ret)
{
return !IS_CHANNEL(item) ? NULL : CHANNEL(item)->name;
}
/* time client was started, $time() format */
static char *expando_clientstarted(SERVER_REC *server, void *item, int *free_ret)
{
*free_ret = TRUE;
return g_strdup_printf("%ld", (long) client_start_time);
}
/* client version text string */
static char *expando_version(SERVER_REC *server, void *item, int *free_ret)
{
return IRSSI_VERSION;
}
/* current value of CMDCHARS */
static char *expando_cmdchars(SERVER_REC *server, void *item, int *free_ret)
{
return (char *) settings_get_str("cmdchars");
}
/* modes of current channel, if any */
static char *expando_chanmode(SERVER_REC *server, void *item, int *free_ret)
{
return !IS_CHANNEL(item) ? NULL : CHANNEL(item)->mode;
}
/* current nickname */
static char *expando_nick(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->nick;
}
/* value of STATUS_OPER if you are an irc operator */
static char *expando_statusoper(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL || !server->server_operator ? "" :
(char *) settings_get_str("STATUS_OPER");
}
/* if you are a channel operator in $C, expands to a '@' */
static char *expando_chanop(SERVER_REC *server, void *item, int *free_ret)
{
return IS_CHANNEL(item) && CHANNEL(item)->chanop ? "@" : "";
}
/* nickname of whomever you are QUERYing */
static char *expando_query(SERVER_REC *server, void *item, int *free_ret)
{
return !IS_QUERY(item) ? "" : QUERY(item)->name;
}
/* version of current server */
static char *expando_serverversion(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->version;
}
/* target of current input (channel or QUERY nickname) */
static char *expando_target(SERVER_REC *server, void *item, int *free_ret)
{
return ((WI_ITEM_REC *) item)->name;
}
/* client release date (numeric version string) */
static char *expando_releasedate(SERVER_REC *server, void *item, int *free_ret)
{
return IRSSI_VERSION_DATE;
}
/* current working directory */
static char *expando_workdir(SERVER_REC *server, void *item, int *free_ret)
{
*free_ret = TRUE;
return g_get_current_dir();
}
/* value of REALNAME */
static char *expando_realname(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->connrec->realname;
}
/* time of day (hh:mm) */
static char *expando_time(SERVER_REC *server, void *item, int *free_ret)
{
time_t now = time(NULL);
struct tm *tm;
tm = localtime(&now);
*free_ret = TRUE;
return g_strdup_printf("%02d:%02d", tm->tm_hour, tm->tm_min);
}
/* a literal '$' */
static char *expando_dollar(SERVER_REC *server, void *item, int *free_ret)
{
return "$";
}
/* system name */
static char *expando_sysname(SERVER_REC *server, void *item, int *free_ret)
{
#ifdef HAVE_SYS_UTSNAME_H
struct utsname un;
if (uname(&un) == -1)
return NULL;
*free_ret = TRUE;
return g_strdup(un.sysname);
#else
return NULL;
#endif
}
/* system release */
static char *expando_sysrelease(SERVER_REC *server, void *item, int *free_ret)
{
#ifdef HAVE_SYS_UTSNAME_H
struct utsname un;
if (uname(&un) == -1)
return NULL;
*free_ret = TRUE;
return g_strdup(un.release);
#else
return NULL;
#endif
}
/* Server tag */
static char *expando_servertag(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->tag;
}
/* Server chatnet */
static char *expando_chatnet(SERVER_REC *server, void *item, int *free_ret)
{
return server == NULL ? "" : server->connrec->chatnet;
}
void special_vars_init(void)
{
settings_add_str("misc", "STATUS_OPER", "*");
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);
history_func = NULL;
char_expandos['A'] = expando_awaymsg;
char_expandos['C'] = expando_channel;
char_expandos['F'] = expando_clientstarted;
char_expandos['J'] = expando_version;
char_expandos['K'] = expando_cmdchars;
char_expandos['M'] = expando_chanmode;
char_expandos['N'] = expando_nick;
char_expandos['O'] = expando_statusoper;
char_expandos['P'] = expando_chanop;
char_expandos['Q'] = expando_query;
char_expandos['R'] = expando_serverversion;
char_expandos['T'] = expando_target;
char_expandos['V'] = expando_releasedate;
char_expandos['W'] = expando_workdir;
char_expandos['Y'] = expando_realname;
char_expandos['Z'] = expando_time;
char_expandos['$'] = expando_dollar;
expando_create("sysname", expando_sysname);
expando_create("sysrelease", expando_sysrelease);
expando_create("tag", expando_servertag);
expando_create("chatnet", expando_chatnet);
}
void special_vars_deinit(void)
{
expando_destroy("sysname", expando_sysname);
expando_destroy("sysrelease", expando_sysrelease);
expando_destroy("tag", expando_servertag);
expando_destroy("chatnet", expando_chatnet);
g_hash_table_destroy(expandos);
}

View File

@ -3,8 +3,6 @@
#include "servers.h"
typedef char* (*EXPANDO_FUNC)
(SERVER_REC *server, void *item, int *free_ret);
typedef char* (*SPECIAL_HISTORY_FUNC)
(const char *text, void *item, int *free_ret);
@ -21,14 +19,6 @@ char *parse_special_string(const char *cmd, SERVER_REC *server, void *item,
void eval_special_string(const char *cmd, const char *data,
SERVER_REC *server, void *item);
/* Create expando - overrides any existing ones. */
void expando_create(const char *key, EXPANDO_FUNC func);
/* Destroy expando */
void expando_destroy(const char *key, EXPANDO_FUNC func);
void special_history_func_set(SPECIAL_HISTORY_FUNC func);
void special_vars_init(void);
void special_vars_deinit(void);
#endif

View File

@ -19,9 +19,9 @@ irssi_LDADD = \
irssi_SOURCES = \
gui-entry.c \
gui-expandos.c \
gui-printtext.c \
gui-readline.c \
gui-special-vars.c \
gui-textwidget.c \
gui-windows.c \
mainwindows.c \
@ -36,8 +36,6 @@ noinst_HEADERS = \
gui-entry.h \
gui-printtext.h \
gui-readline.h \
gui-special-vars.h \
gui-textwidget.h \
gui-windows.h \
mainwindows.h \
statusbar.h \

View File

@ -1,5 +1,5 @@
/*
gui-special-vars.c : irssi
gui-expandos.c : irssi
Copyright (C) 2000 Timo Sirainen
@ -19,7 +19,7 @@
*/
#include "module.h"
#include "special-vars.h"
#include "expandos.h"
#include "gui-entry.h"
#include "gui-readline.h"
@ -40,20 +40,20 @@ static char *expando_inputline(SERVER_REC *server, void *item, int *free_ret)
return gui_entry_get_text();
}
/* FIXME: value of cutbuffer */
/* value of cutbuffer */
static char *expando_cutbuffer(SERVER_REC *server, void *item, int *free_ret)
{
return cutbuffer;
}
void gui_special_vars_init(void)
void gui_expandos_init(void)
{
expando_create("E", expando_idletime);
expando_create("L", expando_inputline);
expando_create("U", expando_cutbuffer);
expando_create("E", expando_idletime, NULL);
expando_create("L", expando_inputline, NULL);
expando_create("U", expando_cutbuffer, NULL);
}
void gui_special_vars_deinit(void)
void gui_expandos_deinit(void)
{
expando_destroy("E", expando_idletime);
expando_destroy("L", expando_inputline);

View File

@ -1,7 +0,0 @@
#ifndef __GUI_SPECIAL_VARS_H
#define __GUI_SPECIAL_VARS_H
void gui_special_vars_init(void);
void gui_special_vars_deinit(void);
#endif

View File

@ -1,7 +0,0 @@
#ifndef __GUI_TEXTWIDGET_H
#define __GUI_TEXTWIDGET_H
void gui_textwidget_init(void);
void gui_textwidget_deinit(void);
#endif

View File

@ -35,9 +35,7 @@
#include "mainwindows.h"
#include "gui-printtext.h"
#include "gui-readline.h"
#include "gui-special-vars.h"
#include "statusbar.h"
#include "gui-textwidget.h"
#include "gui-windows.h"
#include <signal.h>
@ -50,6 +48,12 @@ void perl_deinit(void);
void irc_init(void);
void irc_deinit(void);
void gui_expandos_init(void);
void gui_expandos_deinit(void);
void gui_textwidget_init(void);
void gui_textwidget_deinit(void);
void mainwindow_activity_init(void);
void mainwindow_activity_deinit(void);
@ -111,9 +115,9 @@ static void textui_finish_init(void)
screen_refresh_freeze();
gui_entry_init();
gui_expandos_init();
gui_printtext_init();
gui_readline_init();
gui_special_vars_init();
gui_textwidget_init();
mainwindows_init();
mainwindow_activity_init();
@ -146,13 +150,13 @@ static void textui_deinit(void)
signal_remove("gui exit", (SIGNAL_FUNC) sig_exit);
gui_textwidget_deinit();
gui_special_vars_deinit();
statusbar_deinit();
gui_printtext_deinit();
gui_readline_deinit();
gui_windows_deinit();
mainwindow_activity_deinit();
mainwindows_deinit();
gui_expandos_deinit();
gui_entry_deinit();
deinit_screen();

View File

@ -17,6 +17,7 @@ libirc_core_a_SOURCES = \
irc-channels-setup.c \
irc-chatnets.c \
irc-commands.c \
irc-expandos.c \
irc-log.c \
irc-masks.c \
irc-nicklist.c \
@ -25,7 +26,6 @@ libirc_core_a_SOURCES = \
irc-servers.c \
irc-servers-reconnect.c \
irc-servers-setup.c \
irc-special-vars.c \
lag.c \
massjoin.c \
modes.c \

View File

@ -36,8 +36,8 @@ void irc_commands_deinit(void);
void irc_rawlog_init(void);
void irc_rawlog_deinit(void);
void irc_special_vars_init(void);
void irc_special_vars_deinit(void);
void irc_expandos_init(void);
void irc_expandos_deinit(void);
void irc_log_init(void);
void irc_log_deinit(void);
@ -67,14 +67,14 @@ void irc_core_init(void)
lag_init();
netsplit_init();
irc_rawlog_init();
irc_special_vars_init();
irc_expandos_init();
irc_log_init();
}
void irc_core_deinit(void)
{
irc_log_deinit();
irc_special_vars_deinit();
irc_expandos_deinit();
irc_rawlog_deinit();
netsplit_deinit();
lag_deinit();

119
src/irc/core/irc-expandos.c Normal file
View File

@ -0,0 +1,119 @@
/*
irc-expandos.c : irssi
Copyright (C) 2000 Timo Sirainen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "module.h"
#include "misc.h"
#include "expandos.h"
#include "settings.h"
#include "irc.h"
#include "irc-servers.h"
static char *last_join;
/* last person to join a channel you are on */
static char *expando_lastjoin(SERVER_REC *server, void *item, int *free_ret)
{
return last_join;
}
/* current server numeric being processed */
static char *expando_server_numeric(SERVER_REC *server, void *item, int *free_ret)
{
return current_server_event == NULL ||
!is_numeric(current_server_event, 0) ? NULL :
current_server_event;
}
/* current server name */
static char *expando_servername(SERVER_REC *server, void *item, int *free_ret)
{
IRC_SERVER_REC *ircserver = IRC_SERVER(server);
return ircserver == NULL ? "" : ircserver->real_address;
}
/* your /userhost $N address (user@host) */
static char *expando_userhost(SERVER_REC *server, void *item, int *free_ret)
{
IRC_SERVER_REC *ircserver;
const char *username;
char hostname[100];
ircserver = IRC_SERVER(server);
/* prefer the _real_ /userhost reply */
if (ircserver != NULL && ircserver->userhost != NULL)
return ircserver->userhost;
/* haven't received userhost reply yet. guess something */
*free_ret = TRUE;
if (server == NULL)
username = settings_get_str("user_name");
else
username = ircserver->connrec->username;
if (gethostname(hostname, sizeof(hostname)) != 0 || *hostname == '\0')
strcpy(hostname, "??");
return g_strconcat(username, "@", hostname, NULL);;
}
static void event_join(const char *data, IRC_SERVER_REC *server,
const char *nick, const char *address)
{
g_return_if_fail(nick != NULL);
if (g_strcasecmp(nick, server->nick) != 0) {
g_free_not_null(last_join);
last_join = g_strdup(nick);
}
}
void irc_expandos_init(void)
{
last_join = NULL;
expando_create(":", expando_lastjoin,
"event join", EXPANDO_ARG_SERVER2, NULL);
expando_create("H", expando_server_numeric,
"server event", EXPANDO_ARG_SERVER, NULL);
expando_create("S", expando_servername,
"window changed", EXPANDO_ARG_NONE,
"window server changed", EXPANDO_ARG_WINDOW, NULL);
expando_create("X", expando_userhost,
"window changed", EXPANDO_ARG_NONE,
"window server changed", EXPANDO_ARG_WINDOW, NULL);
expando_add_signal("I", "event invite", EXPANDO_ARG_SERVER2);
signal_add("event join", (SIGNAL_FUNC) event_join);
}
void irc_expandos_deinit(void)
{
g_free_not_null(last_join);
expando_destroy(":", expando_lastjoin);
expando_destroy("H", expando_server_numeric);
expando_destroy("S", expando_servername);
expando_destroy("X", expando_userhost);
signal_remove("event join", (SIGNAL_FUNC) event_join);
}

View File

@ -1,208 +0,0 @@
/*
irc-special-vars.c : irssi
Copyright (C) 2000 Timo Sirainen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "module.h"
#include "misc.h"
#include "special-vars.h"
#include "settings.h"
#include "window-item-def.h"
#include "irc.h"
#include "irc-servers.h"
#include "channels.h"
#include "queries.h"
static char *last_privmsg_from;
static char *last_sent_msg, *last_sent_msg_body;
static char *last_join, *last_public_from;
/* last person who sent you a MSG */
static char *expando_lastmsg(SERVER_REC *server, void *item, int *free_ret)
{
return last_privmsg_from;
}
/* last person to whom you sent a MSG */
static char *expando_lastmymsg(SERVER_REC *server, void *item, int *free_ret)
{
return last_sent_msg;
}
/* last person to join a channel you are on */
static char *expando_lastjoin(SERVER_REC *server, void *item, int *free_ret)
{
return last_join;
}
/* last person to send a public message to a channel you are on */
static char *expando_lastpublic(SERVER_REC *server, void *item, int *free_ret)
{
return last_public_from;
}
/* body of last MSG you sent */
static char *expando_lastmymsg_body(SERVER_REC *server, void *item, int *free_ret)
{
return last_sent_msg_body;
}
/* current server numeric being processed */
static char *expando_server_numeric(SERVER_REC *server, void *item, int *free_ret)
{
return current_server_event == NULL ||
!is_numeric(current_server_event, 0) ? NULL :
current_server_event;
}
/* channel you were last INVITEd to */
static char *expando_last_invite(SERVER_REC *server, void *item, int *free_ret)
{
IRC_SERVER_REC *ircserver = IRC_SERVER(server);
return ircserver == NULL ? "" : ircserver->last_invite;
}
/* current server name */
static char *expando_servername(SERVER_REC *server, void *item, int *free_ret)
{
IRC_SERVER_REC *ircserver = IRC_SERVER(server);
return ircserver == NULL ? "" : ircserver->real_address;
}
/* your /userhost $N address (user@host) */
static char *expando_userhost(SERVER_REC *server, void *item, int *free_ret)
{
IRC_SERVER_REC *ircserver;
const char *username;
char hostname[100];
ircserver = IRC_SERVER(server);
/* prefer the _real_ /userhost reply */
if (ircserver != NULL && ircserver->userhost != NULL)
return ircserver->userhost;
/* haven't received userhost reply yet. guess something */
*free_ret = TRUE;
if (server == NULL)
username = settings_get_str("user_name");
else
username = ircserver->connrec->username;
if (gethostname(hostname, sizeof(hostname)) != 0 || *hostname == '\0')
strcpy(hostname, "??");
return g_strconcat(username, "@", hostname, NULL);;
}
static void event_privmsg(const char *data, IRC_SERVER_REC *server,
const char *nick, const char *addr)
{
char *params, *target, *msg;
g_return_if_fail(data != NULL);
params = event_get_params(data, 2 | PARAM_FLAG_GETREST, &target, &msg);
if (!ischannel(*target)) {
g_free_not_null(last_privmsg_from);
last_privmsg_from = g_strdup(nick);
} else {
g_free_not_null(last_public_from);
last_public_from = g_strdup(nick);
}
g_free(params);
}
static void cmd_msg(const char *data, IRC_SERVER_REC *server)
{
char *target, *msg;
void *free_arg;
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&target, &msg))
return;
if (*target != '\0' && *msg != '\0' &&
!ischannel(*target) && isalpha(*target)) {
g_free_not_null(last_sent_msg);
g_free_not_null(last_sent_msg_body);
last_sent_msg = g_strdup(target);
last_sent_msg_body = g_strdup(msg);
}
cmd_params_free(free_arg);
}
static void event_join(const char *data, IRC_SERVER_REC *server,
const char *nick, const char *address)
{
g_return_if_fail(nick != NULL);
if (g_strcasecmp(nick, server->nick) != 0) {
g_free_not_null(last_join);
last_join = g_strdup(nick);
}
}
void irc_special_vars_init(void)
{
last_privmsg_from = NULL;
last_sent_msg = NULL; last_sent_msg_body = NULL;
last_join = NULL; last_public_from = NULL;
expando_create(",", expando_lastmsg);
expando_create(".", expando_lastmymsg);
expando_create(":", expando_lastjoin);
expando_create(";", expando_lastpublic);
expando_create("B", expando_lastmymsg_body);
expando_create("H", expando_server_numeric);
expando_create("I", expando_last_invite);
expando_create("S", expando_servername);
expando_create("X", expando_userhost);
signal_add("event privmsg", (SIGNAL_FUNC) event_privmsg);
signal_add("event join", (SIGNAL_FUNC) event_join);
signal_add("command msg", (SIGNAL_FUNC) cmd_msg);
}
void irc_special_vars_deinit(void)
{
g_free_not_null(last_privmsg_from);
g_free_not_null(last_sent_msg); g_free_not_null(last_sent_msg_body);
g_free_not_null(last_join); g_free_not_null(last_public_from);
expando_destroy(",", expando_lastmsg);
expando_destroy(".", expando_lastmymsg);
expando_destroy(":", expando_lastjoin);
expando_destroy(";", expando_lastpublic);
expando_destroy("B", expando_lastmymsg_body);
expando_destroy("H", expando_server_numeric);
expando_destroy("I", expando_last_invite);
expando_destroy("S", expando_servername);
expando_destroy("X", expando_userhost);
signal_remove("event privmsg", (SIGNAL_FUNC) event_privmsg);
signal_remove("event join", (SIGNAL_FUNC) event_join);
signal_remove("command msg", (SIGNAL_FUNC) cmd_msg);
}

View File

@ -237,7 +237,7 @@ char *event_get_params(const char *data, int count, ...)
return duprec;
}
static void irc_server_event(const char *line, IRC_SERVER_REC *server, const char *nick, const char *address)
static void irc_server_event(IRC_SERVER_REC *server, const char *line, const char *nick, const char *address)
{
char *event, *args, *callcmd;
GSList *list;
@ -329,7 +329,7 @@ static void irc_parse_incoming_line(IRC_SERVER_REC *server, char *line)
line = irc_parse_prefix(line, &nick, &address);
if (*line != '\0')
signal_emit_id(signal_server_event, 4, line, server, nick, address);
signal_emit_id(signal_server_event, 4, server, line, nick, address);
}
/* input function: handle incoming server messages */

View File

@ -20,7 +20,7 @@
#include "module.h"
#include "signals.h"
#include "special-vars.h"
#include "expandos.h"
#include "irc.h"
#include "irc-servers.h"
@ -168,7 +168,8 @@ void notifylist_whois_init(void)
signal_add("notifylist event whois away", (SIGNAL_FUNC) event_whois_away);
signal_add("notifylist event whois idle", (SIGNAL_FUNC) event_whois_idle);
signal_add("notifylist event whois end", (SIGNAL_FUNC) event_whois_end);
expando_create("D", expando_lastnotify);
expando_create("D", expando_lastnotify,
"notifylist event whois", EXPANDO_ARG_SERVER2, NULL);
}
void notifylist_whois_deinit(void)

View File

@ -321,7 +321,7 @@ static void sig_incoming(IRC_SERVER_REC *server, const char *line)
g_string_sprintf(next_line, "%s\n", line);
}
static void sig_server_event(const char *line, IRC_SERVER_REC *server,
static void sig_server_event(IRC_SERVER_REC *server, const char *line,
const char *nick, const char *address)
{
GSList *list;