]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delaymsg.cpp
m_ssl_openssl Store a pointer to the OpenSSLIOHook object in SSL objects
[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", 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  public:
51         ModuleDelayMsg() : djm(this)
52         {
53         }
54
55         Version GetVersion() CXX11_OVERRIDE;
56         void OnUserJoin(Membership* memb, bool sync, bool created, CUList&) CXX11_OVERRIDE;
57         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE;
58 };
59
60 ModeAction DelayMsgMode::OnSet(User* source, Channel* chan, std::string& parameter)
61 {
62         // Setting a new limit, sanity check
63         unsigned int limit = ConvToInt(parameter);
64         if (limit == 0)
65                 limit = 1;
66
67         ext.set(chan, limit);
68         return MODEACTION_ALLOW;
69 }
70
71 void DelayMsgMode::OnUnset(User* source, Channel* chan)
72 {
73         /*
74          * Clean up metadata
75          */
76         const Channel::MemberMap& users = chan->GetUsers();
77         for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
78                 jointime.set(n->second, 0);
79 }
80
81 Version ModuleDelayMsg::GetVersion()
82 {
83         return Version("Provides channelmode +d <int>, to deny messages to a channel until <int> seconds.", VF_VENDOR);
84 }
85
86 void ModuleDelayMsg::OnUserJoin(Membership* memb, bool sync, bool created, CUList&)
87 {
88         if ((IS_LOCAL(memb->user)) && (memb->chan->IsModeSet(djm)))
89         {
90                 djm.jointime.set(memb, ServerInstance->Time());
91         }
92 }
93
94 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype)
95 {
96         /* Server origin */
97         if ((!user) || (!IS_LOCAL(user)))
98                 return MOD_RES_PASSTHRU;
99
100         if ((target_type != TYPE_CHANNEL) || (msgtype != MSG_PRIVMSG))
101                 return MOD_RES_PASSTHRU;
102
103         Channel* channel = (Channel*) dest;
104         Membership* memb = channel->GetUser(user);
105
106         if (!memb)
107                 return MOD_RES_PASSTHRU;
108
109         time_t ts = djm.jointime.get(memb);
110
111         if (ts == 0)
112                 return MOD_RES_PASSTHRU;
113
114         int len = djm.ext.get(channel);
115
116         if ((ts + len) > ServerInstance->Time())
117         {
118                 if (channel->GetPrefixValue(user) < VOICE_VALUE)
119                 {
120                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s :You must wait %d seconds after joining to send to channel (+d)",
121                                 channel->name.c_str(), 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 MODULE_INIT(ModuleDelayMsg)