The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

m_chanlog.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 
00016 /* $ModDesc: Logs snomask output to channel(s). */
00017 
00018 class ModuleChanLog : public Module
00019 {
00020  private:
00021         /*
00022          * Multimap so people can redirect a snomask to multiple channels.
00023          */
00024         std::multimap<char, std::string> logstreams;
00025 
00026  public:
00027         ModuleChanLog(InspIRCd* Me) : Module(Me)
00028         {
00029                 Implementation eventlist[] = { I_OnRehash, I_OnSendSnotice };
00030                 ServerInstance->Modules->Attach(eventlist, this, 2);
00031 
00032                 OnRehash(NULL, "");
00033         }
00034 
00035         virtual ~ModuleChanLog()
00036         {
00037         }
00038 
00039         virtual void OnRehash(User *user, const std::string &parameter)
00040         {
00041                 ConfigReader MyConf(ServerInstance);
00042                 std::string snomasks;
00043                 std::string channel;
00044 
00045                 logstreams.clear();
00046 
00047                 for (int i = 0; i < MyConf.Enumerate("chanlog"); i++)
00048                 {
00049                         channel = MyConf.ReadValue("chanlog", "channel", i);
00050                         snomasks = MyConf.ReadValue("chanlog", "snomasks", i);
00051 
00052                         if (channel.empty() || snomasks.empty())
00053                         {
00054                                 ServerInstance->Logs->Log("m_chanlog", DEFAULT, "Malformed chanlog tag, ignoring");
00055                                 continue;
00056                         }
00057 
00058                         for (std::string::const_iterator it = snomasks.begin(); it != snomasks.end(); it++)
00059                         {
00060                                 logstreams.insert(std::make_pair(*it, channel));
00061                                 ServerInstance->Logs->Log("m_chanlog", DEFAULT, "Logging %c to %s", *it, channel.c_str());
00062                         }
00063                 }
00064 
00065         }
00066 
00067         virtual int OnSendSnotice(char &sno, std::string &desc, const std::string &msg)
00068         {
00069                 std::multimap<char, std::string>::const_iterator it = logstreams.find(sno);
00070                 char buf[MAXBUF];
00071 
00072                 if (it == logstreams.end())
00073                         return 0;
00074 
00075                 snprintf(buf, MAXBUF, "\2%s\2: %s", desc.c_str(), msg.c_str());
00076 
00077                 while (it != logstreams.end())
00078                 {
00079                         if (it->first != sno)
00080                         {
00081                                 it++;
00082                                 continue;
00083                         }
00084 
00085                         Channel *c = ServerInstance->FindChan(it->second);
00086                         if (c)
00087                         {
00088                                 c->WriteChannelWithServ(ServerInstance->Config->ServerName, "PRIVMSG %s :%s", c->name.c_str(), buf);
00089                                 ServerInstance->PI->SendChannelPrivmsg(c, 0, buf);
00090                         }
00091 
00092                         it++;
00093                 }
00094 
00095                 return 0;
00096         }
00097 
00098         virtual Version GetVersion()
00099         {
00100                 return Version("$Id: m_chanlog.cpp 10291 2008-08-25 20:35:51Z w00t $", VF_VENDOR,API_VERSION);
00101         }
00102 };
00103 
00104 
00105 MODULE_INIT(ModuleChanLog)
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 /*
00116  * This is for the "old" chanlog module which intercepted messages going to the logfile..
00117  * I don't consider it all that useful, and it's quite dangerous if setup incorrectly, so
00118  * this is defined out but left intact in case someone wants to develop it further someday.
00119  *
00120  * -- w00t (aug 23rd, 2008)
00121  */
00122 #define OLD_CHANLOG 0
00123 
00124 #if OLD_CHANLOG
00125 class ChannelLogStream : public LogStream
00126 {
00127  private:
00128         std::string channel;
00129 
00130  public:
00131         ChannelLogStream(InspIRCd *Instance, int loglevel, const std::string &chan) : LogStream(Instance, loglevel), channel(chan)
00132         {
00133         }
00134 
00135         virtual void OnLog(int loglevel, const std::string &type, const std::string &msg)
00136         {
00137                 Channel *c = ServerInstance->FindChan(channel);
00138                 static bool Logging = false;
00139 
00140                 if (loglevel < this->loglvl)
00141                         return;
00142 
00143                 if (Logging)
00144                         return;
00145 
00146                 if (c)
00147                 {
00148                         Logging = true; // this avoids (rare chance) loops with logging server IO on networks
00149                         char buf[MAXBUF];
00150                         snprintf(buf, MAXBUF, "\2%s\2: %s", type.c_str(), msg.c_str());
00151 
00152                         c->WriteChannelWithServ(ServerInstance->Config->ServerName, "PRIVMSG %s :%s", c->name.c_str(), buf);
00153                         ServerInstance->PI->SendChannelPrivmsg(c, 0, buf);
00154                         Logging = false;
00155                 }
00156         }
00157 };
00158 #endif
00159