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