]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanlog.cpp
Modify chanlog to send message remotely, too. Logging now works server <-> server...
[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                         char buf[MAXBUF];
35                         snprintf(buf, MAXBUF, "\2%s\2: %s", type.c_str(), msg.c_str());
36
37                         c->WriteChannelWithServ(ServerInstance->Config->ServerName, "PRIVMSG %s :%s", c->name, buf);
38                         ServerInstance->PI->SendChannelPrivmsg(c, 0, buf);
39                 }
40         }
41 };
42
43 /* $ModDesc: Logs output to a channel instead of / as well as a file. */
44
45 class ModuleChanLog : public Module
46 {
47  private:
48         std::vector<ChannelLogStream*> cls;
49  public:
50         ModuleChanLog(InspIRCd* Me) : Module(Me)
51         {
52         }
53
54         virtual ~ModuleChanLog()
55         {
56                 std::vector<ChannelLogStream*>::iterator i;
57                 while ((i = cls.begin()) != cls.end())
58                 {
59                         ServerInstance->Logs->DelLogStream(*i);
60                         cls.erase(i);
61                 }
62         }
63
64         virtual void OnReadConfig(ServerConfig* sc, ConfigReader* Conf)
65         {
66                 /* Since the CloseLogs prior to this hook just wiped out our logstreams for us, we just need to wipe the vector. */
67                 std::vector<ChannelLogStream*>().swap(cls);
68                 int index, max = Conf->Enumerate("log");
69                 cls.reserve(max);
70
71                 for (index = 0; index < max; ++index)
72                 {
73                         std::string method = Conf->ReadValue("log", "method", index);
74
75                         if (method != "file")
76                                 continue;
77
78                         std::string type = Conf->ReadValue("log", "type", index);
79                         std::string level = Conf->ReadValue("log", "level", index);
80                         int loglevel = DEFAULT;
81
82                         if (level == "debug")
83                         {
84                                 loglevel = DEBUG;
85                                 ServerInstance->Config->debugging = true;
86                         }
87                         else if (level == "verbose")
88                         {
89                                 loglevel = VERBOSE;
90                         }
91                         else if (level == "default")
92                         {
93                                 loglevel = DEFAULT;
94                         }
95                         else if (level == "sparse")
96                         {
97                                 loglevel = SPARSE;
98                         }
99                         else if (level == "none")
100                         {
101                                 loglevel = NONE;
102                         }
103
104                         std::string target = Conf->ReadValue("log", "target", index);
105                         ChannelLogStream* c = new ChannelLogStream(ServerInstance, loglevel, target);
106                         ServerInstance->Logs->AddLogTypes(type, c, true);
107                         cls.push_back(c);
108                 }
109         }
110
111         virtual Version GetVersion()
112         {
113                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
114         }
115 };
116
117
118 MODULE_INIT(ModuleChanLog)
119