Advertisement
NovaYoshi

patched log.cpp

Jun 22nd, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.20 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2008-2012  See the AUTHORS file for details.
  3.  * Copyright (C) 2006-2007, CNU <bshalm@broadpark.no> (http://cnu.dieplz.net/znc)
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License version 2 as published
  7.  * by the Free Software Foundation.
  8.  */
  9.  
  10. #include <znc/FileUtils.h>
  11. #include <znc/User.h>
  12. #include <znc/IRCNetwork.h>
  13. #include <znc/Chan.h>
  14. #include <znc/Server.h>
  15.  
  16. using std::vector;
  17.  
  18. class CLogMod: public CModule {
  19. public:
  20.     MODCONSTRUCTOR(CLogMod) {}
  21.  
  22.     void PutLog(const CString& sLine, const CString& sWindow = "status");
  23.     void PutLog(const CString& sLine, const CChan& Channel);
  24.     void PutLog(const CString& sLine, const CNick& Nick);
  25.     CString GetServer();
  26.  
  27.     virtual bool OnLoad(const CString& sArgs, CString& sMessage);
  28.     virtual void OnIRCConnected();
  29.     virtual void OnIRCDisconnected();
  30.     virtual EModRet OnBroadcast(CString& sMessage);
  31.  
  32.     virtual void OnRawMode(const CNick& OpNick, CChan& Channel, const CString& sModes, const CString& sArgs);
  33.     virtual void OnKick(const CNick& OpNick, const CString& sKickedNick, CChan& Channel, const CString& sMessage);
  34.     virtual void OnQuit(const CNick& Nick, const CString& sMessage, const vector<CChan*>& vChans);
  35.     virtual void OnJoin(const CNick& Nick, CChan& Channel);
  36.     virtual void OnPart(const CNick& Nick, CChan& Channel, const CString& sMessage);
  37.     virtual void OnNick(const CNick& OldNick, const CString& sNewNick, const vector<CChan*>& vChans);
  38.     virtual EModRet OnTopic(CNick& Nick, CChan& Channel, CString& sTopic);
  39.  
  40.     /* notices */
  41.     virtual EModRet OnUserNotice(CString& sTarget, CString& sMessage);
  42.     virtual EModRet OnPrivNotice(CNick& Nick, CString& sMessage);
  43.     virtual EModRet OnChanNotice(CNick& Nick, CChan& Channel, CString& sMessage);
  44.  
  45.     /* actions */
  46.     virtual EModRet OnUserAction(CString& sTarget, CString& sMessage);
  47.     virtual EModRet OnPrivAction(CNick& Nick, CString& sMessage);
  48.     virtual EModRet OnChanAction(CNick& Nick, CChan& Channel, CString& sMessage);
  49.  
  50.     /* msgs */
  51.     virtual EModRet OnUserMsg(CString& sTarget, CString& sMessage);
  52.     virtual EModRet OnPrivMsg(CNick& Nick, CString& sMessage);
  53.     virtual EModRet OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage);
  54.  
  55. private:
  56.     CString                 m_sLogPath;
  57. };
  58.  
  59. void CLogMod::PutLog(const CString& sLine, const CString& sWindow /*= "Status"*/)
  60. {
  61.     CString sPath;
  62.     time_t curtime;
  63.  
  64.     time(&curtime);
  65.     // Generate file name
  66.     sPath = CUtils::FormatTime(curtime, m_sLogPath, m_pUser->GetTimezone());
  67.     if (sPath.empty())
  68.     {
  69.         DEBUG("Could not format log path [" << sPath << "]");
  70.         return;
  71.     }
  72.  
  73.     // $WINDOW has to be handled last, since it can contain %
  74.     sPath.Replace("$NETWORK", (m_pNetwork ? m_pNetwork->GetName() : "znc"));
  75.     sPath.Replace("$WINDOW", sWindow.Replace_n("/", "?"));
  76.     sPath.Replace("$USER", (m_pUser ? m_pUser->GetUserName() : "UNKNOWN"));
  77.  
  78.     // Check if it's allowed to write in this specific path
  79.     sPath = CDir::CheckPathPrefix(GetSavePath(), sPath);
  80.     if (sPath.empty())
  81.     {
  82.         DEBUG("Invalid log path ["<<m_sLogPath<<"].");
  83.         return;
  84.     }
  85.  
  86.     CFile LogFile(sPath);
  87.     CString sLogDir = LogFile.GetDir();
  88.     if (!CFile::Exists(sLogDir)) CDir::MakeDir(sLogDir);
  89.     if (LogFile.Open(O_WRONLY | O_APPEND | O_CREAT))
  90.     {
  91. //      LogFile.Write(CUtils::FormatTime(curtime, "[%H:%M:%S] ", m_pUser->GetTimezone()) + sLine + "\n");
  92.         LogFile.Write(CUtils::FormatTime(curtime, "%b %d %H:%M:%S ", m_pUser->GetTimezone()) + sLine + "\n");
  93.     } else
  94.         DEBUG("Could not open log file [" << sPath << "]: " << strerror(errno));
  95. }
  96.  
  97. void CLogMod::PutLog(const CString& sLine, const CChan& Channel)
  98. {
  99.     PutLog(sLine, Channel.GetName());
  100. }
  101.  
  102. void CLogMod::PutLog(const CString& sLine, const CNick& Nick)
  103. {
  104.     PutLog(sLine, Nick.GetNick());
  105. }
  106.  
  107. CString CLogMod::GetServer()
  108. {
  109.     CServer* pServer = m_pNetwork->GetCurrentServer();
  110.     CString sSSL;
  111.  
  112.     if (!pServer)
  113.         return "(no server)";
  114.  
  115.     if (pServer->IsSSL())
  116.         sSSL = "+";
  117.     return pServer->GetName() + " " + sSSL + CString(pServer->GetPort());
  118. }
  119.  
  120. bool CLogMod::OnLoad(const CString& sArgs, CString& sMessage)
  121. {
  122.     // Use load parameter as save path
  123.     m_sLogPath = sArgs;
  124.  
  125.     // Add default filename to path if it's a folder
  126.     if (GetType() == CModInfo::UserModule) {
  127.         if (m_sLogPath.Right(1) == "/" || m_sLogPath.find("$WINDOW") == CString::npos || m_sLogPath.find("$NETWORK") == CString::npos) {
  128.             if (!m_sLogPath.empty()) {
  129.                 m_sLogPath += "/";
  130.             }
  131.             //m_sLogPath += "$NETWORK_$WINDOW_%Y%m%d.log";
  132.             m_sLogPath += "$NETWORK/$WINDOW.log";
  133.         }
  134.     } else if (GetType() == CModInfo::NetworkModule) {
  135.         if (m_sLogPath.Right(1) == "/" || m_sLogPath.find("$WINDOW") == CString::npos) {
  136.             if (!m_sLogPath.empty()) {
  137.                 m_sLogPath += "/";
  138.             }
  139.             //m_sLogPath += "$WINDOW_%Y%m%d.log";
  140.             m_sLogPath += "$WINDOW.log";
  141.         }
  142.     } else {
  143.         if (m_sLogPath.Right(1) == "/" || m_sLogPath.find("$USER") == CString::npos || m_sLogPath.find("$WINDOW") == CString::npos || m_sLogPath.find("$NETWORK") == CString::npos) {
  144.             if (!m_sLogPath.empty()) {
  145.                 m_sLogPath += "/";
  146.             }
  147.             //m_sLogPath += "$USER_$NETWORK_$WINDOW_%Y%m%d.log";
  148.             m_sLogPath += "$USER/$NETWORK/$WINDOW.log";
  149.         }
  150.     }
  151.  
  152.     // Check if it's allowed to write in this path in general
  153.     m_sLogPath = CDir::CheckPathPrefix(GetSavePath(), m_sLogPath);
  154.     if (m_sLogPath.empty())
  155.     {
  156.         sMessage = "Invalid log path ["+m_sLogPath+"].";
  157.         return false;
  158.     } else {
  159.         sMessage = "Logging to ["+m_sLogPath+"].";
  160.         return true;
  161.     }
  162. }
  163.  
  164.  
  165. void CLogMod::OnIRCConnected()
  166. {
  167.     PutLog("Connected to IRC (" + GetServer() + ")");
  168. }
  169.  
  170. void CLogMod::OnIRCDisconnected()
  171. {
  172.     PutLog("Disconnected from IRC (" + GetServer() + ")");
  173. }
  174.  
  175. CModule::EModRet CLogMod::OnBroadcast(CString& sMessage)
  176. {
  177.     PutLog("Broadcast: " + sMessage);
  178.     return CONTINUE;
  179. }
  180.  
  181. void CLogMod::OnRawMode(const CNick& OpNick, CChan& Channel, const CString& sModes, const CString& sArgs)
  182. {
  183.     PutLog("*** " + OpNick.GetNick() + " sets mode: " + sModes + " " + sArgs, Channel);
  184. }
  185.  
  186. void CLogMod::OnKick(const CNick& OpNick, const CString& sKickedNick, CChan& Channel, const CString& sMessage)
  187. {
  188.     PutLog("*** " + sKickedNick + " was kicked by " + OpNick.GetNick() + " (" + sMessage + ")", Channel);
  189. }
  190.  
  191. void CLogMod::OnQuit(const CNick& Nick, const CString& sMessage, const vector<CChan*>& vChans)
  192. {
  193.     for (std::vector<CChan*>::const_iterator pChan = vChans.begin(); pChan != vChans.end(); ++pChan)
  194.         PutLog("*** Quits: " + Nick.GetNick() + " (" + Nick.GetIdent() + "@" + Nick.GetHost() + ") (" + sMessage + ")", **pChan);
  195. }
  196.  
  197. void CLogMod::OnJoin(const CNick& Nick, CChan& Channel)
  198. {
  199.     PutLog("*** Joins: " + Nick.GetNick() + " (" + Nick.GetIdent() + "@" + Nick.GetHost() + ")", Channel);
  200. }
  201.  
  202. void CLogMod::OnPart(const CNick& Nick, CChan& Channel, const CString& sMessage)
  203. {
  204.     PutLog("*** Parts: " + Nick.GetNick() + " (" + Nick.GetIdent() + "@" + Nick.GetHost() + ") (" + sMessage + ")", Channel);
  205. }
  206.  
  207. void CLogMod::OnNick(const CNick& OldNick, const CString& sNewNick, const vector<CChan*>& vChans)
  208. {
  209.     for (std::vector<CChan*>::const_iterator pChan = vChans.begin(); pChan != vChans.end(); ++pChan)
  210.         PutLog("*** " + OldNick.GetNick() + " is now known as " + sNewNick, **pChan);
  211. }
  212.  
  213. CModule::EModRet CLogMod::OnTopic(CNick& Nick, CChan& Channel, CString& sTopic)
  214. {
  215.     PutLog("*** " + Nick.GetNick() + " changes topic to '" + sTopic + "'", Channel);
  216.     return CONTINUE;
  217. }
  218.  
  219. /* notices */
  220. CModule::EModRet CLogMod::OnUserNotice(CString& sTarget, CString& sMessage)
  221. {
  222.     if (m_pNetwork) {
  223.         PutLog("-" + m_pNetwork->GetCurNick() + "- " + sMessage, sTarget);
  224.     }
  225.  
  226.     return CONTINUE;
  227. }
  228.  
  229. CModule::EModRet CLogMod::OnPrivNotice(CNick& Nick, CString& sMessage)
  230. {
  231.     PutLog("-" + Nick.GetNick() + "- " + sMessage, Nick);
  232.     return CONTINUE;
  233. }
  234.  
  235. CModule::EModRet CLogMod::OnChanNotice(CNick& Nick, CChan& Channel, CString& sMessage)
  236. {
  237.     PutLog("-" + Nick.GetNick() + "- " + sMessage, Channel);
  238.     return CONTINUE;
  239. }
  240.  
  241. /* actions */
  242. CModule::EModRet CLogMod::OnUserAction(CString& sTarget, CString& sMessage)
  243. {
  244.     if (m_pNetwork) {
  245.         PutLog("* " + m_pNetwork->GetCurNick() + " " + sMessage, sTarget);
  246.     }
  247.  
  248.     return CONTINUE;
  249. }
  250.  
  251. CModule::EModRet CLogMod::OnPrivAction(CNick& Nick, CString& sMessage)
  252. {
  253.     PutLog("* " + Nick.GetNick() + " " + sMessage, Nick);
  254.     return CONTINUE;
  255. }
  256.  
  257. CModule::EModRet CLogMod::OnChanAction(CNick& Nick, CChan& Channel, CString& sMessage)
  258. {
  259.     PutLog("* " + Nick.GetNick() + " " + sMessage, Channel);
  260.     return CONTINUE;
  261. }
  262.  
  263. /* msgs */
  264. CModule::EModRet CLogMod::OnUserMsg(CString& sTarget, CString& sMessage)
  265. {
  266.     if (m_pNetwork) {
  267.         PutLog("<" + m_pNetwork->GetCurNick() + "> " + sMessage, sTarget);
  268.     }
  269.  
  270.     return CONTINUE;
  271. }
  272.  
  273. CModule::EModRet CLogMod::OnPrivMsg(CNick& Nick, CString& sMessage)
  274. {
  275.     PutLog("<" + Nick.GetNick() + "> " + sMessage, Nick);
  276.     return CONTINUE;
  277. }
  278.  
  279. CModule::EModRet CLogMod::OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage)
  280. {
  281.     PutLog("<" + Nick.GetNick() + "> " + sMessage, Channel);
  282.     return CONTINUE;
  283. }
  284.  
  285. template<> void TModInfo<CLogMod>(CModInfo& Info) {
  286.     Info.AddType(CModInfo::NetworkModule);
  287.     Info.AddType(CModInfo::GlobalModule);
  288.     Info.SetHasArgs(true);
  289.     Info.SetArgsHelpText("Optional path where to store logs.");
  290.     Info.SetWikiPage("log");
  291. }
  292.  
  293. USERMODULEDEFS(CLogMod, "Write IRC logs")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement