1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 class DelayMsgMode : public ModeHandler
22 DelayMsgMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, Parent, 'd', 1, 0, false, MODETYPE_CHANNEL, false, 0, '@') {};
24 ModePair ModeSet(User*, User*, Channel* channel, const std::string ¶meter)
26 std::string climit = channel->GetModeParameter('d');
29 return std::make_pair(true, climit);
33 return std::make_pair(false, parameter);
37 bool CheckTimeStamp(std::string &their_param, const std::string &our_param, Channel*)
39 return (atoi(their_param.c_str()) < atoi(our_param.c_str()));
42 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding);
45 class ModuleDelayMsg : public Module
50 ModuleDelayMsg(InspIRCd* Me) : Module(Me), djm(Me, this)
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);
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);
66 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
68 ModeAction DelayMsgMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
72 /* Setting a new limit, sanity check */
73 long limit = atoi(parameter.c_str());
75 /* Wrap low values at 32768 */
79 parameter = ConvToStr(limit);
86 CUList* names = channel->GetUsers();
87 for (CUListIter n = names->begin(); n != names->end(); ++n)
88 n->first->Shrink("delaymsg_" + channel->name);
90 channel->SetModeParam('d', adding ? parameter : "");
91 return MODEACTION_ALLOW;
94 ModuleDelayMsg::~ModuleDelayMsg()
96 ServerInstance->Modes->DelMode(&djm);
99 Version ModuleDelayMsg::GetVersion()
101 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
104 void ModuleDelayMsg::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created)
106 if (channel->IsModeSet('d'))
107 user->Extend("delaymsg_"+channel->name, reinterpret_cast<char*>(ServerInstance->Time()));
110 void ModuleDelayMsg::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
112 user->Shrink("delaymsg_"+channel->name);
115 void ModuleDelayMsg::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
117 user->Shrink("delaymsg_"+chan->name);
120 void ModuleDelayMsg::OnCleanup(int target_type, void* item)
122 if (target_type == TYPE_USER)
124 User* user = (User*)item;
125 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
127 Channel* chan = f->first;
128 user->Shrink("delaymsg_"+chan->name);
133 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
137 return MOD_RES_PASSTHRU;
139 if (target_type != TYPE_CHANNEL)
140 return MOD_RES_PASSTHRU;
142 Channel* channel = (Channel*) dest;
144 void* jointime_as_ptr;
146 if (!user->GetExt("delaymsg_"+channel->name, jointime_as_ptr))
147 return MOD_RES_PASSTHRU;
149 time_t jointime = reinterpret_cast<time_t>(jointime_as_ptr);
151 std::string len = channel->GetModeParameter('d');
153 if (jointime + atoi(len.c_str()) > ServerInstance->Time())
155 if (channel->GetStatus(user) < STATUS_VOICE)
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());
164 /* Timer has expired, we can stop checking now */
165 user->Shrink("delaymsg_"+channel->name);
167 return MOD_RES_PASSTHRU;
170 MODULE_INIT(ModuleDelayMsg)