Intégration simple d'un client irc à une application

Description

IRClib est une bibliothèque qui permet d'intégrer un client IRC rapidement et simplement
dans une application C++.

Pour l'utiliser, il suffit de l'initialiser, de se connecter, puis de définir
un gestionnaire d'évènements qui va se charger de gérer les interactions entre
votre client IRC et le serveur.

La source suivante montre l'utilisation d'IRClib.

Source / Exemple :


#include <iostream>
#include <irclib.h>

// ---------------------------------------------------------------------------

using namespace IRCLib;
using namespace std;

// ---------------------------------------------------------------------------

class evts;

// ---------------------------------------------------------------------------

IClientManager* pMgr	= NULL;
IClient*		pClient = NULL;

// ---------------------------------------------------------------------------

int main(int argc, char** av)
{
    // Create the manager
	pMgr = CreateClientManager();

    // Set the server information
	TServerInfo server("irc.uni-irc.net", 6667);

    // Set the client information
	TClientInfo me("test42");

    // Create the client
	pClient = pMgr->NewClient(server, me);

    // Create the messages handler
	pClient->CreateHandler<evts>();

    // Connect the client
	pClient->Connect();

    // Wait for the client to be connected and registered
	while (!pClient->IsRegistered())
		;

    // Create the #test channel
    IChannel* pChan = pClient->AddChannel("#test");

    // Wait for the #test channel to be created
    while (!(pChan = pClient->FindChannel("#test")))
        ;

    // Wait for the channel to be joined
    while (!pChan->OnThere())
        ;

    // Wait for the user to type a message
    char buffer[512] = {0};
    std::cin.getline(buffer, sizeof(buffer));

    // Send the message
    pChan->Message(buffer);

    // Quit the server
    pClient->Quit("Goodbye cruel world!");

    // Destroy the handler
	pClient->DestroyHandler<evts>();

    // Release the manager
	pMgr->Release();

	return 0;
}

// ---------------------------------------------------------------------------

class evts : public IEventHandler
{
protected:

    // Network error
    //
    //
	void OnError(IClient* pClient, int nError)
	{
		if (nError != CER_UNKCMODE)
		{
			cout << "Error(" << nError << "): " << pClient->GetLastErrorMessage() << endl;
		}

        if (nError == CER_NICKINUSE)
        {
            pClient->Nick(pClient->GetNickName() + "_");
        }

		pClient->ClearLastError();
	}

    // Connecting to a server
    //
    //
	void OnConnecting(IClient* pClient)
	{
		cout << "Connecting to server..." << endl;
	}

    // Connected
    //
    //
	void OnConnect(IClient* pClient)
	{
		cout << "Connected!" << endl;
	}

    // We receive a notice
    //
    //
	void OnNotice(IClient* pClient, const string& strFrom, const string& str)
	{
		cout << "(" << strFrom << ") " << str << endl;
	}

    // We receive a channel message
    //
    //
	void OnMessage(IClient* pClient, IChannel* pChan, IUser* pUser, const string& strMessage)
	{
		cout << "<" << pUser->GetName() << "@" << pChan->GetName() << "> " << strMessage << endl;
	}

    // We receive a private message
    //
    //
	void OnQuery(IClient* pCl, const string& strUser, const string& strMessage)
	{
		cout << "[" << ToNick(strUser) << "@" << ToHost(strUser) << "] " << strMessage << endl;
	}

    // Disconnected
    //
    //
	void OnDisconnect(IClient* pClient)
	{
		cout << "Disconnected." << endl;
	}

    // We, or someone, join a channel
    //
    //
	void OnJoin(IClient* pClient, IChannel* pChan, IUser* pUser)
	{
		if (pClient->IsMe(pUser))
		{
            // We entered the channel
		}
		else
		{
            // Someone entered the channel
			cout << pUser->GetName() << " has joined " << pChan->GetName() << endl;
		}
	}

    // Someone (might be us, use pClient->IsMe(pUser) to check) left the channel
    //
    //
	void OnPart(IClient* pClient, IChannel* pChan, IUser* pUser)
	{
		cout << pUser->GetName() << " has left " << pChan->GetName() << endl;
	}

    // Someone has been kicked
    //
    //
	void OnKick(IClient* pCl, IChannel* pChan, IUser* pBy, IUser* pWho, const string& why)
	{
		if (pCl->IsMe(pWho))
		{
			cout << "You were kicked from " << pChan->GetName() << " by " << pBy->GetName() << " (" << why << ")" << endl;
		}
		else
		{
			cout << pWho->GetName() << " was kicked from " << pChan->GetName() << " by " << pBy->GetName() << " (" << why << ")" << endl;
		}
	}

    // The channel mode has changed
    //
    //
	void OnMode(IClient* pCl, IChannel* pChan, IUser* pBy, const string& strModes)
	{
		cout << pBy->GetName() << " sets mode: " << strModes << endl;
	}

    // The user mode has changed
    //
    //
	void OnUMode(IClient* pCl, const string& strModes)
	{
		cout << "Modes changed to " << strModes << endl;
	}

    // We receive the name list for a channel
    //
    //
	void OnNames(IClient* pCl, IChannel* pChan)
	{
		TUserList users = pChan->LockUsers();
		for (TUserList::iterator itr = users.begin(); itr != users.end(); ++itr)
		{
			IUser* pUser = *itr;
			cout << "--> " << pUser->GetName() << " [" << pUser->GetModes() << "]" << endl;
		}
		pChan->UnlockUsers();
	}

    // Someone has changed his nick
    //
    //
	void OnNick(IClient* pCl, IUser* pUser, const string& to)
	{
		cout << pUser->GetName() << " is now known as " << to << endl;
	}

    // Received the topic for channel
    //
    //
	void OnGotTopic(IClient* pCl, IChannel* pChan, const string& strTopic)
	{
		cout << "Topic for " << pChan->GetName() << " is " << strTopic << endl;
	}

    // The topic has changed
    //
    //
	void OnTopic(IClient* pCl, IChannel* pChan, IUser* pUser, const string& strTopic)
	{
		cout << pUser->GetName() << " changes topic to '" << strTopic << "'" << endl;
	}

    // We have been invited to a channel
    //
    //
	void OnInvite(IClient* pCl, const string& strWho, const string& strChan)
	{
		cout << strWho << " invites you to join " << strChan << endl;
	}

    // Received channel modes
    //
    //
	void OnGotModes(IClient* pCl, IChannel* pChan, const string& strModes)
	{
		cout << "Channel " << pChan->GetName() << " modes: " << strModes << endl;
	}

    // Someone has quit IRC
    //
    //
	void OnQuit(IClient* pCl, IChannel* pChan, IUser* pUser, const string& strMsg)
	{
		cout << pUser->GetName() << " has quit IRC (" << strMsg << ")" << endl;
	}

    // You've been killed
    //
    //
	void OnKill(IClient* pCl, const string& str)
	{
		cout << "You've been killed: " << str << endl;
	}

    // An operator has changed the modes
    //
    //
	void OnOpMode(IClient* pCl, IChannel* pChan, IUser* pBy, IUser* pWho, const string& strModes)
	{
		cout << pBy->GetName() << " sets mode: " << strModes << " " << pWho->GetName() << endl;
	}

    // Someone has been banned
    //
    //
	void OnBanMode(IClient* pCl, IChannel* pChan, IUser* pBy, const string& strMask, const string& strModes)
	{
		cout << pBy->GetName() << " sets mode: " << strModes << " " << strMask << endl;
	}

    // Receive a whois result
    //
    //
	void OnWhois(IClient* pCl, const TWhois& w)
	{
        cout << w.UserInfo.Nickname << endl;
	}

    // Received the MOTD
    //
    //
	void OnMotd(IClient* pCl, const string& motd)
	{
        cout << motd << endl;
	}
};

Conclusion :


Vous pouvez télécharger IRClib (libs et headers) à l'adresse http://www.julz.fr/projects/irclib/vs2005/

Cette version n'est malheureusement pas documentée, faute de temps.
Je pense néanmoins que la plupart des informations nécessaires sont assez faciles
à comprendre par la simple lecture du fichier .h ;)

A noter que le projet n'aura une chance d'évoluer que s'il se montre intéressant
pour plusieurs utilisateurs.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.