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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*
fe-dcc-chat.c : irssi
Copyright (C) 1999-2002 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
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "module.h"
#include "signals.h"
#include "commands.h"
#include "levels.h"
#include "settings.h"
#include "irc.h"
#include "irc-servers.h"
#include "irc-queries.h"
#include "dcc-chat.h"
#include "module-formats.h"
#include "printtext.h"
#include "fe-messages.h"
#include "chat-completion.h"
void fe_dcc_chat_messages_init(void);
void fe_dcc_chat_messages_deinit(void);
static int autocreate_dccquery;
static void dcc_request(CHAT_DCC_REC *dcc)
{
if (!IS_DCC_CHAT(dcc)) return;
printformat(dcc->server, NULL, MSGLEVEL_DCC,
ischannel(*dcc->target) ? IRCTXT_DCC_CHAT_CHANNEL :
IRCTXT_DCC_CHAT, dcc->id, dcc->addrstr,
dcc->port, dcc->target);
}
static void dcc_connected(CHAT_DCC_REC *dcc)
{
char *sender;
if (!IS_DCC_CHAT(dcc)) return;
sender = g_strconcat("=", dcc->id, NULL);
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_CHAT_CONNECTED,
dcc->id, dcc->addrstr, dcc->port);
if (query_find(NULL, sender) == NULL) {
if (!autocreate_dccquery)
completion_last_message_add(sender);
else
irc_query_create(dcc->servertag, sender, TRUE);
}
g_free(sender);
}
static void dcc_closed(CHAT_DCC_REC *dcc)
{
char *sender;
if (!IS_DCC_CHAT(dcc)) return;
sender = g_strconcat("=", dcc->id, NULL);
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_CHAT_DISCONNECTED, dcc->id);
g_free(sender);
}
static void dcc_chat_msg(CHAT_DCC_REC *dcc, const char *msg)
{
QUERY_REC *query;
char *sender, *freemsg;
g_return_if_fail(IS_DCC_CHAT(dcc));
g_return_if_fail(msg != NULL);
sender = g_strconcat("=", dcc->id, NULL);
query = query_find(NULL, sender);
if (settings_get_bool("emphasis"))
msg = freemsg = expand_emphasis((WI_ITEM_REC *) query, msg);
else
freemsg = NULL;
if (query == NULL)
completion_last_message_add(sender);
signal_emit("message dcc", 2, dcc, msg);
g_free_not_null(freemsg);
g_free(sender);
}
static void dcc_chat_action(CHAT_DCC_REC *dcc, const char *msg)
{
char *sender;
g_return_if_fail(IS_DCC_CHAT(dcc));
g_return_if_fail(msg != NULL);
sender = g_strconcat("=", dcc->id, NULL);
if (query_find(NULL, sender) == NULL)
completion_last_message_add(sender);
signal_emit("message dcc action", 2, dcc, msg);
g_free(sender);
}
static void dcc_chat_ctcp(CHAT_DCC_REC *dcc, const char *cmd, const char *data)
{
g_return_if_fail(IS_DCC_CHAT(dcc));
signal_emit("message dcc ctcp", 3, dcc, cmd, data);
}
static void dcc_error_ctcp(const char *type, const char *data,
const char *nick, const char *addr,
const char *target)
{
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_INVALID_CTCP, type, nick, addr, target);
}
static void dcc_unknown_ctcp(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *addr,
const char *target, CHAT_DCC_REC *chat)
{
char *type, *args;
void *free_arg;
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&type, &args))
return;
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_UNKNOWN_CTCP,
type, nick, args);
cmd_params_free(free_arg);
}
static void dcc_unknown_reply(IRC_SERVER_REC *server, const char *data,
const char *nick)
{
char *type, *args;
void *free_arg;
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&type, &args))
return;
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_UNKNOWN_REPLY,
type, nick, args);
cmd_params_free(free_arg);
}
static void sig_dcc_destroyed(CHAT_DCC_REC *dcc)
{
QUERY_REC *query;
char *nick;
if (!IS_DCC_CHAT(dcc)) return;
nick = g_strconcat("=", dcc->id, NULL);
query = query_find(NULL, nick);
if (query != NULL) {
/* DCC chat closed, close the query with it. */
if (dcc->connection_lost) query->unwanted = TRUE;
query_destroy(query);
} else {
/* remove nick from msg completion
since it won't work anymore */
completion_last_message_remove(nick);
}
g_free(nick);
}
static void sig_query_destroyed(QUERY_REC *query)
{
CHAT_DCC_REC *dcc;
if (*query->name != '=')
return;
dcc = dcc_chat_find_id(query->name+1);
if (dcc != NULL && !dcc->destroyed) {
/* DCC query window closed, close the dcc chat too. */
dcc_close(DCC(dcc));
}
}
static void dcc_error_close_not_found(const char *type, const char *nick,
const char *fname)
{
g_return_if_fail(type != NULL);
g_return_if_fail(nick != NULL);
if (g_strcasecmp(type, "CHAT") != 0) return;
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_CHAT_NOT_FOUND, nick);
}
static void sig_dcc_list_print(CHAT_DCC_REC *dcc)
{
if (!IS_DCC_CHAT(dcc)) return;
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_LIST_LINE_CHAT,
dcc->id, "CHAT");
}
static void cmd_msg(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
{
CHAT_DCC_REC *dcc;
GHashTable *optlist;
char *text, *target;
void *free_arg;
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_UNKNOWN_OPTIONS |
PARAM_FLAG_OPTIONS | PARAM_FLAG_GETREST, "msg",
&optlist, &target, &text))
return;
/* handle only DCC messages */
if (strcmp(target, "*") == 0)
dcc = item_get_dcc(item);
else if (*target == '=')
dcc = dcc_chat_find_id(target+1);
else
dcc = NULL;
if (dcc == NULL && *target == '=') {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
IRCTXT_DCC_CHAT_NOT_FOUND, target+1);
} else if (dcc != NULL) {
if (query_find(NULL, target) == NULL)
completion_last_message_add(target);
signal_emit("message dcc own", 2, dcc, text);
}
cmd_params_free(free_arg);
}
static void cmd_me(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
{
CHAT_DCC_REC *dcc;
dcc = item_get_dcc(item);
if (dcc != NULL)
signal_emit("message dcc own_action", 2, dcc, data);
}
static void cmd_action(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
{
CHAT_DCC_REC *dcc;
char *target, *text;
void *free_arg;
g_return_if_fail(data != NULL);
if (*data != '=') {
/* handle only DCC actions */
return;
}
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&target, &text))
return;
if (*target == '\0' || *text == '\0')
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
dcc = dcc_chat_find_id(target+1);
if (dcc == NULL || dcc->sendbuf == NULL) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
IRCTXT_DCC_CHAT_NOT_FOUND, target+1);
} else {
if (query_find(NULL, target) == NULL)
completion_last_message_add(target);
signal_emit("message dcc own_action", 2, dcc, text);
}
cmd_params_free(free_arg);
}
static void cmd_ctcp(const char *data, SERVER_REC *server)
{
CHAT_DCC_REC *dcc;
char *target, *ctcpcmd, *ctcpdata;
void *free_arg;
g_return_if_fail(data != NULL);
if (server == NULL || !server->connected)
cmd_return_error(CMDERR_NOT_CONNECTED);
if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST,
&target, &ctcpcmd, &ctcpdata))
return;
if (*target == '\0' || *ctcpcmd == '\0')
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
if (*target != '=') {
/* handle only DCC CTCPs */
cmd_params_free(free_arg);
return;
}
dcc = dcc_chat_find_id(target+1);
if (dcc == NULL || dcc->sendbuf == NULL) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
IRCTXT_DCC_CHAT_NOT_FOUND, target+1);
} else {
g_strup(ctcpcmd);
signal_emit("message dcc own_ctcp", 3, dcc, ctcpcmd, ctcpdata);
}
cmd_params_free(free_arg);
}
static void read_settings(void)
{
int level;
level = settings_get_level("autocreate_query_level");
autocreate_dccquery = (level & MSGLEVEL_DCCMSGS) != 0;
}
void fe_dcc_chat_init(void)
{
read_settings();
fe_dcc_chat_messages_init();
signal_add("dcc request", (SIGNAL_FUNC) dcc_request);
signal_add("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_add("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_add("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg);
signal_add("dcc ctcp action", (SIGNAL_FUNC) dcc_chat_action);
signal_add("default dcc ctcp", (SIGNAL_FUNC) dcc_chat_ctcp);
signal_add("dcc error ctcp", (SIGNAL_FUNC) dcc_error_ctcp);
signal_add("default ctcp msg dcc", (SIGNAL_FUNC) dcc_unknown_ctcp);
signal_add("default ctcp reply dcc", (SIGNAL_FUNC) dcc_unknown_reply);
signal_add("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_add("query destroyed", (SIGNAL_FUNC) sig_query_destroyed);
signal_add("dcc list print", (SIGNAL_FUNC) sig_dcc_list_print);
command_bind("msg", NULL, (SIGNAL_FUNC) cmd_msg);
command_bind("me", NULL, (SIGNAL_FUNC) cmd_me);
command_bind("action", NULL, (SIGNAL_FUNC) cmd_action);
command_bind("ctcp", NULL, (SIGNAL_FUNC) cmd_ctcp);
signal_add("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
}
void fe_dcc_chat_deinit(void)
{
fe_dcc_chat_messages_deinit();
signal_remove("dcc request", (SIGNAL_FUNC) dcc_request);
signal_remove("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_remove("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_remove("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg);
signal_remove("dcc ctcp action", (SIGNAL_FUNC) dcc_chat_action);
signal_remove("default dcc ctcp", (SIGNAL_FUNC) dcc_chat_ctcp);
signal_remove("dcc error ctcp", (SIGNAL_FUNC) dcc_error_ctcp);
signal_remove("default ctcp msg dcc", (SIGNAL_FUNC) dcc_unknown_ctcp);
signal_remove("default ctcp reply dcc", (SIGNAL_FUNC) dcc_unknown_reply);
signal_remove("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_remove("query destroyed", (SIGNAL_FUNC) sig_query_destroyed);
signal_remove("dcc list print", (SIGNAL_FUNC) sig_dcc_list_print);
command_unbind("msg", (SIGNAL_FUNC) cmd_msg);
command_unbind("me", (SIGNAL_FUNC) cmd_me);
command_unbind("action", (SIGNAL_FUNC) cmd_action);
command_unbind("ctcp", (SIGNAL_FUNC) cmd_ctcp);
signal_remove("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
}
|