Skip to content

Commit ad2fb96

Browse files
committed
feat: Command .help implemented
1 parent 06535ff commit ad2fb96

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ The initial idea is to have all the logging of the `IRC` channel dumped into a `
1717
1. Via commands on `IRC`
1818
2. Via a web site that the bot, itself, serves
1919

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.
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, for the time being.

src/bot/bot.pas

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,157 @@ interface
77
uses
88
Classes
99
, SysUtils
10+
, IdContext
11+
, IdIRC
1012
;
1113

14+
type
15+
{ TIRCLogBot }
16+
TIRCLogBot = class(TObject)
17+
private
18+
FIRC: TIdIRC;
19+
FNickName: String;
20+
FUserName: String;
21+
FRealName: String;
22+
FHost: String;
23+
FPort: Word;
24+
FChannel: String;
25+
26+
FJoinedChannel: Boolean;
27+
28+
procedure OnConnected(Sender: TObject);
29+
procedure OnDisconnected(Sender: TObject);
30+
procedure OnNotice(ASender: TIdContext; const ANickname, AHost,
31+
ATarget, ANotice: String);
32+
procedure OnJoin(ASender: TIdContext; const ANickname, AHost,
33+
AChannel: String);
34+
procedure OnPrivateMessage(ASender: TIdContext; const ANickname, AHost,
35+
ATarget, AMessage: String);
36+
protected
37+
public
38+
constructor Create(ANickName, AUserName, ARealName, AHost: String;
39+
APort: Word; AChannel: String);
40+
destructor Destroy; override;
41+
42+
procedure Run;
43+
procedure Shutdown;
44+
published
45+
end;
46+
1247
implementation
1348

49+
{ TIRCLogBot }
50+
51+
procedure TIRCLogBot.OnConnected(Sender: TObject);
52+
begin
53+
WriteLn('Connected to server');
54+
end;
55+
56+
procedure TIRCLogBot.OnDisconnected(Sender: TObject);
57+
begin
58+
WriteLn('Disconnected from server');
59+
end;
60+
61+
procedure TIRCLogBot.OnNotice(ASender: TIdContext; const ANickname, AHost,
62+
ATarget, ANotice: String);
63+
begin
64+
WriteLn(Format('>> NOTICE: <%s> "%s"', [
65+
ANickname,
66+
ANotice
67+
]));
68+
end;
69+
70+
procedure TIRCLogBot.OnJoin(ASender: TIdContext; const ANickname, AHost,
71+
AChannel: String);
72+
begin
73+
WriteLn(Format('>> JOIN: <%s@%s> %s', [
74+
ANickname,
75+
AHost,
76+
AChannel
77+
]));
78+
if (ANickname = FNickName) and (AChannel = FChannel) then
79+
begin
80+
WriteLn('Successfully joined my channel');
81+
FJoinedChannel:= True;
82+
end;
83+
end;
84+
85+
procedure TIRCLogBot.OnPrivateMessage(ASender: TIdContext; const ANickname,
86+
AHost, ATarget, AMessage: String);
87+
begin
88+
WriteLn(Format('>> PRIVMSG: <%s@%s>(%s) "%s"', [
89+
ANickname,
90+
AHost,
91+
ATarget,
92+
AMessage
93+
]));
94+
if ATarget = FNickName then
95+
begin
96+
if Pos('.', AMessage) = 1 then
97+
begin
98+
// Parse commands
99+
if Pos('.help', Trim(AMessage)) = 1 then
100+
begin
101+
WriteLn('Help command.');
102+
FIRC.Say(ANickname, 'Commands:');
103+
FIRC.Say(ANickname, '.help - This help information');
104+
end;
105+
end
106+
else
107+
begin
108+
WriteLn('No command.');
109+
FIRC.Say(ANickname, 'Not a command. Please use ".help" to see a list of commands.');
110+
end;
111+
end;
112+
end;
113+
114+
procedure TIRCLogBot.Run;
115+
begin
116+
WriteLn('Connecting...');
117+
FIRC.Connect;
118+
WriteLn('Joining channel: "', FChannel, '"...');
119+
FIRC.Join(FChannel);
120+
end;
121+
122+
procedure TIRCLogBot.Shutdown;
123+
begin
124+
if FIRC.Connected then
125+
begin
126+
WriteLn('Disconnecting...');
127+
FIRC.Disconnect('Quitting.');
128+
end;
129+
end;
130+
131+
constructor TIRCLogBot.Create(ANickName, AUserName, ARealName, AHost: String;
132+
APort: Word; AChannel: String);
133+
begin
134+
FNickname:= ANickname;
135+
FUsername:= AUserName;
136+
FRealName:= ARealName;
137+
FHost:= AHost;
138+
FPort:= APort;
139+
FChannel:= AChannel;
140+
141+
FIRC:= TIdIRC.Create;
142+
FIRC.Nickname:= FNickName;
143+
FIRC.Username:= FUserName;
144+
FIRC.RealName:= FRealName;
145+
FIRC.Host:= FHost;
146+
FIRC.Port:= FPort;
147+
FIRC.OnConnected:= @OnConnected;
148+
FIRC.OnDisconnected:= @OnDisconnected;
149+
FIRC.OnJoin:= @OnJoin;
150+
FIRC.OnNotice:= @OnNotice;
151+
FIRC.OnPrivateMessage:= @OnPrivateMessage;
152+
153+
FJoinedChannel:= False;
154+
end;
155+
156+
destructor TIRCLogBot.Destroy;
157+
begin
158+
FIRC.Free;
159+
inherited Destroy;
160+
end;
161+
14162
end.
15163

src/paslogbot.lpi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
<RunParams>
8585
<FormatVersion Value="2"/>
8686
</RunParams>
87+
<RequiredPackages>
88+
<Item>
89+
<PackageName Value="indylaz"/>
90+
</Item>
91+
</RequiredPackages>
8792
<Units>
8893
<Unit>
8994
<Filename Value="paslogbot.lpr"/>

src/paslogbot.lpr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
{ TPasLogBot }
1616
TPasLogBot = class(TCustomApplication)
17+
private
18+
FIRCLogBot: TIRCLogBot;
1719
protected
1820
procedure DoRun; override;
1921
public
@@ -108,10 +110,12 @@ procedure TPasLogBot.DoRun;
108110
end;
109111

110112
WriteLn('Starting...');
113+
FIRCLogBot.Run;
111114
while not Terminated do
112115
begin
113116
Sleep(50);
114117
end;
118+
FIRCLogBot.Shutdown;
115119
WriteLn('Exiting.');
116120
// stop program loop
117121
//Terminate;
@@ -121,10 +125,19 @@ constructor TPasLogBot.Create(TheOwner: TComponent);
121125
begin
122126
inherited Create(TheOwner);
123127
StopOnException:= True;
128+
FIRCLogBot:= TIRCLogBot.Create(
129+
'[PasLogBot]',
130+
'paslogbot',
131+
'Pascal channel IRC log bot',
132+
'localhost',
133+
6667,
134+
'#test'
135+
);
124136
end;
125137

126138
destructor TPasLogBot.Destroy;
127139
begin
140+
FIRCLogBot.Free;
128141
inherited Destroy;
129142
end;
130143

0 commit comments

Comments
 (0)