Skip to content

Commit

Permalink
style: New unit for thread and datetime formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarreno committed Dec 9, 2024
1 parent b8b977b commit 472bb0b
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 78 deletions.
64 changes: 1 addition & 63 deletions src/bot/irclogbot.bot.pas
Original file line number Diff line number Diff line change
Expand Up @@ -51,71 +51,9 @@ implementation

uses
IRCLogBot.Common
, IRCLogBot.Replay
;

type
{ TReplayThread }
TReplayThread = class(TTHread)
private
FIRC: TIdIRC;
FTarget: String;
FLines: TStringList;
protected
procedure Execute; override;
public
constructor Create(AIRC: TIdIRC; const ATarget: String;
const ALines: TStringList);
published
end;

{ TReplayThread }

procedure TReplayThread.Execute;
var
line: String;
index: Integer = 0;
begin
if not FIRC.Connected then
begin
debug('Exiting replay thread due not being connected.');
exit;
end;
try
FIRC.Say(FTarget, '!! --> To avoid triggering flooding, for each 5 lines, I will pause for 5 seconds <-- !!');
FIRC.Say(FTarget, Format('*** Here are the last %d lines ***', [FLines.Count]));
for line in FLines do
begin
if (Terminated) or (not FIRC.Connected) then
begin
debug('Exiting replay thread due to termination or not being connected.');
exit;
end;
debug('Sending #%d: "%s"', [index, line]);
Inc(index);
FIRC.Say(FTarget, line);
if (index mod 5) = 0 then
begin
debug('Pausing');
Sleep(5000);
end;
end;
FIRC.Say(FTarget, Format('*** End of the last %d lines ***', [FLines.Count]));
finally
FLines.Free;
end;
end;

constructor TReplayThread.Create(AIRC: TIdIRC; const ATarget: String;
const ALines: TStringList);
begin
inherited Create(True);
FIRC:= AIRC;
FTarget:= ATarget;
FLines:= TStringList.Create;
FLines.Text := ALines.Text;
FreeOnTerminate:= True;
Start;
end;

{ TIRCLogBot }

Expand Down
27 changes: 19 additions & 8 deletions src/database/irclogbot.database.pas
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ procedure TDatabase.Insert(ANickName, AChannel, AMessage: String);
end;

function TDatabase.Get(ACount: Integer): TStringList;
var
timestamp: TDateTime;
date, channel, nick, message: String;
begin
Result:= TStringList.Create;
FCriticalSection.Acquire;
Expand All @@ -106,17 +109,25 @@ function TDatabase.Get(ACount: Integer): TStringList;
begin
FQuery.First;
repeat
timestamp:= FQuery.FieldByName('timestamp').AsDateTime;
date:= FormatDateTime(
DefaultFormatSettings.ShortDateFormat + ' ' + DefaultFormatSettings.LongTimeFormat,
timestamp
);
channel:= FQuery.FieldByName('channel').AsString;
nick:= FQuery.FieldByName('nick').AsString;
message:= FQuery.FieldByName('message').AsString;
debug('Retrieving: %s [%s] %s: %s', [
FQuery.FieldByName('timestamp').AsString,
FQuery.FieldByName('channel').AsString,
FQuery.FieldByName('nick').AsString,
FQuery.FieldByName('message').AsString
date,
channel,
nick,
message
]);
Result.Insert(0, Format('%s [%s] %s: %s',[
FQuery.FieldByName('timestamp').AsString,
FQuery.FieldByName('channel').AsString,
FQuery.FieldByName('nick').AsString,
FQuery.FieldByName('message').AsString
date,
channel,
nick,
message
]));
FQuery.Next;
until FQuery.EOF;
Expand Down
11 changes: 8 additions & 3 deletions src/paslogbot.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="bot;config;common;database"/>
<OtherUnitFiles Value="bot;config;common;database;threads"/>
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Parsing>
Expand Down Expand Up @@ -57,7 +57,7 @@
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="bot;config;common;database"/>
<OtherUnitFiles Value="bot;config;common;database;threads"/>
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<CodeGeneration>
Expand Down Expand Up @@ -114,6 +114,11 @@
<IsPartOfProject Value="True"/>
<UnitName Value="IRCLogBot.Database"/>
</Unit>
<Unit>
<Filename Value="threads/irclogbot.replay.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="IRCLogBot.Replay"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
Expand All @@ -123,7 +128,7 @@
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="bot;config;common;database"/>
<OtherUnitFiles Value="bot;config;common;database;threads"/>
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
</CompilerOptions>
Expand Down
12 changes: 8 additions & 4 deletions src/paslogbot.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
{$IFDEF WINDOWS}
Windows,
{$ENDIF}
Classes, SysUtils, CustApp, IRCLogBot.Common, IRCLogBot.Bot, IRCLogBot.Config,
IRCLogBot.Database
Classes, SysUtils, CustApp, IRCLogBot.Common, IRCLogBot.Bot, IRCLogBot.Config
{ you can add units after this };

type
Expand Down Expand Up @@ -128,6 +127,13 @@ procedure TPasLogBot.DoRun;
// Debug
DebugOn:= HasOption('d', 'debug');

//debug('Long Date: ' + DefaultFormatSettings.LongDateFormat);
//debug('Long Time: ' + DefaultFormatSettings.LongTimeFormat);
//debug('Short Date: ' + DefaultFormatSettings.ShortDateFormat);
//debug('Short Time: ' + DefaultFormatSettings.ShortTimeFormat);
//debug('Date Sep: ' + DefaultFormatSettings.DateSeparator);
DefaultFormatSettings.ShortDateFormat:= 'yyyy/mm/dd';
DefaultFormatSettings.DateSeparator:= '/';
debug(Format('Attempting to read config from: "%s"...', [FConfigFile]));

config:= TBotConfig.Create(FConfigFile);
Expand All @@ -142,12 +148,10 @@ procedure TPasLogBot.DoRun;
end;
end;

{ #todo 100 -ogcarreno : Use data from config }
debug('Creating IRC client...');
FIRCLogBot:= TIRCLogBot.Create(config);
debug('Successfully created IRC client.');
debug('Starting...');
{ #todo 100 -ogcarreno : Read Config }
FIRCLogBot.Run;
while not Terminated do
begin
Expand Down
95 changes: 95 additions & 0 deletions src/threads/irclogbot.replay.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
unit IRCLogBot.Replay;

{$mode ObjFPC}{$H+}

interface

uses
Classes
, SysUtils
, SyncObjs
, IdIRC

;

type
{ TReplayThread }
TReplayThread = class(TThread)
private
FCriticalSection: TCriticalSection;
FIRC: TIdIRC;
FTarget: String;
FLines: TStringList;
protected
procedure Execute; override;
public
constructor Create(AIRC: TIdIRC; const ATarget: String;
const ALines: TStringList);
destructor Destroy; override;
published
end;

implementation

uses
IRCLogBot.Common
;

{ TReplayThread }

procedure TReplayThread.Execute;
var
line: String;
index: Integer = 0;
begin
if not FIRC.Connected then
begin
debug('Exiting replay thread due not being connected.');
exit;
end;
try
FIRC.Say(FTarget, '!! --> To avoid triggering flooding, for each 5 lines, I will pause for 5 seconds <-- !!');
FIRC.Say(FTarget, Format('*** Here are the last %d lines ***', [FLines.Count]));
for line in FLines do
begin
if (Terminated) or (not FIRC.Connected) then
begin
debug('Exiting replay thread due to termination or not being connected.');
exit;
end;
debug('Sending #%d: "%s"', [index, line]);
Inc(index);
FIRC.Say(FTarget, line);
if (index mod 5) = 0 then
begin
debug('Pausing');
Sleep(5000);
end;
end;
FIRC.Say(FTarget, Format('*** End of the last %d lines ***', [FLines.Count]));
finally
FLines.Free;
end;
end;

constructor TReplayThread.Create(AIRC: TIdIRC; const ATarget: String;
const ALines: TStringList);
begin
inherited Create(True);
FCriticalSection:= TCriticalSection.Create;
FIRC:= AIRC;
FTarget:= ATarget;
FLines:= TStringList.Create;
FLines.Text := ALines.Text;
FreeOnTerminate:= True;
Start;
end;

destructor TReplayThread.Destroy;
begin
FCriticalSection.Free;
inherited Destroy;
end;

end.

0 comments on commit 472bb0b

Please sign in to comment.