]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Use DurationString() in the 'Replaying ...' message.
[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;
43         unsigned int maxtime;
44
45         HistoryList(unsigned int len, unsigned int time)
46                 : maxlen(len)
47                 , maxtime(time)
48         {
49         }
50 };
51
52 class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
53 {
54  public:
55         unsigned int maxlines;
56         HistoryMode(Module* Creator)
57                 : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
58         {
59                 syntax = "<max-messages>:<max-duration>";
60         }
61
62         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
63         {
64                 std::string::size_type colon = parameter.find(':');
65                 if (colon == std::string::npos)
66                 {
67                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
68                         return MODEACTION_DENY;
69                 }
70
71                 std::string duration(parameter, colon+1);
72                 if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
73                 {
74                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
75                         return MODEACTION_DENY;
76                 }
77
78                 unsigned int len = ConvToNum<unsigned int>(parameter.substr(0, colon));
79                 unsigned long time;
80                 if (!InspIRCd::Duration(duration, time) || len == 0 || (len > maxlines && IS_LOCAL(source)))
81                 {
82                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
83                         return MODEACTION_DENY;
84                 }
85                 if (len > maxlines)
86                         len = maxlines;
87
88                 HistoryList* history = ext.get(channel);
89                 if (history)
90                 {
91                         // Shrink the list if the new line number limit is lower than the old one
92                         if (len < history->lines.size())
93                                 history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
94
95                         history->maxlen = len;
96                         history->maxtime = time;
97                 }
98                 else
99                 {
100                         ext.set(channel, new HistoryList(len, time));
101                 }
102                 return MODEACTION_ALLOW;
103         }
104
105         void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
106         {
107                 out.append(ConvToStr(history->maxlen));
108                 out.append(":");
109                 out.append(InspIRCd::DurationString(history->maxtime));
110         }
111 };
112
113 class ModuleChanHistory
114         : public Module
115         , public ServerEventListener
116 {
117         HistoryMode m;
118         bool sendnotice;
119         UserModeReference botmode;
120         bool dobots;
121         IRCv3::Batch::CapReference batchcap;
122         IRCv3::Batch::API batchmanager;
123         IRCv3::Batch::Batch batch;
124         IRCv3::ServerTime::API servertimemanager;
125
126  public:
127         ModuleChanHistory()
128                 : ServerEventListener(this)
129                 , m(this)
130                 , botmode(this, "bot")
131                 , batchcap(this)
132                 , batchmanager(this)
133                 , batch("chathistory")
134                 , servertimemanager(this)
135         {
136         }
137
138         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
139         {
140                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
141                 m.maxlines = tag->getUInt("maxlines", 50, 1);
142                 sendnotice = tag->getBool("notice", true);
143                 dobots = tag->getBool("bots", true);
144         }
145
146         ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
147         {
148                 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
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 " + InspIRCd::DurationString(list->maxtime));
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 mode +H, allows for the channel message history to be replayed on join", VF_VENDOR);
216         }
217 };
218
219 MODULE_INIT(ModuleChanHistory)