summaryrefslogtreecommitdiff
path: root/noncore/net/opieirc/ircconnection.cpp
Unidiff
Diffstat (limited to 'noncore/net/opieirc/ircconnection.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/opieirc/ircconnection.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/noncore/net/opieirc/ircconnection.cpp b/noncore/net/opieirc/ircconnection.cpp
new file mode 100644
index 0000000..02c4897
--- a/dev/null
+++ b/noncore/net/opieirc/ircconnection.cpp
@@ -0,0 +1,94 @@
1#include <unistd.h>
2#include <string.h>
3#include "ircconnection.h"
4
5IRCConnection::IRCConnection(IRCServer *server) {
6 m_server = server;
7 m_socket = new QSocket(this);
8 m_connected = FALSE;
9 m_loggedIn = FALSE;
10 connect(m_socket, SIGNAL(connected()), this, SLOT(login()));
11 connect(m_socket, SIGNAL(readyRead()), this, SLOT(dataReady()));
12 connect(m_socket, SIGNAL(error(int)), this, SLOT(error(int)));
13 connect(m_socket, SIGNAL(connectionClosed()), this, SLOT(disconnect()));
14 connect(m_socket, SIGNAL(delayedCloseFinished()), this, SLOT(disconnect()));
15}
16
17/* Connect to the IRC server */
18void IRCConnection::doConnect() {
19 ASSERT(!m_connected);
20 m_socket->connectToHost(m_server->hostname(), m_server->port());
21}
22
23/* Send commands to the IRC server */
24void IRCConnection::sendLine(QString line) {
25 while((line.right(1) == "\n") || (line.right(1) == "\r"))
26 line = line.left(line.length() - 1);
27 line.append("\r\n");
28 m_socket->writeBlock(line, line.length());
29}
30
31void IRCConnection::sendCTCP(QString nick, QString line) {
32 sendLine("NOTICE " + nick + " :\001"+line+"\001");
33}
34
35/*
36 * login() is called right after the connection
37 * to the IRC server has been established
38 */
39void IRCConnection::login() {
40 char hostname[256];
41 QString loginString;
42
43 emit outputReady(IRCOutput(OUTPUT_CLIENTMESSAGE, tr("Connected, logging in ..")));
44 m_connected = TRUE;
45 gethostname(hostname, sizeof(hostname)-1);
46 hostname[sizeof (hostname) - 1] = 0;
47
48 /* Create a logon string and send it */
49 if (m_server->password().length()>0) {
50 loginString += "PASS " + m_server->password() + "\r\n";
51 }
52 loginString += "NICK " + m_server->nick() + "\r\n" +
53 "USER " + m_server->username() + " " + hostname +
54 " " + m_server->hostname() + " :" + m_server->realname() + "\r\n";
55 sendLine(loginString);
56}
57
58/* Called when data arrives on the socket */
59void IRCConnection::dataReady() {
60 while(m_socket->canReadLine()) {
61 IRCMessage message(m_socket->readLine());
62 if (!m_loggedIn && message.isNumerical() && message.commandNumber() == 1) {
63 m_loggedIn = TRUE;
64 emit outputReady(IRCOutput(OUTPUT_CLIENTMESSAGE, tr("Successfully logged in.")));
65 }
66 emit messageArrived(&message);
67 }
68}
69
70/* Called if any type of socket error occurs */
71void IRCConnection::error(int num) {
72 emit outputReady(IRCOutput(OUTPUT_ERROR, tr("Socket error : ") + strerror(num)));
73}
74
75void IRCConnection::disconnect() {
76 m_connected = FALSE;
77 m_loggedIn = FALSE;
78 emit outputReady(IRCOutput(OUTPUT_CONNCLOSE, tr("Connection closed")));
79}
80
81bool IRCConnection::isConnected() {
82 return m_connected;
83}
84
85bool IRCConnection::isLoggedIn() {
86 return m_loggedIn;
87}
88
89void IRCConnection::close() {
90 m_socket->close();
91 if (m_socket->state()==QSocket::Idle) {
92 disconnect();
93 }
94}