httpserver.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. #include "httpserver.h"
  2. #include "ircclient.h"
  3. #include "connectiondata.h"
  4. #include "global.h"
  5. #include "version.h"
  6. #include <QRegularExpression>
  7. #include <QDirIterator>
  8. #include <QDateTime>
  9. #include <QHostAddress>
  10. #include <QTcpSocket>
  11. #include <QDebug>
  12. #include <QJsonDocument>
  13. #include <QJsonArray>
  14. #include <QJsonObject>
  15. #include <QFile>
  16. #include <QDir>
  17. const char CRITICAL_ERROR[] {"<title>Critical error</title><center><h1>NOT FOUND</H1><p>Maybe it's compile time error</p></center>"};
  18. HttpServer::HttpServer(const QString &address, quint16 port, const QString& logFolder,
  19. const QString& mainChannel, bool ajaxIsDisabled, QObject *parent) :
  20. QObject(parent),
  21. m_TcpServer(new QTcpServer),
  22. m_mainChannel(mainChannel),
  23. m_logFolder(logFolder),
  24. m_ajaxIsDisabled(ajaxIsDisabled)
  25. {
  26. if (not m_TcpServer->listen(QHostAddress(address), port)) {
  27. throw std::runtime_error("HttpServer not binded at " +
  28. address.toStdString() + " : " + QString::number(port).toStdString());
  29. }
  30. consoleLog(address + " : " + QString::number(port));
  31. if (m_ajaxIsDisabled) {
  32. consoleLog("JavaScript on webpages removed and AJAX disabled!");
  33. }
  34. connect (m_TcpServer, &QTcpServer::newConnection, this, &HttpServer::acceptor);
  35. }
  36. HttpServer::~HttpServer()
  37. {
  38. m_TcpServer->close();
  39. m_TcpServer->deleteLater();
  40. }
  41. QString HttpServer::convertToClickableLink(const QString &httpLine)
  42. {
  43. QString result;
  44. if (not httpLine.contains(QRegularExpression("http.?://"))) return result;
  45. QString displayedName {httpLine};
  46. displayedName.remove(QRegularExpression("http.?://(www\\.)?"));
  47. displayedName.remove(QRegularExpression("/$"));
  48. result = "<a href=\"" + httpLine + "\"> " + displayedName + " </a>";
  49. return result;
  50. }
  51. std::pair<QString, QString> HttpServer::splitUserNameAndMessage(const QString &rawLine)
  52. {
  53. std::pair<QString, QString> result;
  54. QString nick {rawLine};
  55. nick.remove(QRegularExpression("\\]\\s.*$"));
  56. nick.remove(QRegularExpression("^\\["));
  57. if (nick.isEmpty()) {
  58. return result;
  59. }
  60. nick = nick.toHtmlEscaped();
  61. // long nicks
  62. if (nick.size() > MAX_NICKNAME_LENGTH_WITHOUT_WBR) {
  63. int lastWbr = 0;
  64. for (int i = 0; i < nick.size(); i++) {
  65. if (i-lastWbr > MAX_NICKNAME_LENGTH_WITHOUT_WBR) {
  66. nick.insert(i, "<wbr>");
  67. lastWbr = i;
  68. }
  69. }
  70. }
  71. QString text {rawLine};
  72. text.remove(QRegularExpression("^\\[[^\\s]*\\]\\s"));
  73. if (text.isEmpty()) {
  74. return result;
  75. }
  76. text = text.toHtmlEscaped();
  77. // http links
  78. bool linksFound {false};
  79. while (QRegularExpression("(^|\\s)http.?://").match(text).hasMatch()) {
  80. if (not linksFound) linksFound = true;
  81. int pos = text.indexOf(QRegularExpression("(^|\\s)http.?://"));
  82. if (pos == -1) {
  83. consoleLog("Bug! HttpServer.cpp while (QRegularExpression(\"(^|\\s)http.?://\").match(text).hasMatch())");
  84. break;
  85. }
  86. QString rawLink {text};
  87. rawLink.remove(0, pos);
  88. if (rawLink.startsWith(' ')) {
  89. rawLink.remove(0,1);
  90. }
  91. int space = rawLink.indexOf(' ');
  92. if (space > 0) {
  93. rawLink.remove(space, rawLink.size()-space);
  94. }
  95. text.replace(rawLink, convertToClickableLink(rawLink));
  96. }
  97. // long lines
  98. int space = 0;
  99. bool nbTag = false; // For safe HTML tags like a &it; via <wbr>!
  100. bool isHref = false;
  101. for (int i = 0; i < text.size(); i++) {
  102. if (text[i] == ' ') {
  103. space = i;
  104. if (isHref) {
  105. isHref = false;
  106. }
  107. else {
  108. if (text.indexOf("href=\"http", i+1) == i+1) {
  109. isHref = true;
  110. }
  111. }
  112. }
  113. if (nbTag and text[i-1] == ';') {
  114. nbTag = false;
  115. }
  116. if (text.indexOf(QRegularExpression("(\\&amp;|\\&lt;|\\&gt;|\\&quot;).*"), i) == i) {
  117. nbTag = true;
  118. }
  119. if (not isHref and i-space > MAX_MESSAGE_LENGTH_WITHOUT_WBR and not nbTag) {
  120. text.insert(i, "<wbr>");
  121. space = i;
  122. }
  123. }
  124. if (linksFound) text.replace(" </a>", "</a>"); // delete whitespace in links end
  125. result.first = nick;
  126. result.second = text;
  127. return result;
  128. }
  129. void HttpServer::consoleLog(const QString &message)
  130. {
  131. qInfo().noquote() << "[WEB_UI]" << message;
  132. }
  133. void HttpServer::debugLog(const QString &req)
  134. {
  135. QFile log(m_logFolder + "debug.log");
  136. if (log.open(QIODevice::WriteOnly | QIODevice::Append)) {
  137. log.write(QDateTime::currentDateTime().toString().toUtf8() + ":\n" + req.toUtf8() + "\n\n");
  138. log.close();
  139. }
  140. }
  141. void HttpServer::acceptor()
  142. {
  143. QTcpSocket* socket = m_TcpServer->nextPendingConnection();
  144. connect(socket, &QTcpSocket::readyRead, this, &HttpServer::reader);
  145. connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
  146. }
  147. void HttpServer::reader()
  148. {
  149. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
  150. QString request = socket->read(BUFFER_SIZE);
  151. if (not request.startsWith("GET") and not request.startsWith("HEAD")) {
  152. if (socket->isOpen()) {
  153. socket->write("Your request has been rejected!\n");
  154. socket->disconnectFromHost();
  155. }
  156. return;
  157. }
  158. bool isHeadRequest = false;
  159. if (request.startsWith("HEAD ")) {
  160. isHeadRequest = true;
  161. }
  162. QString urlPath = getRequestPath(request);
  163. // static files
  164. if (urlPath == "/favicon.ico") {
  165. QString eTag = global::getValue(request, "If-None-Match", global::eHttpHeader);
  166. if (eTag == HTTP_ACTUAL_ETAG) {
  167. if (socket->isOpen()) socket->write(HEADER_304.toUtf8());
  168. }
  169. else {
  170. QFile icon("://html/favicon.ico");
  171. if (icon.open(QIODevice::ReadOnly)) {
  172. QByteArray file = icon.readAll();
  173. icon.close();
  174. QString header = HEADER_ICO;
  175. replaceTag(header, "SIZE", QString::number(file.size()));
  176. if (socket->isOpen()) {
  177. socket->write(header.toUtf8());
  178. if (not isHeadRequest) socket->write(file);
  179. }
  180. }
  181. }
  182. }
  183. else if (urlPath == "/newmessage.mp3" and not m_ajaxIsDisabled) {
  184. QString eTag = global::getValue(request, "If-None-Match", global::eHttpHeader);
  185. if (eTag == HTTP_ACTUAL_ETAG) {
  186. if (socket->isOpen()) socket->write(HEADER_304.toUtf8());
  187. }
  188. else {
  189. QFile mp3("://html/newmessage.mp3");
  190. if (mp3.open(QIODevice::ReadOnly)) {
  191. QByteArray file = mp3.readAll();
  192. mp3.close();
  193. QString header = HEADER_MP3;
  194. replaceTag(header, "SIZE", QString::number(file.size()));
  195. if (socket->isOpen()) {
  196. socket->write(header.toUtf8());
  197. if (not isHeadRequest) socket->write(file);
  198. }
  199. }
  200. }
  201. }
  202. else if (urlPath == "/style.css") {
  203. QString eTag = global::getValue(request, "If-None-Match", global::eHttpHeader);
  204. if (eTag == HTTP_ACTUAL_ETAG) {
  205. if (socket->isOpen()) socket->write(HEADER_304.toUtf8());
  206. }
  207. else {
  208. QFile css("://html/style.css");
  209. if (css.open(QIODevice::ReadOnly)) {
  210. QByteArray file = css.readAll();
  211. css.close();
  212. QString header = HEADER_CSS;
  213. replaceTag(header, "SIZE", QString::number(file.size()));
  214. if (socket->isOpen()) {
  215. socket->write(header.toUtf8());
  216. if (not isHeadRequest) socket->write(file);
  217. }
  218. }
  219. }
  220. }
  221. else if (urlPath.endsWith(".svg")) {
  222. QString eTag = global::getValue(request, "If-None-Match", global::eHttpHeader);
  223. if (eTag == HTTP_ACTUAL_ETAG) {
  224. if (socket->isOpen()) socket->write(HEADER_304.toUtf8());
  225. }
  226. else {
  227. QFile svg("://html"+urlPath);
  228. if (svg.open(QIODevice::ReadOnly)) {
  229. QByteArray file = svg.readAll();
  230. svg.close();
  231. QString header = HEADER_SVG;
  232. replaceTag(header, "SIZE", QString::number(file.size()));
  233. if (socket->isOpen()) {
  234. socket->write(header.toUtf8());
  235. if (not isHeadRequest) socket->write(file);
  236. }
  237. }
  238. else {
  239. if (socket->isOpen()) {
  240. socket->write(HEADER_404.toUtf8());
  241. if (not isHeadRequest) writeErrorPage(socket);
  242. }
  243. }
  244. }
  245. }
  246. else if (urlPath == "/realtimechat.js" and not m_ajaxIsDisabled) {
  247. QFile js("://html/realtimechat.js");
  248. if (js.open(QIODevice::ReadOnly)) {
  249. QByteArray file = js.readAll();
  250. js.close();
  251. QString header = HEADER_JS;
  252. replaceTag(header, "SIZE", QString::number(file.size()));
  253. if (socket->isOpen()) {
  254. socket->write(header.toUtf8());
  255. if (not isHeadRequest) socket->write(file);
  256. }
  257. }
  258. }
  259. else if (urlPath.startsWith("/ajax/")) {
  260. if (m_ajaxIsDisabled) {
  261. writeErrorJson(socket, "AJAX disabled");
  262. } else {
  263. writeAjaxAnswer(socket, urlPath, isHeadRequest);
  264. }
  265. }
  266. else if (urlPath.startsWith("/realtimereadingchat/")) {
  267. if (m_ajaxIsDisabled) {
  268. writeErrorPage(socket, "DISABLED"
  269. "<p>No JS, no AJAX, no real time reading!</p>");
  270. } else {
  271. writeRealTimeChatPage(socket, urlPath, isHeadRequest);
  272. }
  273. }
  274. else if (urlPath.startsWith("/serverinformation/")) {
  275. writeAboutServerPage(socket, urlPath, isHeadRequest);
  276. }
  277. else {
  278. writeMainPage(socket, urlPath, isHeadRequest);
  279. }
  280. socket->disconnectFromHost();
  281. }
  282. void HttpServer::ircBotFirstInfo(QString server, QStringList channels)
  283. {
  284. for (const auto &c: channels) {
  285. if (c.isEmpty()) continue;
  286. m_servers[server][c] = QStringList();
  287. }
  288. }
  289. void HttpServer::ircUsersOnline(QString server, QString channel, QStringList users)
  290. {
  291. if (server.isEmpty()) return;
  292. if (channel.isEmpty()) return;
  293. QStringList sortedNicknames;
  294. QStringList ownersNicks;
  295. QStringList operNicks;
  296. QStringList halfopNicks;
  297. QStringList adminNicks;
  298. QStringList voicedNicks;
  299. QStringList plainNicks;
  300. for (auto rawOneNick: users) {
  301. rawOneNick = rawOneNick.toHtmlEscaped();
  302. if (rawOneNick.startsWith('~')) {
  303. ownersNicks.push_back(rawOneNick);
  304. }
  305. else if (rawOneNick.startsWith('@')) {
  306. operNicks.push_back(rawOneNick);
  307. }
  308. else if (rawOneNick.startsWith('%')) {
  309. halfopNicks.push_back(rawOneNick);
  310. }
  311. else if (rawOneNick.startsWith('&')) {
  312. adminNicks.push_back(rawOneNick);
  313. }
  314. else if (rawOneNick.startsWith('+')) {
  315. voicedNicks.push_back(rawOneNick);
  316. }
  317. else {
  318. plainNicks.push_back(rawOneNick);
  319. }
  320. }
  321. if (not ownersNicks.isEmpty()) {
  322. std::sort(ownersNicks.begin(), ownersNicks.end());
  323. sortedNicknames += ownersNicks;
  324. }
  325. if (not operNicks.isEmpty()) {
  326. std::sort(operNicks.begin(), operNicks.end());
  327. sortedNicknames += operNicks;
  328. }
  329. if (not halfopNicks.isEmpty()) {
  330. std::sort(halfopNicks.begin(), halfopNicks.end());
  331. sortedNicknames += halfopNicks;
  332. }
  333. if (not adminNicks.isEmpty()) {
  334. std::sort(adminNicks.begin(), adminNicks.end());
  335. sortedNicknames += adminNicks;
  336. }
  337. if (not voicedNicks.isEmpty()) {
  338. std::sort(voicedNicks.begin(), voicedNicks.end());
  339. sortedNicknames += voicedNicks;
  340. }
  341. if (not plainNicks.isEmpty()) {
  342. std::sort(plainNicks.begin(), plainNicks.end());
  343. sortedNicknames += plainNicks;
  344. }
  345. sortedNicknames.removeAll("");
  346. m_servers[server][channel] = sortedNicknames;
  347. }
  348. void HttpServer::ircChannelTopic(QString server, QString channel, QString topic)
  349. {
  350. m_channelsTopic[server][channel] = topic;
  351. }
  352. void HttpServer::ircServerOnline(QString server, quint8 status)
  353. {
  354. if (server.isEmpty()) return;
  355. bool online = status;
  356. m_serversOnline[server] = online;
  357. }
  358. void HttpServer::ircBotNick(QString server, QString nickname)
  359. {
  360. m_botNick[server] = nickname;
  361. }
  362. void HttpServer::ircMessageCache(QString server, QString channel, QString nick, QString text)
  363. {
  364. QString channelId {server+channel};
  365. if (not m_messageCache.contains(channelId)) return;
  366. // remove timed out session
  367. if (QDateTime::currentMSecsSinceEpoch() - MSECS_TO_AUTOREMOVE_MESSAGES_FROM_BUFFER >
  368. m_messageCache[channelId]->getLastPing())
  369. {
  370. consoleLog("Message caching disabled for "+server+"/#"+channel+". No active reader.");
  371. m_messageCache.remove(channelId);
  372. return;
  373. }
  374. else {
  375. auto processedMsg {splitUserNameAndMessage("["+nick+"] " + text)};
  376. m_messageCache[channelId]->saveNewMessage(processedMsg.first, processedMsg.second);
  377. }
  378. }
  379. QString HttpServer::getRequestPath(const QString &req)
  380. {
  381. if (req.isEmpty()) return QString();
  382. QString result(req);
  383. int begin = result.indexOf(' ');
  384. if (begin == -1) return QString();
  385. result.remove(0, begin+1);
  386. int space = result.indexOf(' ');
  387. int size = result.size();
  388. result.remove(space, size-space);
  389. result = QByteArray::fromPercentEncoding(result.toUtf8());
  390. return result;
  391. }
  392. QString HttpServer::getWordFromPath(const QString &path)
  393. {
  394. QString result {path};
  395. result.remove(QRegularExpression("\\?.*$")); // any actions like a ?toSearch=
  396. if (result.startsWith('/')) {
  397. result.remove(QRegularExpression("^/"));
  398. }
  399. result.remove(QRegularExpression("/.*$"));
  400. return result;
  401. }
  402. void HttpServer::writeErrorPage(QTcpSocket *socket, const QString& text)
  403. {
  404. if (socket->isOpen()) {
  405. socket->write(HEADER_404.toUtf8());
  406. socket->write("<title>404</title><center><h1>"+text.toUtf8()+"</H1></center>");
  407. }
  408. }
  409. void HttpServer::writeErrorJson(QTcpSocket * socket, const QString &text)
  410. {
  411. QString header = HEADER_JSON;
  412. QByteArray body {
  413. "{\"status\": false, \"message\": \"" + text.toUtf8() + "\"}"
  414. };
  415. replaceTag(header, "SIZE", QString::number(body.size()));
  416. socket->write(header.toUtf8());
  417. socket->write(body);
  418. }
  419. void HttpServer::removeBrakelineSymbols(QString &line)
  420. {
  421. line.remove('\r');
  422. line.remove('\n');
  423. line.remove('\t');
  424. }
  425. inline void HttpServer::replaceTag(QString &page, const QString &tag, const QString &payload)
  426. {
  427. page.replace("{{"+tag+"}}", payload);
  428. }
  429. void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath, bool isHeadRequest)
  430. {
  431. auto renderStart = QDateTime::currentMSecsSinceEpoch();
  432. QString searchRequest;
  433. bool isRegexp {false};
  434. int specSymbol = urlPath.indexOf('?'); // any actions like a ?toSearch=
  435. if (specSymbol != -1) {
  436. searchRequest = global::getValue(urlPath, "toSearch", global::eForWeb);
  437. isRegexp = global::getValue(urlPath, "isRegexp", global::eForWeb) == "on";
  438. urlPath.remove(specSymbol, urlPath.size()-specSymbol);
  439. }
  440. if (urlPath == "/") {
  441. urlPath += m_mainChannel;
  442. }
  443. QFile main("://html/main.html");
  444. QString page;
  445. if (main.open(QIODevice::ReadOnly)) {
  446. page = main.readAll();
  447. main.close();
  448. }
  449. else {
  450. if (socket->isOpen()) {
  451. socket->write(HEADER_404.toUtf8());
  452. if (not isHeadRequest) socket->write(CRITICAL_ERROR);
  453. }
  454. }
  455. if (isRegexp) {
  456. page.replace("<input id=\"main_header__search_checkbox__button\" type=\"checkbox\" name=\"isRegexp\">",
  457. "<input id=\"main_header__search_checkbox__button\" type=\"checkbox\" name=\"isRegexp\" checked>");
  458. }
  459. QString server = getWordFromPath(urlPath);
  460. QDir fsPath(m_logFolder+server);
  461. if (not fsPath.exists()) {
  462. if (isHeadRequest) {
  463. if (socket->isOpen()) socket->write(HEADER_404.toUtf8());
  464. } else {
  465. writeErrorPage(socket, "REQUESTED SERVER LOG NOT EXIST");
  466. }
  467. return;
  468. }
  469. urlPath.remove(QRegularExpression("^.*/"+server));
  470. QString channel = getWordFromPath(urlPath);
  471. channel.remove(QRegularExpression("\\?.*$"));
  472. if (channel.isEmpty()){
  473. // First channel is main if not passed directly
  474. QDirIterator it(fsPath.path());
  475. while (it.hasNext()) {
  476. channel = it.next();
  477. if (channel.endsWith(".") or channel.endsWith("..")) continue; // QDir::NoDotAndNoDotDot not works!
  478. while(channel.contains('/')) {
  479. channel.remove(QRegularExpression("^.*/"));
  480. }
  481. break;
  482. }
  483. }
  484. if (not fsPath.cd(channel)) {
  485. if (isHeadRequest) {
  486. if (socket->isOpen()) socket->write(HEADER_404.toUtf8());
  487. } else {
  488. writeErrorPage(socket, "REQUESTED CHANNEL LOG NOT EXIST");
  489. }
  490. return;
  491. }
  492. QString originalServerName;
  493. for (const auto &s: m_servers) {
  494. if (global::toLowerAndNoSpaces(s.first) == server) {
  495. originalServerName = s.first;
  496. }
  497. }
  498. QString originalChannelName;
  499. for (const auto &server: m_servers) {
  500. for (const auto &channel_users: server.second) {
  501. if (global::toLowerAndNoSpaces(channel_users.first) == "#"+channel) {
  502. originalChannelName = global::toLowerAndNoSpaces(channel_users.first);
  503. }
  504. }
  505. }
  506. urlPath.remove(QRegularExpression("^.*/"+channel));
  507. QString year = getWordFromPath(urlPath);
  508. year.remove(QRegularExpression("\\?.*$"));
  509. QString month;
  510. QString day;
  511. if (not year.isEmpty() and fsPath.cd(year)) {
  512. urlPath.remove(QRegularExpression("^.*/"+year));
  513. month = getWordFromPath(urlPath);
  514. month.remove(QRegularExpression("\\?.*$"));
  515. if (not month.isEmpty() and fsPath.cd(month)) {
  516. if (urlPath.startsWith("/"+month+"/")) {
  517. urlPath.remove(0,1);
  518. int pos = urlPath.indexOf('/');
  519. if (pos != -1) {
  520. urlPath.remove(0,pos);
  521. day = getWordFromPath(urlPath);
  522. if (urlPath.endsWith(".txt")) {
  523. QFile plain(fsPath.path()+global::slash+day);
  524. if (plain.open(QIODevice::ReadOnly)) {
  525. QString header = HEADER_TEXT;
  526. QByteArray file = plain.readAll();
  527. plain.close();
  528. replaceTag(header, "SIZE", QString::number(file.size()));
  529. if (socket->isOpen()) {
  530. socket->write(header.toUtf8());
  531. if (not isHeadRequest) socket->write(file);
  532. }
  533. }
  534. else {
  535. if (isHeadRequest) {
  536. if (socket->isOpen()) socket->write(HEADER_404.toUtf8());
  537. } else {
  538. writeErrorPage(socket, "FILE OPENING FAILED");
  539. }
  540. }
  541. return;
  542. }
  543. else {
  544. if (not QFile::exists(fsPath.path()+global::slash+day+".txt")) {
  545. day.clear();
  546. }
  547. }
  548. }
  549. }
  550. }
  551. else { month.clear(); }
  552. }
  553. else { year.clear(); }
  554. //// Left menu compilation
  555. QString htmlServersSectionS;
  556. for (const auto &s: m_servers) {
  557. if (s.first.isEmpty()) continue; // empty server name?
  558. QString htmlServersSection = HTML_SERVER_SECTION;
  559. replaceTag(htmlServersSection, "ABOUT_SERVER", "/serverinformation/"+s.first);
  560. replaceTag(htmlServersSection, "SERVER_NAME", s.first);
  561. QString htmlChannelLineS;
  562. for (const auto &c: s.second) {
  563. QString htmlChannelLine;
  564. if (originalServerName == s.first and originalChannelName == c.first) {
  565. htmlChannelLine = HTML_SERVER_SECTION_CHANNEL_SELECTED;
  566. } else {
  567. htmlChannelLine = HTML_SERVER_SECTION_CHANNEL;
  568. }
  569. replaceTag(htmlChannelLine, "CHANNEL_NAME", c.first);
  570. QString channelNameForUrl {c.first};
  571. channelNameForUrl.remove('#');
  572. QString channelLink = "/" + global::toLowerAndNoSpaces(s.first) + "/" + channelNameForUrl;
  573. if (not year.isEmpty()) {
  574. channelLink += "/" + year;
  575. if (not month.isEmpty()) {
  576. channelLink += "/" + month;
  577. if (not day.isEmpty()) {
  578. channelLink += "/" + day;
  579. }
  580. }
  581. }
  582. replaceTag(htmlChannelLine, "CHANNEL_LINK", channelLink);
  583. htmlChannelLineS += htmlChannelLine;
  584. }
  585. replaceTag(htmlServersSection, "CHANNELS", htmlChannelLineS);
  586. bool online {false};
  587. for (const auto &srv: m_serversOnline) {
  588. if (srv.first == s.first) {
  589. online = srv.second;
  590. break;
  591. }
  592. }
  593. if (online) {
  594. replaceTag(htmlServersSection, "ONLINE_STATUS", HTML_SERVER_ONLINE_MARKER);
  595. } else {
  596. replaceTag(htmlServersSection, "ONLINE_STATUS", HTML_SERVER_OFFLINE_MARKER);
  597. }
  598. if (s.first == originalServerName) {
  599. htmlServersSectionS.push_front(htmlServersSection);
  600. } else {
  601. htmlServersSectionS += htmlServersSection;
  602. }
  603. }
  604. replaceTag(page, "SERVERS_SECTION", htmlServersSectionS);
  605. //// Main section header compilation
  606. QString& topic = m_channelsTopic[originalServerName][originalChannelName];
  607. topic = topic.replace('\"', "&quot;");
  608. QString titlePostfix = " | IRCaBot";
  609. if (not topic.isEmpty()) {
  610. titlePostfix.push_front(" | " + topic);
  611. }
  612. if (m_servers.size() > 1) {
  613. replaceTag(page, "PAGE_TITLE", originalChannelName + " ("+originalServerName+")" + titlePostfix);
  614. } else {
  615. replaceTag(page, "PAGE_TITLE", originalChannelName + titlePostfix);
  616. }
  617. replaceTag(page, "CHANNEL_TOPIC", topic);
  618. replaceTag(page, "MAIN_HEADER", originalChannelName);
  619. if (m_ajaxIsDisabled) {
  620. page.remove("<a href=\"{{REALTIME_LINK}}\" title=\"{{AIRPLAIN_TITLE}}\" class=\"main_header__title_airplaine\"></a>");
  621. }
  622. else {
  623. replaceTag(page, "REALTIME_LINK", "/realtimereadingchat/"+server+"/"+channel);
  624. replaceTag(page, "AIRPLAIN_TITLE", "Read in real time");
  625. }
  626. QString middlePath = "<a style=\"text-decoration: none\" href=\"/"+server+"/"+channel+"\">" + "/" + "</a>";
  627. if (not year.isEmpty()) {
  628. middlePath += "<a style=\"text-decoration: none\" href=\"/"+server+"/"+channel+"/"+year+"\">" + year + "</a>";
  629. if (not month.isEmpty()) {
  630. middlePath += "/<a style=\"text-decoration: none\" href=\"/"+server+"/"+channel+"/"+year+"/"+month+"\">" + month + "</a>";
  631. if (not day.isEmpty()) {
  632. middlePath += "/" + day;
  633. }
  634. }
  635. }
  636. QString arrows = HTML_PAYLOAD_ADDITIONAL_ARROWS;
  637. QString currentYear {QDateTime::currentDateTime().toString("yyyy")};
  638. QString currentMonth {QDateTime::currentDateTime().toString("MM")};
  639. QString currentDay {QDateTime::currentDateTime().toString("dd")};
  640. replaceTag(arrows, "CURRENT_DATA_LOG", "/"+server+"/"+channel+"/"+
  641. currentYear+"/"+currentMonth+"/"+currentDay);
  642. replaceTag(page, "ADDITIONAL_BUTTON", arrows);
  643. int currentOnline = 0;
  644. QString onlineUserS;
  645. for (const auto &user: m_servers[originalServerName][originalChannelName]) {
  646. if (QRegularExpression("^(.*;|~|@|\\&|\\+)?"+m_botNick[originalServerName]+"$").match(user).hasMatch()) {
  647. continue;
  648. }
  649. QString onlineUser = HTML_ONLINE_POINT;
  650. replaceTag(onlineUser, "NICKNAME", user);
  651. onlineUserS += onlineUser;
  652. currentOnline++;
  653. }
  654. replaceTag(page, "ONLINE", QString::number(currentOnline));
  655. replaceTag(page, "ONLINE_LIST", onlineUserS);
  656. if (not searchRequest.isEmpty()) {
  657. page.replace("<input class=\"main_header__search_input\" type=\"search\" name=\"toSearch\" placeholder=\"{{SEARCH_PLACEHOLDER}}\">",
  658. "<input class=\"main_header__search_input\" type=\"search\" name=\"toSearch\" value=\"" + searchRequest.toHtmlEscaped() + "\">");
  659. } else if (middlePath == "/") {
  660. replaceTag(page, "SEARCH_PLACEHOLDER", originalChannelName);
  661. } else if (month.isEmpty()) {
  662. replaceTag(page, "SEARCH_PLACEHOLDER", originalChannelName + "/" + year);
  663. } else {
  664. replaceTag(page, "SEARCH_PLACEHOLDER", originalChannelName + "/" + year + "/" + month);
  665. }
  666. //// Main section body compilation
  667. QString payloadBlock;
  668. // Search request
  669. if (not searchRequest.isEmpty()) {
  670. uint counter = 0;
  671. QRegularExpression userRgx(searchRequest, QRegularExpression::CaseInsensitiveOption);
  672. bool rgxIsValid = false;
  673. if (isRegexp and userRgx.isValid()) {
  674. rgxIsValid = true;
  675. }
  676. consoleLog("Search request (" + server + "): " + searchRequest);
  677. if (not day.isEmpty()) {
  678. middlePath.remove(QRegularExpression("/[0-9]{2}$"));
  679. }
  680. QStringList paths;
  681. QDirIterator it(fsPath.path());
  682. while (it.hasNext()) {
  683. QString currentPath = it.next();
  684. if (currentPath.endsWith(".") or currentPath.endsWith("..")) continue;
  685. QString logFolder = m_logFolder;
  686. #ifdef WIN32
  687. logFolder.replace('\\', '/');
  688. #endif
  689. QString server {currentPath}; // Folder wich is not server folder is ignored
  690. server.remove(QRegularExpression("^"+logFolder));
  691. server.remove(QRegularExpression("/.*$"));
  692. bool serverIsOk = false;
  693. for (const auto &srv: m_servers) {
  694. if (global::toLowerAndNoSpaces(srv.first) == server) {
  695. serverIsOk = true;
  696. break;
  697. }
  698. }
  699. if (not serverIsOk) continue;
  700. QString currentChannel {currentPath}; // Folder wich is not channel folder is ignored
  701. currentChannel.remove(QRegularExpression("^"+logFolder+"[^/]*/"));
  702. currentChannel.remove(QRegularExpression("/.*$"));
  703. bool channelIsOk = false; // Канал явно указан в конфиге
  704. for (const auto &ch: m_servers[originalServerName]) {
  705. QString searchChan {ch.first};
  706. searchChan.remove('#');
  707. if (searchChan == currentChannel) {
  708. channelIsOk = true;
  709. break;
  710. }
  711. }
  712. if (not channelIsOk) continue;
  713. paths.push_back(currentPath);
  714. }
  715. if (paths.isEmpty()) {
  716. payloadBlock = HTML_PAYLOAD_ERROR;
  717. replaceTag(payloadBlock, "ERROR_TITLE", "Not found");
  718. replaceTag(payloadBlock, "ERROR_TEXT", "");
  719. }
  720. else {
  721. std::map<QString, QStringList> matchedPathsAndMessages;
  722. if (not month.isEmpty()) {
  723. for (const auto& path: paths) {
  724. if (not QRegularExpression("^.*[0-9]{2}\\.txt$").match(path).hasMatch()) continue;
  725. QFile file(path);
  726. if (not file.open(QIODevice::ReadOnly)) {
  727. consoleLog("Error! I can't open log file " + fsPath.path());
  728. continue;
  729. }
  730. QString buffer {file.readLine()};
  731. while (not buffer.isEmpty()) {
  732. removeBrakelineSymbols(buffer);
  733. bool finded = false;
  734. if (rgxIsValid) {
  735. if (QRegularExpression(searchRequest, QRegularExpression::CaseInsensitiveOption).match(buffer).hasMatch()) {
  736. finded = true;
  737. }
  738. } else {
  739. if (buffer.contains(searchRequest, Qt::CaseInsensitive)) {
  740. finded = true;
  741. }
  742. }
  743. if (finded) {
  744. std::pair<QString, QString> rawMessage = splitUserNameAndMessage(buffer);
  745. if (rawMessage.first.isEmpty() or rawMessage.second.isEmpty()) {
  746. buffer = file.readLine();
  747. continue;
  748. }
  749. counter++;
  750. QString message = HTML_PAYLOAD_LIST_CHAT_MESSAGE;
  751. if (rawMessage.second == global::BLINDED_MESSAGE_MERKER) {
  752. message.replace("class=\"main_payload__chat\"",
  753. "class=\"main_payload__chat\" style=\"opacity: .5\"");
  754. }
  755. for (const auto &user: m_servers[originalServerName][originalChannelName]) {
  756. if (QRegularExpression("^(.*;|~|@|\\&|\\+)?"+rawMessage.first+"$").match(user).hasMatch()) {
  757. message.replace("class=\"main_payload__chat_username\"",
  758. "class=\"main_payload__chat_username\" style=\"color: green\"");
  759. break;
  760. }
  761. }
  762. replaceTag(message, "USERNAME", rawMessage.first);
  763. replaceTag(message, "MESSAGE_TEXT", rawMessage.second);
  764. matchedPathsAndMessages[path].push_back(message);
  765. }
  766. buffer = file.readLine();
  767. }
  768. file.close();
  769. }
  770. }
  771. else if (month.isEmpty() and not year.isEmpty()){
  772. for (const auto &p: paths) {
  773. QStringList slavePaths;
  774. QDirIterator it(p);
  775. while (it.hasNext()) {
  776. QString fileName = it.next();
  777. if (fileName.endsWith(".") or fileName.endsWith("..")) continue;
  778. if (not QRegularExpression("\\.txt$").match(fileName).hasMatch()) continue;
  779. slavePaths.push_back(fileName);
  780. }
  781. for (const auto &path: slavePaths) {
  782. if (not QRegularExpression("^.*[0-9]{2}\\.txt$").match(path).hasMatch()) continue;
  783. QFile file(path);
  784. if (not file.open(QIODevice::ReadOnly)) {
  785. consoleLog("Error! I can't open log file " + fsPath.path());
  786. continue;
  787. }
  788. QString buffer {file.readLine()};
  789. while (not buffer.isEmpty()) {
  790. removeBrakelineSymbols(buffer);
  791. bool finded = false;
  792. if (rgxIsValid) {
  793. if (QRegularExpression(searchRequest, QRegularExpression::CaseInsensitiveOption).match(buffer).hasMatch()) {
  794. finded = true;
  795. }
  796. } else {
  797. if (buffer.contains(searchRequest, Qt::CaseInsensitive)) {
  798. finded = true;
  799. }
  800. }
  801. if (finded) {
  802. std::pair<QString, QString> rawMessage = splitUserNameAndMessage(buffer);
  803. if (rawMessage.first.isEmpty() or rawMessage.second.isEmpty()) {
  804. buffer = file.readLine();
  805. continue;
  806. }
  807. counter++;
  808. QString message = HTML_PAYLOAD_LIST_CHAT_MESSAGE;
  809. if (rawMessage.second == global::BLINDED_MESSAGE_MERKER) {
  810. message.replace("class=\"main_payload__chat\"",
  811. "class=\"main_payload__chat\" style=\"opacity: .5\"");
  812. }
  813. for (const auto &user: m_servers[originalServerName][originalChannelName]) {
  814. if (QRegularExpression("^^(.*;|~|@|\\&|\\+)?"+rawMessage.first+"$").match(user).hasMatch()) {
  815. message.replace("class=\"main_payload__chat_username\"",
  816. "class=\"main_payload__chat_username\" style=\"color: green\"");
  817. break;
  818. }
  819. }
  820. replaceTag(message, "USERNAME", rawMessage.first);
  821. replaceTag(message, "MESSAGE_TEXT", rawMessage.second);
  822. matchedPathsAndMessages[path].push_back(message);
  823. }
  824. buffer = file.readLine();
  825. }
  826. file.close();
  827. }
  828. }
  829. }
  830. else { // root directory
  831. QStringList yearPaths;
  832. for (const auto& p: paths) {
  833. if (not QRegularExpression("/2[0-9]{3}$").match(p).hasMatch()) continue;
  834. yearPaths.push_back(p);
  835. }
  836. /* If you are reading this code, maybe you are crying now.
  837. * Sorry me, man (woman/fagot/etc). Maybe I'll refactor this hell in the future.
  838. * acetone.
  839. */
  840. QStringList fileNameS;
  841. for (const auto& p: yearPaths) {
  842. QStringList slavePaths;
  843. QDirIterator it(p);
  844. while (it.hasNext()) {
  845. QString folderName = it.next();
  846. if (folderName.endsWith(".") or folderName.endsWith("..")) continue;
  847. if (not QRegularExpression("/[0-9]{2}$").match(folderName).hasMatch()) continue;
  848. slavePaths.push_back(folderName);
  849. }
  850. for (const auto &path: slavePaths) {
  851. QDirIterator itMonth(path);
  852. while (itMonth.hasNext()) {
  853. QString fileName = itMonth.next();
  854. if (fileName.endsWith(".") or fileName.endsWith("..")) continue;
  855. if (not QRegularExpression("^.*[0-9]{2}\\.txt$").match(fileName).hasMatch()) continue;
  856. fileNameS.push_back(fileName);
  857. }
  858. }
  859. }
  860. for (const auto& path: fileNameS) {
  861. QFile file(path);
  862. if (not file.open(QIODevice::ReadOnly)) {
  863. consoleLog("Error! I can't open log file " + fsPath.path());
  864. continue;
  865. }
  866. QString buffer {file.readLine()};
  867. while (not buffer.isEmpty()) {
  868. removeBrakelineSymbols(buffer);
  869. bool finded = false;
  870. if (rgxIsValid) {
  871. if (QRegularExpression(searchRequest, QRegularExpression::CaseInsensitiveOption).match(buffer).hasMatch()) {
  872. finded = true;
  873. }
  874. } else {
  875. if (buffer.contains(searchRequest, Qt::CaseInsensitive)) {
  876. finded = true;
  877. }
  878. }
  879. if (finded) {
  880. std::pair<QString, QString> rawMessage = splitUserNameAndMessage(buffer);
  881. if (rawMessage.first.isEmpty() or rawMessage.second.isEmpty()) {
  882. buffer = file.readLine();
  883. continue;
  884. }
  885. counter++;
  886. QString message = HTML_PAYLOAD_LIST_CHAT_MESSAGE;
  887. if (rawMessage.second == global::BLINDED_MESSAGE_MERKER) {
  888. message.replace("class=\"main_payload__chat\"",
  889. "class=\"main_payload__chat\" style=\"opacity: .5\"");
  890. }
  891. for (const auto &user: m_servers[originalServerName][originalChannelName]) {
  892. if (QRegularExpression("^^(.*;|~|@|\\&|\\+)?"+rawMessage.first+"$").match(user).hasMatch()) {
  893. message.replace("class=\"main_payload__chat_username\"",
  894. "class=\"main_payload__chat_username\" style=\"color: green\"");
  895. break;
  896. }
  897. }
  898. replaceTag(message, "USERNAME", rawMessage.first);
  899. replaceTag(message, "MESSAGE_TEXT", rawMessage.second);
  900. matchedPathsAndMessages[path].push_back(message);
  901. }
  902. buffer = file.readLine();
  903. }
  904. file.close();
  905. }
  906. }
  907. if (matchedPathsAndMessages.empty()) {
  908. payloadBlock = HTML_PAYLOAD_ERROR;
  909. replaceTag(payloadBlock, "ERROR_TITLE", "Not found");
  910. replaceTag(payloadBlock, "ERROR_TEXT", "");
  911. }
  912. else {
  913. QStringList findedPaths;
  914. for (const auto& fp: matchedPathsAndMessages) {
  915. findedPaths << fp.first;
  916. }
  917. std::sort(findedPaths.begin(), findedPaths.end());
  918. for (auto& link: findedPaths) {
  919. QString logFolder {m_logFolder};
  920. logFolder.remove(QRegularExpression(".$"));
  921. QStringList& messages = matchedPathsAndMessages[link];
  922. link.remove(logFolder);
  923. link.remove(QRegularExpression("\\.txt$"));
  924. QString finded = HTML_PAYLOAD_LIST_POINT_MESSAGE;
  925. finded.replace("class=\"main_payload__block\"", "class=\"main_payload__block\" style=\"background: #b6c7d6\"");
  926. replaceTag(finded, "POINT_LINK", link);
  927. link.remove(QRegularExpression("^.*"+channel));
  928. replaceTag(finded, "POINT_CONTENT", link);
  929. payloadBlock += finded;
  930. for(const auto& m: messages) {
  931. payloadBlock += m;
  932. }
  933. payloadBlock += "&nbsp;\n";
  934. }
  935. }
  936. }
  937. middlePath += " " + searchRequest + " ";
  938. if (rgxIsValid) middlePath += "rgx";
  939. middlePath += "(" + QString::number(counter) + ")";
  940. }
  941. // Plain log explorer
  942. else {
  943. if (year.isEmpty()) { // /
  944. QStringList folderNameS;
  945. QDirIterator it(fsPath.path());
  946. while (it.hasNext()) {
  947. QString folderName = it.next();
  948. if (folderName.endsWith(".") or folderName.endsWith("..")) continue;
  949. while(folderName.contains('/')) {
  950. folderName.remove(QRegularExpression("^.*/"));
  951. }
  952. folderName.remove(QRegularExpression("\\.txt$"));
  953. folderNameS << folderName;
  954. }
  955. if (not folderNameS.isEmpty()) {
  956. std::sort(folderNameS.begin(), folderNameS.end());
  957. for (const auto &f: folderNameS) {
  958. QString onePoint = HTML_PAYLOAD_LIST_POINT_FOLDER;
  959. replaceTag(onePoint, "POINT_CONTENT", f);
  960. replaceTag(onePoint, "POINT_LINK", "/"+server+"/"+channel+"/"+f);
  961. payloadBlock += onePoint;
  962. }
  963. }
  964. }
  965. else if (not year.isEmpty() and month.isEmpty()) { // /YYYY
  966. QStringList folderNameS;
  967. QDirIterator it(fsPath.path());
  968. while (it.hasNext()) {
  969. QString folderName = it.next();
  970. if (folderName.endsWith(".") or folderName.endsWith("..")) continue;
  971. while(folderName.contains('/')) {
  972. folderName.remove(QRegularExpression("^.*/"));
  973. }
  974. folderNameS << folderName;
  975. }
  976. if (not folderNameS.isEmpty()) {
  977. std::sort(folderNameS.begin(), folderNameS.end());
  978. for (const auto &f: folderNameS) {
  979. QString onePoint = HTML_PAYLOAD_LIST_POINT_FOLDER;
  980. bool yearIsOk {false};
  981. bool monthIsOk {false};
  982. QDate dateOfMonth (year.toInt(&yearIsOk), f.toInt(&monthIsOk), 1);
  983. QString nameOfMonth;
  984. if (yearIsOk and monthIsOk) {
  985. nameOfMonth = QLocale().standaloneMonthName(dateOfMonth.month(), QLocale::ShortFormat);
  986. }
  987. QDirIterator dayLogs(fsPath.path() + global::slash + f);
  988. int8_t dayLogsCounter {0};
  989. while (dayLogs.hasNext()) {
  990. QString dayLogFile = dayLogs.next();
  991. if (dayLogFile.endsWith(".") or dayLogFile.endsWith("..")) continue;
  992. dayLogsCounter++;
  993. }
  994. QString filesLabel;
  995. dayLogsCounter == 1 ? filesLabel = "day" : filesLabel = "days";
  996. replaceTag(onePoint, "POINT_CONTENT", "<span style=\"font-weight: bold\">" + f + "</span>&nbsp;(" + nameOfMonth + ") " + QString::number(dayLogsCounter) + " " + filesLabel);
  997. replaceTag(onePoint, "POINT_LINK", "/"+server+"/"+channel+"/"+year+"/"+f);
  998. payloadBlock += onePoint;
  999. }
  1000. }
  1001. }
  1002. else if (not month.isEmpty() and day.isEmpty()) { // /YYYY/MM
  1003. QStringList fileNameS;
  1004. QDirIterator it(fsPath.path());
  1005. while (it.hasNext()) {
  1006. QString fileName = it.next();
  1007. if (fileName.endsWith(".") or fileName.endsWith("..")) continue; // QDir::NoDotAndNoDotDot not works!
  1008. while(fileName.contains('/')) {
  1009. fileName.remove(QRegularExpression("^.*/"));
  1010. }
  1011. fileName.remove(QRegularExpression("\\.txt$"));
  1012. fileNameS << fileName;
  1013. }
  1014. if (not fileNameS.isEmpty()) {
  1015. std::sort(fileNameS.begin(), fileNameS.end());
  1016. for (const auto &a: fileNameS) {
  1017. QString onePoint = HTML_PAYLOAD_LIST_POINT_MESSAGE;
  1018. bool yearIsOk {false};
  1019. bool monthIsOk {false};
  1020. bool dayIsOk {false};
  1021. QDate dateOfDay (year.toInt(&yearIsOk), month.toInt(&monthIsOk), a.toInt(&dayIsOk));
  1022. QString nameOfDay;
  1023. if (yearIsOk and monthIsOk and dayIsOk) {
  1024. nameOfDay = QLocale().standaloneDayName(dateOfDay.dayOfWeek(), QLocale::ShortFormat);
  1025. }
  1026. auto logFileSizeBytes = QFile(fsPath.path() + global::slash + a +".txt").size();
  1027. auto logFileSizeinKb = logFileSizeBytes/1000;
  1028. QString logFileSizeString;
  1029. if (logFileSizeinKb != 0) {
  1030. logFileSizeString = QString::number(logFileSizeinKb)+"."+
  1031. QString::number((logFileSizeBytes - logFileSizeinKb*1000)/10)+
  1032. " KB";
  1033. } else {
  1034. logFileSizeString = QString::number(logFileSizeBytes)+" B";
  1035. }
  1036. replaceTag(onePoint, "POINT_CONTENT", "<span style=\"font-weight: bold\">" + a + "</span>&nbsp;(" + nameOfDay + ") " + logFileSizeString);
  1037. replaceTag(onePoint, "POINT_LINK", "/"+server+"/"+channel+"/"+year+"/"+month+"/"+a);
  1038. payloadBlock += onePoint;
  1039. }
  1040. }
  1041. }
  1042. else if (not day.isEmpty()) { // /YYYY/MM/dd
  1043. QFile file(fsPath.path()+global::slash+day+".txt");
  1044. if (not file.open(QIODevice::ReadOnly)) {
  1045. consoleLog("Error! I can't open log file " + fsPath.path());
  1046. payloadBlock = HTML_PAYLOAD_ERROR;
  1047. replaceTag(payloadBlock, "ERROR_TITLE", "Internal error");
  1048. replaceTag(payloadBlock, "ERROR_TEXT", "Requested log file openning failed");
  1049. }
  1050. else {
  1051. QString buffer = file.readLine();
  1052. while (not buffer.isEmpty()) {
  1053. removeBrakelineSymbols(buffer);
  1054. std::pair<QString, QString> rawMessage = splitUserNameAndMessage(buffer);
  1055. if (rawMessage.first.isEmpty() or rawMessage.second.isEmpty()) {
  1056. buffer = file.readLine();
  1057. continue;
  1058. }
  1059. QString message = HTML_PAYLOAD_LIST_CHAT_MESSAGE;
  1060. if (rawMessage.second == global::BLINDED_MESSAGE_MERKER) {
  1061. message.replace("class=\"main_payload__chat\"",
  1062. "class=\"main_payload__chat\" style=\"opacity: .5\"");
  1063. }
  1064. for (const auto &user: m_servers[originalServerName][originalChannelName]) {
  1065. if (QRegularExpression("^(.*;|~|@|\\&|\\+)?"+rawMessage.first+"$").match(user).hasMatch()) {
  1066. message.replace("class=\"main_payload__chat_username\"",
  1067. "class=\"main_payload__chat_username\" style=\"color: green\"");
  1068. break;
  1069. }
  1070. }
  1071. replaceTag(message, "USERNAME", rawMessage.first);
  1072. replaceTag(message, "MESSAGE_TEXT", rawMessage.second);
  1073. payloadBlock += message;
  1074. buffer = file.readLine();
  1075. }
  1076. file.close();
  1077. }
  1078. }
  1079. if (payloadBlock.isEmpty()) {
  1080. payloadBlock = HTML_PAYLOAD_ERROR;
  1081. replaceTag(payloadBlock, "ERROR_TITLE", "Empty");
  1082. replaceTag(payloadBlock, "ERROR_TEXT", "No logs found for this channel");
  1083. }
  1084. }
  1085. replaceTag(page, "MIDDLE_PATH", middlePath);
  1086. replaceTag(page, "PAYLOAD_BLOCK", payloadBlock);
  1087. //// Footer
  1088. replaceTag(page, "VERSION", IRCABOT_VERSION);
  1089. replaceTag(page, "COPYRIGHT_YEAR", COPYRIGHT_YEAR);
  1090. replaceTag(page, "RENDERING_TIMER", QString::number(QDateTime::currentMSecsSinceEpoch() - renderStart));
  1091. QString mainHeader = HEADER_HTML;
  1092. replaceTag(mainHeader, "SIZE", QString::number(QByteArray(page.toUtf8()).size()));
  1093. if (socket->isOpen()) {
  1094. socket->write(mainHeader.toUtf8());
  1095. if (not isHeadRequest) socket->write(page.toUtf8());
  1096. }
  1097. }
  1098. void HttpServer::writeRealTimeChatPage(QTcpSocket *socket, QString &urlPath, bool isHeadRequest)
  1099. {
  1100. if (isHeadRequest) {
  1101. QString header = HEADER_HTML;
  1102. replaceTag(header, "SIZE", "0");
  1103. socket->write(header.toUtf8());
  1104. return;
  1105. }
  1106. auto renderStart = QDateTime::currentMSecsSinceEpoch();
  1107. urlPath.remove(QRegularExpression("^/realtimereadingchat/"));
  1108. QString server = getWordFromPath(urlPath);
  1109. if (server.isEmpty()) {
  1110. writeErrorPage(socket, "SERVER VALUE IS MISSING");
  1111. return;
  1112. }
  1113. urlPath.remove(QRegularExpression("^"+server));
  1114. QString channel = getWordFromPath(urlPath);
  1115. if (channel.isEmpty()) {
  1116. writeErrorPage(socket, "CHANNEL VALUE IS MISSING");
  1117. return;
  1118. }
  1119. QString originalServerName;
  1120. for (const auto &s: m_servers) {
  1121. if (global::toLowerAndNoSpaces(s.first) == server) {
  1122. originalServerName = s.first;
  1123. }
  1124. }
  1125. if (originalServerName.isEmpty()) {
  1126. writeErrorPage(socket, "REQUESTED SERVER NOT EXIST");
  1127. return;
  1128. }
  1129. QString originalChannelName;
  1130. for (const auto &server: m_servers) {
  1131. for (const auto &channel_users: server.second) {
  1132. if (global::toLowerAndNoSpaces(channel_users.first) == "#"+channel) {
  1133. originalChannelName = global::toLowerAndNoSpaces(channel_users.first);
  1134. }
  1135. }
  1136. }
  1137. if (originalChannelName.isEmpty()) {
  1138. writeErrorPage(socket, "REQUESTED CHANNEL NOT EXIST");
  1139. return;
  1140. }
  1141. QFile main("://html/main.html");
  1142. QString page;
  1143. if (main.open(QIODevice::ReadOnly)) {
  1144. page = main.readAll();
  1145. main.close();
  1146. }
  1147. else {
  1148. if (socket->isOpen()) {
  1149. socket->write(HEADER_404.toUtf8());
  1150. if (not isHeadRequest) socket->write(CRITICAL_ERROR);
  1151. }
  1152. }
  1153. QString year {QDateTime::currentDateTime().toString("yyyy")};
  1154. QString month {QDateTime::currentDateTime().toString("MM")};
  1155. QString day {QDateTime::currentDateTime().toString("dd")};
  1156. //// Left menu compilation
  1157. QString htmlServersSectionS;
  1158. for (const auto &s: m_servers) {
  1159. if (s.first.isEmpty()) continue; // empty server name?
  1160. QString htmlServersSection = HTML_SERVER_SECTION;
  1161. replaceTag(htmlServersSection, "ABOUT_SERVER", "/serverinformation/"+server);
  1162. replaceTag(htmlServersSection, "SERVER_NAME", s.first);
  1163. if (s.first == originalServerName) {
  1164. htmlServersSection.replace("<span style=\"font-size: 17px;\">{{ONLINE_STATUS}}",
  1165. "<span style=\"font-size: 17px;\" id=\"serverStatus\">{{ONLINE_STATUS}}");
  1166. }
  1167. QString htmlChannelLineS;
  1168. for (const auto &c: s.second) {
  1169. QString htmlChannelLine;
  1170. if (originalServerName == s.first and originalChannelName == c.first) {
  1171. htmlChannelLine = HTML_SERVER_SECTION_CHANNEL_SELECTED;
  1172. } else {
  1173. htmlChannelLine = HTML_SERVER_SECTION_CHANNEL;
  1174. }
  1175. replaceTag(htmlChannelLine, "CHANNEL_NAME", c.first);
  1176. QString channelNameForUrl {c.first};
  1177. channelNameForUrl.remove('#');
  1178. QString channelLink = "/realtimereadingchat/" + global::toLowerAndNoSpaces(s.first) + "/" + channelNameForUrl;
  1179. replaceTag(htmlChannelLine, "CHANNEL_LINK", channelLink);
  1180. htmlChannelLineS += htmlChannelLine;
  1181. }
  1182. replaceTag(htmlServersSection, "CHANNELS", htmlChannelLineS);
  1183. bool online {false};
  1184. for (const auto &srv: m_serversOnline) {
  1185. if (srv.first == s.first) {
  1186. online = srv.second;
  1187. break;
  1188. }
  1189. }
  1190. if (online) {
  1191. replaceTag(htmlServersSection, "ONLINE_STATUS", HTML_SERVER_ONLINE_MARKER);
  1192. } else {
  1193. replaceTag(htmlServersSection, "ONLINE_STATUS", HTML_SERVER_OFFLINE_MARKER);
  1194. }
  1195. if (s.first == originalServerName) {
  1196. htmlServersSectionS.push_front(htmlServersSection);
  1197. } else {
  1198. htmlServersSectionS += htmlServersSection;
  1199. }
  1200. }
  1201. replaceTag(page, "SERVERS_SECTION", htmlServersSectionS);
  1202. //// Main section header compilation
  1203. QString& topic = m_channelsTopic[originalServerName][originalChannelName];
  1204. topic = topic.replace('\"', "&quot;");
  1205. QString titlePostfix = " | IRCaBot";
  1206. if (not topic.isEmpty()) {
  1207. titlePostfix.push_front(" | " + topic);
  1208. }
  1209. if (m_servers.size() > 1) {
  1210. replaceTag(page, "PAGE_TITLE", originalChannelName + " ("+originalServerName+") [real time]" + titlePostfix);
  1211. } else {
  1212. replaceTag(page, "PAGE_TITLE", originalChannelName + " [real time]" + titlePostfix);
  1213. }
  1214. replaceTag(page, "CHANNEL_TOPIC", topic);
  1215. replaceTag(page, "MAIN_HEADER", originalChannelName);
  1216. replaceTag(page, "ADDITIONAL_BUTTON", HTML_PAYLOAD_ADDITIONAL_NOTIFY);
  1217. replaceTag(page, "REALTIME_LINK", "/"+server+"/"+channel+"/"+year+"/"+month+"/"+day);
  1218. replaceTag(page, "AIRPLAIN_TITLE", "Back to plain text log");
  1219. page.replace("class=\"main_header__title_airplaine\"", "class=\"main_header__title_airplaine\" style=\"transform: scale(-1,1)\"");
  1220. int currentOnline = 0;
  1221. QString onlineUserS;
  1222. for (const auto &user: m_servers[originalServerName][originalChannelName]) {
  1223. if (QRegularExpression("^(.*;|~|@|\\&|\\+)?"+m_botNick[originalServerName]+"$").match(user).hasMatch()) {
  1224. continue;
  1225. }
  1226. QString onlineUser = HTML_ONLINE_POINT;
  1227. replaceTag(onlineUser, "NICKNAME", user);
  1228. onlineUserS += onlineUser;
  1229. currentOnline++;
  1230. }
  1231. page.replace("{{ONLINE}}", "<span id=\"online\">{{ONLINE}}</span>");
  1232. replaceTag(page, "ONLINE", QString::number(currentOnline));
  1233. page.replace("<div class=\"main_middle__online_list\">", "<div id=\"onlineList\" class=\"main_middle__online_list\">");
  1234. replaceTag(page, "ONLINE_LIST", onlineUserS);
  1235. page.replace("class=\"main_header__search_form\"", "action=\"/"+server+"/"+channel+"\" class=\"main_header__search_form\"");
  1236. replaceTag(page, "SEARCH_PLACEHOLDER", originalChannelName);
  1237. page.replace("<div class=\"main_middle__path\">", "<div class=\"main_middle__path\" id=\"path\">");
  1238. replaceTag(page, "MIDDLE_PATH", "");
  1239. //// Payload
  1240. page.replace("<div class=\"main_payload\">", "<div class=\"main_payload\" id=\"payload\">");
  1241. bool logsExisted {false};
  1242. QFile file;
  1243. QDir fsPath(m_logFolder+server+global::slash+channel);
  1244. if (fsPath.cd(year)) {
  1245. if (fsPath.cd(month)) {
  1246. file.setFileName(fsPath.path()+global::slash+day+".txt");
  1247. if (file.open(QIODevice::ReadOnly)) {
  1248. logsExisted = true;
  1249. }
  1250. }
  1251. }
  1252. QString payloadBlock;
  1253. if (logsExisted) {
  1254. QString buffer = file.readLine();
  1255. while (not buffer.isEmpty()) {
  1256. removeBrakelineSymbols(buffer);
  1257. std::pair<QString, QString> rawMessage = splitUserNameAndMessage(buffer);
  1258. if (rawMessage.first.isEmpty() or rawMessage.second.isEmpty()) {
  1259. buffer = file.readLine();
  1260. continue;
  1261. }
  1262. QString message = HTML_PAYLOAD_LIST_CHAT_MESSAGE;
  1263. if (rawMessage.second == global::BLINDED_MESSAGE_MERKER) {
  1264. message.replace("class=\"main_payload__chat\"",
  1265. "class=\"main_payload__chat\" style=\"opacity: .5\"");
  1266. }
  1267. message.replace("class=\"main_payload__chat_username\"",
  1268. "class=\"main_payload__chat_username\" style=\"color: #e34f10\"");
  1269. replaceTag(message, "USERNAME", rawMessage.first);
  1270. replaceTag(message, "MESSAGE_TEXT", rawMessage.second);
  1271. payloadBlock += message;
  1272. buffer = file.readLine();
  1273. }
  1274. file.close();
  1275. }
  1276. QString hr = HTML_PAYLOAD_LIST_CHAT_MESSAGE;
  1277. hr.replace("class=\"main_payload__chat_mail\"", "id=\"hr\" class=\"main_payload__chat_mail\"");
  1278. hr.replace("class=\"main_payload__chat_username\"",
  1279. "class=\"main_payload__chat_username\" style=\"color: white; text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;\"");
  1280. replaceTag(hr, "USERNAME", "IRCaBot");
  1281. replaceTag(hr, "MESSAGE_TEXT", "<b>New messages won't show without JavaScript.</b><br>"
  1282. "My JS code is small and simple. Check it at "
  1283. "<a href=\"/realtimechat.js\">/realtimechat.js</a> and come back "
  1284. "with enabled!");
  1285. payloadBlock += hr;
  1286. replaceTag(page, "PAYLOAD_BLOCK", payloadBlock);
  1287. //// Footer
  1288. replaceTag(page, "VERSION", IRCABOT_VERSION);
  1289. replaceTag(page, "COPYRIGHT_YEAR", COPYRIGHT_YEAR);
  1290. //// Finish
  1291. page.replace("</body>", " <div id=\"LMId\" style=\"display: none\">" + QString::number(QDateTime::currentMSecsSinceEpoch()) + "</div>\n"
  1292. " <div id=\"ajaxPath\" style=\"display: none\">" + server + "/" + channel + "</div>\n"
  1293. " <script src=\"/realtimechat.js\"></script>\n</body>");
  1294. replaceTag(page, "RENDERING_TIMER", QString::number(QDateTime::currentMSecsSinceEpoch() - renderStart));
  1295. QString mainHeader = HEADER_HTML;
  1296. replaceTag(mainHeader, "SIZE", QString::number(QByteArray(page.toUtf8()).size()));
  1297. if (socket->isOpen()) {
  1298. socket->write(mainHeader.toUtf8());
  1299. if (not isHeadRequest) socket->write(page.toUtf8());
  1300. }
  1301. }
  1302. void HttpServer::writeAboutServerPage(QTcpSocket *socket, QString &urlPath, bool isHeadRequest)
  1303. {
  1304. auto renderStart = QDateTime::currentMSecsSinceEpoch();
  1305. urlPath.remove(QRegularExpression("^/serverinformation/"));
  1306. QString server = getWordFromPath(urlPath);
  1307. if (server.isEmpty()) {
  1308. writeErrorPage(socket, "SERVER VALUE IS MISSING");
  1309. return;
  1310. }
  1311. QString originalServerName;
  1312. for (const auto &s: m_servers) {
  1313. if (global::toLowerAndNoSpaces(s.first) == server) {
  1314. originalServerName = s.first;
  1315. }
  1316. }
  1317. if (originalServerName.isEmpty()) {
  1318. writeErrorPage(socket, "SERVER NOT EXIST");
  1319. return;
  1320. }
  1321. QFile main("://html/main.html");
  1322. QString page;
  1323. if (main.open(QIODevice::ReadOnly)) {
  1324. page = main.readAll();
  1325. main.close();
  1326. }
  1327. else {
  1328. if (socket->isOpen()) {
  1329. socket->write(HEADER_404.toUtf8());
  1330. if (not isHeadRequest) socket->write(CRITICAL_ERROR);
  1331. }
  1332. }
  1333. replaceTag(page, "PAGE_TITLE", "About " + originalServerName + " | IRCaBot");
  1334. //// Left menu compilation
  1335. QString htmlServersSectionS;
  1336. for (const auto &s: m_servers) {
  1337. if (s.first.isEmpty()) continue; // empty server name?
  1338. QString htmlServersSection = HTML_SERVER_SECTION;
  1339. if (s.first == originalServerName) {
  1340. htmlServersSection.replace("<div class=\"left_menu__item\">",
  1341. "<div class=\"left_menu__item\" style=\"background: #f0f5fa\">");
  1342. }
  1343. replaceTag(htmlServersSection, "ABOUT_SERVER", "/serverinformation/"+s.first);
  1344. replaceTag(htmlServersSection, "SERVER_NAME", s.first);
  1345. QString htmlChannelLineS;
  1346. for (const auto &c: s.second) {
  1347. QString htmlChannelLine;
  1348. htmlChannelLine = HTML_SERVER_SECTION_CHANNEL;
  1349. replaceTag(htmlChannelLine, "CHANNEL_NAME", c.first);
  1350. QString channelNameForUrl {c.first};
  1351. channelNameForUrl.remove('#');
  1352. QString channelLink = "/" + global::toLowerAndNoSpaces(s.first) + "/" + channelNameForUrl;
  1353. replaceTag(htmlChannelLine, "CHANNEL_LINK", channelLink);
  1354. htmlChannelLineS += htmlChannelLine;
  1355. }
  1356. replaceTag(htmlServersSection, "CHANNELS", htmlChannelLineS);
  1357. bool online {false};
  1358. for (const auto &srv: m_serversOnline) {
  1359. if (srv.first == s.first) {
  1360. online = srv.second;
  1361. break;
  1362. }
  1363. }
  1364. if (online) {
  1365. replaceTag(htmlServersSection, "ONLINE_STATUS", HTML_SERVER_ONLINE_MARKER);
  1366. } else {
  1367. replaceTag(htmlServersSection, "ONLINE_STATUS", HTML_SERVER_OFFLINE_MARKER);
  1368. }
  1369. if (s.first == originalServerName) {
  1370. htmlServersSectionS.push_front(htmlServersSection);
  1371. } else {
  1372. htmlServersSectionS += htmlServersSection;
  1373. }
  1374. }
  1375. replaceTag(page, "SERVERS_SECTION", htmlServersSectionS);
  1376. page.remove(QRegularExpression("<div class=\"main_header\">.*<!-- main_middle -->", QRegularExpression::DotMatchesEverythingOption));
  1377. QString payloadBlock = HTML_PAYLOAD_ABOUT;
  1378. replaceTag(payloadBlock, "ABOUT_TITLE", originalServerName);
  1379. QString aboutBlock;
  1380. QFile about(m_logFolder+server+global::slash+"about_server.txt");
  1381. if (about.open(QIODevice::ReadOnly)) {
  1382. QString rbuffer = about.readLine();
  1383. while (not rbuffer.isEmpty()) {
  1384. if (rbuffer.startsWith('#')) {
  1385. rbuffer = about.readLine();
  1386. continue;
  1387. }
  1388. removeBrakelineSymbols(rbuffer);
  1389. if (not rbuffer.isEmpty()) {
  1390. aboutBlock += rbuffer;
  1391. }
  1392. rbuffer = about.readLine();
  1393. }
  1394. }
  1395. else {
  1396. aboutBlock = "No information provided";
  1397. }
  1398. replaceTag(payloadBlock, "ABOUT_TEXT", aboutBlock);
  1399. replaceTag(page, "PAYLOAD_BLOCK", payloadBlock);
  1400. //// Footer
  1401. replaceTag(page, "VERSION", IRCABOT_VERSION);
  1402. replaceTag(page, "COPYRIGHT_YEAR", COPYRIGHT_YEAR);
  1403. //// Finish
  1404. replaceTag(page, "RENDERING_TIMER", QString::number(QDateTime::currentMSecsSinceEpoch() - renderStart));
  1405. QString mainHeader = HEADER_HTML;
  1406. replaceTag(mainHeader, "SIZE", QString::number(QByteArray(page.toUtf8()).size()));
  1407. if (socket->isOpen()) {
  1408. socket->write(mainHeader.toUtf8());
  1409. if (not isHeadRequest) socket->write(page.toUtf8());
  1410. }
  1411. }
  1412. void HttpServer::writeAjaxAnswer(QTcpSocket *socket, QString &urlPath, bool isHeadRequest)
  1413. {
  1414. if (isHeadRequest) {
  1415. QString header = HEADER_JSON;
  1416. replaceTag(header, "SIZE", "0");
  1417. socket->write(header.toUtf8());
  1418. return;
  1419. }
  1420. //// Validate
  1421. urlPath.remove(QRegularExpression("^/ajax/"));
  1422. QString server = getWordFromPath(urlPath);
  1423. if (server.isEmpty()) {
  1424. writeErrorJson(socket, "Invalid request. Server value is missing!");
  1425. return;
  1426. }
  1427. urlPath.remove(QRegularExpression("^"+server));
  1428. QString channel = getWordFromPath(urlPath);
  1429. if (channel.isEmpty()) {
  1430. writeErrorJson(socket, "Invalid request. Channel value is missing!");
  1431. return;
  1432. }
  1433. QString originalServerName;
  1434. for (const auto &s: m_servers) {
  1435. if (global::toLowerAndNoSpaces(s.first) == server) {
  1436. originalServerName = s.first;
  1437. }
  1438. }
  1439. if (originalServerName.isEmpty()) {
  1440. writeErrorJson(socket, "Invalid request. Server not exist!");
  1441. return;
  1442. }
  1443. QString originalChannelName;
  1444. for (const auto &server: m_servers) {
  1445. for (const auto &channel_users: server.second) {
  1446. if (global::toLowerAndNoSpaces(channel_users.first) == "#"+channel) {
  1447. originalChannelName = global::toLowerAndNoSpaces(channel_users.first);
  1448. }
  1449. }
  1450. }
  1451. if (originalChannelName.isEmpty()) {
  1452. writeErrorJson(socket, "Invalid request. Channel not exist!");
  1453. return;
  1454. }
  1455. //// Parse
  1456. bool userOnlineCounterIsOk {false};
  1457. auto userOnlineCounter = global::getValue(urlPath, "onlineCounter", global::eForWeb).toInt(&userOnlineCounterIsOk);
  1458. if (not userOnlineCounterIsOk) {
  1459. writeErrorJson(socket, "Invalid request: 'onlineCounter' (int) value is missing!");
  1460. return;
  1461. }
  1462. bool userMessageLastIdIsOk {false};
  1463. qint64 userMessageLastId = global::getValue(urlPath, "messageId", global::eForWeb).toLongLong(&userMessageLastIdIsOk);
  1464. if (not userMessageLastIdIsOk) {
  1465. writeErrorJson(socket, "Invalid request: 'messageId' value is missing!");
  1466. return;
  1467. }
  1468. bool userServerStatus = global::getValue(urlPath, "serverStatus", global::eForWeb) == "true";
  1469. //// Building
  1470. QJsonObject jResponse;
  1471. jResponse.insert("status", QJsonValue(true));
  1472. // online server
  1473. if (userServerStatus != m_serversOnline[originalServerName]) {
  1474. jResponse.insert("serverStatusChanged", QJsonValue(true));
  1475. jResponse.insert("serverStatus", QJsonValue(m_serversOnline[originalServerName]));
  1476. } else {
  1477. jResponse.insert("serverStatusChanged", QJsonValue(false));
  1478. }
  1479. // online users
  1480. if (m_servers[originalServerName][originalChannelName].size()-1/*self*/ != userOnlineCounter) {
  1481. jResponse.insert("onlineUsersChanged", QJsonValue(true));
  1482. QJsonObject jOnline;
  1483. int currentOnline = m_servers[originalServerName][originalChannelName].size();
  1484. if (currentOnline > 0) currentOnline -= 1;
  1485. jOnline.insert("count", QJsonValue(currentOnline));
  1486. QJsonArray jOnlineList;
  1487. for (const auto& user: m_servers[originalServerName][originalChannelName]) {
  1488. if (QRegularExpression("^(.*;|~|@|\\&|\\+)?"+m_botNick[originalServerName]+"$").match(user).hasMatch()) {
  1489. continue;
  1490. }
  1491. jOnlineList.push_back(QJsonValue(user));
  1492. }
  1493. jOnline.insert("list", jOnlineList);
  1494. jResponse.insert("online", jOnline);
  1495. } else {
  1496. jResponse.insert("onlineUsersChanged", QJsonValue(false));
  1497. }
  1498. // new messages
  1499. bool newMessagesIsExisted {false};
  1500. QString channelId {server+channel};
  1501. QJsonArray jNewMessages;
  1502. qint64 newLastMessageId {userMessageLastId};
  1503. if (not m_messageCache.contains(channelId)) {
  1504. m_messageCache.insert(channelId, new MessagePool);
  1505. consoleLog("Message caching enabled for "+server+"/#"+channel+". Real time reading started.");
  1506. }
  1507. else {
  1508. auto messages = *(m_messageCache[channelId]->getMessages());
  1509. for (const auto& msg: messages) {
  1510. if (userMessageLastId < msg.first) {
  1511. if (not newMessagesIsExisted) newMessagesIsExisted = true;
  1512. QJsonObject jOneNewMessage;
  1513. jOneNewMessage.insert("user", msg.second->getSender());
  1514. jOneNewMessage.insert("text", msg.second->getText());
  1515. jNewMessages.push_back(jOneNewMessage);
  1516. newLastMessageId = msg.first;
  1517. }
  1518. }
  1519. }
  1520. if (newMessagesIsExisted) {
  1521. jResponse.insert("LMIdChanged" /* last message id == LMId */, QJsonValue(true));
  1522. jResponse.insert("newMessages", jNewMessages);
  1523. jResponse.insert("LMId", QJsonValue(QString::number(newLastMessageId)));
  1524. } else {
  1525. jResponse.insert("LMIdChanged", QJsonValue(false));
  1526. }
  1527. //// Finish
  1528. QByteArray response {QJsonDocument(jResponse).toJson()};
  1529. QString header = HEADER_JSON;
  1530. replaceTag(header, "SIZE", QString::number(response.size()));
  1531. socket->write(header.toUtf8());
  1532. socket->write(response);
  1533. }
  1534. /*\
  1535. |*| //////////'''''''''''''\\\\\\\\\\
  1536. |*| HAPPY NEW YEAR IN RUSSIAN PRISON!
  1537. |*| |||||||||| 2022 ||||||||||
  1538. |*| \\\\\\\\\\_____________//////////
  1539. \*/
  1540. Message::Message(const QString& s, const QString& t, qint64 timestamp, QObject *parent) :
  1541. QObject(parent),
  1542. m_sender(s),
  1543. m_text(t),
  1544. m_timestamp(timestamp)
  1545. {
  1546. connect (&m_selfKiller, &QTimer::timeout, [&](){emit outDated(m_timestamp);});
  1547. m_selfKiller.setSingleShot(true);
  1548. m_selfKiller.start(MSECS_TO_AUTOREMOVE_MESSAGES_FROM_BUFFER);
  1549. }
  1550. const QString Message::getSender()
  1551. {
  1552. return m_sender;
  1553. }
  1554. const QString Message::getText()
  1555. {
  1556. return m_text;
  1557. }
  1558. MessagePool::MessagePool(QObject *parent) :
  1559. QObject(parent),
  1560. m_lastPing(QDateTime::currentMSecsSinceEpoch())
  1561. {}
  1562. void MessagePool::saveNewMessage(const QString &nick, const QString &text)
  1563. {
  1564. qint64 timestamp = QDateTime::currentMSecsSinceEpoch();
  1565. Message* newMessage = new Message (nick, text, timestamp);
  1566. connect (newMessage, SIGNAL(outDated(qint64)), this, SLOT (messageToDelete(qint64)));
  1567. m_messages.insert({timestamp, newMessage});
  1568. }
  1569. const std::multimap<qint64, Message *>* MessagePool::getMessages()
  1570. {
  1571. m_lastPing = QDateTime::currentMSecsSinceEpoch();
  1572. return &m_messages;
  1573. }
  1574. qint64 MessagePool::getLastPing()
  1575. {
  1576. return m_lastPing;
  1577. }
  1578. void MessagePool::messageToDelete(qint64 timestamp)
  1579. {
  1580. while (m_messages.find(timestamp) != m_messages.end()) {
  1581. auto it = m_messages.find(timestamp);
  1582. it->second->deleteLater();
  1583. m_messages.erase(it);
  1584. }
  1585. }