summaryrefslogtreecommitdiff
path: root/src/perl/common/Expando.xs
blob: 5bf27bec54a59c926c0a51085440bb69e33ff1ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "module.h"
#include "expandos.h"

typedef struct {
	PERL_SCRIPT_REC *script;
	SV *func;
} PerlExpando;

static GHashTable *perl_expando_defs;

static char *sig_perl_expando(SERVER_REC *server, void *item, int *free_ret);

static int check_expando_destroy(char *key, PerlExpando *rec,
				 PERL_SCRIPT_REC *script)
{
	if (rec->script == script) {
                expando_destroy(key, sig_perl_expando);
		SvREFCNT_dec(rec->func);
		g_free(key);
		g_free(rec);
		return TRUE;
	}

        return FALSE;
}

static void script_unregister_expandos(PERL_SCRIPT_REC *script)
{
	g_hash_table_foreach_remove(perl_expando_defs,
				    (GHRFunc) check_expando_destroy, script);
}

void perl_expando_init(void)
{
	perl_expando_defs = g_hash_table_new((GHashFunc) g_str_hash,
					     (GCompareFunc) g_str_equal);
	signal_add("script destroyed", (SIGNAL_FUNC) script_unregister_expandos);
}

static void expando_def_destroy(char *key, PerlExpando *rec)
{
	SvREFCNT_dec(rec->func);
	g_free(key);
	g_free(rec);
}

void perl_expando_deinit(void)
{
	signal_remove("script destroyed", (SIGNAL_FUNC) script_unregister_expandos);

	g_hash_table_foreach(perl_expando_defs,
			     (GHFunc) expando_def_destroy, NULL);
	g_hash_table_destroy(perl_expando_defs);
}

static char *perl_expando_event(PerlExpando *rec, SERVER_REC *server,
				WI_ITEM_REC *item, int *free_ret)
{
	dSP;
	char *ret;
	int retcount;

	ENTER;
	SAVETMPS;

	PUSHMARK(SP);
	XPUSHs(sv_2mortal(iobject_bless(server)));
	XPUSHs(sv_2mortal(iobject_bless(item)));
	PUTBACK;

	retcount = perl_call_sv(rec->func, G_EVAL|G_SCALAR);
	SPAGAIN;

	ret = NULL;
	if (SvTRUE(ERRSV)) {
		/* make sure we don't get back here */
		if (rec->script != NULL)
			script_unregister_expandos(rec->script);

		signal_emit("script error", 2, rec->script, SvPV(ERRSV, PL_na));
	} else if (retcount > 0) {
		ret = g_strdup(POPp);
		*free_ret = TRUE;
	}

	PUTBACK;
	FREETMPS;
	LEAVE;

	return ret;
}

static char *sig_perl_expando(SERVER_REC *server, void *item, int *free_ret)
{
        PerlExpando *rec;

	rec = g_hash_table_lookup(perl_expando_defs, current_expando);
	if (rec != NULL)
		return perl_expando_event(rec, server, item, free_ret);
	return NULL;
}

void expando_signals_add_hash(const char *key, SV *signals)
{
	HV *hv;
        HE *he;
	I32 len;
	const char *argstr;
	ExpandoArg arg;

	if (!is_hvref(signals)) {
		croak("Usage: Irssi::expando_create(key, func, hash)");
		return;
	}

        hv = hvref(signals);
	hv_iterinit(hv);
	while ((he = hv_iternext(hv)) != NULL) {
		SV *argsv = HeVAL(he);
		argstr = SvPV(argsv, PL_na);

		if (g_ascii_strcasecmp(argstr, "none") == 0)
			arg = EXPANDO_ARG_NONE;
		else if (g_ascii_strcasecmp(argstr, "server") == 0)
			arg = EXPANDO_ARG_SERVER;
		else if (g_ascii_strcasecmp(argstr, "window") == 0)
			arg = EXPANDO_ARG_WINDOW;
		else if (g_ascii_strcasecmp(argstr, "windowitem") == 0)
			arg = EXPANDO_ARG_WINDOW_ITEM;
		else if (g_ascii_strcasecmp(argstr, "never") == 0)
			arg = EXPANDO_NEVER;
		else {
			croak("Unknown signal type: %s", argstr);
			break;
		}
		expando_add_signal(key, hv_iterkey(he, &len), arg);
	}
}

MODULE = Irssi::Expando  PACKAGE = Irssi
PROTOTYPES: ENABLE

void
expando_create(key, func, signals)
	char *key
	SV *func
	SV *signals
PREINIT:
        PerlExpando *rec;
CODE:
	rec = g_new0(PerlExpando, 1);
	rec->script = perl_script_find_package(perl_get_package());
        rec->func = perl_func_sv_inc(func, perl_get_package());

	expando_create(key, sig_perl_expando, NULL);
	g_hash_table_insert(perl_expando_defs, g_strdup(key), rec);
        expando_signals_add_hash(key, signals);

void
expando_destroy(name)
	char *name
PREINIT:
        gpointer key, value;
CODE:
	if (g_hash_table_lookup_extended(perl_expando_defs, name, &key, &value)) {
                g_hash_table_remove(perl_expando_defs, name);
		g_free(key);
		SvREFCNT_dec((SV *) value);
	}
	expando_destroy(name, sig_perl_expando);