Skip to content

Commit

Permalink
refactor: Bundle types
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarreno committed Dec 9, 2024
1 parent f67f57e commit 972242b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 27 deletions.
13 changes: 3 additions & 10 deletions src/bot/irclogbot.bot.pas
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ procedure TIRCLogBot.Replay(const ATarget: String; ACount: Integer);
debug('Replay command(%d).', [ACount]);
lines:= FDB.Get(ACount);
debug('Lines: %d.', [lines.Count]);
FReplay.Add(ATarget, lines);
FReplay.Add(btReplay, ATarget, lines);
lines.Free;
end;

Expand All @@ -236,7 +236,7 @@ procedure TIRCLogBot.Search(const ATarget, AQuery: String);
lines:= FDB.Search(AQuery);
if lines.Count > 0 then
begin
FReplay.Add(ATarget, lines);
FReplay.Add(btSearch, ATarget, lines);
end
else
begin
Expand Down Expand Up @@ -312,14 +312,7 @@ constructor TIRCLogBot.Create(const AConfig: TBotConfig);
FIRC.OnPrivateMessage:= @OnPrivateMessage;

// Setup Database
try
FDB:= TDatabase.Create(FConfig.Database);
except
on e:Exception do
begin
error('Error creating db: "%s".', [e.Message]);
end;
end;
FDB:= TDatabase.Create(FConfig.Database);
end;

destructor TIRCLogBot.Destroy;
Expand Down
10 changes: 5 additions & 5 deletions src/common/irclogbot.common.pas
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface
;

const
cVersion = 'v0.0.3';
cVersion = 'v0.0.4';
cRepoURL = 'https://github.com/ObjectPascal-Community/pasirclogbot';

var
Expand All @@ -32,16 +32,16 @@ implementation

procedure log(const ALevel: TLogLevel; const AMessage: String);
begin
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz: ', Now);
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz ', Now);
case ALevel of
llInfo:begin
WriteLn(dateTimeStr, '[INF]: ', AMessage);
WriteLn(dateTimeStr, '[INF] ', AMessage);
end;
llError:begin
WriteLn(dateTimeStr, '[ERR]: ', AMessage);
WriteLn(dateTimeStr, '[ERR] ', AMessage);
end;
llDebug:begin
WriteLn(dateTimeStr, '[DBG]: ', AMessage);
WriteLn(dateTimeStr, '[DBG] ', AMessage);
end;
end;

Expand Down
2 changes: 1 addition & 1 deletion src/database/irclogbot.database.pas
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ constructor TDatabase.Create(const ADatabaseFile: String);

destructor TDatabase.Destroy;
begin
FCriticalSection.Free;
FQuery.Free;
FTransaction.Free;
FConnection.Free;
FCriticalSection.Free;
inherited Destroy;
end;

Expand Down
17 changes: 13 additions & 4 deletions src/paslogbot.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ procedure TPasLogBot.DoRun;
ErrorMsg:= CheckOptions('hc:d', ['help', 'config:', 'debug']);
if ErrorMsg<>'' then
begin
WriteLn('Error: ', ErrorMsg);
error('Error: "%s"', [ErrorMsg]);
Terminate;
exit;
end;
Expand Down Expand Up @@ -143,20 +143,29 @@ procedure TPasLogBot.DoRun;
except
on e:Exception do
begin
WriteLn(Format('Error: %s', [e.Message]));
error('Error: %s', [e.Message]);
Terminate;
exit;
end;
end;

debug('Creating IRC client...');
FIRCLogBot:= TIRCLogBot.Create(config);
try
FIRCLogBot:= TIRCLogBot.Create(config);
except
on e:Exception do
begin
error('Error: "%s".', [e.Message]);
Terminate;
exit;
end;
end;
debug('Successfully created IRC client.');
info('Starting...');
FIRCLogBot.Run;
while not Terminated do
begin
Sleep(50);
Sleep(1);
end;
FIRCLogBot.Shutdown;
FIRCLogBot.Free;
Expand Down
38 changes: 31 additions & 7 deletions src/threads/irclogbot.replay.pas
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ interface
;

type
{ TBundleType }
TBundleType = (btReplay, btSearch);

{ TReplayThread }
TReplayThread = class(TThread)
private
Expand All @@ -26,7 +29,7 @@ TReplayThread = class(TThread)
constructor Create(AIRC: TIdIRC);
destructor Destroy; override;

procedure Add(const ANick: String; const AList: TStringList);
procedure Add(const AType: TBundleType; const ANick: String; const AList: TStringList);
published
end;

Expand All @@ -39,13 +42,16 @@ implementation
{ TReplayBundle }
TReplayBundle = class(TObject)
private
FBundleType: TBundleType;
FNick: String;
FLines: TStringList;
protected
public
constructor Create(const ANick: String; const ALines: TStringList);
constructor Create(const AType: TBundleType; const ANick: String; const ALines: TStringList);
destructor Destroy; override;

property BundleType: TBundleType
read FBundleType;
property Nick: String
read FNick;
property Lines: TStringList
Expand All @@ -55,9 +61,10 @@ TReplayBundle = class(TObject)

{ TReplayBundle }

constructor TReplayBundle.Create(const ANick: String;
constructor TReplayBundle.Create(const AType: TBundleType; const ANick: String;
const ALines: TStringList);
begin
FBundleType:= AType;
FNick:= ANick;
FLines:= TStringList.Create;
FLines.Text:= ALines.Text;
Expand Down Expand Up @@ -85,6 +92,7 @@ procedure TReplayThread.Execute;
if FQueue.Count > 0 then
begin
bundle:= TReplayBundle.Create(
(FQueue[0] as TReplayBundle).BundleType,
(FQueue[0] as TReplayBundle).Nick,
(FQueue[0] as TReplayBundle).Lines
);
Expand All @@ -96,10 +104,18 @@ procedure TReplayThread.Execute;
if Assigned(bundle) then
begin
FIRC.Say(bundle.Nick, '!! --> To avoid triggering flooding, for each 5 lines, I will pause for 5 seconds <-- !!');
FIRC.Say(bundle.Nick, Format('*** Here are the last %d lines ***', [bundle.Lines.Count]));
case bundle.BundleType of
btReplay:begin
FIRC.Say(bundle.Nick, Format('*** Here are the last %d lines ***', [bundle.Lines.Count]));
end;
btSearch:begin
FIRC.Say(bundle.Nick, Format('*** Here are the last %d lines of your search ***', [bundle.Lines.Count]));
end;
end;
index:= 1;
for line in bundle.Lines do
begin
If Terminated then exit;
debug('Sending #%d: "%s".', [index, line]);
Inc(index);
FIRC.Say(bundle.Nick, line);
Expand All @@ -109,7 +125,14 @@ procedure TReplayThread.Execute;
Sleep(5000);
end;
end;
FIRC.Say(bundle.Nick, Format('*** End of the last %d lines ***', [bundle.Lines.Count]));
case bundle.BundleType of
btReplay:begin
FIRC.Say(bundle.Nick, Format('*** End of the last %d lines ***', [bundle.Lines.Count]));
end;
btSearch:begin
FIRC.Say(bundle.Nick, Format('*** End of the last %d lines of your search ***', [bundle.Lines.Count]));
end;
end;
bundle.Free;
end
else
Expand All @@ -120,13 +143,14 @@ procedure TReplayThread.Execute;
end;
end;

procedure TReplayThread.Add(const ANick: String; const AList: TStringList);
procedure TReplayThread.Add(const AType: TBundleType; const ANick: String;
const AList: TStringList);
var
bundle: TReplayBundle;
begin
FCriticalSection.Acquire;
try
bundle:= TReplayBundle.Create(ANick, AList);
bundle:= TReplayBundle.Create(AType, ANick, AList);
debug(Format('Adding %d lines for "%s".', [
AList.Count,
ANick
Expand Down

0 comments on commit 972242b

Please sign in to comment.