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