-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconfiguration.pas
160 lines (131 loc) · 3.71 KB
/
configuration.pas
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
unit configuration;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, IniFiles;
type
TUserStatus = (usAdmin, usSimple, usBanned); // in future it can increase enums
{ TConfig }
TConfig = class
private
FIni: TMemIniFile;
FUsers: TStringList;
function GetAPIEndPoint: String;
function GetAPITimeout: Integer;
function GetBotTooken: String;
function GetDefaultDir: String;
function GetHTTPProxyHost: String;
function GetHTTPProxyPort: Word;
function GetHTTPProxyPswd: String;
function GetHTTPProxyUser: String;
function GetScriptsDirectory: String;
function GetServiceUser: Int64;
function GetUsers(UserID: Int64): TUserStatus;
function GetUserList: TStrings;
public
constructor Create(const AConfFile: String);
destructor Destroy; override;
property ServiceUser: Int64 read GetServiceUser;
property APIEndPoint: String read GetAPIEndPoint;
property BotTooken: String read GetBotTooken;
property Users[UserID: Int64]: TUserStatus read GetUsers;
property APITimeout: Integer read GetAPITimeout; // longpolling timeout
property HTTPProxyHost: String read GetHTTPProxyHost;
property HTTPProxyPort: Word read GetHTTPProxyPort;
property HTTPProxyUser: String read GetHTTPProxyUser;
property HTTPProxyPswd: String read GetHTTPProxyPswd;
property ScriptsDirectory: String read GetScriptsDirectory;
property DefaultDir: String read GetDefaultDir;
end;
var
Cnfg: TConfig;
implementation
uses
tgsendertypes
;
var
CnfDir: String;
{ TConfig }
function TConfig.GetAPIEndPoint: String;
begin
Result:=FIni.ReadString('API', 'Endpoint', TelegramAPI_URL);
end;
function TConfig.GetAPITimeout: Integer;
begin
Result:=FIni.ReadInteger('API', 'Timeout', 20); // 20 sec for longpolling request ??
end;
function TConfig.GetBotTooken: String;
begin
Result:=FIni.ReadString('API', 'Token', EmptyStr)
end;
function TConfig.GetDefaultDir: String;
begin
Result:=FIni.ReadString('File', 'DefaultDir', PathDelim);
end;
function TConfig.GetHTTPProxyHost: String;
begin
Result:=FIni.ReadString('Proxy', 'Host', EmptyStr);
end;
function TConfig.GetHTTPProxyPort: Word;
begin
Result:=FIni.ReadInteger('Proxy', 'Port', 3128);
end;
function TConfig.GetHTTPProxyPswd: String;
begin
Result:=FIni.ReadString('Proxy', 'Password', EmptyStr);
end;
function TConfig.GetHTTPProxyUser: String;
begin
Result:=FIni.ReadString('Proxy', 'Username', EmptyStr);
end;
function TConfig.GetScriptsDirectory: String;
begin
Result:=IncludeTrailingPathDelimiter(FIni.ReadString('Scripts', 'Directory', CnfDir));
end;
function TConfig.GetServiceUser: Int64;
begin
Result:=FIni.ReadInt64('Service', 'User', 0);
end;
function TConfig.GetUsers(UserID: Int64): TUserStatus;
var
s: String;
begin
s:=GetUserList.Values[IntToStr(UserID)];
if s=EmptyStr then
Exit(usSimple);
case s[1] of
'a': Result:=usAdmin;
'b': Result:=usBanned;
's': Result:=usSimple;
else
Result:=usSimple;
end;
end;
function TConfig.GetUserList: TStrings;
begin
if not Assigned(FUsers) then
begin
FUsers:=TStringList.Create;
FIni.ReadSectionRaw('users', FUsers);
FUsers.Sorted:=True;
end;
Result:=FUsers;
end;
constructor TConfig.Create(const AConfFile: String);
begin
FIni:=TMemIniFile.Create(AConfFile);
end;
destructor TConfig.Destroy;
begin
FUsers.Free;
FIni.Free;
inherited Destroy;
end;
initialization{$ifdef portable}
CnfDir:=IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0)));
Cnfg:=TConfig.Create(CnfDir+ChangeFileExt(ExtractFileName(ParamStr(0)), '.ini'));{$else}
CnfDir:=GetAppConfigDir(True);
Cnfg:=TConfig.Create(CnfDir+ChangeFileExt(ExtractFileName(ParamStr(0)), '.ini'));{$endif}
finalization
FreeAndNil(Cnfg);
end.