11 #include "networkirc.hpp"
13 using namespace Huggle::IRC;
15 NetworkIrc::NetworkIrc(QString server, QString nick)
17 this->messages_lock =
new QMutex(QMutex::Recursive);
18 this->writer_lock =
new QMutex(QMutex::Recursive);
19 this->__IsConnecting =
false;
20 this->Ident =
"huggle";
23 this->Server = server;
24 this->UserName =
"Huggle client";
25 this->
s =
new QTcpSocket();
26 this->NetworkThread = NULL;
27 this->__Connected =
false;
30 NetworkIrc::~NetworkIrc()
32 delete this->messages_lock;
33 delete this->NetworkThread;
34 delete this->writer_lock;
40 this->
s->connectToHost(this->Server, this->Port);
41 this->__IsConnecting =
true;
42 if (!this->
s->waitForConnected())
45 this->__IsConnecting =
false;
49 this->NetworkThread->root =
this;
50 this->__Connected =
true;
51 this->NetworkThread->start();
57 return this->__Connected;
60 bool NetworkIrc::IsConnecting()
62 return this->__IsConnecting;
65 void NetworkIrc::Disconnect()
71 this->Data(
"QUIT :Huggle, the anti vandalism software. See #huggle on irc://chat.freenode.net");
75 this->
s->disconnect();
81 void NetworkIrc::Join(QString name)
83 this->Data(
"JOIN " + name);
86 void NetworkIrc::Part(QString name)
88 this->Data(
"PART " + name);
91 void NetworkIrc::Data(QString text)
97 this->writer_lock->lock();
98 this->
s->write((text + QString(
"\n")).toUtf8());
99 this->writer_lock->unlock();
102 void NetworkIrc::Send(QString name, QString text)
104 this->Data(
"PRIVMSG " + name +
" :" + text);
107 void NetworkIrc::Exit()
109 this->__Connected =
false;
112 Message* NetworkIrc::GetMessage()
115 this->messages_lock->lock();
116 if (this->Messages.count() == 0)
118 this->messages_lock->unlock();
122 message =
new Message(Messages.at(0));
123 this->Messages.removeAt(0);
125 this->messages_lock->unlock();
130 Message::Message(QString text,
User us)
137 Message::Message(
const Message &ms)
139 this->Text = ms.Text;
141 this->user = ms.user;
144 Message::Message(QString chan, QString text,
User us)
160 this->Text = ms->Text;
161 this->user = ms->user;
165 User::User(QString nick, QString ident, QString host)
179 User::User(
User *user)
181 this->Host = user->Host;
182 this->Ident = user->Ident;
183 this->Nick = user->Nick;
186 User::User(
const User &user)
188 this->Host = user.Host;
189 this->Ident = user.Ident;
190 this->Nick = user.Nick;
193 NetworkIrc_th::NetworkIrc_th(QTcpSocket *socket)
196 this->Stopped =
false;
201 NetworkIrc_th::~NetworkIrc_th()
208 QString Command =
"";
210 if (!line.startsWith(
":") || !line.contains(
" "))
217 Source = xx.mid(0, xx.indexOf(
" "));
218 xx= xx.mid(xx.indexOf(
" ") + 1);
219 if (!xx.contains(
" "))
224 Command = xx.mid(0, xx.indexOf(
" "));
225 xx = xx.mid(xx.indexOf(
" ") + 1);
234 if (Command ==
"PRIVMSG")
236 ProcessPrivmsg(Source, xx);
241 void NetworkIrc_th::ProcessPrivmsg(QString source, QString xx)
244 user.Nick = source.mid(0, source.indexOf(
"!"));
246 if (!xx.contains(
"#") || !xx.contains(
" :"))
250 message.Channel = xx.mid(xx.indexOf(
"#"), xx.indexOf(
" :"));
252 xx = xx.replace(
"\r\n",
"");
253 message.Text = xx.mid(xx.indexOf(
" :") + 2);
254 this->root->messages_lock->lock();
255 this->root->Messages.append(message);
256 this->root->messages_lock->unlock();
259 bool NetworkIrc_th::IsFinished()
264 void NetworkIrc_th::run()
266 this->root->Data(
"USER " + this->root->Ident +
" 8 * :" + this->root->UserName);
267 qsrand(QTime::currentTime().msec());
268 QString nick = this->root->Nick + QString::number(qrand());
269 nick = nick.replace(
" ",
"");
270 this->root->Data(
"NICK " + nick);
277 this->root->Data(
"PING :" + this->root->Server);
280 QString data(
s->readLine());
285 this->usleep(100000);
The NetworkIrc_th class is a network thread for Network Irc.
QTcpSocket * s
Pointer to a QT Socket that is handling the connection to irc server this object is managed by parent...
Represent a message on irc network sent either to a channel or to a user.
bool IsConnected()
IsConnected checks for connection.
The User class represent a user of irc network.
Represent a channel on IRC network.
void Connect()
Connect to server.
QTcpSocket * s
Pointer to a QT Socket that is handling the connection to irc server owned by this class...