Skip to content

Commit 472bb0b

Browse files
committed
style: New unit for thread and datetime formatting
1 parent b8b977b commit 472bb0b

File tree

5 files changed

+131
-78
lines changed

5 files changed

+131
-78
lines changed

src/bot/irclogbot.bot.pas

+1-63
Original file line numberDiff line numberDiff line change
@@ -51,71 +51,9 @@ implementation
5151

5252
uses
5353
IRCLogBot.Common
54+
, IRCLogBot.Replay
5455
;
5556

56-
type
57-
{ TReplayThread }
58-
TReplayThread = class(TTHread)
59-
private
60-
FIRC: TIdIRC;
61-
FTarget: String;
62-
FLines: TStringList;
63-
protected
64-
procedure Execute; override;
65-
public
66-
constructor Create(AIRC: TIdIRC; const ATarget: String;
67-
const ALines: TStringList);
68-
published
69-
end;
70-
71-
{ TReplayThread }
72-
73-
procedure TReplayThread.Execute;
74-
var
75-
line: String;
76-
index: Integer = 0;
77-
begin
78-
if not FIRC.Connected then
79-
begin
80-
debug('Exiting replay thread due not being connected.');
81-
exit;
82-
end;
83-
try
84-
FIRC.Say(FTarget, '!! --> To avoid triggering flooding, for each 5 lines, I will pause for 5 seconds <-- !!');
85-
FIRC.Say(FTarget, Format('*** Here are the last %d lines ***', [FLines.Count]));
86-
for line in FLines do
87-
begin
88-
if (Terminated) or (not FIRC.Connected) then
89-
begin
90-
debug('Exiting replay thread due to termination or not being connected.');
91-
exit;
92-
end;
93-
debug('Sending #%d: "%s"', [index, line]);
94-
Inc(index);
95-
FIRC.Say(FTarget, line);
96-
if (index mod 5) = 0 then
97-
begin
98-
debug('Pausing');
99-
Sleep(5000);
100-
end;
101-
end;
102-
FIRC.Say(FTarget, Format('*** End of the last %d lines ***', [FLines.Count]));
103-
finally
104-
FLines.Free;
105-
end;
106-
end;
107-
108-
constructor TReplayThread.Create(AIRC: TIdIRC; const ATarget: String;
109-
const ALines: TStringList);
110-
begin
111-
inherited Create(True);
112-
FIRC:= AIRC;
113-
FTarget:= ATarget;
114-
FLines:= TStringList.Create;
115-
FLines.Text := ALines.Text;
116-
FreeOnTerminate:= True;
117-
Start;
118-
end;
11957

12058
{ TIRCLogBot }
12159

src/database/irclogbot.database.pas

+19-8
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ procedure TDatabase.Insert(ANickName, AChannel, AMessage: String);
8989
end;
9090

9191
function TDatabase.Get(ACount: Integer): TStringList;
92+
var
93+
timestamp: TDateTime;
94+
date, channel, nick, message: String;
9295
begin
9396
Result:= TStringList.Create;
9497
FCriticalSection.Acquire;
@@ -106,17 +109,25 @@ function TDatabase.Get(ACount: Integer): TStringList;
106109
begin
107110
FQuery.First;
108111
repeat
112+
timestamp:= FQuery.FieldByName('timestamp').AsDateTime;
113+
date:= FormatDateTime(
114+
DefaultFormatSettings.ShortDateFormat + ' ' + DefaultFormatSettings.LongTimeFormat,
115+
timestamp
116+
);
117+
channel:= FQuery.FieldByName('channel').AsString;
118+
nick:= FQuery.FieldByName('nick').AsString;
119+
message:= FQuery.FieldByName('message').AsString;
109120
debug('Retrieving: %s [%s] %s: %s', [
110-
FQuery.FieldByName('timestamp').AsString,
111-
FQuery.FieldByName('channel').AsString,
112-
FQuery.FieldByName('nick').AsString,
113-
FQuery.FieldByName('message').AsString
121+
date,
122+
channel,
123+
nick,
124+
message
114125
]);
115126
Result.Insert(0, Format('%s [%s] %s: %s',[
116-
FQuery.FieldByName('timestamp').AsString,
117-
FQuery.FieldByName('channel').AsString,
118-
FQuery.FieldByName('nick').AsString,
119-
FQuery.FieldByName('message').AsString
127+
date,
128+
channel,
129+
nick,
130+
message
120131
]));
121132
FQuery.Next;
122133
until FQuery.EOF;

src/paslogbot.lpi

+8-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</Target>
2424
<SearchPaths>
2525
<IncludeFiles Value="$(ProjOutDir)"/>
26-
<OtherUnitFiles Value="bot;config;common;database"/>
26+
<OtherUnitFiles Value="bot;config;common;database;threads"/>
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;config;common;database"/>
60+
<OtherUnitFiles Value="bot;config;common;database;threads"/>
6161
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
6262
</SearchPaths>
6363
<CodeGeneration>
@@ -114,6 +114,11 @@
114114
<IsPartOfProject Value="True"/>
115115
<UnitName Value="IRCLogBot.Database"/>
116116
</Unit>
117+
<Unit>
118+
<Filename Value="threads/irclogbot.replay.pas"/>
119+
<IsPartOfProject Value="True"/>
120+
<UnitName Value="IRCLogBot.Replay"/>
121+
</Unit>
117122
</Units>
118123
</ProjectOptions>
119124
<CompilerOptions>
@@ -123,7 +128,7 @@
123128
</Target>
124129
<SearchPaths>
125130
<IncludeFiles Value="$(ProjOutDir)"/>
126-
<OtherUnitFiles Value="bot;config;common;database"/>
131+
<OtherUnitFiles Value="bot;config;common;database;threads"/>
127132
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
128133
</SearchPaths>
129134
</CompilerOptions>

src/paslogbot.lpr

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
{$IFDEF WINDOWS}
1111
Windows,
1212
{$ENDIF}
13-
Classes, SysUtils, CustApp, IRCLogBot.Common, IRCLogBot.Bot, IRCLogBot.Config,
14-
IRCLogBot.Database
13+
Classes, SysUtils, CustApp, IRCLogBot.Common, IRCLogBot.Bot, IRCLogBot.Config
1514
{ you can add units after this };
1615

1716
type
@@ -128,6 +127,13 @@ procedure TPasLogBot.DoRun;
128127
// Debug
129128
DebugOn:= HasOption('d', 'debug');
130129

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

133139
config:= TBotConfig.Create(FConfigFile);
@@ -142,12 +148,10 @@ procedure TPasLogBot.DoRun;
142148
end;
143149
end;
144150

145-
{ #todo 100 -ogcarreno : Use data from config }
146151
debug('Creating IRC client...');
147152
FIRCLogBot:= TIRCLogBot.Create(config);
148153
debug('Successfully created IRC client.');
149154
debug('Starting...');
150-
{ #todo 100 -ogcarreno : Read Config }
151155
FIRCLogBot.Run;
152156
while not Terminated do
153157
begin

src/threads/irclogbot.replay.pas

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
unit IRCLogBot.Replay;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses
8+
Classes
9+
, SysUtils
10+
, SyncObjs
11+
, IdIRC
12+
13+
;
14+
15+
type
16+
{ TReplayThread }
17+
TReplayThread = class(TThread)
18+
private
19+
FCriticalSection: TCriticalSection;
20+
FIRC: TIdIRC;
21+
FTarget: String;
22+
FLines: TStringList;
23+
protected
24+
procedure Execute; override;
25+
public
26+
constructor Create(AIRC: TIdIRC; const ATarget: String;
27+
const ALines: TStringList);
28+
destructor Destroy; override;
29+
published
30+
end;
31+
32+
implementation
33+
34+
uses
35+
IRCLogBot.Common
36+
;
37+
38+
{ TReplayThread }
39+
40+
procedure TReplayThread.Execute;
41+
var
42+
line: String;
43+
index: Integer = 0;
44+
begin
45+
if not FIRC.Connected then
46+
begin
47+
debug('Exiting replay thread due not being connected.');
48+
exit;
49+
end;
50+
try
51+
FIRC.Say(FTarget, '!! --> To avoid triggering flooding, for each 5 lines, I will pause for 5 seconds <-- !!');
52+
FIRC.Say(FTarget, Format('*** Here are the last %d lines ***', [FLines.Count]));
53+
for line in FLines do
54+
begin
55+
if (Terminated) or (not FIRC.Connected) then
56+
begin
57+
debug('Exiting replay thread due to termination or not being connected.');
58+
exit;
59+
end;
60+
debug('Sending #%d: "%s"', [index, line]);
61+
Inc(index);
62+
FIRC.Say(FTarget, line);
63+
if (index mod 5) = 0 then
64+
begin
65+
debug('Pausing');
66+
Sleep(5000);
67+
end;
68+
end;
69+
FIRC.Say(FTarget, Format('*** End of the last %d lines ***', [FLines.Count]));
70+
finally
71+
FLines.Free;
72+
end;
73+
end;
74+
75+
constructor TReplayThread.Create(AIRC: TIdIRC; const ATarget: String;
76+
const ALines: TStringList);
77+
begin
78+
inherited Create(True);
79+
FCriticalSection:= TCriticalSection.Create;
80+
FIRC:= AIRC;
81+
FTarget:= ATarget;
82+
FLines:= TStringList.Create;
83+
FLines.Text := ALines.Text;
84+
FreeOnTerminate:= True;
85+
Start;
86+
end;
87+
88+
destructor TReplayThread.Destroy;
89+
begin
90+
FCriticalSection.Free;
91+
inherited Destroy;
92+
end;
93+
94+
end.
95+

0 commit comments

Comments
 (0)