Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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