Skip to content

Commit 57ad1ec

Browse files
committed
feat: Configuration implemented
1 parent ad2fb96 commit 57ad1ec

File tree

5 files changed

+163
-24
lines changed

5 files changed

+163
-24
lines changed

pasirclogbot.example.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Main]
2+
Host="localhost"
3+
Port=6667
4+
NickName="[PasLogBot]"
5+
UserName="paslogbot"
6+
RealName="IRC channel #pascal log bot"
7+
Channel="#pascal"
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
unit Bot;
1+
unit IRCLogBot.Bot;
22

33
{$mode ObjFPC}{$H+}
44

@@ -35,8 +35,8 @@ TIRCLogBot = class(TObject)
3535
ATarget, AMessage: String);
3636
protected
3737
public
38-
constructor Create(ANickName, AUserName, ARealName, AHost: String;
39-
APort: Word; AChannel: String);
38+
constructor Create(AHost: String; APort: Word;
39+
ANickName, AUserName, ARealName, AChannel: String);
4040
destructor Destroy; override;
4141

4242
procedure Run;
@@ -124,12 +124,12 @@ procedure TIRCLogBot.Shutdown;
124124
if FIRC.Connected then
125125
begin
126126
WriteLn('Disconnecting...');
127-
FIRC.Disconnect('Quitting.');
127+
FIRC.Disconnect('Need to go and have a wee nap.');
128128
end;
129129
end;
130130

131-
constructor TIRCLogBot.Create(ANickName, AUserName, ARealName, AHost: String;
132-
APort: Word; AChannel: String);
131+
constructor TIRCLogBot.Create(AHost: String; APort: Word; ANickName, AUserName,
132+
ARealName, AChannel: String);
133133
begin
134134
FNickname:= ANickname;
135135
FUsername:= AUserName;

src/config/irclogbot.config.pas

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
unit IRCLogBot.Config;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses
8+
Classes
9+
, SysUtils
10+
, IniFiles
11+
;
12+
13+
type
14+
{ TBotConfig }
15+
TBotConfig = class(TObject)
16+
private
17+
FINIFile: String;
18+
FINI: TIniFile;
19+
FHost: String;
20+
FPort: Word;
21+
FNickName: String;
22+
FUserName: String;
23+
FRealName: String;
24+
FChannel: String;
25+
protected
26+
public
27+
constructor Create(AConfigFile: String);
28+
destructor Destroy; override;
29+
30+
procedure LoadValues;
31+
32+
property Host: String
33+
read FHost
34+
write FHost;
35+
property Port: Word
36+
read FPort
37+
write FPort;
38+
property NickName: String
39+
read FNickName
40+
write FNickName;
41+
property UserName: String
42+
read FUserName
43+
write FUserName;
44+
property RealName: String
45+
read FRealName
46+
write FRealName;
47+
property Channel: String
48+
read FChannel
49+
write FChannel;
50+
published
51+
end;
52+
53+
implementation
54+
55+
{ TBotConfig }
56+
57+
constructor TBotConfig.Create(AConfigFile: String);
58+
begin
59+
FINIFile:= AConfigFile;
60+
FHost:= 'localhost';
61+
FPort:= 6667;
62+
FNickName:= '';
63+
FUserName:= '';
64+
FRealName:= '';
65+
FChannel:= '';
66+
end;
67+
68+
destructor TBotConfig.Destroy;
69+
begin
70+
inherited Destroy;
71+
end;
72+
73+
procedure TBotConfig.LoadValues;
74+
begin
75+
if FileExists(FINIFile) then
76+
begin
77+
FINI:= TIniFile.Create(FINIFile);
78+
FHost:= FINI.ReadString('Main', 'Host', 'localhost');
79+
FPort:= FINI.ReadInteger('Main', 'Port', 6667);
80+
FNickName:= FINI.ReadString('Main', 'NickName', '');
81+
FUserName:= FINI.ReadString('Main', 'UserName', '');
82+
FRealName:= FINI.ReadString('Main', 'RealName', '');
83+
FChannel:= FINI.ReadString('Main', 'Channel', '');
84+
end
85+
else
86+
begin
87+
raise EFileNotFoundException.Create(Format('Cannot find file "%s".', [FINIFile]));
88+
end;
89+
end;
90+
91+
end.
92+

src/paslogbot.lpi

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</Target>
2424
<SearchPaths>
2525
<IncludeFiles Value="$(ProjOutDir)"/>
26-
<OtherUnitFiles Value="bot"/>
26+
<OtherUnitFiles Value="bot;config"/>
2727
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
2828
</SearchPaths>
2929
<Parsing>
@@ -57,7 +57,7 @@
5757
</Target>
5858
<SearchPaths>
5959
<IncludeFiles Value="$(ProjOutDir)"/>
60-
<OtherUnitFiles Value="bot"/>
60+
<OtherUnitFiles Value="bot;config"/>
6161
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
6262
</SearchPaths>
6363
<CodeGeneration>
@@ -95,9 +95,14 @@
9595
<IsPartOfProject Value="True"/>
9696
</Unit>
9797
<Unit>
98-
<Filename Value="bot/bot.pas"/>
98+
<Filename Value="bot/irclogbot.bot.pas"/>
9999
<IsPartOfProject Value="True"/>
100-
<UnitName Value="Bot"/>
100+
<UnitName Value="IRCLogBot.Bot"/>
101+
</Unit>
102+
<Unit>
103+
<Filename Value="config/irclogbot.config.pas"/>
104+
<IsPartOfProject Value="True"/>
105+
<UnitName Value="IRCLogBot.Config"/>
101106
</Unit>
102107
</Units>
103108
</ProjectOptions>
@@ -108,7 +113,7 @@
108113
</Target>
109114
<SearchPaths>
110115
<IncludeFiles Value="$(ProjOutDir)"/>
111-
<OtherUnitFiles Value="bot"/>
116+
<OtherUnitFiles Value="bot;config"/>
112117
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
113118
</SearchPaths>
114119
</CompilerOptions>

src/paslogbot.lpr

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
uses
66
{$IFDEF UNIX}
7-
cthreads,
7+
cThreads,
88
BaseUnix,
99
{$ENDIF}
10-
Classes, SysUtils, CustApp, Bot
10+
Classes, SysUtils, CustApp, IRCLogBot.Bot, IRCLogBot.Config
1111
{ you can add units after this };
1212

1313
type
@@ -16,6 +16,8 @@
1616
TPasLogBot = class(TCustomApplication)
1717
private
1818
FIRCLogBot: TIRCLogBot;
19+
FConfigFile: String;
20+
FBotConfig: TBotConfig;
1921
protected
2022
procedure DoRun; override;
2123
public
@@ -93,7 +95,7 @@ procedure TPasLogBot.DoRun;
9395
// Signal Handling
9496
SetupSignalHandlers;
9597
// quick check parameters
96-
ErrorMsg:= CheckOptions('h', 'help');
98+
ErrorMsg:= CheckOptions('hc:', ['help', 'config:']);
9799
if ErrorMsg<>'' then
98100
begin
99101
//ShowException(Exception.Create(ErrorMsg));
@@ -109,13 +111,50 @@ procedure TPasLogBot.DoRun;
109111
exit;
110112
end;
111113

114+
if HasOption('c', 'config') then
115+
begin
116+
FConfigFile:= GetOptionValue('c', 'config');
117+
end
118+
else
119+
begin
120+
FConfigFile:= ConcatPaths([GetUserDir, '.pasirclogbot']);
121+
end;
122+
123+
WriteLn(Format('Attempting to read config from: "%s"...', [FConfigFile]));
124+
125+
{ #todo 100 -ogcarreno : Parse param c/config }
126+
FBotConfig:= TBotConfig.Create(FConfigFile);
127+
try
128+
FBotConfig.LoadValues;
129+
except
130+
on e:Exception do
131+
begin
132+
WriteLn(Format('Error: %s', [e.Message]));
133+
Terminate;
134+
exit;
135+
end;
136+
end;
137+
138+
{ #todo 100 -ogcarreno : Use data from config }
139+
WriteLn('Creating IRC client...');
140+
FIRCLogBot:= TIRCLogBot.Create(
141+
FBotConfig.Host,
142+
FBotConfig.Port,
143+
FBotConfig.NickName,
144+
FBotConfig.UserName,
145+
FBotConfig.Realname,
146+
FBotConfig.Channel
147+
);
148+
WriteLn('Successfully created IRC client.');
112149
WriteLn('Starting...');
150+
{ #todo 100 -ogcarreno : Read Config }
113151
FIRCLogBot.Run;
114152
while not Terminated do
115153
begin
116154
Sleep(50);
117155
end;
118156
FIRCLogBot.Shutdown;
157+
FIRCLogBot.Free;
119158
WriteLn('Exiting.');
120159
// stop program loop
121160
//Terminate;
@@ -125,26 +164,22 @@ constructor TPasLogBot.Create(TheOwner: TComponent);
125164
begin
126165
inherited Create(TheOwner);
127166
StopOnException:= True;
128-
FIRCLogBot:= TIRCLogBot.Create(
129-
'[PasLogBot]',
130-
'paslogbot',
131-
'Pascal channel IRC log bot',
132-
'localhost',
133-
6667,
134-
'#test'
135-
);
136167
end;
137168

138169
destructor TPasLogBot.Destroy;
139170
begin
140-
FIRCLogBot.Free;
141171
inherited Destroy;
142172
end;
143173

144174
procedure TPasLogBot.WriteHelp;
145175
begin
146176
{ add your help code here }
147-
WriteLn('Usage: ', ExtractFileName(ExeName), ' -h');
177+
WriteLn('Usage:');
178+
WriteLn(' ', ExtractFileName(ExeName), ' [PARAMS]');
179+
WriteLn;
180+
WriteLn('PARAMS:');
181+
WriteLn(' -h/--help This help message.');
182+
WriteLn(' -c/--config=FILE Use provided FILE as config. ( default: ~/.pasirclogbot )');
148183
end;
149184

150185
begin

0 commit comments

Comments
 (0)