]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delaymsg.cpp
Add initial query support to m_mysql [patch by Athenon]
[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 class DelayMsgMode : public ModeHandler
18 {
19  private:
20         CUList empty;
21  public:
22         DelayMsgMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, Parent, 'd', 1, 0, false, MODETYPE_CHANNEL, false, 0, '@') {};
23
24         ModePair ModeSet(User*, User*, Channel* channel, const std::string &parameter)
25         {
26                 std::string climit = channel->GetModeParameter('d');
27                 if (!climit.empty())
28                 {
29                         return std::make_pair(true, climit);
30                 }
31                 else
32                 {
33                         return std::make_pair(false, parameter);
34                 }
35         }
36
37         bool CheckTimeStamp(std::string &their_param, const std::string &our_param, Channel*)
38         {
39                 return (atoi(their_param.c_str()) < atoi(our_param.c_str()));
40         }
41
42         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
43 };
44
45 class ModuleDelayMsg : public Module
46 {
47  private:
48         DelayMsgMode djm;
49  public:
50         ModuleDelayMsg(InspIRCd* Me) : Module(Me), djm(Me, this)
51         {
52                 if (!ServerInstance->Modes->AddMode(&djm))
53                         throw ModuleException("Could not add new modes!");
54                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnCleanup, I_OnUserPreMessage};
55                 ServerInstance->Modules->Attach(eventlist, this, 5);
56         }
57         virtual ~ModuleDelayMsg();
58         virtual Version GetVersion();
59         void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created);
60         void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
61         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent);
62         void OnCleanup(int target_type, void* item);
63         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list);
64 };
65
66 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
67
68 ModeAction DelayMsgMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
69 {
70         if (adding)
71         {
72                 /* Setting a new limit, sanity check */
73                 long limit = atoi(parameter.c_str());
74
75                 /* Wrap low values at 32768 */
76                 if (limit < 0)
77                         limit = 0x7FFF;
78
79                 parameter = ConvToStr(limit);
80         }
81         else
82         {
83                 /*
84                  * Clean up metadata
85                  */
86                 CUList* names = channel->GetUsers();
87                 for (CUListIter n = names->begin(); n != names->end(); ++n)
88                         n->first->Shrink("delaymsg_" + channel->name);
89         }
90         channel->SetModeParam('d', adding ? parameter : "");
91         return MODEACTION_ALLOW;
92 }
93
94 ModuleDelayMsg::~ModuleDelayMsg()
95 {
96         ServerInstance->Modes->DelMode(&djm);
97 }
98
99 Version ModuleDelayMsg::GetVersion()
100 {
101         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
102 }
103
104 void ModuleDelayMsg::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created)
105 {
106         if (channel->IsModeSet('d'))
107                 user->Extend("delaymsg_"+channel->name, reinterpret_cast<char*>(ServerInstance->Time()));
108 }
109
110 void ModuleDelayMsg::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
111 {
112         user->Shrink("delaymsg_"+channel->name);
113 }
114
115 void ModuleDelayMsg::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
116 {
117         user->Shrink("delaymsg_"+chan->name);
118 }
119
120 void ModuleDelayMsg::OnCleanup(int target_type, void* item)
121 {
122         if (target_type == TYPE_USER)
123         {
124                 User* user = (User*)item;
125                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
126                 {
127                         Channel* chan = f->first;
128                         user->Shrink("delaymsg_"+chan->name);
129                 }
130         }
131 }
132
133 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
134 {
135         /* Server origin */
136         if (!user)
137                 return MOD_RES_PASSTHRU;
138
139         if (target_type != TYPE_CHANNEL)
140                 return MOD_RES_PASSTHRU;
141
142         Channel* channel = (Channel*) dest;
143
144         void* jointime_as_ptr;
145
146         if (!user->GetExt("delaymsg_"+channel->name, jointime_as_ptr))
147                 return MOD_RES_PASSTHRU;
148
149         time_t jointime = reinterpret_cast<time_t>(jointime_as_ptr);
150
151         std::string len = channel->GetModeParameter('d');
152
153         if (jointime + atoi(len.c_str()) > ServerInstance->Time())
154         {
155                 if (channel->GetStatus(user) < STATUS_VOICE)
156                 {
157                         user->WriteNumeric(404, "%s %s :You must wait %s seconds after joining to send to channel (+d)",
158                                 user->nick.c_str(), channel->name.c_str(), len.c_str());
159                         return MOD_RES_DENY;
160                 }
161         }
162         else
163         {
164                 /* Timer has expired, we can stop checking now */
165                 user->Shrink("delaymsg_"+channel->name);
166         }
167         return MOD_RES_PASSTHRU;
168 }
169
170 MODULE_INIT(ModuleDelayMsg)
171