Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ircconnection.cpp

Go to the documentation of this file.
00001 #include <unistd.h>
00002 #include <string.h>
00003 
00004 #include <qstringlist.h>
00005 #include <qdatetime.h>
00006 
00007 #include "ircconnection.h"
00008 
00009 IRCConnection::IRCConnection(IRCServer *server) {
00010     m_server = server;
00011     m_socket = new QSocket(this);
00012     m_connected = FALSE;
00013     m_loggedIn = FALSE;
00014     connect(m_socket, SIGNAL(connected()), this, SLOT(login()));
00015     connect(m_socket, SIGNAL(readyRead()), this, SLOT(dataReady()));
00016     connect(m_socket, SIGNAL(error(int)), this, SLOT(error(int)));
00017     connect(m_socket, SIGNAL(connectionClosed()), this, SLOT(disconnect()));
00018     connect(m_socket, SIGNAL(delayedCloseFinished()), this, SLOT(disconnect()));
00019 }
00020 
00021 /* Connect to the IRC server */
00022 void IRCConnection::doConnect() {
00023     ASSERT(!m_connected);
00024     m_socket->connectToHost(m_server->hostname(), m_server->port());
00025 }
00026 
00027 /* Send commands to the IRC server */
00028 void IRCConnection::sendLine(QString line) {
00029     while((line.right(1) == "\n") || (line.right(1) == "\r"))
00030         line = line.left(line.length() - 1);
00031     line.append("\r\n");
00032     m_socket->writeBlock(line, line.length());
00033 }
00034 
00035 void IRCConnection::sendCTCPReply(const QString &nickname, const QString &type, const QString &args) {
00036     sendLine("NOTICE " + nickname + " :\001" + type + " " + args + "\001"); 
00037 }
00038 
00039 void IRCConnection::sendCTCPRequest(const QString &nickname, const QString &type, const QString &args) {
00040     sendLine("PRIVMSG " + nickname + " :\001" + type + " " + args + "\001"); 
00041 }
00042 
00043 void IRCConnection::sendCTCPPing(const QString &nickname) {
00044     QDateTime tm;
00045     tm.setTime_t(0);
00046     QString strtime = QString::number(tm.secsTo(QDateTime::currentDateTime()));
00047     sendCTCPRequest(nickname, "PING", strtime);
00048 }
00049 
00050 void IRCConnection::whois(const QString &nickname) {
00051     sendLine("WHOIS " + nickname);
00052 }
00053 
00054 /*
00055  * login() is called right after the connection
00056  * to the IRC server has been established
00057  */
00058 void IRCConnection::login() {
00059     char hostname[256];
00060     QString loginString;
00061     
00062     emit outputReady(IRCOutput(OUTPUT_CLIENTMESSAGE, tr("Connected, logging in ..")));
00063     m_connected = TRUE;
00064     gethostname(hostname, sizeof(hostname)-1);
00065     hostname[sizeof (hostname) - 1] = 0;
00066     
00067     /* Create a logon string and send it */
00068     if (m_server->password().length()>0) {
00069         loginString += "PASS " + m_server->password() + "\r\n";
00070     }
00071     loginString += "NICK " + m_server->nick() + "\r\n" +
00072                    "USER " + m_server->username() + " " + hostname +
00073                    " " + m_server->hostname() + " :" + m_server->realname() + "\r\n";
00074     sendLine(loginString);
00075 }
00076 
00077 /* Called when data arrives on the socket */
00078 void IRCConnection::dataReady() {
00079     while(m_socket->canReadLine()) {
00080         IRCMessage message(m_socket->readLine());
00081         if (!m_loggedIn && message.isNumerical() && message.commandNumber() == 1) {
00082             /* Now autojoin all channels specified inside the server profile */
00083             QStringList channels = QStringList::split(QChar(','), m_server->channels());
00084             for (QStringList::Iterator it = channels.begin(); it != channels.end(); ++it) {
00085                 QString channelName = (*it).stripWhiteSpace();
00086                 if (channelName.startsWith("#") || channelName.startsWith("+")) {
00087                     sendLine("JOIN "+ channelName); 
00088                 }
00089             }
00090             m_loggedIn = TRUE;
00091             emit outputReady(IRCOutput(OUTPUT_CLIENTMESSAGE, tr("Successfully logged in.")));
00092         }
00093         emit messageArrived(&message);
00094     }
00095 }
00096 
00097 /* Called if any type of socket error occurs */
00098 void IRCConnection::error(int num) {
00099     emit outputReady(IRCOutput(OUTPUT_ERROR, tr("Socket error : ") + strerror(num)));
00100 }
00101 
00102 void IRCConnection::disconnect() {
00103     m_connected = FALSE;
00104     m_loggedIn = FALSE;
00105     emit outputReady(IRCOutput(OUTPUT_CONNCLOSE, tr("Connection closed")));
00106 }
00107 
00108 bool IRCConnection::isConnected() {
00109     return m_connected;
00110 }
00111 
00112 bool IRCConnection::isLoggedIn() {
00113     return m_loggedIn;
00114 }
00115 
00116 void IRCConnection::close() {
00117     m_socket->close(); 
00118     if (m_socket->state()==QSocket::Idle) {
00119         disconnect();
00120     }
00121 }
00122 

Generated on Sat Nov 5 16:17:40 2005 for OPIE by  doxygen 1.4.2