Anope IRC Services  Version 2.0
cs_updown.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 
14 class CommandCSUp : public Command
15 {
16  void SetModes(User *u, Channel *c)
17  {
18  if (!c->ci)
19  return;
20 
21  /* whether or not we are giving modes */
22  bool giving = true;
23  /* whether or not we have given a mode */
24  bool given = false;
25  AccessGroup u_access = c->ci->AccessFor(u);
26 
27  for (unsigned i = 0; i < ModeManager::GetStatusChannelModesByRank().size(); ++i)
28  {
30  bool has_priv = u_access.HasPriv("AUTO" + cm->name) || u_access.HasPriv(cm->name);
31 
32  if (has_priv)
33  {
34  /* Always give op. If we have already given one mode, don't give more until it has a symbol */
35  if (cm->name == "OP" || !given || (giving && cm->symbol))
36  {
37  c->SetMode(NULL, cm, u->GetUID(), false);
38  /* Now if this contains a symbol don't give any more modes, to prevent setting +qaohv etc on users */
39  giving = !cm->symbol;
40  given = true;
41  }
42  }
43  }
44  }
45  public:
46  CommandCSUp(Module *creator) : Command(creator, "chanserv/up", 0, 2)
47  {
48  this->SetDesc(_("Updates a selected nicks status on a channel"));
49  this->SetSyntax(_("[\037channel\037 [\037nick\037]]"));
50  }
51 
52  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
53  {
54  if (params.empty())
55  {
56  if (!source.GetUser())
57  return;
58  for (User::ChanUserList::iterator it = source.GetUser()->chans.begin(); it != source.GetUser()->chans.end(); ++it)
59  {
60  Channel *c = it->second->chan;
61  SetModes(source.GetUser(), c);
62  }
63  Log(LOG_COMMAND, source, this, NULL) << "on all channels to update their status modes";
64  }
65  else
66  {
67  const Anope::string &channel = params[0];
68  const Anope::string &nick = params.size() > 1 ? params[1] : source.GetNick();
69 
70  Channel *c = Channel::Find(channel);
71 
72  if (c == NULL)
73  {
74  source.Reply(CHAN_X_NOT_IN_USE, channel.c_str());
75  return;
76  }
77  else if (!c->ci)
78  {
79  source.Reply(CHAN_X_NOT_REGISTERED, channel.c_str());
80  return;
81  }
82 
83  User *u = User::Find(nick, true);
84  bool override = false;
85 
86  if (u == NULL)
87  {
88  source.Reply(NICK_X_NOT_IN_USE, nick.c_str());
89  return;
90  }
91  else if (!u->FindChannel(c))
92  {
93  source.Reply(NICK_X_NOT_ON_CHAN, nick.c_str(), channel.c_str());
94  return;
95  }
96  else if (source.GetUser() && u != source.GetUser() && c->ci->HasExt("PEACE"))
97  {
98  if (c->ci->AccessFor(u) > c->ci->AccessFor(source.GetUser()))
99  {
100  if (source.HasPriv("chanserv/administration"))
101  override = true;
102  else
103  {
104  source.Reply(ACCESS_DENIED);
105  return;
106  }
107  }
108  }
109 
110  Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, c->ci) << "to update the status modes of " << u->nick;
111  SetModes(u, c);
112  }
113 
114  }
115 
116  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
117  {
118  this->SendSyntax(source);
119  source.Reply(" ");
120  source.Reply(_("Updates a selected nicks status modes on a channel. If \037nick\037 is\n"
121  "ommited then your status is updated. If \037channel\037 is ommited then\n"
122  "your channel status is updated on every channel you are in."));
123  return true;
124  }
125 };
126 
127 class CommandCSDown : public Command
128 {
129  void RemoveAll(User *u, Channel *c)
130  {
131  ChanUserContainer *cu = c->FindUser(u);
132  if (cu != NULL)
133  for (size_t i = cu->status.Modes().length(); i > 0;)
135  }
136 
137  public:
138  CommandCSDown(Module *creator) : Command(creator, "chanserv/down", 0, 2)
139  {
140  this->SetDesc(_("Removes a selected nicks status from a channel"));
141  this->SetSyntax(_("[\037channel\037 [\037nick\037]]"));
142  }
143 
144  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
145  {
146  if (params.empty())
147  {
148  if (!source.GetUser())
149  return;
150  for (User::ChanUserList::iterator it = source.GetUser()->chans.begin(); it != source.GetUser()->chans.end(); ++it)
151  {
152  Channel *c = it->second->chan;
153  RemoveAll(source.GetUser(), c);
154  }
155  Log(LOG_COMMAND, source, this, NULL) << "on all channels to remove their status modes";
156  }
157  else
158  {
159  const Anope::string &channel = params[0];
160  const Anope::string &nick = params.size() > 1 ? params[1] : source.GetNick();
161 
162  Channel *c = Channel::Find(channel);
163 
164  if (c == NULL)
165  {
166  source.Reply(CHAN_X_NOT_IN_USE, channel.c_str());
167  return;
168  }
169  else if (!c->ci)
170  {
171  source.Reply(CHAN_X_NOT_REGISTERED, channel.c_str());
172  return;
173  }
174 
175  User *u = User::Find(nick, true);
176  bool override = false;
177 
178  if (u == NULL)
179  {
180  source.Reply(NICK_X_NOT_IN_USE, nick.c_str());
181  return;
182  }
183  else if (!u->FindChannel(c))
184  {
185  source.Reply(NICK_X_NOT_ON_CHAN, nick.c_str(), channel.c_str());
186  return;
187  }
188  else if (source.GetUser() && u != source.GetUser() && c->ci->HasExt("PEACE"))
189  {
190  if (c->ci->AccessFor(u) > c->ci->AccessFor(source.GetUser()))
191  {
192  if (source.HasPriv("chanserv/administration"))
193  override = true;
194  else
195  {
196  source.Reply(ACCESS_DENIED);
197  return;
198  }
199  }
200  }
201 
202  Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, c->ci) << "to remove the status modes from " << u->nick;
203  RemoveAll(u, c);
204  }
205  }
206 
207  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
208  {
209  this->SendSyntax(source);
210  source.Reply(" ");
211  source.Reply(_("Removes a selected nicks status modes on a channel. If \037nick\037 is\n"
212  "ommited then your status is removed. If \037channel\037 is ommited then\n"
213  "your channel status is removed on every channel you are in."));
214  return true;
215  }
216 };
217 
218 class CSUpDown : public Module
219 {
222 
223  public:
224  CSUpDown(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
225  commandcsup(this), commandcsdown(this)
226  {
227 
228  }
229 };
230 
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: cs_updown.cpp:207
CommandCSUp commandcsup
Definition: cs_updown.cpp:220
#define CHAN_X_NOT_IN_USE
Definition: language.h:85
#define ACCESS_DENIED
Definition: language.h:73
Definition: users.h:34
ChanUserContainer * FindChannel(Channel *c) const
Definition: users.cpp:716
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: cs_updown.cpp:52
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
Serialize::Reference< ChannelInfo > ci
Definition: channels.h:46
CommandCSDown commandcsdown
Definition: cs_updown.cpp:221
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: cs_updown.cpp:144
bool HasPriv(const Anope::string &priv) const
Definition: access.cpp:384
#define NICK_X_NOT_IN_USE
Definition: language.h:80
size_type length() const
Definition: anope.h:131
void SetModes(User *u, Channel *c)
Definition: cs_updown.cpp:16
const Anope::string & GetUID() const
Definition: users.cpp:230
static ChannelMode * FindChannelModeByChar(char mode)
Definition: modes.cpp:524
CSUpDown(const Anope::string &modname, const Anope::string &creator)
Definition: cs_updown.cpp:224
#define anope_override
Definition: services.h:56
void RemoveAll(User *u, Channel *c)
Definition: cs_updown.cpp:129
#define MODULE_INIT(x)
Definition: modules.h:45
ChanUserContainer * FindUser(User *u) const
Definition: channels.cpp:173
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
ChannelStatus status
Definition: channels.h:28
#define CHAN_X_NOT_REGISTERED
Definition: language.h:84
Anope::string nick
Definition: users.h:62
static User * Find(const Anope::string &name, bool nick_only=false)
Definition: users.cpp:815
static Channel * Find(const Anope::string &name)
Definition: channels.cpp:920
void SetMode(BotInfo *bi, ChannelMode *cm, const Anope::string &param="", bool enforce_mlock=true)
Definition: channels.cpp:401
CommandCSUp(Module *creator)
Definition: cs_updown.cpp:46
void SendSyntax(CommandSource &)
Definition: command.cpp:145
const Anope::string & Modes() const
Definition: modes.cpp:96
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
CommandCSDown(Module *creator)
Definition: cs_updown.cpp:138
static const std::vector< ChannelModeStatus * > & GetStatusChannelModesByRank()
Definition: modes.cpp:581
Anope::string name
Definition: modes.h:43
#define _(x)
Definition: services.h:50
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: cs_updown.cpp:116
#define NICK_X_NOT_ON_CHAN
Definition: language.h:81
void RemoveMode(BotInfo *bi, ChannelMode *cm, const Anope::string &param="", bool enforce_mlock=true)
Definition: channels.cpp:459