Skip to content

Commit 4f1ef66

Browse files
committed
feat: Debug and initial .replay command
1 parent 5e6e754 commit 4f1ef66

File tree

6 files changed

+159
-40
lines changed

6 files changed

+159
-40
lines changed

pasirclogbot.example.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[Main]
1+
[IRC]
22
Host="localhost"
33
Port=6667
44
NickName="[PasLogBot]"

src/bot/irclogbot.bot.pas

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ TIRCLogBot = class(TObject)
3333
AChannel: String);
3434
procedure OnPrivateMessage(ASender: TIdContext; const ANickname, AHost,
3535
ATarget, AMessage: String);
36+
37+
procedure Help(const ATarget: String);
38+
procedure Replay(const ATarget: String; Count: Integer);
3639
protected
3740
public
3841
constructor Create(AHost: String; APort: Word;
@@ -46,85 +49,145 @@ TIRCLogBot = class(TObject)
4649

4750
implementation
4851

52+
uses
53+
IRCLogBot.Common
54+
;
55+
4956
{ TIRCLogBot }
5057

5158
procedure TIRCLogBot.OnConnected(Sender: TObject);
5259
begin
53-
WriteLn('Connected to server');
60+
debug('Connected to server');
5461
end;
5562

5663
procedure TIRCLogBot.OnDisconnected(Sender: TObject);
5764
begin
58-
WriteLn('Disconnected from server');
65+
debug('Disconnected from server');
5966
end;
6067

6168
procedure TIRCLogBot.OnNotice(ASender: TIdContext; const ANickname, AHost,
6269
ATarget, ANotice: String);
6370
begin
64-
WriteLn(Format('>> NOTICE: <%s> "%s"', [
71+
debug('>> NOTICE: <%s> "%s"', [
6572
ANickname,
6673
ANotice
67-
]));
74+
]);
6875
end;
6976

7077
procedure TIRCLogBot.OnJoin(ASender: TIdContext; const ANickname, AHost,
7178
AChannel: String);
7279
begin
73-
WriteLn(Format('>> JOIN: <%s@%s> %s', [
80+
debug('>> JOIN: <%s@%s> %s', [
7481
ANickname,
7582
AHost,
7683
AChannel
77-
]));
84+
]);
7885
if (ANickname = FNickName) and (AChannel = FChannel) then
7986
begin
80-
WriteLn('Successfully joined my channel');
87+
debug('Successfully joined my channel');
8188
FJoinedChannel:= True;
89+
FIRC.Say(AChannel, 'I have arrived!!! TADAAAAA!!! Send me a private message with ".help" to see what I can do for you.');
8290
end;
8391
end;
8492

8593
procedure TIRCLogBot.OnPrivateMessage(ASender: TIdContext; const ANickname,
8694
AHost, ATarget, AMessage: String);
95+
var
96+
strings: TStringArray;
97+
count: Integer;
8798
begin
88-
WriteLn(Format('>> PRIVMSG: <%s@%s>(%s) "%s"', [
99+
debug('>> PRIVMSG: <%s@%s>(%s) "%s"', [
89100
ANickname,
90101
AHost,
91102
ATarget,
92103
AMessage
93-
]));
104+
]);
94105
if ATarget = FNickName then
95106
begin
96107
if Pos('.', AMessage) = 1 then
97108
begin
98109
// Parse commands
99110
if Pos('.help', Trim(AMessage)) = 1 then
100111
begin
101-
WriteLn('Help command.');
102-
FIRC.Say(ANickname, 'Commands:');
103-
FIRC.Say(ANickname, '.help - This help information');
112+
Help(ANickname);
113+
exit;
114+
end;
115+
if Pos('.replay', Trim(AMessage)) = 1 then
116+
begin
117+
strings:= AMessage.Split([' ']);
118+
try
119+
if Length(strings) > 1 then
120+
begin
121+
count:= StrToInt(strings[1]);
122+
end
123+
else
124+
begin
125+
count:= 0;
126+
end;
127+
Replay(ANickname, count);
128+
except
129+
FIRC.Say(ANickname, 'That <count> is not a number.');
130+
end;
131+
exit;
104132
end;
105133
end
106134
else
107135
begin
108-
WriteLn('No command.');
136+
debug('No command.');
109137
FIRC.Say(ANickname, 'Not a command. Please use ".help" to see a list of commands.');
110138
end;
111139
end;
112140
end;
113141

142+
procedure TIRCLogBot.Help(const ATarget: String);
143+
begin
144+
debug('Help command.');
145+
FIRC.Say(ATarget, 'Commands:');
146+
FIRC.Say(ATarget, '.help - This help information.');
147+
FIRC.Say(ATarget, '.replay [count] - Raplays last <count> lines. Default is last 10 lines.');
148+
end;
149+
150+
procedure TIRCLogBot.Replay(const ATarget: String; Count: Integer);
151+
begin
152+
debug('Replay command.');
153+
FIRC.Say(ATarget, Format('Not fully implemented yet: %d',[Count]));
154+
end;
155+
114156
procedure TIRCLogBot.Run;
115157
begin
116-
WriteLn('Connecting...');
117-
FIRC.Connect;
118-
WriteLn('Joining channel: "', FChannel, '"...');
119-
FIRC.Join(FChannel);
158+
debug('Connecting...');
159+
try
160+
FIRC.Connect;
161+
except
162+
on e:Exception do
163+
begin
164+
debug('Error connecting: %s', [e.Message]);
165+
end;
166+
end;
167+
debug('Joining channel: "%s"...', [FChannel]);
168+
try
169+
FIRC.Join(FChannel);
170+
except
171+
on e:Exception do
172+
begin
173+
debug('Error joining: %s', [e.Message]);
174+
end;
175+
end;
120176
end;
121177

122178
procedure TIRCLogBot.Shutdown;
123179
begin
124180
if FIRC.Connected then
125181
begin
126-
WriteLn('Disconnecting...');
127-
FIRC.Disconnect('Need to go and have a wee nap.');
182+
debug('Disconnecting...');
183+
try
184+
FIRC.Disconnect('Need to go and have a wee nap.');
185+
except
186+
on e:Exception do
187+
begin
188+
debug('Error: %s', [e.Message]);
189+
end;
190+
end;
128191
end;
129192
end;
130193

src/common/irclogbot.common.pas

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
unit IRCLogBot.Common;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses
8+
SysUtils
9+
;
10+
11+
var
12+
DebugOn: Boolean;
13+
14+
procedure debug(const AMessage: String); overload;
15+
procedure debug(const AFormat: String; AValues: array of const);overload;
16+
17+
implementation
18+
19+
var
20+
dateTimeStr: String;
21+
22+
procedure debug(const AMessage: String);
23+
begin
24+
if DebugOn then
25+
begin
26+
dateTimeStr:= FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz:', Now);
27+
WriteLn(Format('%s %s', [dateTimeStr, AMessage]));
28+
end;
29+
end;
30+
31+
procedure debug(const AFormat: String; AValues: array of const);
32+
begin
33+
if DebugOn then
34+
begin
35+
dateTimeStr:= FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz: ', Now);
36+
WriteLn(Format(dateTimeStr+AFormat, AValues));
37+
end;
38+
end;
39+
40+
end.
41+

src/config/irclogbot.config.pas

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ procedure TBotConfig.LoadValues;
7575
if FileExists(FINIFile) then
7676
begin
7777
FINI:= TIniFile.Create(FINIFile);
78-
FHost:= FINI.ReadString('Main', 'Host', 'localhost');
79-
FPort:= FINI.ReadInteger('Main', 'Port', 6667);
80-
FNickName:= FINI.ReadString('Main', 'NickName', '');
81-
FUserName:= FINI.ReadString('Main', 'UserName', '');
82-
FRealName:= FINI.ReadString('Main', 'RealName', '');
83-
FChannel:= FINI.ReadString('Main', 'Channel', '');
78+
FHost:= FINI.ReadString('IRC', 'Host', 'localhost');
79+
FPort:= FINI.ReadInteger('IRC', 'Port', 6667);
80+
FNickName:= FINI.ReadString('IRC', 'NickName', '');
81+
FUserName:= FINI.ReadString('IRC', 'UserName', '');
82+
FRealName:= FINI.ReadString('IRC', 'RealName', '');
83+
FChannel:= FINI.ReadString('IRC', 'Channel', '');
8484
end
8585
else
8686
begin

src/paslogbot.lpi

Lines changed: 8 additions & 3 deletions
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"/>
26+
<OtherUnitFiles Value="bot;config;common"/>
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"/>
60+
<OtherUnitFiles Value="bot;config;common"/>
6161
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
6262
</SearchPaths>
6363
<CodeGeneration>
@@ -104,6 +104,11 @@
104104
<IsPartOfProject Value="True"/>
105105
<UnitName Value="IRCLogBot.Config"/>
106106
</Unit>
107+
<Unit>
108+
<Filename Value="common/irclogbot.common.pas"/>
109+
<IsPartOfProject Value="True"/>
110+
<UnitName Value="IRCLogBot.Common"/>
111+
</Unit>
107112
</Units>
108113
</ProjectOptions>
109114
<CompilerOptions>
@@ -113,7 +118,7 @@
113118
</Target>
114119
<SearchPaths>
115120
<IncludeFiles Value="$(ProjOutDir)"/>
116-
<OtherUnitFiles Value="bot;config"/>
121+
<OtherUnitFiles Value="bot;config;common"/>
117122
<UnitOutputDirectory Value="../bin/lib/$(TargetCPU)-$(TargetOS)"/>
118123
</SearchPaths>
119124
</CompilerOptions>

src/paslogbot.lpr

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
cThreads,
88
BaseUnix,
99
{$ENDIF}
10-
Classes, SysUtils, CustApp, IRCLogBot.Bot, IRCLogBot.Config
10+
Classes, SysUtils, CustApp, IRCLogBot.Common, IRCLogBot.Bot, IRCLogBot.Config
1111
{ you can add units after this };
1212

1313
type
@@ -37,13 +37,13 @@ procedure SignalHandler(signal: longint; info: psiginfo; context: psigcontext);
3737
SIGTERM, SIGINT:
3838
begin
3939
WriteLn;
40-
WriteLn('Received termination signal');
40+
debug('Received termination signal');
4141
if Assigned(Application) then
4242
Application.Terminate;
4343
end;
4444
SIGHUP:
4545
begin
46-
//WriteLn('Received SIGHUP - could implement config reload here');
46+
//debug('Received SIGHUP - could implement config reload here');
4747
// Could implement configuration reload here
4848
end;
4949
end;
@@ -95,7 +95,7 @@ procedure TPasLogBot.DoRun;
9595
// Signal Handling
9696
SetupSignalHandlers;
9797
// quick check parameters
98-
ErrorMsg:= CheckOptions('hc:', ['help', 'config:']);
98+
ErrorMsg:= CheckOptions('hc:d', ['help', 'config:', 'debug']);
9999
if ErrorMsg<>'' then
100100
begin
101101
//ShowException(Exception.Create(ErrorMsg));
@@ -111,6 +111,7 @@ procedure TPasLogBot.DoRun;
111111
exit;
112112
end;
113113

114+
// Config
114115
if HasOption('c', 'config') then
115116
begin
116117
FConfigFile:= GetOptionValue('c', 'config');
@@ -120,7 +121,10 @@ procedure TPasLogBot.DoRun;
120121
FConfigFile:= ConcatPaths([GetUserDir, '.pasirclogbot']);
121122
end;
122123

123-
WriteLn(Format('Attempting to read config from: "%s"...', [FConfigFile]));
124+
// Debug
125+
DebugOn:= HasOption('d', 'debug');
126+
127+
debug(Format('Attempting to read config from: "%s"...', [FConfigFile]));
124128

125129
{ #todo 100 -ogcarreno : Parse param c/config }
126130
FBotConfig:= TBotConfig.Create(FConfigFile);
@@ -129,14 +133,14 @@ procedure TPasLogBot.DoRun;
129133
except
130134
on e:Exception do
131135
begin
132-
WriteLn(Format('Error: %s', [e.Message]));
136+
debug(Format('Error: %s', [e.Message]));
133137
Terminate;
134138
exit;
135139
end;
136140
end;
137141

138142
{ #todo 100 -ogcarreno : Use data from config }
139-
WriteLn('Creating IRC client...');
143+
debug('Creating IRC client...');
140144
FIRCLogBot:= TIRCLogBot.Create(
141145
FBotConfig.Host,
142146
FBotConfig.Port,
@@ -145,8 +149,8 @@ procedure TPasLogBot.DoRun;
145149
FBotConfig.Realname,
146150
FBotConfig.Channel
147151
);
148-
WriteLn('Successfully created IRC client.');
149-
WriteLn('Starting...');
152+
debug('Successfully created IRC client.');
153+
debug('Starting...');
150154
{ #todo 100 -ogcarreno : Read Config }
151155
FIRCLogBot.Run;
152156
while not Terminated do
@@ -155,7 +159,7 @@ procedure TPasLogBot.DoRun;
155159
end;
156160
FIRCLogBot.Shutdown;
157161
FIRCLogBot.Free;
158-
WriteLn('Exiting.');
162+
debug('Exiting.');
159163
// stop program loop
160164
//Terminate;
161165
end;
@@ -179,7 +183,13 @@ procedure TPasLogBot.WriteHelp;
179183
WriteLn;
180184
WriteLn('PARAMS:');
181185
WriteLn(' -h/--help This help message.');
182-
WriteLn(' -c/--config=FILE Use provided FILE as config. ( default: ~/.pasirclogbot )');
186+
{$IFDEF UNIX}
187+
WriteLn(' -c/--config=FILE Use provided FILE as config. ( default: ~/.config/pasirclogbot.conf )');
188+
{$ENDIF}
189+
{$IFDEF WINDOWS}
190+
WriteLn(' -c/--config=FILE Use provided FILE as config. ( default: %APPDATA%/pasirclogbot )');
191+
{$ENDIF}
192+
WriteLn(' -d/--debug Turn debug On. (default: Off)');
183193
end;
184194

185195
begin

0 commit comments

Comments
 (0)