summaryrefslogtreecommitdiff
path: root/src/irc/dcc/dcc-files.c
blob: b5e1a490eb786bfe74b1270a9d3fc6fd311f1fd5 (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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
 dcc-files.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 "commands.h"
#include "network.h"
#include "net-sendbuffer.h"
#include "line-split.h"
#include "misc.h"
#include "settings.h"

#include "masks.h"
#include "irc.h"
#include "servers-setup.h"

#include "dcc.h"

static int dcc_file_create_mode;

static char *dcc_get_download_path(const char *fname)
{
	char *str, *downpath;

	downpath = convert_home(settings_get_str("dcc_download_path"));
	str = g_strconcat(downpath, G_DIR_SEPARATOR_S, g_basename(fname), NULL);
	g_free(downpath);

	return str;
}

static void sig_dccget_send(DCC_REC *dcc);

void dcc_get_send_received(DCC_REC *dcc)
{
	guint32 recd;

	recd = (guint32) htonl(dcc->transfd);
	memcpy(dcc->count_buf, &recd, 4);

	dcc->count_pos = net_transmit(dcc->handle, dcc->count_buf+dcc->count_pos, 4-dcc->count_pos);
	if (dcc->count_pos == 4) dcc->count_pos = 0;

	/* count_pos might be -1 here. if this happens, the
	   count_buf should be re-sent.. also, if it's 1, 2 or 3, the
	   last 1-3 bytes should be sent later. these happen probably
	   never, but I just want to do it right.. :) */
	if (dcc->tagwrite == -1) {
		dcc->tagwrite = g_input_add(dcc->handle, G_INPUT_WRITE,
					    (GInputFunction) sig_dccget_send, dcc);
	}
}

/* input function: DCC GET is free to send data */
static void sig_dccget_send(DCC_REC *dcc)
{
	guint32 recd;
	int ret;

	if (dcc->count_pos != 0) {
		ret = net_transmit(dcc->handle, dcc->count_buf+dcc->count_pos, 4-dcc->count_pos);
		if (dcc->count_pos <= 0)
			dcc->count_pos = ret;
		else if (ret > 0)
			dcc->count_pos += ret;

		if (dcc->count_pos == 4) dcc->count_pos = 0;

	}

	if (dcc->count_pos == 0) {
		g_source_remove(dcc->tagwrite);
                dcc->tagwrite = -1;
	}

	memcpy(&recd, dcc->count_buf, 4);
	if (recd != (guint32) htonl(dcc->transfd))
                dcc_get_send_received(dcc);
}

/* input function: DCC GET received data */
static void sig_dccget_receive(DCC_REC *dcc)
{
	int ret;

	g_return_if_fail(dcc != NULL);

	for (;;) {
		ret = net_receive(dcc->handle, dcc->databuf, dcc->databufsize);
		if (ret == 0) break;

		if (ret < 0) {
			/* socket closed - transmit complete,
			   or other side died.. */
			signal_emit("dcc closed", 1, dcc);
			dcc_destroy(dcc);
			return;
		}

		write(dcc->fhandle, dcc->databuf, ret);
		dcc->transfd += ret;
	}

	/* send number of total bytes received */
	if (dcc->count_pos <= 0)
		dcc_get_send_received(dcc);

	signal_emit("dcc transfer update", 1, dcc);
}

static char *get_rename_file(const char *fname)
{
	GString *newname;
	struct stat statbuf;
	char *ret;
	int num;

	newname = g_string_new(NULL);
	num = 1;
	do {
		g_string_sprintf(newname, "%s.%d", fname, num);
		num++;
	} while (stat(newname->str, &statbuf) == 0);

	ret = newname->str;
	g_string_free(newname, FALSE);
	return ret;
}

/* callback: net_connect() finished for DCC GET */
static void sig_dccget_connected(DCC_REC *dcc)
{
	struct stat statbuf;
	char *fname;

	g_return_if_fail(dcc != NULL);

	if (net_geterror(dcc->handle) != 0) {
		/* error connecting */
		signal_emit("dcc error connect", 1, dcc);
		dcc_destroy(dcc);
		return;
	}

	g_source_remove(dcc->tagconn);

	g_free_not_null(dcc->file);
	dcc->file = dcc_get_download_path(dcc->arg);

	/* if some plugin wants to change the file name/path here.. */
	signal_emit("dcc get receive", 1, dcc);

	if (stat(dcc->file, &statbuf) == 0 && dcc->get_type == DCC_GET_RENAME) {
		/* file exists, rename.. */
		fname = get_rename_file(dcc->file);
		g_free(dcc->file);
		dcc->file = fname;
	}

	if (dcc->get_type != DCC_GET_RESUME) {
		dcc->fhandle = open(dcc->file, O_WRONLY | O_TRUNC | O_CREAT, dcc_file_create_mode);
		if (dcc->fhandle == -1) {
			signal_emit("dcc error file create", 2, dcc, dcc->file);
			dcc_destroy(dcc);
			return;
		}
	}

	dcc->databufsize = settings_get_int("dcc_block_size");
        if (dcc->databufsize <= 0) dcc->databufsize = 2048;
	dcc->databuf = g_malloc(dcc->databufsize);

	dcc->starttime = time(NULL);
	dcc->tagread = g_input_add(dcc->handle, G_INPUT_READ,
				   (GInputFunction) sig_dccget_receive, dcc);
	signal_emit("dcc connected", 1, dcc);
}

static void dcc_get_connect(DCC_REC *dcc)
{
	dcc->handle = net_connect_ip(&dcc->addr, dcc->port,
				     source_host_ok ? source_host_ip : NULL);
	if (dcc->handle != -1) {
		dcc->tagconn = g_input_add(dcc->handle,
					   G_INPUT_WRITE | G_INPUT_READ,
					   (GInputFunction) sig_dccget_connected, dcc);
	} else {
		/* error connecting */
		signal_emit("dcc error connect", 1, dcc);
		dcc_destroy(dcc);
	}
}

#define dcc_is_unget(dcc) \
        ((dcc)->type == DCC_TYPE_GET && (dcc)->handle == -1)

/* SYNTAX: DCC GET <nick> [<file>] */
static void cmd_dcc_get(const char *data)
{
	DCC_REC *dcc;
	GSList *tmp, *next;
	char *nick, *fname;
	void *free_arg;
	int found;

	g_return_if_fail(data != NULL);

	if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &nick, &fname))
		return;
	if (*nick == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);

	dcc = NULL; found = FALSE;
	for (tmp = dcc_conns; tmp != NULL; tmp = next) {
		dcc = tmp->data;
		next = tmp->next;

		if (dcc_is_unget(dcc) && g_strcasecmp(dcc->nick, nick) == 0 &&
		    (*fname == '\0' || strcmp(dcc->arg, fname) == 0)) {
			found = TRUE;
			dcc_get_connect(dcc);
		}
	}

	if (!found)
		signal_emit("dcc error get not found", 1, nick);

	cmd_params_free(free_arg);
}

static void dcc_resume_send(DCC_REC *dcc, int port)
{
	char *str;

	g_return_if_fail(dcc != NULL);
	g_return_if_fail(dcc->type == DCC_TYPE_SEND);

	str = g_strdup_printf("DCC ACCEPT %s %d %lu",
			      dcc->arg, port, dcc->transfd);
	dcc_ctcp_message(dcc->nick, dcc->server, dcc->chat, FALSE, str);
	g_free(str);
}

#define is_resume_type(type) \
	(g_strcasecmp(type, "RESUME") == 0 || \
	g_strcasecmp(type, "ACCEPT") == 0)

#define is_resume_ok(type, dcc) \
	(g_strcasecmp(type, "RESUME") != 0 || \
	((dcc)->type == DCC_TYPE_SEND && (dcc)->transfd == 0))

#define is_accept_ok(type, dcc) \
	(g_strcasecmp(type, "ACCEPT") != 0 || \
	((dcc)->type == DCC_TYPE_GET && \
	(dcc)->get_type == DCC_GET_RESUME && (dcc)->handle == -1))

static void dcc_ctcp_msg(const char *data, IRC_SERVER_REC *server,
			 const char *sender, const char *sendaddr,
			 const char *target, DCC_REC *chat)
{
	char *type, *arg, *portstr, *sizestr;
	void *free_arg;
	long size;
        int port;
	DCC_REC *dcc;

	g_return_if_fail(data != NULL);
	g_return_if_fail(sender != NULL);

	if (!cmd_get_params(data, &free_arg, 4 | PARAM_FLAG_NOQUOTES,
			    &type, &arg, &portstr, &sizestr))
		return;

	port = atoi(portstr);
	size = atol(sizestr);

	dcc = dcc_find_by_port(sender, port);
	if (dcc == NULL || !is_resume_type(type) ||
	    !is_resume_ok(type, dcc) || !is_accept_ok(type, dcc)) {
		cmd_params_free(free_arg);
		return;
	}

	if (lseek(dcc->fhandle, size, SEEK_SET) != size) {
		/* error, or trying to seek after end of file */
		signal_emit("dcc closed", 1, dcc);
		dcc_destroy(dcc);
	} else {
		dcc->transfd = dcc->skipped = size;

		if (dcc->type == DCC_TYPE_SEND)
			dcc_resume_send(dcc, port);
		else
			dcc_get_connect(dcc);
	}

	cmd_params_free(free_arg);
}

static void dcc_resume_rec(DCC_REC *dcc)
{
	char *str;

	g_return_if_fail(dcc != NULL);

	dcc->get_type = DCC_GET_RESUME;
	dcc->file = dcc_get_download_path(dcc->arg);

	dcc->fhandle = open(dcc->file, O_WRONLY, dcc_file_create_mode);
	if (dcc->fhandle == -1) {
		signal_emit("dcc error file not found", 2, dcc, dcc->file);
		dcc_destroy(dcc);
		return;
	}

	dcc->transfd = lseek(dcc->fhandle, 0, SEEK_END);
	if (dcc->transfd < 0) dcc->transfd = 0;
	dcc->skipped = dcc->transfd;

	str = g_strdup_printf("DCC RESUME %s %d %lu",
			      dcc->arg, dcc->port, dcc->transfd);
	dcc_ctcp_message(dcc->nick, dcc->server, dcc->chat, FALSE, str);
	g_free(str);
}

/* SYNTAX: DCC RESUME <nick> [<file>] */
static void cmd_dcc_resume(const char *data)
{
	DCC_REC *dcc;
	GSList *tmp;
	char *nick, *fname;
	void *free_arg;
	int found;

	g_return_if_fail(data != NULL);

	if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &nick, &fname))
		return;
	if (*nick == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);

	dcc = NULL; found = FALSE;
	for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
		dcc = tmp->data;

		if (dcc_is_unget(dcc) && g_strcasecmp(dcc->nick, nick) == 0 &&
		    (*fname == '\0' || strcmp(dcc->arg, fname) == 0)) {
			dcc_resume_rec(dcc);
			found = TRUE;
		}
	}

	if (!found)
		signal_emit("dcc error get not found", 1, nick);

	cmd_params_free(free_arg);
}

/* input function: DCC SEND - we're ready to send more data */
static void dcc_send_data(DCC_REC *dcc)
{
	int ret;

	g_return_if_fail(dcc != NULL);

	if (!dcc->fastsend && !dcc->gotalldata) {
		/* haven't received everything we've send there yet.. */
		return;
	}

	ret = read(dcc->fhandle, dcc->databuf, dcc->databufsize);
	if (ret <= 0) {
		/* end of file .. or some error .. */
		if (dcc->fastsend) {
			/* no need to call this function anymore..
			   in fact it just eats all the cpu.. */
			dcc->waitforend = TRUE;
			g_source_remove(dcc->tagwrite);
			dcc->tagwrite = -1;
		} else {
			signal_emit("dcc closed", 1, dcc);
			dcc_destroy(dcc);
		}
		return;
	}

	ret = net_transmit(dcc->handle, dcc->databuf, ret);
	if (ret > 0) dcc->transfd += ret;
	dcc->gotalldata = FALSE;

	lseek(dcc->fhandle, dcc->transfd, SEEK_SET);

	signal_emit("dcc transfer update", 1, dcc);
}

/* input function: DCC SEND - received some data */
static void dcc_send_read_size(DCC_REC *dcc)
{
	guint32 bytes;
	int ret;

	g_return_if_fail(dcc != NULL);

	if (dcc->count_pos == 4)
		return;

	/* we need to get 4 bytes.. */
	ret = net_receive(dcc->handle, dcc->count_buf+dcc->count_pos, 4-dcc->count_pos);
	if (ret == -1) {
		signal_emit("dcc closed", 1, dcc);
		dcc_destroy(dcc);
		return;
	}

	dcc->count_pos += ret;

	if (dcc->count_pos != 4)
		return;

	memcpy(&bytes, dcc->count_buf, 4);
	bytes = (guint32) ntohl(bytes);

	dcc->gotalldata = (long) bytes == dcc->transfd;
	dcc->count_pos = 0;

	if (!dcc->fastsend) {
		/* send more data.. */
		dcc_send_data(dcc);
	}

	if (dcc->waitforend && dcc->gotalldata) {
		/* file is sent */
		signal_emit("dcc closed", 1, dcc);
		dcc_destroy(dcc);
	}
}

/* input function: DCC SEND - someone tried to connect to our socket */
static void dcc_send_init(DCC_REC *dcc)
{
	int handle, port;
	IPADDR addr;

	g_return_if_fail(dcc != NULL);

	/* accept connection */
	handle = net_accept(dcc->handle, &addr, &port);
	if (handle == -1)
		return;

	/* TODO: some kind of paranoia check would be nice. it would check
	   that the host of the nick who we sent the request matches the
	   address who connected us. */

	g_source_remove(dcc->tagconn);
	close(dcc->handle);

	dcc->starttime = time(NULL);
	dcc->fastsend = settings_get_bool("dcc_fast_send");
	dcc->handle = handle;
	memcpy(&dcc->addr, &addr, sizeof(IPADDR));
	net_ip2host(&dcc->addr, dcc->addrstr);
	dcc->port = port;

	dcc->databufsize = settings_get_int("dcc_block_size");
        if (dcc->databufsize <= 0) dcc->databufsize = 2048;
	dcc->databuf = g_malloc(dcc->databufsize);

	dcc->tagread = g_input_add(handle, G_INPUT_READ,
				   (GInputFunction) dcc_send_read_size, dcc);
	dcc->tagwrite = !dcc->fastsend ? -1 :
		g_input_add(handle, G_INPUT_WRITE, (GInputFunction) dcc_send_data, dcc);

	signal_emit("dcc connected", 1, dcc);

	if (!dcc->fastsend) {
		/* send first block */
		dcc->gotalldata = TRUE;
		dcc_send_data(dcc);
	}
}

/* SYNTAX: DCC SEND <nick> <file> */
static void cmd_dcc_send(const char *data, IRC_SERVER_REC *server, void *item)
{
	char *target, *fname, *str, *ptr;
	void *free_arg;
	char host[MAX_IP_LEN];
	int hfile, hlisten, port;
	long fsize;
	DCC_REC *dcc, *chat;
	IPADDR own_ip;

	g_return_if_fail(data != NULL);

	if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &target, &fname))
		return;
	if (*target == '\0' || *fname == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);

	/* if we're in dcc chat, send the request via it. */
	chat = item_get_dcc(item);

	if (chat != NULL && (chat->mirc_ctcp || g_strcasecmp(target, chat->nick) != 0))
		chat = NULL;

	if ((server == NULL || !server->connected) && chat == NULL)
		cmd_param_error(CMDERR_NOT_CONNECTED);

	if (dcc_find_item(DCC_TYPE_SEND, target, fname)) {
		signal_emit("dcc error send exists", 2, target, fname);
		cmd_params_free(free_arg);
		return;
	}

	str = convert_home(fname);
	if (!g_path_is_absolute(str)) {
		char *path;

		g_free(str);
		path = convert_home(settings_get_str("dcc_upload_path"));
		str = g_strconcat(path, G_DIR_SEPARATOR_S, fname, NULL);
		g_free(path);
	}

	hfile = open(str, O_RDONLY);
	g_free(str);

	if (hfile == -1) {
		signal_emit("dcc error file not found", 2, target, fname);
		cmd_params_free(free_arg);
		return;
	}
	fsize = lseek(hfile, 0, SEEK_END);
	lseek(hfile, 0, SEEK_SET);

	/* get the IP address we use with IRC server */
	if (net_getsockname(chat != NULL ? chat->handle : net_sendbuffer_handle(server->handle), &own_ip, NULL) == -1) {
		close(hfile);
		cmd_param_error(CMDERR_ERRNO);
	}

	/* start listening */
	port = settings_get_int("dcc_port");
	hlisten = net_listen(&own_ip, &port);
	if (hlisten == -1) {
		close(hfile);
		cmd_param_error(CMDERR_ERRNO);
	}

	/* skip path, change all spaces to _ */
	fname = g_strdup(g_basename(fname));
	for (ptr = fname; *ptr != '\0'; ptr++)
		if (*ptr == ' ') *ptr = '_';

	dcc = dcc_create(DCC_TYPE_SEND, hlisten, target, fname, server, chat);
	dcc->port = port;
	dcc->size = fsize;
	dcc->fhandle = hfile;
	dcc->tagconn = g_input_add(hlisten, G_INPUT_READ,
				   (GInputFunction) dcc_send_init, dcc);

	/* send DCC request */
	dcc_make_address(&own_ip, host);
	str = g_strdup_printf("DCC SEND %s %s %d %lu",
			      fname, host, port, fsize);
	dcc_ctcp_message(target, server, chat, FALSE, str);
	g_free(str);

	g_free(fname);
	cmd_params_free(free_arg);
}

static void read_settings(void)
{
	dcc_file_create_mode = octal2dec(settings_get_int("dcc_file_create_mode"));
}

void dcc_files_init(void)
{
	signal_add("ctcp msg dcc", (SIGNAL_FUNC) dcc_ctcp_msg);
	signal_add("setup changed", (SIGNAL_FUNC) read_settings);
	signal_add("irssi init finished", (SIGNAL_FUNC) read_settings);
	command_bind("dcc send", NULL, (SIGNAL_FUNC) cmd_dcc_send);
	command_bind("dcc get", NULL, (SIGNAL_FUNC) cmd_dcc_get);
	command_bind("dcc resume", NULL, (SIGNAL_FUNC) cmd_dcc_resume);
}

void dcc_files_deinit(void)
{
	signal_remove("ctcp msg dcc", (SIGNAL_FUNC) dcc_ctcp_msg);
	signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
	signal_remove("irssi init finished", (SIGNAL_FUNC) read_settings);
	command_unbind("dcc send", (SIGNAL_FUNC) cmd_dcc_send);
	command_unbind("dcc get", (SIGNAL_FUNC) cmd_dcc_get);
	command_unbind("dcc resume", (SIGNAL_FUNC) cmd_dcc_resume);
}