]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanlog.cpp
fix warnings
[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                 static bool Logging = false;
30
31 printf("I got called\n");
32                 if (loglevel < this->loglvl)
33                         return;
34
35                 if (Logging)
36                         return;
37
38                 if (c)
39                 {
40                         Logging = true; // this avoids (rare chance) loops with logging server IO on networks
41                         char buf[MAXBUF];
42                         snprintf(buf, MAXBUF, "\2%s\2: %s", type.c_str(), msg.c_str());
43
44                         c->WriteChannelWithServ(ServerInstance->Config->ServerName, "PRIVMSG %s :%s", c->name, buf);
45                         ServerInstance->PI->SendChannelPrivmsg(c, 0, buf);
46                         Logging = false;
47                 }
48         }
49 };
50
51 /* $ModDesc: Logs output to a channel instead of / as well as a file. */
52
53 class ModuleChanLog : public Module
54 {
55  private:
56         std::vector<ChannelLogStream*> cls;
57  public:
58         ModuleChanLog(InspIRCd* Me) : Module(Me)
59         {
60                 Implementation eventlist[] = { I_OnRehash };
61                 ServerInstance->Modules->Attach(eventlist, this, 1);
62
63                 OnRehash(NULL, "");
64         }
65
66         virtual ~ModuleChanLog()
67         {
68                 std::vector<ChannelLogStream*>::iterator i;
69                 while ((i = cls.begin()) != cls.end())
70                 {
71                         ServerInstance->Logs->DelLogStream(*i);
72                         cls.erase(i);
73                 }
74         }
75
76         virtual void OnRehash(User *user, const std::string &parameter)
77         {
78                 ConfigReader Conf(ServerInstance);
79
80                 /* Since the CloseLogs prior to this hook just wiped out our logstreams for us, we just need to wipe the vector. */
81                 std::vector<ChannelLogStream*>().swap(cls);
82                 int index, max = Conf.Enumerate("log");
83                 cls.reserve(max);
84
85                 for (index = 0; index < max; ++index)
86                 {
87                         std::string method = Conf.ReadValue("log", "method", index);
88
89
90                         //if (method != "file")
91                         //      continue;
92
93                         std::string type = Conf.ReadValue("log", "type", index);
94                         std::string level = Conf.ReadValue("log", "level", index);
95                         int loglevel = DEFAULT;
96
97                         if (level == "debug")
98                         {
99                                 loglevel = DEBUG;
100                                 ServerInstance->Config->debugging = true;
101                         }
102                         else if (level == "verbose")
103                         {
104                                 loglevel = VERBOSE;
105                         }
106                         else if (level == "default")
107                         {
108                                 loglevel = DEFAULT;
109                         }
110                         else if (level == "sparse")
111                         {
112                                 loglevel = SPARSE;
113                         }
114                         else if (level == "none")
115                         {
116                                 loglevel = NONE;
117                         }
118
119                         std::string target = Conf.ReadValue("log", "target", index);
120
121 printf("looking at tag with method: %s type: %s level: %s target: %s", method.c_str(), type.c_str(), level.c_str(), target.c_str());
122
123                         ChannelLogStream* c = new ChannelLogStream(ServerInstance, loglevel, target);
124                         ServerInstance->Logs->AddLogTypes(type, c, true);
125                         cls.push_back(c);
126                 }
127         }
128
129         virtual Version GetVersion()
130         {
131                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
132         }
133 };
134
135
136 MODULE_INIT(ModuleChanLog)
137