Browse Source

daemon begin

master
Robin Thoni 10 years ago
parent
commit
1622e07ea2

+ 2
- 0
.gitignore View File

@@ -12,3 +12,5 @@ netsoul/netsoul
12 12
 ns_finder/loginfinder
13 13
 usermgr/usermgr
14 14
 intrabocal/intrabocal
15
+daemon/daemon/netsould
16
+daemon/client/client

+ 1
- 1
Epimafia/intrabocal.cpp View File

@@ -72,7 +72,7 @@ void IntraBocal::searchUser(QString lastName, QString firstName, QString login,
72 72
     connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(searchSslError(QList<QSslError>)));
73 73
 }
74 74
 
75
-void IntraBocal::loginError(QNetworkReply::NetworkError e)
75
+void IntraBocal::loginError(QNetworkReply::NetworkError)
76 76
 {
77 77
     emit error(NetworkError);
78 78
 }

+ 1
- 1
Epimafia/intrabocal.h View File

@@ -39,7 +39,7 @@ public slots:
39 39
     void searchUser(QString lastName, QString firstName, QString login, QString promo = QString(), QString school = QString(), QString city = QString());
40 40
 
41 41
 private slots:
42
-    void loginError(QNetworkReply::NetworkError e);
42
+	void loginError(QNetworkReply::NetworkError);
43 43
     void loginFinished();
44 44
     void loginSslError(QList<QSslError>);
45 45
 

+ 105
- 9
Epimafia/netsoul.cpp View File

@@ -1,6 +1,6 @@
1 1
 #include "netsoul.h"
2 2
 
3
-int NetSoul::m_defaultTimeout = 1500;
3
+int NetSoul::m_defaultTimeout = 3000;
4 4
 
5 5
 NetSoul::NetSoul(QObject *parent) : QObject(parent)
6 6
 {
@@ -42,7 +42,12 @@ NetSoul::User NetSoul::getUser() const
42 42
 
43 43
 NetSoul::State NetSoul::getState() const
44 44
 {
45
-    return m_state;
45
+	return m_state;
46
+}
47
+
48
+QString NetSoul::getLocation() const
49
+{
50
+	return m_location;
46 51
 }
47 52
 
48 53
 NetSoul::UserState NetSoul::getUserStateFromString(QString state)
@@ -543,33 +548,124 @@ void NetSoul::commandFinished()
543 548
     nextCommand();
544 549
 }
545 550
 
546
-QDebug operator<< (QDebug d, NetSoul::Command& cmd)
551
+QDebug &operator<< (QDebug d, NetSoul::Command& cmd)
547 552
 {
548 553
     const QMetaObject & mo = NetSoul::staticMetaObject;
549
-    QMetaEnum me = mo.enumerator(mo.indexOfEnumerator("Command"));
550
-    d<<me.valueToKey(cmd);
551
-    return d;
554
+	QMetaEnum me = mo.enumerator(mo.indexOfEnumerator("Command"));
555
+	return d<<me.valueToKey(cmd);
552 556
 }
553 557
 
554
-QDebug operator<< (QDebug d, NetSoul::State& state)
558
+QDebug &operator<< (QDebug d, NetSoul::State& state)
555 559
 {
556 560
     return d << NetSoul::getStringFromState(state);
557 561
 }
558 562
 
559
-QDebug operator<< (QDebug d, NetSoul::UserState& state)
563
+QDebug &operator<< (QDebug d, NetSoul::UserState& state)
560 564
 {
561 565
     return d << NetSoul::getStringFromUserState(state);
562 566
 }
563 567
 
564
-QDebug operator<< (QDebug d, NetSoul::User& user)
568
+QDebug &operator<< (QDebug d, NetSoul::User& user)
565 569
 {
566 570
     d.nospace()<<"("<<user.id<<" "<<user.login.toStdString().c_str()<<"@"<<user.location.toStdString().c_str()
567 571
               <<" "<<user.ip.toStdString().c_str()<<" "<<user.state<< (user.state == NetSoul::UnknownState ? " " + user.stateString : "")<<" "<<user.promo<<" "<<user.data<<")";
568 572
     return d.maybeSpace();
569 573
 }
570 574
 
575
+QDebug &operator<< (QDebug d, NetSoul::Message& msg)
576
+{
577
+	return d<<"("<<msg.from<<msg.message<<")";
578
+}
571 579
 
572 580
 void NSListUsersEmitter::emitList(NetSoul::Users users)
573 581
 {
574 582
     emit listed(users);
575 583
 }
584
+
585
+QDataStream &operator<<(QDataStream& stream, NetSoul::Message& msg)
586
+{
587
+	return stream << msg.from << msg.message;
588
+}
589
+
590
+QDataStream &operator>>(QDataStream& stream, NetSoul::Message& msg)
591
+{
592
+	QString m;
593
+	NetSoul::User u;
594
+	stream >> u;
595
+	stream >> m;
596
+	if(stream.status() == QDataStream::Ok)
597
+	{
598
+		msg.from = u;
599
+		msg.message = m;
600
+	}
601
+	return stream;
602
+}
603
+
604
+QDataStream &operator<<(QDataStream& stream, NetSoul::UserState& state)
605
+{
606
+	return stream << (quint8)state;
607
+}
608
+
609
+QDataStream &operator>>(QDataStream& stream, NetSoul::UserState& state)
610
+{
611
+	quint8 s;
612
+	stream >> s;
613
+	if(stream.status() == QDataStream::Ok)
614
+		state = (NetSoul::UserState)s;
615
+	return stream;
616
+}
617
+
618
+QDataStream &operator<<(QDataStream& stream, NetSoul::State& state)
619
+{
620
+	return stream << (quint8)state;
621
+}
622
+
623
+QDataStream &operator>>(QDataStream& stream, NetSoul::State& state)
624
+{
625
+	quint8 s;
626
+	stream >> s;
627
+	if(stream.status() == QDataStream::Ok)
628
+		state = (NetSoul::State)s;
629
+	return stream;
630
+}
631
+
632
+QDataStream &operator<<(QDataStream& stream, NetSoul::User& user)
633
+{
634
+	return stream << user.data
635
+	<< user.id
636
+	<< user.ip
637
+	<< user.location
638
+	<< user.login
639
+	<< user.promo
640
+	<< user.state
641
+	<< user.stateString;
642
+}
643
+
644
+QDataStream &operator>>(QDataStream& stream, NetSoul::User& user)
645
+{
646
+	QString data, id, ip, location, login, promo, stateString;
647
+	NetSoul::UserState state;
648
+
649
+	stream >> data;
650
+	stream >> id;
651
+	stream >> ip;
652
+	stream >> location;
653
+	stream >> login;
654
+	stream >> promo;
655
+	stream >> state;
656
+	stream >> stateString;
657
+
658
+	if(stream.status() == QDataStream::Ok)
659
+	{
660
+		user.data = data;
661
+		user.id = id;
662
+		user.ip = ip;
663
+		user.location = location;
664
+		user.login = login;
665
+		user.promo = promo;
666
+		user.state = state;
667
+		user.stateString = stateString;
668
+	}
669
+
670
+	return stream;
671
+}

+ 20
- 5
Epimafia/netsoul.h View File

@@ -12,6 +12,7 @@
12 12
 #include <QDateTime>
13 13
 #include <QDebug>
14 14
 #include <QStringList>
15
+#include <QDataStream>
15 16
 #include "epimafia_global.h"
16 17
 #include "epimafia.h"
17 18
 
@@ -23,7 +24,7 @@ class EPIMAFIASHARED_EXPORT NetSoul : public QObject
23 24
 public:
24 25
     enum State
25 26
     {
26
-        Disconnected,
27
+		Disconnected,//TODO explicit values
27 28
         HostLookUp,
28 29
         Connecting,
29 30
         Connected,
@@ -86,6 +87,7 @@ public:
86 87
 	EpiUser getEpiUser() const;
87 88
 	User getUser() const;
88 89
 	NetSoul::State getState() const;
90
+	QString getLocation() const;
89 91
 
90 92
     static UserState getUserStateFromString(QString state);
91 93
     static QString getStringFromUserState(UserState state);
@@ -174,9 +176,22 @@ public slots:
174 176
     void emitList(NetSoul::Users users);
175 177
 };
176 178
 
177
-QDebug EPIMAFIASHARED_EXPORT operator<< (QDebug, NetSoul::Command&);
178
-QDebug EPIMAFIASHARED_EXPORT operator<< (QDebug, NetSoul::State&);
179
-QDebug EPIMAFIASHARED_EXPORT operator<< (QDebug, NetSoul::UserState&);
180
-QDebug EPIMAFIASHARED_EXPORT operator<< (QDebug, NetSoul::User&);
179
+QDebug EPIMAFIASHARED_EXPORT &operator<< (QDebug, NetSoul::Command&);
180
+QDebug EPIMAFIASHARED_EXPORT &operator<< (QDebug, NetSoul::State&);
181
+QDebug EPIMAFIASHARED_EXPORT &operator<< (QDebug, NetSoul::UserState&);
182
+QDebug EPIMAFIASHARED_EXPORT &operator<< (QDebug, NetSoul::User&);
183
+QDebug EPIMAFIASHARED_EXPORT &operator<< (QDebug, NetSoul::Message&);
184
+
185
+QDataStream EPIMAFIASHARED_EXPORT &operator<< (QDataStream&, NetSoul::Message&);
186
+QDataStream EPIMAFIASHARED_EXPORT &operator>> (QDataStream&, NetSoul::Message&);
187
+
188
+QDataStream EPIMAFIASHARED_EXPORT &operator<< (QDataStream&, NetSoul::UserState&);
189
+QDataStream EPIMAFIASHARED_EXPORT &operator>> (QDataStream&, NetSoul::UserState&);
190
+
191
+QDataStream EPIMAFIASHARED_EXPORT &operator<< (QDataStream&, NetSoul::State&);
192
+QDataStream EPIMAFIASHARED_EXPORT &operator>> (QDataStream&, NetSoul::State&);
193
+
194
+QDataStream EPIMAFIASHARED_EXPORT &operator<< (QDataStream&, NetSoul::User&);
195
+QDataStream EPIMAFIASHARED_EXPORT &operator>> (QDataStream&, NetSoul::User&);
181 196
 
182 197
 #endif // NETSOUL_H

+ 31
- 0
daemon/client/client.pro View File

@@ -0,0 +1,31 @@
1
+#-------------------------------------------------
2
+#
3
+# Project created by QtCreator 2014-02-15T22:13:56
4
+#
5
+#-------------------------------------------------
6
+
7
+QT       += core network
8
+
9
+QT       -= gui
10
+
11
+TARGET = client
12
+CONFIG   += console
13
+CONFIG   -= app_bundle
14
+
15
+TEMPLATE = app
16
+
17
+LIBS += -lptsocket
18
+
19
+SOURCES += main.cpp \
20
+    mainclass.cpp
21
+
22
+HEADERS += mainclass.h
23
+
24
+win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../Epimafia/release/ -lEpimafia
25
+else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../Epimafia/debug/ -lEpimafia
26
+else:unix: LIBS += -L$$OUT_PWD/../../Epimafia/ -lEpimafia
27
+
28
+INCLUDEPATH += $$PWD/../../Epimafia
29
+DEPENDPATH += $$PWD/../../Epimafia
30
+
31
+OTHER_FILES += ../defs.h

+ 45
- 0
daemon/client/main.cpp View File

@@ -0,0 +1,45 @@
1
+#include <QCoreApplication>
2
+#include <QStringList>
3
+#include "mainclass.h"
4
+
5
+void printUsage()
6
+{
7
+	qDebug()<<"Usage: netsouldc [options] (events|ns|users)";
8
+	qDebug()<<"\t-h, --help \t\t print this help.";
9
+	qDebug()<<"\t-k, --keep \t\t keep session active if queries are provided in arguments. Exit by default.";
10
+	qDebug()<<"\t-r, --responseOnly \t only print the responses, but not the info, ok and error.";
11
+	qDebug()<<"\t-p, --pid file \t\t indicate the file path to write de pid.";
12
+	qDebug()<<"\t-c, --color \t\t disable the colors in the info, ok and error.";
13
+}
14
+
15
+int main(int argc, char *argv[])
16
+{
17
+	QCoreApplication a(argc, argv);
18
+	MainClass m;
19
+	m.setCloseOnNextQuery(qApp->argc() >= 2);
20
+
21
+	QStringList cmds;
22
+
23
+	for(int i = 1; i < qApp->argc(); ++i)
24
+	{
25
+		QString c = qApp->arguments().at(i);
26
+		if(c == "-h" || c == "--help")
27
+		{
28
+			printUsage();
29
+			return 0;
30
+		}
31
+		else if(c == "-k" || c == "--keep")
32
+			m.setCloseOnNextQuery(false);
33
+		else if(c == "-r" || c == "--responseOnly")
34
+			m.setShowOnlyResponse(true);
35
+		else if(c == "-c" || c == "--color")
36
+			m.setColored(false);
37
+		else
38
+			cmds.append(c);
39
+	}
40
+
41
+	m.addCommands(cmds);
42
+	m.connectToDaemon();
43
+
44
+	return a.exec();
45
+}

+ 430
- 0
daemon/client/mainclass.cpp View File

@@ -0,0 +1,430 @@
1
+#include "mainclass.h"
2
+#include <QCoreApplication>
3
+#include <QStringList>
4
+#include <iostream>
5
+#include "netsoul.h"
6
+
7
+MainClass::MainClass(QObject *parent) : QObject(parent)
8
+{
9
+	m_sock = new PTSocket(this);
10
+	connect(m_sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(stateChanged(PTSocket::State)));
11
+	connect(m_sock, SIGNAL(packetReceived(int,QByteArray)), this, SLOT(packetReceived(int,QByteArray)));
12
+	m_closeOnNextQuery = false;
13
+	m_showOnlyResponse = false;
14
+	m_colored = true;
15
+	m_lastQueryResult = true;
16
+	m_responsesCount = 0;
17
+}
18
+
19
+void MainClass::send(QString cmd, bool print)
20
+{
21
+	QByteArray queryData;
22
+	QDataStream query(&queryData, QIODevice::ReadWrite);
23
+	PacketType type = Invalid;
24
+	QString error;
25
+
26
+	QStringList commands = cmd.split(" ", QString::SkipEmptyParts);
27
+	if(!commands.isEmpty())
28
+	{
29
+		QString c = commands.at(0);
30
+		commands.removeFirst();
31
+		if(c == "users")
32
+		{
33
+			if(!commands.isEmpty())
34
+			{
35
+				c = commands.at(0);
36
+				commands.removeFirst();
37
+				if(c == "list")
38
+					type = UsersList;
39
+				else if(c == "add")
40
+				{
41
+					if(commands.size() >= 2)
42
+					{
43
+						type = UsersAdd;
44
+						query << commands.at(0) << commands.at(1);
45
+					}
46
+					else
47
+						error = "Usage: users add username password";
48
+				}
49
+				else if(c == "remove")
50
+				{
51
+					if(commands.size() >= 1)
52
+					{
53
+						type = UsersRemove;
54
+						query << commands.at(0);
55
+					}
56
+					else
57
+						error = "Usage: users remove username";
58
+				}
59
+				else
60
+					error = "Usage: users (add|list|remove)";
61
+			}
62
+			else
63
+				error = "Usage: users (add|list|remove)";
64
+		}
65
+		else if(c == "ns")
66
+		{
67
+			if(!commands.isEmpty())
68
+			{
69
+				c = commands.at(0);
70
+				commands.removeFirst();
71
+				if(c == "connect")
72
+				{
73
+					if(commands.size() >= 1)
74
+					{
75
+						type = NSConnect;
76
+						query << commands.at(0);
77
+					}
78
+					else
79
+						error = "Usage: ns connect username";
80
+				}
81
+				else if(c == "disconnect")
82
+					type = NSDisconnect;
83
+				else if(c == "status")
84
+					type = NSStatus;
85
+				else if(c == "location")
86
+				{
87
+					if(!commands.isEmpty())
88
+					{
89
+						c = commands.at(0);
90
+						commands.removeFirst();
91
+						if(c == "get")
92
+							type = NSLocationGet;
93
+						else if(c == "set")
94
+						{
95
+							c = cmd;
96
+							c = c.remove(0, c.indexOf("set") + 3);
97
+							if(!c.isEmpty())
98
+								c = c.remove(0, 1);
99
+							type = NSLocationSet;
100
+							query << c;
101
+						}
102
+						else
103
+							error = "Usage: ns location (get|set)";
104
+					}
105
+					else
106
+						error = "Usage: ns location (get|set)";
107
+				}
108
+				else
109
+					error = "Usage: ns (connect|disconnect|location|status)";
110
+			}
111
+			else
112
+				error = "Usage: ns (connect|disconnect|location|status)";
113
+		}
114
+		else if(c == "events")
115
+		{
116
+			if(commands.size() >= 2)
117
+			{
118
+				c = commands.at(0);
119
+				QString t = commands.at(1);
120
+				if(c == "ns")
121
+				{
122
+					if(t == "add")
123
+						type = EventsNSAdd;
124
+					else if(t == "remove")
125
+						type = EventsNSRemove;
126
+					else
127
+						error = "Usage: events ns (add|remove)";
128
+				}
129
+				else if(c == "chat")
130
+				{
131
+					if(t == "add")
132
+						type = EventsChatAdd;
133
+					else if(t == "remove")
134
+						type = EventsChatRemove;
135
+					else
136
+						error = "Usage: events chat (add|remove)";
137
+				}
138
+				else if(c == "watchlist")
139
+				{
140
+					if(t == "add")
141
+						type = EventsWatchlistAdd;
142
+					else if(t == "remove")
143
+						type = EventsWatchlistRemove;
144
+					else
145
+						error = "Usage: events watchlist (add|remove)";
146
+				}
147
+				else if(c == "all")
148
+				{
149
+					if(t == "add")
150
+						type = EventsAllAdd;
151
+					else if(t == "remove")
152
+						type = EventsAllRemove;
153
+					else
154
+						error = "Usage: events all (add|remove)";
155
+				}
156
+				else
157
+					error = "Usage: events (all|chat|ns|watchlist) (add|remove)";
158
+			}
159
+			else
160
+				error = "Usage: events (all|chat|ns|watchlist) (add|remove)";
161
+		}
162
+		else if(c == "watchlist")
163
+		{
164
+			if(!commands.isEmpty())
165
+			{
166
+				c = commands.at(0);
167
+				commands.removeFirst();
168
+				if(c == "add")
169
+				{
170
+					if(!commands.isEmpty())
171
+					{
172
+						type = WatchlistAdd;
173
+						query << commands;
174
+					}
175
+					else
176
+						error = "Usage: watchlist add login1 login2 ...";
177
+				}
178
+				else if(c == "whois")
179
+				{
180
+					if(!commands.isEmpty())
181
+					{
182
+						type = WatchlistWhois;
183
+						query << commands;
184
+					}
185
+					else
186
+						error = "Usage: watchlist whois login1 login2 ...";
187
+				}
188
+				else
189
+					error = "Usage: watchlist (add|whois)";
190
+			}
191
+			else
192
+				error = "Usage: watchlist (add|whois)";
193
+		}
194
+		else
195
+			error = "Usage: (events|ns|users)";
196
+	}
197
+	else
198
+		error = "Empty query";
199
+
200
+	if(error.isEmpty())
201
+	{
202
+		if(print && !m_showOnlyResponse)
203
+			qinfo<<"Sending command:"<<cmd.toStdString().c_str();
204
+		m_sock->send(type, queryData);
205
+	}
206
+	else
207
+	{
208
+		m_lastQueryResult = false;
209
+		if(!m_showOnlyResponse)
210
+			qerror<<error.toStdString().c_str();
211
+		mayDisconnect();
212
+	}
213
+
214
+	if(!print)
215
+		readStdin();
216
+}
217
+
218
+void MainClass::setCloseOnNextQuery(bool m)
219
+{
220
+	m_closeOnNextQuery = m;
221
+}
222
+
223
+void MainClass::setShowOnlyResponse(bool m)
224
+{
225
+	m_showOnlyResponse = m;
226
+}
227
+
228
+void MainClass::setColored(bool m)
229
+{
230
+	m_colored = m;
231
+}
232
+
233
+void MainClass::connectToDaemon()
234
+{
235
+	if(!m_sock->isOpen())
236
+		m_sock->connectToHost(QHostAddress::LocalHost, 12642);
237
+}
238
+
239
+void MainClass::addCommands(QStringList cmds)
240
+{
241
+	m_cmds << cmds;
242
+}
243
+
244
+void MainClass::stateChanged(PTSocket::State s)
245
+{
246
+	if(s == PTSocket::Connecting)
247
+	{
248
+		if(!m_showOnlyResponse)
249
+			qinfo<<"Connecting to daemon...";
250
+	}
251
+	else if(s == PTSocket::Handshaked)
252
+	{
253
+		if(!m_showOnlyResponse)
254
+			qok<<"Connected to daemon";
255
+		if(!m_cmds.isEmpty())
256
+			foreach(QString cmd, m_cmds)
257
+				send(cmd.toStdString().c_str(), true);
258
+		else
259
+			readStdin();
260
+	}
261
+	else if(s == PTSocket::Error)
262
+	{
263
+		if(!m_showOnlyResponse)
264
+			qerror<<m_sock->getError().toStdString().c_str();
265
+		qApp->exit(1);
266
+	}
267
+	else if(s == PTSocket::Disconnected)
268
+	{
269
+		if(!m_showOnlyResponse)
270
+			qinfo<<"Disconnected from daemon";
271
+		qApp->exit(m_lastQueryResult ? 0 : 1);
272
+	}
273
+}
274
+
275
+void MainClass::packetReceived(int type, QByteArray replyData)
276
+{
277
+	++m_responsesCount;
278
+	QDataStream reply(&replyData, QIODevice::ReadOnly);
279
+	reply >> m_lastQueryResult;
280
+	if(m_lastQueryResult)
281
+	{
282
+		if(type == UsersList)
283
+		{
284
+			QStringList users;
285
+			reply >> users;
286
+			if(!m_showOnlyResponse)
287
+				qinfo<<"Available users:";
288
+			foreach(QString user, users)
289
+				qDebug()<<user.toStdString().c_str();
290
+		}
291
+		else if(type == UsersAdd)
292
+		{
293
+			if(!m_showOnlyResponse)
294
+				qok<<"User successfully added or updated";
295
+		}
296
+		else if(type == UsersRemove)
297
+		{
298
+			if(!m_showOnlyResponse)
299
+				qok<<"User or user\'s socks password successfully removed";
300
+		}
301
+
302
+		else if(type == NSConnect)
303
+		{
304
+			if(!m_showOnlyResponse)
305
+				qinfo<<"Connecting to netsoul server";
306
+		}
307
+		else if(type == NSDisconnect)
308
+		{
309
+			if(!m_showOnlyResponse)
310
+				qinfo<<"Disconnecting from netsoul server";
311
+		}
312
+		else if(type == NSStatus)
313
+		{
314
+			NetSoul::State s;
315
+			reply >> s;
316
+			if(!m_showOnlyResponse)
317
+				qinfo<<"Status:"<<NetSoul::getStringFromState(s).toStdString().c_str();
318
+			else
319
+				qDebug()<<NetSoul::getStringFromState(s).toStdString().c_str();
320
+		}
321
+		else if(type == NSLocationGet)
322
+		{
323
+			QString l;
324
+			reply >> l;
325
+			if(!m_showOnlyResponse)
326
+				qinfo<<"Location:"<<l;
327
+			else
328
+				qDebug()<<l;
329
+		}
330
+		else if(type == NSLocationSet)
331
+		{
332
+			if(!m_showOnlyResponse)
333
+				qinfo<<"Location successfully changed";
334
+		}
335
+
336
+		else if(type == EventsNSAdd)
337
+		{
338
+			if(!m_showOnlyResponse)
339
+				qok<<"Netsoul event registered";
340
+		}
341
+		else if(type == EventsNSRemove)
342
+		{
343
+			if(!m_showOnlyResponse)
344
+				qok<<"Netsoul event removed";
345
+		}
346
+
347
+		else if(type == EventsWatchlistAdd)
348
+		{
349
+			if(!m_showOnlyResponse)
350
+				qok<<"Watchlist event registered";
351
+		}
352
+		else if(type == EventsWatchlistRemove)
353
+		{
354
+			if(!m_showOnlyResponse)
355
+				qok<<"Watchlist event removed";
356
+		}
357
+
358
+		else if(type == EventsChat)
359
+		{
360
+			NetSoul::Message msg;
361
+			reply >> msg;
362
+			if(!m_showOnlyResponse)
363
+				qinfo<<"Message received:"<<msg;
364
+			else
365
+				qDebug()<<msg;
366
+		}
367
+		else if(type == EventsChatAdd)
368
+		{
369
+			if(!m_showOnlyResponse)
370
+				qok<<"Chat event registered";
371
+		}
372
+		else if(type == EventsChatRemove)
373
+		{
374
+			if(!m_showOnlyResponse)
375
+				qok<<"Chat event removed";
376
+		}
377
+		else if(type == EventsAllAdd)
378
+		{
379
+			if(!m_showOnlyResponse)
380
+				qok<<"All events registered";
381
+		}
382
+		else if(type == EventsAllRemove)
383
+		{
384
+			if(!m_showOnlyResponse)
385
+				qok<<"All events removed";
386
+		}
387
+
388
+		else if(type == WatchlistAdd)
389
+		{
390
+			if(!m_showOnlyResponse)
391
+				qok<<"Users added to watchlist";
392
+		}
393
+		else if(type == WatchlistWhois)
394
+		{
395
+			NetSoul::User usr;
396
+			reply >> usr;
397
+			if(reply.status() == QDataStream::Ok)
398
+			{
399
+				if(!m_showOnlyResponse)
400
+					qinfo<<"Watchlist event:"<<usr;
401
+				else
402
+					qDebug()<<usr;
403
+			}
404
+			else
405
+				qerror<<"Failed to retreive user in whois reply";
406
+		}
407
+		else
408
+			qerror<<"Unknown reply ("<<type<<")";
409
+	}
410
+	else
411
+	{
412
+		QString error;
413
+		reply >> error;
414
+		if(!m_showOnlyResponse)
415
+			qerror<<error.toStdString().c_str();
416
+	}
417
+	mayDisconnect();
418
+}
419
+
420
+void MainClass::readStdin()
421
+{
422
+	/*QTextStream t(stdin);
423
+	send(t.readLine(), false);*/
424
+}
425
+
426
+void MainClass::mayDisconnect()
427
+{
428
+	if(m_closeOnNextQuery && m_responsesCount >= m_cmds.size())
429
+		m_sock->disconnectFromServer();
430
+}

+ 43
- 0
daemon/client/mainclass.h View File

@@ -0,0 +1,43 @@
1
+#ifndef MAINCLASS_H
2
+#define MAINCLASS_H
3
+
4
+#include <QObject>
5
+#include <QStringList>
6
+#include <ptsocket/ptsocket.h>
7
+#include "../defs.h"
8
+
9
+#define qinfo qDebug()<<(m_colored ?	"[\033[1m\033[36mInfo\033[m ]" :		"[Info ]")
10
+#define qerror qDebug()<<(m_colored ?	"[\033[1m\033[31mError\033[m]" :	"[Error]")
11
+#define qok qDebug()<<(m_colored ?		"[ \033[1m\033[32mOK\033[m  ]" :		"[ OK  ]")
12
+
13
+class MainClass : public QObject
14
+{
15
+	Q_OBJECT
16
+public:
17
+	explicit MainClass(QObject *parent = 0);
18
+
19
+public slots:
20
+	void send(QString cmd, bool print);
21
+	void setCloseOnNextQuery(bool m);
22
+	void setShowOnlyResponse(bool m);
23
+	void setColored(bool m);
24
+	void addCommands(QStringList cmds);
25
+	void connectToDaemon();
26
+
27
+private slots:
28
+	void stateChanged(PTSocket::State s);
29
+	void packetReceived(int type, QByteArray data);
30
+	void readStdin();
31
+	void mayDisconnect();
32
+
33
+private:
34
+	PTSocket* m_sock;
35
+	bool m_closeOnNextQuery;
36
+	QStringList m_cmds;
37
+	bool m_showOnlyResponse;
38
+	bool m_colored;
39
+	bool m_lastQueryResult;
40
+	int m_responsesCount;
41
+};
42
+
43
+#endif // MAINCLASS_H

+ 5
- 0
daemon/daemon.pro View File

@@ -0,0 +1,5 @@
1
+TEMPLATE = subdirs
2
+CONFIG += ordered
3
+SUBDIRS += \
4
+    daemon \
5
+    client

+ 31
- 0
daemon/daemon/daemon.pro View File

@@ -0,0 +1,31 @@
1
+#-------------------------------------------------
2
+#
3
+# Project created by QtCreator 2014-02-15T19:24:40
4
+#
5
+#-------------------------------------------------
6
+
7
+QT       += core network
8
+
9
+QT       -= gui
10
+
11
+TARGET = netsould
12
+CONFIG   += console
13
+CONFIG   -= app_bundle
14
+
15
+TEMPLATE = app
16
+
17
+LIBS += -lptsocket
18
+
19
+SOURCES += main.cpp \
20
+    mainclass.cpp
21
+
22
+HEADERS += mainclass.h
23
+
24
+win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../Epimafia/release/ -lEpimafia
25
+else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../Epimafia/debug/ -lEpimafia
26
+else:unix: LIBS += -L$$OUT_PWD/../../Epimafia/ -lEpimafia
27
+
28
+INCLUDEPATH += $$PWD/../../Epimafia
29
+DEPENDPATH += $$PWD/../../Epimafia
30
+
31
+OTHER_FILES += ../defs.h

+ 48
- 0
daemon/daemon/main.cpp View File

@@ -0,0 +1,48 @@
1
+#include <QCoreApplication>
2
+#include <QDebug>
3
+#include <unistd.h>
4
+#include <QSettings>
5
+#include "mainclass.h"
6
+
7
+void printUsage()
8
+{
9
+	qDebug()<<"Usage: netsould";
10
+	qDebug()<<"\t-h, --help \t\t print this help.";
11
+	qDebug()<<"\t-p, --pid file \t\t indicate the file path to write de pid. The pid will be written when the daemon is connected to the netsoul server, and a blank file when disconnected.";
12
+}
13
+
14
+int main(int argc, char *argv[])
15
+{
16
+	QCoreApplication a(argc, argv);
17
+	MainClass m;
18
+
19
+	for(int i = 1; i < qApp->argc(); ++i)
20
+	{
21
+		QString c = qApp->arguments().at(i);
22
+		if(c == "-p" || c == "--pid")
23
+		{
24
+			if(qApp->argc() > i + 1)
25
+				m.setPidFile(qApp->arguments().at(++i));
26
+			else
27
+			{
28
+				printUsage();
29
+				return 1;
30
+			}
31
+		}
32
+		else
33
+		{
34
+			printUsage();
35
+			return 1;
36
+		}
37
+	}
38
+
39
+	while(!m.start())
40
+	{
41
+		qDebug()<<"[Error] Failed to start. Trying again in 5 seconds...";
42
+		sleep(5);
43
+	}
44
+	qDebug()<<"[Info ] Daemon started";
45
+
46
+
47
+	return a.exec();
48
+}

+ 276
- 0
daemon/daemon/mainclass.cpp View File

@@ -0,0 +1,276 @@
1
+#include "mainclass.h"
2
+#include <QFile>
3
+#include <unistd.h>
4
+#include <limits.h>
5
+
6
+MainClass::MainClass(QObject *parent) : QObject(parent)
7
+{
8
+	m_serv = new PTServer(this);
9
+	connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(newClient(PTSocket*)));
10
+
11
+	m_ns = new NetSoul(this);
12
+	connect(m_ns, SIGNAL(stateChanged(NetSoul::State)), this, SLOT(netsoulStateChanged(NetSoul::State)));
13
+	connect(m_ns, SIGNAL(newMessage(NetSoul::Message)), this, SLOT(netsoulNewMessage(NetSoul::Message)));
14
+	connect(m_ns, SIGNAL(userDataChanged(NetSoul::User)), this, SLOT(netsoulUserDataChanged(NetSoul::User)));
15
+
16
+	m_eventsList.append(&m_nsEvents);
17
+	m_eventsList.append(&m_chatEvents);
18
+	m_eventsList.append(&m_watchlistEvents);
19
+}
20
+
21
+bool MainClass::start()
22
+{
23
+	return m_serv->listen(QHostAddress::LocalHost, 12642);
24
+}
25
+
26
+void MainClass::execute(PacketType type, QByteArray queryData, PTSocket* sock)
27
+{
28
+	QByteArray respData;
29
+	QDataStream resp(&respData, QIODevice::ReadWrite);
30
+	QDataStream query(&queryData, QIODevice::ReadOnly);
31
+	QString error;
32
+	resp << true;
33
+	if(type == UsersList)
34
+	{
35
+		QStringList users;
36
+		foreach(EpiUser user, Epimafia::loadUsers())
37
+			users << user.getLogin();
38
+		resp << users;
39
+	}
40
+	else if(type == UsersAdd)
41
+	{
42
+		QString user, password;
43
+		query >> user >> password;
44
+		if(!user.isEmpty() && !password.isEmpty())
45
+		{
46
+			EpiUser usr;
47
+			foreach(EpiUser u, Epimafia::loadUsers())
48
+				if(u.getLogin() == user)
49
+				{
50
+					usr = u;
51
+					break;
52
+				}
53
+			usr.setLogin(user);
54
+			usr.setPwd_socks(password);
55
+			Epimafia::saveUser(usr);
56
+		}
57
+		else
58
+			error = "Empty username or password";
59
+	}
60
+	else if(type == UsersRemove)
61
+	{
62
+		QString user;
63
+		query >> user;
64
+		if(!user.isEmpty())
65
+		{
66
+			EpiUsers users = Epimafia::loadUsers();
67
+			EpiUser usr;
68
+			foreach(EpiUser u, users)
69
+				if(u.getLogin() == user)
70
+				{
71
+					usr = u;
72
+					break;
73
+				}
74
+			if(usr.hasLogin())
75
+			{
76
+				if(usr.hasPassword() || usr.hasPwd_num() || usr.hasPwd_ppp())
77
+				{
78
+					usr.setPwd_socks(QString());
79
+					Epimafia::saveUser(usr);
80
+				}
81
+				else
82
+					Epimafia::removeUser(usr);
83
+			}
84
+		}
85
+		else
86
+			error = "Empty username";
87
+	}
88
+	else if(type == NSConnect)
89
+	{
90
+		if(!isConnected())
91
+		{
92
+			QString user;
93
+			query >> user;
94
+			if(!user.isEmpty())
95
+			{
96
+				EpiUsers users = Epimafia::loadUsers();
97
+				EpiUser usr;
98
+				foreach(EpiUser u, users)
99
+					if(u.getLogin() == user)
100
+					{
101
+						usr = u;
102
+						break;
103
+					}
104
+				if(usr.hasLogin())
105
+				{
106
+					if(usr.hasPwd_socks())
107
+					{
108
+						m_ns->setLogin(usr);
109
+						m_ns->login();
110
+					}
111
+					else
112
+						error = "User has no socks password";
113
+				}
114
+				else
115
+					error = "User not found";
116
+			}
117
+			else
118
+				error = "Empty username";
119
+		}
120
+		else
121
+			error = "Already in use (" + NetSoul::getStringFromState(m_ns->getState()) + ")";
122
+	}
123
+	else if(type == NSDisconnect)
124
+	{
125
+		if(isConnected())
126
+			m_ns->logout();
127
+		else
128
+			error = "Not connected (" + NetSoul::getStringFromState(m_ns->getState()) + ")";
129
+	}
130
+	else if(type == NSStatus)
131
+		resp << m_ns->getState();
132
+	else if(type == NSLocationGet)
133
+		resp << m_ns->getLocation();
134
+	else if(type == NSLocationSet)
135
+	{
136
+		QString l;
137
+		query >> l;
138
+		m_ns->setLocation(l);
139
+	}
140
+	else if(type == EventsNSAdd)
141
+	{
142
+		if(!m_nsEvents.contains(sock))
143
+			m_nsEvents.append(sock);
144
+	}
145
+	else if(type == EventsNSRemove)
146
+		m_nsEvents.removeOne(sock);
147
+	else if(type == EventsChatAdd)
148
+	{
149
+		if(!m_chatEvents.contains(sock))
150
+			m_chatEvents.append(sock);
151
+	}
152
+	else if(type == EventsChatRemove)
153
+		m_chatEvents.removeOne(sock);
154
+	else if(type == EventsWatchlistAdd)
155
+	{
156
+		if(!m_watchlistEvents.contains(sock))
157
+			m_watchlistEvents.append(sock);
158
+	}
159
+	else if(type == EventsWatchlistRemove)
160
+		m_watchlistEvents.removeOne(sock);
161
+	else if(type == EventsAllAdd)
162
+	{
163
+		foreach(QList<PTSocket*>* e, m_eventsList)
164
+			if(!e->contains(sock))
165
+				e->append(sock);
166
+	}
167
+	else if(type == EventsAllRemove)
168
+	{
169
+		foreach(QList<PTSocket*>* e, m_eventsList)
170
+			e->removeOne(sock);
171
+	}
172
+	else if(type == WatchlistAdd)
173
+	{
174
+		QStringList logins;
175
+		query >> logins;
176
+		m_ns->addToWatchList(logins);
177
+	}
178
+	else if(type == WatchlistWhois)
179
+	{
180
+		QStringList logins;
181
+		query >> logins;
182
+		m_ns->whois(logins);
183
+		return;
184
+	}
185
+	else
186
+		error = "Unknown query type " + QString::number((int)type);
187
+	resp.device()->seek(0);
188
+	resp << error.isEmpty();
189
+	if(!error.isEmpty())
190
+		resp << error;
191
+
192
+	if(sock)
193
+		sock->send(type, respData);
194
+}
195
+
196
+void MainClass::setPidFile(QString f)
197
+{
198
+	m_pidFile = f;
199
+}
200
+
201
+void MainClass::newClient(PTSocket* client)
202
+{
203
+	connect(client, SIGNAL(packetReceived(int,QByteArray)), this, SLOT(packetReceived(int,QByteArray)));
204
+	connect(client, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(clientStateChanged(PTSocket::State)));
205
+	qDebug()<<"[Info ] New client ("<<m_serv->getClients().size()<<")";
206
+}
207
+
208
+void MainClass::packetReceived(int type, QByteArray data)
209
+{
210
+	PTSocket* sock = (PTSocket*)sender();
211
+	if(!sock)
212
+		return;
213
+
214
+	execute((PacketType)type, data, sock);
215
+}
216
+
217
+void MainClass::clientStateChanged(PTSocket::State s)
218
+{
219
+	PTSocket* sock = (PTSocket*)sender();
220
+	if(!sock)
221
+		return;
222
+	if(s == PTSocket::Disconnected)
223
+		qDebug()<<"[Info ] Client left ("<<m_serv->getClients().size()<<")";
224
+	else if(s == PTSocket::Error)
225
+		qDebug()<<"[Error] Client error:"<<sock->getError().toStdString().c_str()<<"("<<m_serv->getClients().size()<<")";
226
+	if(s == PTSocket::Disconnected || s == PTSocket::Error)
227
+		foreach(QList<PTSocket*>* e, m_eventsList)
228
+			e->removeOne(sock);
229
+}
230
+
231
+void MainClass::netsoulStateChanged(NetSoul::State s)
232
+{
233
+	if(!m_pidFile.isEmpty())
234
+	{
235
+		QFile f(m_pidFile);
236
+		if(f.open(QIODevice::Truncate | QIODevice::WriteOnly))
237
+		{
238
+			int pid = s == NetSoul::LoggedIn ? getpid() : INT_MAX;
239
+			f.write(QString::number(pid).toStdString().c_str());
240
+			f.close();
241
+		}
242
+		else
243
+			qDebug()<<"[Error] Could not write the pid file";
244
+	}
245
+	QByteArray respData;
246
+	QDataStream resp(&respData, QIODevice::WriteOnly);
247
+	resp << true;
248
+	resp << s;
249
+	foreach(PTSocket* sock, m_nsEvents)
250
+		sock->send(NSStatus, respData);
251
+}
252
+
253
+void MainClass::netsoulNewMessage(NetSoul::Message msg)
254
+{
255
+	QByteArray respData;
256
+	QDataStream resp(&respData, QIODevice::WriteOnly);
257
+	resp << true;
258
+	resp << msg;
259
+	foreach(PTSocket* sock, m_chatEvents)
260
+		sock->send(EventsChat, respData);
261
+}
262
+
263
+void MainClass::netsoulUserDataChanged(NetSoul::User usr)
264
+{
265
+	QByteArray respData;
266
+	QDataStream resp(&respData, QIODevice::WriteOnly);
267
+	resp << true;
268
+	resp << usr;
269
+	foreach(PTSocket* sock, m_watchlistEvents)
270
+		sock->send(WatchlistWhois, respData);
271
+}
272
+
273
+bool MainClass::isConnected()
274
+{
275
+	return !(m_ns->getState() == NetSoul::Disconnected || m_ns->getState() == NetSoul::ProtocolError || m_ns->getState() == NetSoul::NetworkError || m_ns->getState() == NetSoul::BadLogin);
276
+}

+ 40
- 0
daemon/daemon/mainclass.h View File

@@ -0,0 +1,40 @@
1
+#ifndef MAINCLASS_H
2
+#define MAINCLASS_H
3
+
4
+#include <QObject>
5
+#include <ptsocket/ptserver.h>
6
+#include "netsoul.h"
7
+#include "epimafia.h"
8
+#include "../defs.h"
9
+
10
+class MainClass : public QObject
11
+{
12
+	Q_OBJECT
13
+public:
14
+	explicit MainClass(QObject *parent = 0);
15
+
16
+public slots:
17
+	void setPidFile(QString f);
18
+	bool start();
19
+	void execute(PacketType type, QByteArray queryData, PTSocket* sock);
20
+
21
+private slots:
22
+	void newClient(PTSocket* client);
23
+	void packetReceived(int type, QByteArray data);
24
+	void clientStateChanged(PTSocket::State s);
25
+	void netsoulStateChanged(NetSoul::State s);
26
+	void netsoulNewMessage(NetSoul::Message msg);
27
+	void netsoulUserDataChanged(NetSoul::User usr);
28
+
29
+private:
30
+	bool isConnected();
31
+	PTServer* m_serv;
32
+	NetSoul* m_ns;
33
+	QList<QList<PTSocket*>*> m_eventsList;
34
+	QList<PTSocket*> m_nsEvents;
35
+	QList<PTSocket*> m_chatEvents;
36
+	QList<PTSocket*> m_watchlistEvents;
37
+	QString m_pidFile;
38
+};
39
+
40
+#endif // MAINCLASS_H

+ 35
- 0
daemon/defs.h View File

@@ -0,0 +1,35 @@
1
+#ifndef DEFS_H
2
+#define DEFS_H
3
+
4
+enum PacketType
5
+{
6
+	Invalid = 0,
7
+
8
+	UsersList = 1,
9
+	UsersAdd = 2,
10
+	UsersRemove = 3,
11
+
12
+	NSConnect = 10,
13
+	NSDisconnect = 11,
14
+	NSStatus = 12,
15
+	NSLocationGet = 13,
16
+	NSLocationSet = 14,
17
+
18
+	EventsNSAdd = 20,
19
+	EventsNSRemove = 21,
20
+
21
+	EventsChat = 30,
22
+	EventsChatAdd = 31,
23
+	EventsChatRemove = 32,
24
+
25
+	EventsWatchlistAdd = 40,
26
+	EventsWatchlistRemove = 41,
27
+
28
+	WatchlistAdd = 50,
29
+	WatchlistWhois = 51,
30
+
31
+	EventsAllAdd = 60,
32
+	EventsAllRemove = 61,
33
+};
34
+
35
+#endif

+ 2
- 1
epimafia.pro View File

@@ -7,4 +7,5 @@ SUBDIRS += \
7 7
     ns_finder \
8 8
 	intrabocal \
9 9
 	usermgr \
10
-	netsoul
10
+	netsoul \
11
+    daemon

+ 1
- 1
netsoul/dialogaddcontact.cpp View File

@@ -28,7 +28,7 @@ void DialogAddContact::on_lineSearch_returnPressed()
28 28
     emit contactAdded(ui->lineSearch->text());
29 29
 }
30 30
 
31
-void DialogAddContact::on_listContacts_itemDoubleClicked(QTreeWidgetItem *item, int column)
31
+void DialogAddContact::on_listContacts_itemDoubleClicked(QTreeWidgetItem *item, int)
32 32
 {
33 33
     emit contactAdded(item->text(0));
34 34
 }

+ 1
- 1
netsoul/dialogaddcontact.h View File

@@ -27,7 +27,7 @@ signals:
27 27
 
28 28
 private slots:
29 29
     void on_lineSearch_returnPressed();
30
-    void on_listContacts_itemDoubleClicked(QTreeWidgetItem *item, int column);
30
+	void on_listContacts_itemDoubleClicked(QTreeWidgetItem *item, int);
31 31
     void chat();
32 32
     void addContact();
33 33
     void menuRequested(QPoint p);

Loading…
Cancel
Save