Skip to content

Commit

Permalink
refactor: Replay thread now long running
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarreno committed Dec 9, 2024
1 parent 6c1d650 commit 6a507ab
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 56 deletions.
45 changes: 26 additions & 19 deletions src/bot/irclogbot.bot.pas
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface
, IdIRC
, IRCLogBot.Config
, IRCLogBot.Database
, IRCLogBot.Replay
;

type
Expand All @@ -24,6 +25,8 @@ TIRCLogBot = class(TObject)

FDB: TDatabase;

FReplay: TReplayThread;

procedure OnConnected(Sender: TObject);
procedure OnDisconnected(Sender: TObject);
procedure OnNotice(ASender: TIdContext; const ANickname, AHost,
Expand Down Expand Up @@ -51,26 +54,25 @@ implementation

uses
IRCLogBot.Common
, IRCLogBot.Replay
;


{ TIRCLogBot }

procedure TIRCLogBot.OnConnected(Sender: TObject);
begin
debug('Connected to server');
debug('Connected to server.');
end;

procedure TIRCLogBot.OnDisconnected(Sender: TObject);
begin
debug('Disconnected from server');
debug('Disconnected from server.');
end;

procedure TIRCLogBot.OnNotice(ASender: TIdContext; const ANickname, AHost,
ATarget, ANotice: String);
begin
debug('>> NOTICE: <%s:%s> (%s) "%s"', [
debug('>> NOTICE: <%s:%s> (%s) "%s".', [
ANickname,
AHost,
ATarget,
Expand All @@ -81,7 +83,7 @@ procedure TIRCLogBot.OnNotice(ASender: TIdContext; const ANickname, AHost,
procedure TIRCLogBot.OnServerQuit(ASender: TIdContext; const ANickname, AHost,
AServer, AReason: String);
begin
debug('>> QUIT: <%s:%s> %s "%s"',[
debug('>> QUIT: <%s:%s> %s "%s".',[
ANickname,
AHost,
AServer,
Expand All @@ -92,14 +94,14 @@ procedure TIRCLogBot.OnServerQuit(ASender: TIdContext; const ANickname, AHost,
procedure TIRCLogBot.OnJoin(ASender: TIdContext; const ANickname, AHost,
AChannel: String);
begin
debug('>> JOIN: <%s:%s> %s', [
debug('>> JOIN: <%s:%s> %s.', [
ANickname,
AHost,
AChannel
]);
if (ANickname = FConfig.NickName) and (AChannel = FConfig.Channel) then
begin
debug('Successfully joined my channel');
debug('Successfully joined my channel.');
FJoinedChannel:= True;
FIRC.Say(AChannel, 'I have arrived!!! TADAAAAA!!! Send me a private message with ".help" to see what I can do for you.');
end;
Expand All @@ -111,15 +113,15 @@ procedure TIRCLogBot.OnPrivateMessage(ASender: TIdContext; const ANickname,
strings: TStringArray;
count: Integer;
begin
debug('>> PRIVMSG: <%s:%s>(%s) "%s"', [
debug('>> PRIVMSG: <%s:%s>(%s) "%s".', [
ANickname,
AHost,
ATarget,
AMessage
]);
if ATarget = FConfig.Channel then
begin
debug('Inserting: %s, %s, %s, %s', [
debug('Inserting: %s, %s, %s, %s.', [
ANickname,
AHost,
ATarget,
Expand Down Expand Up @@ -175,46 +177,51 @@ procedure TIRCLogBot.Help(const ATarget: String);
begin
debug('Help command.');
FIRC.Say(ATarget, 'Commands:');
FIRC.Say(ATarget, '.help - This help information.');
FIRC.Say(ATarget, '.help - This help information.');
FIRC.Say(ATarget, '.replay [count] - Raplays last <count> lines. Default is last 10 lines.');
end;

procedure TIRCLogBot.Replay(const ATarget: String; ACount: Integer);
var
replayThread: TReplayThread;
lines: TStringList;
begin
debug('Replay command(%d).', [ACount]);
lines:= FDB.Get(ACount);
debug('Lines: %d', [lines.Count]);
replayThread:= TReplayThread.Create(FIRC, ATarget, lines);
debug('Lines: %d.', [lines.Count]);
FReplay.Add(ATarget, lines);
lines.Free;
end;

procedure TIRCLogBot.Run;
begin
debug('Connecting...');
try
debug('Connecting...');
FIRC.Connect;
except
on e:Exception do
begin
debug('Error connecting: %s', [e.Message]);
debug('Error connecting: "%s".', [e.Message]);
end;
end;
debug('Joining channel: "%s"...', [FConfig.Channel]);
try
debug('Joining channel: "%s"...', [FConfig.Channel]);
FIRC.Join(FConfig.Channel);
except
on e:Exception do
begin
debug('Error joining: %s', [e.Message]);
debug('Error joining: "%s".', [e.Message]);
end;
end;
debug('Starting Replay Thread.');
FReplay:= TReplayThread.Create(FIRC);
end;

procedure TIRCLogBot.Shutdown;
begin
debug('Terminating Replay Thread.');
FReplay.Terminate;
debug('Waiting for Replay Thread to terminate...');
FReplay.WaitFor;
if FIRC.Connected then
begin
debug('Disconnecting...');
Expand All @@ -224,7 +231,7 @@ procedure TIRCLogBot.Shutdown;
except
on e:Exception do
begin
debug('Error: %s', [e.Message]);
debug('Error disconnecting: "%s".', [e.Message]);
end;
end;
end;
Expand Down Expand Up @@ -257,7 +264,7 @@ constructor TIRCLogBot.Create(const AConfig: TBotConfig);
except
on e:Exception do
begin
debug('Error creating db: ', [e.Message]);
debug('Error creating db: "%s".', [e.Message]);
end;
end;
end;
Expand Down
4 changes: 2 additions & 2 deletions src/common/irclogbot.common.pas
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ procedure debug(const AMessage: String);
begin
if DebugOn then
begin
dateTimeStr:= FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz:', Now);
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz: ', Now);
WriteLn(Format('%s %s', [dateTimeStr, AMessage]));
end;
end;
Expand All @@ -32,7 +32,7 @@ procedure debug(const AFormat: String; AValues: array of const);
begin
if DebugOn then
begin
dateTimeStr:= FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz: ', Now);
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz: ', Now);
WriteLn(Format(dateTimeStr+AFormat, AValues));
end;
end;
Expand Down
10 changes: 5 additions & 5 deletions src/database/irclogbot.database.pas
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ procedure TDatabase.SetupTables;
on e:Exception do
begin
FTransaction.Rollback;
debug('Error setting tables: %s', [e.Message]);
debug('Error setting tables: "%s".', [e.Message]);
end;
end;
end;
Expand All @@ -68,7 +68,7 @@ procedure TDatabase.Insert(ANickName, AChannel, AMessage: String);
debug('Starting Transaction...');
FTransaction.StartTransaction;
try
debug('Inserting: <%s> [%s] "%s"', [ANickName, AChannel, AMessage]);
debug('Inserting: <%s> [%s] "%s".', [ANickName, AChannel, AMessage]);
FConnection.ExecuteDirect(Format(
'INSERT INTO logs (nick, channel, message) VALUES (%s, %s, %s)',
[QuotedStr(ANickName), QuotedStr(AChannel), QuotedStr(AMessage)]));
Expand All @@ -80,7 +80,7 @@ procedure TDatabase.Insert(ANickName, AChannel, AMessage: String);
on e:Exception do
begin
FTransaction.Rollback;
debug('Error inserting message: %s', [e.Message]);
debug('Error inserting message: "%s".', [e.Message]);
end;
end;
finally
Expand Down Expand Up @@ -112,7 +112,7 @@ function TDatabase.Get(ACount: Integer): TStringList;
channel:= FQuery.FieldByName('channel').AsString;
nick:= FQuery.FieldByName('nick').AsString;
message:= FQuery.FieldByName('message').AsString;
debug('Retrieving: %s [%s] %s: %s', [
debug('Retrieving: %s [%s] %s: %s.', [
date,
channel,
nick,
Expand All @@ -136,7 +136,7 @@ function TDatabase.Get(ACount: Integer): TStringList;
on e:Exception do
begin
FTransaction.Rollback;
debug('Error retrieving lines: %s', [e.Message]);
debug('Error retrieving lines: "%s".', [e.Message]);
end;
end;
finally
Expand Down
2 changes: 1 addition & 1 deletion src/paslogbot.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ procedure TPasLogBot.DoRun;
//debug('Short Date: ' + DefaultFormatSettings.ShortDateFormat);
//debug('Short Time: ' + DefaultFormatSettings.ShortTimeFormat);
//debug('Date Sep: ' + DefaultFormatSettings.DateSeparator);
debug('Setting Date and Time formats');
debug('Setting Date and Time formats.');
DefaultFormatSettings.ShortDateFormat:= 'yyyy/mm/dd';
DefaultFormatSettings.DateSeparator:= '/';
debug(Format('Attempting to read config from: "%s"...', [FConfigFile]));
Expand Down
Loading

0 comments on commit 6a507ab

Please sign in to comment.