123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #include "mainclass.h"
- #include <QCoreApplication>
- #include <QStringList>
- #include <iostream>
- #include "netsoul.h"
-
- MainClass::MainClass(QObject *parent) : QObject(parent)
- {
- m_sock = new PTSocket(this);
- connect(m_sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(stateChanged(PTSocket::State)));
- connect(m_sock, SIGNAL(packetReceived(int,QByteArray)), this, SLOT(packetReceived(int,QByteArray)));
- m_closeOnNextQuery = false;
- m_showOnlyResponse = false;
- m_colored = true;
- m_lastQueryResult = true;
- m_responsesCount = 0;
- }
-
- void MainClass::send(QString cmd, bool print)
- {
- QByteArray queryData;
- QDataStream query(&queryData, QIODevice::ReadWrite);
- PacketType type = Invalid;
- QString error;
-
- QStringList commands = cmd.split(" ", QString::SkipEmptyParts);
- if(!commands.isEmpty())
- {
- QString c = commands.at(0);
- commands.removeFirst();
- if(c == "users")
- {
- if(!commands.isEmpty())
- {
- c = commands.at(0);
- commands.removeFirst();
- if(c == "list")
- type = UsersList;
- else if(c == "add")
- {
- if(commands.size() >= 2)
- {
- type = UsersAdd;
- query << commands.at(0) << commands.at(1);
- }
- else
- error = "Usage: users add username password";
- }
- else if(c == "remove")
- {
- if(commands.size() >= 1)
- {
- type = UsersRemove;
- query << commands.at(0);
- }
- else
- error = "Usage: users remove username";
- }
- else
- error = "Usage: users (add|list|remove)";
- }
- else
- error = "Usage: users (add|list|remove)";
- }
- else if(c == "ns")
- {
- if(!commands.isEmpty())
- {
- c = commands.at(0);
- commands.removeFirst();
- if(c == "connect")
- {
- if(commands.size() >= 1)
- {
- type = NSConnect;
- query << commands.at(0);
- }
- else
- error = "Usage: ns connect username";
- }
- else if(c == "disconnect")
- type = NSDisconnect;
- else if(c == "status")
- type = NSStatus;
- else if(c == "location")
- {
- if(!commands.isEmpty())
- {
- c = commands.at(0);
- commands.removeFirst();
- if(c == "get")
- type = NSLocationGet;
- else if(c == "set")
- {
- c = cmd;
- c = c.remove(0, c.indexOf("set") + 3);
- if(!c.isEmpty())
- c = c.remove(0, 1);
- type = NSLocationSet;
- query << c;
- }
- else
- error = "Usage: ns location (get|set)";
- }
- else
- error = "Usage: ns location (get|set)";
- }
- else
- error = "Usage: ns (connect|disconnect|location|status)";
- }
- else
- error = "Usage: ns (connect|disconnect|location|status)";
- }
- else if(c == "events")
- {
- if(commands.size() >= 2)
- {
- c = commands.at(0);
- QString t = commands.at(1);
- if(c == "ns")
- {
- if(t == "add")
- type = EventsNSAdd;
- else if(t == "remove")
- type = EventsNSRemove;
- else
- error = "Usage: events ns (add|remove)";
- }
- else if(c == "chat")
- {
- if(t == "add")
- type = EventsChatAdd;
- else if(t == "remove")
- type = EventsChatRemove;
- else
- error = "Usage: events chat (add|remove)";
- }
- else if(c == "watchlist")
- {
- if(t == "add")
- type = EventsWatchlistAdd;
- else if(t == "remove")
- type = EventsWatchlistRemove;
- else
- error = "Usage: events watchlist (add|remove)";
- }
- else if(c == "all")
- {
- if(t == "add")
- type = EventsAllAdd;
- else if(t == "remove")
- type = EventsAllRemove;
- else
- error = "Usage: events all (add|remove)";
- }
- else
- error = "Usage: events (all|chat|ns|watchlist) (add|remove)";
- }
- else
- error = "Usage: events (all|chat|ns|watchlist) (add|remove)";
- }
- else if(c == "watchlist")
- {
- if(!commands.isEmpty())
- {
- c = commands.at(0);
- commands.removeFirst();
- if(c == "add")
- {
- if(!commands.isEmpty())
- {
- type = WatchlistAdd;
- query << commands;
- }
- else
- error = "Usage: watchlist add login1 login2 ...";
- }
- else if(c == "whois")
- {
- if(!commands.isEmpty())
- {
- type = WatchlistWhois;
- query << commands;
- }
- else
- error = "Usage: watchlist whois login1 login2 ...";
- }
- else
- error = "Usage: watchlist (add|whois)";
- }
- else
- error = "Usage: watchlist (add|whois)";
- }
- else
- error = "Usage: (events|ns|users)";
- }
- else
- error = "Empty query";
-
- if(error.isEmpty())
- {
- if(print && !m_showOnlyResponse)
- qinfo<<"Sending command:"<<cmd.toStdString().c_str();
- m_sock->send(type, queryData);
- }
- else
- {
- m_lastQueryResult = false;
- if(!m_showOnlyResponse)
- qerror<<error.toStdString().c_str();
- mayDisconnect();
- }
-
- if(!print)
- readStdin();
- }
-
- void MainClass::setCloseOnNextQuery(bool m)
- {
- m_closeOnNextQuery = m;
- }
-
- void MainClass::setShowOnlyResponse(bool m)
- {
- m_showOnlyResponse = m;
- }
-
- void MainClass::setColored(bool m)
- {
- m_colored = m;
- }
-
- void MainClass::connectToDaemon()
- {
- if(!m_sock->isOpen())
- m_sock->connectToHost(QHostAddress::LocalHost, 12642);
- }
-
- void MainClass::addCommands(QStringList cmds)
- {
- m_cmds << cmds;
- }
-
- void MainClass::stateChanged(PTSocket::State s)
- {
- if(s == PTSocket::Connecting)
- {
- if(!m_showOnlyResponse)
- qinfo<<"Connecting to daemon...";
- }
- else if(s == PTSocket::Handshaked)
- {
- if(!m_showOnlyResponse)
- qok<<"Connected to daemon";
- if(!m_cmds.isEmpty())
- foreach(QString cmd, m_cmds)
- send(cmd.toStdString().c_str(), true);
- else
- readStdin();
- }
- else if(s == PTSocket::Error)
- {
- if(!m_showOnlyResponse)
- qerror<<m_sock->getError().toStdString().c_str();
- qApp->exit(1);
- }
- else if(s == PTSocket::Disconnected)
- {
- if(!m_showOnlyResponse)
- qinfo<<"Disconnected from daemon";
- qApp->exit(m_lastQueryResult ? 0 : 1);
- }
- }
-
- void MainClass::packetReceived(int type, QByteArray replyData)
- {
- ++m_responsesCount;
- QDataStream reply(&replyData, QIODevice::ReadOnly);
- reply >> m_lastQueryResult;
- if(m_lastQueryResult)
- {
- if(type == UsersList)
- {
- QStringList users;
- reply >> users;
- if(!m_showOnlyResponse)
- qinfo<<"Available users:";
- foreach(QString user, users)
- qDebug()<<user.toStdString().c_str();
- }
- else if(type == UsersAdd)
- {
- if(!m_showOnlyResponse)
- qok<<"User successfully added or updated";
- }
- else if(type == UsersRemove)
- {
- if(!m_showOnlyResponse)
- qok<<"User or user\'s socks password successfully removed";
- }
-
- else if(type == NSConnect)
- {
- if(!m_showOnlyResponse)
- qinfo<<"Connecting to netsoul server";
- }
- else if(type == NSDisconnect)
- {
- if(!m_showOnlyResponse)
- qinfo<<"Disconnecting from netsoul server";
- }
- else if(type == NSStatus)
- {
- NetSoul::State s;
- reply >> s;
- if(!m_showOnlyResponse)
- qinfo<<"Status:"<<NetSoul::getStringFromState(s).toStdString().c_str();
- else
- qDebug()<<NetSoul::getStringFromState(s).toStdString().c_str();
- }
- else if(type == NSLocationGet)
- {
- QString l;
- reply >> l;
- if(!m_showOnlyResponse)
- qinfo<<"Location:"<<l;
- else
- qDebug()<<l;
- }
- else if(type == NSLocationSet)
- {
- if(!m_showOnlyResponse)
- qinfo<<"Location successfully changed";
- }
-
- else if(type == EventsNSAdd)
- {
- if(!m_showOnlyResponse)
- qok<<"Netsoul event registered";
- }
- else if(type == EventsNSRemove)
- {
- if(!m_showOnlyResponse)
- qok<<"Netsoul event removed";
- }
-
- else if(type == EventsWatchlistAdd)
- {
- if(!m_showOnlyResponse)
- qok<<"Watchlist event registered";
- }
- else if(type == EventsWatchlistRemove)
- {
- if(!m_showOnlyResponse)
- qok<<"Watchlist event removed";
- }
-
- else if(type == EventsChat)
- {
- NetSoul::Message msg;
- reply >> msg;
- if(!m_showOnlyResponse)
- qinfo<<"Message received:"<<msg;
- else
- qDebug()<<msg;
- }
- else if(type == EventsChatAdd)
- {
- if(!m_showOnlyResponse)
- qok<<"Chat event registered";
- }
- else if(type == EventsChatRemove)
- {
- if(!m_showOnlyResponse)
- qok<<"Chat event removed";
- }
- else if(type == EventsAllAdd)
- {
- if(!m_showOnlyResponse)
- qok<<"All events registered";
- }
- else if(type == EventsAllRemove)
- {
- if(!m_showOnlyResponse)
- qok<<"All events removed";
- }
-
- else if(type == WatchlistAdd)
- {
- if(!m_showOnlyResponse)
- qok<<"Users added to watchlist";
- }
- else if(type == WatchlistWhois)
- {
- NetSoul::User usr;
- reply >> usr;
- if(reply.status() == QDataStream::Ok)
- {
- if(!m_showOnlyResponse)
- qinfo<<"Watchlist event:"<<usr;
- else
- qDebug()<<usr;
- }
- else
- qerror<<"Failed to retreive user in whois reply";
- }
- else
- qerror<<"Unknown reply ("<<type<<")";
- }
- else
- {
- QString error;
- reply >> error;
- if(!m_showOnlyResponse)
- qerror<<error.toStdString().c_str();
- }
- mayDisconnect();
- }
-
- void MainClass::readStdin()
- {
- /*QTextStream t(stdin);
- send(t.readLine(), false);*/
- }
-
- void MainClass::mayDisconnect()
- {
- if(m_closeOnNextQuery && m_responsesCount >= m_cmds.size())
- m_sock->disconnectFromServer();
- }
|