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 * ---------------------------------------------------
16 /* $ModDesc: Gives /cban, aka C:lines. Think Q:lines, for channels. */
20 class CBan : public classbase
33 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)
38 bool CBanComp(const CBan &ban1, const CBan &ban2);
40 typedef std::vector<CBan> cbanlist;
42 /* cbans is declared here, as our type is right above. Don't try move it. */
47 class CommandCban : public Command
50 CommandCban(InspIRCd* Me) : Command(Me, "CBAN", 'o', 1)
52 this->source = "m_cban.so";
53 this->syntax = "<channel> [<duration> :<reason>]";
54 TRANSLATE4(TR_TEXT,TR_TEXT,TR_TEXT,TR_END);
57 CmdResult Handle(const char** parameters, int pcnt, User *user)
59 /* syntax: CBAN #channel time :reason goes here */
60 /* 'time' is a human-readable timestring, like 2d3h2s. */
64 /* form: CBAN #channel removes a CBAN */
65 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
67 if (parameters[0] == iter->chname)
69 long remaining = iter->length + ServerInstance->Time();
70 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());
78 /* full form to add a CBAN */
79 if (ServerInstance->IsChannel(parameters[0]))
81 // parameters[0] = #channel
82 // parameters[1] = 1h3m2s
83 // parameters[2] = Tortoise abuser
84 long length = ServerInstance->Duration(parameters[1]);
85 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
87 cbans.push_back(CBan(parameters[0], user->nick, ServerInstance->Time(), length, reason));
89 std::sort(cbans.begin(), cbans.end(), CBanComp);
93 user->WriteServ("385 %s %s :Added %lu second channel ban (%s)", user->nick, parameters[0], length, reason.c_str());
94 ServerInstance->WriteOpers("*** %s added %lu second channel ban on %s (%s)", user->nick, length, parameters[0], reason.c_str());
98 user->WriteServ("385 %s %s :Added permanent channel ban (%s)", user->nick, parameters[0], reason.c_str());
99 ServerInstance->WriteOpers("*** %s added permanent channel ban on %s (%s)", user->nick, parameters[0], reason.c_str());
104 user->WriteServ("403 %s %s :Invalid channel name", user->nick, parameters[0]);
109 /* we want this routed! */
114 bool CBanComp(const CBan &ban1, const CBan &ban2)
116 return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
119 class ModuleCBan : public Module
121 CommandCban* mycommand;
125 ModuleCBan(InspIRCd* Me) : Module(Me)
128 mycommand = new CommandCban(Me);
129 ServerInstance->AddCommand(mycommand);
132 void Implements(char* List)
134 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
137 virtual int OnStats(char symbol, User* user, string_list &results)
143 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
145 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
146 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);
153 virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs)
157 /* check cbans in here, and apply as necessary. */
158 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
160 if(iter->chname == cname && !user->modes[UM_OPERATOR])
162 // Channel is banned.
163 user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
164 ServerInstance->WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
171 virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
173 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
175 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
179 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
181 if((target_type == TYPE_OTHER) && (extname == "cban"))
183 cbans.push_back(DecodeCBan(extdata));
184 std::sort(cbans.begin(), cbans.end(), CBanComp);
188 virtual ~ModuleCBan()
192 virtual Version GetVersion()
194 return Version(1, 1, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
197 std::string EncodeCBan(const CBan &ban)
199 std::ostringstream stream;
200 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " :" << ban.reason;
204 CBan DecodeCBan(const std::string &data)
208 irc::tokenstream tokens(data);
209 tokens.GetToken(res.chname);
210 tokens.GetToken(res.set_by);
211 tokens.GetToken(set_on);
213 tokens.GetToken(res.length);
214 tokens.GetToken(res.reason);
220 bool go_again = true;
226 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
228 /* 0 == permanent, don't mess with them! -- w00t */
229 if (iter->length != 0)
231 if (iter->set_on + iter->length <= ServerInstance->Time())
233 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());
239 if (go_again == true)
246 MODULE_INIT(ModuleCBan)