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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
server-redirect.c : irssi
Copyright (C) 1999-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 "misc.h"
#include "server.h"
#include "server-redirect.h"
static int redirect_group;
static void server_eventtable_destroy(char *key, GSList *value)
{
GSList *tmp;
g_free(key);
for (tmp = value; tmp != NULL; tmp = tmp->next) {
REDIRECT_REC *rec = tmp->data;
g_free_not_null(rec->arg);
g_free(rec->name);
g_free(rec);
}
g_slist_free(value);
}
static void server_eventgrouptable_destroy(gpointer key, GSList *value)
{
g_slist_foreach(value, (GFunc) g_free, NULL);
g_slist_free(value);
}
static void server_cmdtable_destroy(char *key, REDIRECT_CMD_REC *value)
{
g_free(key);
g_slist_foreach(value->events, (GFunc) g_free, NULL);
g_slist_free(value->events);
g_free(value);
}
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_destroy(server->eventtable);
}
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_destroy(server->cmdtable);
}
}
void server_redirect_initv(SERVER_REC *server, const char *command,
int last, GSList *list)
{
REDIRECT_CMD_REC *rec;
g_return_if_fail(server != NULL);
g_return_if_fail(command != NULL);
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. */
g_slist_foreach(list, (GFunc) g_free, NULL);
g_slist_free(list);
return;
}
rec = g_new(REDIRECT_CMD_REC, 1);
rec->last = last;
rec->events = list;
g_hash_table_insert(server->cmdtable, g_strdup(command), rec);
}
void server_redirect_init(SERVER_REC *server, const char *command,
int last, ...)
{
va_list args;
GSList *list;
char *event;
va_start(args, last);
list = NULL;
while ((event = va_arg(args, gchar *)) != NULL)
list = g_slist_append(list, g_strdup(event));
va_end(args);
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)
{
REDIRECT_REC *rec;
GSList *list, *grouplist;
char *origkey;
g_return_val_if_fail(server != NULL, 0);
g_return_val_if_fail(event != NULL, 0);
g_return_val_if_fail(signal != NULL, 0);
g_return_val_if_fail(arg != NULL || argpos == -1, 0);
if (group == 0) group = ++redirect_group;
rec = g_new0(REDIRECT_REC, 1);
rec->arg = arg == NULL ? NULL : g_strdup(arg);
rec->argpos = argpos;
rec->name = g_strdup(signal);
rec->group = group;
rec->last = last;
if (g_hash_table_lookup_extended(server->eventtable, event,
(gpointer *) &origkey,
(gpointer *) &list)) {
g_hash_table_remove(server->eventtable, origkey);
} 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));
}
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);
return group;
}
void server_redirect_event(SERVER_REC *server, const char *arg, int last, ...)
{
va_list args;
char *event, *signal;
int argpos, group;
g_return_if_fail(server != NULL);
va_start(args, last);
group = 0;
while ((event = va_arg(args, gchar *)) != NULL) {
signal = va_arg(args, gchar *);
argpos = va_arg(args, gint);
group = server_redirect_single_event(server, arg, last > 0,
group, event, signal,
argpos);
last--;
}
va_end(args);
}
void server_redirect_default(SERVER_REC *server, const char *command)
{
REDIRECT_CMD_REC *cmdrec;
REDIRECT_REC *rec;
GSList *events, *list, *grouplist;
char *event, *origkey;
int last;
g_return_if_fail(server != NULL);
g_return_if_fail(command != NULL);
if (server->cmdtable == NULL)
return; /* not connected yet */
cmdrec = g_hash_table_lookup(server->cmdtable, command);
if (cmdrec == NULL) return;
/* 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) {
event = events->data;
if (g_hash_table_lookup_extended(server->eventtable, event,
(gpointer *) &origkey,
(gpointer *) &list)) {
g_hash_table_remove(server->eventtable, origkey);
} else {
list = NULL;
origkey = g_strdup(event);
}
rec = g_new0(REDIRECT_REC, 1);
rec->argpos = -1;
rec->name = g_strdup(event);
rec->group = redirect_group;
rec->last = last > 0;
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);
}
void server_redirect_remove_next(SERVER_REC *server, const char *event,
GSList *item)
{
REDIRECT_REC *rec;
GSList *grouplist, *list, *events, *tmp;
char *origkey;
int group;
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))
return;
rec = item == NULL ? list->data : item->data;
if (!rec->last) {
/* this wasn't last expected event */
return;
}
group = rec->group;
/* get list of events from this 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");
continue;
}
/* remove the right group */
for (tmp = events; tmp != NULL; tmp = tmp->next) {
rec = tmp->data;
if (rec->group == group)
break;
}
if (rec == NULL) {
g_warning("server_redirect_remove_next() : "
"event in eventgrouptable but not in "
"eventtable (group)");
continue;
}
g_free(event);
events = g_slist_remove(events, rec);
g_free_not_null(rec->arg);
g_free(rec->name);
g_free(rec);
/* update hash table */
g_hash_table_remove(server->eventtable, origkey);
if (events == NULL)
g_free(origkey);
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)
{
REDIRECT_REC *rec;
GSList *list;
char **arglist;
int found;
list = g_hash_table_lookup(server->eventtable, event);
for (; list != NULL; list = list->next) {
rec = list->data;
if (rec->argpos == -1)
break;
if (rec->arg == NULL)
continue;
/* we need to check that the argument is right.. */
arglist = g_strsplit(args, " ", -1);
found = (strarray_length(arglist) > rec->argpos &&
find_substr(rec->arg, arglist[rec->argpos]));
g_strfreev(arglist);
if (found) break;
}
return list;
}
void servers_redirect_init(void)
{
redirect_group = 0;
signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
}
void servers_redirect_deinit(void)
{
signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
}
|