by Jay2k1 • 8 months ago • C++
  1. // Updated for 1.x - KindOne @ chat.freenode.net
  2. // Turned into a network module. - Loading as a user would spam all networks into a single file.
  3. // RUN AT OWN RISK ...
  4.  
  5. // Original Source: https://tomaw.net/tmp/rawlog.cpp
  6.  
  7. /*
  8.  * rawlog module for znc.  Based on CNU's Logging Module
  9.  *
  10.  * Copyright (C) 2006-2007, CNU <bshalm@broadpark.no> (http://cnu.dieplz.net/znc)
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the
  24.  * Free Software Foundation, Inc.,
  25.  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  26.  */
  27.  
  28. #include <znc/IRCNetwork.h>
  29. #include <znc/Modules.h>
  30. #include <znc/Server.h>
  31.  
  32. #include <iomanip>
  33. #include <fstream>
  34. #include <sstream>
  35. #include <ctime>
  36.  
  37. class CRawLogMod: public CModule {
  38. public:
  39.         MODCONSTRUCTOR(CRawLogMod) {}
  40.  
  41.         void PutLog(const CString& sLine);
  42.         CString GetServer();
  43.         virtual void OnIRCConnected();
  44.         virtual void OnIRCDisconnected();
  45.         virtual EModRet OnRaw(CString& sLine);
  46. };
  47.  
  48. void CRawLogMod::PutLog(const CString& sLine)
  49. {
  50.         std::ofstream ofLog;
  51.         std::stringstream ssPath;
  52.         time_t curtime;
  53.         tm* timeinfo;
  54.  
  55.         time(&curtime);
  56.         timeinfo = localtime(&curtime);
  57.  
  58.         ssPath.fill('0');
  59.         ssPath << GetSavePath() << "/"
  60.            << std::setw(4) << (timeinfo->tm_year + 1900)
  61.            << std::setw(2) << (timeinfo->tm_mon + 1)
  62.            << std::setw(2) << (timeinfo->tm_mday)
  63.            << ".log";
  64.  
  65.         ofLog.open(ssPath.str().c_str(), std::ofstream::app);
  66.  
  67.         if (ofLog.good())
  68.         {
  69.                 /* Write line: [HH:MM:SS] MSG */
  70.                 ofLog.fill('0');
  71.                 ofLog << "[" << std::setw(2) << timeinfo->tm_hour
  72.                           << ":" << std::setw(2) << timeinfo->tm_min
  73.                           << ":" << std::setw(2) << timeinfo->tm_sec
  74.                           << "] " << sLine << std::endl;
  75.         }
  76. }
  77.  
  78. CString CRawLogMod::GetServer()
  79. {
  80.         CServer* pServer = GetNetwork()->GetCurrentServer();
  81.  
  82.         if (!pServer)
  83.                 return "(no server)";
  84.  
  85.         return pServer->GetName() + ":" + CString(pServer->GetPort());
  86. }
  87.  
  88. void CRawLogMod::OnIRCConnected()
  89. {
  90.         PutLog("Connected to IRC (" + GetServer() + ")");
  91. }
  92.  
  93. void CRawLogMod::OnIRCDisconnected()
  94. {
  95.         PutLog("Disconnected from IRC (" + GetServer() + ")");
  96. }
  97.  
  98. CModule::EModRet CRawLogMod::OnRaw(CString& sMessage)
  99. {
  100.         PutLog(sMessage);
  101.         return CONTINUE;
  102. }
  103.  
  104. template<> void TModInfo<CRawLogMod>(CModInfo& Info) {
  105.         Info.AddType(CModInfo::NetworkModule);
  106. }
  107.  
  108. NETWORKMODULEDEFS(CRawLogMod, "tomaw's IRC raw-logging Module")