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