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
1659 lines (1377 loc) · 50.1 KB
/
main.pas
File metadata and controls
1659 lines (1377 loc) · 50.1 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
{ MIT licence
Copyright (c) Gerry Ferdinandus
}
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, Forms, Controls, Graphics, Dialogs, StdCtrls,
ExtCtrls, CheckLst, DecodeTorrent, LCLType, ActnList, Menus, ComCtrls,
Grids, controlergridtorrentdata, torrent_miscellaneous,
controler_trackerlist_online, controler_treeview_torrent_data;
type
{ TFormTrackerModify }
TFormTrackerModify = class(TForm)
CheckListBoxPublicPrivateTorrent: TCheckListBox;
GroupBoxPublicPrivateTorrent: TGroupBox;
GroupBoxNewTracker: TGroupBox;
GroupBoxPresentTracker: TGroupBox;
MainMenu: TMainMenu;
MemoNewTrackers: TMemo;
MenuFile: TMenuItem;
MenuFileTorrentFolder: TMenuItem;
MenuFileOpenTrackerList: TMenuItem;
MenuHelpReportingIssue: TMenuItem;
MenuHelpSeperator1: TMenuItem;
MenuHelpVisitNewTrackon: TMenuItem;
MenuItemOnlineCheckSubmitNewTrackon: TMenuItem;
MenuItemOnlineCheckAppendStableTrackers: TMenuItem;
MenuTrackersDeleteDeadTrackers: TMenuItem;
MenuTrackersDeleteUnstableTrackers: TMenuItem;
MenuTrackersDeleteUnknownTrackers: TMenuItem;
MenuTrackersSeperator2: TMenuItem;
MenuTrackersSeperator1: TMenuItem;
MenuItemOnlineCheckDownloadNewTrackon: TMenuItem;
MenuOnlineCheck: TMenuItem;
MenuUpdateRandomize: TMenuItem;
MenuUpdateTorrentAddBeforeKeepOriginalInstactAndRemoveNothing: TMenuItem;
MenuUpdateTorrentAddAfterKeepOriginalInstactAndRemoveNothing: TMenuItem;
MenuUpdateTorrentAddBeforeRemoveOriginal: TMenuItem;
MenuUpdateTorrentAddAfterRemoveOriginal: TMenuItem;
MenuUpdateTorrentAddBeforeRemoveNew: TMenuItem;
MenuUpdateTorrentAddAfterRemoveNew: TMenuItem;
MenuUpdateTorrentSort: TMenuItem;
MenuUpdateTorrentAddAfter: TMenuItem;
MenuUpdateTorrentAddBefore: 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;
SelectDirectoryDialog1: TSelectDirectoryDialog;
Splitter1: TSplitter;
StringGridTrackerOnline: TStringGrid;
StringGridTorrentData: TStringGrid;
TabSheetTorrentsContents: TTabSheet;
TabSheetTorrentData: TTabSheet;
TabSheetTrackersList: TTabSheet;
TabSheetPublicPrivateTorrent: TTabSheet;
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 MenuHelpReportingIssueClick(Sender: TObject);
procedure MenuHelpVisitNewTrackonClick(Sender: TObject);
procedure MenuHelpVisitWebsiteClick(Sender: TObject);
procedure MenuItemOnlineCheckSubmitNewTrackonClick(Sender: TObject);
//Select via menu torrent file or directory
procedure MenuOpenTorrentFileClick(Sender: TObject);
procedure MenuFileTorrentFolderClick(Sender: TObject);
procedure MenuFileOpenTrackerListClick(Sender: TObject);
//Menu trackers
procedure MenuTrackersAllTorrentArePublicPrivateClick(Sender: TObject);
procedure MenuTrackersKeepOrDeleteAllTrackersClick(Sender: TObject);
procedure MenuTrackersDeleteTrackersWithStatusClick(Sender: TObject);
//Menu update torrent
procedure MenuUpdateTorrentAddAfterRemoveNewClick(Sender: TObject);
procedure MenuUpdateTorrentAddAfterRemoveOriginalClick(Sender: TObject);
procedure MenuUpdateTorrentAddBeforeKeepOriginalInstactAndRemoveNothingClick(
Sender: TObject);
procedure MenuUpdateTorrentAddBeforeRemoveNewClick(Sender: TObject);
procedure MenuUpdateTorrentAddBeforeRemoveOriginalClick(Sender: TObject);
procedure MenuUpdateTorrentSortClick(Sender: TObject);
procedure MenuUpdateTorrentAddAfterKeepOriginalInstactAndRemoveNothingClick(
Sender: TObject);
procedure MenuUpdateRandomizeClick(Sender: TObject);
//Menu online check
procedure MenuItemOnlineCheckAppendStableTrackersClick(Sender: TObject);
procedure MenuItemOnlineCheckDownloadNewTrackonClick(Sender: TObject);
private
{ private declarations }
FTrackerList: TTrackerList;
FControlerTrackerListOnline: TControlerTrackerListOnline;
Fcontroler_treeview_torrent_data: Tcontroler_treeview_torrent_data;
FDownloadStatus: boolean;
// is the present torrent file being process
FDecodePresentTorrent: TDecodeTorrent;
FConsoleMode, //user have start the program in console mode
FFilePresentBanByUserList//There is a file 'remove_trackers.txt' detected
: boolean;
FLogFile, FTrackerFile: TextFile;
FProcessTimeStart, FProcessTimeTotal: TDateTime;
FControlerGridTorrentData: TControlerGridTorrentData;
procedure ShowUserErrorMessage(const ErrorText: string; const FormText: string = '');
function TrackerWithURLAndAnnounce(const TrackerURL: UTF8String): boolean;
procedure UpdateTorrent;
procedure ShowHourGlassCursor(HourGlass: boolean);
procedure ViewUpdateBegin;
procedure ViewUpdateOneTorrentFileDecoded;
procedure ViewUpdateEnd;
procedure ViewUpdateFormCaption;
procedure ClearAllTorrentFilesNameAndTrackerInside;
procedure SaveTrackerFinalListToFile;
procedure ConsoleMode;
procedure UpdateViewRemoveTracker;
function ReloadAllTorrentAndRefreshView: boolean;
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 ShowTrackerInsideFileList;
procedure CheckedOnOffAllTrackers(Value: boolean);
function CopyUserInputNewTrackersToList: boolean;
procedure LoadTrackersTextFileAddTrackers;
procedure LoadTrackersTextFileRemoveTrackers;
public
{ public declarations }
end;
var
FormTrackerModify: TFormTrackerModify;
implementation
uses LCLIntf, lazutf8, LazFileUtils, trackerlist_online;
const
RECOMENDED_TRACKERS: array[0..3] of UTF8String =
(
'udp://tracker.coppersurfer.tk:6969/announce',
'udp://tracker.leechers-paradise.org:6969/announce',
'udp://tracker.opentrackr.org:1337/announce',
'wss://tracker.openwebtorrent.com'
);
//program name and version (http://semver.org/)
FORM_CAPTION = 'Bittorrent tracker editor (1.33.0.beta.5)';
GROUPBOX_PRESENT_TRACKERS_CAPTION =
'Present trackers in all torrent files. Select the one that you want to keep. And added to all torrent files.';
{$R *.lfm}
{ TFormTrackerModify }
procedure TFormTrackerModify.FormCreate(Sender: TObject);
begin
//Update some captions
Caption := FORM_CAPTION;
GroupBoxPresentTracker.Caption := GROUPBOX_PRESENT_TRACKERS_CAPTION;
//Create controler for StringGridTorrentData
FControlerGridTorrentData := TControlerGridTorrentData.Create(StringGridTorrentData);
//Log file output string List.
FTrackerList.LogStringList := TStringList.Create;
//Create filename list for all the torrent files.
FTrackerList.TorrentFileNameList := TStringList.Create;
FTrackerList.TorrentFileNameList.Duplicates := dupIgnore;
//Must NOT be sorted. Must in sync with CheckListBoxPublicPrivateTorrent.
FTrackerList.TorrentFileNameList.Sorted := False;
//Create ban tracker list where the user can manualy add items to it.
FTrackerList.TrackerBanByUserList := TStringList.Create;
FTrackerList.TrackerBanByUserList.Duplicates := dupIgnore;
FTrackerList.TrackerBanByUserList.Sorted := False;
//Create deselect tracker list where the user select via user interface checkbox
FTrackerList.TrackerManualyDeselectedByUserList := TStringList.Create;
FTrackerList.TrackerManualyDeselectedByUserList.Duplicates := dupIgnore;
FTrackerList.TrackerManualyDeselectedByUserList.Sorted := False;
//Create tracker list where the user can manualy add items to it
FTrackerList.TrackerAddedByUserList := TStringList.Create;
FTrackerList.TrackerAddedByUserList.Duplicates := dupIgnore;
//Trackers List added by user must keep in the same order.
FTrackerList.TrackerAddedByUserList.Sorted := False;
//drag and drop tracker list will accept duplicates in memo text, if false. Need to check out why.
//Create tracker list where all the trackers from all the torrent files are collected
FTrackerList.TrackerFromInsideTorrentFilesList := TStringList.Create;
FTrackerList.TrackerFromInsideTorrentFilesList.Duplicates := dupIgnore;
//Must be sorted. is visible to user. In tracker list tab page.
FTrackerList.TrackerFromInsideTorrentFilesList.Sorted := True;
//Create tracker list that combine all other together.
FTrackerList.TrackerFinalList := TStringList.Create;
FTrackerList.TrackerFinalList.Duplicates := dupIgnore;
//must NOT be sorted. Must keep the original order intact.
FTrackerList.TrackerFinalList.Sorted := False;
//Decoding class for torrent.
FDecodePresentTorrent := TDecodeTorrent.Create;
//Create view for trackerURL with checkbox
FControlerTrackerListOnline :=
TControlerTrackerListOnline.Create(StringGridTrackerOnline,
FTrackerList.TrackerFromInsideTorrentFilesList, @TrackerWithURLAndAnnounce);
//Create view for treeview data of all the torrent files
Fcontroler_treeview_torrent_data :=
Tcontroler_treeview_torrent_data.Create(TabSheetTorrentsContents);
//start the program at mimimum visual size. (this is optional)
Width := Constraints.MinWidth;
Height := Constraints.MinHeight;
//there must be two command line or more for a console mode.
//one is for drag and drop via shortcut in windows mode.
FConsoleMode := ParamCount >= 2;
//Show the default trackers
LoadTrackersTextFileAddTrackers;
//Load the unwanted trackers list.
LoadTrackersTextFileRemoveTrackers;
//Start program in console mode ( >= 2)
//or in windows mode via shortcut with drag/drop ( = 1)
if ParamCount > 0 then
begin
ConsoleMode;
end;
end;
procedure TFormTrackerModify.FormDestroy(Sender: TObject);
begin
//The program is being closed. Free all the memory.
FTrackerList.LogStringList.Free;
FTrackerList.TrackerFinalList.Free;
FDecodePresentTorrent.Free;
FTrackerList.TrackerAddedByUserList.Free;
FTrackerList.TrackerBanByUserList.Free;
FTrackerList.TrackerFromInsideTorrentFilesList.Free;
FTrackerList.TorrentFileNameList.Free;
FControlerGridTorrentData.Free;
FTrackerList.TrackerManualyDeselectedByUserList.Free;
FControlerTrackerListOnline.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('https://github.com/GerryFerdinandus/bittorrent-tracker-editor');
end;
procedure TFormTrackerModify.MenuItemOnlineCheckSubmitNewTrackonClick(Sender: TObject);
var
SendStatus: boolean;
TrackerSendCount: integer;
PopupStr: string;
begin
try
screen.Cursor := crHourGlass;
SendStatus := FControlerTrackerListOnline.SubmitTrackers(
FTrackerList.TrackerFromInsideTorrentFilesList, TrackerSendCount);
finally
screen.Cursor := crDefault;
end;
if SendStatus then
begin
//Succesful upload
PopupStr := format('Successful upload of %d unique tracker URL', [TrackerSendCount]);
Application.MessageBox(
PChar(@PopupStr[1]),
'', MB_ICONINFORMATION + MB_OK);
end
else
begin
//something is wrong with uploading
ShowUserErrorMessage('Can not uploading the tracker list');
end;
end;
procedure TFormTrackerModify.MenuItemOnlineCheckAppendStableTrackersClick(
Sender: TObject);
var
tracker: UTF8String;
begin
//User want to use the downloaded tracker list.
//check if tracker is already downloaded
if not FDownloadStatus then
begin
//Download it now.
MenuItemOnlineCheckDownloadNewTrackonClick(nil);
end;
//Append all the trackers to MemoNewTrackers
MemoNewTrackers.Lines.BeginUpdate;
for Tracker in FControlerTrackerListOnline.StableTrackers do
begin
MemoNewTrackers.Lines.Add(tracker);
end;
MemoNewTrackers.Lines.EndUpdate;
//Check for error in tracker list
if not CopyUserInputNewTrackersToList then
begin
MemoNewTrackers.Lines.Clear;
end;
end;
procedure TFormTrackerModify.MenuItemOnlineCheckDownloadNewTrackonClick(
Sender: TObject);
begin
try
screen.Cursor := crHourGlass;
FDownloadStatus := FControlerTrackerListOnline.DownloadTrackers_All_Live_Stable;
finally
screen.Cursor := crDefault;
end;
if not FDownloadStatus then
begin
//something is wrong with downloading
ShowUserErrorMessage('Can not downloading the trackers from internet');
end;
end;
procedure TFormTrackerModify.ShowUserErrorMessage(const ErrorText: string;
const FormText: string);
begin
if FConsoleMode then
begin
if FormText = '' then
FTrackerList.LogStringList.Add(ErrorText)
else
FTrackerList.LogStringList.Add(FormText + ' : ' + ErrorText);
end
else
begin
if FormText = '' then
Application.MessageBox(PChar(@ErrorText[1]), '', MB_ICONERROR)
else
Application.MessageBox(PChar(@ErrorText[1]), PChar(@FormText[1]), MB_ICONERROR);
end;
end;
function TFormTrackerModify.TrackerWithURLAndAnnounce(
const TrackerURL: UTF8String): boolean;
begin
Result := ValidTrackerURL(TrackerURL);
if Result then
begin
//Web Torrent does not have 'announce'
if not WebTorrentTrackerURL(TrackerURL) then
begin
Result := TrackerURLWithAnnounce(TrackerURL);
end;
end;
end;
procedure TFormTrackerModify.MenuTrackersDeleteTrackersWithStatusClick(
Sender: TObject);
procedure UncheckTrackers(Value: TTrackerListOnlineStatus);
var
i: integer;
begin
if FControlerTrackerListOnline.Count > 0 then
begin
for i := 0 to FControlerTrackerListOnline.Count - 1 do
begin
if FControlerTrackerListOnline.TrackerStatus(i) = Value then
begin
FControlerTrackerListOnline.Checked[i] := False;
end;
end;
end;
end;
begin
//check if tracker is already downloaded
if not FDownloadStatus then
begin
MenuItemOnlineCheckDownloadNewTrackonClick(nil);
end;
//0 = Unstable
//1 = Dead
//2 = Unknown
case TMenuItem(Sender).Tag of
0: UncheckTrackers(tos_live_but_unstable);
1: UncheckTrackers(tos_dead);
2: UncheckTrackers(tos_unknown);
else
Assert(True, 'Unknown Menu item selection')
end;
end;
procedure TFormTrackerModify.MenuUpdateRandomizeClick(Sender: TObject);
begin
//User can select to randomize the tracker list
FTrackerList.TrackerListOrderForUpdatedTorrent := tloRandomize;
UpdateTorrent;
end;
procedure TFormTrackerModify.UpdateTorrent;
var
Reply, BoxStyle, i, CountTrackers: integer;
PopUpMenuStr: string;
SomeFilesCannotBeWriten, SomeFilesAreReadOnly, AllFilesAreReadBackCorrectly: boolean;
begin
//Update all the torrent files.
//The StringGridTorrentData where the comment are place by user
// must be in sync again with FTrackerList.TorrentFileNameList.
//Undo all posible sort column used by the user. Sort it back to 'begin state'
FControlerGridTorrentData.ReorderGrid;
//initial value is false, will be set to true if some file fails to write
SomeFilesCannotBeWriten := False;
try
if not FConsoleMode 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 (FTrackerList.TorrentFileNameList.Count = 0) then
begin
ShowUserErrorMessage('ERROR: No torrent file selected');
ShowHourGlassCursor(True);
exit;
end;
//User must wait for a while.
ShowHourGlassCursor(True);
//Copy the tracker list inside torrent -> FTrackerList.TrackerFromInsideTorrentFilesList
UpdateTrackerInsideFileList;
//Check for error in user tracker list -> FTrackerList.TrackerAddedByUserList
if not CopyUserInputNewTrackersToList then
Exit;
//There are 5 list that must be combine.
//Must use 'sort' for correct FTrackerFinalList.Count
CombineFiveTrackerListToOne(tloSort, FTrackerList,
FDecodePresentTorrent.TrackerList);
//How many trackers must be put inside each torrent file.
CountTrackers := FTrackerList.TrackerFinalList.Count;
//In console mode we can ignore this warning
if not FConsoleMode and (CountTrackers = 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;
//initial value is false, will be set to true if read only files are found
SomeFilesAreReadOnly := False;
if FTrackerList.TrackerListOrderForUpdatedTorrent = tloRandomize then
begin
Randomize;
end;
//process all the files one by one.
//FTrackerList.TorrentFileNameList is not sorted it is still in sync with CheckListBoxPublicPrivateTorrent
for i := 0 to FTrackerList.TorrentFileNameList.Count - 1 do
begin //read the torrent file in FDecodePresentTorrent and modify it.
//check for read only files. It can not be updated by tracker editor
if (FileGetAttr(FTrackerList.TorrentFileNameList[i]) and faReadOnly) <> 0 then
begin
SomeFilesAreReadOnly := True;
Continue;
end;
//read one torrent file. If error then skip it. (continue)
if not FDecodePresentTorrent.DecodeTorrent(
FTrackerList.TorrentFileNameList[i]) then
begin
Continue;
end;
//tloSort it is already process. But if not tloSort then process it.
if FTrackerList.TrackerListOrderForUpdatedTorrent <> tloSort then
begin
//Add the new tracker before of after the original trackers inside the torrent.
CombineFiveTrackerListToOne(FTrackerList.TrackerListOrderForUpdatedTorrent,
FTrackerList, FDecodePresentTorrent.TrackerList);
//How many trackers must be put inside each torrent file
CountTrackers := FTrackerList.TrackerFinalList.Count;
end;
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(FTrackerList.TrackerFinalList[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(FTrackerList.TrackerFinalList[0]);
FDecodePresentTorrent.ChangeAnnounceList(FTrackerList.TrackerFinalList);
end;
end;
//update the torrent public/private flag
if CheckListBoxPublicPrivateTorrent.Checked[i] then
begin
//Create a public torrent
//if private torrent then make it public torrent by removing the private flag.
if FDecodePresentTorrent.PrivateTorrent then
FDecodePresentTorrent.RemovePrivateTorrentFlag;
end
else
begin
//Create a private torrent
FDecodePresentTorrent.AddPrivateTorrentFlag;
end;
//update the comment item
FDecodePresentTorrent.Comment := FControlerGridTorrentData.ReadComment(i + 1);
//save the torrent file.
if not FDecodePresentTorrent.SaveTorrent(FTrackerList.TorrentFileNameList[i]) then
begin
SomeFilesCannotBeWriten := True;
end;
end;//for
//Create tracker.txt file
SaveTrackerFinalListToFile;
//Show/reload the just updated torrent files.
AllFilesAreReadBackCorrectly := ReloadAllTorrentAndRefreshView;
//make sure cursor is default again
finally
ShowHourGlassCursor(False);
ViewUpdateFormCaption;
end;
if FConsoleMode then
begin
//When succesfull the log file shows, 3 lines,
// OK + Count torrent files + Count Trackers
//if there is already a items inside there there must be something wrong.
//Do not add 'OK'
if FTrackerList.LogStringList.Count = 0 then
begin
FTrackerList.LogStringList.Add(CONSOLE_SUCCESS_STATUS);
FTrackerList.LogStringList.Add(IntToStr(FTrackerList.TorrentFileNameList.Count));
FTrackerList.LogStringList.Add(IntToStr(CountTrackers));
end;
end
else
begin
case FTrackerList.TrackerListOrderForUpdatedTorrent of
tloInsertNewBeforeAndKeepNewIntact,
tloInsertNewBeforeAndKeepOriginalIntact,
tloAppendNewAfterAndKeepNewIntact,
tloAppendNewAfterAndKeepOriginalIntact,
tloSort,
tloRandomize:
begin
//Via popup show user how many trackers are inside the torrent after update.
PopUpMenuStr := 'All torrent file(s) have now ' + IntToStr(CountTrackers) +
' trackers.';
end;
tloInsertNewBeforeAndKeepOriginalIntactAndRemoveNothing,
tloAppendNewAfterAndKeepOriginalIntactAndRemoveNothing:
begin
//Via popup show user that all the torrent files are updated.
PopUpMenuStr := 'All torrent file(s) are updated.';
end;
else
begin
Assert(True, 'case else: Should never been called. UpdateTorrent');
end;
end;//case
//Check if there are some error that need to be notify to the end user.
if not AllFilesAreReadBackCorrectly then
begin
//add warning if torrent files can not be read back again
PopUpMenuStr := PopUpMenuStr +
' WARNING: Some torrent files can not be read back again after updating.';
end;
if SomeFilesAreReadOnly then
begin
//add warning if read only files are detected.
PopUpMenuStr := PopUpMenuStr +
' WARNING: Some torrent files are not updated bacause they are READ-ONLY files.';
end;
if SomeFilesCannotBeWriten then
begin
//add warning if some files writen are failed. Someting is wrong with the disk.
PopUpMenuStr := PopUpMenuStr +
' WARNING: Some torrent files are not updated bacause they failed at write.';
end;
//Show the MessageBox
Application.MessageBox(
PChar(@PopUpMenuStr[1]),
'', MB_ICONINFORMATION + MB_OK);
end;
end;
procedure TFormTrackerModify.SaveTrackerFinalListToFile;
var
TrackerStr: UTF8String;
begin
//Create the tracker text file. The old one will be overwritten
AssignFile(FTrackerFile, ExtractFilePath(Application.ExeName) +
FILE_NAME_EXPORT_TRACKERS);
ReWrite(FTrackerFile);
for TrackerStr in FTrackerList.TrackerFinalList 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;
MustExitWithErrorCode: boolean;
begin
// There are two options
//-
// One parameter only
// This is the first tracker-editor version with only 'sort' trackers list.
// The first parameter[1] is path to file or dir.
//-
// Two parameter version
// This is later version where there is more selection about the tracker list.
//Will be set to True when error occure.
MustExitWithErrorCode := False;
ViewUpdateBegin;
try
if FConsoleMode then
begin
//Create the log file. The old one will be overwritten
AssignFile(FLogFile, ExtractFilePath(Application.ExeName) + FILE_NAME_CONSOLE_LOG);
ReWrite(FLogFile);
end;
//Get the startup command lime parameters.
if ConsoleModeDecodeParameter(FileNameOrDirStr, FTrackerList) 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;
//Some tracker must be removed. Console and windows mode.
UpdateViewRemoveTracker;
if FConsoleMode then
begin
//update torrent
UpdateTorrent;
end;
end
else
begin
//failed to load the torrent via folders
ShowUserErrorMessage('Can not load torrent via folder');
end;
end
else //a single torrent file is selected?
begin
if ExtractFileExt(FileNameOrDirStr) = '.torrent' then
begin
StringList := TStringList.Create;
try
//Convert Filenames to stringlist format.
StringList.Add(FileNameOrDirStr);
//Extract all the trackers inside the torrent file
if AddTorrentFileList(StringList) then
begin
//Show all the tracker inside the torrent files.
ShowTrackerInsideFileList;
//Some tracker must be removed. Console and windows mode.
UpdateViewRemoveTracker;
if FConsoleMode then
begin
//update torrent
UpdateTorrent;
end;
end
else
begin
//failed to load one torrent
ShowUserErrorMessage('Can not load torrent file.');
end;
finally
StringList.Free;
end;
end
else
begin //Error. this is not a torrent file
ShowUserErrorMessage('ERROR: No torrent file selected.');
end;
end;
end;
if FConsoleMode then
begin
//Write to log file. And close the file.
WriteLn(FLogFile, FTrackerList.LogStringList.Text);
CloseFile(FLogFile);
//check if log data is success full
//if (no data) or (not CONSOLE_SUCCESS_STATUS) then error
MustExitWithErrorCode := FTrackerList.LogStringList.Count = 0;
if not MustExitWithErrorCode then
begin
MustExitWithErrorCode := FTrackerList.LogStringList[0] <> CONSOLE_SUCCESS_STATUS;
end;
end;
except
//Shutdown the console program.
//This is needed or else the program will keep running forever.
//exit with error code
MustExitWithErrorCode := True;
end;
ViewUpdateEnd;
if MustExitWithErrorCode then
begin
//exit with error code
System.ExitCode := 1;
end;
if FConsoleMode then
begin
//Always shutdown the program when in console mode.
Application.terminate;
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(FTrackerList.TrackerBanByUserList.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 FTrackerList.TrackerBanByUserList do
begin
//uncheck tracker that are listed in FTrackerList.TrackerBanByUserList
//the FTrackerList.TrackerFromInsideTorrentFilesList is use in the view
i := FTrackerList.TrackerFromInsideTorrentFilesList.IndexOf(UTF8Trim(TrackerStr));
if i >= 0 then //Found it.
begin
FControlerTrackerListOnline.Checked[i] := False;
end;
//remove tracker from user memo text that are listed in FTrackerList.TrackerBanByUserList
//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);
if Result then
begin
//visual update this one torrent file.
ViewUpdateOneTorrentFileDecoded;
end;
end;
procedure TFormTrackerModify.UpdateTorrentTrackerList;
var
TrackerStr: UTF8String;
begin
//Copy the trackers found in one torrent file to FTrackerList.TrackerFromInsideTorrentFilesList
for TrackerStr in FDecodePresentTorrent.TrackerList do
begin
AddButIngnoreDuplicates(FTrackerList.TrackerFromInsideTorrentFilesList, TrackerStr);
end;
end;
procedure TFormTrackerModify.ShowTrackerInsideFileList;
begin
//Called after torrent is being loaded.
FControlerTrackerListOnline.UpdateView;
end;
procedure TFormTrackerModify.CheckedOnOffAllTrackers(Value: boolean);
var
i: integer;
begin
//Set all the trackers checkbox ON or OFF
if FControlerTrackerListOnline.Count > 0 then
begin
for i := 0 to FControlerTrackerListOnline.Count - 1 do
begin
FControlerTrackerListOnline.Checked[i] := Value;
end;
end;
end;
function TFormTrackerModify.CopyUserInputNewTrackersToList: boolean;
var
TrackerStrLoop, TrackerStr, ErrorStr: UTF8String;
begin
{
Called after 'update torrent' is selected.
All the user entery from Memo text field will be add to FTrackerList.TrackerAddedByUserList.
}
FTrackerList.TrackerAddedByUserList.Clear;
//Will set to false when error is detected
Result := True;
for TrackerStrLoop in MemoNewTrackers.Lines do
begin
TrackerStr := UTF8trim(TrackerStrLoop);
//Skip empty line
if TrackerStr = '' then
continue;
Result := ValidTrackerURL(TrackerStr);
if Result then
begin