]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delaymsg.cpp
Merge pull request #241 from attilamolnar/insp20+delaymsgfix
[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                 if (!ServerInstance->Modes->AddMode(&djm))
50                         throw ModuleException("Could not add new modes!");
51                 ServerInstance->Extensions.Register(&djm.jointime);
52                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPreMessage};
53                 ServerInstance->Modules->Attach(eventlist, this, 2);
54         }
55         Version GetVersion();
56         void OnUserJoin(Membership* memb, bool sync, bool created, CUList&);
57         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list);
58 };
59
60 ModeAction DelayMsgMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
61 {
62         if (adding)
63         {
64                 if ((channel->IsModeSet('d')) && (channel->GetModeParameter('d') == parameter))
65                         return MODEACTION_DENY;
66
67                 /* Setting a new limit, sanity check */
68                 long limit = atoi(parameter.c_str());
69
70                 /* Wrap low values at 32768 */
71                 if (limit < 0)
72                         limit = 0x7FFF;
73
74                 parameter = ConvToStr(limit);
75         }
76         else
77         {
78                 if (!channel->IsModeSet('d'))
79                         return MODEACTION_DENY;
80
81                 /*
82                  * Clean up metadata
83                  */
84                 const UserMembList* names = channel->GetUsers();
85                 for (UserMembCIter n = names->begin(); n != names->end(); ++n)
86                         jointime.set(n->second, 0);
87         }
88         channel->SetModeParam('d', adding ? parameter : "");
89         return MODEACTION_ALLOW;
90 }
91
92 Version ModuleDelayMsg::GetVersion()
93 {
94         return Version("Provides channelmode +d <int>, to deny messages to a channel until <int> seconds.", VF_VENDOR);
95 }
96
97 void ModuleDelayMsg::OnUserJoin(Membership* memb, bool sync, bool created, CUList&)
98 {
99         if ((IS_LOCAL(memb->user)) && (memb->chan->IsModeSet('d')))
100         {
101                 djm.jointime.set(memb, ServerInstance->Time());
102         }
103 }
104
105 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
106 {
107         /* Server origin */
108         if ((!user) || (!IS_LOCAL(user)))
109                 return MOD_RES_PASSTHRU;
110
111         if (target_type != TYPE_CHANNEL)
112                 return MOD_RES_PASSTHRU;
113
114         Channel* channel = (Channel*) dest;
115         Membership* memb = channel->GetUser(user);
116
117         if (!memb)
118                 return MOD_RES_PASSTHRU;
119
120         time_t ts = djm.jointime.get(memb);
121
122         if (ts == 0)
123                 return MOD_RES_PASSTHRU;
124
125         std::string len = channel->GetModeParameter('d');
126
127         if (ts + atoi(len.c_str()) > ServerInstance->Time())
128         {
129                 if (channel->GetPrefixValue(user) < VOICE_VALUE)
130                 {
131                         user->WriteNumeric(404, "%s %s :You must wait %s seconds after joining to send to channel (+d)",
132                                 user->nick.c_str(), channel->name.c_str(), len.c_str());
133                         return MOD_RES_DENY;
134                 }
135         }
136         else
137         {
138                 /* Timer has expired, we can stop checking now */
139                 djm.jointime.set(memb, 0);
140         }
141         return MOD_RES_PASSTHRU;
142 }
143
144 MODULE_INIT(ModuleDelayMsg)
145