-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdcc.c
2558 lines (2269 loc) · 77.9 KB
/
dcc.c
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* dcc.c: Things dealing client to client connections.
*
* Written By Troy Rollo <[email protected]>
*
* Copyright(c) 1991
*
* See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT
* Heavily modified Colten Edwards 1996-97
*
* needs to be rewritten
* it really sucks.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "irc.h"
#include <sys/stat.h>
#include <stdarg.h>
#include <stdio.h>
#include "ctcp.h"
#include "dcc.h"
#include "hook.h"
#include "ircaux.h"
#include "lastlog.h"
#include "newio.h"
#include "output.h"
#include "parse.h"
#include "server.h"
#include "status.h"
#include "vars.h"
#include "whois.h"
#include "window.h"
#include "ircterm.h"
#include "hook.h"
#include "misc.h"
#include "screen.h"
#include "hash.h"
#include "fset.h"
#include "tcommand.h"
#include <float.h>
static off_t filesize = 0;
DCC_list *ClientList = NULL;
static char DCC_current_transfer_buffer[BIG_BUFFER_SIZE / 4];
static void dcc_add_deadclient(register DCC_list *);
static void dcc_close(const char *, char *);
static void dcc_getfile(const char *, char *);
int dcc_open(DCC_list *);
static void dcc_really_erase(void);
static void dcc_rename(const char *, char *);
static void dcc_send_raw(const char *, char *);
static void add_to_dcc_buffer(DCC_list * Client, char *buf);
static void output_reject_ctcp(char *, char *);
static void process_incoming_chat(register DCC_list *);
static void process_incoming_listen(register DCC_list *);
static void process_incoming_raw(register DCC_list *);
static void process_outgoing_file(register DCC_list *, int);
static void process_incoming_file(register DCC_list *);
static void DCC_close_filesend(DCC_list *, char *);
static void update_transfer_buffer(const char *format, ...);
static void dcc_got_connected(DCC_list *);
static void dcc_set_paths(const char *, char *);
static void dcc_set_quiet(const char *, char *);
static void dcc_show_active(const char *, char *);
static void dcc_help1(const char *, char *);
static unsigned char byteordertest(void);
static void dcc_reject_notify(char *, char *, const char *);
static int get_to_from(const char *);
static char *strip_path(char *);
static void dcc_tog_rename(const char *, char *);
static char *check_paths(char *);
static void dcc_overwrite_toggle(const char *, char *);
typedef void (*dcc_function) (const char *, char *);
typedef struct {
const char *name;
dcc_function function;
} DCC_commands;
static const DCC_commands dcc_commands[] = {
{"ACTIVE", dcc_show_active},
{"AUTO_RENAME", dcc_tog_rename},
{"CHAT", dcc_chat},
{"LIST", dcc_list},
{"GLIST", dcc_glist},
{"SEND", dcc_filesend},
{"RESEND", dcc_resend},
{"GET", dcc_getfile},
{"REGET", dcc_regetfile},
{"CLOSE", dcc_close},
{"RENAME", dcc_rename},
{"RAW", dcc_send_raw},
{"QUIET", dcc_set_quiet},
{"PATHS", dcc_set_paths},
{"OVERWRITE", dcc_overwrite_toggle},
{"?", dcc_help1},
{"HELP", dcc_help1},
{NULL, (dcc_function) NULL}
};
#define BAR_LENGTH 50
static int dcc_count = 1;
int dcc_active_count = 0;
static int doing_multi = 0;
static int dcc_quiet = 0;
static int dcc_paths = 0;
static int dcc_overwrite_var = 0;
unsigned int send_count_stat = 0;
unsigned int get_count_stat = 0;
static char *last_chat_req = NULL;
static const char *dcc_types[] = {
"<none>",
"CHAT",
"SEND",
"GET",
"RAW_LISTEN",
"RAW",
"RESEND",
"REGET",
NULL
};
static struct deadlist {
DCC_list *it;
struct deadlist *next;
} *deadlist = NULL;
static int dccBlockSize(void)
{
register int BlockSize;
BlockSize = get_int_var(DCC_BLOCK_SIZE_VAR);
if (BlockSize > MAX_DCC_BLOCK_SIZE) {
BlockSize = MAX_DCC_BLOCK_SIZE;
set_int_var(DCC_BLOCK_SIZE_VAR, MAX_DCC_BLOCK_SIZE);
} else if (BlockSize < 16) {
BlockSize = 16;
set_int_var(DCC_BLOCK_SIZE_VAR, 16);
}
return (BlockSize);
}
/*
* dcc_searchlist searches through the dcc_list and finds the client
* with the the flag described in type set.
*/
static DCC_list *dcc_searchlist(const char *name, const char *user, int type, int flag, char *othername, char *userhost, int active)
{
DCC_list **Client = NULL, *NewClient = NULL;
int dcc_num = 0;
if (user && *user == '#' && my_atol(user + 1)) {
/* we got a number so find that instead */
const char *p;
p = user;
p++;
dcc_num = my_atol(p);
if (dcc_num > 0) {
for (Client = (&ClientList); *Client; Client = (&(**Client).next))
if (dcc_num && ((**Client).dccnum == dcc_num))
return *Client;
}
} else {
for (Client = (&ClientList); *Client; Client = (&(**Client).next)) {
if ((**Client).flags & DCC_DELETE)
continue; /* ignore deleted clients */
/*
* The following things have to be true:
* -> The types have to be the same
* -> One of the following things is true:
* -> `name' is NULL or `name' is the same as the
* entry's description
* *OR*
* -> `othername' is the same as the entry's othername
* -> `user' is the same as the entry's user-perr
* -> One of the following is true:
* -> `active' is 1 and the entry is active
* -> `active' is 0 and the entry is not active
* -> `active' is -1 (dont care)
*/
if ((((**Client).flags & DCC_TYPES) == type) &&
((!name || ((**Client).description && !my_stricmp(name, (**Client).description))) ||
(othername && (**Client).othername && !my_stricmp(othername, (**Client).othername))) &&
(user && !my_stricmp(user, (**Client).user)) &&
((active == -1) ||
((active == 0) && !((**Client).flags & DCC_ACTIVE)) || ((active == 1) && ((**Client).flags & DCC_ACTIVE))
)
)
return *Client;
}
}
if (!flag)
return NULL;
*Client = NewClient = new_malloc(sizeof(DCC_list));
NewClient->flags = type;
NewClient->read = NewClient->write = NewClient->file = -1;
NewClient->filesize = filesize;
malloc_strcpy(&NewClient->description, name);
malloc_strcpy(&NewClient->user, user);
malloc_strcpy(&NewClient->othername, othername);
malloc_strcpy(&NewClient->userhost, userhost);
NewClient->packets_total = filesize ? (filesize / dccBlockSize() + 1) : 0;
get_time(&NewClient->lasttime);
if (dcc_count == 0)
dcc_count = 1;
NewClient->dccnum = dcc_count++;
return NewClient;
}
/*
* Added by Chaos: Is used in edit.c for checking redirect.
*/
int dcc_active(char *user)
{
return (dcc_searchlist("chat", user, DCC_CHAT, 0, NULL, NULL, 1)) ? 1 : 0;
}
int dcc_activeraw(char *user)
{
return (dcc_searchlist( /* "RAW" */ NULL, user, DCC_RAW, 0, NULL, NULL, 1)) ? 1 : 0;
}
static void dcc_add_deadclient(register DCC_list * client)
{
struct deadlist *new;
new = new_malloc(sizeof(struct deadlist));
new->next = deadlist;
new->it = client;
deadlist = new;
}
/*
* dcc_erase searches for the given entry in the dcc_list and
* removes it
*/
int dcc_erase(DCC_list * Element)
{
register DCC_list **Client;
int erase_one = 0;
dcc_count = 1;
for (Client = &ClientList; *Client; Client = &(**Client).next) {
if (*Client == Element) {
*Client = Element->next;
if (Element->write > -1) {
close(Element->write);
}
if (Element->read > -1) {
close(Element->read);
}
if (Element->file > -1)
close(Element->file);
new_free(&Element->othername);
new_free(&Element->description);
new_free(&Element->userhost);
new_free(&Element->user);
new_free(&Element->buffer);
new_free(&Element->cksum);
if (Element->dcc_handler)
(Element->dcc_handler) (NULL, NULL);
new_free(&Element);
erase_one++;
break;
}
}
for (Client = &ClientList; *Client; Client = &(**Client).next)
(*Client)->dccnum = dcc_count++;
if (erase_one) {
*DCC_current_transfer_buffer = 0;
}
return erase_one;
}
static void dcc_really_erase(void)
{
register struct deadlist *dies;
for (; (dies = deadlist) != NULL;) {
deadlist = deadlist->next;
dcc_erase(dies->it);
}
}
/*
* Set the descriptor set to show all fds in Client connections to
* be checked for data.
*/
void set_dcc_bits(fd_set * rd, fd_set * wd)
{
register DCC_list *Client;
unsigned int flag;
for (Client = ClientList; Client != NULL; Client = Client->next) {
flag = Client->flags & DCC_TYPES;
if (Client->write != -1 && ((Client->flags & DCC_CNCT_PEND) || (flag == DCC_FILEOFFER) || (flag == DCC_RESENDOFFER)))
FD_SET(Client->write, wd);
if (Client->read != -1)
FD_SET(Client->read, rd);
}
if (!ClientList && dcc_active_count)
dcc_active_count = 0; /* A HACK */
}
static void dcc_got_connected(DCC_list * client)
{
struct sockaddr_in remaddr = { 0 };
socklen_t rl = sizeof(remaddr);
if (getpeername(client->read, (struct sockaddr *) &remaddr, &rl) != -1) {
if (client->flags & DCC_OFFER) {
client->flags &= ~DCC_OFFER;
if ((client->flags & DCC_TYPES) != DCC_RAW) {
if (do_hook(DCC_CONNECT_LIST, "%s %s %s %d", client->user,
dcc_types[client->flags & DCC_TYPES], inet_ntoa(remaddr.sin_addr), ntohs(remaddr.sin_port))) {
if (!dcc_quiet)
put_it("%s", convert_output_format(get_fset_var(FORMAT_DCC_CONNECT_FSET),
"%s %s %s %s %s %d", update_clock(GET_TIME),
dcc_types[client->flags & DCC_TYPES], client->user,
client->userhost ? client->userhost : "u@h", inet_ntoa(remaddr.sin_addr),
(int) ntohs(remaddr.sin_port)));
}
}
}
get_time(&client->starttime);
client->flags &= ~DCC_CNCT_PEND;
if (((client->flags & DCC_TYPES) == DCC_REGETFILE)) {
/* send a packet to the sender with transfer resume instructions */
put_it("%s",
convert_output_format("$G %RDCC %YTelling uplink we want to start at%n: $0", "%l",
client->transfer_orders.byteoffset));
send(client->read, (const char *) &client->transfer_orders, sizeof(struct transfer_struct), 0);
}
if (0 /* !get_int_var(DCC_FAST_VAR) */ ) {
set_blocking(client->read);
if (client->read != client->write)
set_blocking(client->write);
}
}
}
/*
* Check all DCCs for data, and if they have any, perform whatever
* actions are required.
*/
void dcc_check(fd_set * Readables, fd_set * Writables)
{
register DCC_list **Client;
struct timeval timeout;
int previous_server;
int lastlog_level;
register int flags;
previous_server = from_server;
from_server = (-1);
timeout.tv_sec = timeout.tv_usec = 0;
message_from(NULL, LOG_DCC);
lastlog_level = set_lastlog_msg_level(LOG_DCC);
for (Client = (&ClientList); *Client != NULL;) {
flags = (*Client)->flags;
if (flags & DCC_CNCT_PEND)
dcc_got_connected(*Client);
if (!(flags & DCC_DELETE) && (*Client)->read != -1) {
if (FD_ISSET((*Client)->read, Readables)) {
FD_CLR((*Client)->read, Readables);
switch (flags & DCC_TYPES) {
case DCC_CHAT:
from_server = previous_server;
process_incoming_chat(*Client);
from_server = -1;
break;
case DCC_RAW_LISTEN:
process_incoming_listen(*Client);
break;
case DCC_RAW:
process_incoming_raw(*Client);
break;
case DCC_FILEOFFER:
case DCC_RESENDOFFER:
process_outgoing_file(*Client, 1);
break;
case DCC_FILEREAD:
case DCC_REGETFILE:
process_incoming_file(*Client);
break;
}
}
}
if (flags & DCC_DELETE)
dcc_add_deadclient(*Client);
Client = (&(**Client).next);
}
message_from(NULL, LOG_CRAP);
(void) set_lastlog_msg_level(lastlog_level);
dcc_really_erase();
from_server = previous_server;
}
/*
* Process a DCC command from the user.
*/
void process_dcc(char *args)
{
char *command;
int i;
int lastlog_level;
if (!(command = next_arg(args, &args)))
return;
for (i = 0; dcc_commands[i].name != NULL; i++) {
if (!my_stricmp(dcc_commands[i].name, command)) {
message_from(NULL, LOG_DCC);
lastlog_level = set_lastlog_msg_level(LOG_DCC);
dcc_commands[i].function(dcc_commands[i].name, args);
message_from(NULL, LOG_CRAP);
(void) set_lastlog_msg_level(lastlog_level);
return;
}
}
put_it("%s", convert_output_format("$G Unknown %RDCC%n command: $0", "%s", command));
}
int dcc_open(DCC_list * Client)
{
char *user;
const char *type;
struct in_addr myip;
int old_server;
struct sockaddr *saddr;
socklen_t slen;
user = Client->user;
old_server = from_server;
if (from_server == -1)
from_server = get_window_server(0);
myip.s_addr = 0;
if (sa_addr_a2s(server_list[from_server].lcl_addr, &saddr, &slen) == SA_OK) {
if (saddr->sa_family == AF_INET) {
myip.s_addr = ((struct sockaddr_in *) saddr)->sin_addr.s_addr;
free(saddr);
} else {
yell("hello sorry dcc works only with IPv4 kthx");
free(saddr);
return 0;
}
} else {
yell("something bad happened, I couldn't get local address");
return 0;
}
type = dcc_types[Client->flags & DCC_TYPES];
if (Client->flags & DCC_OFFER) {
message_from(NULL, LOG_DCC);
Client->flags |= DCC_CNCT_PEND;
/* BLAH! */
Client->remport = ntohs(Client->remport);
if ((Client->write = connect_by_number(inet_ntoa(Client->remote), &Client->remport, SERVICE_CLIENT, PROTOCOL_TCP, 1)) < 0) {
put_it("%s",
convert_output_format("$G %RDCC%n Unable to create connection: $0-", "%s",
errno ? strerror(errno) : "Unknown Host"));
message_from(NULL, LOG_CRAP);
dcc_erase(Client);
from_server = old_server;
return 0;
}
/* BLAH! */
Client->remport = htons(Client->remport);
Client->read = Client->write;
Client->flags |= DCC_ACTIVE;
message_from(NULL, LOG_CRAP);
get_time(&Client->starttime);
Client->bytes_read = Client->bytes_sent = 0L;
from_server = old_server;
return 1;
} else {
unsigned short portnum = 0;
Client->flags |= DCC_WAIT | DCC_CNCT_PEND;
message_from(NULL, LOG_DCC);
if (Client->remport)
portnum = Client->remport;
if ((Client->read = connect_by_number(NULL, &portnum, SERVICE_SERVER, PROTOCOL_TCP, 1)) < 0) {
put_it("%s",
convert_output_format("$G %RDCC%n Unable to create connection: $0-", "%s",
errno ? strerror(errno) : "Unknown Host"));
message_from(NULL, LOG_CRAP);
dcc_erase(Client);
from_server = old_server;
return 0;
}
if (get_to_from(type) != -1)
dcc_active_count++;
if (Client->flags & DCC_TWOCLIENTS) {
/* patch to NOT send pathname accross */
char *nopath;
if (((Client->flags & DCC_FILEOFFER) == DCC_FILEOFFER) && (nopath = strrchr(Client->description, '/')))
nopath++;
else
nopath = Client->description;
if (Client->filesize)
send_ctcp(CTCP_PRIVMSG, user, CTCP_DCC,
"%s %s %lu %u %lu", type, nopath, (u_long) ntohl(myip.s_addr), (u_short) portnum, Client->filesize);
else
send_ctcp(CTCP_PRIVMSG, user, CTCP_DCC, "%s %s %lu %u", type, nopath, (u_long) ntohl(myip.s_addr), (u_short) portnum);
message_from(NULL, LOG_DCC);
if (!doing_multi && !dcc_quiet)
put_it("%s", convert_output_format(get_fset_var(FORMAT_SEND_DCC_CHAT_FSET),
"%s %s %s", update_clock(GET_TIME), type, user));
message_from(NULL, LOG_CRAP);
}
Client->starttime.tv_sec = Client->starttime.tv_usec = 0;
from_server = old_server;
return 2;
}
message_from(NULL, LOG_CRAP);
}
void add_userhost_to_dcc(WhoisStuff * stuff, char *nick, char *args)
{
DCC_list *Client;
Client = dcc_searchlist("chat", nick, DCC_CHAT, 0, NULL, NULL, -1);
if (!stuff || !nick || !stuff->nick || !stuff->user || !stuff->host || !strcmp(stuff->user, "<UNKNOWN>"))
return;
if (Client)
Client->userhost = m_sprintf("%s@%s", stuff->user, stuff->host);
return;
}
void dcc_chat(const char *command, char *args)
{
char *user;
char *port = NULL;
char *equal_user = NULL;
DCC_list *Client;
if ((user = next_arg(args, &args)) == NULL) {
put_it("%s", convert_output_format("$G %RDCC%n You must supply a nickname for DCC chat", NULL, NULL));
return;
}
while (args && *args) {
char *argument = next_arg(args, &args);
if (argument && strlen(argument) >= 2) {
if (argument[0] == '-' || argument[1] == 'p')
port = next_arg(args, &args);
}
}
Client = dcc_searchlist("chat", user, DCC_CHAT, 1, NULL, NULL, -1);
if ((Client->flags & DCC_ACTIVE) || (Client->flags & DCC_WAIT)) {
put_it("%s", convert_output_format("$G %RDCC%n A previous DCC chat to $0 exists", "%s", user));
return;
}
if (port)
Client->remport = atol(port);
Client->flags |= DCC_TWOCLIENTS;
equal_user = m_sprintf("=%s", user);
addtabkey(equal_user, "msg");
new_free(&equal_user);
dcc_open(Client);
add_to_userhost_queue(user, add_userhost_to_dcc, "%d %s", Client->read, user);
}
char *dcc_raw_listen(int port)
{
DCC_list *Client;
char *PortName;
struct sockaddr_in locaddr;
char *RetName = NULL;
socklen_t size;
int lastlog_level;
lastlog_level = set_lastlog_msg_level(LOG_DCC);
if (port && port < 1025) {
put_it("%s", convert_output_format("$G %RDCC%n Cannot bind to a privileged port", NULL, NULL));
(void) set_lastlog_msg_level(lastlog_level);
return NULL;
}
PortName = ltoa(port);
Client = dcc_searchlist("raw_listen", PortName, DCC_RAW_LISTEN, 1, NULL, NULL, -1);
if (Client->flags & DCC_ACTIVE) {
put_it("%s", convert_output_format("$G %RDCC%n A previous Raw Listen on $0 exists", "%s", PortName));
set_lastlog_msg_level(lastlog_level);
return RetName;
}
memset((char *) &locaddr, 0, sizeof(locaddr));
locaddr.sin_family = AF_INET;
locaddr.sin_addr.s_addr = htonl(INADDR_ANY);
locaddr.sin_port = htons(port);
if (0 > (Client->read = socket(AF_INET, SOCK_STREAM, 0))) {
dcc_erase(Client);
put_it("%s", convert_output_format("$G %RDCC%n socket() failed: $0-", "%s", strerror(errno)));
(void) set_lastlog_msg_level(lastlog_level);
return RetName;
}
set_socket_options(Client->read);
if (bind(Client->read, (struct sockaddr *) &locaddr, sizeof(locaddr)) == -1) {
dcc_erase(Client);
put_it("%s", convert_output_format("$G %RDCC%n Could not bind port: $0-", "%s", strerror(errno)));
(void) set_lastlog_msg_level(lastlog_level);
return RetName;
}
listen(Client->read, 4);
size = sizeof(locaddr);
get_time(&Client->starttime);
getsockname(Client->read, (struct sockaddr *) &locaddr, &size);
Client->write = ntohs(locaddr.sin_port);
Client->flags |= DCC_ACTIVE;
malloc_strcpy(&Client->user, ltoa(Client->write));
malloc_strcpy(&RetName, Client->user);
(void) set_lastlog_msg_level(lastlog_level);
return RetName;
}
char *dcc_raw_connect(char *host, u_short port)
{
DCC_list *Client;
char *PortName;
struct in_addr address;
struct hostent *hp;
int lastlog_level;
lastlog_level = set_lastlog_msg_level(LOG_DCC);
if ((address.s_addr = inet_addr(host)) == -1) {
hp = gethostbyname(host);
if (!hp) {
put_it("%s", convert_output_format("$G %RDCC%n Unknown host: $0-", "%s", host));
set_lastlog_msg_level(lastlog_level);
return m_strdup(empty_str);
}
memcpy(&address, hp->h_addr, sizeof(address));
}
Client = dcc_searchlist(host, ltoa(port), DCC_RAW, 1, NULL, NULL, -1);
if (Client->flags & DCC_ACTIVE) {
put_it("%s", convert_output_format("$G %RDCC%n A previous DCC raw to $0 on $1 exists", "%s %d", host, port));
set_lastlog_msg_level(lastlog_level);
return m_strdup(empty_str);
}
/* Sorry. The missing 'htons' call here broke $connect() */
Client->remport = htons(port);
memcpy((char *) &Client->remote, (char *) &address, sizeof(address));
Client->flags = DCC_OFFER | DCC_RAW;
if (!dcc_open(Client))
return m_strdup(empty_str);
PortName = ltoa(Client->read);
Client->user = m_strdup(PortName);
if (do_hook(DCC_RAW_LIST, "%s %s E %d", PortName, host, port))
if (do_hook(DCC_CONNECT_LIST, "%s RAW %s %d", PortName, host, port))
put_it("%s", convert_output_format(get_fset_var(FORMAT_DCC_CONNECT_FSET),
"%s %s %s %s %s %d", update_clock(GET_TIME), "RAW", host,
Client->userhost ? Client->userhost : "u@h", PortName, port));
(void) set_lastlog_msg_level(lastlog_level);
return m_strdup(ltoa(Client->read));
}
void real_dcc_filesend(char *filename, char *real_file, char *user, int type, int portnum)
{
DCC_list *Client;
struct stat stat_buf;
char *nick = NULL;
stat(filename, &stat_buf);
#ifdef S_IFDIR
if (stat_buf.st_mode & S_IFDIR) {
put_it("%s", convert_output_format("$G %RDCC%n Cannot send a directory", NULL, NULL));
return;
}
#endif
filesize = stat_buf.st_size;
while ((nick = next_in_comma_list(user, &user))) {
if (!nick || !*nick)
break;
Client = dcc_searchlist(filename, nick, type, 1, real_file, NULL, -1);
filesize = 0;
if (!(Client->flags & DCC_ACTIVE) && !(Client->flags & DCC_WAIT)) {
Client->flags |= DCC_TWOCLIENTS;
Client->remport = portnum;
dcc_open(Client);
} else
put_it("%s", convert_output_format("$G %RDCC%n A previous DCC send:$0 to $1 exists", "%s %s", filename, nick));
}
}
void dcc_resend(const char *command, char *args)
{
char *user = NULL;
char *filename = NULL, *fullname = NULL;
char *FileBuf = NULL;
int portnum = 0;
if (!(user = next_arg(args, &args)) || !(filename = next_arg(args, &args))) {
put_it("%s", convert_output_format("$G %RDCC%n You must supply a nickname and a filename for DCC resend", NULL, NULL));
return;
}
if (*filename == '/') {
malloc_strcpy(&FileBuf, filename);
} else if (*filename == '~') {
if (!(fullname = expand_twiddle(filename))) {
put_it("%s", convert_output_format("$G %RDCC%n Unable to expand: $0", "%s", filename));
return;
}
malloc_strcpy(&FileBuf, fullname);
new_free(&fullname);
} else {
char current_dir[BIG_BUFFER_SIZE + 1];
getcwd(current_dir, sizeof(current_dir) - strlen(filename) - 4);
malloc_sprintf(&FileBuf, "%s/%s", current_dir, filename);
}
if (access(FileBuf, R_OK)) {
put_it("%s", convert_output_format("$G %RDCC%n Cannot access: $0", "%s", FileBuf));
new_free(&FileBuf);
return;
}
while (args && *args) {
char *argument = next_arg(args, &args);
if (argument && strlen(argument) >= 2) {
if (*argument == '-' || *(argument + 1) == 'p') {
char *v = next_arg(args, &args);
portnum = (v) ? my_atol(v) : 0;
}
}
}
real_dcc_filesend(FileBuf, filename, user, DCC_RESENDOFFER, portnum);
new_free(&FileBuf);
}
void dcc_filesend(const char *command, char *args)
{
char *user = NULL;
char *filename = NULL, *fullname = NULL;
char FileBuf[BIG_BUFFER_SIZE + 1];
int portnum = 0;
if (!(user = next_arg(args, &args)) || !(filename = next_arg(args, &args))) {
put_it("%s", convert_output_format("$G %RDCC%n You must supply a nickname and a filename for DCC send", NULL, NULL));
return;
}
if (*filename == '/')
strcpy(FileBuf, filename);
else if (*filename == '~') {
if (0 == (fullname = expand_twiddle(filename))) {
put_it("%s", convert_output_format("$G %RDCC%n Unable to expand: $0", "%s", filename));
return;
}
strcpy(FileBuf, fullname);
new_free(&fullname);
} else {
getcwd(FileBuf, sizeof(FileBuf));
strcat(FileBuf, "/");
strcat(FileBuf, filename);
}
while (args && *args) {
char *argument = next_arg(args, &args);
if (argument && strlen(argument) >= 2) {
if (*argument == '-' && *(argument + 1) == 'p') {
char *v = next_arg(args, &args);
portnum = (v) ? my_atol(v) : 0;
}
}
}
real_dcc_filesend(FileBuf, filename, user, DCC_FILEOFFER, portnum);
}
void multiget(char *usern, char *filen)
{
DCC_list *dccList;
char *newbuf = NULL;
char *expand = NULL;
char *q = NULL;
doing_multi = 1;
if ((q = strchr(filen, ' ')))
*q = 0;
for (dccList = ClientList; dccList; dccList = dccList->next) {
if ((match(dccList->description, filen) || match(filen, dccList->description)) &&
!my_stricmp(usern, dccList->user) && (dccList->flags & DCC_TYPES) == DCC_FILEREAD) {
if (get_string_var(DCC_DLDIR_VAR)) {
expand = expand_twiddle(get_string_var(DCC_DLDIR_VAR));
malloc_sprintf(&newbuf, "%s %s/%s", usern, expand, dccList->description);
} else
malloc_sprintf(&newbuf, "%s %s", usern, dccList->description);
if (!dcc_quiet)
put_it("%s", convert_output_format("$G %RDCC%n Attempting DCC get: $0", "%s", dccList->description));
dcc_getfile(NULL, newbuf);
new_free(&expand);
new_free(&newbuf);
}
}
doing_multi = 0;
}
static void dcc_getfile(const char *command, char *args)
{
char *user;
char *filename = NULL;
char *tmp = NULL;
DCC_list *Client;
char *fullname = NULL;
char *argument = NULL;
if (0 == (user = next_arg(args, &args))) {
put_it("%s", convert_output_format("$G %RDCC%n You must supply a nickname for DCC get", NULL, NULL));
return;
}
if (args && *args) {
if (args && strlen(args) >= 2) {
if (*args != '-' && *(args + 1) != 'e')
filename = next_arg(args, &args);
else if (args && *args == '-' && *(args + 1) == 'e') {
argument = next_arg(args, &args);
}
}
}
if (0 == (Client = dcc_searchlist(filename, user, DCC_FILEREAD, 0, NULL, NULL, 0))) {
if (filename)
put_it("%s", convert_output_format("$G %RDCC%n No file ($0) offered in SEND mode by $1", "%s %s", filename, user));
else
put_it("%s", convert_output_format("$G %RDCC%n No file offered in SEND mode by $0", "%s", user));
return;
}
if ((Client->flags & DCC_ACTIVE) || (Client->flags & DCC_WAIT)) {
put_it("%s", convert_output_format("$G %RDCC%n A previous DCC get:$0 to $0 exists", "%s %s", filename, user));
return;
}
Client->flags |= DCC_TWOCLIENTS;
Client->bytes_sent = Client->bytes_read = 0L;
if (!dcc_open(Client))
return;
if (get_string_var(DCC_DLDIR_VAR))
malloc_sprintf(&tmp, "%s/%s", get_string_var(DCC_DLDIR_VAR), Client->description);
else
malloc_sprintf(&tmp, "%s", Client->description);
if (!(fullname = expand_twiddle(tmp)))
malloc_strcpy(&fullname, tmp);
if (-1 == (Client->file = open(fullname, O_WRONLY | O_TRUNC | O_CREAT, 0644))) {
put_it("%s",
convert_output_format("$G %RDCC%n Unable to open $0: $1-", "%s %s", Client->description,
errno ? strerror(errno) : "Unknown"));
if (dcc_active_count)
dcc_active_count--;
Client->flags |= DCC_DELETE;
}
new_free(&fullname);
new_free(&tmp);
}
void dcc_regetfile(const char *command, char *args)
{
char *user;
char *tmp = NULL;
char *filename;
DCC_list *Client;
char *fullname = NULL;
struct stat buf;
if (0 == (user = next_arg(args, &args))) {
put_it("%s", convert_output_format("$G %RDCC%n You must supply a nickname for DCC reget", NULL, NULL));
return;
}
filename = next_arg(args, &args);
if (!(Client = dcc_searchlist(filename, user, DCC_REGETFILE, 0, NULL, NULL, 0))) {
if (filename)
put_it("%s", convert_output_format("$G %RDCC%n No file ($0) offered in RESEND mode by $1", "%s %s", filename, user));
else
put_it("%s", convert_output_format("$G %RDCC%n No file offered in RESEND mode by $0", "%s", user));
return;
}
if ((Client->flags & DCC_ACTIVE) || (Client->flags & DCC_WAIT)) {
put_it("%s", convert_output_format("$G %RDCC%n A previous DCC reget:$0 to $0 exists", "%s %s", filename, user));
return;
}
Client->flags |= DCC_TWOCLIENTS;
Client->bytes_sent = Client->bytes_read = 0L;
if (get_string_var(DCC_DLDIR_VAR))
malloc_sprintf(&tmp, "%s/%s", get_string_var(DCC_DLDIR_VAR), Client->description);
else
malloc_sprintf(&tmp, "%s", Client->description);
if (!(fullname = expand_twiddle(tmp)))
malloc_strcpy(&fullname, tmp);
if (!dcc_open(Client))
return;
if (-1 == (Client->file = open(fullname, O_WRONLY | O_CREAT, 0644))) {
put_it("%s",
convert_output_format("$G %RDCC%n Unable to open $0: $1-", "%s %s", Client->description,
errno ? strerror(errno) : "Unknown"));
Client->flags |= DCC_DELETE;
if (dcc_active_count)
dcc_active_count--;
return;
}
/* seek to the end of the file about to be resumed */
lseek(Client->file, 0, SEEK_END);
/* get the size of our file to be resumed */
fstat(Client->file, &buf);
Client->transfer_orders.packet_id = DCC_PACKETID;
Client->transfer_orders.byteoffset = buf.st_size;
Client->transfer_orders.byteorder = byteordertest();
new_free(&fullname);
new_free(&tmp);
}
void register_dcc_offer(char *user, char *type, char *description, char *address, char *port, char *size, char *extra, char *userhost)
{
DCC_list *Client;
int CType;
char *c = NULL;
u_long TempLong;
unsigned TempInt;
long packets = 0;
if ((c = strrchr(description, '/')))
description = c + 1;
if ('.' == *description)
*description = '_';
TempInt = (unsigned) strtoul(port, NULL, 10);
if (TempInt < 1024) {
put_it("%s", convert_output_format("$G $RDCC%n Priveleged port attempt [$0]", "%d", TempInt));
message_from(NULL, LOG_CRAP);
return;
}
message_from(NULL, LOG_DCC);
if (size && *size) {
filesize = my_atol(size);
packets = filesize / dccBlockSize() + 1;
} else
packets = filesize = 0;
if (!my_stricmp(type, "CHAT"))
CType = DCC_CHAT;
else if (!my_stricmp(type, "SEND"))
CType = DCC_FILEREAD;
else if (!my_stricmp(type, "RESEND"))
CType = DCC_REGETFILE;
else {
put_it("%s", convert_output_format("$G %RDCC%n Unknown DCC $0 ($1) recieved from $2", "%s %s %s", type, description, user));
message_from(NULL, LOG_CRAP);