Follow me on Twitter to receive updates, free scripts and ongoing announcements!

TIDIRC Tutorial

Written by Codes Tips on March 21, 2009 – 6:47 am -

TIDIRC is a component of Indy Client, that can help you connect to an IRC server  channel. You can send and receive messages in order to communicate with the server.

There are some important properties which you can use it to specify the server properties:

Host - is the IRC server where you want to connect

Nickname - your nick that will appear in the channel.

ReadTimeout - the time that application should wait until the server don’t respond, it is specified in miliseconds.

RealName - the real name that will appear in the server 

Connecting to host

First you should connect to server and you should use the command:

//variable
IdIRC1: TIdIRC;// IDIRC1 is a variable of TIDIRC component.
//On button click
IDIRC1.Connect;

ON succesfull you have an even OnConnected, where you can do something after the connection is established.

After connecting you can receive each response row from the server with the event IdIRCRaw. With this event you have the row AContent which is a string and you can parse it to take the information from the server or other things that you are interested.

You can take the messages that the other users send to the server, get the status of the channel. 

 

Getting Channels

To get a list of channels you should use the property Channels and access the Items property.

 for i:=1 to idirc1.Channels.Items.count do 
  List[i] := idirc1.Channels.Items[i];//it's a TIDIRCChannel component

In TIDIRCChannel you will have the list of all channels and for every channel you have properties and events for handling a channel.
 Some time, this property will not grab the channels correctly. Here is a function to get the channels from the host. You should put it in the IDIRCRaw event and make a list of channels. This function return a channel:

//GetChannel function
 
function GetChannel(s:string):string;
var p1:integer;
    aux:string;
 begin
    p1:=pos('#',s);
    inc(p1);
    aux:='';
    if p1>1 then
    while  (s[p1]<>' ')and(p1<=length(s)) do
      begin
         aux:=aux+s[p1];
         inc(p1);
      end;
   GetChannel:=aux;
 end;
 
//calling the channel function
 channel:=GetChannel(continut);
 channel:='#'+channel;

Joining a channel

To join a channel on a server you should use the command

IDIRC1.Join(channel);

Sending message

To send a message to one of the users that are connected to the channel

TIDIRC.Say(touser,message)

Parting a channel

To part from a channel you should use the command

TIDIRC.Part(channel,reason)

You can specify an optional string parameter, what was the reason of parting.

Kick user

If you are OP of a channel you can kick a user by specifying the channel, the user to kick and a reason message why you kick it.

Other usefull functions for TIDIRC Component:

GetTopic(channel) - get the topic of the channel
SetTopic(channel,message) - if you are the  OP you can set the topic of a channel
Disconnect - disconnects from the current session.
IsOp(User:string) - test to see if a user is operator.
SetAwayMessage(message) - sets a message when you are not at the computer, to clear the message you should use ClearAwayMessage.

Thread IRC

You should use thread because when you connect to server or send a messsage the form will freeze and the user will not be able to see the form details.
To create a thread with the TIDIRC components you should use the following commands:
IDIRC1.IRCThread.Create(IDIRC1);
IDIRC1.IRCThread.Start;
To end a thread you should use stop it and dispose, like this:
IDIRC1.IRCThread.Terminate;
IDIRC1.IRCThread.Free;

Tags: ,
Posted in Delphi |

Leave a Comment

You must be logged in to post a comment.


Plugintaylor.com - Plugintaylor and Social Network