1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 * ---------------------------------------------------
19 #include "configreader.h"
21 /* $ModDesc: Gives /cban, aka C:lines. Think Q:lines, for channels. */
25 class CBan : public classbase
38 CBan(irc::string cn, std::string sb, time_t so, long ln, std::string rs) : chname(cn), set_by(sb), set_on(so), length(ln), reason(rs)
43 bool CBanComp(const CBan &ban1, const CBan &ban2);
45 typedef std::vector<CBan> cbanlist;
47 /* cbans is declared here, as our type is right above. Don't try move it. */
52 class cmd_cban : public command_t
55 cmd_cban(InspIRCd* Me) : command_t(Me, "CBAN", 'o', 1)
57 this->source = "m_cban.so";
58 this->syntax = "<channel> [<duration> :<reason>]";
61 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
63 /* syntax: CBAN #channel time :reason goes here */
64 /* 'time' is a human-readable timestring, like 2d3h2s. */
68 /* form: CBAN #channel removes a CBAN */
69 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
71 if (parameters[0] == iter->chname)
73 long remaining = iter->length + ServerInstance->Time();
74 user->WriteServ("386 %s %s :Removed CBAN due to expire at %s (%s)", user->nick, iter->chname.c_str(), ServerInstance->TimeString(remaining).c_str(), iter->reason.c_str());
82 /* full form to add a CBAN */
83 if (ServerInstance->IsChannel(parameters[0]))
85 // parameters[0] = #channel
86 // parameters[1] = 1h3m2s
87 // parameters[2] = Tortoise abuser
88 long length = ServerInstance->Duration(parameters[1]);
89 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
91 cbans.push_back(CBan(parameters[0], user->nick, ServerInstance->Time(), length, reason));
93 std::sort(cbans.begin(), cbans.end(), CBanComp);
97 user->WriteServ("385 %s %s :Added %lu second channel ban (%s)", user->nick, parameters[0], length, reason.c_str());
98 ServerInstance->WriteOpers("*** %s added %lu second channel ban on %s (%s)", user->nick, length, parameters[0], reason.c_str());
102 user->WriteServ("385 %s %s :Added permanent channel ban (%s)", user->nick, parameters[0], reason.c_str());
103 ServerInstance->WriteOpers("*** %s added permanent channel ban on %s (%s)", user->nick, parameters[0], reason.c_str());
108 user->WriteServ("403 %s %s :Invalid channel name", user->nick, parameters[0]);
113 /* we want this routed! */
118 bool CBanComp(const CBan &ban1, const CBan &ban2)
120 return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
123 class ModuleCBan : public Module
129 ModuleCBan(InspIRCd* Me) : Module(Me)
132 mycommand = new cmd_cban(Me);
133 ServerInstance->AddCommand(mycommand);
136 void Implements(char* List)
138 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
141 virtual int OnStats(char symbol, userrec* user, string_list &results)
147 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
149 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
150 results.push_back(std::string(ServerInstance->Config->ServerName)+" 210 "+user->nick+" "+iter->chname.c_str()+" "+iter->set_by+" "+ConvToStr(iter->set_on)+" "+ConvToStr(iter->length)+" "+ConvToStr(remaining)+" :"+iter->reason);
157 virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname, std::string &privs)
161 /* check cbans in here, and apply as necessary. */
162 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
164 if(iter->chname == cname && !user->modes[UM_OPERATOR])
166 // Channel is banned.
167 user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
168 ServerInstance->WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
175 virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
177 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
179 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
183 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
185 if((target_type == TYPE_OTHER) && (extname == "cban"))
187 cbans.push_back(DecodeCBan(extdata));
188 std::sort(cbans.begin(), cbans.end(), CBanComp);
192 virtual ~ModuleCBan()
196 virtual Version GetVersion()
198 return Version(1, 1, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
201 std::string EncodeCBan(const CBan &ban)
203 std::ostringstream stream;
204 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " :" << ban.reason;
208 CBan DecodeCBan(const std::string &data)
212 irc::tokenstream tokens(data);
213 tokens.GetToken(res.chname);
214 tokens.GetToken(res.set_by);
215 tokens.GetToken(set_on);
217 tokens.GetToken(res.length);
218 tokens.GetToken(res.reason);
224 bool go_again = true;
230 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
232 /* 0 == permanent, don't mess with them! -- w00t */
233 if (iter->length != 0)
235 if (iter->set_on + iter->length <= ServerInstance->Time())
237 ServerInstance->WriteOpers("*** %li second CBAN on %s (%s) set on %s expired", iter->length, iter->chname.c_str(), iter->reason.c_str(), ServerInstance->TimeString(iter->set_on).c_str());
243 if (go_again == true)
250 MODULE_INIT(ModuleCBan)