]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanlog.cpp
Only valid targets for encap are now server ids
[user/henk/code/inspircd.git] / src / modules / m_chanlog.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 class ChannelLogStream : public LogStream
17 {
18  private:
19         std::string channel;
20
21  public:
22         ChannelLogStream(InspIRCd *Instance, int loglevel, const std::string &chan) : LogStream(Instance, loglevel), channel(chan)
23         {
24         }
25
26         virtual void OnLog(int loglevel, const std::string &type, const std::string &msg)
27         {
28                 Channel *c = ServerInstance->FindChan(channel);
29
30                 if (loglevel < this->loglvl) return;
31
32                 if (c)
33                 {
34                         // So this won't work remotely. Oh well.
35                         c->WriteChannelWithServ(ServerInstance->Config->ServerName, "PRIVMSG %s :\2%s\2: %s", c->name, type.c_str(), msg.c_str());
36                 }
37         }
38 };
39
40 /* $ModDesc: Logs output to a channel instead of / as well as a file. */
41
42 class ModuleChanLog : public Module
43 {
44  private:
45         std::vector<ChannelLogStream*> cls;
46  public:
47         ModuleChanLog(InspIRCd* Me) : Module(Me)
48         {
49         }
50
51         virtual ~ModuleChanLog()
52         {
53                 std::vector<ChannelLogStream*>::iterator i;
54                 while ((i = cls.begin()) != cls.end())
55                 {
56                         ServerInstance->Logs->DelLogStream(*i);
57                         cls.erase(i);
58                 }
59         }
60
61         virtual void OnReadConfig(ServerConfig* sc, ConfigReader* Conf)
62         {
63                 /* Since the CloseLogs prior to this hook just wiped out our logstreams for us, we just need to wipe the vector. */
64                 std::vector<ChannelLogStream*>().swap(cls);
65                 int index, max = Conf->Enumerate("log");
66                 cls.reserve(max);
67                 for (index = 0; index < max; ++index)
68                 {
69                         std::string method = Conf->ReadValue("log", "method", index);
70                         if (method != "file") continue;
71                         std::string type = Conf->ReadValue("log", "type", index);
72                         std::string level = Conf->ReadValue("log", "level", index);
73                         int loglevel = DEFAULT;
74                         if (level == "debug")
75                         {
76                                 loglevel = DEBUG;
77                                 ServerInstance->Config->debugging = true;
78                         }
79                         else if (level == "verbose")
80                         {
81                                 loglevel = VERBOSE;
82                         }
83                         else if (level == "default")
84                         {
85                                 loglevel = DEFAULT;
86                         }
87                         else if (level == "sparse")
88                         {
89                                 loglevel = SPARSE;
90                         }
91                         else if (level == "none")
92                         {
93                                 loglevel = NONE;
94                         }
95                         std::string target = Conf->ReadValue("log", "target", index);
96                         ChannelLogStream* c = new ChannelLogStream(ServerInstance, loglevel, target);
97                         ServerInstance->Logs->AddLogTypes(type, c, true);
98                         cls.push_back(c);
99                 }
100         }
101
102         virtual Version GetVersion()
103         {
104                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
105         }
106 };
107
108
109 MODULE_INIT(ModuleChanLog)
110