]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanlog.cpp
8ba07d9badf105f8aa14bf581e8b87f38ebf0dfb
[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 /* $ModDesc: Logs snomask output to channel(s). */
17
18 /*
19  * This is for the "old" chanlog module which intercepted messages going to the logfile..
20  * I don't consider it all that useful, and it's quite dangerous if setup incorrectly, so
21  * this is defined out but left intact in case someone wants to develop it further someday.
22  *
23  * -- w00t (aug 23rd, 2008)
24  */
25 #define OLD_CHANLOG 0
26
27 #if OLD_CHANLOG
28 class ChannelLogStream : public LogStream
29 {
30  private:
31         std::string channel;
32
33  public:
34         ChannelLogStream(InspIRCd *Instance, int loglevel, const std::string &chan) : LogStream(Instance, loglevel), channel(chan)
35         {
36         }
37
38         virtual void OnLog(int loglevel, const std::string &type, const std::string &msg)
39         {
40                 Channel *c = ServerInstance->FindChan(channel);
41                 static bool Logging = false;
42
43                 if (loglevel < this->loglvl)
44                         return;
45
46                 if (Logging)
47                         return;
48
49                 if (c)
50                 {
51                         Logging = true; // this avoids (rare chance) loops with logging server IO on networks
52                         char buf[MAXBUF];
53                         snprintf(buf, MAXBUF, "\2%s\2: %s", type.c_str(), msg.c_str());
54
55                         c->WriteChannelWithServ(ServerInstance->Config->ServerName, "PRIVMSG %s :%s", c->name.c_str(), buf);
56                         ServerInstance->PI->SendChannelPrivmsg(c, 0, buf);
57                         Logging = false;
58                 }
59         }
60 };
61 #endif
62
63 class ModuleChanLog : public Module
64 {
65  private:
66         /*
67          * Multimap so people can redirect a snomask to multiple channels.
68          */
69         std::multimap<char, std::string> logstreams;
70
71  public:
72         ModuleChanLog(InspIRCd* Me) : Module(Me)
73         {
74                 Implementation eventlist[] = { I_OnRehash, I_OnSendSnotice };
75                 ServerInstance->Modules->Attach(eventlist, this, 2);
76
77                 OnRehash(NULL, "");
78         }
79
80         virtual ~ModuleChanLog()
81         {
82         }
83
84         virtual void OnRehash(User *user, const std::string &parameter)
85         {
86                 ConfigReader MyConf(ServerInstance);
87                 std::string snomasks;
88                 std::string channel;
89
90                 logstreams.clear();
91
92                 for (int i = 0; i < MyConf.Enumerate("chanlog"); i++)
93                 {
94                         channel = MyConf.ReadValue("chanlog", "channel", i);
95                         snomasks = MyConf.ReadValue("chanlog", "snomasks", i);
96
97                         if (channel.empty() || snomasks.empty())
98                         {
99                                 ServerInstance->Logs->Log("m_chanlog", DEFAULT, "Malformed chanlog tag, ignoring");
100                                 continue;
101                         }
102
103                         for (std::string::const_iterator it = snomasks.begin(); it != snomasks.end(); it++)
104                         {
105                                 logstreams.insert(std::make_pair(*it, channel));
106                                 ServerInstance->Logs->Log("m_chanlog", DEFAULT, "Logging %c to %s", *it, channel.c_str());
107                         }
108                 }
109
110         }
111
112         virtual int OnSendSnotice(char &sno, std::string &desc, const std::string &msg)
113         {
114                 std::multimap<char, std::string>::const_iterator it = logstreams.find(sno);
115                 char buf[MAXBUF];
116
117                 if (it == logstreams.end())
118                         return 0;
119
120                 snprintf(buf, MAXBUF, "\2%s\2: %s", desc.c_str(), msg.c_str());
121
122                 while (it != logstreams.end())
123                 {
124                         if (it->first != sno)
125                         {
126                                 it++;
127                                 continue;
128                         }
129
130                         Channel *c = ServerInstance->FindChan(it->second);
131                         if (c)
132                         {
133                                 c->WriteChannelWithServ(ServerInstance->Config->ServerName, "PRIVMSG %s :%s", c->name.c_str(), buf);
134                                 ServerInstance->PI->SendChannelPrivmsg(c, 0, buf);
135                         }
136
137                         it++;
138                 }
139
140                 return 0;
141         }
142
143         virtual Version GetVersion()
144         {
145                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
146         }
147 };
148
149
150 MODULE_INIT(ModuleChanLog)
151