Skip to content

Commit b65b6b7

Browse files
committed
feat: Skeleton of the application
1 parent d2d1df7 commit b65b6b7

9 files changed

+2848
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Pascal IRC Log Bot
22

3-
> [!]Important
3+
> [!Important]
44
> Since I was lazy enough to not bother with the boilerplate, I asked some `AI` bots to help me.\
5-
> You can find the collected answers on the [reserach](research) folder.
5+
> You can find the collected answers on the [research](research) folder.
66
77
An attempt at producing an `IRC` bot that logs a channel's content.
88

research/ChapGTP-IRC-Bot-Indy.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Prompt
2+
3+
Can you please give me a simple example of an `IRC` logging bot using Free Pascal with the `IRC` client from `Indy`?
4+
5+
# Answer
6+
7+
Here's an example of an IRC logging bot using Free Pascal with the Indy `IRC` client component (`TIdIRC`). This bot connects to an IRC server, joins a channel, and logs messages to a file.
8+
9+
### Example Code
10+
11+
```pas
12+
program IRCLoggerBot;
13+
14+
{$mode objfpc}{$H+}
15+
16+
uses
17+
SysUtils, Classes, IdIRC, IdGlobal, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient;
18+
19+
type
20+
TIRCLoggerBot = class
21+
private
22+
FIRC: TIdIRC;
23+
FLogFile: TextFile;
24+
procedure IRCConnect(AThread: TObject);
25+
procedure IRCDisconnect(AThread: TObject);
26+
procedure IRCMessage(ASender: TIdCommand; const AText: string);
27+
procedure OpenLogFile(const FileName: string);
28+
procedure CloseLogFile;
29+
procedure WriteLog(const Msg: string);
30+
public
31+
constructor Create;
32+
destructor Destroy; override;
33+
procedure Start(const Server, Nickname, Channel: string; Port: Integer);
34+
end;
35+
36+
{ TIRCLoggerBot }
37+
38+
constructor TIRCLoggerBot.Create;
39+
begin
40+
FIRC := TIdIRC.Create(nil);
41+
FIRC.OnConnect := @IRCConnect;
42+
FIRC.OnDisconnect := @IRCDisconnect;
43+
FIRC.OnPrivMessage := @IRCMessage;
44+
end;
45+
46+
destructor TIRCLoggerBot.Destroy;
47+
begin
48+
FIRC.Disconnect;
49+
CloseLogFile;
50+
FIRC.Free;
51+
inherited Destroy;
52+
end;
53+
54+
procedure TIRCLoggerBot.IRCConnect(AThread: TObject);
55+
begin
56+
WriteLog('Connected to IRC server.');
57+
end;
58+
59+
procedure TIRCLoggerBot.IRCDisconnect(AThread: TObject);
60+
begin
61+
WriteLog('Disconnected from IRC server.');
62+
end;
63+
64+
procedure TIRCLoggerBot.IRCMessage(ASender: TIdCommand; const AText: string);
65+
var
66+
LogMessage: string;
67+
begin
68+
LogMessage := Format('[%s] <%s> %s', [FormatDateTime('hh:nn:ss', Now), ASender.Params[0], AText]);
69+
WriteLog(LogMessage);
70+
end;
71+
72+
procedure TIRCLoggerBot.OpenLogFile(const FileName: string);
73+
begin
74+
AssignFile(FLogFile, FileName);
75+
if FileExists(FileName) then
76+
Append(FLogFile)
77+
else
78+
Rewrite(FLogFile);
79+
end;
80+
81+
procedure TIRCLoggerBot.CloseLogFile;
82+
begin
83+
CloseFile(FLogFile);
84+
end;
85+
86+
procedure TIRCLoggerBot.WriteLog(const Msg: string);
87+
begin
88+
WriteLn(FLogFile, Msg);
89+
Flush(FLogFile);
90+
end;
91+
92+
procedure TIRCLoggerBot.Start(const Server, Nickname, Channel: string; Port: Integer);
93+
begin
94+
OpenLogFile('IRCLog.txt');
95+
WriteLog('Starting IRC bot.');
96+
97+
FIRC.Host := Server;
98+
FIRC.Port := Port;
99+
FIRC.Nickname := Nickname;
100+
101+
FIRC.Connect;
102+
if FIRC.Connected then
103+
begin
104+
WriteLog('Joining channel ' + Channel);
105+
FIRC.Join(Channel);
106+
end;
107+
end;
108+
109+
{ Main program }
110+
var
111+
Bot: TIRCLoggerBot;
112+
begin
113+
Bot := TIRCLoggerBot.Create;
114+
try
115+
Bot.Start('irc.example.com', 'PascalBot', '#examplechannel', 6667);
116+
WriteLn('Press Enter to exit...');
117+
ReadLn;
118+
finally
119+
Bot.Free;
120+
end;
121+
end.
122+
```
123+
124+
### Explanation
125+
126+
1. **Setup**:
127+
- The bot uses `TIdIRC` from Indy components for IRC communication.
128+
- Logging is done via a text file (`IRCLog.txt`).
129+
130+
2. **Event Handlers**:
131+
- `IRCConnect`: Logs a message when connected to the server.
132+
- `IRCDisconnect`: Logs a message when disconnected.
133+
- `IRCMessage`: Logs messages received in the IRC channel.
134+
135+
3. **File Handling**:
136+
- `OpenLogFile` opens (or creates) the log file for writing.
137+
- `CloseLogFile` ensures the file is properly closed when the bot exits.
138+
- `WriteLog` writes log messages to the file with timestamps.
139+
140+
4. **Connection**:
141+
- `Start` sets the IRC server, nickname, and channel. It also opens the log file and connects to the IRC server.
142+
143+
### Running the Code
144+
- Ensure Indy components are installed and added to your project.
145+
- Replace `irc.example.com` and `#examplechannel` with the actual IRC server and channel you want to connect to.
146+
147+
### Notes
148+
- This bot logs messages only. You can extend it to handle more IRC events (e.g., notices, kicks, etc.).
149+
- Ensure you have permissions to write to the directory where the bot runs.

0 commit comments

Comments
 (0)