]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delaymsg.cpp
5e2bb838cce057f7d5032a9e86c25877b121195a
[user/henk/code/inspircd.git] / src / modules / m_delaymsg.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <stdarg.h>
16
17 /* $ModDesc: Provides channelmode +d <int>, to deny messages to a channel until <int> seconds. */
18
19 class DelayMsgMode : public ModeHandler
20 {
21  private:
22         CUList empty;
23  public:
24         LocalIntExt jointime;
25         DelayMsgMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, Parent, 'd', 1, 0, false, MODETYPE_CHANNEL, false, 0, '@'), jointime("delaymsg", Parent) {};
26
27         ModePair ModeSet(User*, User*, Channel* channel, const std::string &parameter)
28         {
29                 std::string climit = channel->GetModeParameter('d');
30                 if (!climit.empty())
31                 {
32                         return std::make_pair(true, climit);
33                 }
34                 else
35                 {
36                         return std::make_pair(false, parameter);
37                 }
38         }
39
40         bool CheckTimeStamp(std::string &their_param, const std::string &our_param, Channel*)
41         {
42                 return (atoi(their_param.c_str()) < atoi(our_param.c_str()));
43         }
44
45         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
46 };
47
48 class ModuleDelayMsg : public Module
49 {
50  private:
51         DelayMsgMode djm;
52  public:
53         ModuleDelayMsg(InspIRCd* Me) : Module(Me), djm(Me, this)
54         {
55                 if (!ServerInstance->Modes->AddMode(&djm))
56                         throw ModuleException("Could not add new modes!");
57                 Extensible::Register(&djm.jointime);
58                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPreMessage};
59                 ServerInstance->Modules->Attach(eventlist, this, 2);
60         }
61         ~ModuleDelayMsg();
62         Version GetVersion();
63         void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created);
64         void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
65         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent);
66         void OnCleanup(int target_type, void* item);
67         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list);
68 };
69
70 ModeAction DelayMsgMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
71 {
72         if (adding)
73         {
74                 /* Setting a new limit, sanity check */
75                 long limit = atoi(parameter.c_str());
76
77                 /* Wrap low values at 32768 */
78                 if (limit < 0)
79                         limit = 0x7FFF;
80
81                 parameter = ConvToStr(limit);
82         }
83         else
84         {
85                 /*
86                  * Clean up metadata
87                  */
88                 const UserMembList* names = channel->GetUsers();
89                 for (UserMembCIter n = names->begin(); n != names->end(); ++n)
90                         jointime.set(n->second, 0);
91         }
92         channel->SetModeParam('d', adding ? parameter : "");
93         return MODEACTION_ALLOW;
94 }
95
96 ModuleDelayMsg::~ModuleDelayMsg()
97 {
98         ServerInstance->Modes->DelMode(&djm);
99 }
100
101 Version ModuleDelayMsg::GetVersion()
102 {
103         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
104 }
105
106 void ModuleDelayMsg::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created)
107 {
108         if (channel->IsModeSet('d'))
109         {
110                 Membership* memb = channel->GetUser(user);
111                 djm.jointime.set(memb, ServerInstance->Time());
112         }
113 }
114
115 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
116 {
117         /* Server origin */
118         if (!user)
119                 return MOD_RES_PASSTHRU;
120
121         if (target_type != TYPE_CHANNEL)
122                 return MOD_RES_PASSTHRU;
123
124         Channel* channel = (Channel*) dest;
125         Membership* memb = channel->GetUser(user);
126
127         if (!memb)
128                 return MOD_RES_PASSTHRU;
129         
130         time_t ts = djm.jointime.get(memb);
131
132         if (ts == 0)
133                 return MOD_RES_PASSTHRU;
134
135         std::string len = channel->GetModeParameter('d');
136
137         if (ts + atoi(len.c_str()) > ServerInstance->Time())
138         {
139                 if (channel->GetPrefixValue(user) < VOICE_VALUE)
140                 {
141                         user->WriteNumeric(404, "%s %s :You must wait %s seconds after joining to send to channel (+d)",
142                                 user->nick.c_str(), channel->name.c_str(), len.c_str());
143                         return MOD_RES_DENY;
144                 }
145         }
146         else
147         {
148                 /* Timer has expired, we can stop checking now */
149                 djm.jointime.set(memb, 0);
150         }
151         return MOD_RES_PASSTHRU;
152 }
153
154 MODULE_INIT(ModuleDelayMsg)
155