#include "mainclass.h" #include #include #include #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:"<send(type, queryData); } else { m_lastQueryResult = false; if(!m_showOnlyResponse) qerror<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<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()<> s; if(!m_showOnlyResponse) qinfo<<"Status:"<> l; if(!m_showOnlyResponse) qinfo<<"Location:"<> msg; if(!m_showOnlyResponse) qinfo<<"Message received:"<> usr; if(reply.status() == QDataStream::Ok) { if(!m_showOnlyResponse) qinfo<<"Watchlist event:"<> error; if(!m_showOnlyResponse) qerror<= m_cmds.size()) m_sock->disconnectFromServer(); }