From e4ccb9c3e8baae8632d52ec7158f64eabc2b4d76 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Tue, 10 Nov 2015 21:48:29 +0100 Subject: Add logging --- src/crsm.cpp | 146 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 112 insertions(+), 34 deletions(-) (limited to 'src/crsm.cpp') diff --git a/src/crsm.cpp b/src/crsm.cpp index a4ccbfa..92ee2e4 100644 --- a/src/crsm.cpp +++ b/src/crsm.cpp @@ -15,8 +15,6 @@ CRSM::CRSM(QObject *parent) : { qsrand(QDateTime::currentMSecsSinceEpoch()); codec = QTextCodec::codecForName("Windows-1252"); - qout = new QTextStream(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered); - qout->setCodec(QTextCodec::codecForName("UTF-8")); outputBuffer.setFileName(MGMT_BUFFER_FILENAME); outputBuffer.open(QFile::WriteOnly | QFile::Unbuffered); @@ -75,10 +73,6 @@ CRSM::CRSM(QObject *parent) : afkAdminTimer.setSingleShot(true); connect(&afkAdminTimer, SIGNAL(timeout()), this, SLOT(afkAdminTimeout())); - QFile *reallog = new QFile(Config.Auto.Volatile.Clonk.Directory + "CRSM.log"); - reallog->open(QIODevice::Append | QIODevice::Text); - logstream.setDevice(reallog); - ok = true; } @@ -117,24 +111,24 @@ void CRSM::readServerOutput() if(what.isEmpty()) return; - logstream << what.trimmed() << endl; - if(Config.IRC.Use) { foreach(const QString &mess, what.split("\n", QString::SkipEmptyParts)) foreach(const QString &mod, ircModIOList) { - connection->sendCommand(IrcCommand::createNotice(mod, mess)); + sendIrcMessage(mess, mod, false, true); } } out(what.trimmed() + "\n"); if(!timeRemover.exactMatch(what)) return; what = timeRemover.cap(2).trimmed(); + Log.clonkLog(what); static QRegExp userexp("^\\s*(\\*?)\\s*<(.*)\\|(\\d+)\\|([^>]*)>\\s+(.*)$"); if(userexp.exactMatch(what)) { + Log.clonkChatLog(what); bool isMeMessage = (userexp.cap(1) == "*"); QString user = userexp.cap(4); QString pcName = userexp.cap(2); @@ -142,6 +136,7 @@ void CRSM::readServerOutput() if(pcName != Config.Auto.Volatile.Clonk.ServerPCName) { ClientInfo& info = getClientInfo(pcName, cuid, user); + Log.clonkUserLog(what, info, isMeMessage); if(info == Session.Clonk.Admin) { checkActivity(Session.Clonk.Admin); @@ -164,12 +159,12 @@ void CRSM::readServerOutput() } else if(Session.IRC.UseIngameChat) { - connection->sendCommand(IrcCommand::createMessage(Config.IRC.IngameChannel, "[Clonk]<" + user + "> " + msg)); + sendIrcMessage("[Clonk]<" + user + "> " + msg, Config.IRC.IngameChannel, false, false); } } else if(Session.IRC.UseIngameChat) { - connection->sendCommand(IrcCommand::createCtcpAction(Config.IRC.IngameChannel, "[Clonk] " + user + " " + msg)); + sendIrcMessage("[Clonk] " + user + " " + msg, Config.IRC.IngameChannel, true, false); } } } @@ -188,7 +183,7 @@ void CRSM::readServerOutput() timer->start(1000); if(Session.IRC.UseIngameChat) { - connection->sendCommand(IrcCommand::createMessage(Config.IRC.IngameChannel, "[Clonk] " + info.toString() + " verbunden.")); + sendIrcMessage("[Clonk] " + info.toString() + " verbunden.", Config.IRC.IngameChannel, false, false); } } } @@ -246,7 +241,7 @@ void CRSM::readServerOutput() if(Session.IRC.UseIngameChat) { - connection->sendCommand(IrcCommand::createMessage(Config.IRC.IngameChannel, "[Clonk] " + info.toString() + " entfernt" + leaveExp.cap(2))); + sendIrcMessage("[Clonk] " + info.toString() + " entfernt" + leaveExp.cap(2), Config.IRC.IngameChannel, false, false); } if(info == Session.Clonk.Admin) @@ -300,10 +295,10 @@ void CRSM::readServerOutput() out(gameRegisterFailMessage.arg(reason)); if(Config.IRC.Use) { - connection->sendCommand(IrcCommand::createMessage(Config.IRC.Channel, gameRegisterFailMessage.arg(reason))); + sendIrcMessage(gameRegisterFailMessage.arg(reason), Config.IRC.Channel, false, false); if(Config.IRC.UseIngameChat) { - connection->sendCommand(IrcCommand::createMessage(Config.IRC.IngameChannel, gameRegisterFailMessage.arg(reason))); + sendIrcMessage(gameRegisterFailMessage.arg(reason), Config.IRC.IngameChannel, false, false); } } } @@ -367,16 +362,15 @@ void CRSM::scenarioFinished() void CRSM::ircMessageReceived(IrcMessage *message) { - if(message->isOwn()) - return; if(message->type() == IrcMessage::Notice) { + IrcNoticeMessage* noticeMessage = (IrcNoticeMessage*)message; if(message->nick() == "NickServ") { QStringList split = message->parameters().at(1).split(' ', QString::SkipEmptyParts); - if(((IrcNoticeMessage*)message)->content().contains("nickname is registered")) + if(noticeMessage->content().contains("nickname is registered")) { - connection->sendCommand(IrcCommand::createMessage("NickServ", "IDENTIFY " + Config.IRC.Nick + " " + Config.IRC.Password)); + sendIrcMessage("IDENTIFY " + Config.IRC.Nick + " " + Config.IRC.Password, "NickServ", false, false); } else if(split.first() == "STATUS") { @@ -393,11 +387,22 @@ void CRSM::ircMessageReceived(IrcMessage *message) } } } + Log.ircLog(noticeMessage->content(), noticeMessage->nick(), noticeMessage->isPrivate(), noticeMessage->target(), false, true); + if(!message->isOwn()) + { + Log.ircUserLog(noticeMessage->content(), ClientInfo::ircClient(noticeMessage->nick(), noticeMessage->target()), noticeMessage->isPrivate(), noticeMessage->target(), false, true); + } } else if(message->type() == IrcMessage::Private) { IrcPrivateMessage* privMessage = (IrcPrivateMessage*)message; QString target = privMessage->target(); + Log.ircLog(privMessage->content(), privMessage->nick(), privMessage->isPrivate(), privMessage->target(), privMessage->isAction(), false); + if(message->isOwn()) + { + return; + } + Log.ircUserLog(privMessage->content(), ClientInfo::ircClient(privMessage->nick(), privMessage->target()), privMessage->isPrivate(), privMessage->target(), privMessage->isAction(), false); if(target == connection->nickName()) target = message->nick(); QString mess = privMessage->content(); @@ -464,11 +469,15 @@ void CRSM::ircMessageReceived(IrcMessage *message) } } } + else if(message->isOwn()) + { + return; + } else if(message->type() == IrcMessage::Join) { IrcJoinMessage* joinMessage = (IrcJoinMessage*) message; QString joinChannel = joinMessage->channel(); - connection->sendCommand(IrcCommand::createMessage(joinChannel, "Hallo " + message->nick() + "!")); + sendIrcMessage("Hallo " + message->nick() + "!", joinChannel, false, false); if(joinChannel == Config.IRC.IngameChannel && Session.IRC.UseIngameChat) { writeToServer("[IRC] " + message->nick() + " hat den Channel betreten." + "\n"); @@ -683,6 +692,7 @@ void CRSM::startScen(const ScenarioSettings &scen, QStringList argList) processManager->setWorkingDirectory(Config.Auto.Volatile.Clonk.Directory); out(Packs.linkScenarioPacks(filename)); processManager->start(Config.Clonk.Server.Executable, argList); + Log.scenLog(scen); } void CRSM::readConfig() @@ -769,7 +779,7 @@ void CRSM::listC4Folders() } } } - out(QString(3, '\b') + ": Finished\n"); + out("Finished\n"); } void CRSM::cleanUp() @@ -1049,7 +1059,7 @@ void CRSM::informModsAboutAliasWish() { foreach(const QString& mod, ircMods) { - connection->sendCommand(IrcCommand::createNotice(mod, "Ein neuer Aliaswunsch ist verfügbar. Insgesamt verfügbar: " + QString::number(Config.Auto.Hosting.AliasWishes.size()))); + sendIrcMessage("Ein neuer Aliaswunsch ist verfügbar. Insgesamt verfügbar: " + QString::number(Config.Auto.Hosting.AliasWishes.size()), mod, false, true); } } @@ -1057,13 +1067,13 @@ void CRSM::editAliasWishes() { if(Config.Auto.Hosting.AliasWishes.isEmpty()) { - connection->sendCommand(IrcCommand::createMessage(aliasWishEditor, "Keine Aliaswünsche " + (currentAliasWish == "" ? QString("") : QString("mehr ")) + "vorhanden.")); + sendIrcMessage("Keine Aliaswünsche " + (currentAliasWish == "" ? QString("") : QString("mehr ")) + "vorhanden.", aliasWishEditor, false, false); stopAliasWishEditing(); } else { currentAliasWish = Config.Auto.Hosting.AliasWishes.firstKey(); - connection->sendCommand(IrcCommand::createMessage(aliasWishEditor, currentAliasWish + " = " + Config.Auto.Hosting.AliasWishes[currentAliasWish] + " [Ja|Nein|Stop]")); + sendIrcMessage(currentAliasWish + " = " + Config.Auto.Hosting.AliasWishes[currentAliasWish] + " [Ja|Nein|Stop]", aliasWishEditor, false, false); } } @@ -1080,13 +1090,13 @@ void CRSM::editAliasWishes(const QString &message) } else if(message.toLower() == "s" || message.toLower() == "stop") { - connection->sendCommand(IrcCommand::createMessage(aliasWishEditor, "Aliaswunsch-Bearbeitung gestoppt.")); + sendIrcMessage("Aliaswunsch-Bearbeitung gestoppt.", aliasWishEditor, false, false); stopAliasWishEditing(); return; } else { - connection->sendCommand(IrcCommand::createMessage(aliasWishEditor, "\"" + message + "\" ist keine Antwortmöglichkeit. Antwortmöglichkeiten: [Ja|Nein|Stop]")); + sendIrcMessage("\"" + message + "\" ist keine Antwortmöglichkeit. Antwortmöglichkeiten: [Ja|Nein|Stop]", aliasWishEditor, false, false); } editAliasWishes(); } @@ -1245,6 +1255,7 @@ CmdFunctionRef* CRSM::findCommand(QStringList&& cmd, ClientInterface interface, bool CRSM::cmd(const QString& cmd, const ClientInfo &client) { + Log.commandLog(cmd, client, false); CmdFunctionRef* cmdPtr; QString realCmd = cmd; if((cmdPtr = findCommand(cmd, client.interface, realCmd)) != nullptr) @@ -1439,11 +1450,11 @@ void CRSM::respond(const ClientInfo &client, const QString &message, const Respo { case RespondType::Normal: case RespondType::Private: - connection->sendCommand(IrcCommand::createMessage(type == Normal ? client.target : client.nick, line)); + sendIrcMessage(line, type == Normal ? client.target : client.nick, false, false); break; case RespondType::Notice: case RespondType::PrivateNotice: - connection->sendCommand(IrcCommand::createNotice(type == Notice ? client.target : client.nick, line)); + sendIrcMessage(line, type == Notice ? client.target : client.nick, false, true); break; } } @@ -1454,12 +1465,17 @@ void CRSM::respond(const ClientInfo &client, const QString &message, const Respo case Auto: //just to avoid the compiler warning break; } + Log.commandLog(message, client, true); + if(clientUserType(client) == Moderator) + { + Log.modLog(message, client, clientModName(client), true); + } } void CRSM::ircCheckUserStatus(const ClientInfo &requester, const ClientInfo &subject, IrcCheckCallback callback) { ircStatusFifos[subject.nick].append(qMakePair(requester, callback)); - connection->sendCommand(IrcCommand::createMessage("NickServ", "STATUS " + subject.nick)); + sendIrcMessage("STATUS " + subject.nick, "NickServ", false, false); } void CRSM::setIngameAdmin(const ClientInfo &client, const QString& newAdmin) @@ -1515,8 +1531,11 @@ void CRSM::setIngameAdmin(const ClientInfo &client, const QString& newAdmin) void CRSM::out(const QString &text) { - *qout << text; - qout->flush(); + if(text.isEmpty()) + { + return; + } + Log.log(text); if(managementConnections.size() == 0) { outputBuffer.write(text.toUtf8()); @@ -1628,7 +1647,7 @@ void CRSM::afkAdminTimeout() writeToServer("Der Rundenadmin war zu lange AFK und wurde freigegeben.\n"); if(Session.IRC.UseIngameChat) { - connection->sendCommand(IrcCommand::createMessage(Config.IRC.IngameChannel, "Der Rundenadmin war zu lange AFK und wurde freigegeben.\n")); + sendIrcMessage("Der Rundenadmin war zu lange AFK und wurde freigegeben.\n", Config.IRC.IngameChannel, false, false); } Session.AfkAdmin = false; } @@ -1661,6 +1680,7 @@ void CRSM::applyConfig() afkAdminTimer.setInterval(Config.Hosting.AfkAdminTime * 1000); Config.CRSM.ListFolder = QDir(Config.CRSM.ListFolder).absolutePath() + QDir::separator(); + Config.Logging.Folder = QDir(Config.Logging.Folder).absolutePath() + QDir::separator(); if(!QDir(Config.CRSM.ListFolder).exists()) { @@ -1739,6 +1759,10 @@ void CRSM::applyConfig() Session.IRC.UseIngameChat = false; } + out(Log.setLogFolder(Config.Logging.Folder)); + out(Log.setMainLog(Config.Logging.MainLog)); + Log.setTimestampFormat(Config.Logging.TimestampFormat); + setupCmds(); out(Stats.read(Config.CRSM.StatsFile)); out(Packs.read(Config.CRSM.PacksFile, Config.Auto.Volatile.Clonk.Directory)); @@ -1753,6 +1777,11 @@ QString CRSM::scenarioFileName(QString name) CmdResult CRSM::callCommand(const CmdFunctionRef &func, const QString &args, const ClientInfo &client, UserType userType) { + if(userType == Moderator) + { + + Log.modLog(func.name + " " + args, client, clientModName(client), false); + } CmdResult ret = (this->*func.func)(func.name, args, client, userType); Stats.AddCommandResult(client, func.name, ret); return ret; @@ -1850,6 +1879,55 @@ void CRSM::substituteCommandAlias(QString &command) } } +QString CRSM::clientModName(const ClientInfo &client) +{ + if(client.interface == IRC) + { + return Config.IRC.Moderators.value(client.nick); + } + else if(client.interface == Clonk) + { + return Config.Clonk.Chat.Moderators.value(client.CUID); + } + else if(client.interface == Management) + { + return client.management.name; + } + else + { + return ""; + } +} + +void CRSM::sendIrcMessage(const QString& message, const QString& target, bool action, bool notice) +{ + bool query = !isChannelName(target); + Log.ircLog(message, (query ? target : connection->nickName()), query, target, action, notice, query); + /*if(cmd->type() == IrcCommand::Message || cmd->type() == IrcCommand::CtcpAction) + { + IrcPrivateMessage* messageCmd = (IrcPrivateMessage*)cmd; + Log.ircLog(messageCmd->content(), connection->nickName(), !isChannelName(messageCmd->parameter(0)), messageCmd->parameter(0), cmd->type() == IrcCommand::CtcpAction, false); + } + else if(cmd->type() == IrcCommand::Notice) + { + IrcNoticeMessage* noticeCmd = (IrcNoticeMessage*)cmd; + Log.ircLog(noticeCmd->content(), connection->nickName(), !isChannelName(noticeCmd->parameter(0)), noticeCmd->parameter(0), false, true); + } + connection->sendCommand(cmd);*/ + if(action) + { + connection->sendCommand(IrcCommand::createCtcpAction(target, message)); + } + else if(notice) + { + connection->sendCommand(IrcCommand::createNotice(target, message)); + } + else + { + connection->sendCommand(IrcCommand::createMessage(target, message)); + } +} + CMD_FUNCTION_IMPL(help) bool longHelp = (args == "long"); if(args.isEmpty() || longHelp) @@ -2611,7 +2689,7 @@ CMD_FUNCTION_IMPL(ircSay) } else { - connection->sendCommand(IrcCommand::createMessage(target, message)); + sendIrcMessage(message, target, false, false); } } return Success; @@ -2862,7 +2940,7 @@ IRC_CHECK_CALLBACK_IMPL(ircSayQuery) { foreach(const QString& message, ircSayQueryFifos[subject.nick]) { - connection->sendCommand(IrcCommand::createMessage(subject.nick, message)); + sendIrcMessage(message, subject.nick, false, false); } } ircSayQueryFifos[subject.nick].clear(); -- cgit v1.2.3-54-g00ecf