Can you please give me a simple example of an IRC logging bot using Free Pascal with the IRC client from Indy?
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.
program IRCLoggerBot;
{$mode objfpc}{$H+}
uses
SysUtils, Classes, IdIRC, IdGlobal, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient;
type
TIRCLoggerBot = class
private
FIRC: TIdIRC;
FLogFile: TextFile;
procedure IRCConnect(AThread: TObject);
procedure IRCDisconnect(AThread: TObject);
procedure IRCMessage(ASender: TIdCommand; const AText: string);
procedure OpenLogFile(const FileName: string);
procedure CloseLogFile;
procedure WriteLog(const Msg: string);
public
constructor Create;
destructor Destroy; override;
procedure Start(const Server, Nickname, Channel: string; Port: Integer);
end;
{ TIRCLoggerBot }
constructor TIRCLoggerBot.Create;
begin
FIRC := TIdIRC.Create(nil);
FIRC.OnConnect := @IRCConnect;
FIRC.OnDisconnect := @IRCDisconnect;
FIRC.OnPrivMessage := @IRCMessage;
end;
destructor TIRCLoggerBot.Destroy;
begin
FIRC.Disconnect;
CloseLogFile;
FIRC.Free;
inherited Destroy;
end;
procedure TIRCLoggerBot.IRCConnect(AThread: TObject);
begin
WriteLog('Connected to IRC server.');
end;
procedure TIRCLoggerBot.IRCDisconnect(AThread: TObject);
begin
WriteLog('Disconnected from IRC server.');
end;
procedure TIRCLoggerBot.IRCMessage(ASender: TIdCommand; const AText: string);
var
LogMessage: string;
begin
LogMessage := Format('[%s] <%s> %s', [FormatDateTime('hh:nn:ss', Now), ASender.Params[0], AText]);
WriteLog(LogMessage);
end;
procedure TIRCLoggerBot.OpenLogFile(const FileName: string);
begin
AssignFile(FLogFile, FileName);
if FileExists(FileName) then
Append(FLogFile)
else
Rewrite(FLogFile);
end;
procedure TIRCLoggerBot.CloseLogFile;
begin
CloseFile(FLogFile);
end;
procedure TIRCLoggerBot.WriteLog(const Msg: string);
begin
WriteLn(FLogFile, Msg);
Flush(FLogFile);
end;
procedure TIRCLoggerBot.Start(const Server, Nickname, Channel: string; Port: Integer);
begin
OpenLogFile('IRCLog.txt');
WriteLog('Starting IRC bot.');
FIRC.Host := Server;
FIRC.Port := Port;
FIRC.Nickname := Nickname;
FIRC.Connect;
if FIRC.Connected then
begin
WriteLog('Joining channel ' + Channel);
FIRC.Join(Channel);
end;
end;
{ Main program }
var
Bot: TIRCLoggerBot;
begin
Bot := TIRCLoggerBot.Create;
try
Bot.Start('irc.example.com', 'PascalBot', '#examplechannel', 6667);
WriteLn('Press Enter to exit...');
ReadLn;
finally
Bot.Free;
end;
end.-
Setup:
- The bot uses
TIdIRCfrom Indy components for IRC communication. - Logging is done via a text file (
IRCLog.txt).
- The bot uses
-
Event Handlers:
IRCConnect: Logs a message when connected to the server.IRCDisconnect: Logs a message when disconnected.IRCMessage: Logs messages received in the IRC channel.
-
File Handling:
OpenLogFileopens (or creates) the log file for writing.CloseLogFileensures the file is properly closed when the bot exits.WriteLogwrites log messages to the file with timestamps.
-
Connection:
Startsets the IRC server, nickname, and channel. It also opens the log file and connects to the IRC server.
- Ensure Indy components are installed and added to your project.
- Replace
irc.example.comand#examplechannelwith the actual IRC server and channel you want to connect to.
- This bot logs messages only. You can extend it to handle more IRC events (e.g., notices, kicks, etc.).
- Ensure you have permissions to write to the directory where the bot runs.