]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Fix linking servers that are using the nationalchars module.
[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 typedef insp::flat_map<std::string, std::string> HistoryTagMap;
26
27 struct HistoryItem
28 {
29         time_t ts;
30         std::string text;
31         MessageType type;
32         HistoryTagMap tags;
33         std::string sourcemask;
34
35         HistoryItem(User* source, const MessageDetails& details)
36                 : ts(ServerInstance->Time())
37                 , text(details.text)
38                 , type(details.type)
39                 , sourcemask(source->GetFullHost())
40         {
41                 tags.reserve(details.tags_out.size());
42                 for (ClientProtocol::TagMap::const_iterator iter = details.tags_out.begin(); iter != details.tags_out.end(); ++iter)
43                         tags[iter->first] = iter->second.value;
44         }
45 };
46
47 struct HistoryList
48 {
49         std::deque<HistoryItem> lines;
50         unsigned int maxlen;
51         unsigned int maxtime;
52
53         HistoryList(unsigned int len, unsigned int time)
54                 : maxlen(len)
55                 , maxtime(time)
56         {
57         }
58 };
59
60 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
61 {
62  public:
63         unsigned int maxlines;
64         HistoryMode(Module* Creator)
65                 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
66         {
67                 syntax = "<max-messages>:<max-duration>";
68         }
69
70         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
71         {
72                 std::string::size_type colon = parameter.find(':');
73                 if (colon == std::string::npos)
74                 {
75                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
76                         return MODEACTION_DENY;
77                 }
78
79                 std::string duration(parameter, colon+1);
80                 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
81                 {
82                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
83                         return MODEACTION_DENY;
84                 }
85
86                 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
87                 unsigned long time;
88                 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
89                 {
90                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
91                         return MODEACTION_DENY;
92                 }
93                 if (len > maxlines)
94                         len = maxlines;
95
96                 HistoryList* history = ext.get(channel);
97                 if (history)
98                 {
99                         // Shrink the list if the new line number limit is lower than the old one
100                         if (len < history->lines.size())
101                                 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
102
103                         history->maxlen = len;
104                         history->maxtime = time;
105                 }
106                 else
107                 {
108                         ext.set(channel, new HistoryList(len, time));
109                 }
110                 return MODEACTION_ALLOW;
111         }
112
113         void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
114         {
115                 out.append(ConvToStr(history->maxlen));
116                 out.append(":");
117                 out.append(InspIRCd::DurationString(history->maxtime));
118         }
119 };
120
121 class ModuleChanHistory
122         : public Module
123         , public ServerProtocol::BroadcastEventListener
124 {
125  private:
126         HistoryMode m;
127         bool prefixmsg;
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         ClientProtocol::MessageTagEvent tagevent;
135
136         void AddTag(ClientProtocol::Message& msg, const std::string& tagkey, std::string& tagval)
137         {
138                 const Events::ModuleEventProvider::SubscriberList& list = tagevent.GetSubscribers();
139                 for (Events::ModuleEventProvider::SubscriberList::const_iterator i = list.begin(); i != list.end(); ++i)
140                 {
141                         ClientProtocol::MessageTagProvider* const tagprov = static_cast<ClientProtocol::MessageTagProvider*>(*i);
142                         const ModResult res = tagprov->OnProcessTag(ServerInstance->FakeClient, tagkey, tagval);
143                         if (res == MOD_RES_ALLOW)
144                                 msg.AddTag(tagkey, tagprov, tagval);
145                         else if (res == MOD_RES_DENY)
146                                 break;
147                 }
148         }
149
150         void SendHistory(LocalUser* user, Channel* channel, HistoryList* list, time_t mintime)
151         {
152                 if (batchmanager)
153                 {
154                         batchmanager->Start(batch);
155                         batch.GetBatchStartMessage().PushParamRef(channel->name);
156                 }
157
158                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
159                 {
160                         HistoryItem& item = *i;
161                         if (item.ts >= mintime)
162                         {
163                                 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, channel, item.text, item.type);
164                                 for (HistoryTagMap::iterator iter = item.tags.begin(); iter != item.tags.end(); ++iter)
165                                         AddTag(msg, iter->first, iter->second);
166                                 if (servertimemanager)
167                                         servertimemanager->Set(msg, item.ts);
168                                 batch.AddToBatch(msg);
169                                 user->Send(ServerInstance->GetRFCEvents().privmsg, msg);
170                         }
171                 }
172
173                 if (batchmanager)
174                         batchmanager->End(batch);
175         }
176
177  public:
178         ModuleChanHistory()
179                 : ServerProtocol::BroadcastEventListener(this)
180                 , m(this)
181                 , botmode(this, "bot")
182                 , batchcap(this)
183                 , batchmanager(this)
184                 , batch("chathistory")
185                 , servertimemanager(this)
186                 , tagevent(this)
187         {
188         }
189
190         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
191         {
192                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
193                 m.maxlines = tag->getUInt("maxlines", 50, 1);
194                 prefixmsg = tag->getBool("prefixmsg", tag->getBool("notice", true));
195                 dobots = tag->getBool("bots", true);
196         }
197
198         ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
199         {
200                 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
201         }
202
203         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
204         {
205                 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && !details.IsCTCP())
206                 {
207                         Channel* c = target.Get<Channel>();
208                         HistoryList* list = m.ext.get(c);
209                         if (list)
210                         {
211                                 list->lines.push_back(HistoryItem(user, details));
212                                 if (list->lines.size() > list->maxlen)
213                                         list->lines.pop_front();
214                         }
215                 }
216         }
217
218         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
219         {
220                 LocalUser* localuser = IS_LOCAL(memb->user);
221                 if (!localuser)
222                         return;
223
224                 if (memb->user->IsModeSet(botmode) && !dobots)
225                         return;
226
227                 HistoryList* list = m.ext.get(memb->chan);
228                 if (!list)
229                         return;
230
231                 if ((prefixmsg) && (!batchcap.get(localuser)))
232                 {
233                         std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
234                         if (list->maxtime > 0)
235                                 message.append(" from the last " + InspIRCd::DurationString(list->maxtime));
236                         memb->WriteNotice(message);
237                 }
238
239                 time_t mintime = 0;
240                 if (list->maxtime)
241                         mintime = ServerInstance->Time() - list->maxtime;
242
243                 SendHistory(localuser, memb->chan, list, mintime);
244         }
245
246         Version GetVersion() CXX11_OVERRIDE
247         {
248                 return Version("Provides channel mode +H, allows for the channel message history to be replayed on join", VF_VENDOR);
249         }
250 };
251
252 MODULE_INIT(ModuleChanHistory)