]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delaymsg.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[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 class DelayMsgMode : public ParamMode<DelayMsgMode, LocalIntExt>
23 {
24  public:
25         LocalIntExt jointime;
26         DelayMsgMode(Module* Parent)
27                 : ParamMode<DelayMsgMode, LocalIntExt>(Parent, "delaymsg", 'd')
28                 , jointime("delaymsg", ExtensionItem::EXT_MEMBERSHIP, Parent)
29         {
30                 levelrequired = OP_VALUE;
31         }
32
33         bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel*)
34         {
35                 return (atoi(their_param.c_str()) < atoi(our_param.c_str()));
36         }
37
38         ModeAction OnSet(User* source, Channel* chan, std::string& parameter);
39         void OnUnset(User* source, Channel* chan);
40
41         void SerializeParam(Channel* chan, int n, std::string& out)
42         {
43                 out += ConvToStr(n);
44         }
45 };
46
47 class ModuleDelayMsg : public Module
48 {
49         DelayMsgMode djm;
50         bool allownotice;
51  public:
52         ModuleDelayMsg() : djm(this)
53         {
54         }
55
56         Version GetVersion() CXX11_OVERRIDE;
57         void OnUserJoin(Membership* memb, bool sync, bool created, CUList&) CXX11_OVERRIDE;
58         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE;
59         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
60 };
61
62 ModeAction DelayMsgMode::OnSet(User* source, Channel* chan, std::string& parameter)
63 {
64         // Setting a new limit, sanity check
65         unsigned int limit = ConvToInt(parameter);
66         if (limit == 0)
67                 limit = 1;
68
69         ext.set(chan, limit);
70         return MODEACTION_ALLOW;
71 }
72
73 void DelayMsgMode::OnUnset(User* source, Channel* chan)
74 {
75         /*
76          * Clean up metadata
77          */
78         const Channel::MemberMap& users = chan->GetUsers();
79         for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
80                 jointime.set(n->second, 0);
81 }
82
83 Version ModuleDelayMsg::GetVersion()
84 {
85         return Version("Provides channelmode +d <int>, to deny messages to a channel until <int> seconds.", VF_VENDOR);
86 }
87
88 void ModuleDelayMsg::OnUserJoin(Membership* memb, bool sync, bool created, CUList&)
89 {
90         if ((IS_LOCAL(memb->user)) && (memb->chan->IsModeSet(djm)))
91         {
92                 djm.jointime.set(memb, ServerInstance->Time());
93         }
94 }
95
96 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype)
97 {
98         if (!IS_LOCAL(user))
99                 return MOD_RES_PASSTHRU;
100
101         if ((target_type != TYPE_CHANNEL) || ((!allownotice) && (msgtype == MSG_NOTICE)))
102                 return MOD_RES_PASSTHRU;
103
104         Channel* channel = (Channel*) dest;
105         Membership* memb = channel->GetUser(user);
106
107         if (!memb)
108                 return MOD_RES_PASSTHRU;
109
110         time_t ts = djm.jointime.get(memb);
111
112         if (ts == 0)
113                 return MOD_RES_PASSTHRU;
114
115         int len = djm.ext.get(channel);
116
117         if ((ts + len) > ServerInstance->Time())
118         {
119                 if (channel->GetPrefixValue(user) < VOICE_VALUE)
120                 {
121                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, channel->name, InspIRCd::Format("You must wait %d seconds after joining to send to channel (+d)", len));
122                         return MOD_RES_DENY;
123                 }
124         }
125         else
126         {
127                 /* Timer has expired, we can stop checking now */
128                 djm.jointime.set(memb, 0);
129         }
130         return MOD_RES_PASSTHRU;
131 }
132
133 void ModuleDelayMsg::ReadConfig(ConfigStatus& status)
134 {
135         ConfigTag* tag = ServerInstance->Config->ConfValue("delaymsg");
136         allownotice = tag->getBool("allownotice", true);
137 }
138
139 MODULE_INIT(ModuleDelayMsg)