Skip to content

Commit 972242b

Browse files
committed
refactor: Bundle types
1 parent f67f57e commit 972242b

File tree

5 files changed

+53
-27
lines changed

5 files changed

+53
-27
lines changed

src/bot/irclogbot.bot.pas

+3-10
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ procedure TIRCLogBot.Replay(const ATarget: String; ACount: Integer);
224224
debug('Replay command(%d).', [ACount]);
225225
lines:= FDB.Get(ACount);
226226
debug('Lines: %d.', [lines.Count]);
227-
FReplay.Add(ATarget, lines);
227+
FReplay.Add(btReplay, ATarget, lines);
228228
lines.Free;
229229
end;
230230

@@ -236,7 +236,7 @@ procedure TIRCLogBot.Search(const ATarget, AQuery: String);
236236
lines:= FDB.Search(AQuery);
237237
if lines.Count > 0 then
238238
begin
239-
FReplay.Add(ATarget, lines);
239+
FReplay.Add(btSearch, ATarget, lines);
240240
end
241241
else
242242
begin
@@ -312,14 +312,7 @@ constructor TIRCLogBot.Create(const AConfig: TBotConfig);
312312
FIRC.OnPrivateMessage:= @OnPrivateMessage;
313313

314314
// Setup Database
315-
try
316-
FDB:= TDatabase.Create(FConfig.Database);
317-
except
318-
on e:Exception do
319-
begin
320-
error('Error creating db: "%s".', [e.Message]);
321-
end;
322-
end;
315+
FDB:= TDatabase.Create(FConfig.Database);
323316
end;
324317

325318
destructor TIRCLogBot.Destroy;

src/common/irclogbot.common.pas

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface
99
;
1010

1111
const
12-
cVersion = 'v0.0.3';
12+
cVersion = 'v0.0.4';
1313
cRepoURL = 'https://github.com/ObjectPascal-Community/pasirclogbot';
1414

1515
var
@@ -32,16 +32,16 @@ implementation
3232

3333
procedure log(const ALevel: TLogLevel; const AMessage: String);
3434
begin
35-
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz: ', Now);
35+
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz ', Now);
3636
case ALevel of
3737
llInfo:begin
38-
WriteLn(dateTimeStr, '[INF]: ', AMessage);
38+
WriteLn(dateTimeStr, '[INF] ', AMessage);
3939
end;
4040
llError:begin
41-
WriteLn(dateTimeStr, '[ERR]: ', AMessage);
41+
WriteLn(dateTimeStr, '[ERR] ', AMessage);
4242
end;
4343
llDebug:begin
44-
WriteLn(dateTimeStr, '[DBG]: ', AMessage);
44+
WriteLn(dateTimeStr, '[DBG] ', AMessage);
4545
end;
4646
end;
4747

src/database/irclogbot.database.pas

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ constructor TDatabase.Create(const ADatabaseFile: String);
220220

221221
destructor TDatabase.Destroy;
222222
begin
223-
FCriticalSection.Free;
224223
FQuery.Free;
225224
FTransaction.Free;
226225
FConnection.Free;
226+
FCriticalSection.Free;
227227
inherited Destroy;
228228
end;
229229

src/paslogbot.lpr

+13-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ procedure TPasLogBot.DoRun;
101101
ErrorMsg:= CheckOptions('hc:d', ['help', 'config:', 'debug']);
102102
if ErrorMsg<>'' then
103103
begin
104-
WriteLn('Error: ', ErrorMsg);
104+
error('Error: "%s"', [ErrorMsg]);
105105
Terminate;
106106
exit;
107107
end;
@@ -143,20 +143,29 @@ procedure TPasLogBot.DoRun;
143143
except
144144
on e:Exception do
145145
begin
146-
WriteLn(Format('Error: %s', [e.Message]));
146+
error('Error: %s', [e.Message]);
147147
Terminate;
148148
exit;
149149
end;
150150
end;
151151

152152
debug('Creating IRC client...');
153-
FIRCLogBot:= TIRCLogBot.Create(config);
153+
try
154+
FIRCLogBot:= TIRCLogBot.Create(config);
155+
except
156+
on e:Exception do
157+
begin
158+
error('Error: "%s".', [e.Message]);
159+
Terminate;
160+
exit;
161+
end;
162+
end;
154163
debug('Successfully created IRC client.');
155164
info('Starting...');
156165
FIRCLogBot.Run;
157166
while not Terminated do
158167
begin
159-
Sleep(50);
168+
Sleep(1);
160169
end;
161170
FIRCLogBot.Shutdown;
162171
FIRCLogBot.Free;

src/threads/irclogbot.replay.pas

+31-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ interface
1414
;
1515

1616
type
17+
{ TBundleType }
18+
TBundleType = (btReplay, btSearch);
19+
1720
{ TReplayThread }
1821
TReplayThread = class(TThread)
1922
private
@@ -26,7 +29,7 @@ TReplayThread = class(TThread)
2629
constructor Create(AIRC: TIdIRC);
2730
destructor Destroy; override;
2831

29-
procedure Add(const ANick: String; const AList: TStringList);
32+
procedure Add(const AType: TBundleType; const ANick: String; const AList: TStringList);
3033
published
3134
end;
3235

@@ -39,13 +42,16 @@ implementation
3942
{ TReplayBundle }
4043
TReplayBundle = class(TObject)
4144
private
45+
FBundleType: TBundleType;
4246
FNick: String;
4347
FLines: TStringList;
4448
protected
4549
public
46-
constructor Create(const ANick: String; const ALines: TStringList);
50+
constructor Create(const AType: TBundleType; const ANick: String; const ALines: TStringList);
4751
destructor Destroy; override;
4852

53+
property BundleType: TBundleType
54+
read FBundleType;
4955
property Nick: String
5056
read FNick;
5157
property Lines: TStringList
@@ -55,9 +61,10 @@ TReplayBundle = class(TObject)
5561

5662
{ TReplayBundle }
5763

58-
constructor TReplayBundle.Create(const ANick: String;
64+
constructor TReplayBundle.Create(const AType: TBundleType; const ANick: String;
5965
const ALines: TStringList);
6066
begin
67+
FBundleType:= AType;
6168
FNick:= ANick;
6269
FLines:= TStringList.Create;
6370
FLines.Text:= ALines.Text;
@@ -85,6 +92,7 @@ procedure TReplayThread.Execute;
8592
if FQueue.Count > 0 then
8693
begin
8794
bundle:= TReplayBundle.Create(
95+
(FQueue[0] as TReplayBundle).BundleType,
8896
(FQueue[0] as TReplayBundle).Nick,
8997
(FQueue[0] as TReplayBundle).Lines
9098
);
@@ -96,10 +104,18 @@ procedure TReplayThread.Execute;
96104
if Assigned(bundle) then
97105
begin
98106
FIRC.Say(bundle.Nick, '!! --> To avoid triggering flooding, for each 5 lines, I will pause for 5 seconds <-- !!');
99-
FIRC.Say(bundle.Nick, Format('*** Here are the last %d lines ***', [bundle.Lines.Count]));
107+
case bundle.BundleType of
108+
btReplay:begin
109+
FIRC.Say(bundle.Nick, Format('*** Here are the last %d lines ***', [bundle.Lines.Count]));
110+
end;
111+
btSearch:begin
112+
FIRC.Say(bundle.Nick, Format('*** Here are the last %d lines of your search ***', [bundle.Lines.Count]));
113+
end;
114+
end;
100115
index:= 1;
101116
for line in bundle.Lines do
102117
begin
118+
If Terminated then exit;
103119
debug('Sending #%d: "%s".', [index, line]);
104120
Inc(index);
105121
FIRC.Say(bundle.Nick, line);
@@ -109,7 +125,14 @@ procedure TReplayThread.Execute;
109125
Sleep(5000);
110126
end;
111127
end;
112-
FIRC.Say(bundle.Nick, Format('*** End of the last %d lines ***', [bundle.Lines.Count]));
128+
case bundle.BundleType of
129+
btReplay:begin
130+
FIRC.Say(bundle.Nick, Format('*** End of the last %d lines ***', [bundle.Lines.Count]));
131+
end;
132+
btSearch:begin
133+
FIRC.Say(bundle.Nick, Format('*** End of the last %d lines of your search ***', [bundle.Lines.Count]));
134+
end;
135+
end;
113136
bundle.Free;
114137
end
115138
else
@@ -120,13 +143,14 @@ procedure TReplayThread.Execute;
120143
end;
121144
end;
122145

123-
procedure TReplayThread.Add(const ANick: String; const AList: TStringList);
146+
procedure TReplayThread.Add(const AType: TBundleType; const ANick: String;
147+
const AList: TStringList);
124148
var
125149
bundle: TReplayBundle;
126150
begin
127151
FCriticalSection.Acquire;
128152
try
129-
bundle:= TReplayBundle.Create(ANick, AList);
153+
bundle:= TReplayBundle.Create(AType, ANick, AList);
130154
debug(Format('Adding %d lines for "%s".', [
131155
AList.Count,
132156
ANick

0 commit comments

Comments
 (0)