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
|
/*
server-idle.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 "irc-servers.h"
#include "servers-idle.h"
#include "servers-redirect.h"
typedef struct {
char *cmd;
char *arg;
int tag;
char *redirect_cmd;
int count;
int remote;
char *failure_signal;
GSList *redirects;
} SERVER_IDLE_REC;
static int idle_tag, idlepos;
/* Add new idle command to queue */
static SERVER_IDLE_REC *
server_idle_create(const char *cmd, const char *redirect_cmd, int count,
const char *arg, int remote, const char *failure_signal,
va_list va)
{
SERVER_IDLE_REC *rec;
const char *event, *signal;
g_return_val_if_fail(cmd != NULL, FALSE);
rec = g_new0(SERVER_IDLE_REC, 1);
rec->cmd = g_strdup(cmd);
rec->arg = g_strdup(arg);
rec->tag = ++idlepos;
rec->redirect_cmd = g_strdup(redirect_cmd);
rec->count = count;
rec->remote = remote;
rec->failure_signal = g_strdup(failure_signal);
while ((event = va_arg(va, const char *)) != NULL) {
signal = va_arg(va, const char *);
if (signal == NULL) {
g_warning("server_idle_create(%s): "
"signal not specified for event",
redirect_cmd);
break;
}
rec->redirects =
g_slist_append(rec->redirects, g_strdup(event));
rec->redirects =
g_slist_append(rec->redirects, g_strdup(signal));
}
return rec;
}
static SERVER_IDLE_REC *server_idle_find_rec(IRC_SERVER_REC *server, int tag)
{
GSList *tmp;
g_return_val_if_fail(server != NULL, FALSE);
for (tmp = server->idles; tmp != NULL; tmp = tmp->next) {
SERVER_IDLE_REC *rec = tmp->data;
if (rec->tag == tag)
return rec;
}
return NULL;
}
/* Add new idle command to queue */
int server_idle_add_redir(IRC_SERVER_REC *server, const char *cmd,
const char *redirect_cmd, int count, const char *arg,
int remote, const char *failure_signal, ...)
{
va_list va;
SERVER_IDLE_REC *rec;
g_return_val_if_fail(server != NULL, -1);
va_start(va, failure_signal);
rec = server_idle_create(cmd, redirect_cmd, count, arg, remote,
failure_signal, va);
server->idles = g_slist_append(server->idles, rec);
va_end(va);
return rec->tag;
}
/* Add new idle command to first of queue */
int server_idle_add_first_redir(IRC_SERVER_REC *server, const char *cmd,
const char *redirect_cmd, int count,
const char *arg, int remote,
const char *failure_signal, ...)
{
va_list va;
SERVER_IDLE_REC *rec;
g_return_val_if_fail(server != NULL, -1);
va_start(va, failure_signal);
rec = server_idle_create(cmd, redirect_cmd, count, arg, remote,
failure_signal, va);
server->idles = g_slist_prepend(server->idles, rec);
va_end(va);
return rec->tag;
}
/* Add new idle command to specified position of queue */
int server_idle_insert_redir(IRC_SERVER_REC *server, const char *cmd, int tag,
const char *redirect_cmd, int count,
const char *arg, int remote,
const char *failure_signal, ...)
{
va_list va;
SERVER_IDLE_REC *rec;
int pos;
g_return_val_if_fail(server != NULL, -1);
va_start(va, failure_signal);
/* find the position of tag in idle list */
rec = server_idle_find_rec(server, tag);
pos = g_slist_index(server->idles, rec);
rec = server_idle_create(cmd, redirect_cmd, count, arg, remote,
failure_signal, va);
server->idles = pos < 0 ?
g_slist_append(server->idles, rec) :
g_slist_insert(server->idles, rec, pos);
va_end(va);
return rec->tag;
}
static void server_idle_destroy(IRC_SERVER_REC *server, SERVER_IDLE_REC *rec)
{
g_return_if_fail(server != NULL);
server->idles = g_slist_remove(server->idles, rec);
g_slist_foreach(rec->redirects, (GFunc) g_free, NULL);
g_slist_free(rec->redirects);
g_free_not_null(rec->arg);
g_free_not_null(rec->redirect_cmd);
g_free_not_null(rec->failure_signal);
g_free(rec->cmd);
g_free(rec);
}
/* Check if record is still in queue */
int server_idle_find(IRC_SERVER_REC *server, int tag)
{
return server_idle_find_rec(server, tag) != NULL;
}
/* Remove record from idle queue */
int server_idle_remove(IRC_SERVER_REC *server, int tag)
{
SERVER_IDLE_REC *rec;
g_return_val_if_fail(server != NULL, FALSE);
rec = server_idle_find_rec(server, tag);
if (rec == NULL)
return FALSE;
server_idle_destroy(server, rec);
return TRUE;
}
/* Execute next idle command */
static void server_idle_next(IRC_SERVER_REC *server)
{
SERVER_IDLE_REC *rec;
g_return_if_fail(server != NULL);
if (server->idles == NULL)
return;
rec = server->idles->data;
/* Send command */
if (rec->redirect_cmd != NULL) {
server_redirect_event_list(server, rec->redirect_cmd,
rec->count, rec->arg,
rec->remote, rec->failure_signal,
rec->redirects);
}
irc_send_cmd(server, rec->cmd);
server_idle_destroy(server, rec);
}
static void sig_disconnected(IRC_SERVER_REC *server)
{
g_return_if_fail(server != NULL);
if (!IS_IRC_SERVER(server))
return;
while (server->idles != NULL)
server_idle_destroy(server, server->idles->data);
}
static int sig_idle_timeout(void)
{
GSList *tmp;
/* Scan through every server */
for (tmp = servers; tmp != NULL; tmp = tmp->next) {
IRC_SERVER_REC *rec = tmp->data;
if (IS_IRC_SERVER(rec) &&
rec->idles != NULL && rec->cmdcount == 0) {
/* We're idling and we have idle commands to run! */
server_idle_next(rec);
}
}
return 1;
}
void servers_idle_init(void)
{
idlepos = 0;
idle_tag = g_timeout_add(1000, (GSourceFunc) sig_idle_timeout, NULL);
signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
}
void servers_idle_deinit(void)
{
g_source_remove(idle_tag);
signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
}
|