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