]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delaymsg.cpp
Release v2.0.18
[user/henk/code/inspircd.git] / src / modules / m_delaymsg.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 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 channelmode +d <int>, to deny messages to a channel until <int> seconds. */
23
24 class DelayMsgMode : public ModeHandler
25 {
26  public:
27         LocalIntExt jointime;
28         DelayMsgMode(Module* Parent) : ModeHandler(Parent, "delaymsg", 'd', PARAM_SETONLY, MODETYPE_CHANNEL)
29                 , jointime("delaymsg", Parent)
30         {
31                 levelrequired = OP_VALUE;
32         }
33
34         bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel*)
35         {
36                 return (atoi(their_param.c_str()) < atoi(our_param.c_str()));
37         }
38
39         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
40 };
41
42 class ModuleDelayMsg : public Module
43 {
44  private:
45         DelayMsgMode djm;
46  public:
47         ModuleDelayMsg() : djm(this)
48         {
49         }
50
51         void init()
52         {
53                 ServerInstance->Modules->AddService(djm);
54                 ServerInstance->Modules->AddService(djm.jointime);
55                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPreMessage, I_OnRehash };
56                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
57                 OnRehash(NULL);
58         }
59         Version GetVersion();
60         void OnUserJoin(Membership* memb, bool sync, bool created, CUList&);
61         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list);
62         ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list);
63         void OnRehash(User* user);
64 };
65
66 ModeAction DelayMsgMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
67 {
68         if (adding)
69         {
70                 if ((channel->IsModeSet('d')) && (channel->GetModeParameter('d') == parameter))
71                         return MODEACTION_DENY;
72
73                 /* Setting a new limit, sanity check */
74                 long limit = atoi(parameter.c_str());
75
76                 /* Wrap low values at 32768 */
77                 if (limit < 0)
78                         limit = 0x7FFF;
79
80                 parameter = ConvToStr(limit);
81         }
82         else
83         {
84                 if (!channel->IsModeSet('d'))
85                         return MODEACTION_DENY;
86
87                 /*
88                  * Clean up metadata
89                  */
90                 const UserMembList* names = channel->GetUsers();
91                 for (UserMembCIter n = names->begin(); n != names->end(); ++n)
92                         jointime.set(n->second, 0);
93         }
94         channel->SetModeParam('d', adding ? parameter : "");
95         return MODEACTION_ALLOW;
96 }
97
98 Version ModuleDelayMsg::GetVersion()
99 {
100         return Version("Provides channelmode +d <int>, to deny messages to a channel until <int> seconds.", VF_VENDOR);
101 }
102
103 void ModuleDelayMsg::OnUserJoin(Membership* memb, bool sync, bool created, CUList&)
104 {
105         if ((IS_LOCAL(memb->user)) && (memb->chan->IsModeSet('d')))
106         {
107                 djm.jointime.set(memb, ServerInstance->Time());
108         }
109 }
110
111 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
112 {
113         /* Server origin */
114         if ((!user) || (!IS_LOCAL(user)))
115                 return MOD_RES_PASSTHRU;
116
117         if (target_type != TYPE_CHANNEL)
118                 return MOD_RES_PASSTHRU;
119
120         Channel* channel = (Channel*) dest;
121         Membership* memb = channel->GetUser(user);
122
123         if (!memb)
124                 return MOD_RES_PASSTHRU;
125
126         time_t ts = djm.jointime.get(memb);
127
128         if (ts == 0)
129                 return MOD_RES_PASSTHRU;
130
131         std::string len = channel->GetModeParameter('d');
132
133         if (ts + atoi(len.c_str()) > ServerInstance->Time())
134         {
135                 if (channel->GetPrefixValue(user) < VOICE_VALUE)
136                 {
137                         user->WriteNumeric(404, "%s %s :You must wait %s seconds after joining to send to channel (+d)",
138                                 user->nick.c_str(), channel->name.c_str(), len.c_str());
139                         return MOD_RES_DENY;
140                 }
141         }
142         else
143         {
144                 /* Timer has expired, we can stop checking now */
145                 djm.jointime.set(memb, 0);
146         }
147         return MOD_RES_PASSTHRU;
148 }
149
150 ModResult ModuleDelayMsg::OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list)
151 {
152         return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
153 }
154
155 void ModuleDelayMsg::OnRehash(User* user)
156 {
157         ConfigTag* tag = ServerInstance->Config->ConfValue("delaymsg");
158         if (tag->getBool("allownotice", true))
159                 ServerInstance->Modules->Detach(I_OnUserPreNotice, this);
160         else
161                 ServerInstance->Modules->Attach(I_OnUserPreNotice, this);
162 }
163
164 MODULE_INIT(ModuleDelayMsg)
165