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