forked from GerryFerdinandus/bittorrent-tracker-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.pas
More file actions
1467 lines (1192 loc) · 44.4 KB
/
main.pas
File metadata and controls
1467 lines (1192 loc) · 44.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
unit main;
{
Unicode:
variable 'Utf8string' is the same as 'string'
UTF8String = type ansistring;
All 'string' should be rename to 'UTF8String' to show the intention that we should
use UTF8 in the program.
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtCtrls, CheckLst, DecodeTorrent, LCLType, ActnList, Menus, ComCtrls,
Grids, controlergridtorrentdata;
type
{ TFormTrackerModify }
TFormTrackerModify = class(TForm)
CheckListBoxPublicPrivateTorrent: TCheckListBox;
CheckListBoxTrackersList: TCheckListBox;
GroupBoxTorrentContents: TGroupBox;
GroupBoxPublicPrivateTorrent: TGroupBox;
GroupBoxNewTracker: TGroupBox;
GroupBoxPresentTracker: TGroupBox;
MainMenu: TMainMenu;
MemoNewTrackers: TMemo;
MenuFile: TMenuItem;
MenuFileTorrentFolder: TMenuItem;
MenuFileOpenTrackerList: TMenuItem;
MenuHelpReportingIssue: TMenuItem;
MenuItemTorrentFilesTreeHideAll: TMenuItem;
MenuItemTorrentFilesTreeShowTrackers: TMenuItem;
MenuItemTorrentFilesTreeShowInfo: TMenuItem;
MenuItemTorrentFilesTreeShowAll: TMenuItem;
MenuItemTorrentFilesTreeShowFiles: TMenuItem;
MenuTrackersAllTorrentArePrivate: TMenuItem;
MenuTrackersAllTorrentArePublic: TMenuItem;
MenuUpdateTorrent: TMenuItem;
MenuHelp: TMenuItem;
MenuHelpVisitWebsite: TMenuItem;
MenuTrackersDeleteAllTrackers: TMenuItem;
MenuTrackersKeepAllTrackers: TMenuItem;
MenuTrackers: TMenuItem;
MenuOpenTorrentFile: TMenuItem;
OpenDialog: TOpenDialog;
PageControl: TPageControl;
PanelTopPublicTorrent: TPanel;
PanelTop: TPanel;
PopupMenuTorrentFilesContent: TPopupMenu;
SelectDirectoryDialog1: TSelectDirectoryDialog;
Splitter1: TSplitter;
StringGridTorrentData: TStringGrid;
TabSheetTorrentsContents: TTabSheet;
TabSheetTorrentData: TTabSheet;
TabSheetTrackersList: TTabSheet;
TabSheetPublicPrivateTorrent: TTabSheet;
TreeViewFileContents: TTreeView;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
//Drag and drop '*.torrent' files/directory or 'tracker.txt'
procedure FormDropFiles(Sender: TObject; const FileNames: array of UTF8String);
//At start of the program the form will be show/hide
procedure FormShow(Sender: TObject);
procedure MenuFileOpenTrackerListClick(Sender: TObject);
procedure MenuHelpReportingIssueClick(Sender: TObject);
procedure MenuHelpVisitWebsiteClick(Sender: TObject);
//Popup menu in treeview show all/hide all/ individual items selection.
procedure MenuItemTorrentFilesTreeShowAllClick(Sender: TObject);
procedure MenuItemTorrentFilesTreeHideAllClick(Sender: TObject);
procedure MenuItemTorrentFilesTreeShowOrHideItemClick(Sender: TObject);
//Select via menu torrent file or directory
procedure MenuOpenTorrentFileClick(Sender: TObject);
procedure MenuFileTorrentFolderClick(Sender: TObject);
//Menu trackers
procedure MenuTrackersAllTorrentArePublicPrivateClick(Sender: TObject);
procedure MenuTrackersKeepOrDeleteAllTrackersClick(Sender: TObject);
//Menu update torrent
procedure MenuUpdateTorrentClick(Sender: TObject);
private
{ private declarations }
FTrackerFinalList, //Trackers that must be put inside the torrent.
FTrackerAddedByUserList, //Trackers that we want too add.
FTrackerBanByUserList, //trackers that must not be present inside torrent.
FTrackerFromInsideTorrentFilesList, //Trackers that are already inside the torrent.
FTorrentFileNameList,// All the torrent files that must be updated
FLogStringList //Log string text output
: TStringList;
FDecodePresentTorrent: TDecodeTorrent; // is the present torrent file being process
FConcoleMode, //user have start the program in console mode
FFilePresentBanByUserList//There is a file 'remove_trackers.txt' detected
: boolean;
FLogFile, FTrackerFile: TextFile;
FTotalFileInsideTorrent: integer;
FTotalFileSizeInsideTorrent: int64;
FProcessTimeStart, FProcessTimeTotal: TDateTime;
FTreeNodeRoot: TTreeNode;
FControlerGridTorrentData: TControlerGridTorrentData;
function ByteSizeToBiggerSizeFormatStr(ByteSize: int64): string;
procedure ShowHourGlassCursor(HourGlass: boolean);
procedure ViewUpdateBegin(ClearView: boolean = True);
procedure ViewUpdateOneTorrentFileDecoded;
procedure ViewUpdateEnd;
procedure ViewUpdateFormCaption;
procedure ClearAllTorrentFilesNameAndTrackerInside;
procedure MenuItemTorrentFilesTreeSyncWithPopupMenu;
procedure SaveTrackerFinalListToFile;
procedure ConsoleMode;
procedure UpdateViewRemoveTracker;
procedure ReloadAllTorrentAndRefreshView;
function AddTorrentFileList(TorrentFileNameStringList: TStringList): boolean;
function ReadAddTrackerFileFromUser(const FileName: UTF8String): boolean;
function LoadTorrentViaDir(const Dir: UTF8String): boolean;
function DecodeTorrentFile(const FileName: UTF8String): boolean;
procedure UpdateTrackerInsideFileList;
procedure UpdateTorrentTrackerList;
procedure CombineThreeTrackerListToOne;
procedure ShowTrackerInsideFileList;
procedure CheckedOnOffAllTrackers(Value: boolean);
function CopyUserInputNewTrackersToList: boolean;
procedure LoadTrackersTextFileAddTrackers;
procedure LoadTrackersTextFileRemoveTrackers;
function ValidTrackerURL(const TrackerURL: UTF8String): boolean;
public
{ public declarations }
end;
var
FormTrackerModify: TFormTrackerModify;
implementation
uses LCLIntf, lazutf8;
const
RECOMENDED_TRACKERS: array[0..2] of UTF8String =
(
'udp://tracker.openbittorrent.com:80/announce',
'udp://tracker.publicbt.com:80/announce',
'udp://tracker.istole.it:80/announce'
// 'udp://open.demonii.com:1337/announce'
);
//program name and version
FORM_CAPTION = 'Bittorrent tracker editor (1.3x) BETA';
TORRENT_FILES_CONTENTS_FORM_CAPTION =
'Show all the files inside the torrents. (Use right mouse for popup menu.)';
//'add trackers' text file must be place in the same directory as the program.
ADD_TRACKERS_FILE_NAME = 'add_trackers.txt';
//'remove trackers' text file must be place in the same directory as the program.
REMOVE_TRACKERS_FILE_NAME = 'remove_trackers.txt';
//'export trackers' text file wil be created in the same directory as the program.
EXPORT_TRACKERS_FILE_NAME = 'export_trackers.txt';
//'log' text file will be saved in the same directory as the program
// only in the console mode.
LOG_FILE_NAME = 'console_log.txt';
{$R *.lfm}
{ TFormTrackerModify }
procedure TFormTrackerModify.FormCreate(Sender: TObject);
begin
Caption := FORM_CAPTION;
//Create controler for StringGridTorrentData
FControlerGridTorrentData := TControlerGridTorrentData.Create(StringGridTorrentData);
//Log file output string List.
FLogStringList := TStringList.Create;
//Create filename list for all the torrent files.
FTorrentFileNameList := TStringList.Create;
FTorrentFileNameList.Duplicates := dupIgnore;
//Must NOT be sorted. Must in sync with CheckListBoxPublicPrivateTorrent.
FTorrentFileNameList.Sorted := False;
//Create ban tracker list where the user can manualy add items to it.
FTrackerBanByUserList := TStringList.Create;
FTrackerBanByUserList.Duplicates := dupIgnore;
FTrackerBanByUserList.Sorted := False;
//Create tracker list where the user can manualy add items to it
FTrackerAddedByUserList := TStringList.Create;
FTrackerAddedByUserList.Duplicates := dupIgnore;
//must be sorted. is visible to user.
//drag and drop tracker list will accept duplicates in memo text, if false. Need to check out why.
FTrackerAddedByUserList.Sorted := True;
//Create tracker list where all the trackers from all the torrent files are collected
FTrackerFromInsideTorrentFilesList := TStringList.Create;
FTrackerFromInsideTorrentFilesList.Duplicates := dupIgnore;
//must be sorted. is visible to user. In tracker list tab page.
FTrackerFromInsideTorrentFilesList.Sorted := True;
//Create tracker list that combine FTrackerFromInsideTorrentFilesList + FTrackerAddedByUserList together.
FTrackerFinalList := TStringList.Create;
FTrackerFinalList.Duplicates := dupIgnore;
//must be sorted. because we want to insert it in torrent files.
FTrackerFinalList.Sorted := True;
//Decoding class for torrent.
FDecodePresentTorrent := TDecodeTorrent.Create;
//start the program at mimimum visual size. (this is optional)
Width := Constraints.MinWidth;
Height := Constraints.MinHeight;
//Show the default trackers
LoadTrackersTextFileAddTrackers;
//Load the unwanted trackers list.
LoadTrackersTextFileRemoveTrackers;
//Check is program is started as console
ConsoleMode;
GroupBoxTorrentContents.Caption := TORRENT_FILES_CONTENTS_FORM_CAPTION;
end;
procedure TFormTrackerModify.FormDestroy(Sender: TObject);
begin
//The program is being closed. Free all the memory.
FLogStringList.Free;
FTrackerFinalList.Free;
FDecodePresentTorrent.Free;
FTrackerAddedByUserList.Free;
FTrackerBanByUserList.Free;
FTrackerFromInsideTorrentFilesList.Free;
FTorrentFileNameList.Free;
FControlerGridTorrentData.Free;
end;
procedure TFormTrackerModify.MenuFileTorrentFolderClick(Sender: TObject);
begin
ClearAllTorrentFilesNameAndTrackerInside;
ViewUpdateBegin;
//User what to select one torrent file. Show the user dialog file selection.
SelectDirectoryDialog1.InitialDir := ExtractFilePath(Application.ExeName);
if SelectDirectoryDialog1.Execute then
begin
ShowHourGlassCursor(True);
LoadTorrentViaDir(SelectDirectoryDialog1.FileName);
ShowHourGlassCursor(False);
end;
ViewUpdateEnd;
end;
procedure TFormTrackerModify.MenuHelpVisitWebsiteClick(Sender: TObject);
begin
//There is no help file in this progam. Show user main web site.
OpenURL('http://code.google.com/p/bittorrent-tracker-editor');
end;
procedure TFormTrackerModify.MenuItemTorrentFilesTreeHideAllClick(Sender: TObject);
var
i, CountTorrents: integer;
begin
//Show only torrent file names
//user what to hide all the items.
//All the popup menu item must first be unchecked.
MenuItemTorrentFilesTreeShowInfo.Checked := False;
MenuItemTorrentFilesTreeShowFiles.Checked := False;
MenuItemTorrentFilesTreeShowTrackers.Checked := False;
//Update the TorrentFilesTree
// MenuItemTorrentFilesTreeSyncWithPopupMenu;
if not assigned(FTreeNodeRoot) then
exit;
//how many torrent files are there.
CountTorrents := FTreeNodeRoot.Count;
if CountTorrents = 0 then
exit;
//Show the torrent files names only.
for i := 0 to CountTorrents - 1 do
begin
FTreeNodeRoot.Items[i].Collapse(True);
end;
end;
procedure TFormTrackerModify.MenuItemTorrentFilesTreeShowAllClick(Sender: TObject);
begin
//show everything
if assigned(FTreeNodeRoot) then
FTreeNodeRoot.Expand(True);
//user what to see all the items.
//All the popup menu item must first be checked.
MenuItemTorrentFilesTreeShowInfo.Checked := True;
MenuItemTorrentFilesTreeShowFiles.Checked := True;
MenuItemTorrentFilesTreeShowTrackers.Checked := True;
//Update the TorrentFilesTree
// MenuItemTorrentFilesTreeSyncWithPopupMenu;
end;
procedure TFormTrackerModify.MenuItemTorrentFilesTreeShowOrHideItemClick(
Sender: TObject);
var
i, CountTorrents, itemsNr: integer;
ShowNode: boolean;
begin
//Show or hide all the items below the torrent files.
//Get the top node.
if not assigned(FTreeNodeRoot) then
exit;
//how many torrent files are there.
CountTorrents := FTreeNodeRoot.Count;
if CountTorrents = 0 then
exit;
//The tag number define if it is for files, trackers or info items
itemsNr := TMenuItem(Sender).tag;
//Must show or hide the items
ShowNode := TMenuItem(Sender).Checked;
//process all the torrent files one by one.
for i := 0 to CountTorrents - 1 do
begin
if ShowNode then
begin
FTreeNodeRoot.Items[i].Expand(False); //Show the torrent name + child
FTreeNodeRoot.Items[i].Items[itemsNr].Expand(False); //expand child
end
else
begin
FTreeNodeRoot.Items[i].Items[itemsNr].Collapse(False);
end;
end;
end;
procedure TFormTrackerModify.MenuUpdateTorrentClick(Sender: TObject);
var
Reply, BoxStyle, i, CountTrackers: integer;
PopUpMenuStr: string;
begin
//Update the all the torrent files.
//The StringGridTorrentData where the comment are place by user
// must be in sync again with FTorrentFileNameList.
//Undo all posible sort column used by the user. Sort it back to 'begin state'
FControlerGridTorrentData.ReorderGrid;
try
if not FConcoleMode then
begin
//Warn user before updating the torrent
BoxStyle := MB_ICONWARNING + MB_OKCANCEL;
Reply := Application.MessageBox('Warning: There is no undo.',
'Torrent files will be change!', BoxStyle);
if Reply <> idOk then
begin
ShowHourGlassCursor(True);
exit;
end;
end;
//Must have some torrent selected
if (FTorrentFileNameList.Count = 0) then
begin
if FConcoleMode then
begin
FLogStringList.Add('ERROR: No torrent file selected');
end
else
begin
Application.MessageBox('No torrent file selected',
'', MB_ICONERROR);
end;
ShowHourGlassCursor(True);
exit;
end;
//User must wait for a while.
ShowHourGlassCursor(True);
//Copy the tracker list inside torrent -> FTrackerFromInsideTorrentFilesList
UpdateTrackerInsideFileList;
//Check for error in user tracker list -> FTrackerAddedByUserList
if not CopyUserInputNewTrackersToList then
Exit;
//There are 3 list that must be combine
//FTrackerFinalList := FTrackerAddedByUserList + FTrackerFromInsideTorrentFilesList
// - FTrackerBanByUserList
CombineThreeTrackerListToOne;
//In console mode we can ignore this warning
if not FConcoleMode and (FTrackerFinalList.Count = 0) then
begin //Torrent without a tracker is posible. But is this what the user realy want? a DHT torrent.
BoxStyle := MB_ICONWARNING + MB_OKCANCEL;
Reply := Application.MessageBox(
'Warning: Create torrent file without any URL of the tracker?',
'There are no Trackers selected!', BoxStyle);
if Reply <> idOk then
begin
ShowHourGlassCursor(False);
exit;
end;
//Reset process timer
ShowHourGlassCursor(True);
end;
//How many trackers must be put inside each torrent file
CountTrackers := FTrackerFinalList.Count;
//process all the files one by one.
//FTorrentFileNameList is not sorted it is still in sync with CheckListBoxPublicPrivateTorrent
for i := 0 to FTorrentFileNameList.Count - 1 do
begin //read the torrent file in FDecodePresentTorrent and modify it.
//read one torrent file. If error then skip it. (continue)
if not FDecodePresentTorrent.DecodeTorrent(FTorrentFileNameList[i]) then
Continue;
case CountTrackers of
0://if no tracker selected then delete 'announce' and 'announce-list'
begin
FDecodePresentTorrent.RemoveAnnounce;
FDecodePresentTorrent.RemoveAnnounceList;
end;
1://if one tracker selected then delete 'announce-list'
begin
//Announce use the only tracker present in the FTrackerFinalList. index 0
FDecodePresentTorrent.ChangeAnnounce(FTrackerFinalList[0]);
FDecodePresentTorrent.RemoveAnnounceList;
end;
else//More than 1 trackers selected. Create 'announce-list'
begin
//Announce use the first tracker from the list. index 0
FDecodePresentTorrent.ChangeAnnounce(FTrackerFinalList[0]);
FDecodePresentTorrent.ChangeAnnounceList(FTrackerFinalList);
end;
end;
//update the torrent public/private flag
if CheckListBoxPublicPrivateTorrent.Checked[i] then
begin
//if private torrent then make it public torrent by removing the private flag.
if FDecodePresentTorrent.PrivateTorrent then
FDecodePresentTorrent.RemovePrivateTorrentFlag;
end
else
begin
FDecodePresentTorrent.AddPrivateTorrentFlag;
end;
//update the comment item
FDecodePresentTorrent.Comment := FControlerGridTorrentData.ReadComment(i + 1);
//save the torrent file.
FDecodePresentTorrent.SaveTorrent(FTorrentFileNameList[i]);
end;
//Create tracker.txt file
SaveTrackerFinalListToFile;
//Show/reload the just updated torrent files.
ReloadAllTorrentAndRefreshView;
//make sure cursor is default again
finally
ShowHourGlassCursor(False);
ViewUpdateFormCaption;
end;
if FConcoleMode then
begin
//When succesfull the log file shows, 3 lines,
// OK + Count torrent files + Count Trackers
FLogStringList.Add('OK');
FLogStringList.Add(IntToStr(FTorrentFileNameList.Count));
FLogStringList.Add(IntToStr(CountTrackers));
end
else
begin
//Via popup show user how many trackers are inside the torrent after update.
PopUpMenuStr := 'All torrent file(s) have now ' + IntToStr(CountTrackers) +
' trackers.';
Application.MessageBox(
PChar(@PopUpMenuStr[1]),
'', MB_ICONINFORMATION + MB_OK);
end;
end;
function TFormTrackerModify.ByteSizeToBiggerSizeFormatStr(ByteSize: int64): string;
begin
if ByteSize >= (1024 * 1024 * 1024) then
Result := Format('%0.2f GiB', [ByteSize / (1024 * 1024 * 1024)])
else
if ByteSize >= (1024 * 1024) then
Result := Format('%0.2f MiB', [ByteSize / (1024 * 1024)])
else
if ByteSize >= (1024) then
Result := Format('%0.2f KiB', [ByteSize / 1024]);
Result := Result + Format(' (%d Bytes)', [ByteSize]);
end;
procedure TFormTrackerModify.MenuItemTorrentFilesTreeSyncWithPopupMenu;
begin
MenuItemTorrentFilesTreeShowOrHideItemClick(MenuItemTorrentFilesTreeShowTrackers);
MenuItemTorrentFilesTreeShowOrHideItemClick(MenuItemTorrentFilesTreeShowInfo);
MenuItemTorrentFilesTreeShowOrHideItemClick(MenuItemTorrentFilesTreeShowFiles);
end;
procedure TFormTrackerModify.SaveTrackerFinalListToFile;
var
TrackerStr: UTF8String;
begin
//Create the tracker text file. The old one will be overwritten
AssignFile(FTrackerFile, ExtractFilePath(Application.ExeName) +
EXPORT_TRACKERS_FILE_NAME);
ReWrite(FTrackerFile);
for TrackerStr in FTrackerFinalList do
begin
WriteLn(FTrackerFile, TrackerStr);
//Must create an empty line betwean trackers.
//Every tracker must be a seperate tracker group.
//This is what the user probably want.
//The file content can then be copy/pasted to uTorrent etc.
WriteLn(FTrackerFile, '');
end;
CloseFile(FTrackerFile);
end;
procedure TFormTrackerModify.ConsoleMode;
var
FileNameOrDirStr: UTF8String;
StringList: TStringList;
begin
//if program is started with one parameter then in must be stated console mode.
//This parameter is path to file or dir.
//update the torrent via console mode if there is a parameter detected.
if ParamCount > 0 then
begin
FConcoleMode := True;
//Create the log file. The old one will be overwritten
AssignFile(FLogFile, ExtractFilePath(Application.ExeName) + LOG_FILE_NAME);
ReWrite(FLogFile);
//Get the first parameter.
FileNameOrDirStr := UTF8Trim(ParamStr(1));
//If FLogStringList empty then there is no error.
if FLogStringList.Text = '' then
begin
//There is no error. Proceed with reading the torrent files
if ExtractFileExt(FileNameOrDirStr) = '' then
begin //There is no file extention. It must be a folder.
if LoadTorrentViaDir(FileNameOrDirStr) then
begin
//Show all the tracker inside the torrent files.
ShowTrackerInsideFileList;
//Mark all trackers as selected
CheckedOnOffAllTrackers(True);
//Some tracker must be removed. Console and windows mode.
UpdateViewRemoveTracker;
//update torrent
MenuUpdateTorrentClick(self);
end;
end
else //a torrent file is selected?
begin
if ExtractFileExt(FileNameOrDirStr) = '.torrent' then
begin
StringList := TStringList.Create;
try
//Convert Filenames to stringlist format.
StringList.Add(FileNameOrDirStr);
AddTorrentFileList(StringList);
//Show all the tracker inside the torrent files.
ShowTrackerInsideFileList;
//Mark all trackers as selected
CheckedOnOffAllTrackers(True);
//Some tracker must be removed. Console and windows mode.
UpdateViewRemoveTracker;
//update torrent
MenuUpdateTorrentClick(self);
finally
StringList.Free;
end;
end
else
begin //Error. this is not a torrent file
FLogStringList.Add('ERROR: No torrent file selected.');
end;
end;
end;
//Write to log file. And close the file.
WriteLn(FLogFile, FLogStringList.Text);
CloseFile(FLogFile);
//Shutdown the console program
Application.terminate;
end
else
begin //the program
FConcoleMode := False;
end;
end;
procedure TFormTrackerModify.UpdateViewRemoveTracker;
var
TrackerStr: UTF8String;
i: integer;
begin
{
Called when user load the torrent files.
Trackers that are forbidden must be uncheck.
Trackers add by user in the memo text filed must also be removed.
This routine is also use in the console mode to remove trackers
}
//If file remove_trackers.txt is present but empty then remove all tracker inside torrent.
if FFilePresentBanByUserList and (UTF8Trim(FTrackerBanByUserList.Text) = '') then
begin
CheckedOnOffAllTrackers(False);
end;
//reload the memo. This will sanitize the MemoNewTrackers.Lines.
if not CopyUserInputNewTrackersToList then
exit;
//remove all the trackers that are ban.
MemoNewTrackers.Lines.BeginUpdate;
for TrackerStr in FTrackerBanByUserList do
begin
//uncheck tracker that are listed in FTrackerBanByUserList
i := CheckListBoxTrackersList.Items.IndexOf(UTF8Trim(TrackerStr));
if i >= 0 then //Found it.
begin
CheckListBoxTrackersList.Checked[i] := False;
end;
//remove tracker from user memo text that are listed in FTrackerBanByUserList
//Find TrackerStr in MemoNewTrackers.Lines and remove it.
i := MemoNewTrackers.Lines.IndexOf(UTF8Trim(TrackerStr));
if i >= 0 then //Found it.
begin
MemoNewTrackers.Lines.Delete(i);
end;
end;
MemoNewTrackers.Lines.EndUpdate;
//reload the memo again.
CopyUserInputNewTrackersToList;
end;
function TFormTrackerModify.DecodeTorrentFile(const FileName: UTF8String): boolean;
begin
//Called when user add torrent files
//False if something is wrong with decoding torrent.
Result := FDecodePresentTorrent.DecodeTorrent(FileName);
ViewUpdateOneTorrentFileDecoded;
end;
procedure TFormTrackerModify.UpdateTorrentTrackerList;
var
TrackerStr: UTF8String;
begin
//Copy the trackers found in one torrent file to FTrackerFromInsideTorrentFilesList
for TrackerStr in FDecodePresentTorrent.TrackerList do
FTrackerFromInsideTorrentFilesList.Add(TrackerStr);
end;
procedure TFormTrackerModify.ShowTrackerInsideFileList;
var
TrackerStr: UTF8String;
begin
//Called after torrent is being loaded.
CheckListBoxTrackersList.Items.BeginUpdate;
//remove the previeus list
CheckListBoxTrackersList.Clear;
//Add new items to the list.
for TrackerStr in FTrackerFromInsideTorrentFilesList do
begin
CheckListBoxTrackersList.Items.Add(TrackerStr);
end;
CheckListBoxTrackersList.Items.EndUpdate;
end;
procedure TFormTrackerModify.CheckedOnOffAllTrackers(Value: boolean);
var
i: integer;
begin
//Set all the trackers checkbox ON or OFF
with CheckListBoxTrackersList do
if Count > 0 then
for i := 0 to Count - 1 do
Checked[i] := Value;
end;
function TFormTrackerModify.ValidTrackerURL(const TrackerURL: UTF8String): boolean;
begin
//TrackerURL should be cleanup with UTF8trim()
Result := (Pos('http://', TrackerURL) = 1) or (Pos('udp://', TrackerURL) = 1);
end;
function TFormTrackerModify.CopyUserInputNewTrackersToList: boolean;
var
TrackerStr: UTF8String;
begin
{
Called after 'update torrent' is selected.
All the user entery from Memo text field will be add to FTrackerAddedByUserList.
}
FTrackerAddedByUserList.Clear;
for TrackerStr in MemoNewTrackers.Lines do
begin
TrackerStr := UTF8trim(TrackerStr);
//Skip empty line
if TrackerStr = '' then
continue;
//All the tracker must begin with 'http://' or 'udp://'
if ValidTrackerURL(TrackerStr) then
begin
FTrackerAddedByUserList.Add(TrackerStr);
end
else
begin
//There is error. Show the error and do not continue.
if FConcoleMode then
begin
FLogStringList.Add('ERROR: Tracker URL must begin with http:// or udp://');
end
else
begin
//Show error
Application.MessageBox(PChar(@TrackerStr[1]),
'Error: Tracker URL must begin with http:// or udp://', MB_ICONERROR);
end;
//dot not continue with error.
Result := False;
exit;
end;
end;
Result := True; //no error
//Show the torrent list we have just created.
MemoNewTrackers.Text := FTrackerAddedByUserList.Text;
end;
procedure TFormTrackerModify.CombineThreeTrackerListToOne;
var
TrackerStr: UTF8String;
i: integer;
begin
// FTrackerFinalList = (FTrackerAddedByUserList + FTrackerFromInsideTorrentFilesList)
// - FTrackerBanByUserList
FTrackerFinalList.Clear;
for TrackerStr in FTrackerAddedByUserList do
FTrackerFinalList.Add(TrackerStr);
for TrackerStr in FTrackerFromInsideTorrentFilesList do
FTrackerFinalList.Add(TrackerStr);
//Remove the trackers must be the last step.
for TrackerStr in FTrackerBanByUserList do
begin
//Find the tracker and remove it from the list.
//FTrackerBanByUserList is not UTF8Trim() before. Must use with UTF8Trim()
i := FTrackerFinalList.IndexOf(UTF8Trim(TrackerStr));
if i >= 0 then
FTrackerFinalList.Delete(i);
end;
end;
procedure TFormTrackerModify.UpdateTrackerInsideFileList;
var
i: integer;
begin
//Copy items from CheckListBoxTrackersList to FTrackerFromInsideTorrentFilesList
FTrackerFromInsideTorrentFilesList.Clear;
with CheckListBoxTrackersList do
if Count > 0 then
for i := 0 to Count - 1 do
if Checked[i] then
FTrackerFromInsideTorrentFilesList.add(Items[i]);
end;
procedure TFormTrackerModify.LoadTrackersTextFileAddTrackers;
var
i: integer;
begin
//Called at the start of the program. Load a trackers list from file
//if no file is found the use the default tracker list.
if not ReadAddTrackerFileFromUser(ExtractFilePath(Application.ExeName) +
ADD_TRACKERS_FILE_NAME) then
begin
MemoNewTrackers.Lines.BeginUpdate;
for i := low(RECOMENDED_TRACKERS) to high(RECOMENDED_TRACKERS) do
begin
MemoNewTrackers.Lines.Add(RECOMENDED_TRACKERS[i]);
end;
MemoNewTrackers.Lines.EndUpdate;
end;
//Check for error in tracker list
if not CopyUserInputNewTrackersToList then
begin
MemoNewTrackers.Lines.Clear;
end;
end;
procedure TFormTrackerModify.LoadTrackersTextFileRemoveTrackers;
var
filename: UTF8String;
begin
filename := ExtractFilePath(Application.ExeName) + REMOVE_TRACKERS_FILE_NAME;
try
FFilePresentBanByUserList := FileExistsUTF8(fileName);
if FFilePresentBanByUserList then
begin
FTrackerBanByUserList.LoadFromFile(fileName);
end;
except
FFilePresentBanByUserList := False;
end;
end;
procedure TFormTrackerModify.MenuOpenTorrentFileClick(Sender: TObject);
var
StringList: TStringList;
begin
ClearAllTorrentFilesNameAndTrackerInside;
ViewUpdateBegin;
//User what to select a torrent file. Show the user dialog.
OpenDialog.Title := 'Select a torrent file';
OpenDialog.Filter := 'torrent|*.torrent';
if OpenDialog.Execute then
begin
ShowHourGlassCursor(True);
StringList := TStringList.Create;
try
StringList.Add(UTF8Trim(OpenDialog.FileName));
AddTorrentFileList(StringList);
finally
StringList.Free;
ShowHourGlassCursor(False);
end;
end;
ViewUpdateEnd;
end;
procedure TFormTrackerModify.MenuTrackersAllTorrentArePublicPrivateClick(
Sender: TObject);
var
i: integer;
begin
//Warn user about torrent Hash.
if Application.MessageBox(
'Warning: Changing the public/private torrent flag will change the info hash.',
'Are you sure!', MB_ICONWARNING + MB_OKCANCEL) <> idOk then
exit;
//Set all the trackers publick/private checkbox ON or OFF
with CheckListBoxPublicPrivateTorrent do
if Count > 0 then
for i := 0 to Count - 1 do
Checked[i] := TMenuItem(Sender).Tag = 1;
end;
procedure TFormTrackerModify.MenuFileOpenTrackerListClick(Sender: TObject);
begin
//Clear the present list
MemoNewTrackers.Lines.Clear;
//User what to select a tracker file. Show the user dialog.
OpenDialog.Title := 'Select a tracker list file';
OpenDialog.Filter := 'tracker text file|*.txt';
if OpenDialog.Execute then
begin
ReadAddTrackerFileFromUser(OpenDialog.FileName);
end;
end;
procedure TFormTrackerModify.MenuHelpReportingIssueClick(Sender: TObject);
begin
OpenURL('http://code.google.com/p/bittorrent-tracker-editor/issues');
end;
function TFormTrackerModify.ReadAddTrackerFileFromUser(
const FileName: UTF8String): boolean;
var
TrackerFileList: TStringList;
begin
//read the file and show it to the user.
TrackerFileList := TStringList.Create;
try
TrackerFileList.LoadFromFile(FileName);
MemoNewTrackers.Text := UTF8Trim(TrackerFileList.Text);
Result := True;
except
Result := False;
//suppres all error in reading the file.
end;
TrackerFileList.Free;
// It can be simpler, but does this suport UTF8?
// MemoNewTrackers.Lines.LoadFromFile(FileName);
end;
procedure TFormTrackerModify.MenuTrackersKeepOrDeleteAllTrackersClick(Sender: TObject);
begin
CheckedOnOffAllTrackers(TMenuItem(Sender).Tag = 1);
end;
function TFormTrackerModify.LoadTorrentViaDir(const Dir: UTF8String): boolean;
var
Info: TSearchRec;
TorrentFilesNameStringList: TStringList;
begin
//place all the torrent file name in TorrentFilesNameStringList
TorrentFilesNameStringList := TStringList.Create;
try
if FindFirstUTF8(dir + PathDelim + '*.torrent', faAnyFile, Info) = 0 then
begin
//Read all the torrent files inside this dir.
repeat
TorrentFilesNameStringList.Add(UTF8Trim(dir + PathDelim + Info.Name));
until FindNextUTF8(info) <> 0;