27 |
#include <stdlib.h> |
#include <stdlib.h> |
28 |
#include <unistd.h> |
#include <unistd.h> |
29 |
#include <string.h> |
#include <string.h> |
30 |
|
#include <stdarg.h> |
31 |
#include <fcntl.h> |
#include <fcntl.h> |
32 |
#include <sys/stat.h> |
#include <sys/stat.h> |
33 |
#include <sys/types.h> |
#include <sys/types.h> |
71 |
{ |
{ |
72 |
struct sockaddr_in addr; |
struct sockaddr_in addr; |
73 |
|
|
74 |
ptr_dcc->status = DCC_CONNECTING; |
if (ptr_dcc->type == DCC_CHAT_SEND) |
75 |
|
ptr_dcc->status = DCC_WAITING; |
76 |
|
else |
77 |
|
ptr_dcc->status = DCC_CONNECTING; |
78 |
|
|
79 |
if (ptr_dcc->sock == -1) |
if (ptr_dcc->sock == -1) |
80 |
{ |
{ |
85 |
if (fcntl (ptr_dcc->sock, F_SETFL, O_NONBLOCK) == -1) |
if (fcntl (ptr_dcc->sock, F_SETFL, O_NONBLOCK) == -1) |
86 |
return 0; |
return 0; |
87 |
|
|
88 |
/* for DCC SEND, listen to socket for a connection */ |
/* for sending (chat or file), listen to socket for a connection */ |
89 |
if (ptr_dcc->type == DCC_FILE_SEND) |
if (DCC_IS_SEND(ptr_dcc->type)) |
90 |
{ |
{ |
91 |
if (listen (ptr_dcc->sock, 1) == -1) |
if (listen (ptr_dcc->sock, 1) == -1) |
92 |
return 0; |
return 0; |
94 |
return 0; |
return 0; |
95 |
} |
} |
96 |
|
|
97 |
/* for DCC RECV, connect to listening host */ |
/* for receiving (chat or file), connect to listening host */ |
98 |
if (ptr_dcc->type == DCC_FILE_RECV) |
if (DCC_IS_RECV(ptr_dcc->type)) |
99 |
{ |
{ |
100 |
memset (&addr, 0, sizeof (addr)); |
memset (&addr, 0, sizeof (addr)); |
101 |
addr.sin_port = htons (ptr_dcc->port); |
addr.sin_port = htons (ptr_dcc->port); |
129 |
|
|
130 |
if (ptr_dcc->nick) |
if (ptr_dcc->nick) |
131 |
free (ptr_dcc->nick); |
free (ptr_dcc->nick); |
132 |
|
if (ptr_dcc->unterminated_message) |
133 |
|
free (ptr_dcc->unterminated_message); |
134 |
if (ptr_dcc->filename) |
if (ptr_dcc->filename) |
135 |
free (ptr_dcc->filename); |
free (ptr_dcc->filename); |
136 |
|
|
149 |
|
|
150 |
if (status == DCC_DONE) |
if (status == DCC_DONE) |
151 |
{ |
{ |
152 |
if ((ptr_dcc->type == DCC_FILE_SEND) || (ptr_dcc->type == DCC_FILE_RECV)) |
if (DCC_IS_FILE(ptr_dcc->type)) |
153 |
{ |
{ |
154 |
irc_display_prefix (ptr_dcc->server->buffer, PREFIX_INFO); |
irc_display_prefix (ptr_dcc->server->buffer, PREFIX_INFO); |
155 |
gui_printf (ptr_dcc->server->buffer, _("DCC: file ")); |
gui_printf (ptr_dcc->server->buffer, _("DCC: file ")); |
173 |
gui_printf (ptr_dcc->server->buffer, _(": ok!\n")); |
gui_printf (ptr_dcc->server->buffer, _(": ok!\n")); |
174 |
} |
} |
175 |
} |
} |
176 |
|
if (status == DCC_ABORTED) |
177 |
|
{ |
178 |
|
if (DCC_IS_CHAT(ptr_dcc->type)) |
179 |
|
{ |
180 |
|
irc_display_prefix (ptr_dcc->channel->buffer, PREFIX_INFO); |
181 |
|
gui_printf (ptr_dcc->channel->buffer, _("DCC chat closed with ")); |
182 |
|
gui_printf_color (ptr_dcc->channel->buffer, COLOR_WIN_CHAT_NICK, |
183 |
|
"%s", ptr_dcc->nick); |
184 |
|
gui_printf_color (ptr_dcc->channel->buffer, COLOR_WIN_CHAT_DARK, " ("); |
185 |
|
gui_printf_color (ptr_dcc->channel->buffer, COLOR_WIN_CHAT_HOST, |
186 |
|
"%d.%d.%d.%d", |
187 |
|
ptr_dcc->addr >> 24, (ptr_dcc->addr >> 16) & 0xff, |
188 |
|
(ptr_dcc->addr >> 8) & 0xff, ptr_dcc->addr & 0xff); |
189 |
|
gui_printf_color (ptr_dcc->channel->buffer, COLOR_WIN_CHAT_DARK, ")\n"); |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
|
if (DCC_IS_CHAT(ptr_dcc->type)) |
194 |
|
channel_remove_dcc (ptr_dcc); |
195 |
|
|
196 |
if (ptr_dcc->sock != -1) |
if (ptr_dcc->sock != -1) |
197 |
{ |
{ |
206 |
} |
} |
207 |
|
|
208 |
/* |
/* |
209 |
|
* dcc_channel_for_chat: create channel for DCC chat |
210 |
|
*/ |
211 |
|
|
212 |
|
void |
213 |
|
dcc_channel_for_chat (t_irc_dcc *ptr_dcc) |
214 |
|
{ |
215 |
|
if (!channel_create_dcc (ptr_dcc)) |
216 |
|
{ |
217 |
|
irc_display_prefix (ptr_dcc->server->buffer, PREFIX_ERROR); |
218 |
|
gui_printf (ptr_dcc->server->buffer, |
219 |
|
_("%s can't associate DCC chat with private buffer " |
220 |
|
"(maybe private buffer has already DCC CHAT?)\n"), |
221 |
|
WEECHAT_ERROR); |
222 |
|
dcc_close (ptr_dcc, DCC_FAILED); |
223 |
|
dcc_redraw (1); |
224 |
|
return; |
225 |
|
} |
226 |
|
|
227 |
|
irc_display_prefix (ptr_dcc->channel->buffer, PREFIX_INFO); |
228 |
|
gui_printf_type (ptr_dcc->channel->buffer, MSG_TYPE_MSG, |
229 |
|
_("Connected to ")); |
230 |
|
gui_printf_color (ptr_dcc->channel->buffer, COLOR_WIN_CHAT_NICK, |
231 |
|
"%s", ptr_dcc->nick); |
232 |
|
gui_printf_color (ptr_dcc->channel->buffer, COLOR_WIN_CHAT_DARK, " ("); |
233 |
|
gui_printf_color (ptr_dcc->channel->buffer, COLOR_WIN_CHAT_HOST, |
234 |
|
"%d.%d.%d.%d", |
235 |
|
ptr_dcc->addr >> 24, (ptr_dcc->addr >> 16) & 0xff, |
236 |
|
(ptr_dcc->addr >> 8) & 0xff, ptr_dcc->addr & 0xff); |
237 |
|
gui_printf_color (ptr_dcc->channel->buffer, COLOR_WIN_CHAT_DARK, ") "); |
238 |
|
gui_printf (ptr_dcc->channel->buffer, _("via DCC chat\n")); |
239 |
|
} |
240 |
|
|
241 |
|
/* |
242 |
* dcc_accept: accepts a DCC file or chat request |
* dcc_accept: accepts a DCC file or chat request |
243 |
*/ |
*/ |
244 |
|
|
255 |
else |
else |
256 |
{ |
{ |
257 |
ptr_dcc->status = DCC_ACTIVE; |
ptr_dcc->status = DCC_ACTIVE; |
|
ptr_home = getenv ("HOME"); |
|
|
ptr_dcc->local_filename = (char *) malloc (strlen (cfg_dcc_download_path) + |
|
|
strlen (ptr_dcc->nick) + |
|
|
strlen (ptr_dcc->filename) + |
|
|
((cfg_dcc_download_path[0] == '~') ? |
|
|
strlen (ptr_home) : 0) + |
|
|
4); |
|
|
if (!ptr_dcc->local_filename) |
|
|
{ |
|
|
dcc_close (ptr_dcc, DCC_FAILED); |
|
|
dcc_redraw (1); |
|
|
return; |
|
|
} |
|
|
if (cfg_dcc_download_path[0] == '~') |
|
|
{ |
|
|
strcpy (ptr_dcc->local_filename, ptr_home); |
|
|
strcat (ptr_dcc->local_filename, cfg_dcc_download_path + 1); |
|
|
} |
|
|
else |
|
|
strcpy (ptr_dcc->local_filename, cfg_dcc_download_path); |
|
|
if (ptr_dcc->local_filename[strlen (ptr_dcc->local_filename) - 1] != DIR_SEPARATOR_CHAR) |
|
|
strcat (ptr_dcc->local_filename, DIR_SEPARATOR); |
|
|
strcat (ptr_dcc->local_filename, ptr_dcc->nick); |
|
|
strcat (ptr_dcc->local_filename, "."); |
|
|
strcat (ptr_dcc->local_filename, ptr_dcc->filename); |
|
258 |
|
|
259 |
/* file already exists? */ |
/* DCC file => look for local filename and open it in writing mode */ |
260 |
if (access (ptr_dcc->local_filename, F_OK) == 0) |
if (DCC_IS_FILE(ptr_dcc->type)) |
261 |
{ |
{ |
262 |
/* if auto rename is not set, then abort DCC */ |
ptr_home = getenv ("HOME"); |
263 |
if (!cfg_dcc_auto_rename) |
ptr_dcc->local_filename = (char *) malloc (strlen (cfg_dcc_download_path) + |
264 |
|
strlen (ptr_dcc->nick) + |
265 |
|
strlen (ptr_dcc->filename) + |
266 |
|
((cfg_dcc_download_path[0] == '~') ? |
267 |
|
strlen (ptr_home) : 0) + |
268 |
|
4); |
269 |
|
if (!ptr_dcc->local_filename) |
270 |
{ |
{ |
271 |
dcc_close (ptr_dcc, DCC_FAILED); |
dcc_close (ptr_dcc, DCC_FAILED); |
272 |
dcc_redraw (1); |
dcc_redraw (1); |
273 |
return; |
return; |
274 |
} |
} |
275 |
|
if (cfg_dcc_download_path[0] == '~') |
|
filename2 = (char *) malloc (strlen (ptr_dcc->local_filename) + 16); |
|
|
if (!filename2) |
|
276 |
{ |
{ |
277 |
dcc_close (ptr_dcc, DCC_FAILED); |
strcpy (ptr_dcc->local_filename, ptr_home); |
278 |
dcc_redraw (1); |
strcat (ptr_dcc->local_filename, cfg_dcc_download_path + 1); |
|
return; |
|
279 |
} |
} |
280 |
ptr_dcc->filename_suffix = 0; |
else |
281 |
do |
strcpy (ptr_dcc->local_filename, cfg_dcc_download_path); |
282 |
|
if (ptr_dcc->local_filename[strlen (ptr_dcc->local_filename) - 1] != DIR_SEPARATOR_CHAR) |
283 |
|
strcat (ptr_dcc->local_filename, DIR_SEPARATOR); |
284 |
|
strcat (ptr_dcc->local_filename, ptr_dcc->nick); |
285 |
|
strcat (ptr_dcc->local_filename, "."); |
286 |
|
strcat (ptr_dcc->local_filename, ptr_dcc->filename); |
287 |
|
|
288 |
|
/* file already exists? */ |
289 |
|
if (access (ptr_dcc->local_filename, F_OK) == 0) |
290 |
{ |
{ |
291 |
ptr_dcc->filename_suffix++; |
/* if auto rename is not set, then abort DCC */ |
292 |
sprintf (filename2, "%s.%d", |
if (!cfg_dcc_auto_rename) |
293 |
ptr_dcc->local_filename, |
{ |
294 |
ptr_dcc->filename_suffix); |
dcc_close (ptr_dcc, DCC_FAILED); |
295 |
|
dcc_redraw (1); |
296 |
|
return; |
297 |
|
} |
298 |
|
|
299 |
|
filename2 = (char *) malloc (strlen (ptr_dcc->local_filename) + 16); |
300 |
|
if (!filename2) |
301 |
|
{ |
302 |
|
dcc_close (ptr_dcc, DCC_FAILED); |
303 |
|
dcc_redraw (1); |
304 |
|
return; |
305 |
|
} |
306 |
|
ptr_dcc->filename_suffix = 0; |
307 |
|
do |
308 |
|
{ |
309 |
|
ptr_dcc->filename_suffix++; |
310 |
|
sprintf (filename2, "%s.%d", |
311 |
|
ptr_dcc->local_filename, |
312 |
|
ptr_dcc->filename_suffix); |
313 |
|
} |
314 |
|
while (access (filename2, F_OK) == 0); |
315 |
|
|
316 |
|
free (ptr_dcc->local_filename); |
317 |
|
ptr_dcc->local_filename = strdup (filename2); |
318 |
|
free (filename2); |
319 |
} |
} |
320 |
while (access (filename2, F_OK) == 0); |
ptr_dcc->file = open (ptr_dcc->local_filename, |
321 |
|
O_CREAT | O_TRUNC | O_WRONLY | O_NONBLOCK, |
322 |
free (ptr_dcc->local_filename); |
0644); |
323 |
ptr_dcc->local_filename = strdup (filename2); |
} |
324 |
free (filename2); |
else |
325 |
|
{ |
326 |
|
/* DCC CHAT => associate DCC with channel */ |
327 |
|
dcc_channel_for_chat (ptr_dcc); |
328 |
} |
} |
|
ptr_dcc->file = open (ptr_dcc->local_filename, |
|
|
O_CREAT | O_TRUNC | O_WRONLY | O_NONBLOCK, |
|
|
0644); |
|
329 |
} |
} |
330 |
dcc_redraw (1); |
dcc_redraw (1); |
331 |
} |
} |
340 |
{ |
{ |
341 |
t_irc_dcc *new_dcc; |
t_irc_dcc *new_dcc; |
342 |
|
|
343 |
|
/* create new DCC struct */ |
344 |
if ((new_dcc = (t_irc_dcc *) malloc (sizeof (t_irc_dcc))) == NULL) |
if ((new_dcc = (t_irc_dcc *) malloc (sizeof (t_irc_dcc))) == NULL) |
345 |
{ |
{ |
346 |
gui_printf_nolog (server->buffer, |
irc_display_prefix (server->buffer, PREFIX_ERROR); |
347 |
_("%s not enough memory for new DCC\n"), |
gui_printf (server->buffer, |
348 |
WEECHAT_ERROR); |
_("%s not enough memory for new DCC\n"), |
349 |
|
WEECHAT_ERROR); |
350 |
return NULL; |
return NULL; |
351 |
} |
} |
352 |
|
|
353 |
|
/* initialize new DCC */ |
354 |
new_dcc->server = server; |
new_dcc->server = server; |
355 |
|
new_dcc->channel = NULL; |
356 |
new_dcc->type = type; |
new_dcc->type = type; |
357 |
new_dcc->status = DCC_WAITING; |
new_dcc->status = DCC_WAITING; |
358 |
new_dcc->addr = addr; |
new_dcc->addr = addr; |
359 |
new_dcc->port = port; |
new_dcc->port = port; |
360 |
new_dcc->nick = strdup (nick); |
new_dcc->nick = strdup (nick); |
361 |
new_dcc->sock = sock; |
new_dcc->sock = sock; |
362 |
|
new_dcc->unterminated_message = NULL; |
363 |
new_dcc->file = -1; |
new_dcc->file = -1; |
364 |
new_dcc->filename = strdup (filename); |
if (DCC_IS_CHAT(type)) |
365 |
|
new_dcc->filename = strdup (_("DCC chat")); |
366 |
|
else |
367 |
|
new_dcc->filename = (filename) ? strdup (filename) : NULL; |
368 |
new_dcc->local_filename = (local_filename) ? strdup (local_filename) : NULL; |
new_dcc->local_filename = (local_filename) ? strdup (local_filename) : NULL; |
369 |
new_dcc->filename_suffix = -1; |
new_dcc->filename_suffix = -1; |
370 |
new_dcc->size = size; |
new_dcc->size = size; |
379 |
gui_current_window->dcc_first = NULL; |
gui_current_window->dcc_first = NULL; |
380 |
gui_current_window->dcc_selected = NULL; |
gui_current_window->dcc_selected = NULL; |
381 |
|
|
382 |
|
/* write info message on server buffer */ |
383 |
if (type == DCC_FILE_RECV) |
if (type == DCC_FILE_RECV) |
384 |
{ |
{ |
385 |
irc_display_prefix (server->buffer, PREFIX_INFO); |
irc_display_prefix (server->buffer, PREFIX_INFO); |
396 |
gui_printf_color (server->buffer, COLOR_WIN_CHAT_CHANNEL, "%lu", size); |
gui_printf_color (server->buffer, COLOR_WIN_CHAT_CHANNEL, "%lu", size); |
397 |
gui_printf (server->buffer, _(" bytes\n")); |
gui_printf (server->buffer, _(" bytes\n")); |
398 |
} |
} |
|
|
|
399 |
if (type == DCC_FILE_SEND) |
if (type == DCC_FILE_SEND) |
400 |
{ |
{ |
401 |
irc_display_prefix (server->buffer, PREFIX_INFO); |
irc_display_prefix (server->buffer, PREFIX_INFO); |
402 |
gui_printf (server->buffer, _("Sending DCC file to ")); |
gui_printf (server->buffer, _("Sending DCC file to ")); |
403 |
gui_printf_color (server->buffer, COLOR_WIN_CHAT_NICK, "%s", nick); |
gui_printf_color (server->buffer, COLOR_WIN_CHAT_NICK, "%s", nick); |
|
gui_printf_color (server->buffer, COLOR_WIN_CHAT_DARK, " ("); |
|
|
gui_printf_color (server->buffer, COLOR_WIN_CHAT_HOST, |
|
|
"%d.%d.%d.%d", |
|
|
addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff); |
|
|
gui_printf_color (server->buffer, COLOR_WIN_CHAT_DARK, ")"); |
|
404 |
gui_printf (server->buffer, ": "); |
gui_printf (server->buffer, ": "); |
405 |
gui_printf_color (server->buffer, COLOR_WIN_CHAT_CHANNEL, "%s", filename); |
gui_printf_color (server->buffer, COLOR_WIN_CHAT_CHANNEL, "%s", filename); |
406 |
gui_printf (server->buffer, _(" (local filename: ")); |
gui_printf (server->buffer, _(" (local filename: ")); |
409 |
gui_printf_color (server->buffer, COLOR_WIN_CHAT_CHANNEL, "%lu", size); |
gui_printf_color (server->buffer, COLOR_WIN_CHAT_CHANNEL, "%lu", size); |
410 |
gui_printf (server->buffer, _(" bytes\n")); |
gui_printf (server->buffer, _(" bytes\n")); |
411 |
} |
} |
412 |
|
if (type == DCC_CHAT_RECV) |
413 |
|
{ |
414 |
|
irc_display_prefix (server->buffer, PREFIX_INFO); |
415 |
|
gui_printf (server->buffer, _("Incoming DCC chat request from ")); |
416 |
|
gui_printf_color (server->buffer, COLOR_WIN_CHAT_NICK, "%s", nick); |
417 |
|
gui_printf_color (server->buffer, COLOR_WIN_CHAT_DARK, " ("); |
418 |
|
gui_printf_color (server->buffer, COLOR_WIN_CHAT_HOST, |
419 |
|
"%d.%d.%d.%d", |
420 |
|
addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff); |
421 |
|
gui_printf_color (server->buffer, COLOR_WIN_CHAT_DARK, ")\n"); |
422 |
|
} |
423 |
|
if (type == DCC_CHAT_SEND) |
424 |
|
{ |
425 |
|
irc_display_prefix (server->buffer, PREFIX_INFO); |
426 |
|
gui_printf (server->buffer, _("Sending DCC chat request to ")); |
427 |
|
gui_printf_color (server->buffer, COLOR_WIN_CHAT_NICK, "%s\n", nick); |
428 |
|
} |
429 |
|
|
430 |
if (type == DCC_FILE_SEND) |
/* connect if needed and redraw DCC buffer */ |
431 |
|
if (DCC_IS_SEND(type)) |
432 |
{ |
{ |
433 |
if (!dcc_connect (new_dcc)) |
if (!dcc_connect (new_dcc)) |
434 |
{ |
{ |
448 |
} |
} |
449 |
|
|
450 |
/* |
/* |
451 |
* dcc_send: send DCC request (file or chat) |
* dcc_send_request: send DCC request (file or chat) |
452 |
*/ |
*/ |
453 |
|
|
454 |
void |
void |
455 |
dcc_send (t_irc_server *server, char *nick, char *filename) |
dcc_send_request (t_irc_server *server, int type, char *nick, char *filename) |
456 |
{ |
{ |
457 |
char *ptr_home, *filename2, *short_filename, *pos; |
char *ptr_home, *filename2, *short_filename, *pos; |
458 |
int spaces; |
int spaces; |
461 |
struct sockaddr_in addr; |
struct sockaddr_in addr; |
462 |
socklen_t length; |
socklen_t length; |
463 |
unsigned long local_addr; |
unsigned long local_addr; |
464 |
|
t_irc_dcc *ptr_dcc; |
465 |
|
|
466 |
/* add home if filename not beginning with '/' (not for Win32) */ |
filename2 = NULL; |
467 |
#ifdef _WIN32 |
short_filename = NULL; |
468 |
filename2 = strdup (filename); |
spaces = 0; |
469 |
#else |
|
470 |
if (filename[0] == '/') |
if (type == DCC_FILE_SEND) |
|
filename2 = strdup (filename); |
|
|
else |
|
471 |
{ |
{ |
472 |
ptr_home = getenv ("HOME"); |
/* add home if filename not beginning with '/' (not for Win32) */ |
473 |
filename2 = (char *) malloc (strlen (cfg_dcc_upload_path) + |
#ifdef _WIN32 |
474 |
strlen (filename) + |
filename2 = strdup (filename); |
475 |
((cfg_dcc_upload_path[0] == '~') ? |
#else |
476 |
strlen (ptr_home) : 0) + |
if (filename[0] == '/') |
477 |
4); |
filename2 = strdup (filename); |
478 |
if (!filename2) |
else |
479 |
{ |
{ |
480 |
irc_display_prefix (server->buffer, PREFIX_ERROR); |
ptr_home = getenv ("HOME"); |
481 |
gui_printf_nolog (server->buffer, |
filename2 = (char *) malloc (strlen (cfg_dcc_upload_path) + |
482 |
_("%s not enough memory for DCC SEND\n"), |
strlen (filename) + |
483 |
WEECHAT_ERROR); |
((cfg_dcc_upload_path[0] == '~') ? |
484 |
return; |
strlen (ptr_home) : 0) + |
485 |
|
4); |
486 |
|
if (!filename2) |
487 |
|
{ |
488 |
|
irc_display_prefix (server->buffer, PREFIX_ERROR); |
489 |
|
gui_printf (server->buffer, |
490 |
|
_("%s not enough memory for DCC SEND\n"), |
491 |
|
WEECHAT_ERROR); |
492 |
|
return; |
493 |
|
} |
494 |
|
if (cfg_dcc_upload_path[0] == '~') |
495 |
|
{ |
496 |
|
strcpy (filename2, ptr_home); |
497 |
|
strcat (filename2, cfg_dcc_upload_path + 1); |
498 |
|
} |
499 |
|
else |
500 |
|
strcpy (filename2, cfg_dcc_upload_path); |
501 |
|
if (filename2[strlen (filename2) - 1] != DIR_SEPARATOR_CHAR) |
502 |
|
strcat (filename2, DIR_SEPARATOR); |
503 |
|
strcat (filename2, filename); |
504 |
} |
} |
505 |
if (cfg_dcc_upload_path[0] == '~') |
#endif |
506 |
|
|
507 |
|
/* check if file exists */ |
508 |
|
if (stat (filename2, &st) == -1) |
509 |
{ |
{ |
510 |
strcpy (filename2, ptr_home); |
irc_display_prefix (server->buffer, PREFIX_ERROR); |
511 |
strcat (filename2, cfg_dcc_upload_path + 1); |
gui_printf (server->buffer, |
512 |
|
_("%s cannot access file \"%s\"\n"), |
513 |
|
WEECHAT_ERROR, filename2); |
514 |
|
if (filename2) |
515 |
|
free (filename2); |
516 |
|
return; |
517 |
} |
} |
|
else |
|
|
strcpy (filename2, cfg_dcc_upload_path); |
|
|
if (filename2[strlen (filename2) - 1] != DIR_SEPARATOR_CHAR) |
|
|
strcat (filename2, DIR_SEPARATOR); |
|
|
strcat (filename2, filename); |
|
|
} |
|
|
#endif |
|
|
|
|
|
/* check if file exists */ |
|
|
if (stat (filename2, &st) == -1) |
|
|
{ |
|
|
irc_display_prefix (server->buffer, PREFIX_ERROR); |
|
|
gui_printf_nolog (server->buffer, |
|
|
_("%s cannot access file \"%s\"\n"), |
|
|
WEECHAT_ERROR, filename2); |
|
|
free (filename2); |
|
|
return; |
|
518 |
} |
} |
519 |
|
|
520 |
/* get local IP address */ |
/* get local IP address */ |
521 |
memset (&addr, 0, sizeof (struct sockaddr_in)); |
memset (&addr, 0, sizeof (struct sockaddr_in)); |
522 |
length = sizeof (addr); |
length = sizeof (addr); |
523 |
getsockname (server->sock4, (struct sockaddr *) &addr, &length); |
getsockname (server->sock, (struct sockaddr *) &addr, &length); |
524 |
addr.sin_family = AF_INET; |
addr.sin_family = AF_INET; |
525 |
local_addr = ntohl (addr.sin_addr.s_addr); |
local_addr = ntohl (addr.sin_addr.s_addr); |
526 |
|
|
529 |
if (sock == -1) |
if (sock == -1) |
530 |
{ |
{ |
531 |
irc_display_prefix (server->buffer, PREFIX_ERROR); |
irc_display_prefix (server->buffer, PREFIX_ERROR); |
532 |
gui_printf_nolog (server->buffer, |
gui_printf (server->buffer, |
533 |
_("%s cannot create socket for DCC\n"), |
_("%s cannot create socket for DCC\n"), |
534 |
WEECHAT_ERROR); |
WEECHAT_ERROR); |
535 |
free (filename2); |
if (filename2) |
536 |
|
free (filename2); |
537 |
return; |
return; |
538 |
} |
} |
539 |
|
|
542 |
if (bind (sock, (struct sockaddr *) &addr, sizeof (addr)) == -1) |
if (bind (sock, (struct sockaddr *) &addr, sizeof (addr)) == -1) |
543 |
{ |
{ |
544 |
irc_display_prefix (server->buffer, PREFIX_ERROR); |
irc_display_prefix (server->buffer, PREFIX_ERROR); |
545 |
gui_printf_nolog (server->buffer, |
gui_printf (server->buffer, |
546 |
_("%s cannot find port for DCC\n"), |
_("%s cannot find port for DCC\n"), |
547 |
WEECHAT_ERROR); |
WEECHAT_ERROR); |
548 |
close (sock); |
close (sock); |
549 |
free (filename2); |
if (filename2) |
550 |
|
free (filename2); |
551 |
return; |
return; |
552 |
} |
} |
553 |
length = sizeof (addr); |
length = sizeof (addr); |
554 |
getsockname (sock, (struct sockaddr *) &addr, &length); |
getsockname (sock, (struct sockaddr *) &addr, &length); |
555 |
port = ntohs (addr.sin_port); |
port = ntohs (addr.sin_port); |
556 |
|
|
557 |
/* extract short filename (without path) */ |
if (type == DCC_FILE_SEND) |
|
pos = strrchr (filename2, DIR_SEPARATOR_CHAR); |
|
|
if (pos) |
|
|
short_filename = strdup (pos + 1); |
|
|
else |
|
|
short_filename = strdup (filename2); |
|
|
|
|
|
/* convert spaces to underscore if asked and needed */ |
|
|
pos = short_filename; |
|
|
spaces = 0; |
|
|
while (pos[0]) |
|
558 |
{ |
{ |
559 |
if (pos[0] == ' ') |
/* extract short filename (without path) */ |
560 |
|
pos = strrchr (filename2, DIR_SEPARATOR_CHAR); |
561 |
|
if (pos) |
562 |
|
short_filename = strdup (pos + 1); |
563 |
|
else |
564 |
|
short_filename = strdup (filename2); |
565 |
|
|
566 |
|
/* convert spaces to underscore if asked and needed */ |
567 |
|
pos = short_filename; |
568 |
|
spaces = 0; |
569 |
|
while (pos[0]) |
570 |
{ |
{ |
571 |
if (cfg_dcc_convert_spaces) |
if (pos[0] == ' ') |
572 |
pos[0] = '_'; |
{ |
573 |
else |
if (cfg_dcc_convert_spaces) |
574 |
spaces = 1; |
pos[0] = '_'; |
575 |
|
else |
576 |
|
spaces = 1; |
577 |
|
} |
578 |
|
pos++; |
579 |
} |
} |
|
pos++; |
|
580 |
} |
} |
581 |
|
|
582 |
/* add DCC entry and listen to socket */ |
/* add DCC entry and listen to socket */ |
583 |
if (!dcc_add (server, DCC_FILE_SEND, local_addr, port, nick, sock, |
if (type == DCC_CHAT_SEND) |
584 |
short_filename, filename2, st.st_size)) |
ptr_dcc = dcc_add (server, DCC_CHAT_SEND, local_addr, port, nick, sock, |
585 |
|
NULL, NULL, 0); |
586 |
|
else |
587 |
|
ptr_dcc = dcc_add (server, DCC_FILE_SEND, local_addr, port, nick, sock, |
588 |
|
short_filename, filename2, st.st_size); |
589 |
|
if (!ptr_dcc) |
590 |
{ |
{ |
591 |
irc_display_prefix (server->buffer, PREFIX_ERROR); |
irc_display_prefix (server->buffer, PREFIX_ERROR); |
592 |
gui_printf_nolog (server->buffer, |
gui_printf (server->buffer, |
593 |
_("%s cannot send DCC\n"), |
_("%s cannot send DCC\n"), |
594 |
WEECHAT_ERROR); |
WEECHAT_ERROR); |
595 |
close (sock); |
close (sock); |
596 |
free (short_filename); |
if (short_filename) |
597 |
free (filename2); |
free (short_filename); |
598 |
|
if (filename2) |
599 |
|
free (filename2); |
600 |
return; |
return; |
601 |
} |
} |
602 |
|
|
603 |
/* send DCC request to nick */ |
/* send DCC request to nick */ |
604 |
server_sendf (server, |
if (type == DCC_CHAT_SEND) |
605 |
(spaces) ? |
server_sendf (server, |
606 |
"PRIVMSG %s :\01DCC SEND \"%s\" %lu %d %u\01\r\n" : |
"PRIVMSG %s :\01DCC CHAT chat %lu %d\01\r\n", |
607 |
"PRIVMSG %s :\01DCC SEND %s %lu %d %u\01\r\n", |
nick, local_addr, port); |
608 |
nick, short_filename, local_addr, port, |
else |
609 |
(unsigned long) st.st_size); |
server_sendf (server, |
610 |
|
(spaces) ? |
611 |
|
"PRIVMSG %s :\01DCC SEND \"%s\" %lu %d %u\01\r\n" : |
612 |
|
"PRIVMSG %s :\01DCC SEND %s %lu %d %u\01\r\n", |
613 |
|
nick, short_filename, local_addr, port, |
614 |
|
(unsigned long) st.st_size); |
615 |
|
|
616 |
free (short_filename); |
if (short_filename) |
617 |
free (filename2); |
free (short_filename); |
618 |
|
if (filename2) |
619 |
|
free (filename2); |
620 |
|
} |
621 |
|
|
622 |
|
/* |
623 |
|
* dcc_chat_send: send data to remote host via DCC CHAT |
624 |
|
*/ |
625 |
|
|
626 |
|
int |
627 |
|
dcc_chat_send (t_irc_dcc *ptr_dcc, char *buffer, int size_buf) |
628 |
|
{ |
629 |
|
if (!ptr_dcc) |
630 |
|
return -1; |
631 |
|
|
632 |
|
return send (ptr_dcc->sock, buffer, size_buf, 0); |
633 |
|
} |
634 |
|
|
635 |
|
/* |
636 |
|
* dcc_chat_sendf: send formatted data to remote host via DCC CHAT |
637 |
|
*/ |
638 |
|
|
639 |
|
void |
640 |
|
dcc_chat_sendf (t_irc_dcc *ptr_dcc, char *fmt, ...) |
641 |
|
{ |
642 |
|
va_list args; |
643 |
|
static char buffer[4096]; |
644 |
|
char *buf2; |
645 |
|
int size_buf; |
646 |
|
|
647 |
|
if (!ptr_dcc || (ptr_dcc->sock == -1)) |
648 |
|
return; |
649 |
|
|
650 |
|
va_start (args, fmt); |
651 |
|
size_buf = vsnprintf (buffer, sizeof (buffer) - 1, fmt, args); |
652 |
|
va_end (args); |
653 |
|
|
654 |
|
if ((size_buf == 0) || (strcmp (buffer, "\r\n") == 0)) |
655 |
|
return; |
656 |
|
|
657 |
|
buffer[sizeof (buffer) - 1] = '\0'; |
658 |
|
if ((size_buf < 0) || (size_buf > (int) (sizeof (buffer) - 1))) |
659 |
|
size_buf = strlen (buffer); |
660 |
|
#ifdef DEBUG |
661 |
|
buffer[size_buf - 2] = '\0'; |
662 |
|
gui_printf (ptr_dcc->server->buffer, "[DEBUG] Sending to remote host (DCC CHAT) >>> %s\n", buffer); |
663 |
|
buffer[size_buf - 2] = '\r'; |
664 |
|
#endif |
665 |
|
buf2 = weechat_convert_encoding ((cfg_look_charset_internal && cfg_look_charset_internal[0]) ? |
666 |
|
cfg_look_charset_internal : local_charset, |
667 |
|
cfg_look_charset_encode, |
668 |
|
buffer); |
669 |
|
if (dcc_chat_send (ptr_dcc, buf2, strlen (buf2)) <= 0) |
670 |
|
{ |
671 |
|
irc_display_prefix (ptr_dcc->server->buffer, PREFIX_ERROR); |
672 |
|
gui_printf (ptr_dcc->server->buffer, _("%s error sending data to \"%s\" via DCC CHAT\n"), |
673 |
|
WEECHAT_ERROR, ptr_dcc->nick); |
674 |
|
dcc_close (ptr_dcc, DCC_FAILED); |
675 |
|
} |
676 |
|
free (buf2); |
677 |
} |
} |
678 |
|
|
679 |
|
/* |
680 |
|
* dcc_chat_recv: receive data from DCC CHAT host |
681 |
|
*/ |
682 |
|
|
683 |
|
void |
684 |
|
dcc_chat_recv (t_irc_dcc *ptr_dcc) |
685 |
|
{ |
686 |
|
static char buffer[4096 + 2]; |
687 |
|
char *buf2, *pos, *ptr_buf, *next_ptr_buf; |
688 |
|
int num_read; |
689 |
|
|
690 |
|
num_read = recv (ptr_dcc->sock, buffer, sizeof (buffer) - 2, 0); |
691 |
|
if (num_read > 0) |
692 |
|
{ |
693 |
|
buffer[num_read] = '\0'; |
694 |
|
|
695 |
|
buf2 = NULL; |
696 |
|
ptr_buf = buffer; |
697 |
|
if (ptr_dcc->unterminated_message) |
698 |
|
{ |
699 |
|
buf2 = (char *) malloc (strlen (ptr_dcc->unterminated_message) + |
700 |
|
strlen (buffer) + 1); |
701 |
|
if (buf2) |
702 |
|
{ |
703 |
|
strcpy (buf2, ptr_dcc->unterminated_message); |
704 |
|
strcat (buf2, buffer); |
705 |
|
} |
706 |
|
ptr_buf = buf2; |
707 |
|
free (ptr_dcc->unterminated_message); |
708 |
|
ptr_dcc->unterminated_message = NULL; |
709 |
|
} |
710 |
|
|
711 |
|
while (ptr_buf && ptr_buf[0]) |
712 |
|
{ |
713 |
|
next_ptr_buf = NULL; |
714 |
|
pos = strstr (ptr_buf, "\r\n"); |
715 |
|
if (pos) |
716 |
|
{ |
717 |
|
pos[0] = '\0'; |
718 |
|
next_ptr_buf = pos + 2; |
719 |
|
} |
720 |
|
else |
721 |
|
{ |
722 |
|
pos = strstr (ptr_buf, "\n"); |
723 |
|
if (pos) |
724 |
|
{ |
725 |
|
pos[0] = '\0'; |
726 |
|
next_ptr_buf = pos + 1; |
727 |
|
} |
728 |
|
else |
729 |
|
{ |
730 |
|
ptr_dcc->unterminated_message = strdup (ptr_buf); |
731 |
|
ptr_buf = NULL; |
732 |
|
next_ptr_buf = NULL; |
733 |
|
} |
734 |
|
} |
735 |
|
|
736 |
|
if (ptr_buf) |
737 |
|
{ |
738 |
|
gui_printf_type_color (ptr_dcc->channel->buffer, |
739 |
|
MSG_TYPE_NICK, |
740 |
|
COLOR_WIN_CHAT_DARK, "<"); |
741 |
|
if (strstr (ptr_buf, ptr_dcc->server->nick)) |
742 |
|
{ |
743 |
|
gui_printf_type_color (ptr_dcc->channel->buffer, |
744 |
|
MSG_TYPE_NICK | MSG_TYPE_HIGHLIGHT, |
745 |
|
COLOR_WIN_CHAT_HIGHLIGHT, |
746 |
|
"%s", ptr_dcc->nick); |
747 |
|
if ( (cfg_look_infobar_delay_highlight > 0) |
748 |
|
&& (ptr_dcc->channel->buffer != gui_current_window->buffer) ) |
749 |
|
gui_infobar_printf (cfg_look_infobar_delay_highlight, |
750 |
|
COLOR_WIN_INFOBAR_HIGHLIGHT, |
751 |
|
_("Private %s> %s"), |
752 |
|
ptr_dcc->nick, ptr_buf); |
753 |
|
} |
754 |
|
else |
755 |
|
gui_printf_type_color (ptr_dcc->channel->buffer, |
756 |
|
MSG_TYPE_NICK, |
757 |
|
COLOR_WIN_NICK_PRIVATE, |
758 |
|
"%s", ptr_dcc->nick); |
759 |
|
gui_printf_type_color (ptr_dcc->channel->buffer, |
760 |
|
MSG_TYPE_NICK, |
761 |
|
COLOR_WIN_CHAT_DARK, "> "); |
762 |
|
gui_printf_type_color (ptr_dcc->channel->buffer, |
763 |
|
MSG_TYPE_MSG, |
764 |
|
COLOR_WIN_CHAT, "%s\n", ptr_buf); |
765 |
|
} |
766 |
|
|
767 |
|
ptr_buf = next_ptr_buf; |
768 |
|
} |
769 |
|
|
770 |
|
if (buf2) |
771 |
|
free (buf2); |
772 |
|
} |
773 |
|
else |
774 |
|
{ |
775 |
|
dcc_close (ptr_dcc, DCC_ABORTED); |
776 |
|
dcc_redraw (1); |
777 |
|
} |
778 |
|
} |
779 |
|
|
780 |
/* |
/* |
781 |
* dcc_handle: receive/send data for each active DCC |
* dcc_handle: receive/send data for each active DCC (files only) |
782 |
*/ |
*/ |
783 |
|
|
784 |
void |
void |
830 |
ptr_dcc->addr = ntohl (addr.sin_addr.s_addr); |
ptr_dcc->addr = ntohl (addr.sin_addr.s_addr); |
831 |
ptr_dcc->status = DCC_ACTIVE; |
ptr_dcc->status = DCC_ACTIVE; |
832 |
ptr_dcc->file = open (ptr_dcc->local_filename, O_RDONLY | O_NONBLOCK, 0644); |
ptr_dcc->file = open (ptr_dcc->local_filename, O_RDONLY | O_NONBLOCK, 0644); |
833 |
|
dcc_redraw (1); |
834 |
|
} |
835 |
|
} |
836 |
|
} |
837 |
|
} |
838 |
|
|
839 |
|
if (ptr_dcc->status == DCC_WAITING) |
840 |
|
{ |
841 |
|
if (ptr_dcc->type == DCC_CHAT_SEND) |
842 |
|
{ |
843 |
|
FD_ZERO (&read_fd); |
844 |
|
FD_SET (ptr_dcc->sock, &read_fd); |
845 |
|
timeout.tv_sec = 0; |
846 |
|
timeout.tv_usec = 0; |
847 |
|
|
848 |
|
/* something to read on socket? */ |
849 |
|
if (select (FD_SETSIZE, &read_fd, NULL, NULL, &timeout) > 0) |
850 |
|
{ |
851 |
|
if (FD_ISSET (ptr_dcc->sock, &read_fd)) |
852 |
|
{ |
853 |
|
length = sizeof (addr); |
854 |
|
sock = accept (ptr_dcc->sock, (struct sockaddr *) &addr, &length); |
855 |
|
close (ptr_dcc->sock); |
856 |
|
ptr_dcc->sock = -1; |
857 |
|
if (sock < 0) |
858 |
|
{ |
859 |
|
dcc_close (ptr_dcc, DCC_FAILED); |
860 |
|
dcc_redraw (1); |
861 |
|
return; |
862 |
|
} |
863 |
|
ptr_dcc->sock = sock; |
864 |
|
if (fcntl (ptr_dcc->sock, F_SETFL, O_NONBLOCK) == -1) |
865 |
|
{ |
866 |
|
dcc_close (ptr_dcc, DCC_FAILED); |
867 |
|
dcc_redraw (1); |
868 |
|
return; |
869 |
|
} |
870 |
|
ptr_dcc->addr = ntohl (addr.sin_addr.s_addr); |
871 |
|
ptr_dcc->status = DCC_ACTIVE; |
872 |
|
dcc_redraw (1); |
873 |
|
dcc_channel_for_chat (ptr_dcc); |
874 |
} |
} |
875 |
} |
} |
876 |
} |
} |
878 |
|
|
879 |
if (ptr_dcc->status == DCC_ACTIVE) |
if (ptr_dcc->status == DCC_ACTIVE) |
880 |
{ |
{ |
881 |
|
if (DCC_IS_CHAT(ptr_dcc->type)) |
882 |
|
{ |
883 |
|
FD_ZERO (&read_fd); |
884 |
|
FD_SET (ptr_dcc->sock, &read_fd); |
885 |
|
timeout.tv_sec = 0; |
886 |
|
timeout.tv_usec = 0; |
887 |
|
|
888 |
|
/* something to read on socket? */ |
889 |
|
if (select (FD_SETSIZE, &read_fd, NULL, NULL, &timeout) > 0) |
890 |
|
{ |
891 |
|
if (FD_ISSET (ptr_dcc->sock, &read_fd)) |
892 |
|
dcc_chat_recv (ptr_dcc); |
893 |
|
} |
894 |
|
} |
895 |
if (ptr_dcc->type == DCC_FILE_RECV) |
if (ptr_dcc->type == DCC_FILE_RECV) |
896 |
{ |
{ |
897 |
num_read = recv (ptr_dcc->sock, buffer, sizeof (buffer), 0); |
num_read = recv (ptr_dcc->sock, buffer, sizeof (buffer), 0); |
926 |
{ |
{ |
927 |
if (cfg_dcc_blocksize > (int) sizeof (buffer)) |
if (cfg_dcc_blocksize > (int) sizeof (buffer)) |
928 |
{ |
{ |
929 |
|
irc_display_prefix (NULL, PREFIX_ERROR); |
930 |
gui_printf (NULL, _("%s DCC failed because blocksize is too " |
gui_printf (NULL, _("%s DCC failed because blocksize is too " |
931 |
"big. Check value of \"dcc_blocksize\" option, " |
"big. Check value of \"dcc_blocksize\" option, " |
932 |
"max is %d.\n"), |
"max is %d.\n"), |