-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
2099 lines (1883 loc) · 62.4 KB
/
Copy pathmain.cpp
File metadata and controls
2099 lines (1883 loc) · 62.4 KB
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
/*
WASTE - main.cpp (Windows main entry point and a lot of code :)
Copyright (C) 2003 Nullsoft, Inc.
WASTE 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.
WASTE 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 WASTE; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
---- some things that would be good to do eventually:
permissions per key:
C=chat
S=search/browse out
s=search/browsable
X=file transfer
K=key distribution
U=uploads
P=pings/ips/etc
?=unknown (all others)
have the connection keep track of the flags and key hash, then have the
prefs update all connections' flags.
improve ip list sorting (use a sorted listview?)
plugin interface
ipv6 support
Ephemeral Diffie-Hellman for session key exchange?
---- bits about how the negotiation works:
RNG: based on RSAREF's, but extended (uses 32 byte state, with 16 bytes counter, and 16 bytes of system entropy
constantly mixed in -- this takes from when messages arrive, mouse movement, timing information on when connections
come up, etc).
On connection:
1)
A sends B 16 random bytes (bfhpkA)
A sends B blowFish(bfhpkA,20 byte SHA-1 of public key + 4 pad bytes)
B sends A 16 random bytes (bfhpkB)
B sends A blowFish(bfhpkB,20 byte SHA-1 of public key + 4 pad bytes)
2)
if A knows B's public key, and B knows A public key, continue.
A sends B: RSA(pubkey_B,sKey1 + bfhpkB), sKey1 is 64 random bytes. (+ = concat)
B sends A: RSA(pubKey_A,sKey2 + bfhpkA), sKey2 is 64 random bytes. (+ = concat)
Check to make sure bfhpkA and bfhpkB are correct, and to make sure sKey1^sKey2 is nonzero,
with the last 8 bytes of sKey1 differing from the last 8 bytes of sKey2.
3)
each side initializes blowfish using the first 56 bytes sKey1^sKey2 as key.
A uses the last 8 bytes of sKey2 as recv CBC IV.
B uses the last 8 bytes of sKey1 as recv CBC IV.
4)
each side sends a 16 byte signature, blowfished.
each side verifies 16 byte signature.
message communication begins.
further messages are verified with a MD5 to detect tampering.
*/
#include "main.h"
#include "resource.h"
#include "rsa/md5.h"
#include "childwnd.h"
#include "m_upload.h"
#include "m_chat.h"
#include "m_search.h"
#include "m_ping.h"
#include "m_keydist.h"
#include "m_lcaps.h"
#include "d_chat.h"
#include "prefs.h"
#include "xferwnd.h"
#include "netq.h"
#include "netkern.h"
#include "srchwnd.h"
#define VERSION "v1.0b"
char *g_nameverstr=APP_NAME " " VERSION;
char *g_def_extlist="ppt;doc;xls;txt;zip;";
C_FileDB *g_database, *g_newdatabase;
C_AsyncDNS *g_dns;
C_Listen *g_listen;
C_MessageQueueList *g_mql;
C_Config *g_config;
HMENU g_context_menus;
HWND g_mainwnd;
HICON g_hSmallIcon;
HINSTANCE g_hInst;
int g_hidewnd_state;
unsigned char g_networkhash[SHA_OUTSIZE];
int g_use_networkhash;
char g_config_prefix[1024];
char g_config_mainini[1024];
char g_profile_name[128];
int g_extrainf;
int g_keepup;
int g_conspeed,g_route_traffic;
int g_do_log;
int g_max_simul_dl;
unsigned int g_max_simul_dl_host;
int g_forceip, g_forceip_addr;
int g_use_accesslist;
int g_appendprofiletitles;
int g_do_autorefresh;
int g_accept_downloads;
int g_port;
int g_chat_timestamp;
int g_keydist_flags;
R_RSA_PRIVATE_KEY g_key;
unsigned char g_pubkeyhash[SHA_OUTSIZE];
char g_regnick[32];
char g_filedlg_ulpath[256];
int g_throttle_flag, g_throttle_send, g_throttle_recv;
int g_search_showfull,g_search_showfullbytes;
int g_scanloadhack;
char g_scan_status_buf[128];
time_t g_next_refreshtime;
time_t g_last_pingtime,g_last_bcastkeytime;
T_GUID g_last_scanid;
int g_last_scanid_used;
T_GUID g_last_pingid;
int g_last_pingid_used;
T_GUID g_client_id;
char g_client_id_str[33];
static C_ItemList<C_UploadRequest> uploadPrompts;
static C_ItemList<C_KeydistRequest> keydistPrompts;
void main_BroadcastPublicKey(T_Message *src)
{
C_KeydistRequest rep;
rep.set_nick(g_regnick);
rep.set_flags((g_port && g_listen && !g_listen->is_error())?M_KEYDIST_FLAG_LISTEN:0);
R_RSA_PUBLIC_KEY k;
k.bits=g_key.bits;
memcpy(k.exponent,g_key.publicExponent,MAX_RSA_MODULUS_LEN);
memcpy(k.modulus,g_key.modulus,MAX_RSA_MODULUS_LEN);
rep.set_key(&k);
T_Message msg={0,};
msg.data=rep.Make();
if (msg.data)
{
if (src)
{
msg.message_guid=src->message_guid;
msg.message_type=MESSAGE_KEYDIST_REPLY;
}
else
{
msg.message_type=MESSAGE_KEYDIST;
}
msg.message_length=msg.data->GetLength();
g_mql->send(&msg);
}
}
static void main_handleKeyDist(C_KeydistRequest *kdr, int pending)
{
if (!kdr->get_key()->bits) return;
if (findPublicKeyFromKey(kdr->get_key())) return;
PKitem *p=(PKitem *)malloc(sizeof(PKitem));
SHAify m;
safe_strncpy(p->name,kdr->get_nick(),sizeof(p->name));
p->pk = *kdr->get_key();
m.add((unsigned char *)p->pk.modulus,MAX_RSA_MODULUS_LEN);
m.add((unsigned char *)p->pk.exponent,MAX_RSA_MODULUS_LEN);
m.final(p->hash);
if (pending)
g_pklist_pending.Add(p);
else
g_pklist.Add(p);
savePKList();
}
static void main_handleUpload(char *guidstr, char *fnstr, C_UploadRequest *t)
{
int willq=Xfer_WillQ(fnstr,guidstr);
int p=g_lvrecvq.InsertItem(g_lvrecvq.GetCount(),fnstr,0);
char sizebuf[64];
strcpy(sizebuf,"?");
int fs_l,fs_h;
t->get_fsize(&fs_l,&fs_h);
if (fs_l != -1 || fs_h != -1)
{
FormatSizeStr64(sizebuf,fs_l,fs_h);
}
g_lvrecvq.SetItemText(p,1,sizebuf);
g_lvrecvq.SetItemText(p,2,guidstr);
g_files_in_download_queue++;
if (g_config->ReadInt("aorecv",1))
{
HWND h=GetForegroundWindow();
SendMessage(g_mainwnd,WM_COMMAND,ID_VIEW_TRANSFERS,0);
if (g_config->ReadInt("aorecv_btf",0)) SetForegroundWindow(g_xferwnd);
else SetForegroundWindow(h);
XferDlg_SetSel(willq);
}
RecvQ_UpdateStatusText();
}
void main_MsgCallback(T_Message *message, C_MessageQueueList *_this, C_Connection *cn)
{
switch(message->message_type)
{
case MESSAGE_LOCAL_SATURATE:
//debug_printf("got a %d byte saturation message\n",40+message->message_length);
break;
case MESSAGE_LOCAL_CAPS:
{
C_MessageLocalCaps mlc(message->data);
int x;
for (x = 0; x < mlc.get_numcaps(); x ++)
{
int n,v;
mlc.get_cap(x,&n,&v);
switch (n)
{
case MLC_SATURATION:
debug_printf("got request that saturation be %d on this link\n",v);
if (cn) cn->set_saturatemode(v); // if ((get_saturatemode()&1) && (g_throttle_flag&32)) do outbound saturation
break;
case MLC_BANDWIDTH:
debug_printf("got request that max sendbuf size be %d on this link\n",v);
if (cn) cn->set_max_sendsize(v);
break;
}
}
}
break;
case MESSAGE_KEYDIST_REPLY:
case MESSAGE_KEYDIST:
{
C_KeydistRequest *r=new C_KeydistRequest(message->data);
if ((r->get_flags() & M_KEYDIST_FLAG_LISTEN) || (g_port && g_listen && !g_listen->is_error()) && r->get_key()->bits)
{
if (g_keydist_flags&1) // add without prompt
{
main_handleKeyDist(r,0);
delete r;
}
else if (g_keydist_flags&2) // add with prompt
{
keydistPrompts.Add(r);
}
else // add to pending
{
main_handleKeyDist(r,1);
delete r;
}
if (message->message_type != MESSAGE_KEYDIST_REPLY)
{
main_BroadcastPublicKey(message);
}
}
else delete r;
}
break;
case MESSAGE_PING:
{
MYSRANDUPDATE((unsigned char *)&message->message_guid,16);
C_MessagePing rep(message->data);
if (rep.m_port && rep.m_ip && (int)cn->get_interface() != rep.m_ip)
add_to_netq(rep.m_ip,(int)(unsigned short)rep.m_port,90,0);
if (rep.m_nick[0] && rep.m_nick[0] != '#' && rep.m_nick[0] != '&' &&
rep.m_nick[0] != '.' && strlen(rep.m_nick)<24)
main_onGotNick(rep.m_nick,0);
}
break;
case MESSAGE_SEARCH_USERLIST:
if (g_regnick[0] && g_regnick[0] != '.' && strlen(g_regnick)<24)
{
C_MessageSearchReply repl;
repl.set_conspeed(g_conspeed);
repl.set_guid(&g_client_id);
repl.add_item(-1,g_regnick,"Node",g_database->GetNumFiles(),g_database->GetNumMB(),g_database->GetLatestTime());
T_Message msg={0,};
msg.message_guid=message->message_guid;
msg.data=repl.Make();
if (msg.data)
{
msg.message_type=MESSAGE_SEARCH_REPLY;
msg.message_length=msg.data->GetLength();
_this->send(&msg);
}
}
break;
case MESSAGE_SEARCH:
if ((g_accept_downloads&1) && g_database->GetNumFiles()>0)
{
C_MessageSearchRequest req(message->data);
char *ss=req.get_searchstring();
if (
(ss[0] == '/' && (g_accept_downloads&4)) || (ss[0] && ss[0] != '/' && (g_accept_downloads&2))
)
{
C_MessageSearchReply repl;
repl.set_conspeed(g_conspeed);
repl.set_guid(&g_client_id);
g_database->Search(ss,&repl,_this,message,main_MsgCallback);
}
}
break;
case MESSAGE_SEARCH_REPLY:
{
if (g_last_scanid_used && !memcmp(&g_last_scanid,&message->message_guid,16))
{
C_MessageSearchReply repl(message->data);
if (repl.get_numitems()==1)
{
char name[1024],metadata[256];
if (!repl.get_item(0,NULL,name,metadata,NULL,NULL,NULL))
main_onGotNick(name,0);
}
}
else Search_AddReply(message);
}
break;
case MESSAGE_FILE_REQUEST:
{
C_FileSendRequest *r = new C_FileSendRequest(message->data);
if (!memcmp(r->get_guid(),&g_client_id,sizeof(g_client_id)))
{
int n=g_sends.GetSize();
int x;
for (x = 0; x < n; x ++)
{
if (!memcmp(r->get_prev_guid(),g_sends.Get(x)->get_guid(),16))
{
if (r->is_abort()==2)
{
int a=g_sends.Get(x)->GetIdx()-UPLOAD_BASE_IDX;
if (a >= 0 && a < g_uploads.GetSize())
{
char *p=g_uploads.Get(a);
if (p)
{
int lvidx;
while ((lvidx=g_lvsend.FindItemByParam((int)p)) >= 0)
{
g_lvsend.DeleteItem(lvidx);
}
free(p);
g_uploads.Set(a,NULL);
}
}
}
g_sends.Get(x)->set_guid(&message->message_guid);
g_sends.Get(x)->onGotMsg(r);
break;
}
}
if (x == n && !r->is_abort()) // new file request
{
if (g_config->ReadInt("limit_uls",1) && n < g_config->ReadInt("ul_limit",160))
{
char fn[2048];
fn[0]=0;
int idx=r->get_idx();
if (idx >= 0)
{
if (idx < UPLOAD_BASE_IDX)
{
if (!(g_accept_downloads&1)) fn[0]=0;
else g_database->GetFile(idx,fn,NULL,NULL,NULL);
}
else
{
idx-=UPLOAD_BASE_IDX;
if (idx<g_uploads.GetSize() && g_uploads.Get(idx))
{
safe_strncpy(fn,g_uploads.Get(idx),sizeof(fn));
int lvidx;
if ((lvidx=g_lvsend.FindItemByParam((int)g_uploads.Get(idx))) >= 0)
{
g_lvsend.DeleteItem(lvidx);
}
}
}
}
if (fn[0])
{
XferSend *a=new XferSend(_this,&message->message_guid,r,fn);
// remove any timed out files of a->GetName() from r->get_nick()
n=g_lvsend.GetCount();
for (x = 0; x < n; x ++)
{
if (g_lvsend.GetParam(x)) continue;
char buf[1024];
g_lvsend.GetText(x,3,buf,sizeof(buf));
if (strncmp(buf,"Timed out",9)) continue;
g_lvsend.GetText(x,0,buf,sizeof(buf));
if (strcmp(buf,a->GetName())) continue;
g_lvsend.GetText(x,1,buf,sizeof(buf));
if (strcmp(buf,r->get_nick())) continue;
g_lvsend.DeleteItem(x);
x--;
n--;
}
char *err=a->GetError();
if (err)
{
if (!g_config->ReadInt("send_autoclear",0))
{
g_lvsend.InsertItem(0,a->GetName(),0);
char buf[32];
int fs_l,fs_h;
a->GetSize((unsigned int *)&fs_l,(unsigned int *)&fs_h);
FormatSizeStr64(buf,fs_l,fs_h);
g_lvsend.SetItemText(0,1,r->get_nick());
g_lvsend.SetItemText(0,2,buf);
g_lvsend.SetItemText(0,3,err);
}
delete a;
}
else
{
g_sends.Add(a);
g_lvsend.InsertItem(0,a->GetName(),(int)a);
char buf[32];
int fs_l,fs_h;
a->GetSize((unsigned int *)&fs_l,(unsigned int *)&fs_h);
FormatSizeStr64(buf,fs_l,fs_h);
g_lvsend.SetItemText(0,1,r->get_nick());
g_lvsend.SetItemText(0,2,buf);
g_lvsend.SetItemText(0,3,"Sending");
}
PostMessage(g_xferwnd,WM_USER_TITLEUPDATE,0,0);
}
else
{
T_Message msg={0,};
C_FileSendReply reply;
reply.set_error(1);
msg.data=reply.Make();
if (msg.data)
{
msg.message_type=MESSAGE_FILE_REQUEST_REPLY;
msg.message_length=msg.data->GetLength();
msg.message_guid=message->message_guid;
g_mql->send(&msg);
}
}
}
}
}
delete r;
}
break;
case MESSAGE_FILE_REQUEST_REPLY:
{
int n=g_recvs.GetSize();
int x;
for (x = 0; x < n; x ++)
{
if (!memcmp(g_recvs.Get(x)->get_guid(),&message->message_guid,16))
{
g_recvs.Get(x)->onGotMsg(new C_FileSendReply(message->data));
break;
}
}
}
break;
case MESSAGE_CHAT_REPLY:
chat_HandleMsg(message);
break;
case MESSAGE_CHAT:
if (chat_HandleMsg(message) && g_regnick[0])
{ // send reply
C_MessageChatReply rep;
rep.setnick(g_regnick);
T_Message msg={0,};
msg.message_guid=message->message_guid;
msg.data=rep.Make();
if (msg.data)
{
msg.message_type=MESSAGE_CHAT_REPLY;
msg.message_length=msg.data->GetLength();
_this->send(&msg);
}
}
break;
case MESSAGE_UPLOAD:
{
int upflag=g_config->ReadInt("accept_uploads",1);
if (upflag&1) {
C_UploadRequest *r = new C_UploadRequest(message->data);
if (!stricmp(r->get_dest(),g_client_id_str) || (g_regnick[0] && !stricmp(r->get_dest(),g_regnick)))
{
char *fn=r->get_fn();
int weirdfn=!strncmp(fn,"..",2) || strstr(fn,":") || strstr(fn,"..\\") || strstr(fn,"../") || fn[0]=='\\' || fn[0] == '/';
if (!(upflag & 4) || weirdfn)
{
char *p=fn;
while (*p) p++;
while (p >= fn && *p != '/' && *p != '\\') p--;
p++;
if (p != fn || !weirdfn) fn=p;
else fn="";
}
if (fn[0])
{
if (!(upflag & 2))
{
char str[64];
MakeID128Str(r->get_guid(),str);
sprintf(str+strlen(str),":%d",r->get_idx());
main_handleUpload(str,fn,r);
}
else
{
char *t=strdup(fn);
r->set_fn(t);
free(t);
uploadPrompts.Add(r);
break;
}
}
else
{
debug_printf("got upload request that was invalid\n");
}
}
delete r;
}
}
break;
default:
// debug_printf("unknown message received : %d\n",message->message_type);
break;
}
}
static UINT CALLBACK fileHookProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_INITDIALOG)
{
SetDlgItemText(hwndDlg,IDC_UPATH,g_filedlg_ulpath);
}
if (uMsg == WM_COMMAND)
{
if (LOWORD(wParam) == IDC_UPATH && HIWORD(wParam) == EN_CHANGE)
{
GetDlgItemText(hwndDlg,IDC_UPATH,g_filedlg_ulpath,sizeof(g_filedlg_ulpath));
return 1;
}
}
return 0;
}
void UserListContextMenu(HWND htree)
{
TVHITTESTINFO hit;
hit.pt.x=GET_X_LPARAM(GetMessagePos());
hit.pt.y=GET_Y_LPARAM(GetMessagePos());
ScreenToClient(htree,&hit.pt);
HTREEITEM h=TreeView_HitTest(htree,&hit);
if (!h) h=TreeView_GetSelection(htree);
if (h)
{
char text[256];
TVITEM i;
i.mask=TVIF_TEXT|TVIF_HANDLE;
i.hItem=h;
i.pszText=text;
i.cchTextMax=sizeof(text);
text[0]=0;
TreeView_GetItem(htree,&i);
if (i.pszText[0])
{
HMENU hMenu=GetSubMenu(g_context_menus,2);
POINT p;
GetCursorPos(&p);
int x=TrackPopupMenu(hMenu,TPM_RETURNCMD|TPM_RIGHTBUTTON|TPM_LEFTBUTTON|TPM_NONOTIFY,p.x,p.y,0,GetParent(htree),NULL);
if (x == ID_SENDFILENODE)
{
char *fnroot=(char*)malloc(65536*4);
OPENFILENAME l={sizeof(l),};
fnroot[0]=0;
l.hwndOwner = GetParent(htree);
l.lpstrFilter = "All files (*.*)\0*.*\0";
l.lpstrFile = fnroot;
l.nMaxFile = 65535*4;
l.lpstrTitle = "Open file(s) to send";
l.lpstrDefExt = "";
l.hInstance=g_hInst;
l.lpfnHook=fileHookProc;
l.lpTemplateName=MAKEINTRESOURCE(IDD_FILESUBDLG);
l.Flags = OFN_HIDEREADONLY|OFN_EXPLORER|OFN_ALLOWMULTISELECT|OFN_ENABLETEMPLATE|OFN_ENABLEHOOK|OFN_FILEMUSTEXIST;
if (GetOpenFileName(&l))
{
char *fn=fnroot;
char *pathstr="";
if (fn[strlen(fn)+1]) // multiple files
{
pathstr=fn;
fn+=strlen(fn)+1;
}
while (*fn)
{
char fullfn[4096];
fullfn[0]=0;
if (*pathstr)
{
strcpy(fullfn,pathstr);
if (fullfn[strlen(fullfn)-1]!='\\') strcat(fullfn,"\\");
}
strcat(fullfn,fn);
Xfer_UploadFileToUser(GetParent(htree),fullfn,i.pszText,g_filedlg_ulpath);
fn+=strlen(fn)+1;
}
}
free(fnroot);
}
else if (x == ID_BROWSEUSER)
{
char buf[1024];
sprintf(buf,"/%s",i.pszText);
SendMessage(g_mainwnd,WM_COMMAND,IDC_SEARCH,0);
Search_Search(buf);
}
else if (x == ID_PRIVMSGNODE)
{
chat_ShowRoom(i.pszText,1);
}
else if (x == ID_WHOISUSER)
{
T_Message msg;
// send a message to text that is /whois
C_MessageChat req;
req.set_chatstring("/whois");
req.set_dest(i.pszText);
req.set_src(g_regnick);
msg.data=req.Make();
msg.message_type=MESSAGE_CHAT;
if (msg.data)
{
msg.message_length=msg.data->GetLength();
g_mql->send(&msg);
}
}
}
}
}
void handleWasteURL(char *url)
{
char tmp[2048];
safe_strncpy(tmp,url,sizeof(tmp));
char *in=tmp+6,*out=tmp;
while (*in)
{
if (!strncmp(in,"%20",3)) { in+=3; *out++=' '; }
else *out++=*in++;
}
*out=0;
if (tmp[0] == '?')
{
if (!strnicmp(tmp,"?chat=",6))
{
chat_ShowRoom(tmp+6,2);
}
else if (!strnicmp(tmp,"?browse=",8))
{
SendMessage(g_mainwnd,WM_COMMAND,IDC_SEARCH,0);
Search_Search(tmp+8);
}
}
else
{
SendMessage(g_mainwnd,WM_COMMAND,IDC_SEARCH,0);
Search_Search(tmp);
}
}
static void SendDirectoryToUser(HWND hwndDlg, char *fullfn, char *text, int offs)
{
char maskstr[2048];
WIN32_FIND_DATA d;
sprintf(maskstr,"%s\\*.*",fullfn);
HANDLE h=FindFirstFile(maskstr,&d);
if (h != INVALID_HANDLE_VALUE)
{
do
{
if (d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (d.cFileName[0] != '.')
{
sprintf(maskstr,"%s\\%s",fullfn,d.cFileName);
SendDirectoryToUser(hwndDlg,maskstr,text,offs);
}
}
else
{
sprintf(maskstr,"%s\\%s",fullfn,d.cFileName);
Xfer_UploadFileToUser(hwndDlg,maskstr,text,fullfn+offs);
}
}
while (FindNextFile(h,&d));
FindClose(h);
}
}
void UserListOnDropFiles(HWND hwndDlg, HWND htree, HDROP hdrop, char *forcenick)
{
TVHITTESTINFO i;
DragQueryPoint(hdrop,&i.pt);
ClientToScreen(hwndDlg,&i.pt);
ScreenToClient(htree,&i.pt);
HTREEITEM htt=0;
if (!forcenick) htt=TreeView_HitTest(htree,&i);
if (forcenick || htt)
{
char fullfn[1024];
int x,y = DragQueryFile(hdrop,0xffffffff,fullfn,sizeof(fullfn));
char texto[64];
TVITEM tvi;
tvi.mask=TVIF_TEXT|TVIF_HANDLE;
tvi.hItem=htt;
texto[0]=0;
tvi.pszText=texto;
tvi.cchTextMax=sizeof(texto);
if (!forcenick) TreeView_GetItem(htree,&tvi);
if (forcenick || tvi.pszText[0])
{
char *text=forcenick?forcenick:tvi.pszText;
if (text[0]) for (x = 0; x < y; x ++)
{
DragQueryFile(hdrop,x,fullfn,sizeof(fullfn));
DWORD attr=GetFileAttributes(fullfn);
if (attr != 0xFFFFFFFF)
{
if (attr & FILE_ATTRIBUTE_DIRECTORY)
{
int a=strlen(fullfn);
while (a>=0 && fullfn[a] != '\\') a--;
a++;
SendDirectoryToUser(hwndDlg,fullfn,text,a);
}
else Xfer_UploadFileToUser(hwndDlg,fullfn,text,"");
}
}
}
DragFinish(hdrop);
}
}
void update_set_port()
{
delete g_listen;
g_listen=NULL;
g_port=(g_route_traffic && g_config->ReadInt("listen",1)) ?
g_config->ReadInt("port",1337) : 0;
if (g_port)
{
debug_printf("[main] creating listen object on %d\n",g_port);
g_listen = new C_Listen((short)g_port);
}
}
void doDatabaseRescan()
{
debug_printf("DB: reinitializing database\n");
if (g_database && g_database != g_newdatabase && g_newdatabase)
{
debug_printf("DB: deleting temp db\n");
delete g_newdatabase;
}
g_newdatabase=new C_FileDB();
if (g_config->ReadInt("use_extlist",0))
g_newdatabase->UpdateExtList(g_config->ReadString("extlist",g_def_extlist));
else
g_newdatabase->UpdateExtList("*");
g_newdatabase->Scan(g_config->ReadString("databasepath",""));
if (!g_database || !g_database->GetNumFiles())
{
if (g_database)
{
debug_printf("DB: making scanning db live\n");
delete g_database;
}
g_database=g_newdatabase;
}
}
void main_onGotChannel(char *cnl)
{
HWND htree=GetDlgItem(g_mainwnd,IDC_CHATROOMS);
int added=0;
HTREEITEM h = TreeView_GetChild(htree,TVI_ROOT);
while (h)
{
char text[512];
TVITEM i;
i.mask=TVIF_HANDLE|TVIF_PARAM|TVIF_TEXT;
i.cchTextMax=sizeof(text);
i.pszText=text;
i.hItem=h;
TreeView_GetItem(htree,&i);
if (cnl)
{
if (!stricmp(text,cnl))
{
i.pszText=cnl;
i.lParam=time(NULL);
TreeView_SetItem(htree,&i);
added=1;
}
}
if (time(NULL) - (unsigned int)i.lParam > 4*60)
{
h=TreeView_GetNextSibling(htree,h);
chatroom_item *p=L_Chatroom;
while(p!=NULL)
{
if (!stricmp(p->channel,text)) break;
p=p->next;
}
if (!p) TreeView_DeleteItem(htree,i.hItem);
}
else h=TreeView_GetNextSibling(htree,h);
}
if (!added && cnl)
{
TVINSERTSTRUCT i;
i.hParent=TVI_ROOT;
i.hInsertAfter=TVI_SORT;
i.item.mask=TVIF_PARAM|TVIF_TEXT;
i.item.pszText=cnl;
i.item.lParam=time(NULL);
TreeView_InsertItem(htree,&i);
}
}
void main_onGotNick(char *nick, int del)
{
if (nick)
{
if (!stricmp(nick,g_regnick)) return;
KillTimer(g_mainwnd,5);
SetTimer(g_mainwnd,5,5000,0);
}
HWND htree=GetDlgItem(g_mainwnd,IDC_USERS);
HTREEITEM h=TreeView_GetChild(htree,TVI_ROOT);
while (h)
{
char text[512];
TVITEM i;
i.mask=TVIF_HANDLE|TVIF_PARAM|TVIF_TEXT;
text[0]=0;
i.cchTextMax=sizeof(text);
i.pszText=text;
i.hItem=h;
TreeView_GetItem(htree,&i);
if (nick)
{
if (!stricmp(nick,i.pszText))
{
if (del)
{
TreeView_DeleteItem(htree,h);
return;
}
i.mask=TVIF_HANDLE|TVIF_PARAM|TVIF_TEXT;
i.pszText=nick;
i.lParam=time(NULL);
TreeView_SetItem(htree,&i);
break;
}
h=TreeView_GetNextSibling(htree,h);
}
else
{
h=TreeView_GetNextSibling(htree,h);
if (time(NULL)-i.lParam > 4*60)
{
TreeView_DeleteItem(htree,i.hItem);
}
}
}
if (!h && nick)
{
TVINSERTSTRUCT i;
i.hParent=TVI_ROOT;
i.hInsertAfter=TVI_SORT;
i.item.mask=TVIF_PARAM|TVIF_TEXT;
i.item.pszText=nick;
i.item.lParam=time(NULL);
TreeView_InsertItem(htree,&i);
}
}
static int mainwnd_old_yoffs;
static ChildWndResizeItem mainwnd_rlist[]={
{IDC_USERSLABEL,0x0010},
{IDC_USERS,0x0011},
{IDC_DIVIDER,0x0111},
{IDC_CHATROOMSLABEL,0x0111},
{IDC_CHATROOMS,0x0111},
{IDC_CREATECHATROOM,0x1010},
{IDC_SEARCH,0x1010},
{IDC_NETSTATUS,0x0010},
};
//unfortunately this code is super super dumb. I actually did a much better
//version of it for gen_ml later on, but alas, too late now.
static void MainDiv_UpdPos(int yp)
{
RECT r,r2;
GetClientRect(g_mainwnd,&r);