Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
networkirc.cpp
1 //This program is free software: you can redistribute it and/or modify
2 //it under the terms of the GNU General Public License as published by
3 //the Free Software Foundation, either version 3 of the License, or
4 //(at your option) any later version.
5 
6 //This program is distributed in the hope that it will be useful,
7 //but WITHOUT ANY WARRANTY; without even the implied warranty of
8 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 //GNU General Public License for more details.
10 
11 #include "networkirc.hpp"
12 
13 using namespace Huggle::IRC;
14 
15 NetworkIrc::NetworkIrc(QString server, QString nick)
16 {
17  this->messages_lock = new QMutex(QMutex::Recursive);
18  this->writer_lock = new QMutex(QMutex::Recursive);
19  this->__IsConnecting = false;
20  this->Ident = "huggle";
21  this->Nick = nick;
22  this->Port = 6667;
23  this->Server = server;
24  this->UserName = "Huggle client";
25  this->s = new QTcpSocket();
26  this->NetworkThread = NULL;
27  this->__Connected = false;
28 }
29 
30 NetworkIrc::~NetworkIrc()
31 {
32  delete this->messages_lock;
33  delete this->NetworkThread;
34  delete this->writer_lock;
35  delete this->s;
36 }
37 
39 {
40  this->s->connectToHost(this->Server, this->Port);
41  this->__IsConnecting = true;
42  if (!this->s->waitForConnected())
43  {
44  this->Exit();
45  this->__IsConnecting = false;
46  return;
47  }
48  this->NetworkThread = new NetworkIrc_th(this->s);
49  this->NetworkThread->root = this;
50  this->__Connected = true;
51  this->NetworkThread->start();
52  return;
53 }
54 
56 {
57  return this->__Connected;
58 }
59 
60 bool NetworkIrc::IsConnecting()
61 {
62  return this->__IsConnecting;
63 }
64 
65 void NetworkIrc::Disconnect()
66 {
67  if (!this->IsConnected())
68  {
69  return;
70  }
71  this->Data("QUIT :Huggle, the anti vandalism software. See #huggle on irc://chat.freenode.net");
72  this->Exit();
73  if (s != NULL)
74  {
75  this->s->disconnect();
76  delete this->s;
77  s = NULL;
78  }
79 }
80 
81 void NetworkIrc::Join(QString name)
82 {
83  this->Data("JOIN " + name);
84 }
85 
86 void NetworkIrc::Part(QString name)
87 {
88  this->Data("PART " + name);
89 }
90 
91 void NetworkIrc::Data(QString text)
92 {
93  if (!__Connected)
94  {
95  return;
96  }
97  this->writer_lock->lock();
98  this->s->write((text + QString("\n")).toUtf8());
99  this->writer_lock->unlock();
100 }
101 
102 void NetworkIrc::Send(QString name, QString text)
103 {
104  this->Data("PRIVMSG " + name + " :" + text);
105 }
106 
107 void NetworkIrc::Exit()
108 {
109  this->__Connected = false;
110 }
111 
112 Message* NetworkIrc::GetMessage()
113 {
114  Message *message;
115  this->messages_lock->lock();
116  if (this->Messages.count() == 0)
117  {
118  this->messages_lock->unlock();
119  return NULL;
120  } else
121  {
122  message = new Message(Messages.at(0));
123  this->Messages.removeAt(0);
124  }
125  this->messages_lock->unlock();
126  return message;
127 }
128 
129 
130 Message::Message(QString text, User us)
131 {
132  this->Text = text;
133  this->Channel = "";
134  this->user = us;
135 }
136 
137 Message::Message(const Message &ms)
138 {
139  this->Text = ms.Text;
140  this->Channel = ms.Channel;
141  this->user = ms.user;
142 }
143 
144 Message::Message(QString chan, QString text, User us)
145 {
146  this->Channel = chan;
147  this->Text = text;
148  this->user = us;
149 }
150 
151 Message::Message()
152 {
153  this->Channel = "";
154  this->Text = "";
155 }
156 
157 Message::Message(Message *ms)
158 {
159  this->Channel = ms->Channel;
160  this->Text = ms->Text;
161  this->user = ms->user;
162 }
163 
164 
165 User::User(QString nick, QString ident, QString host)
166 {
167  this->Nick = nick;
168  this->Ident = ident;
169  this->Host = host;
170 }
171 
172 User::User()
173 {
174  this->Ident = "";
175  this->Nick = "";
176  this->Host = "";
177 }
178 
179 User::User(User *user)
180 {
181  this->Host = user->Host;
182  this->Ident = user->Ident;
183  this->Nick = user->Nick;
184 }
185 
186 User::User(const User &user)
187 {
188  this->Host = user.Host;
189  this->Ident = user.Ident;
190  this->Nick = user.Nick;
191 }
192 
193 NetworkIrc_th::NetworkIrc_th(QTcpSocket *socket)
194 {
195  this->s = socket;
196  this->Stopped = false;
197  this->root = NULL;
198  Running = true;
199 }
200 
201 NetworkIrc_th::~NetworkIrc_th()
202 {
203 
204 }
205 
206 void NetworkIrc_th::Line(QString line)
207 {
208  QString Command = "";
209  QString Source = "";
210  if (!line.startsWith(":") || !line.contains(" "))
211  {
212  return;
213  }
214 
215  QString xx = line;
216  xx = xx.mid(1);
217  Source = xx.mid(0, xx.indexOf(" "));
218  xx= xx.mid(xx.indexOf(" ") + 1);
219  if (!xx.contains(" "))
220  {
221  Command = xx;
222  } else
223  {
224  Command = xx.mid(0, xx.indexOf(" "));
225  xx = xx.mid(xx.indexOf(" ") + 1);
226  }
227 
228  /// \todo implement PART
229  /// \todo implement KICK
230  /// \todo implement QUIT
231  /// \todo implement TOPIC
232  /// \todo implement CTCP
233  /// \todo implement NOTICES
234  if (Command == "PRIVMSG")
235  {
236  ProcessPrivmsg(Source, xx);
237  return;
238  }
239 }
240 
241 void NetworkIrc_th::ProcessPrivmsg(QString source, QString xx)
242 {
243  User user;
244  user.Nick = source.mid(0, source.indexOf("!"));
245  Message message;
246  if (!xx.contains("#") || !xx.contains(" :"))
247  {
248  return;
249  }
250  message.Channel = xx.mid(xx.indexOf("#"), xx.indexOf(" :"));
251  message.user = user;
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();
257 }
258 
259 bool NetworkIrc_th::IsFinished()
260 {
261  return false;
262 }
263 
264 void NetworkIrc_th::run()
265 {
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);
271  int ping = 0;
272  while (this->root->IsConnected() && this->s->isOpen())
273  {
274  ping++;
275  if (ping > 200)
276  {
277  this->root->Data("PING :" + this->root->Server);
278  ping = 0;
279  }
280  QString data(s->readLine());
281  if (data != "")
282  {
283  this->Line(data);
284  }
285  this->usleep(100000);
286  }
287  return;
288 }
The NetworkIrc_th class is a network thread for Network Irc.
Definition: networkirc.hpp:69
QTcpSocket * s
Pointer to a QT Socket that is handling the connection to irc server this object is managed by parent...
Definition: networkirc.hpp:87
Represent a message on irc network sent either to a channel or to a user.
Definition: networkirc.hpp:53
bool IsConnected()
IsConnected checks for connection.
Definition: networkirc.cpp:55
The User class represent a user of irc network.
Definition: networkirc.hpp:40
Represent a channel on IRC network.
Definition: networkirc.hpp:29
void Connect()
Connect to server.
Definition: networkirc.cpp:38
void Line(QString line)
Definition: networkirc.cpp:206
QTcpSocket * s
Pointer to a QT Socket that is handling the connection to irc server owned by this class...
Definition: networkirc.hpp:130