Anope IRC Services  Version 2.0
cs_suspend.cpp
Go to the documentation of this file.
1 /* ChanServ core functions
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  */
11 
12 #include "module.h"
13 #include "modules/suspend.h"
14 
16 {
17  CSSuspendInfo(Extensible *) : Serializable("CSSuspendInfo") { }
18 
20  {
21  data["chan"] << what;
22  data["by"] << by;
23  data["reason"] << reason;
24  data["time"] << when;
25  data["expires"] << expires;
26  }
27 
29  {
30  Anope::string schan;
31  data["chan"] >> schan;
32 
33  CSSuspendInfo *si;
34  if (obj)
36  else
37  {
39  if (!ci)
40  return NULL;
41  si = ci->Extend<CSSuspendInfo>("CS_SUSPENDED");
42  data["chan"] >> si->what;
43  }
44 
45  data["by"] >> si->by;
46  data["reason"] >> si->reason;
47  data["time"] >> si->when;
48  data["expires"] >> si->expires;
49  return si;
50  }
51 };
52 
53 class CommandCSSuspend : public Command
54 {
55  public:
56  CommandCSSuspend(Module *creator) : Command(creator, "chanserv/suspend", 2, 3)
57  {
58  this->SetDesc(_("Prevent a channel from being used preserving channel data and settings"));
59  this->SetSyntax(_("\037channel\037 [+\037expiry\037] [\037reason\037]"));
60  }
61 
62  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
63  {
64  const Anope::string &chan = params[0];
65  Anope::string expiry = params[1];
66  Anope::string reason = params.size() > 2 ? params[2] : "";
67  time_t expiry_secs = Config->GetModule(this->owner)->Get<time_t>("expire");
68 
69  if (!expiry.empty() && expiry[0] != '+')
70  {
71  reason = expiry + " " + reason;
72  reason.trim();
73  expiry.clear();
74  }
75  else
76  {
77  expiry_secs = Anope::DoTime(expiry);
78  if (expiry_secs == -1)
79  {
80  source.Reply(BAD_EXPIRY_TIME);
81  return;
82  }
83  }
84 
85  if (Anope::ReadOnly)
86  source.Reply(READ_ONLY_MODE);
87 
89  if (ci == NULL)
90  {
91  source.Reply(CHAN_X_NOT_REGISTERED, chan.c_str());
92  return;
93  }
94 
95  if (ci->HasExt("CS_SUSPENDED"))
96  {
97  source.Reply(_("\002%s\002 is already suspended."), ci->name.c_str());
98  return;
99  }
100 
101  CSSuspendInfo *si = ci->Extend<CSSuspendInfo>("CS_SUSPENDED");
102  si->what = ci->name;
103  si->by = source.GetNick();
104  si->reason = reason;
105  si->when = Anope::CurTime;
106  si->expires = expiry_secs ? expiry_secs + Anope::CurTime : 0;
107 
108  if (ci->c)
109  {
110  std::vector<User *> users;
111 
112  for (Channel::ChanUserList::iterator it = ci->c->users.begin(), it_end = ci->c->users.end(); it != it_end; ++it)
113  {
114  ChanUserContainer *uc = it->second;
115  User *user = uc->user;
116  if (!user->HasMode("OPER") && user->server != Me)
117  users.push_back(user);
118  }
119 
120  for (unsigned i = 0; i < users.size(); ++i)
121  ci->c->Kick(NULL, users[i], "%s", !reason.empty() ? reason.c_str() : Language::Translate(users[i], _("This channel has been suspended.")));
122  }
123 
124  Log(LOG_ADMIN, source, this, ci) << "(" << (!reason.empty() ? reason : "No reason") << "), expires on " << (expiry_secs ? Anope::strftime(Anope::CurTime + expiry_secs) : "never");
125  source.Reply(_("Channel \002%s\002 is now suspended."), ci->name.c_str());
126 
127  FOREACH_MOD(OnChanSuspend, (ci));
128  }
129 
130  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
131  {
132  this->SendSyntax(source);
133  source.Reply(" ");
134  source.Reply(_("Disallows anyone from using the given channel.\n"
135  "May be cancelled by using the \002UNSUSPEND\002\n"
136  "command to preserve all previous channel data/settings.\n"
137  "If an expiry is given the channel will be unsuspended after\n"
138  "that period of time, else the default expiry from the\n"
139  "configuration is used.\n"
140  " \n"
141  "Reason may be required on certain networks."));
142  return true;
143  }
144 };
145 
147 {
148  public:
149  CommandCSUnSuspend(Module *creator) : Command(creator, "chanserv/unsuspend", 1, 1)
150  {
151  this->SetDesc(_("Releases a suspended channel"));
152  this->SetSyntax(_("\037channel\037"));
153  }
154 
155  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
156  {
157 
158  if (Anope::ReadOnly)
159  source.Reply(READ_ONLY_MODE);
160 
161  ChannelInfo *ci = ChannelInfo::Find(params[0]);
162  if (ci == NULL)
163  {
164  source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str());
165  return;
166  }
167 
168  /* Only UNSUSPEND already suspended channels */
169  CSSuspendInfo *si = ci->GetExt<CSSuspendInfo>("CS_SUSPENDED");
170  if (!si)
171  {
172  source.Reply(_("Channel \002%s\002 isn't suspended."), ci->name.c_str());
173  return;
174  }
175 
176  Log(LOG_ADMIN, source, this, ci) << "which was suspended by " << si->by << " for: " << (!si->reason.empty() ? si->reason : "No reason");
177 
178  ci->Shrink<CSSuspendInfo>("CS_SUSPENDED");
179 
180  source.Reply(_("Channel \002%s\002 is now released."), ci->name.c_str());
181 
182  FOREACH_MOD(OnChanUnsuspend, (ci));
183 
184  return;
185  }
186 
187  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
188  {
189  this->SendSyntax(source);
190  source.Reply(" ");
191  source.Reply(_("Releases a suspended channel. All data and settings\n"
192  "are preserved from before the suspension."));
193  return true;
194  }
195 };
196 
197 class CSSuspend : public Module
198 {
203  std::vector<Anope::string> show;
204 
205  struct trim
206  {
208  {
209  return s.trim();
210  }
211  };
212 
213  bool Show(CommandSource &source, const Anope::string &what) const
214  {
215  return source.IsOper() || std::find(show.begin(), show.end(), what) != show.end();
216  }
217 
218  public:
219  CSSuspend(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
220  commandcssuspend(this), commandcsunsuspend(this), suspend(this, "CS_SUSPENDED"),
221  suspend_type("CSSuspendInfo", CSSuspendInfo::Unserialize)
222  {
223  }
224 
225  void OnChanInfo(CommandSource &source, ChannelInfo *ci, InfoFormatter &info, bool show_hidden) anope_override
226  {
227  CSSuspendInfo *si = suspend.Get(ci);
228  if (!si)
229  return;
230 
231  if (show_hidden || Show(source, "suspended"))
232  info[_("Suspended")] = _("This channel is \002suspended\002.");
233  if (!si->by.empty() && (show_hidden || Show(source, "by")))
234  info[_("Suspended by")] = si->by;
235  if (!si->reason.empty() && (show_hidden || Show(source, "reason")))
236  info[_("Suspend reason")] = si->reason;
237  if (si->when && (show_hidden || Show(source, "on")))
238  info[_("Suspended on")] = Anope::strftime(si->when, source.GetAccount(), true);
239  if (si->expires && (show_hidden || Show(source, "expires")))
240  info[_("Suspension expires")] = Anope::strftime(si->expires, source.GetAccount(), true);
241  }
242 
244  {
245  CSSuspendInfo *si = suspend.Get(ci);
246  if (!si)
247  return;
248 
249  expire = false;
250 
251  if (!si->expires)
252  return;
253 
254  if (si->expires < Anope::CurTime)
255  {
256  ci->last_used = Anope::CurTime;
257  suspend.Unset(ci);
258 
259  Log(this) << "Expiring suspend for " << ci->name;
260  }
261  }
262 
264  {
265  if (u->HasMode("OPER") || !c->ci || !suspend.HasExt(c->ci))
266  return EVENT_CONTINUE;
267 
268  reason = Language::Translate(u, _("This channel may not be used."));
269  return EVENT_STOP;
270  }
271 
273  {
274  CSSuspendInfo *si = suspend.Get(ci);
275  if (si && !source.HasCommand("chanserv/drop"))
276  {
277  source.Reply(CHAN_X_SUSPENDED, ci->name.c_str());
278  return EVENT_STOP;
279  }
280 
281  return EVENT_CONTINUE;
282  }
283 };
284 
bool HasMode(const Anope::string &name) const
Definition: users.cpp:513
CoreExport bool ReadOnly
Definition: main.cpp:28
bool Show(CommandSource &source, const Anope::string &what) const
Definition: cs_suspend.cpp:213
Definition: hashcomp.h:84
void clear()
Definition: anope.h:187
Anope::string name
Definition: regchannel.h:63
EventReturn OnCheckKick(User *u, Channel *c, Anope::string &mask, Anope::string &reason) anope_override
Definition: cs_suspend.cpp:263
time_t expires
Definition: suspend.h:14
Anope::string reason
Definition: suspend.h:13
Anope::string what
Definition: suspend.h:13
Definition: users.h:34
T * Extend(const Anope::string &name, const T &what)
Definition: extensible.h:224
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: cs_suspend.cpp:62
EventReturn OnChanDrop(CommandSource &source, ChannelInfo *ci) anope_override
Definition: cs_suspend.cpp:272
#define READ_ONLY_MODE
Definition: language.h:71
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
void Unset(Extensible *obj) anope_override
Definition: extensible.h:95
CoreExport time_t CurTime
Definition: main.cpp:41
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: cs_suspend.cpp:187
#define FOREACH_MOD(ename, args)
Definition: modules.h:62
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: cs_suspend.cpp:155
static ChannelInfo * Find(const Anope::string &name)
Definition: regchannel.cpp:630
static Serializable * Unserialize(Serializable *obj, Serialize::Data &data)
Definition: cs_suspend.cpp:28
Definition: Config.cs:26
Channel * c
Definition: regchannel.h:79
void Shrink(const Anope::string &name)
Definition: extensible.h:253
CommandCSUnSuspend commandcsunsuspend
Definition: cs_suspend.cpp:200
CommandCSSuspend commandcssuspend
Definition: cs_suspend.cpp:199
CoreExport time_t DoTime(const Anope::string &s)
Definition: misc.cpp:275
#define CHAN_X_SUSPENDED
Definition: language.h:83
time_t when
Definition: suspend.h:14
string & trim(const Anope::string &what=" \t\r\n")
Definition: anope.h:177
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
void OnPreChanExpire(ChannelInfo *ci, bool &expire) anope_override
Definition: cs_suspend.cpp:243
EventReturn
Definition: modules.h:129
#define MODULE_INIT(x)
Definition: modules.h:45
Server * server
Definition: users.h:77
CoreExport const char * Translate(const char *string)
Definition: language.cpp:59
ChanUserList users
Definition: channels.h:56
ExtensibleItem< CSSuspendInfo > suspend
Definition: cs_suspend.cpp:201
bool IsOper()
Definition: command.cpp:87
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
std::vector< Anope::string > show
Definition: cs_suspend.cpp:203
#define CHAN_X_NOT_REGISTERED
Definition: language.h:84
bool Kick(BotInfo *bi, User *u, const char *reason=NULL,...)
Definition: channels.cpp:762
T * Get(const Extensible *obj) const
Definition: extensible.h:103
T anope_dynamic_static_cast(O ptr)
Definition: anope.h:774
CoreExport Server * Me
Definition: servers.cpp:24
void SendSyntax(CommandSource &)
Definition: command.cpp:145
Serialize::Type suspend_type
Definition: cs_suspend.cpp:202
CommandCSUnSuspend(Module *creator)
Definition: cs_suspend.cpp:149
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
CSSuspend(const Anope::string &modname, const Anope::string &creator)
Definition: cs_suspend.cpp:219
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: cs_suspend.cpp:130
CommandCSSuspend(Module *creator)
Definition: cs_suspend.cpp:56
#define BAD_EXPIRY_TIME
Definition: language.h:69
T * GetExt(const Anope::string &name) const
Definition: extensible.h:213
Anope::string operator()(Anope::string s) const
Definition: cs_suspend.cpp:207
CSSuspendInfo(Extensible *)
Definition: cs_suspend.cpp:17
CoreExport Anope::string strftime(time_t t, const NickCore *nc=NULL, bool short_output=false)
Definition: misc.cpp:356
bool HasExt(const Extensible *obj) const
Definition: extensible.h:111
#define _(x)
Definition: services.h:50
bool HasExt(const Anope::string &name) const
Definition: extensible.cpp:31
Type(const Anope::string &n, unserialize_func f, Module *owner=NULL)
void OnChanInfo(CommandSource &source, ChannelInfo *ci, InfoFormatter &info, bool show_hidden) anope_override
Definition: cs_suspend.cpp:225
void Serialize(Serialize::Data &data) const anope_override
Definition: cs_suspend.cpp:19
Anope::string by
Definition: suspend.h:13
Module * owner
Definition: service.h:84