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