-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbip0122handler.lpr
287 lines (254 loc) · 8.72 KB
/
bip0122handler.lpr
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
program bip0122handler;
{$ifdef fpc}{$mode delphi}{$endif}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, CustApp,
Generics.Collections, Bip0122Uriparser, Bip0122Processor, Explorersprocessor,
UnitTranslator, lclintf, LazFileUtils;
type
TOptionsDictionary = TDictionary<string, string>;
{ TBIP0122Application }
TBIP0122Application = class(TCustomApplication)
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
private
Options: TOptionsDictionary;
procedure CreateOptions;
function GetShortOptionsList: String;
function GetLongOptionsList: TStringArray;
procedure Process(SettingExplorerNames: TStrings; BipProcessor: TBIP0122Processor; const BipUri: TBIP0122URI);
procedure WriteHelpPleaseSetExplorers(ExplorerNames: TStrings);
end;
const
ExplorersJsonFileName = 'block-explorers.json';
ShortOptionHelp = 'h';
ShortOptionSetExplorers = 's';
ShortOptionGetExplorers = 'g';
ShortOptionClearExplorers = 'x';
ShortOptionLang = 'l';
resourcestring
SExplorersNotSet = 'Block explorer(s) not yet set, cannot continue';
SExplorersSet = 'Block explorer(s) set to the following:';
SExplorersCleared = 'Block explorers cleared. You may set them again with %s';
SHelpDisplayThisHelp = 'display this help and exit';
SHelpGetExplorers = 'gets the list of currently used block explorers';
SHelpParamOption = '[option]';
SHelpParamURI = '[URI]';
SHelpSetExplorers = 'sets comma-separated list of used explorers. ' + LineEnding + 'Use names from %s';
SHelpClearExplorers = 'clear all used block explorers';
SHelpUsage = 'Usage: ';
SHelpWhereOptionIs = 'where [option] is one of the following:';
SHelpWhereURIIs = 'where [URI] is a BIP0122 URI, or';
SPleaseSetExplorers = 'Please set your desired block explorer(s) by using -' + ShortOptionSetExplorers + ' "comma-separated list containing selection of explorers below"';
SUnhandledException = 'Unhandled %s exception: %s';
SWarningUnknownExplorers = 'Warning: unknown explorers specified. Please use names from %s';
{ TBIP0122Application }
procedure TBIP0122Application.DoRun;
var
ErrorMsg: String;
NonOptions: TStringArray;
NonOption: String;
ShortOptions: String;
LongOptions: TStringArray;
BipUri: TBIP0122URI = nil;
BipProcessor: TBIP0122Processor;
ExplorersProcessor: TExplorersProcessor;
ExplorerStorage: TRegistryExplorerStorage;
SoftwareName: String;
OptionExplorers: String = '';
ShowHelp: Boolean = false;
RegistryKey: String;
begin
{$if declared(useHeapTrace)}
SetHeapTraceOutput('heaptrace.log');
globalSkipIfNoLeaks := true;
{$endIf}
ShortOptions := GetShortOptionsList();
LongOptions := GetLongOptionsList();
// quick check parameters
ErrorMsg := CheckOptions(ShortOptions, LongOptions);
if ErrorMsg<>'' then
begin
Writeln(stderr, ErrorMsg);
Terminate;
Exit;
end;
// parse parameters
if HasOption(ShortOptionHelp, Options[ShortOptionHelp]) then
begin
WriteHelp;
Terminate;
Exit;
end;
{ add your program here }
SoftwareName := ExtractFileName(ExtractFileNameWithoutExt(ExeName));
RegistryKey := '\SOFTWARE\' + SoftwareName;
try
BipProcessor := TBip0122Processor.Create([
ExplorersJsonFileName,
GetAppConfigDir(true) + ExplorersJsonFileName,
GetAppConfigDir(false) + ExplorersJsonFileName
]);
ExplorerStorage := TRegistryExplorerStorage.Create(RegistryKey);
try
ExplorersProcessor := TExplorersProcessor.Create(BipProcessor.GetExplorerNames, ExplorerStorage);
if HasOption(ShortOptionSetExplorers, Options[ShortOptionSetExplorers]) then
begin
OptionExplorers := GetOptionValue(ShortOptionSetExplorers, Options[ShortOptionSetExplorers]);
ExplorersProcessor.SetExplorers(OptionExplorers);
if ExplorersProcessor.ListOfUnknownExplorers.Count > 0 then
begin
writeln(Format(SWarningUnknownExplorers, [ExplorersJsonFileName]),
LineEnding, ExplorersProcessor.ListOfUnknownExplorers.CommaText);
end;
if ExplorersProcessor.ListOfKnownExplorers.Count > 0 then
begin
writeln(SExplorersSet, LineEnding, ExplorersProcessor.ListOfKnownExplorers.CommaText);
end
else
begin
WriteHelpPleaseSetExplorers(ExplorersProcessor.ListOfAllExplorers);
end;
end
else if HasOption(ShortOptionClearExplorers, Options[ShortOptionClearExplorers]) then
begin
ExplorersProcessor.SetExplorers('');
writeln(Format(SExplorersCleared, ['-' + ShortOptionSetExplorers]));
end
else
begin
ExplorersProcessor.GetExplorers;
if ExplorersProcessor.ListOfKnownExplorers.DelimitedText = '' then
begin
writeln(SExplorersNotSet);
WriteHelpPleaseSetExplorers(ExplorersProcessor.ListOfAllExplorers);
end
else if HasOption(ShortOptionGetExplorers, Options[ShortOptionGetExplorers]) then
begin
writeln(ExplorersProcessor.ListOfKnownExplorers.CommaText);
end
else
begin
if ExplorersProcessor.ListOfUnknownExplorers.Count > 0 then
begin
writeln(Format(SWarningUnknownExplorers, [ExplorersJsonFileName]),
LineEnding, ExplorersProcessor.ListOfUnknownExplorers.CommaText);
end;
NonOptions := GetNonOptions(ShortOptions, LongOptions);
ShowHelp := True;
if Length(NonOptions) > 0 then
begin
for NonOption in NonOptions do
begin
if TBIP0122URI.TryParse(NonOption, BipUri) then
begin
ShowHelp := False;
Process(ExplorersProcessor.ListOfKnownExplorers, BipProcessor, BipUri);
BipUri.Free;
break;
end
end
end;
if ShowHelp then
WriteHelp;
end
end
finally
FreeAndNil(ExplorersProcessor);
FreeAndNil(BipProcessor);
end;
except
on E: Exception do
begin
Writeln(stderr, Format(SUnhandledException, [E.ClassName, E.Message]));
end;
end;
Terminate;
Exit;
end;
constructor TBIP0122Application.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException:=True;
CreateOptions;
end;
destructor TBIP0122Application.Destroy;
begin
FreeAndNil(Options);
inherited Destroy;
end;
procedure TBIP0122Application.WriteHelp;
begin
writeln(SHelpUsage + ExtractFileName(ExeName) + ' ' + SHelpParamOption + ' ' + SHelpParamURI);
writeln;
writeln(SHelpWhereURIIs);
writeln(SHelpWhereOptionIs);
writeln(' -' + ShortOptionHelp, ' --' + Options[ShortOptionHelp], #9, #9, SHelpDisplayThisHelp);
writeln(' -' + ShortOptionSetExplorers, ' --' + Options[ShortOptionSetExplorers], #9, StringReplace(Trim(Format(SHelpSetExplorers, [ExplorersJsonFileName])), LineEnding, LineEnding + #9#9#9, [rfReplaceAll]));
writeln(' -' + ShortOptionGetExplorers, ' --' + Options[ShortOptionGetExplorers], #9, SHelpGetExplorers);
writeln(' -' + ShortOptionClearExplorers, ' --' + Options[ShortOptionClearExplorers], #9, SHelpClearExplorers);
end;
procedure TBIP0122Application.WriteHelpPleaseSetExplorers(ExplorerNames: TStrings);
var
ExplorerName: String;
begin
writeln(SPleaseSetExplorers);
for ExplorerName in ExplorerNames do
writeln(ExplorerName);
end;
function TBIP0122Application.GetShortOptionsList: String;
begin
Result := string.Join('', Options.Keys.ToArray);
end;
procedure TBIP0122Application.CreateOptions;
begin
Options := TOptionsDictionary.Create;
Options.Add(ShortOptionHelp, 'help');
Options.Add(ShortOptionSetExplorers, 'setexplorers');
Options.Add(ShortOptionGetExplorers, 'getexplorers');
Options.Add(ShortOptionClearExplorers, 'clearexplorers');
Options.Add(ShortOptionLang, 'lang');
end;
function TBIP0122Application.GetLongOptionsList: TStringArray;
var
Value: String;
i: Integer;
begin
i := 0;
Result := TStringArray.Create;
SetLength(Result, Options.Values.Count);
for Value in Options.Values do
begin
Result[i] := Value;
i := i + 1;
end;
end;
procedure TBIP0122Application.Process(SettingExplorerNames: TStrings; BipProcessor: TBIP0122Processor; const BipUri: TBIP0122URI);
var
ResultUrl: String;
ExplorerName: String;
begin
for ExplorerName in SettingExplorerNames do
begin
ResultUrl := BipProcessor.GetUrlForBipUri(ExplorerName, BipUri);
if ResultUrl <> '' then
begin
OpenUrl(ResultUrl);
end;
end;
end;
var
Application: TBIP0122Application;
{$R *.res}
begin
Application := TBIP0122Application.Create(nil);
Application.Title:='BIP-0122 handler';
Application.Run;
Application.Free;
end.