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