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