]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
Only send ACCOUNT and CHGHOST to clients that have sent NICK/USER.
[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 ServerProtocol::BroadcastEventListener
116 {
117  private:
118         HistoryMode m;
119         bool sendnotice;
120         UserModeReference botmode;
121         bool dobots;
122         IRCv3::Batch::CapReference batchcap;
123         IRCv3::Batch::API batchmanager;
124         IRCv3::Batch::Batch batch;
125         IRCv3::ServerTime::API servertimemanager;
126
127         void SendHistory(LocalUser* user, Channel* channel, HistoryList* list, time_t mintime)
128         {
129                 if (batchmanager)
130                 {
131                         batchmanager->Start(batch);
132                         batch.GetBatchStartMessage().PushParamRef(channel->name);
133                 }
134
135                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
136                 {
137                         const HistoryItem& item = *i;
138                         if (item.ts >= mintime)
139                         {
140                                 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, item.sourcemask, channel, item.text);
141                                 if (servertimemanager)
142                                         servertimemanager->Set(msg, item.ts);
143                                 batch.AddToBatch(msg);
144                                 user->Send(ServerInstance->GetRFCEvents().privmsg, msg);
145                         }
146                 }
147
148                 if (batchmanager)
149                         batchmanager->End(batch);
150         }
151
152  public:
153         ModuleChanHistory()
154                 : ServerProtocol::BroadcastEventListener(this)
155                 , m(this)
156                 , botmode(this, "bot")
157                 , batchcap(this)
158                 , batchmanager(this)
159                 , batch("chathistory")
160                 , servertimemanager(this)
161         {
162         }
163
164         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
165         {
166                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
167                 m.maxlines = tag->getUInt("maxlines", 50, 1);
168                 sendnotice = tag->getBool("notice", true);
169                 dobots = tag->getBool("bots", true);
170         }
171
172         ModResult OnBroadcastMessage(Channel* channel, const Server* server) CXX11_OVERRIDE
173         {
174                 return channel->IsModeSet(m) ? MOD_RES_ALLOW : MOD_RES_PASSTHRU;
175         }
176
177         void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
178         {
179                 if ((target.type == MessageTarget::TYPE_CHANNEL) && (target.status == 0) && (details.type == MSG_PRIVMSG))
180                 {
181                         Channel* c = target.Get<Channel>();
182                         HistoryList* list = m.ext.get(c);
183                         if (list)
184                         {
185                                 list->lines.push_back(HistoryItem(user, details.text));
186                                 if (list->lines.size() > list->maxlen)
187                                         list->lines.pop_front();
188                         }
189                 }
190         }
191
192         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
193         {
194                 LocalUser* localuser = IS_LOCAL(memb->user);
195                 if (!localuser)
196                         return;
197
198                 if (memb->user->IsModeSet(botmode) && !dobots)
199                         return;
200
201                 HistoryList* list = m.ext.get(memb->chan);
202                 if (!list)
203                         return;
204
205                 if ((sendnotice) && (!batchcap.get(localuser)))
206                 {
207                         std::string message("Replaying up to " + ConvToStr(list->maxlen) + " lines of pre-join history");
208                         if (list->maxtime > 0)
209                                 message.append(" spanning up to " + InspIRCd::DurationString(list->maxtime));
210                         memb->WriteNotice(message);
211                 }
212
213                 time_t mintime = 0;
214                 if (list->maxtime)
215                         mintime = ServerInstance->Time() - list->maxtime;
216
217                 SendHistory(localuser, memb->chan, list, mintime);
218         }
219
220         Version GetVersion() CXX11_OVERRIDE
221         {
222                 return Version("Provides channel mode +H, allows for the channel message history to be replayed on join", VF_VENDOR);
223         }
224 };
225
226 MODULE_INIT(ModuleChanHistory)