1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides channel mode +J (delay rejoin after kick) */
18 inline int strtoint(const std::string &str)
20 std::istringstream ss(str);
26 typedef std::map<User*, time_t> delaylist;
28 /** Handles channel mode +J
30 class KickRejoin : public ModeHandler
33 KickRejoin(InspIRCd* Instance) : ModeHandler(Instance, 'J', 1, 0, false, MODETYPE_CHANNEL, false) { }
35 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
37 if (channel->IsModeSet('J'))
38 return std::make_pair(true, channel->GetModeParameter('J'));
40 return std::make_pair(false, parameter);
43 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
45 /* When TS is equal, the alphabetically later one wins */
46 return (their_param < our_param);
49 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
53 // Taking the mode off, we need to clean up.
56 if (channel->GetExt("norejoinusers", dl))
59 channel->Shrink("norejoinusers");
62 if (!channel->IsModeSet('J'))
64 return MODEACTION_DENY;
68 channel->SetMode('J', false);
69 return MODEACTION_ALLOW;
72 else if (atoi(parameter.c_str()) > 0)
74 if (!channel->IsModeSet('J'))
76 parameter = ConvToStr(atoi(parameter.c_str()));
77 channel->SetModeParam('J', parameter.c_str(), adding);
78 channel->SetMode('J', adding);
79 return MODEACTION_ALLOW;
83 std::string cur_param = channel->GetModeParameter('J');
84 if (cur_param == parameter)
86 // mode params match, don't change mode
87 return MODEACTION_DENY;
91 // new mode param, replace old with new
92 parameter = ConvToStr(atoi(parameter.c_str()));
93 cur_param = ConvToStr(atoi(cur_param.c_str()));
96 channel->SetModeParam('J', cur_param.c_str(), false);
97 channel->SetModeParam('J', parameter.c_str(), adding);
98 return MODEACTION_ALLOW;
102 /* Fix to jamie's fix, dont allow +J 0 on the new value! */
103 return MODEACTION_DENY;
110 return MODEACTION_DENY;
115 class ModuleKickNoRejoin : public Module
122 ModuleKickNoRejoin(InspIRCd* Me)
126 kr = new KickRejoin(ServerInstance);
127 if (!ServerInstance->Modes->AddMode(kr))
128 throw ModuleException("Could not add new modes!");
129 Implementation eventlist[] = { I_OnCleanup, I_OnChannelDelete, I_OnUserPreJoin, I_OnUserKick };
130 ServerInstance->Modules->Attach(eventlist, this, 4);
133 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs)
138 if (chan->GetExt("norejoinusers", dl))
140 std::vector<User*> itemstoremove;
142 for (delaylist::iterator iter = dl->begin(); iter != dl->end(); iter++)
144 if (iter->second > time(NULL))
146 if (iter->first == user)
148 user->WriteServ( "495 %s %s :You cannot rejoin this channel yet after being kicked (+J)", user->nick, chan->name);
154 // Expired record, remove.
155 itemstoremove.push_back(iter->first);
159 for (unsigned int i = 0; i < itemstoremove.size(); i++)
160 dl->erase(itemstoremove[i]);
166 chan->Shrink("norejoinusers");
173 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
175 if (chan->IsModeSet('J') && (source != user))
178 if (!chan->GetExt("norejoinusers", dl))
181 chan->Extend("norejoinusers", dl);
183 (*dl)[user] = time(NULL) + strtoint(chan->GetModeParameter('J'));
187 virtual void OnChannelDelete(Channel* chan)
191 if (chan->GetExt("norejoinusers", dl))
194 chan->Shrink("norejoinusers");
198 virtual void OnCleanup(int target_type, void* item)
200 if(target_type == TYPE_CHANNEL)
201 OnChannelDelete((Channel*)item);
205 virtual ~ModuleKickNoRejoin()
207 ServerInstance->Modes->DelMode(kr);
211 virtual Version GetVersion()
213 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
218 MODULE_INIT(ModuleKickNoRejoin)