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