2000-12-04 19:53:04 -05:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
|
2007-05-08 14:41:10 -04:00
|
|
|
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.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2000-12-04 19:53:04 -05:00
|
|
|
*/
|
|
|
|
|
2006-10-20 08:50:08 -04:00
|
|
|
#include "core.h"
|
2000-12-04 19:53:04 -05:00
|
|
|
#include "module.h"
|
2001-01-01 02:45:54 -05:00
|
|
|
#include "modules.h"
|
2000-12-04 19:53:04 -05:00
|
|
|
#include "signals.h"
|
|
|
|
#include "expandos.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "commands.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "irssi-version.h"
|
|
|
|
|
2001-01-01 02:45:54 -05:00
|
|
|
#include "servers.h"
|
2000-12-04 19:53:04 -05:00
|
|
|
#include "channels.h"
|
|
|
|
#include "queries.h"
|
|
|
|
#include "window-item-def.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UTSNAME_H
|
|
|
|
# include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
|
2000-12-05 20:06:35 -05:00
|
|
|
#define MAX_EXPANDO_SIGNALS 10
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
typedef struct {
|
|
|
|
EXPANDO_FUNC func;
|
2000-12-05 20:06:35 -05:00
|
|
|
|
|
|
|
int signals;
|
|
|
|
int signal_ids[MAX_EXPANDO_SIGNALS];
|
|
|
|
int signal_args[MAX_EXPANDO_SIGNALS];
|
2000-12-04 19:53:04 -05:00
|
|
|
} EXPANDO_REC;
|
|
|
|
|
2002-10-27 17:30:41 -05:00
|
|
|
const char *current_expando = NULL;
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
static int timer_tag;
|
|
|
|
|
2001-08-31 12:47:14 -04:00
|
|
|
static EXPANDO_REC *char_expandos[255];
|
2000-12-04 19:53:04 -05:00
|
|
|
static GHashTable *expandos;
|
|
|
|
static char *last_sent_msg, *last_sent_msg_body;
|
|
|
|
static char *last_privmsg_from, *last_public_from;
|
2001-04-20 16:00:23 -04:00
|
|
|
static char *sysname, *sysrelease, *sysarch;
|
2001-09-23 13:32:05 -04:00
|
|
|
|
2001-01-27 23:03:56 -05:00
|
|
|
static const char *timestamp_format;
|
2001-09-23 13:32:05 -04:00
|
|
|
static int timestamp_seconds;
|
|
|
|
static time_t last_timestamp;
|
2000-12-04 19:53:04 -05:00
|
|
|
|
2001-08-31 12:47:14 -04:00
|
|
|
#define CHAR_EXPANDO(chr) \
|
|
|
|
(char_expandos[(int) (unsigned char) chr])
|
2001-01-15 18:36:11 -05:00
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
/* 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 */
|
2001-08-31 12:47:14 -04:00
|
|
|
rec = CHAR_EXPANDO(*key);
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2000-12-05 20:06:35 -05:00
|
|
|
if (rec != NULL)
|
|
|
|
rec->signals = 0;
|
|
|
|
else {
|
2000-12-04 19:53:04 -05:00
|
|
|
rec = g_new0(EXPANDO_REC, 1);
|
|
|
|
if (key[1] != '\0')
|
|
|
|
g_hash_table_insert(expandos, g_strdup(key), rec);
|
|
|
|
else
|
2001-08-31 12:47:14 -04:00
|
|
|
char_expandos[(int) (unsigned char) *key] = rec;
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
static EXPANDO_REC *expando_find(const char *key)
|
|
|
|
{
|
|
|
|
if (key[1] != '\0')
|
|
|
|
return g_hash_table_lookup(expandos, key);
|
|
|
|
else
|
2001-08-31 12:47:14 -04:00
|
|
|
return CHAR_EXPANDO(*key);
|
2000-12-17 02:39:22 -05:00
|
|
|
}
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
/* Add new signal to expando */
|
|
|
|
void expando_add_signal(const char *key, const char *signal, ExpandoArg arg)
|
|
|
|
{
|
2000-12-05 20:06:35 -05:00
|
|
|
EXPANDO_REC *rec;
|
2000-12-04 19:53:04 -05:00
|
|
|
|
|
|
|
g_return_if_fail(key != NULL);
|
|
|
|
g_return_if_fail(signal != NULL);
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
rec = expando_find(key);
|
2000-12-04 19:53:04 -05:00
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
if (arg == EXPANDO_NEVER) {
|
|
|
|
/* expando changes never */
|
|
|
|
rec->signals = -1;
|
|
|
|
} else if (rec->signals < MAX_EXPANDO_SIGNALS) {
|
|
|
|
g_return_if_fail(rec->signals != -1);
|
|
|
|
|
2000-12-05 20:06:35 -05:00
|
|
|
rec->signal_ids[rec->signals] = signal_get_uniq_id(signal);
|
|
|
|
rec->signal_args[rec->signals] = arg;
|
|
|
|
rec->signals++;
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy expando */
|
|
|
|
void expando_destroy(const char *key, EXPANDO_FUNC func)
|
|
|
|
{
|
2003-11-16 12:57:44 -05:00
|
|
|
gpointer origkey, value;
|
2000-12-04 19:53:04 -05:00
|
|
|
EXPANDO_REC *rec;
|
|
|
|
|
|
|
|
g_return_if_fail(key != NULL || *key == '\0');
|
|
|
|
g_return_if_fail(func != NULL);
|
|
|
|
|
|
|
|
if (key[1] == '\0') {
|
|
|
|
/* single character expando */
|
2001-08-31 12:47:14 -04:00
|
|
|
rec = CHAR_EXPANDO(*key);
|
2000-12-04 19:53:04 -05:00
|
|
|
if (rec != NULL && rec->func == func) {
|
2001-08-31 12:47:14 -04:00
|
|
|
char_expandos[(int) (unsigned char) *key] = NULL;
|
2000-12-05 20:06:35 -05:00
|
|
|
g_free(rec);
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
2003-11-16 12:57:44 -05:00
|
|
|
} else if (g_hash_table_lookup_extended(expandos, key,
|
|
|
|
&origkey, &value)) {
|
|
|
|
rec = value;
|
2000-12-04 19:53:04 -05:00
|
|
|
if (rec->func == func) {
|
|
|
|
g_hash_table_remove(expandos, key);
|
2001-07-25 15:29:50 -04:00
|
|
|
g_free(origkey);
|
2000-12-05 20:06:35 -05:00
|
|
|
g_free(rec);
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
void expando_bind(const char *key, int funccount, SIGNAL_FUNC *funcs)
|
|
|
|
{
|
|
|
|
SIGNAL_FUNC func;
|
|
|
|
EXPANDO_REC *rec;
|
|
|
|
int n, arg;
|
|
|
|
|
|
|
|
g_return_if_fail(key != NULL);
|
|
|
|
g_return_if_fail(funccount >= 1);
|
|
|
|
g_return_if_fail(funcs != NULL);
|
|
|
|
g_return_if_fail(funcs[0] != NULL);
|
|
|
|
|
|
|
|
rec = expando_find(key);
|
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
|
|
|
if (rec->signals == 0) {
|
|
|
|
/* it's unknown when this expando changes..
|
|
|
|
check it once in a second */
|
|
|
|
signal_add("expando timer", funcs[EXPANDO_ARG_NONE]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < rec->signals; n++) {
|
|
|
|
arg = rec->signal_args[n];
|
|
|
|
func = arg < funccount ? funcs[arg] : NULL;
|
|
|
|
if (func == NULL) func = funcs[EXPANDO_ARG_NONE];
|
|
|
|
|
2002-04-16 14:11:06 -04:00
|
|
|
signal_add_full_id(MODULE_NAME, SIGNAL_PRIORITY_DEFAULT,
|
|
|
|
rec->signal_ids[n], func, NULL);
|
2000-12-17 02:39:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void expando_unbind(const char *key, int funccount, SIGNAL_FUNC *funcs)
|
|
|
|
{
|
|
|
|
SIGNAL_FUNC func;
|
|
|
|
EXPANDO_REC *rec;
|
|
|
|
int n, arg;
|
|
|
|
|
|
|
|
g_return_if_fail(key != NULL);
|
|
|
|
g_return_if_fail(funccount >= 1);
|
|
|
|
g_return_if_fail(funcs != NULL);
|
|
|
|
g_return_if_fail(funcs[0] != NULL);
|
|
|
|
|
|
|
|
rec = expando_find(key);
|
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
|
|
|
if (rec->signals == 0) {
|
|
|
|
/* it's unknown when this expando changes..
|
|
|
|
check it once in a second */
|
|
|
|
signal_remove("expando timer", funcs[EXPANDO_ARG_NONE]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < rec->signals; n++) {
|
|
|
|
arg = rec->signal_args[n];
|
|
|
|
func = arg < funccount ? funcs[arg] : NULL;
|
|
|
|
if (func == NULL) func = funcs[EXPANDO_ARG_NONE];
|
|
|
|
|
2002-04-15 09:59:09 -04:00
|
|
|
signal_remove_id(rec->signal_ids[n], func, NULL);
|
2000-12-17 02:39:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-23 13:32:05 -04:00
|
|
|
/* Returns [<signal id>, EXPANDO_ARG_xxx, <signal id>, ..., -1] */
|
|
|
|
int *expando_get_signals(const char *key)
|
|
|
|
{
|
|
|
|
EXPANDO_REC *rec;
|
|
|
|
int *signals;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
g_return_val_if_fail(key != NULL, NULL);
|
|
|
|
|
|
|
|
rec = expando_find(key);
|
|
|
|
if (rec == NULL || rec->signals < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (rec->signals == 0) {
|
|
|
|
/* it's unknown when this expando changes..
|
|
|
|
check it once in a second */
|
|
|
|
signals = g_new(int, 3);
|
|
|
|
signals[0] = signal_get_uniq_id("expando timer");
|
|
|
|
signals[1] = EXPANDO_ARG_NONE;
|
|
|
|
signals[2] = -1;
|
|
|
|
return signals;
|
|
|
|
}
|
|
|
|
|
|
|
|
signals = g_new(int, rec->signals*2+1);
|
|
|
|
for (n = 0; n < rec->signals; n++) {
|
|
|
|
signals[n*2] = rec->signal_ids[n];
|
|
|
|
signals[n*2+1] = rec->signal_args[n];
|
|
|
|
}
|
|
|
|
signals[rec->signals*2] = -1;
|
|
|
|
return signals;
|
|
|
|
}
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
EXPANDO_FUNC expando_find_char(char chr)
|
|
|
|
{
|
2001-08-31 12:47:14 -04:00
|
|
|
return CHAR_EXPANDO(chr) == NULL ? NULL :
|
|
|
|
CHAR_EXPANDO(chr)->func;
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPANDO_FUNC expando_find_long(const char *key)
|
|
|
|
{
|
|
|
|
EXPANDO_REC *rec = g_hash_table_lookup(expandos, key);
|
|
|
|
return rec == NULL ? NULL : rec->func;
|
|
|
|
}
|
|
|
|
|
2006-10-23 06:36:47 -04:00
|
|
|
static gboolean free_expando(gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
|
|
|
g_free(key);
|
|
|
|
g_free(value);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
/* 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)
|
|
|
|
{
|
2007-08-07 10:55:14 -04:00
|
|
|
return PACKAGE_VERSION;
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* current value of CMDCHARS */
|
|
|
|
static char *expando_cmdchars(SERVER_REC *server, void *item, int *free_ret)
|
|
|
|
{
|
|
|
|
return (char *) settings_get_str("cmdchars");
|
|
|
|
}
|
|
|
|
|
2002-10-14 14:24:22 -04:00
|
|
|
/* first CMDCHAR */
|
|
|
|
static char *expando_cmdchar(SERVER_REC *server, void *item, int *free_ret)
|
|
|
|
{
|
|
|
|
char str[2] = { 0, 0 };
|
|
|
|
|
2002-10-14 14:35:59 -04:00
|
|
|
str[0] = *settings_get_str("cmdchars");
|
2002-10-14 14:24:22 -04:00
|
|
|
|
|
|
|
*free_ret = TRUE;
|
|
|
|
return g_strdup(str);
|
|
|
|
}
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
/* modes of current channel, if any */
|
|
|
|
static char *expando_chanmode(SERVER_REC *server, void *item, int *free_ret)
|
2008-02-04 10:30:38 -05:00
|
|
|
{
|
2003-10-31 10:05:52 -05:00
|
|
|
char *cmode;
|
|
|
|
|
|
|
|
*free_ret = FALSE;
|
|
|
|
|
|
|
|
if (!IS_CHANNEL(item))
|
|
|
|
return NULL;
|
|
|
|
|
2003-12-10 17:51:28 -05:00
|
|
|
if (!settings_get_bool("chanmode_expando_strip"))
|
2003-10-31 10:05:52 -05:00
|
|
|
return CHANNEL(item)->mode;
|
|
|
|
|
|
|
|
*free_ret = TRUE;
|
|
|
|
cmode = g_strdup(CHANNEL(item)->mode);
|
|
|
|
if (strchr(cmode, ' ') != NULL)
|
|
|
|
*(strchr(cmode, ' ')) = 0;
|
|
|
|
|
|
|
|
return cmode;
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2002-05-15 20:34:37 -04:00
|
|
|
return item == NULL ? "" :
|
|
|
|
(char *) window_item_get_target((WI_ITEM_REC *) item);
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2001-12-15 18:26:09 -05:00
|
|
|
/* client release date (in YYYYMMDD format) */
|
2000-12-04 19:53:04 -05:00
|
|
|
static char *expando_releasedate(SERVER_REC *server, void *item, int *free_ret)
|
|
|
|
{
|
2001-12-15 14:28:44 -05:00
|
|
|
*free_ret = TRUE;
|
|
|
|
return g_strdup_printf("%d", IRSSI_VERSION_DATE);
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2001-12-15 18:26:09 -05:00
|
|
|
/* client release time (in HHMM format) */
|
|
|
|
static char *expando_releasetime(SERVER_REC *server, void *item, int *free_ret)
|
|
|
|
{
|
|
|
|
*free_ret = TRUE;
|
2001-12-15 20:41:29 -05:00
|
|
|
return g_strdup_printf("%04d", IRSSI_VERSION_TIME);
|
2001-12-15 18:26:09 -05:00
|
|
|
}
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
/* 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)
|
|
|
|
{
|
2001-01-27 23:03:56 -05:00
|
|
|
time_t now;
|
2000-12-04 19:53:04 -05:00
|
|
|
struct tm *tm;
|
2001-01-27 23:03:56 -05:00
|
|
|
char str[256];
|
2000-12-04 19:53:04 -05:00
|
|
|
|
2001-01-27 23:03:56 -05:00
|
|
|
now = time(NULL);
|
2000-12-04 19:53:04 -05:00
|
|
|
tm = localtime(&now);
|
2001-01-27 23:03:56 -05:00
|
|
|
|
|
|
|
if (strftime(str, sizeof(str), timestamp_format, tm) == 0)
|
|
|
|
return "";
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
*free_ret = TRUE;
|
2001-01-27 23:03:56 -05:00
|
|
|
return g_strdup(str);
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2000-12-05 20:06:35 -05:00
|
|
|
return sysname;
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* system release */
|
|
|
|
static char *expando_sysrelease(SERVER_REC *server, void *item, int *free_ret)
|
|
|
|
{
|
2000-12-05 20:06:35 -05:00
|
|
|
return sysrelease;
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2001-04-20 16:00:23 -04:00
|
|
|
/* system architecture */
|
|
|
|
static char *expando_sysarch(SERVER_REC *server, void *item, int *free_ret)
|
|
|
|
{
|
|
|
|
return sysarch;
|
|
|
|
}
|
|
|
|
|
2000-12-10 00:41:01 -05:00
|
|
|
/* Topic of active channel (or address of queried nick) */
|
|
|
|
static char *expando_topic(SERVER_REC *server, void *item, int *free_ret)
|
|
|
|
{
|
2002-03-22 17:30:45 -05:00
|
|
|
if (IS_CHANNEL(item))
|
|
|
|
return CHANNEL(item)->topic;
|
|
|
|
if (IS_QUERY(item)) {
|
|
|
|
QUERY_REC *query = QUERY(item);
|
|
|
|
|
2002-03-23 12:42:48 -05:00
|
|
|
if (query->server_tag == NULL)
|
|
|
|
return "";
|
|
|
|
|
2002-03-22 17:30:45 -05:00
|
|
|
*free_ret = TRUE;
|
2002-03-23 12:42:48 -05:00
|
|
|
return query->address == NULL ?
|
|
|
|
g_strdup_printf("(%s)", query->server_tag) :
|
|
|
|
g_strdup_printf("%s (%s)", query->address,
|
|
|
|
query->server_tag);
|
2002-03-22 17:30:45 -05:00
|
|
|
}
|
|
|
|
return "";
|
2000-12-10 00:41:01 -05:00
|
|
|
}
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2002-05-15 20:34:37 -04:00
|
|
|
/* visible_name of current window item */
|
|
|
|
static char *expando_itemname(SERVER_REC *server, void *item, int *free_ret)
|
|
|
|
{
|
|
|
|
return item == NULL ? "" : ((WI_ITEM_REC *) item)->visible_name;
|
|
|
|
}
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2001-01-01 11:57:25 -05:00
|
|
|
static void sig_message_private(SERVER_REC *server, const char *msg,
|
|
|
|
const char *nick, const char *address)
|
2000-12-04 19:53:04 -05:00
|
|
|
{
|
2001-01-01 11:57:25 -05:00
|
|
|
g_free_not_null(last_privmsg_from);
|
|
|
|
last_privmsg_from = g_strdup(nick);
|
|
|
|
}
|
2000-12-04 19:53:04 -05:00
|
|
|
|
2001-01-01 11:57:25 -05:00
|
|
|
static void sig_message_own_private(SERVER_REC *server, const char *msg,
|
|
|
|
const char *target, const char *origtarget)
|
|
|
|
{
|
|
|
|
g_return_if_fail(server != NULL);
|
|
|
|
g_return_if_fail(msg != NULL);
|
2000-12-04 19:53:04 -05:00
|
|
|
|
2001-01-01 11:57:25 -05:00
|
|
|
if (target != NULL) {
|
|
|
|
if (target != last_sent_msg) {
|
|
|
|
g_free_not_null(last_sent_msg);
|
|
|
|
last_sent_msg = g_strdup(target);
|
|
|
|
}
|
2000-12-04 19:53:04 -05:00
|
|
|
g_free_not_null(last_sent_msg_body);
|
|
|
|
last_sent_msg_body = g_strdup(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
static int sig_timer(void)
|
|
|
|
{
|
2001-09-23 13:32:05 -04:00
|
|
|
time_t now;
|
|
|
|
struct tm *tm;
|
|
|
|
int last_min;
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
signal_emit("expando timer", 0);
|
2001-09-23 13:32:05 -04:00
|
|
|
|
|
|
|
/* check if $Z has changed */
|
|
|
|
now = time(NULL);
|
|
|
|
if (last_timestamp != now) {
|
2001-11-12 09:17:23 -05:00
|
|
|
if (!timestamp_seconds && last_timestamp != 0) {
|
2001-09-23 13:32:05 -04:00
|
|
|
/* assume it changes every minute */
|
|
|
|
tm = localtime(&last_timestamp);
|
|
|
|
last_min = tm->tm_min;
|
|
|
|
|
|
|
|
tm = localtime(&now);
|
|
|
|
if (tm->tm_min == last_min)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
signal_emit("time changed", 0);
|
|
|
|
last_timestamp = now;
|
|
|
|
}
|
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-01-27 23:03:56 -05:00
|
|
|
static void read_settings(void)
|
|
|
|
{
|
2001-09-23 13:32:05 -04:00
|
|
|
timestamp_format = settings_get_str("timestamp_format");
|
|
|
|
timestamp_seconds =
|
|
|
|
strstr(timestamp_format, "%r") != NULL ||
|
|
|
|
strstr(timestamp_format, "%s") != NULL ||
|
|
|
|
strstr(timestamp_format, "%S") != NULL ||
|
2001-11-12 17:03:07 -05:00
|
|
|
strstr(timestamp_format, "%X") != NULL ||
|
2001-09-23 13:32:05 -04:00
|
|
|
strstr(timestamp_format, "%T") != NULL;
|
|
|
|
|
2001-01-27 23:03:56 -05:00
|
|
|
}
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
void expandos_init(void)
|
|
|
|
{
|
2000-12-05 20:06:35 -05:00
|
|
|
#ifdef HAVE_SYS_UTSNAME_H
|
|
|
|
struct utsname un;
|
|
|
|
#endif
|
2000-12-04 19:53:04 -05:00
|
|
|
settings_add_str("misc", "STATUS_OPER", "*");
|
2002-05-11 18:53:16 -04:00
|
|
|
settings_add_str("lookandfeel", "timestamp_format", "%H:%M");
|
2003-12-10 17:51:28 -05:00
|
|
|
settings_add_bool("lookandfeel", "chanmode_expando_strip", FALSE);
|
2000-12-04 19:53:04 -05:00
|
|
|
|
|
|
|
last_sent_msg = NULL; last_sent_msg_body = NULL;
|
|
|
|
last_privmsg_from = NULL; last_public_from = NULL;
|
2001-09-23 13:32:05 -04:00
|
|
|
last_timestamp = 0;
|
2000-12-04 19:53:04 -05:00
|
|
|
|
2001-04-20 16:00:23 -04:00
|
|
|
sysname = sysrelease = sysarch = NULL;
|
2000-12-05 20:06:35 -05:00
|
|
|
#ifdef HAVE_SYS_UTSNAME_H
|
2001-07-13 15:17:43 -04:00
|
|
|
if (uname(&un) >= 0) {
|
2000-12-05 20:06:35 -05:00
|
|
|
sysname = g_strdup(un.sysname);
|
|
|
|
sysrelease = g_strdup(un.release);
|
2001-04-20 16:00:23 -04:00
|
|
|
sysarch = g_strdup(un.machine);
|
2000-12-05 20:06:35 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-12-04 19:53:04 -05:00
|
|
|
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);
|
2002-10-14 14:24:22 -04:00
|
|
|
expando_create("k", expando_cmdchar,
|
|
|
|
"setup changed", EXPANDO_ARG_NONE, NULL);
|
2000-12-04 19:53:04 -05:00
|
|
|
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,
|
2002-10-14 07:36:49 -04:00
|
|
|
"window connect changed", EXPANDO_ARG_WINDOW,
|
2000-12-04 19:53:04 -05:00
|
|
|
"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);
|
2001-12-15 18:26:09 -05:00
|
|
|
expando_create("versiontime", expando_releasetime,
|
|
|
|
"", EXPANDO_NEVER, NULL);
|
2000-12-04 19:53:04 -05:00
|
|
|
expando_create("W", expando_workdir, NULL);
|
|
|
|
expando_create("Y", expando_realname,
|
|
|
|
"window changed", EXPANDO_ARG_NONE,
|
2002-10-14 07:36:49 -04:00
|
|
|
"window connect changed", EXPANDO_ARG_WINDOW,
|
2000-12-04 19:53:04 -05:00
|
|
|
"window server changed", EXPANDO_ARG_WINDOW, NULL);
|
2001-09-23 13:32:05 -04:00
|
|
|
expando_create("Z", expando_time,
|
|
|
|
"time changed", EXPANDO_ARG_NONE, NULL);
|
2000-12-04 19:53:04 -05:00
|
|
|
expando_create("$", expando_dollar,
|
|
|
|
"", EXPANDO_NEVER, NULL);
|
|
|
|
|
|
|
|
expando_create("sysname", expando_sysname,
|
|
|
|
"", EXPANDO_NEVER, NULL);
|
|
|
|
expando_create("sysrelease", expando_sysrelease,
|
|
|
|
"", EXPANDO_NEVER, NULL);
|
2001-04-20 16:00:23 -04:00
|
|
|
expando_create("sysarch", expando_sysarch,
|
|
|
|
"", EXPANDO_NEVER, NULL);
|
2000-12-10 00:41:01 -05:00
|
|
|
expando_create("topic", expando_topic,
|
|
|
|
"window changed", EXPANDO_ARG_NONE,
|
|
|
|
"window item changed", EXPANDO_ARG_WINDOW,
|
|
|
|
"channel topic changed", EXPANDO_ARG_WINDOW_ITEM,
|
|
|
|
"query address changed", EXPANDO_ARG_WINDOW_ITEM, NULL);
|
2000-12-04 19:53:04 -05:00
|
|
|
expando_create("tag", expando_servertag,
|
|
|
|
"window changed", EXPANDO_ARG_NONE,
|
2002-10-14 07:36:49 -04:00
|
|
|
"window connect changed", EXPANDO_ARG_WINDOW,
|
2000-12-04 19:53:04 -05:00
|
|
|
"window server changed", EXPANDO_ARG_WINDOW, NULL);
|
|
|
|
expando_create("chatnet", expando_chatnet,
|
|
|
|
"window changed", EXPANDO_ARG_NONE,
|
2002-10-14 07:36:49 -04:00
|
|
|
"window connect changed", EXPANDO_ARG_WINDOW,
|
2000-12-04 19:53:04 -05:00
|
|
|
"window server changed", EXPANDO_ARG_WINDOW, NULL);
|
2002-05-15 20:34:37 -04:00
|
|
|
expando_create("itemname", expando_itemname,
|
|
|
|
"window changed", EXPANDO_ARG_NONE,
|
2002-05-17 11:22:05 -04:00
|
|
|
"window item changed", EXPANDO_ARG_WINDOW,
|
|
|
|
"window item name changed", EXPANDO_ARG_WINDOW_ITEM,
|
|
|
|
NULL);
|
2000-12-04 19:53:04 -05:00
|
|
|
|
2001-01-27 23:03:56 -05:00
|
|
|
read_settings();
|
|
|
|
|
2001-10-21 07:06:58 -04:00
|
|
|
timer_tag = g_timeout_add(500, (GSourceFunc) sig_timer, NULL);
|
2000-12-04 19:53:04 -05:00
|
|
|
signal_add("message public", (SIGNAL_FUNC) sig_message_public);
|
|
|
|
signal_add("message private", (SIGNAL_FUNC) sig_message_private);
|
2001-01-01 11:57:25 -05:00
|
|
|
signal_add("message own_private", (SIGNAL_FUNC) sig_message_own_private);
|
2001-03-14 22:01:04 -05:00
|
|
|
signal_add_first("setup changed", (SIGNAL_FUNC) read_settings);
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void expandos_deinit(void)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2001-08-31 12:47:14 -04:00
|
|
|
for (n = 0; n < sizeof(char_expandos)/sizeof(char_expandos[0]); n++)
|
2000-12-05 20:06:35 -05:00
|
|
|
g_free_not_null(char_expandos[n]);
|
|
|
|
|
2006-10-23 06:36:47 -04:00
|
|
|
g_hash_table_foreach_remove(expandos, free_expando, NULL);
|
2000-12-04 19:53:04 -05:00
|
|
|
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);
|
2000-12-05 20:06:35 -05:00
|
|
|
g_free_not_null(sysname); g_free_not_null(sysrelease);
|
2001-04-20 16:00:23 -04:00
|
|
|
g_free_not_null(sysarch);
|
2000-12-04 19:53:04 -05:00
|
|
|
|
2000-12-17 02:39:22 -05:00
|
|
|
g_source_remove(timer_tag);
|
2000-12-04 19:53:04 -05:00
|
|
|
signal_remove("message public", (SIGNAL_FUNC) sig_message_public);
|
|
|
|
signal_remove("message private", (SIGNAL_FUNC) sig_message_private);
|
2001-01-01 11:57:25 -05:00
|
|
|
signal_remove("message own_private", (SIGNAL_FUNC) sig_message_own_private);
|
2001-01-27 23:03:56 -05:00
|
|
|
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
|
2000-12-04 19:53:04 -05:00
|
|
|
}
|