]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Implement support for IPv6 GeoIP lookups.
[user/henk/code/inspircd.git] / src / modules / m_chanhistory.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/ircv3_servertime.h"
22 #include "modules/ircv3_batch.h"
23 #include "modules/server.h"
24
25 struct HistoryItem
26 {
27         time_t ts;
28         std::string text;
29         std::string sourcemask;
30
31         HistoryItem(User* source, const std::string& Text)
32                 : ts(ServerInstance->Time())
33                 , text(Text)
34                 , sourcemask(source->GetFullHost())
35         {
36         }
37 };
38
39 struct HistoryList
40 {
41         std::deque<HistoryItem> lines;
42         unsigned int maxlen, maxtime;
43         std::string param;
44
45         HistoryList(unsigned int len, unsigned int time, const std::string& oparam)
46                 : maxlen(len), maxtime(time), param(oparam) { }
47 };
48
49 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
50 {
51         bool IsValidDuration(const std::string& duration)
52         {
53                 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
54                 {
55                         unsigned char c = *i;
56                         if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
57                                 continue;
58
59                         if (duration_multi[c] == 1)
60                                 return false;
61                 }
62                 return true;
63         }
64
65  public:
66         unsigned int maxlines;
67         HistoryMode(Module* Creator)
68                 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
69         {
70         }
71
72         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
73         {
74                 std::string::size_type colon = parameter.find(':');
75                 if (colon == std::string::npos)
76                 {
77                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
78                         return MODEACTION_DENY;
79                 }
80
81                 std::string duration(parameter, colon+1);
82                 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
83                 {
84                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
85                         return MODEACTION_DENY;
86                 }
87
88                 unsigned int len = ConvToInt(parameter.substr(0, colon));
89                 unsigned int time = InspIRCd::Duration(duration);
90                 if (len == 0 || (len > maxlines && IS_LOCAL(source)))
91                 {
92                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
93                         return MODEACTION_DENY;
94                 }
95                 if (len > maxlines)
96                         len = maxlines;
97
98                 HistoryList* history = ext.get(channel);
99                 if (history)
100                 {
101                         // Shrink the list if the new line number limit is lower than the old one
102                         if (len < history->lines.size())
103                                 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
104
105                         history->maxlen = len;
106                         history->maxtime = time;
107                         history->param = parameter;
108                 }
109                 else
110                 {
111                         ext.set(channel, new HistoryList(len, time, parameter));
112                 }
113                 return MODEACTION_ALLOW;
114         }
115
116         void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
117         {
118                 out.append(history->param);
119         }
120 };
121
122 class ModuleChanHistory
123         : public Module
124         , public ServerEventListener
125 {
126         HistoryMode m;
127         bool sendnotice;
128         UserModeReference botmode;
129         bool dobots;
130         IRCv3::Batch::CapReference batchcap;
131         IRCv3::Batch::API batchmanager;
132         IRCv3::Batch::Batch batch;
133         IRCv3::ServerTime::API servertimemanager;
134
135  public:
136         ModuleChanHistory()
137                 : ServerEventListener(this)
138                 , m(this)
139                 , botmode(this, "bot")
140                 , batchcap(this)
141                 , batchmanager(this)
142                 , batch("chathistory")
143                 , servertimemanager(this)
144         {
145         }
146
147         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
148         {
149                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
150                 m.maxlines = tag->getUInt("maxlines", 50, 1);
151                 sendnotice = tag->getBool("notice", true);
152                 dobots = tag->getBool("bots", true);
153         }
154
155         ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
156         {
157                 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
158         }
159
160         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
161         {
162                 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
163                 {
164                         Channel* c = target.Get<Channel>();
165                         HistoryList* list = m.ext.get(c);
166                         if (list)
167                         {
168                                 list->lines.push_back(HistoryItem(user, details.text));
169                                 if (list->lines.size() > list->maxlen)
170                                         list->lines.pop_front();
171                         }
172                 }
173         }
174
175         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
176         {
177                 LocalUser* localuser = IS_LOCAL(memb->user);
178                 if (!localuser)
179                         return;
180
181                 if (memb->user->IsModeSet(botmode) && !dobots)
182                         return;
183
184                 HistoryList* list = m.ext.get(memb->chan);
185                 if (!list)
186                         return;
187                 time_t mintime = 0;
188                 if (list->maxtime)
189                         mintime = ServerInstance->Time() - list->maxtime;
190
191                 if ((sendnotice) && (!batchcap.get(localuser)))
192                 {
193                         std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
194                         if (list->maxtime > 0)
195                                 message.append(" spanning up to " + ConvToStr(list->maxtime) + " seconds");
196                         memb->WriteNotice(message);
197                 }
198
199                 if (batchmanager)
200                 {
201                         batchmanager->Start(batch);
202                         batch.GetBatchStartMessage().PushParamRef(memb->chan->name);
203                 }
204
205                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
206                 {
207                         const HistoryItem& item = *i;
208                         if (item.ts >= mintime)
209                         {
210                                 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, memb->chan, item.text);
211                                 if (servertimemanager)
212                                         servertimemanager->Set(msg, item.ts);
213                                 batch.AddToBatch(msg);
214                                 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
215                         }
216                 }
217
218                 if (batchmanager)
219                         batchmanager->End(batch);
220         }
221
222         Version GetVersion() CXX11_OVERRIDE
223         {
224                 return Version("Provides channel history replayed on join", VF_VENDOR);
225         }
226 };
227
228 MODULE_INIT(ModuleChanHistory)