]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanhistory.cpp
m_chanhistory Don't allow durations that contain an invalid char or too long
[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 /* $ModDesc: Provides channel history for a given number of lines */
23
24 struct HistoryItem
25 {
26         time_t ts;
27         std::string line;
28         HistoryItem(const std::string& Line) : ts(ServerInstance->Time()), line(Line) {}
29 };
30
31 struct HistoryList
32 {
33         std::deque<HistoryItem> lines;
34         unsigned int maxlen, maxtime;
35         HistoryList(unsigned int len, unsigned int time) : maxlen(len), maxtime(time) {}
36 };
37
38 class HistoryMode : public ModeHandler
39 {
40         bool IsValidDuration(const std::string duration)
41         {
42                 for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
43                 {
44                         unsigned char c = *i;
45                         if (((c >= '0') && (c <= '9')) || (c == 's') || (c != 'S'))
46                                 continue;
47
48                         if (duration_multi[c] == 1)
49                                 return false;
50                 }
51                 return true;
52         }
53
54  public:
55         SimpleExtItem<HistoryList> ext;
56         unsigned int maxlines;
57         HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
58                 ext("history", Creator) { }
59
60         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
61         {
62                 if (adding)
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.substr(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                         int time = ServerInstance->Duration(duration);
74                         if (len == 0 || time < 0)
75                                 return MODEACTION_DENY;
76                         if (len > maxlines && IS_LOCAL(source))
77                                 return MODEACTION_DENY;
78                         if (len > maxlines)
79                                 len = maxlines;
80                         if (parameter == channel->GetModeParameter(this))
81                                 return MODEACTION_DENY;
82                         ext.set(channel, new HistoryList(len, time));
83                         channel->SetModeParam('H', parameter);
84                 }
85                 else
86                 {
87                         if (!channel->IsModeSet('H'))
88                                 return MODEACTION_DENY;
89                         ext.unset(channel);
90                         channel->SetModeParam('H', "");
91                 }
92                 return MODEACTION_ALLOW;
93         }
94 };
95
96 class ModuleChanHistory : public Module
97 {
98         HistoryMode m;
99         bool sendnotice;
100  public:
101         ModuleChanHistory() : m(this)
102         {
103         }
104
105         void init()
106         {
107                 ServerInstance->Modules->AddService(m);
108
109                 Implementation eventlist[] = { I_OnPostJoin, I_OnUserMessage, I_OnRehash };
110                 ServerInstance->Modules->Attach(eventlist, this, 3);
111                 OnRehash(NULL);
112         }
113
114         void OnRehash(User*)
115         {
116                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanhistory");
117                 m.maxlines = tag->getInt("maxlines", 50);
118                 sendnotice = tag->getInt("notice", true);
119         }
120
121         ~ModuleChanHistory()
122         {
123                 ServerInstance->Modes->DelMode(&m);
124         }
125
126         void OnUserMessage(User* user,void* dest,int target_type, const std::string &text, char status, const CUList&)
127         {
128                 if (target_type == TYPE_CHANNEL && status == 0)
129                 {
130                         Channel* c = (Channel*)dest;
131                         HistoryList* list = m.ext.get(c);
132                         if (list)
133                         {
134                                 char buf[MAXBUF];
135                                 snprintf(buf, MAXBUF, ":%s PRIVMSG %s :%s",
136                                         user->GetFullHost().c_str(), c->name.c_str(), text.c_str());
137                                 list->lines.push_back(HistoryItem(buf));
138                                 if (list->lines.size() > list->maxlen)
139                                         list->lines.pop_front();
140                         }
141                 }
142         }
143
144         void OnPostJoin(Membership* memb)
145         {
146                 if (IS_REMOTE(memb->user))
147                         return;
148
149                 HistoryList* list = m.ext.get(memb->chan);
150                 if (!list)
151                         return;
152                 time_t mintime = 0;
153                 if (list->maxtime)
154                         mintime = ServerInstance->Time() - list->maxtime;
155
156                 if (sendnotice)
157                 {
158                         memb->user->WriteServ("NOTICE %s :Replaying up to %d lines of pre-join history spanning up to %d seconds",
159                                 memb->chan->name.c_str(), list->maxlen, list->maxtime);
160                 }
161
162                 for(std::deque<HistoryItem>::iterator i = list->lines.begin(); i != list->lines.end(); ++i)
163                 {
164                         if (i->ts >= mintime)
165                                 memb->user->Write(i->line);
166                 }
167         }
168
169         Version GetVersion()
170         {
171                 return Version("Provides channel history replayed on join", VF_VENDOR);
172         }
173 };
174
175 MODULE_INIT(ModuleChanHistory)