Skip to content

Commit b96ef44

Browse files
committed
feat: Search and Log INF/DBG
1 parent 4de4492 commit b96ef44

File tree

5 files changed

+150
-25
lines changed

5 files changed

+150
-25
lines changed

src/bot/irclogbot.bot.pas

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ TIRCLogBot = class(TObject)
4141
procedure Help(const ATarget: String);
4242
procedure Version(const ATarget: String);
4343
procedure Replay(const ATarget: String; ACount: Integer);
44+
procedure Search(const ATarget, AQuery: String);
4445
protected
4546
public
4647
constructor Create(const AConfig: TBotConfig);
@@ -73,7 +74,7 @@ procedure TIRCLogBot.OnDisconnected(Sender: TObject);
7374
procedure TIRCLogBot.OnNotice(ASender: TIdContext; const ANickname, AHost,
7475
ATarget, ANotice: String);
7576
begin
76-
debug('>> NOTICE: <%s:%s> (%s) "%s".', [
77+
info('>> NOTICE: <%s:%s> (%s) "%s".', [
7778
ANickname,
7879
AHost,
7980
ATarget,
@@ -84,7 +85,7 @@ procedure TIRCLogBot.OnNotice(ASender: TIdContext; const ANickname, AHost,
8485
procedure TIRCLogBot.OnServerQuit(ASender: TIdContext; const ANickname, AHost,
8586
AServer, AReason: String);
8687
begin
87-
debug('>> QUIT: <%s:%s> %s "%s".',[
88+
info('>> QUIT: <%s:%s> %s "%s".',[
8889
ANickname,
8990
AHost,
9091
AServer,
@@ -95,7 +96,7 @@ procedure TIRCLogBot.OnServerQuit(ASender: TIdContext; const ANickname, AHost,
9596
procedure TIRCLogBot.OnJoin(ASender: TIdContext; const ANickname, AHost,
9697
AChannel: String);
9798
begin
98-
debug('>> JOIN: <%s:%s> %s.', [
99+
info('>> JOIN: <%s:%s> %s.', [
99100
ANickname,
100101
AHost,
101102
AChannel
@@ -114,7 +115,7 @@ procedure TIRCLogBot.OnPrivateMessage(ASender: TIdContext; const ANickname,
114115
strings: TStringArray;
115116
count: Integer;
116117
begin
117-
debug('>> PRIVMSG: <%s:%s>(%s) "%s".', [
118+
info('>> PRIVMSG: <%s:%s>(%s) "%s".', [
118119
ANickname,
119120
AHost,
120121
ATarget,
@@ -169,6 +170,19 @@ procedure TIRCLogBot.OnPrivateMessage(ASender: TIdContext; const ANickname,
169170
end;
170171
exit;
171172
end;
173+
if Pos('.search', Trim(AMessage)) = 1 then
174+
begin
175+
strings:= AMessage.Split([' ']);
176+
if Length(strings[1]) > 2 then
177+
begin
178+
Search(ANickname, strings[1]);
179+
end
180+
else
181+
begin
182+
FIRC.Say(ANickname, 'I will only search if query is 3 characters or more.');
183+
end;
184+
exit;
185+
end;
172186
end
173187
else
174188
begin
@@ -185,13 +199,18 @@ procedure TIRCLogBot.Help(const ATarget: String);
185199
FIRC.Say(ATarget, 'Commands:');
186200
FIRC.Say(ATarget, '.help - This help information.');
187201
FIRC.Say(ATarget, '.version - Version and info about the bot.');
188-
FIRC.Say(ATarget, '.replay [count] - Raplays last <count> lines. Default is last 10 lines.');
202+
FIRC.Say(ATarget, '.replay [count] - Replays last <count> lines. Default is last 10 lines.');
203+
Sleep(5000);
204+
FIRC.Say(ATarget, '.search [query] - Searches for <query> in the logs.');
205+
FIRC.Say(ATarget, ' Will only search if <query> is 3 characters or more.');
206+
FIRC.Say(ATarget, ' Will only use the first word after the command. No multi word search(yet?).');
207+
FIRC.Say(ATarget, ' Returns the last 10 lines with the searched <query>.');
189208
end;
190209

191210
procedure TIRCLogBot.Version(const ATarget: String);
192211
begin
193212
debug('Version command.');
194-
FIRC.Say(ATarget, Format('Version: %s, %s',[
213+
FIRC.Say(ATarget, Format('Version: %s; Source: %s',[
195214
cVersion,
196215
cRepoURL
197216
]));
@@ -208,10 +227,27 @@ procedure TIRCLogBot.Replay(const ATarget: String; ACount: Integer);
208227
lines.Free;
209228
end;
210229

230+
procedure TIRCLogBot.Search(const ATarget, AQuery: String);
231+
var
232+
lines: TStringList;
233+
begin
234+
debug('Search command: "%s"', [AQuery]);
235+
lines:= FDB.Search(AQuery);
236+
if lines.Count > 0 then
237+
begin
238+
FReplay.Add(ATarget, lines);
239+
end
240+
else
241+
begin
242+
FIRC.Say(ATarget, 'Your query returned no lines.');
243+
end;
244+
lines.Free;
245+
end;
246+
211247
procedure TIRCLogBot.Run;
212248
begin
213249
try
214-
debug('Connecting...');
250+
info('Connecting...');
215251
FIRC.Connect;
216252
except
217253
on e:Exception do
@@ -220,7 +256,7 @@ procedure TIRCLogBot.Run;
220256
end;
221257
end;
222258
try
223-
debug('Joining channel: "%s"...', [FConfig.Channel]);
259+
info('Joining channel: "%s"...', [FConfig.Channel]);
224260
FIRC.Join(FConfig.Channel);
225261
except
226262
on e:Exception do
@@ -234,13 +270,13 @@ procedure TIRCLogBot.Run;
234270

235271
procedure TIRCLogBot.Shutdown;
236272
begin
237-
debug('Terminating Replay Thread.');
273+
info('Terminating Replay Thread.');
238274
FReplay.Terminate;
239-
debug('Waiting for Replay Thread to terminate...');
275+
info('Waiting for Replay Thread to terminate...');
240276
FReplay.WaitFor;
241277
if FIRC.Connected then
242278
begin
243-
debug('Disconnecting...');
279+
info('Disconnecting...');
244280
try
245281
if FJoinedChannel then FIRC.Say(FConfig.Channel, 'Boss sais I need to have a wee nap. See Y''All later...');
246282
FIRC.Disconnect('ZzZzZzZzZzZzZzZz...');

src/common/irclogbot.common.pas

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,56 @@ interface
1515
var
1616
DebugOn: Boolean;
1717

18+
procedure info(const AMessage: String); overload;
19+
procedure info(const AFormat: String; AValues: array of const);overload;
1820
procedure debug(const AMessage: String); overload;
1921
procedure debug(const AFormat: String; AValues: array of const);overload;
2022

2123
implementation
2224

25+
type
26+
TLogLevel = (llInfo, llDebug);
27+
2328
var
2429
dateTimeStr: String;
2530

31+
procedure log(const ALevel: TLogLevel; const AMessage: String);
32+
begin
33+
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz: ', Now);
34+
case ALevel of
35+
llInfo:begin
36+
WriteLn(dateTimeStr, '[INF]: ', AMessage);
37+
end;
38+
llDebug:begin
39+
WriteLn(dateTimeStr, '[DBG]: ', AMessage);
40+
end;
41+
end;
42+
43+
end;
44+
45+
procedure info(const AMessage: String);
46+
begin
47+
Log(llInfo, AMessage);
48+
end;
49+
50+
procedure info(const AFormat: String; AValues: array of const);
51+
begin
52+
Log(llInfo, Format(AFormat, AValues));
53+
end;
54+
2655
procedure debug(const AMessage: String);
2756
begin
2857
if DebugOn then
2958
begin
30-
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz: ', Now);
31-
WriteLn(Format('%s %s', [dateTimeStr, AMessage]));
59+
Log(llDebug, AMessage);
3260
end;
3361
end;
3462

3563
procedure debug(const AFormat: String; AValues: array of const);
3664
begin
3765
if DebugOn then
3866
begin
39-
dateTimeStr:= FormatDateTime('yyyy/mm/dd hh:nn:ss.zzz: ', Now);
40-
WriteLn(Format(dateTimeStr+AFormat, AValues));
67+
Log(llDebug, Format(AFormat, AValues));
4168
end;
4269
end;
4370

src/database/irclogbot.database.pas

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ TDatabase = class(TObject)
2525
procedure SetupTables;
2626
protected
2727
public
28-
constructor Create(ADatabaseFile: String);
28+
constructor Create(const ADatabaseFile: String);
2929
destructor Destroy; override;
3030

31-
procedure Insert(ANickName, AChannel, AMessage: String);
32-
function Get(ACount: Integer): TStringList;
31+
procedure Insert(const ANickName, AChannel, AMessage: String);
32+
function Get(const ACount: Integer): TStringList;
33+
function Search(const AQuery: String): TStringList;
3334
published
3435
end;
3536

@@ -60,7 +61,7 @@ procedure TDatabase.SetupTables;
6061
end;
6162
end;
6263

63-
procedure TDatabase.Insert(ANickName, AChannel, AMessage: String);
64+
procedure TDatabase.Insert(const ANickName, AChannel, AMessage: String);
6465
begin
6566
FCriticalSection.Acquire;
6667
try
@@ -88,14 +89,14 @@ procedure TDatabase.Insert(ANickName, AChannel, AMessage: String);
8889
end;
8990
end;
9091

91-
function TDatabase.Get(ACount: Integer): TStringList;
92+
function TDatabase.Get(const ACount: Integer): TStringList;
9293
var
9394
date, channel, nick, message: String;
9495
begin
9596
Result:= TStringList.Create;
9697
FCriticalSection.Acquire;
9798
try
98-
debug('Starting transaction.');
99+
debug('Starting GET transaction.');
99100
FTransaction.StartTransaction;
100101
try
101102
try
@@ -130,7 +131,7 @@ function TDatabase.Get(ACount: Integer): TStringList;
130131
end;
131132
finally
132133
FTransaction.EndTransaction;
133-
debug('Transaction ended.');
134+
debug('Transaction GET ended.');
134135
end;
135136
except
136137
on e:Exception do
@@ -144,7 +145,67 @@ function TDatabase.Get(ACount: Integer): TStringList;
144145
end;
145146
end;
146147

147-
constructor TDatabase.Create(ADatabaseFile: String);
148+
function TDatabase.Search(const AQuery: String): TStringList;
149+
var
150+
date, channel, nick, message: String;
151+
begin
152+
Result:= TStringList.Create;
153+
FCriticalSection.Acquire;
154+
try
155+
debug('Starting SEARCH transaction.');
156+
FTransaction.StartTransaction;
157+
try
158+
try
159+
FQuery.SQL.Text:= Format(
160+
'SELECT timestamp, nick, channel, message ' +
161+
'FROM logs ' +
162+
'WHERE message LIKE %s ' +
163+
'ORDER BY id DESC ' +
164+
'LIMIT %d',
165+
[QuotedStr('%' + AQuery + '%'), 10]
166+
);
167+
FQuery.Open;
168+
if FQuery.RecordCount > 0 then
169+
begin
170+
FQuery.First;
171+
repeat
172+
date:= FQuery.FieldByName('timestamp').AsString;
173+
channel:= FQuery.FieldByName('channel').AsString;
174+
nick:= FQuery.FieldByName('nick').AsString;
175+
message:= FQuery.FieldByName('message').AsString;
176+
debug('Retrieving: %s [%s] %s: %s.', [
177+
date,
178+
channel,
179+
nick,
180+
message
181+
]);
182+
Result.Insert(0, Format('%s [%s] %s: %s',[
183+
date,
184+
channel,
185+
nick,
186+
message
187+
]));
188+
FQuery.Next;
189+
until FQuery.EOF;
190+
FQuery.Close;
191+
end;
192+
finally
193+
FTransaction.EndTransaction;
194+
debug('Transaction SEARCH ended.');
195+
end;
196+
except
197+
on e:Exception do
198+
begin
199+
FTransaction.Rollback;
200+
debug('Error retrieving lines: "%s".', [e.Message]);
201+
end;
202+
end;
203+
finally
204+
FCriticalSection.Release;
205+
end;
206+
end;
207+
208+
constructor TDatabase.Create(const ADatabaseFile: String);
148209
begin
149210
FCriticalSection:= TCriticalSection.Create;
150211
FConnection:= TSQLite3Connection.Create(nil);

src/paslogbot.lpr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ procedure TPasLogBot.DoRun;
152152
debug('Creating IRC client...');
153153
FIRCLogBot:= TIRCLogBot.Create(config);
154154
debug('Successfully created IRC client.');
155-
debug('Starting...');
155+
info('Starting...');
156156
FIRCLogBot.Run;
157157
while not Terminated do
158158
begin
@@ -161,7 +161,7 @@ procedure TPasLogBot.DoRun;
161161
FIRCLogBot.Shutdown;
162162
FIRCLogBot.Free;
163163
config.Free;
164-
debug('Exiting.');
164+
info('Exiting.');
165165
// stop program loop
166166
//Terminate;
167167
end;

src/threads/irclogbot.replay.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ procedure TReplayThread.Execute;
110110
end;
111111
end;
112112
FIRC.Say(bundle.Nick, Format('*** End of the last %d lines ***', [bundle.Lines.Count]));
113+
bundle.Free;
113114
end
114115
else
115116
begin

0 commit comments

Comments
 (0)