Skip to content

Commit 06535ff

Browse files
committed
feat: Hooking to signals
1 parent 242b919 commit 06535ff

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
77
An attempt at producing an `IRC` bot that logs a channel's content.
88

9-
# Preamble
9+
## Preamble
1010

11-
In terms of internet protocols we can choose between `Indy` and `Synapse` as the libraries available. I decided that I was going to use `Indy` due to the fact that it already has an `IRC` client and I didn't want to do all the necessary string parsing for the `IRC` protocol.
11+
In terms of internet protocols we can choose between `Indy` and `Synapse` as the libraries available. I decided that I was going to use `Indy` due to the fact that it already has an `IRC` client and I didn't want to do all the necessary string parsing for the `IRC` protocol.
12+
13+
## Features
14+
15+
The initial idea is to have all the logging of the `IRC` channel dumped into a `SQLite` database. To query the database, the objective is twofold:
16+
17+
1. Via commands on `IRC`
18+
2. Via a web site that the bot, itself, serves
19+
20+
Not sure if it's worth having the bot itself serve the website, but due to the fact that `SQLite` will be locked during the bot's operation, then this will have to be the path.

src/bot/bot.pas

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
unit Bot;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses
8+
Classes
9+
, SysUtils
10+
;
11+
12+
implementation
13+
14+
end.
15+

src/paslogbot.lpi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</Target>
2424
<SearchPaths>
2525
<IncludeFiles Value="$(ProjOutDir)"/>
26+
<OtherUnitFiles Value="bot"/>
2627
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
2728
</SearchPaths>
2829
<Parsing>
@@ -56,6 +57,7 @@
5657
</Target>
5758
<SearchPaths>
5859
<IncludeFiles Value="$(ProjOutDir)"/>
60+
<OtherUnitFiles Value="bot"/>
5961
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
6062
</SearchPaths>
6163
<CodeGeneration>
@@ -87,6 +89,11 @@
8789
<Filename Value="paslogbot.lpr"/>
8890
<IsPartOfProject Value="True"/>
8991
</Unit>
92+
<Unit>
93+
<Filename Value="bot/bot.pas"/>
94+
<IsPartOfProject Value="True"/>
95+
<UnitName Value="Bot"/>
96+
</Unit>
9097
</Units>
9198
</ProjectOptions>
9299
<CompilerOptions>
@@ -96,6 +103,7 @@
96103
</Target>
97104
<SearchPaths>
98105
<IncludeFiles Value="$(ProjOutDir)"/>
106+
<OtherUnitFiles Value="bot"/>
99107
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
100108
</SearchPaths>
101109
</CompilerOptions>

src/paslogbot.lpr

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
cthreads,
88
BaseUnix,
99
{$ENDIF}
10-
Classes, SysUtils, CustApp
10+
Classes, SysUtils, CustApp, Bot
1111
{ you can add units after this };
1212

1313
type
@@ -32,13 +32,14 @@ procedure SignalHandler(signal: longint; info: psiginfo; context: psigcontext);
3232
case signal of
3333
SIGTERM, SIGINT:
3434
begin
35+
WriteLn;
3536
WriteLn('Received termination signal');
3637
if Assigned(Application) then
3738
Application.Terminate;
3839
end;
3940
SIGHUP:
4041
begin
41-
WriteLn('Received SIGHUP - could implement config reload here');
42+
//WriteLn('Received SIGHUP - could implement config reload here');
4243
// Could implement configuration reload here
4344
end;
4445
end;
@@ -49,8 +50,8 @@ procedure SetupSignalHandlers;
4950
act: SigActionRec;
5051
begin
5152
FillChar(act, SizeOf(act), 0);
52-
act.sa_handler := @SignalHandler;
53-
act.sa_flags := 0;
53+
act.sa_handler:= @SignalHandler;
54+
act.sa_flags:= 0;
5455

5556
// Set up signal handlers
5657
fpSigAction(SIGTERM, @act, nil);
@@ -87,33 +88,39 @@ procedure TPasLogBot.DoRun;
8788
var
8889
ErrorMsg: String;
8990
begin
91+
// Signal Handling
92+
SetupSignalHandlers;
9093
// quick check parameters
91-
ErrorMsg:=CheckOptions('h', 'help');
92-
if ErrorMsg<>'' then begin
94+
ErrorMsg:= CheckOptions('h', 'help');
95+
if ErrorMsg<>'' then
96+
begin
9397
//ShowException(Exception.Create(ErrorMsg));
9498
Terminate;
95-
Exit;
99+
exit;
96100
end;
97101

98102
// parse parameters
99-
if HasOption('h', 'help') then begin
103+
if HasOption('h', 'help') then
104+
begin
100105
WriteHelp;
101106
Terminate;
102-
Exit;
107+
exit;
103108
end;
104109

110+
WriteLn('Starting...');
105111
while not Terminated do
106112
begin
107113
Sleep(50);
108114
end;
115+
WriteLn('Exiting.');
109116
// stop program loop
110117
//Terminate;
111118
end;
112119

113120
constructor TPasLogBot.Create(TheOwner: TComponent);
114121
begin
115122
inherited Create(TheOwner);
116-
StopOnException:=True;
123+
StopOnException:= True;
117124
end;
118125

119126
destructor TPasLogBot.Destroy;
@@ -124,12 +131,12 @@ destructor TPasLogBot.Destroy;
124131
procedure TPasLogBot.WriteHelp;
125132
begin
126133
{ add your help code here }
127-
writeln('Usage: ', ExeName, ' -h');
134+
WriteLn('Usage: ', ExtractFileName(ExeName), ' -h');
128135
end;
129136

130137
begin
131-
Application:=TPasLogBot.Create(nil);
132-
Application.Title:='Pascal Log Bot';
138+
Application:= TPasLogBot.Create(nil);
139+
Application.Title:= 'Pascal Log Bot';
133140
Application.Run;
134141
Application.Free;
135142
end.

0 commit comments

Comments
 (0)