author | wazlaf <wazlaf> | 2002-09-10 23:12:17 (UTC) |
---|---|---|
committer | wazlaf <wazlaf> | 2002-09-10 23:12:17 (UTC) |
commit | d478be344e2ba383a1e38d2a1705de1cdbe2e838 (patch) (unidiff) | |
tree | 9daf7b496c74c695ed8bd72a6ccd321f770b043c /noncore/net/opieirc/ircconnection.cpp | |
parent | 2d6c8bd7290b766c31c100cd2d66ef0d1d0c407c (diff) | |
download | opie-d478be344e2ba383a1e38d2a1705de1cdbe2e838.zip opie-d478be344e2ba383a1e38d2a1705de1cdbe2e838.tar.gz opie-d478be344e2ba383a1e38d2a1705de1cdbe2e838.tar.bz2 |
initial import of OpieIRC v0.1
Diffstat (limited to 'noncore/net/opieirc/ircconnection.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/opieirc/ircconnection.cpp | 94 |
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 | |||
5 | IRCConnection::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 */ | ||
18 | void 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 */ | ||
24 | void 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 | |||
31 | void 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 | */ | ||
39 | void 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 */ | ||
59 | void 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 */ | ||
71 | void IRCConnection::error(int num) { | ||
72 | emit outputReady(IRCOutput(OUTPUT_ERROR, tr("Socket error : ") + strerror(num))); | ||
73 | } | ||
74 | |||
75 | void IRCConnection::disconnect() { | ||
76 | m_connected = FALSE; | ||
77 | m_loggedIn = FALSE; | ||
78 | emit outputReady(IRCOutput(OUTPUT_CONNCLOSE, tr("Connection closed"))); | ||
79 | } | ||
80 | |||
81 | bool IRCConnection::isConnected() { | ||
82 | return m_connected; | ||
83 | } | ||
84 | |||
85 | bool IRCConnection::isLoggedIn() { | ||
86 | return m_loggedIn; | ||
87 | } | ||
88 | |||
89 | void IRCConnection::close() { | ||
90 | m_socket->close(); | ||
91 | if (m_socket->state()==QSocket::Idle) { | ||
92 | disconnect(); | ||
93 | } | ||
94 | } | ||