]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Add support for the IRCv3 batch specification.
[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
24 struct HistoryItem
25 {
26         time_t ts;
27         std::string text;
28         std::string sourcemask;
29
30         HistoryItem(User* source, const std::string& Text)
31                 : ts(ServerInstance->Time())
32                 , text(Text)
33                 , sourcemask(source->GetFullHost())
34         {
35         }
36 };
37
38 struct HistoryList
39 {
40         std::deque<HistoryItem> lines;
41         unsigned int maxlen, maxtime;
42         std::string param;
43
44         HistoryList(unsigned int len, unsigned int time, const std::string& oparam)
45                 : maxlen(len), maxtime(time), param(oparam) { }
46 };
47
48 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
49 {
50         bool IsValidDuration(const std::string& duration)
51         {
52                 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
53                 {
54                         unsigned char c = *i;
55                         if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
56                                 continue;
57
58                         if (duration_multi[c] == 1)
59                                 return false;
60                 }
61                 return true;
62         }
63
64  public:
65         unsigned int maxlines;
66         HistoryMode(Module* Creator)
67                 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
68         {
69         }
70
71         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
72         {
73                 std::string::size_type colon = parameter.find(':');
74                 if (colon == std::string::npos)
75                 {
76                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
77                         return MODEACTION_DENY;
78                 }
79
80                 std::string duration(parameter, colon+1);
81                 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
82                 {
83                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
84                         return MODEACTION_DENY;
85                 }
86
87                 unsigned int len = ConvToInt(parameter.substr(0, colon));
88                 unsigned int time = InspIRCd::Duration(duration);
89                 if (len == 0 || (len > maxlines && IS_LOCAL(source)))
90                 {
91                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
92                         return MODEACTION_DENY;
93                 }
94                 if (len > maxlines)
95                         len = maxlines;
96
97                 HistoryList* history = ext.get(channel);
98                 if (history)
99                 {
100                         // Shrink the list if the new line number limit is lower than the old one
101                         if (len < history->lines.size())
102                                 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
103
104                         history->maxlen = len;
105                         history->maxtime = time;
106                         history->param = parameter;
107                 }
108                 else
109                 {
110                         ext.set(channel, new HistoryList(len, time, parameter));
111                 }
112                 return MODEACTION_ALLOW;
113         }
114
115         void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
116         {
117                 out.append(history->param);
118         }
119 };
120
121 class ModuleChanHistory : public Module
122 {
123         HistoryMode m;
124         bool sendnotice;
125         UserModeReference botmode;
126         bool dobots;
127         IRCv3::Batch::CapReference batchcap;
128         IRCv3::Batch::API batchmanager;
129         IRCv3::Batch::Batch batch;
130         IRCv3::ServerTime::API servertimemanager;
131
132  public:
133         ModuleChanHistory()
134                 : m(this)
135                 , botmode(this, "bot")
136                 , batchcap(this)
137                 , batchmanager(this)
138                 , batch("chathistory")
139                 , servertimemanager(this)
140         {
141         }
142
143         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
144         {
145                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
146                 m.maxlines = tag->getUInt("maxlines", 50, 1);
147                 sendnotice = tag->getBool("notice", true);
148                 dobots = tag->getBool("bots", true);
149         }
150
151         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
152         {
153                 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
154                 {
155                         Channel* c = target.Get<Channel>();
156                         HistoryList* list = m.ext.get(c);
157                         if (list)
158                         {
159                                 list->lines.push_back(HistoryItem(user, details.text));
160                                 if (list->lines.size() > list->maxlen)
161                                         list->lines.pop_front();
162                         }
163                 }
164         }
165
166         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
167         {
168                 LocalUser* localuser = IS_LOCAL(memb->user);
169                 if (!localuser)
170                         return;
171
172                 if (memb->user->IsModeSet(botmode) && !dobots)
173                         return;
174
175                 HistoryList* list = m.ext.get(memb->chan);
176                 if (!list)
177                         return;
178                 time_t mintime = 0;
179                 if (list->maxtime)
180                         mintime = ServerInstance->Time() - list->maxtime;
181
182                 if ((sendnotice) && (!batchcap.get(localuser)))
183                 {
184                         std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
185                         if (list->maxtime > 0)
186                                 message.append(" spanning up to " + ConvToStr(list->maxtime) + " seconds");
187                         memb->WriteNotice(message);
188                 }
189
190                 if (batchmanager)
191                 {
192                         batchmanager->Start(batch);
193                         batch.GetBatchStartMessage().PushParamRef(memb->chan->name);
194                 }
195
196                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
197                 {
198                         const HistoryItem& item = *i;
199                         if (item.ts >= mintime)
200                         {
201                                 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, memb->chan, item.text);
202                                 if (servertimemanager)
203                                         servertimemanager->Set(msg, item.ts);
204                                 batch.AddToBatch(msg);
205                                 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
206                         }
207                 }
208
209                 if (batchmanager)
210                         batchmanager->End(batch);
211         }
212
213         Version GetVersion() CXX11_OVERRIDE
214         {
215                 return Version("Provides channel history replayed on join", VF_VENDOR);
216         }
217 };
218
219 MODULE_INIT(ModuleChanHistory)