]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanlog.cpp
Merge pull request #92 from Robby-/insp20-headers
[user/henk/code/inspircd.git] / src / modules / m_chanlog.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
5  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22
23 /* $ModDesc: Logs snomask output to channel(s). */
24
25 class ModuleChanLog : public Module
26 {
27  private:
28         /*
29          * Multimap so people can redirect a snomask to multiple channels.
30          */
31         std::multimap<char, std::string> logstreams;
32
33  public:
34         ModuleChanLog()         {
35                 Implementation eventlist[] = { I_OnRehash, I_OnSendSnotice };
36                 ServerInstance->Modules->Attach(eventlist, this, 2);
37
38                 OnRehash(NULL);
39         }
40
41         virtual ~ModuleChanLog()
42         {
43         }
44
45         virtual void OnRehash(User *user)
46         {
47                 ConfigReader MyConf;
48                 std::string snomasks;
49                 std::string channel;
50
51                 logstreams.clear();
52
53                 for (int i = 0; i < MyConf.Enumerate("chanlog"); i++)
54                 {
55                         channel = MyConf.ReadValue("chanlog", "channel", i);
56                         snomasks = MyConf.ReadValue("chanlog", "snomasks", i);
57
58                         if (channel.empty() || snomasks.empty())
59                         {
60                                 ServerInstance->Logs->Log("m_chanlog", DEFAULT, "Malformed chanlog tag, ignoring");
61                                 continue;
62                         }
63
64                         for (std::string::const_iterator it = snomasks.begin(); it != snomasks.end(); it++)
65                         {
66                                 logstreams.insert(std::make_pair(*it, channel));
67                                 ServerInstance->Logs->Log("m_chanlog", DEFAULT, "Logging %c to %s", *it, channel.c_str());
68                         }
69                 }
70
71         }
72
73         virtual ModResult OnSendSnotice(char &sno, std::string &desc, const std::string &msg)
74         {
75                 std::multimap<char, std::string>::const_iterator it = logstreams.find(sno);
76                 char buf[MAXBUF];
77
78                 if (it == logstreams.end())
79                         return MOD_RES_PASSTHRU;
80
81                 snprintf(buf, MAXBUF, "\2%s\2: %s", desc.c_str(), msg.c_str());
82
83                 while (it != logstreams.end())
84                 {
85                         if (it->first != sno)
86                         {
87                                 it++;
88                                 continue;
89                         }
90
91                         Channel *c = ServerInstance->FindChan(it->second);
92                         if (c)
93                         {
94                                 c->WriteChannelWithServ(ServerInstance->Config->ServerName.c_str(), "PRIVMSG %s :%s", c->name.c_str(), buf);
95                                 ServerInstance->PI->SendChannelPrivmsg(c, 0, buf);
96                         }
97
98                         it++;
99                 }
100
101                 return MOD_RES_PASSTHRU;
102         }
103
104         virtual Version GetVersion()
105         {
106                 return Version("Logs snomask output to channel(s).", VF_VENDOR);
107         }
108 };
109
110
111 MODULE_INIT(ModuleChanLog)
112
113
114
115
116
117
118
119
120
121 /*
122  * This is for the "old" chanlog module which intercepted messages going to the logfile..
123  * I don't consider it all that useful, and it's quite dangerous if setup incorrectly, so
124  * this is defined out but left intact in case someone wants to develop it further someday.
125  *
126  * -- w00t (aug 23rd, 2008)
127  */
128 #define OLD_CHANLOG 0
129
130 #if OLD_CHANLOG
131 class ChannelLogStream : public LogStream
132 {
133  private:
134         std::string channel;
135
136  public:
137         ChannelLogStream(int loglevel, const std::string &chan) : LogStream(loglevel), channel(chan)
138         {
139         }
140
141         virtual void OnLog(int loglevel, const std::string &type, const std::string &msg)
142         {
143                 Channel *c = ServerInstance->FindChan(channel);
144                 static bool Logging = false;
145
146                 if (loglevel < this->loglvl)
147                         return;
148
149                 if (Logging)
150                         return;
151
152                 if (c)
153                 {
154                         Logging = true; // this avoids (rare chance) loops with logging server IO on networks
155                         char buf[MAXBUF];
156                         snprintf(buf, MAXBUF, "\2%s\2: %s", type.c_str(), msg.c_str());
157
158                         c->WriteChannelWithServ(ServerInstance->Config->ServerName.c_str(), "PRIVMSG %s :%s", c->name.c_str(), buf);
159                         ServerInstance->PI->SendChannelPrivmsg(c, 0, buf);
160                         Logging = false;
161                 }
162         }
163 };
164 #endif
165