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